arrow_cast/cast/
mod.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18//! Cast kernels to convert [`ArrayRef`]  between supported datatypes.
19//!
20//! See [`cast_with_options`] for more information on specific conversions.
21//!
22//! Example:
23//!
24//! ```
25//! # use arrow_array::*;
26//! # use arrow_cast::cast;
27//! # use arrow_schema::DataType;
28//! # use std::sync::Arc;
29//! # use arrow_array::types::Float64Type;
30//! # use arrow_array::cast::AsArray;
31//! // int32 to float64
32//! let a = Int32Array::from(vec![5, 6, 7]);
33//! let b = cast(&a, &DataType::Float64).unwrap();
34//! let c = b.as_primitive::<Float64Type>();
35//! assert_eq!(5.0, c.value(0));
36//! assert_eq!(6.0, c.value(1));
37//! assert_eq!(7.0, c.value(2));
38//! ```
39
40mod decimal;
41mod dictionary;
42mod list;
43mod map;
44mod string;
45use crate::cast::decimal::*;
46use crate::cast::dictionary::*;
47use crate::cast::list::*;
48use crate::cast::map::*;
49use crate::cast::string::*;
50
51use arrow_buffer::IntervalMonthDayNano;
52use arrow_data::ByteView;
53use chrono::{NaiveTime, Offset, TimeZone, Utc};
54use std::cmp::Ordering;
55use std::sync::Arc;
56
57use crate::display::{ArrayFormatter, FormatOptions};
58use crate::parse::{
59    parse_interval_day_time, parse_interval_month_day_nano, parse_interval_year_month,
60    string_to_datetime, Parser,
61};
62use arrow_array::{builder::*, cast::*, temporal_conversions::*, timezone::Tz, types::*, *};
63use arrow_buffer::{i256, ArrowNativeType, OffsetBuffer};
64use arrow_data::transform::MutableArrayData;
65use arrow_data::ArrayData;
66use arrow_schema::*;
67use arrow_select::take::take;
68use num::cast::AsPrimitive;
69use num::{NumCast, ToPrimitive};
70
71/// CastOptions provides a way to override the default cast behaviors
72#[derive(Debug, Clone, PartialEq, Eq, Hash)]
73pub struct CastOptions<'a> {
74    /// how to handle cast failures, either return NULL (safe=true) or return ERR (safe=false)
75    pub safe: bool,
76    /// Formatting options when casting from temporal types to string
77    pub format_options: FormatOptions<'a>,
78}
79
80impl Default for CastOptions<'_> {
81    fn default() -> Self {
82        Self {
83            safe: true,
84            format_options: FormatOptions::default(),
85        }
86    }
87}
88
89/// Return true if a value of type `from_type` can be cast into a value of `to_type`.
90///
91/// See [`cast_with_options`] for more information
92pub fn can_cast_types(from_type: &DataType, to_type: &DataType) -> bool {
93    use self::DataType::*;
94    use self::IntervalUnit::*;
95    use self::TimeUnit::*;
96    if from_type == to_type {
97        return true;
98    }
99
100    match (from_type, to_type) {
101        (
102            Null,
103            Boolean
104            | Int8
105            | UInt8
106            | Int16
107            | UInt16
108            | Int32
109            | UInt32
110            | Float32
111            | Date32
112            | Time32(_)
113            | Int64
114            | UInt64
115            | Float64
116            | Date64
117            | Timestamp(_, _)
118            | Time64(_)
119            | Duration(_)
120            | Interval(_)
121            | FixedSizeBinary(_)
122            | Binary
123            | Utf8
124            | LargeBinary
125            | LargeUtf8
126            | BinaryView
127            | Utf8View
128            | List(_)
129            | LargeList(_)
130            | FixedSizeList(_, _)
131            | Struct(_)
132            | Map(_, _)
133            | Dictionary(_, _),
134        ) => true,
135        // Dictionary/List conditions should be put in front of others
136        (Dictionary(_, from_value_type), Dictionary(_, to_value_type)) => {
137            can_cast_types(from_value_type, to_value_type)
138        }
139        (Dictionary(_, value_type), _) => can_cast_types(value_type, to_type),
140        (_, Dictionary(_, value_type)) => can_cast_types(from_type, value_type),
141        (List(list_from) | LargeList(list_from), List(list_to) | LargeList(list_to)) => {
142            can_cast_types(list_from.data_type(), list_to.data_type())
143        }
144        (List(list_from) | LargeList(list_from), Utf8 | LargeUtf8) => {
145            can_cast_types(list_from.data_type(), to_type)
146        }
147        (List(list_from) | LargeList(list_from), FixedSizeList(list_to, _)) => {
148            can_cast_types(list_from.data_type(), list_to.data_type())
149        }
150        (List(_), _) => false,
151        (FixedSizeList(list_from,_), List(list_to)) |
152        (FixedSizeList(list_from,_), LargeList(list_to)) => {
153            can_cast_types(list_from.data_type(), list_to.data_type())
154        }
155        (FixedSizeList(inner, size), FixedSizeList(inner_to, size_to)) if size == size_to => {
156            can_cast_types(inner.data_type(), inner_to.data_type())
157        }
158        (_, List(list_to)) => can_cast_types(from_type, list_to.data_type()),
159        (_, LargeList(list_to)) => can_cast_types(from_type, list_to.data_type()),
160        (_, FixedSizeList(list_to,size)) if *size == 1 => {
161            can_cast_types(from_type, list_to.data_type())},
162        (FixedSizeList(list_from,size), _) if *size == 1 => {
163            can_cast_types(list_from.data_type(), to_type)},
164        (Map(from_entries,ordered_from), Map(to_entries, ordered_to)) if ordered_from == ordered_to =>
165            match (key_field(from_entries), key_field(to_entries), value_field(from_entries), value_field(to_entries)) {
166                (Some(from_key), Some(to_key), Some(from_value), Some(to_value)) =>
167                    can_cast_types(from_key.data_type(), to_key.data_type()) && can_cast_types(from_value.data_type(), to_value.data_type()),
168                _ => false
169            },
170        // cast one decimal type to another decimal type
171        (Decimal128(_, _), Decimal128(_, _)) => true,
172        (Decimal256(_, _), Decimal256(_, _)) => true,
173        (Decimal128(_, _), Decimal256(_, _)) => true,
174        (Decimal256(_, _), Decimal128(_, _)) => true,
175        // unsigned integer to decimal
176        (UInt8 | UInt16 | UInt32 | UInt64, Decimal128(_, _)) |
177        (UInt8 | UInt16 | UInt32 | UInt64, Decimal256(_, _)) |
178        // signed numeric to decimal
179        (Null | Int8 | Int16 | Int32 | Int64 | Float32 | Float64, Decimal128(_, _)) |
180        (Null | Int8 | Int16 | Int32 | Int64 | Float32 | Float64, Decimal256(_, _)) |
181        // decimal to unsigned numeric
182        (Decimal128(_, _) | Decimal256(_, _), UInt8 | UInt16 | UInt32 | UInt64) |
183        // decimal to signed numeric
184        (Decimal128(_, _) | Decimal256(_, _), Null | Int8 | Int16 | Int32 | Int64 | Float32 | Float64) => true,
185        // decimal to string
186        (Decimal128(_, _) | Decimal256(_, _), Utf8View | Utf8 | LargeUtf8) => true,
187        // string to decimal
188        (Utf8View | Utf8 | LargeUtf8, Decimal128(_, _) | Decimal256(_, _)) => true,
189        (Struct(from_fields), Struct(to_fields)) => {
190            from_fields.len() == to_fields.len() &&
191                from_fields.iter().zip(to_fields.iter()).all(|(f1, f2)| {
192                    // Assume that nullability between two structs are compatible, if not,
193                    // cast kernel will return error.
194                    can_cast_types(f1.data_type(), f2.data_type())
195                })
196        }
197        (Struct(_), _) => false,
198        (_, Struct(_)) => false,
199        (_, Boolean) => {
200            DataType::is_integer(from_type)
201                || DataType::is_floating(from_type)
202                || from_type == &Utf8View
203                || from_type == &Utf8
204                || from_type == &LargeUtf8
205        }
206        (Boolean, _) => {
207            DataType::is_integer(to_type)
208                || DataType::is_floating(to_type)
209                || to_type == &Utf8View
210                || to_type == &Utf8
211                || to_type == &LargeUtf8
212        }
213
214        (Binary, LargeBinary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView | Utf8View ) => true,
215        (LargeBinary, Binary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView | Utf8View ) => true,
216        (FixedSizeBinary(_), Binary | LargeBinary) => true,
217        (
218            Utf8 | LargeUtf8 | Utf8View,
219            Binary
220            | LargeBinary
221            | Utf8
222            | LargeUtf8
223            | Date32
224            | Date64
225            | Time32(Second)
226            | Time32(Millisecond)
227            | Time64(Microsecond)
228            | Time64(Nanosecond)
229            | Timestamp(Second, _)
230            | Timestamp(Millisecond, _)
231            | Timestamp(Microsecond, _)
232            | Timestamp(Nanosecond, _)
233            | Interval(_)
234            | BinaryView,
235        ) => true,
236        (Utf8 | LargeUtf8, Utf8View) => true,
237        (BinaryView, Binary | LargeBinary | Utf8 | LargeUtf8 | Utf8View) => true,
238        (Utf8View | Utf8 | LargeUtf8, _) => to_type.is_numeric() && to_type != &Float16,
239        (_, Utf8 | LargeUtf8) => from_type.is_primitive(),
240        (_, Utf8View) => from_type.is_numeric(),
241
242        (_, Binary | LargeBinary) => from_type.is_integer(),
243
244        // start numeric casts
245        (
246            UInt8 | UInt16 | UInt32 | UInt64 | Int8 | Int16 | Int32 | Int64 | Float16 | Float32 | Float64,
247            UInt8 | UInt16 | UInt32 | UInt64 | Int8 | Int16 | Int32 | Int64 | Float16 | Float32 | Float64,
248        ) => true,
249        // end numeric casts
250
251        // temporal casts
252        (Int32, Date32 | Date64 | Time32(_)) => true,
253        (Date32, Int32 | Int64) => true,
254        (Time32(_), Int32) => true,
255        (Int64, Date64 | Date32 | Time64(_)) => true,
256        (Date64, Int64 | Int32) => true,
257        (Time64(_), Int64) => true,
258        (Date32 | Date64, Date32 | Date64) => true,
259        // time casts
260        (Time32(_), Time32(_)) => true,
261        (Time32(_), Time64(_)) => true,
262        (Time64(_), Time64(_)) => true,
263        (Time64(_), Time32(to_unit)) => {
264            matches!(to_unit, Second | Millisecond)
265        }
266        (Timestamp(_, _), _) if to_type.is_numeric() => true,
267        (_, Timestamp(_, _)) if from_type.is_numeric() => true,
268        (Date64, Timestamp(_, None)) => true,
269        (Date32, Timestamp(_, None)) => true,
270        (
271            Timestamp(_, _),
272            Timestamp(_, _)
273            | Date32
274            | Date64
275            | Time32(Second)
276            | Time32(Millisecond)
277            | Time64(Microsecond)
278            | Time64(Nanosecond),
279        ) => true,
280        (_, Duration(_)) if from_type.is_numeric() => true,
281        (Duration(_), _) if to_type.is_numeric() => true,
282        (Duration(_), Duration(_)) => true,
283        (Interval(from_type), Int64) => {
284            match from_type {
285                YearMonth => true,
286                DayTime => true,
287                MonthDayNano => false, // Native type is i128
288            }
289        }
290        (Int32, Interval(to_type)) => match to_type {
291            YearMonth => true,
292            DayTime => false,
293            MonthDayNano => false,
294        },
295        (Duration(_), Interval(MonthDayNano)) => true,
296        (Interval(MonthDayNano), Duration(_)) => true,
297        (Interval(YearMonth), Interval(MonthDayNano)) => true,
298        (Interval(DayTime), Interval(MonthDayNano)) => true,
299        (_, _) => false,
300    }
301}
302
303/// Cast `array` to the provided data type and return a new Array with type `to_type`, if possible.
304///
305/// See [`cast_with_options`] for more information
306pub fn cast(array: &dyn Array, to_type: &DataType) -> Result<ArrayRef, ArrowError> {
307    cast_with_options(array, to_type, &CastOptions::default())
308}
309
310fn cast_integer_to_decimal<
311    T: ArrowPrimitiveType,
312    D: DecimalType + ArrowPrimitiveType<Native = M>,
313    M,
314>(
315    array: &PrimitiveArray<T>,
316    precision: u8,
317    scale: i8,
318    base: M,
319    cast_options: &CastOptions,
320) -> Result<ArrayRef, ArrowError>
321where
322    <T as ArrowPrimitiveType>::Native: AsPrimitive<M>,
323    M: ArrowNativeTypeOp,
324{
325    let scale_factor = base.pow_checked(scale.unsigned_abs() as u32).map_err(|_| {
326        ArrowError::CastError(format!(
327            "Cannot cast to {:?}({}, {}). The scale causes overflow.",
328            D::PREFIX,
329            precision,
330            scale,
331        ))
332    })?;
333
334    let array = if scale < 0 {
335        match cast_options.safe {
336            true => array.unary_opt::<_, D>(|v| {
337                v.as_()
338                    .div_checked(scale_factor)
339                    .ok()
340                    .and_then(|v| (D::is_valid_decimal_precision(v, precision)).then_some(v))
341            }),
342            false => array.try_unary::<_, D, _>(|v| {
343                v.as_()
344                    .div_checked(scale_factor)
345                    .and_then(|v| D::validate_decimal_precision(v, precision).map(|_| v))
346            })?,
347        }
348    } else {
349        match cast_options.safe {
350            true => array.unary_opt::<_, D>(|v| {
351                v.as_()
352                    .mul_checked(scale_factor)
353                    .ok()
354                    .and_then(|v| (D::is_valid_decimal_precision(v, precision)).then_some(v))
355            }),
356            false => array.try_unary::<_, D, _>(|v| {
357                v.as_()
358                    .mul_checked(scale_factor)
359                    .and_then(|v| D::validate_decimal_precision(v, precision).map(|_| v))
360            })?,
361        }
362    };
363
364    Ok(Arc::new(array.with_precision_and_scale(precision, scale)?))
365}
366
367/// Cast the array from interval year month to month day nano
368fn cast_interval_year_month_to_interval_month_day_nano(
369    array: &dyn Array,
370    _cast_options: &CastOptions,
371) -> Result<ArrayRef, ArrowError> {
372    let array = array.as_primitive::<IntervalYearMonthType>();
373
374    Ok(Arc::new(array.unary::<_, IntervalMonthDayNanoType>(|v| {
375        let months = IntervalYearMonthType::to_months(v);
376        IntervalMonthDayNanoType::make_value(months, 0, 0)
377    })))
378}
379
380/// Cast the array from interval day time to month day nano
381fn cast_interval_day_time_to_interval_month_day_nano(
382    array: &dyn Array,
383    _cast_options: &CastOptions,
384) -> Result<ArrayRef, ArrowError> {
385    let array = array.as_primitive::<IntervalDayTimeType>();
386    let mul = 1_000_000;
387
388    Ok(Arc::new(array.unary::<_, IntervalMonthDayNanoType>(|v| {
389        let (days, ms) = IntervalDayTimeType::to_parts(v);
390        IntervalMonthDayNanoType::make_value(0, days, ms as i64 * mul)
391    })))
392}
393
394/// Cast the array from interval to duration
395fn cast_month_day_nano_to_duration<D: ArrowTemporalType<Native = i64>>(
396    array: &dyn Array,
397    cast_options: &CastOptions,
398) -> Result<ArrayRef, ArrowError> {
399    let array = array.as_primitive::<IntervalMonthDayNanoType>();
400    let scale = match D::DATA_TYPE {
401        DataType::Duration(TimeUnit::Second) => 1_000_000_000,
402        DataType::Duration(TimeUnit::Millisecond) => 1_000_000,
403        DataType::Duration(TimeUnit::Microsecond) => 1_000,
404        DataType::Duration(TimeUnit::Nanosecond) => 1,
405        _ => unreachable!(),
406    };
407
408    if cast_options.safe {
409        let iter = array.iter().map(|v| {
410            v.and_then(|v| (v.days == 0 && v.months == 0).then_some(v.nanoseconds / scale))
411        });
412        Ok(Arc::new(unsafe {
413            PrimitiveArray::<D>::from_trusted_len_iter(iter)
414        }))
415    } else {
416        let vec = array
417            .iter()
418            .map(|v| {
419                v.map(|v| match v.days == 0 && v.months == 0 {
420                    true => Ok((v.nanoseconds) / scale),
421                    _ => Err(ArrowError::ComputeError(
422                        "Cannot convert interval containing non-zero months or days to duration"
423                            .to_string(),
424                    )),
425                })
426                .transpose()
427            })
428            .collect::<Result<Vec<_>, _>>()?;
429        Ok(Arc::new(unsafe {
430            PrimitiveArray::<D>::from_trusted_len_iter(vec.iter())
431        }))
432    }
433}
434
435/// Cast the array from duration and interval
436fn cast_duration_to_interval<D: ArrowTemporalType<Native = i64>>(
437    array: &dyn Array,
438    cast_options: &CastOptions,
439) -> Result<ArrayRef, ArrowError> {
440    let array = array
441        .as_any()
442        .downcast_ref::<PrimitiveArray<D>>()
443        .ok_or_else(|| {
444            ArrowError::ComputeError(
445                "Internal Error: Cannot cast duration to DurationArray of expected type"
446                    .to_string(),
447            )
448        })?;
449
450    let scale = match array.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| {
461                v.checked_mul(scale)
462                    .map(|v| IntervalMonthDayNano::new(0, 0, v))
463            })
464        });
465        Ok(Arc::new(unsafe {
466            PrimitiveArray::<IntervalMonthDayNanoType>::from_trusted_len_iter(iter)
467        }))
468    } else {
469        let vec = array
470            .iter()
471            .map(|v| {
472                v.map(|v| {
473                    if let Ok(v) = v.mul_checked(scale) {
474                        Ok(IntervalMonthDayNano::new(0, 0, v))
475                    } else {
476                        Err(ArrowError::ComputeError(format!(
477                            "Cannot cast to {:?}. Overflowing on {:?}",
478                            IntervalMonthDayNanoType::DATA_TYPE,
479                            v
480                        )))
481                    }
482                })
483                .transpose()
484            })
485            .collect::<Result<Vec<_>, _>>()?;
486        Ok(Arc::new(unsafe {
487            PrimitiveArray::<IntervalMonthDayNanoType>::from_trusted_len_iter(vec.iter())
488        }))
489    }
490}
491
492/// Cast the primitive array using [`PrimitiveArray::reinterpret_cast`]
493fn cast_reinterpret_arrays<I: ArrowPrimitiveType, O: ArrowPrimitiveType<Native = I::Native>>(
494    array: &dyn Array,
495) -> Result<ArrayRef, ArrowError> {
496    Ok(Arc::new(array.as_primitive::<I>().reinterpret_cast::<O>()))
497}
498
499fn make_timestamp_array(
500    array: &PrimitiveArray<Int64Type>,
501    unit: TimeUnit,
502    tz: Option<Arc<str>>,
503) -> ArrayRef {
504    match unit {
505        TimeUnit::Second => Arc::new(
506            array
507                .reinterpret_cast::<TimestampSecondType>()
508                .with_timezone_opt(tz),
509        ),
510        TimeUnit::Millisecond => Arc::new(
511            array
512                .reinterpret_cast::<TimestampMillisecondType>()
513                .with_timezone_opt(tz),
514        ),
515        TimeUnit::Microsecond => Arc::new(
516            array
517                .reinterpret_cast::<TimestampMicrosecondType>()
518                .with_timezone_opt(tz),
519        ),
520        TimeUnit::Nanosecond => Arc::new(
521            array
522                .reinterpret_cast::<TimestampNanosecondType>()
523                .with_timezone_opt(tz),
524        ),
525    }
526}
527
528fn make_duration_array(array: &PrimitiveArray<Int64Type>, unit: TimeUnit) -> ArrayRef {
529    match unit {
530        TimeUnit::Second => Arc::new(array.reinterpret_cast::<DurationSecondType>()),
531        TimeUnit::Millisecond => Arc::new(array.reinterpret_cast::<DurationMillisecondType>()),
532        TimeUnit::Microsecond => Arc::new(array.reinterpret_cast::<DurationMicrosecondType>()),
533        TimeUnit::Nanosecond => Arc::new(array.reinterpret_cast::<DurationNanosecondType>()),
534    }
535}
536
537fn as_time_res_with_timezone<T: ArrowPrimitiveType>(
538    v: i64,
539    tz: Option<Tz>,
540) -> Result<NaiveTime, ArrowError> {
541    let time = match tz {
542        Some(tz) => as_datetime_with_timezone::<T>(v, tz).map(|d| d.time()),
543        None => as_datetime::<T>(v).map(|d| d.time()),
544    };
545
546    time.ok_or_else(|| {
547        ArrowError::CastError(format!(
548            "Failed to create naive time with {} {}",
549            std::any::type_name::<T>(),
550            v
551        ))
552    })
553}
554
555fn timestamp_to_date32<T: ArrowTimestampType>(
556    array: &PrimitiveArray<T>,
557) -> Result<ArrayRef, ArrowError> {
558    let err = |x: i64| {
559        ArrowError::CastError(format!(
560            "Cannot convert {} {x} to datetime",
561            std::any::type_name::<T>()
562        ))
563    };
564
565    let array: Date32Array = match array.timezone() {
566        Some(tz) => {
567            let tz: Tz = tz.parse()?;
568            array.try_unary(|x| {
569                as_datetime_with_timezone::<T>(x, tz)
570                    .ok_or_else(|| err(x))
571                    .map(|d| Date32Type::from_naive_date(d.date_naive()))
572            })?
573        }
574        None => array.try_unary(|x| {
575            as_datetime::<T>(x)
576                .ok_or_else(|| err(x))
577                .map(|d| Date32Type::from_naive_date(d.date()))
578        })?,
579    };
580    Ok(Arc::new(array))
581}
582
583/// Try to cast `array` to `to_type` if possible.
584///
585/// Returns a new Array with type `to_type` if possible.
586///
587/// Accepts [`CastOptions`] to specify cast behavior. See also [`cast()`].
588///
589/// # Behavior
590/// * `Boolean` to `Utf8`: `true` => '1', `false` => `0`
591/// * `Utf8` to `Boolean`: `true`, `yes`, `on`, `1` => `true`, `false`, `no`, `off`, `0` => `false`,
592///   short variants are accepted, other strings return null or error
593/// * `Utf8` to Numeric: strings that can't be parsed to numbers return null, float strings
594///   in integer casts return null
595/// * Numeric to `Boolean`: 0 returns `false`, any other value returns `true`
596/// * `List` to `List`: the underlying data type is cast
597/// * `List` to `FixedSizeList`: the underlying data type is cast. If safe is true and a list element
598///   has the wrong length it will be replaced with NULL, otherwise an error will be returned
599/// * Primitive to `List`: a list array with 1 value per slot is created
600/// * `Date32` and `Date64`: precision lost when going to higher interval
601/// * `Time32 and `Time64`: precision lost when going to higher interval
602/// * `Timestamp` and `Date{32|64}`: precision lost when going to higher interval
603/// * Temporal to/from backing Primitive: zero-copy with data type change
604/// * `Float32/Float64` to `Decimal(precision, scale)` rounds to the `scale` decimals
605///   (i.e. casting `6.4999` to `Decimal(10, 1)` becomes `6.5`).
606///
607/// Unsupported Casts (check with `can_cast_types` before calling):
608/// * To or from `StructArray`
609/// * `List` to `Primitive`
610/// * `Interval` and `Duration`
611///
612/// # Timestamps and Timezones
613///
614/// Timestamps are stored with an optional timezone in Arrow.
615///
616/// ## Casting timestamps to a timestamp without timezone / UTC
617/// ```
618/// # use arrow_array::Int64Array;
619/// # use arrow_array::types::TimestampSecondType;
620/// # use arrow_cast::{cast, display};
621/// # use arrow_array::cast::AsArray;
622/// # use arrow_schema::{DataType, TimeUnit};
623/// // can use "UTC" if chrono-tz feature is enabled, here use offset based timezone
624/// let data_type = DataType::Timestamp(TimeUnit::Second, None);
625/// let a = Int64Array::from(vec![1_000_000_000, 2_000_000_000, 3_000_000_000]);
626/// let b = cast(&a, &data_type).unwrap();
627/// let b = b.as_primitive::<TimestampSecondType>(); // downcast to result type
628/// assert_eq!(2_000_000_000, b.value(1)); // values are the same as the type has no timezone
629/// // use display to show them (note has no trailing Z)
630/// assert_eq!("2033-05-18T03:33:20", display::array_value_to_string(&b, 1).unwrap());
631/// ```
632///
633/// ## Casting timestamps to a timestamp with timezone
634///
635/// Similarly to the previous example, if you cast numeric values to a timestamp
636/// with timezone, the cast kernel will not change the underlying values
637/// but display and other functions will interpret them as being in the provided timezone.
638///
639/// ```
640/// # use arrow_array::Int64Array;
641/// # use arrow_array::types::TimestampSecondType;
642/// # use arrow_cast::{cast, display};
643/// # use arrow_array::cast::AsArray;
644/// # use arrow_schema::{DataType, TimeUnit};
645/// // can use "Americas/New_York" if chrono-tz feature is enabled, here use offset based timezone
646/// let data_type = DataType::Timestamp(TimeUnit::Second, Some("-05:00".into()));
647/// let a = Int64Array::from(vec![1_000_000_000, 2_000_000_000, 3_000_000_000]);
648/// let b = cast(&a, &data_type).unwrap();
649/// let b = b.as_primitive::<TimestampSecondType>(); // downcast to result type
650/// assert_eq!(2_000_000_000, b.value(1)); // values are still the same
651/// // displayed in the target timezone (note the offset -05:00)
652/// assert_eq!("2033-05-17T22:33:20-05:00", display::array_value_to_string(&b, 1).unwrap());
653/// ```
654/// # Casting timestamps without timezone to timestamps with timezone
655///
656/// When casting from a timestamp without timezone to a timestamp with
657/// timezone, the cast kernel interprets the timestamp values as being in
658/// the destination timezone and then adjusts the underlying value to UTC as required
659///
660/// However, note that when casting from a timestamp with timezone BACK to a
661/// timestamp without timezone the cast kernel does not adjust the values.
662///
663/// Thus round trip casting a timestamp without timezone to a timestamp with
664/// timezone and back to a timestamp without timezone results in different
665/// values than the starting values.
666///
667/// ```
668/// # use arrow_array::Int64Array;
669/// # use arrow_array::types::{TimestampSecondType};
670/// # use arrow_cast::{cast, display};
671/// # use arrow_array::cast::AsArray;
672/// # use arrow_schema::{DataType, TimeUnit};
673/// let data_type  = DataType::Timestamp(TimeUnit::Second, None);
674/// let data_type_tz = DataType::Timestamp(TimeUnit::Second, Some("-05:00".into()));
675/// let a = Int64Array::from(vec![1_000_000_000, 2_000_000_000, 3_000_000_000]);
676/// let b = cast(&a, &data_type).unwrap(); // cast to timestamp without timezone
677/// let b = b.as_primitive::<TimestampSecondType>(); // downcast to result type
678/// assert_eq!(2_000_000_000, b.value(1)); // values are still the same
679/// // displayed without a timezone (note lack of offset or Z)
680/// assert_eq!("2033-05-18T03:33:20", display::array_value_to_string(&b, 1).unwrap());
681///
682/// // Convert timestamps without a timezone to timestamps with a timezone
683/// let c = cast(&b, &data_type_tz).unwrap();
684/// let c = c.as_primitive::<TimestampSecondType>(); // downcast to result type
685/// assert_eq!(2_000_018_000, c.value(1)); // value has been adjusted by offset
686/// // displayed with the target timezone offset (-05:00)
687/// assert_eq!("2033-05-18T03:33:20-05:00", display::array_value_to_string(&c, 1).unwrap());
688///
689/// // Convert from timestamp with timezone back to timestamp without timezone
690/// let d = cast(&c, &data_type).unwrap();
691/// let d = d.as_primitive::<TimestampSecondType>(); // downcast to result type
692/// assert_eq!(2_000_018_000, d.value(1)); // value has not been adjusted
693/// // NOTE: the timestamp is adjusted (08:33:20 instead of 03:33:20 as in previous example)
694/// assert_eq!("2033-05-18T08:33:20", display::array_value_to_string(&d, 1).unwrap());
695/// ```
696pub fn cast_with_options(
697    array: &dyn Array,
698    to_type: &DataType,
699    cast_options: &CastOptions,
700) -> Result<ArrayRef, ArrowError> {
701    use DataType::*;
702    let from_type = array.data_type();
703    // clone array if types are the same
704    if from_type == to_type {
705        return Ok(make_array(array.to_data()));
706    }
707    match (from_type, to_type) {
708        (
709            Null,
710            Boolean
711            | Int8
712            | UInt8
713            | Int16
714            | UInt16
715            | Int32
716            | UInt32
717            | Float32
718            | Date32
719            | Time32(_)
720            | Int64
721            | UInt64
722            | Float64
723            | Date64
724            | Timestamp(_, _)
725            | Time64(_)
726            | Duration(_)
727            | Interval(_)
728            | FixedSizeBinary(_)
729            | Binary
730            | Utf8
731            | LargeBinary
732            | LargeUtf8
733            | BinaryView
734            | Utf8View
735            | List(_)
736            | LargeList(_)
737            | FixedSizeList(_, _)
738            | Struct(_)
739            | Map(_, _)
740            | Dictionary(_, _),
741        ) => Ok(new_null_array(to_type, array.len())),
742        (Dictionary(index_type, _), _) => match **index_type {
743            Int8 => dictionary_cast::<Int8Type>(array, to_type, cast_options),
744            Int16 => dictionary_cast::<Int16Type>(array, to_type, cast_options),
745            Int32 => dictionary_cast::<Int32Type>(array, to_type, cast_options),
746            Int64 => dictionary_cast::<Int64Type>(array, to_type, cast_options),
747            UInt8 => dictionary_cast::<UInt8Type>(array, to_type, cast_options),
748            UInt16 => dictionary_cast::<UInt16Type>(array, to_type, cast_options),
749            UInt32 => dictionary_cast::<UInt32Type>(array, to_type, cast_options),
750            UInt64 => dictionary_cast::<UInt64Type>(array, to_type, cast_options),
751            _ => Err(ArrowError::CastError(format!(
752                "Casting from dictionary type {from_type:?} to {to_type:?} not supported",
753            ))),
754        },
755        (_, Dictionary(index_type, value_type)) => match **index_type {
756            Int8 => cast_to_dictionary::<Int8Type>(array, value_type, cast_options),
757            Int16 => cast_to_dictionary::<Int16Type>(array, value_type, cast_options),
758            Int32 => cast_to_dictionary::<Int32Type>(array, value_type, cast_options),
759            Int64 => cast_to_dictionary::<Int64Type>(array, value_type, cast_options),
760            UInt8 => cast_to_dictionary::<UInt8Type>(array, value_type, cast_options),
761            UInt16 => cast_to_dictionary::<UInt16Type>(array, value_type, cast_options),
762            UInt32 => cast_to_dictionary::<UInt32Type>(array, value_type, cast_options),
763            UInt64 => cast_to_dictionary::<UInt64Type>(array, value_type, cast_options),
764            _ => Err(ArrowError::CastError(format!(
765                "Casting from type {from_type:?} to dictionary type {to_type:?} not supported",
766            ))),
767        },
768        (List(_), List(to)) => cast_list_values::<i32>(array, to, cast_options),
769        (LargeList(_), LargeList(to)) => cast_list_values::<i64>(array, to, cast_options),
770        (List(_), LargeList(list_to)) => cast_list::<i32, i64>(array, list_to, cast_options),
771        (LargeList(_), List(list_to)) => cast_list::<i64, i32>(array, list_to, cast_options),
772        (List(_), FixedSizeList(field, size)) => {
773            let array = array.as_list::<i32>();
774            cast_list_to_fixed_size_list::<i32>(array, field, *size, cast_options)
775        }
776        (LargeList(_), FixedSizeList(field, size)) => {
777            let array = array.as_list::<i64>();
778            cast_list_to_fixed_size_list::<i64>(array, field, *size, cast_options)
779        }
780        (List(_) | LargeList(_), _) => match to_type {
781            Utf8 => value_to_string::<i32>(array, cast_options),
782            LargeUtf8 => value_to_string::<i64>(array, cast_options),
783            _ => Err(ArrowError::CastError(
784                "Cannot cast list to non-list data types".to_string(),
785            )),
786        },
787        (FixedSizeList(list_from, size), List(list_to)) => {
788            if list_to.data_type() != list_from.data_type() {
789                // To transform inner type, can first cast to FSL with new inner type.
790                let fsl_to = DataType::FixedSizeList(list_to.clone(), *size);
791                let array = cast_with_options(array, &fsl_to, cast_options)?;
792                cast_fixed_size_list_to_list::<i32>(array.as_ref())
793            } else {
794                cast_fixed_size_list_to_list::<i32>(array)
795            }
796        }
797        (FixedSizeList(list_from, size), LargeList(list_to)) => {
798            if list_to.data_type() != list_from.data_type() {
799                // To transform inner type, can first cast to FSL with new inner type.
800                let fsl_to = DataType::FixedSizeList(list_to.clone(), *size);
801                let array = cast_with_options(array, &fsl_to, cast_options)?;
802                cast_fixed_size_list_to_list::<i64>(array.as_ref())
803            } else {
804                cast_fixed_size_list_to_list::<i64>(array)
805            }
806        }
807        (FixedSizeList(_, size_from), FixedSizeList(list_to, size_to)) => {
808            if size_from != size_to {
809                return Err(ArrowError::CastError(
810                    "cannot cast fixed-size-list to fixed-size-list with different size".into(),
811                ));
812            }
813            let array = array.as_any().downcast_ref::<FixedSizeListArray>().unwrap();
814            let values = cast_with_options(array.values(), list_to.data_type(), cast_options)?;
815            Ok(Arc::new(FixedSizeListArray::try_new(
816                list_to.clone(),
817                *size_from,
818                values,
819                array.nulls().cloned(),
820            )?))
821        }
822        (_, List(ref to)) => cast_values_to_list::<i32>(array, to, cast_options),
823        (_, LargeList(ref to)) => cast_values_to_list::<i64>(array, to, cast_options),
824        (_, FixedSizeList(ref to, size)) if *size == 1 => {
825            cast_values_to_fixed_size_list(array, to, *size, cast_options)
826        }
827        (FixedSizeList(_, size), _) if *size == 1 => {
828            cast_single_element_fixed_size_list_to_values(array, to_type, cast_options)
829        }
830        (Map(_, ordered1), Map(_, ordered2)) if ordered1 == ordered2 => {
831            cast_map_values(array.as_map(), to_type, cast_options, ordered1.to_owned())
832        }
833        // Decimal to decimal, same width
834        (Decimal128(p1, s1), Decimal128(p2, s2)) => {
835            cast_decimal_to_decimal_same_type::<Decimal128Type>(
836                array.as_primitive(),
837                *p1,
838                *s1,
839                *p2,
840                *s2,
841                cast_options,
842            )
843        }
844        (Decimal256(p1, s1), Decimal256(p2, s2)) => {
845            cast_decimal_to_decimal_same_type::<Decimal256Type>(
846                array.as_primitive(),
847                *p1,
848                *s1,
849                *p2,
850                *s2,
851                cast_options,
852            )
853        }
854        // Decimal to decimal, different width
855        (Decimal128(_, s1), Decimal256(p2, s2)) => {
856            cast_decimal_to_decimal::<Decimal128Type, Decimal256Type>(
857                array.as_primitive(),
858                *s1,
859                *p2,
860                *s2,
861                cast_options,
862            )
863        }
864        (Decimal256(_, s1), Decimal128(p2, s2)) => {
865            cast_decimal_to_decimal::<Decimal256Type, Decimal128Type>(
866                array.as_primitive(),
867                *s1,
868                *p2,
869                *s2,
870                cast_options,
871            )
872        }
873        // Decimal to non-decimal
874        (Decimal128(_, scale), _) if !to_type.is_temporal() => {
875            cast_from_decimal::<Decimal128Type, _>(
876                array,
877                10_i128,
878                scale,
879                from_type,
880                to_type,
881                |x: i128| x as f64,
882                cast_options,
883            )
884        }
885        (Decimal256(_, scale), _) if !to_type.is_temporal() => {
886            cast_from_decimal::<Decimal256Type, _>(
887                array,
888                i256::from_i128(10_i128),
889                scale,
890                from_type,
891                to_type,
892                |x: i256| x.to_f64().unwrap(),
893                cast_options,
894            )
895        }
896        // Non-decimal to decimal
897        (_, Decimal128(precision, scale)) if !from_type.is_temporal() => {
898            cast_to_decimal::<Decimal128Type, _>(
899                array,
900                10_i128,
901                precision,
902                scale,
903                from_type,
904                to_type,
905                cast_options,
906            )
907        }
908        (_, Decimal256(precision, scale)) if !from_type.is_temporal() => {
909            cast_to_decimal::<Decimal256Type, _>(
910                array,
911                i256::from_i128(10_i128),
912                precision,
913                scale,
914                from_type,
915                to_type,
916                cast_options,
917            )
918        }
919        (Struct(_), Struct(to_fields)) => {
920            let array = array.as_struct();
921            let fields = array
922                .columns()
923                .iter()
924                .zip(to_fields.iter())
925                .map(|(l, field)| cast_with_options(l, field.data_type(), cast_options))
926                .collect::<Result<Vec<ArrayRef>, ArrowError>>()?;
927            let array = StructArray::try_new(to_fields.clone(), fields, array.nulls().cloned())?;
928            Ok(Arc::new(array) as ArrayRef)
929        }
930        (Struct(_), _) => Err(ArrowError::CastError(format!(
931            "Casting from {from_type:?} to {to_type:?} not supported"
932        ))),
933        (_, Struct(_)) => Err(ArrowError::CastError(format!(
934            "Casting from {from_type:?} to {to_type:?} not supported"
935        ))),
936        (_, Boolean) => match from_type {
937            UInt8 => cast_numeric_to_bool::<UInt8Type>(array),
938            UInt16 => cast_numeric_to_bool::<UInt16Type>(array),
939            UInt32 => cast_numeric_to_bool::<UInt32Type>(array),
940            UInt64 => cast_numeric_to_bool::<UInt64Type>(array),
941            Int8 => cast_numeric_to_bool::<Int8Type>(array),
942            Int16 => cast_numeric_to_bool::<Int16Type>(array),
943            Int32 => cast_numeric_to_bool::<Int32Type>(array),
944            Int64 => cast_numeric_to_bool::<Int64Type>(array),
945            Float16 => cast_numeric_to_bool::<Float16Type>(array),
946            Float32 => cast_numeric_to_bool::<Float32Type>(array),
947            Float64 => cast_numeric_to_bool::<Float64Type>(array),
948            Utf8View => cast_utf8view_to_boolean(array, cast_options),
949            Utf8 => cast_utf8_to_boolean::<i32>(array, cast_options),
950            LargeUtf8 => cast_utf8_to_boolean::<i64>(array, cast_options),
951            _ => Err(ArrowError::CastError(format!(
952                "Casting from {from_type:?} to {to_type:?} not supported",
953            ))),
954        },
955        (Boolean, _) => match to_type {
956            UInt8 => cast_bool_to_numeric::<UInt8Type>(array, cast_options),
957            UInt16 => cast_bool_to_numeric::<UInt16Type>(array, cast_options),
958            UInt32 => cast_bool_to_numeric::<UInt32Type>(array, cast_options),
959            UInt64 => cast_bool_to_numeric::<UInt64Type>(array, cast_options),
960            Int8 => cast_bool_to_numeric::<Int8Type>(array, cast_options),
961            Int16 => cast_bool_to_numeric::<Int16Type>(array, cast_options),
962            Int32 => cast_bool_to_numeric::<Int32Type>(array, cast_options),
963            Int64 => cast_bool_to_numeric::<Int64Type>(array, cast_options),
964            Float16 => cast_bool_to_numeric::<Float16Type>(array, cast_options),
965            Float32 => cast_bool_to_numeric::<Float32Type>(array, cast_options),
966            Float64 => cast_bool_to_numeric::<Float64Type>(array, cast_options),
967            Utf8View => value_to_string_view(array, cast_options),
968            Utf8 => value_to_string::<i32>(array, cast_options),
969            LargeUtf8 => value_to_string::<i64>(array, cast_options),
970            _ => Err(ArrowError::CastError(format!(
971                "Casting from {from_type:?} to {to_type:?} not supported",
972            ))),
973        },
974        (Utf8, _) => match to_type {
975            UInt8 => parse_string::<UInt8Type, i32>(array, cast_options),
976            UInt16 => parse_string::<UInt16Type, i32>(array, cast_options),
977            UInt32 => parse_string::<UInt32Type, i32>(array, cast_options),
978            UInt64 => parse_string::<UInt64Type, i32>(array, cast_options),
979            Int8 => parse_string::<Int8Type, i32>(array, cast_options),
980            Int16 => parse_string::<Int16Type, i32>(array, cast_options),
981            Int32 => parse_string::<Int32Type, i32>(array, cast_options),
982            Int64 => parse_string::<Int64Type, i32>(array, cast_options),
983            Float32 => parse_string::<Float32Type, i32>(array, cast_options),
984            Float64 => parse_string::<Float64Type, i32>(array, cast_options),
985            Date32 => parse_string::<Date32Type, i32>(array, cast_options),
986            Date64 => parse_string::<Date64Type, i32>(array, cast_options),
987            Binary => Ok(Arc::new(BinaryArray::from(
988                array.as_string::<i32>().clone(),
989            ))),
990            LargeBinary => {
991                let binary = BinaryArray::from(array.as_string::<i32>().clone());
992                cast_byte_container::<BinaryType, LargeBinaryType>(&binary)
993            }
994            Utf8View => Ok(Arc::new(StringViewArray::from(array.as_string::<i32>()))),
995            BinaryView => Ok(Arc::new(
996                StringViewArray::from(array.as_string::<i32>()).to_binary_view(),
997            )),
998            LargeUtf8 => cast_byte_container::<Utf8Type, LargeUtf8Type>(array),
999            Time32(TimeUnit::Second) => parse_string::<Time32SecondType, i32>(array, cast_options),
1000            Time32(TimeUnit::Millisecond) => {
1001                parse_string::<Time32MillisecondType, i32>(array, cast_options)
1002            }
1003            Time64(TimeUnit::Microsecond) => {
1004                parse_string::<Time64MicrosecondType, i32>(array, cast_options)
1005            }
1006            Time64(TimeUnit::Nanosecond) => {
1007                parse_string::<Time64NanosecondType, i32>(array, cast_options)
1008            }
1009            Timestamp(TimeUnit::Second, to_tz) => {
1010                cast_string_to_timestamp::<i32, TimestampSecondType>(array, to_tz, cast_options)
1011            }
1012            Timestamp(TimeUnit::Millisecond, to_tz) => cast_string_to_timestamp::<
1013                i32,
1014                TimestampMillisecondType,
1015            >(array, to_tz, cast_options),
1016            Timestamp(TimeUnit::Microsecond, to_tz) => cast_string_to_timestamp::<
1017                i32,
1018                TimestampMicrosecondType,
1019            >(array, to_tz, cast_options),
1020            Timestamp(TimeUnit::Nanosecond, to_tz) => {
1021                cast_string_to_timestamp::<i32, TimestampNanosecondType>(array, to_tz, cast_options)
1022            }
1023            Interval(IntervalUnit::YearMonth) => {
1024                cast_string_to_year_month_interval::<i32>(array, cast_options)
1025            }
1026            Interval(IntervalUnit::DayTime) => {
1027                cast_string_to_day_time_interval::<i32>(array, cast_options)
1028            }
1029            Interval(IntervalUnit::MonthDayNano) => {
1030                cast_string_to_month_day_nano_interval::<i32>(array, cast_options)
1031            }
1032            _ => Err(ArrowError::CastError(format!(
1033                "Casting from {from_type:?} to {to_type:?} not supported",
1034            ))),
1035        },
1036        (Utf8View, _) => match to_type {
1037            UInt8 => parse_string_view::<UInt8Type>(array, cast_options),
1038            UInt16 => parse_string_view::<UInt16Type>(array, cast_options),
1039            UInt32 => parse_string_view::<UInt32Type>(array, cast_options),
1040            UInt64 => parse_string_view::<UInt64Type>(array, cast_options),
1041            Int8 => parse_string_view::<Int8Type>(array, cast_options),
1042            Int16 => parse_string_view::<Int16Type>(array, cast_options),
1043            Int32 => parse_string_view::<Int32Type>(array, cast_options),
1044            Int64 => parse_string_view::<Int64Type>(array, cast_options),
1045            Float32 => parse_string_view::<Float32Type>(array, cast_options),
1046            Float64 => parse_string_view::<Float64Type>(array, cast_options),
1047            Date32 => parse_string_view::<Date32Type>(array, cast_options),
1048            Date64 => parse_string_view::<Date64Type>(array, cast_options),
1049            Binary => cast_view_to_byte::<StringViewType, GenericBinaryType<i32>>(array),
1050            LargeBinary => cast_view_to_byte::<StringViewType, GenericBinaryType<i64>>(array),
1051            BinaryView => Ok(Arc::new(array.as_string_view().clone().to_binary_view())),
1052            Utf8 => cast_view_to_byte::<StringViewType, GenericStringType<i32>>(array),
1053            LargeUtf8 => cast_view_to_byte::<StringViewType, GenericStringType<i64>>(array),
1054            Time32(TimeUnit::Second) => parse_string_view::<Time32SecondType>(array, cast_options),
1055            Time32(TimeUnit::Millisecond) => {
1056                parse_string_view::<Time32MillisecondType>(array, cast_options)
1057            }
1058            Time64(TimeUnit::Microsecond) => {
1059                parse_string_view::<Time64MicrosecondType>(array, cast_options)
1060            }
1061            Time64(TimeUnit::Nanosecond) => {
1062                parse_string_view::<Time64NanosecondType>(array, cast_options)
1063            }
1064            Timestamp(TimeUnit::Second, to_tz) => {
1065                cast_view_to_timestamp::<TimestampSecondType>(array, to_tz, cast_options)
1066            }
1067            Timestamp(TimeUnit::Millisecond, to_tz) => {
1068                cast_view_to_timestamp::<TimestampMillisecondType>(array, to_tz, cast_options)
1069            }
1070            Timestamp(TimeUnit::Microsecond, to_tz) => {
1071                cast_view_to_timestamp::<TimestampMicrosecondType>(array, to_tz, cast_options)
1072            }
1073            Timestamp(TimeUnit::Nanosecond, to_tz) => {
1074                cast_view_to_timestamp::<TimestampNanosecondType>(array, to_tz, cast_options)
1075            }
1076            Interval(IntervalUnit::YearMonth) => {
1077                cast_view_to_year_month_interval(array, cast_options)
1078            }
1079            Interval(IntervalUnit::DayTime) => cast_view_to_day_time_interval(array, cast_options),
1080            Interval(IntervalUnit::MonthDayNano) => {
1081                cast_view_to_month_day_nano_interval(array, cast_options)
1082            }
1083            _ => Err(ArrowError::CastError(format!(
1084                "Casting from {from_type:?} to {to_type:?} not supported",
1085            ))),
1086        },
1087        (LargeUtf8, _) => match to_type {
1088            UInt8 => parse_string::<UInt8Type, i64>(array, cast_options),
1089            UInt16 => parse_string::<UInt16Type, i64>(array, cast_options),
1090            UInt32 => parse_string::<UInt32Type, i64>(array, cast_options),
1091            UInt64 => parse_string::<UInt64Type, i64>(array, cast_options),
1092            Int8 => parse_string::<Int8Type, i64>(array, cast_options),
1093            Int16 => parse_string::<Int16Type, i64>(array, cast_options),
1094            Int32 => parse_string::<Int32Type, i64>(array, cast_options),
1095            Int64 => parse_string::<Int64Type, i64>(array, cast_options),
1096            Float32 => parse_string::<Float32Type, i64>(array, cast_options),
1097            Float64 => parse_string::<Float64Type, i64>(array, cast_options),
1098            Date32 => parse_string::<Date32Type, i64>(array, cast_options),
1099            Date64 => parse_string::<Date64Type, i64>(array, cast_options),
1100            Utf8 => cast_byte_container::<LargeUtf8Type, Utf8Type>(array),
1101            Binary => {
1102                let large_binary = LargeBinaryArray::from(array.as_string::<i64>().clone());
1103                cast_byte_container::<LargeBinaryType, BinaryType>(&large_binary)
1104            }
1105            LargeBinary => Ok(Arc::new(LargeBinaryArray::from(
1106                array.as_string::<i64>().clone(),
1107            ))),
1108            Utf8View => Ok(Arc::new(StringViewArray::from(array.as_string::<i64>()))),
1109            BinaryView => Ok(Arc::new(BinaryViewArray::from(
1110                array
1111                    .as_string::<i64>()
1112                    .into_iter()
1113                    .map(|x| x.map(|x| x.as_bytes()))
1114                    .collect::<Vec<_>>(),
1115            ))),
1116            Time32(TimeUnit::Second) => parse_string::<Time32SecondType, i64>(array, cast_options),
1117            Time32(TimeUnit::Millisecond) => {
1118                parse_string::<Time32MillisecondType, i64>(array, cast_options)
1119            }
1120            Time64(TimeUnit::Microsecond) => {
1121                parse_string::<Time64MicrosecondType, i64>(array, cast_options)
1122            }
1123            Time64(TimeUnit::Nanosecond) => {
1124                parse_string::<Time64NanosecondType, i64>(array, cast_options)
1125            }
1126            Timestamp(TimeUnit::Second, to_tz) => {
1127                cast_string_to_timestamp::<i64, TimestampSecondType>(array, to_tz, cast_options)
1128            }
1129            Timestamp(TimeUnit::Millisecond, to_tz) => cast_string_to_timestamp::<
1130                i64,
1131                TimestampMillisecondType,
1132            >(array, to_tz, cast_options),
1133            Timestamp(TimeUnit::Microsecond, to_tz) => cast_string_to_timestamp::<
1134                i64,
1135                TimestampMicrosecondType,
1136            >(array, to_tz, cast_options),
1137            Timestamp(TimeUnit::Nanosecond, to_tz) => {
1138                cast_string_to_timestamp::<i64, TimestampNanosecondType>(array, to_tz, cast_options)
1139            }
1140            Interval(IntervalUnit::YearMonth) => {
1141                cast_string_to_year_month_interval::<i64>(array, cast_options)
1142            }
1143            Interval(IntervalUnit::DayTime) => {
1144                cast_string_to_day_time_interval::<i64>(array, cast_options)
1145            }
1146            Interval(IntervalUnit::MonthDayNano) => {
1147                cast_string_to_month_day_nano_interval::<i64>(array, cast_options)
1148            }
1149            _ => Err(ArrowError::CastError(format!(
1150                "Casting from {from_type:?} to {to_type:?} not supported",
1151            ))),
1152        },
1153        (Binary, _) => match to_type {
1154            Utf8 => cast_binary_to_string::<i32>(array, cast_options),
1155            LargeUtf8 => {
1156                let array = cast_binary_to_string::<i32>(array, cast_options)?;
1157                cast_byte_container::<Utf8Type, LargeUtf8Type>(array.as_ref())
1158            }
1159            LargeBinary => cast_byte_container::<BinaryType, LargeBinaryType>(array),
1160            FixedSizeBinary(size) => {
1161                cast_binary_to_fixed_size_binary::<i32>(array, *size, cast_options)
1162            }
1163            BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i32>()))),
1164            Utf8View => Ok(Arc::new(StringViewArray::from(
1165                cast_binary_to_string::<i32>(array, cast_options)?.as_string::<i32>(),
1166            ))),
1167            _ => Err(ArrowError::CastError(format!(
1168                "Casting from {from_type:?} to {to_type:?} not supported",
1169            ))),
1170        },
1171        (LargeBinary, _) => match to_type {
1172            Utf8 => {
1173                let array = cast_binary_to_string::<i64>(array, cast_options)?;
1174                cast_byte_container::<LargeUtf8Type, Utf8Type>(array.as_ref())
1175            }
1176            LargeUtf8 => cast_binary_to_string::<i64>(array, cast_options),
1177            Binary => cast_byte_container::<LargeBinaryType, BinaryType>(array),
1178            FixedSizeBinary(size) => {
1179                cast_binary_to_fixed_size_binary::<i64>(array, *size, cast_options)
1180            }
1181            BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i64>()))),
1182            Utf8View => {
1183                let array = cast_binary_to_string::<i64>(array, cast_options)?;
1184                Ok(Arc::new(StringViewArray::from(array.as_string::<i64>())))
1185            }
1186            _ => Err(ArrowError::CastError(format!(
1187                "Casting from {from_type:?} to {to_type:?} not supported",
1188            ))),
1189        },
1190        (FixedSizeBinary(size), _) => match to_type {
1191            Binary => cast_fixed_size_binary_to_binary::<i32>(array, *size),
1192            LargeBinary => cast_fixed_size_binary_to_binary::<i64>(array, *size),
1193            _ => Err(ArrowError::CastError(format!(
1194                "Casting from {from_type:?} to {to_type:?} not supported",
1195            ))),
1196        },
1197        (BinaryView, Binary) => cast_view_to_byte::<BinaryViewType, GenericBinaryType<i32>>(array),
1198        (BinaryView, LargeBinary) => {
1199            cast_view_to_byte::<BinaryViewType, GenericBinaryType<i64>>(array)
1200        }
1201        (BinaryView, Utf8) => {
1202            let binary_arr = cast_view_to_byte::<BinaryViewType, GenericBinaryType<i32>>(array)?;
1203            cast_binary_to_string::<i32>(&binary_arr, cast_options)
1204        }
1205        (BinaryView, LargeUtf8) => {
1206            let binary_arr = cast_view_to_byte::<BinaryViewType, GenericBinaryType<i64>>(array)?;
1207            cast_binary_to_string::<i64>(&binary_arr, cast_options)
1208        }
1209        (BinaryView, Utf8View) => {
1210            Ok(Arc::new(array.as_binary_view().clone().to_string_view()?) as ArrayRef)
1211        }
1212        (BinaryView, _) => Err(ArrowError::CastError(format!(
1213            "Casting from {from_type:?} to {to_type:?} not supported",
1214        ))),
1215        (from_type, Utf8View) if from_type.is_primitive() => {
1216            value_to_string_view(array, cast_options)
1217        }
1218        (from_type, LargeUtf8) if from_type.is_primitive() => {
1219            value_to_string::<i64>(array, cast_options)
1220        }
1221        (from_type, Utf8) if from_type.is_primitive() => {
1222            value_to_string::<i32>(array, cast_options)
1223        }
1224        (from_type, Binary) if from_type.is_integer() => match from_type {
1225            UInt8 => cast_numeric_to_binary::<UInt8Type, i32>(array),
1226            UInt16 => cast_numeric_to_binary::<UInt16Type, i32>(array),
1227            UInt32 => cast_numeric_to_binary::<UInt32Type, i32>(array),
1228            UInt64 => cast_numeric_to_binary::<UInt64Type, i32>(array),
1229            Int8 => cast_numeric_to_binary::<Int8Type, i32>(array),
1230            Int16 => cast_numeric_to_binary::<Int16Type, i32>(array),
1231            Int32 => cast_numeric_to_binary::<Int32Type, i32>(array),
1232            Int64 => cast_numeric_to_binary::<Int64Type, i32>(array),
1233            _ => unreachable!(),
1234        },
1235        (from_type, LargeBinary) if from_type.is_integer() => match from_type {
1236            UInt8 => cast_numeric_to_binary::<UInt8Type, i64>(array),
1237            UInt16 => cast_numeric_to_binary::<UInt16Type, i64>(array),
1238            UInt32 => cast_numeric_to_binary::<UInt32Type, i64>(array),
1239            UInt64 => cast_numeric_to_binary::<UInt64Type, i64>(array),
1240            Int8 => cast_numeric_to_binary::<Int8Type, i64>(array),
1241            Int16 => cast_numeric_to_binary::<Int16Type, i64>(array),
1242            Int32 => cast_numeric_to_binary::<Int32Type, i64>(array),
1243            Int64 => cast_numeric_to_binary::<Int64Type, i64>(array),
1244            _ => unreachable!(),
1245        },
1246        // start numeric casts
1247        (UInt8, UInt16) => cast_numeric_arrays::<UInt8Type, UInt16Type>(array, cast_options),
1248        (UInt8, UInt32) => cast_numeric_arrays::<UInt8Type, UInt32Type>(array, cast_options),
1249        (UInt8, UInt64) => cast_numeric_arrays::<UInt8Type, UInt64Type>(array, cast_options),
1250        (UInt8, Int8) => cast_numeric_arrays::<UInt8Type, Int8Type>(array, cast_options),
1251        (UInt8, Int16) => cast_numeric_arrays::<UInt8Type, Int16Type>(array, cast_options),
1252        (UInt8, Int32) => cast_numeric_arrays::<UInt8Type, Int32Type>(array, cast_options),
1253        (UInt8, Int64) => cast_numeric_arrays::<UInt8Type, Int64Type>(array, cast_options),
1254        (UInt8, Float16) => cast_numeric_arrays::<UInt8Type, Float16Type>(array, cast_options),
1255        (UInt8, Float32) => cast_numeric_arrays::<UInt8Type, Float32Type>(array, cast_options),
1256        (UInt8, Float64) => cast_numeric_arrays::<UInt8Type, Float64Type>(array, cast_options),
1257
1258        (UInt16, UInt8) => cast_numeric_arrays::<UInt16Type, UInt8Type>(array, cast_options),
1259        (UInt16, UInt32) => cast_numeric_arrays::<UInt16Type, UInt32Type>(array, cast_options),
1260        (UInt16, UInt64) => cast_numeric_arrays::<UInt16Type, UInt64Type>(array, cast_options),
1261        (UInt16, Int8) => cast_numeric_arrays::<UInt16Type, Int8Type>(array, cast_options),
1262        (UInt16, Int16) => cast_numeric_arrays::<UInt16Type, Int16Type>(array, cast_options),
1263        (UInt16, Int32) => cast_numeric_arrays::<UInt16Type, Int32Type>(array, cast_options),
1264        (UInt16, Int64) => cast_numeric_arrays::<UInt16Type, Int64Type>(array, cast_options),
1265        (UInt16, Float16) => cast_numeric_arrays::<UInt16Type, Float16Type>(array, cast_options),
1266        (UInt16, Float32) => cast_numeric_arrays::<UInt16Type, Float32Type>(array, cast_options),
1267        (UInt16, Float64) => cast_numeric_arrays::<UInt16Type, Float64Type>(array, cast_options),
1268
1269        (UInt32, UInt8) => cast_numeric_arrays::<UInt32Type, UInt8Type>(array, cast_options),
1270        (UInt32, UInt16) => cast_numeric_arrays::<UInt32Type, UInt16Type>(array, cast_options),
1271        (UInt32, UInt64) => cast_numeric_arrays::<UInt32Type, UInt64Type>(array, cast_options),
1272        (UInt32, Int8) => cast_numeric_arrays::<UInt32Type, Int8Type>(array, cast_options),
1273        (UInt32, Int16) => cast_numeric_arrays::<UInt32Type, Int16Type>(array, cast_options),
1274        (UInt32, Int32) => cast_numeric_arrays::<UInt32Type, Int32Type>(array, cast_options),
1275        (UInt32, Int64) => cast_numeric_arrays::<UInt32Type, Int64Type>(array, cast_options),
1276        (UInt32, Float16) => cast_numeric_arrays::<UInt32Type, Float16Type>(array, cast_options),
1277        (UInt32, Float32) => cast_numeric_arrays::<UInt32Type, Float32Type>(array, cast_options),
1278        (UInt32, Float64) => cast_numeric_arrays::<UInt32Type, Float64Type>(array, cast_options),
1279
1280        (UInt64, UInt8) => cast_numeric_arrays::<UInt64Type, UInt8Type>(array, cast_options),
1281        (UInt64, UInt16) => cast_numeric_arrays::<UInt64Type, UInt16Type>(array, cast_options),
1282        (UInt64, UInt32) => cast_numeric_arrays::<UInt64Type, UInt32Type>(array, cast_options),
1283        (UInt64, Int8) => cast_numeric_arrays::<UInt64Type, Int8Type>(array, cast_options),
1284        (UInt64, Int16) => cast_numeric_arrays::<UInt64Type, Int16Type>(array, cast_options),
1285        (UInt64, Int32) => cast_numeric_arrays::<UInt64Type, Int32Type>(array, cast_options),
1286        (UInt64, Int64) => cast_numeric_arrays::<UInt64Type, Int64Type>(array, cast_options),
1287        (UInt64, Float16) => cast_numeric_arrays::<UInt64Type, Float16Type>(array, cast_options),
1288        (UInt64, Float32) => cast_numeric_arrays::<UInt64Type, Float32Type>(array, cast_options),
1289        (UInt64, Float64) => cast_numeric_arrays::<UInt64Type, Float64Type>(array, cast_options),
1290
1291        (Int8, UInt8) => cast_numeric_arrays::<Int8Type, UInt8Type>(array, cast_options),
1292        (Int8, UInt16) => cast_numeric_arrays::<Int8Type, UInt16Type>(array, cast_options),
1293        (Int8, UInt32) => cast_numeric_arrays::<Int8Type, UInt32Type>(array, cast_options),
1294        (Int8, UInt64) => cast_numeric_arrays::<Int8Type, UInt64Type>(array, cast_options),
1295        (Int8, Int16) => cast_numeric_arrays::<Int8Type, Int16Type>(array, cast_options),
1296        (Int8, Int32) => cast_numeric_arrays::<Int8Type, Int32Type>(array, cast_options),
1297        (Int8, Int64) => cast_numeric_arrays::<Int8Type, Int64Type>(array, cast_options),
1298        (Int8, Float16) => cast_numeric_arrays::<Int8Type, Float16Type>(array, cast_options),
1299        (Int8, Float32) => cast_numeric_arrays::<Int8Type, Float32Type>(array, cast_options),
1300        (Int8, Float64) => cast_numeric_arrays::<Int8Type, Float64Type>(array, cast_options),
1301
1302        (Int16, UInt8) => cast_numeric_arrays::<Int16Type, UInt8Type>(array, cast_options),
1303        (Int16, UInt16) => cast_numeric_arrays::<Int16Type, UInt16Type>(array, cast_options),
1304        (Int16, UInt32) => cast_numeric_arrays::<Int16Type, UInt32Type>(array, cast_options),
1305        (Int16, UInt64) => cast_numeric_arrays::<Int16Type, UInt64Type>(array, cast_options),
1306        (Int16, Int8) => cast_numeric_arrays::<Int16Type, Int8Type>(array, cast_options),
1307        (Int16, Int32) => cast_numeric_arrays::<Int16Type, Int32Type>(array, cast_options),
1308        (Int16, Int64) => cast_numeric_arrays::<Int16Type, Int64Type>(array, cast_options),
1309        (Int16, Float16) => cast_numeric_arrays::<Int16Type, Float16Type>(array, cast_options),
1310        (Int16, Float32) => cast_numeric_arrays::<Int16Type, Float32Type>(array, cast_options),
1311        (Int16, Float64) => cast_numeric_arrays::<Int16Type, Float64Type>(array, cast_options),
1312
1313        (Int32, UInt8) => cast_numeric_arrays::<Int32Type, UInt8Type>(array, cast_options),
1314        (Int32, UInt16) => cast_numeric_arrays::<Int32Type, UInt16Type>(array, cast_options),
1315        (Int32, UInt32) => cast_numeric_arrays::<Int32Type, UInt32Type>(array, cast_options),
1316        (Int32, UInt64) => cast_numeric_arrays::<Int32Type, UInt64Type>(array, cast_options),
1317        (Int32, Int8) => cast_numeric_arrays::<Int32Type, Int8Type>(array, cast_options),
1318        (Int32, Int16) => cast_numeric_arrays::<Int32Type, Int16Type>(array, cast_options),
1319        (Int32, Int64) => cast_numeric_arrays::<Int32Type, Int64Type>(array, cast_options),
1320        (Int32, Float16) => cast_numeric_arrays::<Int32Type, Float16Type>(array, cast_options),
1321        (Int32, Float32) => cast_numeric_arrays::<Int32Type, Float32Type>(array, cast_options),
1322        (Int32, Float64) => cast_numeric_arrays::<Int32Type, Float64Type>(array, cast_options),
1323
1324        (Int64, UInt8) => cast_numeric_arrays::<Int64Type, UInt8Type>(array, cast_options),
1325        (Int64, UInt16) => cast_numeric_arrays::<Int64Type, UInt16Type>(array, cast_options),
1326        (Int64, UInt32) => cast_numeric_arrays::<Int64Type, UInt32Type>(array, cast_options),
1327        (Int64, UInt64) => cast_numeric_arrays::<Int64Type, UInt64Type>(array, cast_options),
1328        (Int64, Int8) => cast_numeric_arrays::<Int64Type, Int8Type>(array, cast_options),
1329        (Int64, Int16) => cast_numeric_arrays::<Int64Type, Int16Type>(array, cast_options),
1330        (Int64, Int32) => cast_numeric_arrays::<Int64Type, Int32Type>(array, cast_options),
1331        (Int64, Float16) => cast_numeric_arrays::<Int64Type, Float16Type>(array, cast_options),
1332        (Int64, Float32) => cast_numeric_arrays::<Int64Type, Float32Type>(array, cast_options),
1333        (Int64, Float64) => cast_numeric_arrays::<Int64Type, Float64Type>(array, cast_options),
1334
1335        (Float16, UInt8) => cast_numeric_arrays::<Float16Type, UInt8Type>(array, cast_options),
1336        (Float16, UInt16) => cast_numeric_arrays::<Float16Type, UInt16Type>(array, cast_options),
1337        (Float16, UInt32) => cast_numeric_arrays::<Float16Type, UInt32Type>(array, cast_options),
1338        (Float16, UInt64) => cast_numeric_arrays::<Float16Type, UInt64Type>(array, cast_options),
1339        (Float16, Int8) => cast_numeric_arrays::<Float16Type, Int8Type>(array, cast_options),
1340        (Float16, Int16) => cast_numeric_arrays::<Float16Type, Int16Type>(array, cast_options),
1341        (Float16, Int32) => cast_numeric_arrays::<Float16Type, Int32Type>(array, cast_options),
1342        (Float16, Int64) => cast_numeric_arrays::<Float16Type, Int64Type>(array, cast_options),
1343        (Float16, Float32) => cast_numeric_arrays::<Float16Type, Float32Type>(array, cast_options),
1344        (Float16, Float64) => cast_numeric_arrays::<Float16Type, Float64Type>(array, cast_options),
1345
1346        (Float32, UInt8) => cast_numeric_arrays::<Float32Type, UInt8Type>(array, cast_options),
1347        (Float32, UInt16) => cast_numeric_arrays::<Float32Type, UInt16Type>(array, cast_options),
1348        (Float32, UInt32) => cast_numeric_arrays::<Float32Type, UInt32Type>(array, cast_options),
1349        (Float32, UInt64) => cast_numeric_arrays::<Float32Type, UInt64Type>(array, cast_options),
1350        (Float32, Int8) => cast_numeric_arrays::<Float32Type, Int8Type>(array, cast_options),
1351        (Float32, Int16) => cast_numeric_arrays::<Float32Type, Int16Type>(array, cast_options),
1352        (Float32, Int32) => cast_numeric_arrays::<Float32Type, Int32Type>(array, cast_options),
1353        (Float32, Int64) => cast_numeric_arrays::<Float32Type, Int64Type>(array, cast_options),
1354        (Float32, Float16) => cast_numeric_arrays::<Float32Type, Float16Type>(array, cast_options),
1355        (Float32, Float64) => cast_numeric_arrays::<Float32Type, Float64Type>(array, cast_options),
1356
1357        (Float64, UInt8) => cast_numeric_arrays::<Float64Type, UInt8Type>(array, cast_options),
1358        (Float64, UInt16) => cast_numeric_arrays::<Float64Type, UInt16Type>(array, cast_options),
1359        (Float64, UInt32) => cast_numeric_arrays::<Float64Type, UInt32Type>(array, cast_options),
1360        (Float64, UInt64) => cast_numeric_arrays::<Float64Type, UInt64Type>(array, cast_options),
1361        (Float64, Int8) => cast_numeric_arrays::<Float64Type, Int8Type>(array, cast_options),
1362        (Float64, Int16) => cast_numeric_arrays::<Float64Type, Int16Type>(array, cast_options),
1363        (Float64, Int32) => cast_numeric_arrays::<Float64Type, Int32Type>(array, cast_options),
1364        (Float64, Int64) => cast_numeric_arrays::<Float64Type, Int64Type>(array, cast_options),
1365        (Float64, Float16) => cast_numeric_arrays::<Float64Type, Float16Type>(array, cast_options),
1366        (Float64, Float32) => cast_numeric_arrays::<Float64Type, Float32Type>(array, cast_options),
1367        // end numeric casts
1368
1369        // temporal casts
1370        (Int32, Date32) => cast_reinterpret_arrays::<Int32Type, Date32Type>(array),
1371        (Int32, Date64) => cast_with_options(
1372            &cast_with_options(array, &Date32, cast_options)?,
1373            &Date64,
1374            cast_options,
1375        ),
1376        (Int32, Time32(TimeUnit::Second)) => {
1377            cast_reinterpret_arrays::<Int32Type, Time32SecondType>(array)
1378        }
1379        (Int32, Time32(TimeUnit::Millisecond)) => {
1380            cast_reinterpret_arrays::<Int32Type, Time32MillisecondType>(array)
1381        }
1382        // No support for microsecond/nanosecond with i32
1383        (Date32, Int32) => cast_reinterpret_arrays::<Date32Type, Int32Type>(array),
1384        (Date32, Int64) => cast_with_options(
1385            &cast_with_options(array, &Int32, cast_options)?,
1386            &Int64,
1387            cast_options,
1388        ),
1389        (Time32(TimeUnit::Second), Int32) => {
1390            cast_reinterpret_arrays::<Time32SecondType, Int32Type>(array)
1391        }
1392        (Time32(TimeUnit::Millisecond), Int32) => {
1393            cast_reinterpret_arrays::<Time32MillisecondType, Int32Type>(array)
1394        }
1395        (Int64, Date64) => cast_reinterpret_arrays::<Int64Type, Date64Type>(array),
1396        (Int64, Date32) => cast_with_options(
1397            &cast_with_options(array, &Int32, cast_options)?,
1398            &Date32,
1399            cast_options,
1400        ),
1401        // No support for second/milliseconds with i64
1402        (Int64, Time64(TimeUnit::Microsecond)) => {
1403            cast_reinterpret_arrays::<Int64Type, Time64MicrosecondType>(array)
1404        }
1405        (Int64, Time64(TimeUnit::Nanosecond)) => {
1406            cast_reinterpret_arrays::<Int64Type, Time64NanosecondType>(array)
1407        }
1408
1409        (Date64, Int64) => cast_reinterpret_arrays::<Date64Type, Int64Type>(array),
1410        (Date64, Int32) => cast_with_options(
1411            &cast_with_options(array, &Int64, cast_options)?,
1412            &Int32,
1413            cast_options,
1414        ),
1415        (Time64(TimeUnit::Microsecond), Int64) => {
1416            cast_reinterpret_arrays::<Time64MicrosecondType, Int64Type>(array)
1417        }
1418        (Time64(TimeUnit::Nanosecond), Int64) => {
1419            cast_reinterpret_arrays::<Time64NanosecondType, Int64Type>(array)
1420        }
1421        (Date32, Date64) => Ok(Arc::new(
1422            array
1423                .as_primitive::<Date32Type>()
1424                .unary::<_, Date64Type>(|x| x as i64 * MILLISECONDS_IN_DAY),
1425        )),
1426        (Date64, Date32) => Ok(Arc::new(
1427            array
1428                .as_primitive::<Date64Type>()
1429                .unary::<_, Date32Type>(|x| (x / MILLISECONDS_IN_DAY) as i32),
1430        )),
1431
1432        (Time32(TimeUnit::Second), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1433            array
1434                .as_primitive::<Time32SecondType>()
1435                .unary::<_, Time32MillisecondType>(|x| x * MILLISECONDS as i32),
1436        )),
1437        (Time32(TimeUnit::Second), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1438            array
1439                .as_primitive::<Time32SecondType>()
1440                .unary::<_, Time64MicrosecondType>(|x| x as i64 * MICROSECONDS),
1441        )),
1442        (Time32(TimeUnit::Second), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1443            array
1444                .as_primitive::<Time32SecondType>()
1445                .unary::<_, Time64NanosecondType>(|x| x as i64 * NANOSECONDS),
1446        )),
1447
1448        (Time32(TimeUnit::Millisecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1449            array
1450                .as_primitive::<Time32MillisecondType>()
1451                .unary::<_, Time32SecondType>(|x| x / MILLISECONDS as i32),
1452        )),
1453        (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1454            array
1455                .as_primitive::<Time32MillisecondType>()
1456                .unary::<_, Time64MicrosecondType>(|x| x as i64 * (MICROSECONDS / MILLISECONDS)),
1457        )),
1458        (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1459            array
1460                .as_primitive::<Time32MillisecondType>()
1461                .unary::<_, Time64NanosecondType>(|x| x as i64 * (MICROSECONDS / NANOSECONDS)),
1462        )),
1463
1464        (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1465            array
1466                .as_primitive::<Time64MicrosecondType>()
1467                .unary::<_, Time32SecondType>(|x| (x / MICROSECONDS) as i32),
1468        )),
1469        (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1470            array
1471                .as_primitive::<Time64MicrosecondType>()
1472                .unary::<_, Time32MillisecondType>(|x| (x / (MICROSECONDS / MILLISECONDS)) as i32),
1473        )),
1474        (Time64(TimeUnit::Microsecond), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1475            array
1476                .as_primitive::<Time64MicrosecondType>()
1477                .unary::<_, Time64NanosecondType>(|x| x * (NANOSECONDS / MICROSECONDS)),
1478        )),
1479
1480        (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1481            array
1482                .as_primitive::<Time64NanosecondType>()
1483                .unary::<_, Time32SecondType>(|x| (x / NANOSECONDS) as i32),
1484        )),
1485        (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1486            array
1487                .as_primitive::<Time64NanosecondType>()
1488                .unary::<_, Time32MillisecondType>(|x| (x / (NANOSECONDS / MILLISECONDS)) as i32),
1489        )),
1490        (Time64(TimeUnit::Nanosecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1491            array
1492                .as_primitive::<Time64NanosecondType>()
1493                .unary::<_, Time64MicrosecondType>(|x| x / (NANOSECONDS / MICROSECONDS)),
1494        )),
1495
1496        // Timestamp to integer/floating/decimals
1497        (Timestamp(TimeUnit::Second, _), _) if to_type.is_numeric() => {
1498            let array = cast_reinterpret_arrays::<TimestampSecondType, Int64Type>(array)?;
1499            cast_with_options(&array, to_type, cast_options)
1500        }
1501        (Timestamp(TimeUnit::Millisecond, _), _) if to_type.is_numeric() => {
1502            let array = cast_reinterpret_arrays::<TimestampMillisecondType, Int64Type>(array)?;
1503            cast_with_options(&array, to_type, cast_options)
1504        }
1505        (Timestamp(TimeUnit::Microsecond, _), _) if to_type.is_numeric() => {
1506            let array = cast_reinterpret_arrays::<TimestampMicrosecondType, Int64Type>(array)?;
1507            cast_with_options(&array, to_type, cast_options)
1508        }
1509        (Timestamp(TimeUnit::Nanosecond, _), _) if to_type.is_numeric() => {
1510            let array = cast_reinterpret_arrays::<TimestampNanosecondType, Int64Type>(array)?;
1511            cast_with_options(&array, to_type, cast_options)
1512        }
1513
1514        (_, Timestamp(unit, tz)) if from_type.is_numeric() => {
1515            let array = cast_with_options(array, &Int64, cast_options)?;
1516            Ok(make_timestamp_array(
1517                array.as_primitive(),
1518                *unit,
1519                tz.clone(),
1520            ))
1521        }
1522
1523        (Timestamp(from_unit, from_tz), Timestamp(to_unit, to_tz)) => {
1524            let array = cast_with_options(array, &Int64, cast_options)?;
1525            let time_array = array.as_primitive::<Int64Type>();
1526            let from_size = time_unit_multiple(from_unit);
1527            let to_size = time_unit_multiple(to_unit);
1528            // we either divide or multiply, depending on size of each unit
1529            // units are never the same when the types are the same
1530            let converted = match from_size.cmp(&to_size) {
1531                Ordering::Greater => {
1532                    let divisor = from_size / to_size;
1533                    time_array.unary::<_, Int64Type>(|o| o / divisor)
1534                }
1535                Ordering::Equal => time_array.clone(),
1536                Ordering::Less => {
1537                    let mul = to_size / from_size;
1538                    if cast_options.safe {
1539                        time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
1540                    } else {
1541                        time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
1542                    }
1543                }
1544            };
1545            // Normalize timezone
1546            let adjusted = match (from_tz, to_tz) {
1547                // Only this case needs to be adjusted because we're casting from
1548                // unknown time offset to some time offset, we want the time to be
1549                // unchanged.
1550                //
1551                // i.e. Timestamp('2001-01-01T00:00', None) -> Timestamp('2001-01-01T00:00', '+0700')
1552                (None, Some(to_tz)) => {
1553                    let to_tz: Tz = to_tz.parse()?;
1554                    match to_unit {
1555                        TimeUnit::Second => adjust_timestamp_to_timezone::<TimestampSecondType>(
1556                            converted,
1557                            &to_tz,
1558                            cast_options,
1559                        )?,
1560                        TimeUnit::Millisecond => adjust_timestamp_to_timezone::<
1561                            TimestampMillisecondType,
1562                        >(
1563                            converted, &to_tz, cast_options
1564                        )?,
1565                        TimeUnit::Microsecond => adjust_timestamp_to_timezone::<
1566                            TimestampMicrosecondType,
1567                        >(
1568                            converted, &to_tz, cast_options
1569                        )?,
1570                        TimeUnit::Nanosecond => adjust_timestamp_to_timezone::<
1571                            TimestampNanosecondType,
1572                        >(
1573                            converted, &to_tz, cast_options
1574                        )?,
1575                    }
1576                }
1577                _ => converted,
1578            };
1579            Ok(make_timestamp_array(&adjusted, *to_unit, to_tz.clone()))
1580        }
1581        (Timestamp(TimeUnit::Microsecond, _), Date32) => {
1582            timestamp_to_date32(array.as_primitive::<TimestampMicrosecondType>())
1583        }
1584        (Timestamp(TimeUnit::Millisecond, _), Date32) => {
1585            timestamp_to_date32(array.as_primitive::<TimestampMillisecondType>())
1586        }
1587        (Timestamp(TimeUnit::Second, _), Date32) => {
1588            timestamp_to_date32(array.as_primitive::<TimestampSecondType>())
1589        }
1590        (Timestamp(TimeUnit::Nanosecond, _), Date32) => {
1591            timestamp_to_date32(array.as_primitive::<TimestampNanosecondType>())
1592        }
1593        (Timestamp(TimeUnit::Second, _), Date64) => Ok(Arc::new(match cast_options.safe {
1594            true => {
1595                // change error to None
1596                array
1597                    .as_primitive::<TimestampSecondType>()
1598                    .unary_opt::<_, Date64Type>(|x| x.checked_mul(MILLISECONDS))
1599            }
1600            false => array
1601                .as_primitive::<TimestampSecondType>()
1602                .try_unary::<_, Date64Type, _>(|x| x.mul_checked(MILLISECONDS))?,
1603        })),
1604        (Timestamp(TimeUnit::Millisecond, _), Date64) => {
1605            cast_reinterpret_arrays::<TimestampMillisecondType, Date64Type>(array)
1606        }
1607        (Timestamp(TimeUnit::Microsecond, _), Date64) => Ok(Arc::new(
1608            array
1609                .as_primitive::<TimestampMicrosecondType>()
1610                .unary::<_, Date64Type>(|x| x / (MICROSECONDS / MILLISECONDS)),
1611        )),
1612        (Timestamp(TimeUnit::Nanosecond, _), Date64) => Ok(Arc::new(
1613            array
1614                .as_primitive::<TimestampNanosecondType>()
1615                .unary::<_, Date64Type>(|x| x / (NANOSECONDS / MILLISECONDS)),
1616        )),
1617        (Timestamp(TimeUnit::Second, tz), Time64(TimeUnit::Microsecond)) => {
1618            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1619            Ok(Arc::new(
1620                array
1621                    .as_primitive::<TimestampSecondType>()
1622                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1623                        Ok(time_to_time64us(as_time_res_with_timezone::<
1624                            TimestampSecondType,
1625                        >(x, tz)?))
1626                    })?,
1627            ))
1628        }
1629        (Timestamp(TimeUnit::Second, tz), Time64(TimeUnit::Nanosecond)) => {
1630            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1631            Ok(Arc::new(
1632                array
1633                    .as_primitive::<TimestampSecondType>()
1634                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1635                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1636                            TimestampSecondType,
1637                        >(x, tz)?))
1638                    })?,
1639            ))
1640        }
1641        (Timestamp(TimeUnit::Millisecond, tz), Time64(TimeUnit::Microsecond)) => {
1642            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1643            Ok(Arc::new(
1644                array
1645                    .as_primitive::<TimestampMillisecondType>()
1646                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1647                        Ok(time_to_time64us(as_time_res_with_timezone::<
1648                            TimestampMillisecondType,
1649                        >(x, tz)?))
1650                    })?,
1651            ))
1652        }
1653        (Timestamp(TimeUnit::Millisecond, tz), Time64(TimeUnit::Nanosecond)) => {
1654            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1655            Ok(Arc::new(
1656                array
1657                    .as_primitive::<TimestampMillisecondType>()
1658                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1659                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1660                            TimestampMillisecondType,
1661                        >(x, tz)?))
1662                    })?,
1663            ))
1664        }
1665        (Timestamp(TimeUnit::Microsecond, tz), Time64(TimeUnit::Microsecond)) => {
1666            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1667            Ok(Arc::new(
1668                array
1669                    .as_primitive::<TimestampMicrosecondType>()
1670                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1671                        Ok(time_to_time64us(as_time_res_with_timezone::<
1672                            TimestampMicrosecondType,
1673                        >(x, tz)?))
1674                    })?,
1675            ))
1676        }
1677        (Timestamp(TimeUnit::Microsecond, tz), Time64(TimeUnit::Nanosecond)) => {
1678            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1679            Ok(Arc::new(
1680                array
1681                    .as_primitive::<TimestampMicrosecondType>()
1682                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1683                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1684                            TimestampMicrosecondType,
1685                        >(x, tz)?))
1686                    })?,
1687            ))
1688        }
1689        (Timestamp(TimeUnit::Nanosecond, tz), Time64(TimeUnit::Microsecond)) => {
1690            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1691            Ok(Arc::new(
1692                array
1693                    .as_primitive::<TimestampNanosecondType>()
1694                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1695                        Ok(time_to_time64us(as_time_res_with_timezone::<
1696                            TimestampNanosecondType,
1697                        >(x, tz)?))
1698                    })?,
1699            ))
1700        }
1701        (Timestamp(TimeUnit::Nanosecond, tz), Time64(TimeUnit::Nanosecond)) => {
1702            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1703            Ok(Arc::new(
1704                array
1705                    .as_primitive::<TimestampNanosecondType>()
1706                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1707                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1708                            TimestampNanosecondType,
1709                        >(x, tz)?))
1710                    })?,
1711            ))
1712        }
1713        (Timestamp(TimeUnit::Second, tz), Time32(TimeUnit::Second)) => {
1714            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1715            Ok(Arc::new(
1716                array
1717                    .as_primitive::<TimestampSecondType>()
1718                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
1719                        Ok(time_to_time32s(as_time_res_with_timezone::<
1720                            TimestampSecondType,
1721                        >(x, tz)?))
1722                    })?,
1723            ))
1724        }
1725        (Timestamp(TimeUnit::Second, tz), Time32(TimeUnit::Millisecond)) => {
1726            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1727            Ok(Arc::new(
1728                array
1729                    .as_primitive::<TimestampSecondType>()
1730                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
1731                        Ok(time_to_time32ms(as_time_res_with_timezone::<
1732                            TimestampSecondType,
1733                        >(x, tz)?))
1734                    })?,
1735            ))
1736        }
1737        (Timestamp(TimeUnit::Millisecond, tz), Time32(TimeUnit::Second)) => {
1738            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1739            Ok(Arc::new(
1740                array
1741                    .as_primitive::<TimestampMillisecondType>()
1742                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
1743                        Ok(time_to_time32s(as_time_res_with_timezone::<
1744                            TimestampMillisecondType,
1745                        >(x, tz)?))
1746                    })?,
1747            ))
1748        }
1749        (Timestamp(TimeUnit::Millisecond, tz), Time32(TimeUnit::Millisecond)) => {
1750            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1751            Ok(Arc::new(
1752                array
1753                    .as_primitive::<TimestampMillisecondType>()
1754                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
1755                        Ok(time_to_time32ms(as_time_res_with_timezone::<
1756                            TimestampMillisecondType,
1757                        >(x, tz)?))
1758                    })?,
1759            ))
1760        }
1761        (Timestamp(TimeUnit::Microsecond, tz), Time32(TimeUnit::Second)) => {
1762            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1763            Ok(Arc::new(
1764                array
1765                    .as_primitive::<TimestampMicrosecondType>()
1766                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
1767                        Ok(time_to_time32s(as_time_res_with_timezone::<
1768                            TimestampMicrosecondType,
1769                        >(x, tz)?))
1770                    })?,
1771            ))
1772        }
1773        (Timestamp(TimeUnit::Microsecond, tz), Time32(TimeUnit::Millisecond)) => {
1774            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1775            Ok(Arc::new(
1776                array
1777                    .as_primitive::<TimestampMicrosecondType>()
1778                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
1779                        Ok(time_to_time32ms(as_time_res_with_timezone::<
1780                            TimestampMicrosecondType,
1781                        >(x, tz)?))
1782                    })?,
1783            ))
1784        }
1785        (Timestamp(TimeUnit::Nanosecond, tz), Time32(TimeUnit::Second)) => {
1786            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1787            Ok(Arc::new(
1788                array
1789                    .as_primitive::<TimestampNanosecondType>()
1790                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
1791                        Ok(time_to_time32s(as_time_res_with_timezone::<
1792                            TimestampNanosecondType,
1793                        >(x, tz)?))
1794                    })?,
1795            ))
1796        }
1797        (Timestamp(TimeUnit::Nanosecond, tz), Time32(TimeUnit::Millisecond)) => {
1798            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1799            Ok(Arc::new(
1800                array
1801                    .as_primitive::<TimestampNanosecondType>()
1802                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
1803                        Ok(time_to_time32ms(as_time_res_with_timezone::<
1804                            TimestampNanosecondType,
1805                        >(x, tz)?))
1806                    })?,
1807            ))
1808        }
1809        (Date64, Timestamp(TimeUnit::Second, None)) => Ok(Arc::new(
1810            array
1811                .as_primitive::<Date64Type>()
1812                .unary::<_, TimestampSecondType>(|x| x / MILLISECONDS),
1813        )),
1814        (Date64, Timestamp(TimeUnit::Millisecond, None)) => {
1815            cast_reinterpret_arrays::<Date64Type, TimestampMillisecondType>(array)
1816        }
1817        (Date64, Timestamp(TimeUnit::Microsecond, None)) => Ok(Arc::new(
1818            array
1819                .as_primitive::<Date64Type>()
1820                .unary::<_, TimestampMicrosecondType>(|x| x * (MICROSECONDS / MILLISECONDS)),
1821        )),
1822        (Date64, Timestamp(TimeUnit::Nanosecond, None)) => Ok(Arc::new(
1823            array
1824                .as_primitive::<Date64Type>()
1825                .unary::<_, TimestampNanosecondType>(|x| x * (NANOSECONDS / MILLISECONDS)),
1826        )),
1827        (Date32, Timestamp(TimeUnit::Second, None)) => Ok(Arc::new(
1828            array
1829                .as_primitive::<Date32Type>()
1830                .unary::<_, TimestampSecondType>(|x| (x as i64) * SECONDS_IN_DAY),
1831        )),
1832        (Date32, Timestamp(TimeUnit::Millisecond, None)) => Ok(Arc::new(
1833            array
1834                .as_primitive::<Date32Type>()
1835                .unary::<_, TimestampMillisecondType>(|x| (x as i64) * MILLISECONDS_IN_DAY),
1836        )),
1837        (Date32, Timestamp(TimeUnit::Microsecond, None)) => Ok(Arc::new(
1838            array
1839                .as_primitive::<Date32Type>()
1840                .unary::<_, TimestampMicrosecondType>(|x| (x as i64) * MICROSECONDS_IN_DAY),
1841        )),
1842        (Date32, Timestamp(TimeUnit::Nanosecond, None)) => Ok(Arc::new(
1843            array
1844                .as_primitive::<Date32Type>()
1845                .unary::<_, TimestampNanosecondType>(|x| (x as i64) * NANOSECONDS_IN_DAY),
1846        )),
1847
1848        (_, Duration(unit)) if from_type.is_numeric() => {
1849            let array = cast_with_options(array, &Int64, cast_options)?;
1850            Ok(make_duration_array(array.as_primitive(), *unit))
1851        }
1852        (Duration(TimeUnit::Second), _) if to_type.is_numeric() => {
1853            let array = cast_reinterpret_arrays::<DurationSecondType, Int64Type>(array)?;
1854            cast_with_options(&array, to_type, cast_options)
1855        }
1856        (Duration(TimeUnit::Millisecond), _) if to_type.is_numeric() => {
1857            let array = cast_reinterpret_arrays::<DurationMillisecondType, Int64Type>(array)?;
1858            cast_with_options(&array, to_type, cast_options)
1859        }
1860        (Duration(TimeUnit::Microsecond), _) if to_type.is_numeric() => {
1861            let array = cast_reinterpret_arrays::<DurationMicrosecondType, Int64Type>(array)?;
1862            cast_with_options(&array, to_type, cast_options)
1863        }
1864        (Duration(TimeUnit::Nanosecond), _) if to_type.is_numeric() => {
1865            let array = cast_reinterpret_arrays::<DurationNanosecondType, Int64Type>(array)?;
1866            cast_with_options(&array, to_type, cast_options)
1867        }
1868
1869        (Duration(from_unit), Duration(to_unit)) => {
1870            let array = cast_with_options(array, &Int64, cast_options)?;
1871            let time_array = array.as_primitive::<Int64Type>();
1872            let from_size = time_unit_multiple(from_unit);
1873            let to_size = time_unit_multiple(to_unit);
1874            // we either divide or multiply, depending on size of each unit
1875            // units are never the same when the types are the same
1876            let converted = match from_size.cmp(&to_size) {
1877                Ordering::Greater => {
1878                    let divisor = from_size / to_size;
1879                    time_array.unary::<_, Int64Type>(|o| o / divisor)
1880                }
1881                Ordering::Equal => time_array.clone(),
1882                Ordering::Less => {
1883                    let mul = to_size / from_size;
1884                    if cast_options.safe {
1885                        time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
1886                    } else {
1887                        time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
1888                    }
1889                }
1890            };
1891            Ok(make_duration_array(&converted, *to_unit))
1892        }
1893
1894        (Duration(TimeUnit::Second), Interval(IntervalUnit::MonthDayNano)) => {
1895            cast_duration_to_interval::<DurationSecondType>(array, cast_options)
1896        }
1897        (Duration(TimeUnit::Millisecond), Interval(IntervalUnit::MonthDayNano)) => {
1898            cast_duration_to_interval::<DurationMillisecondType>(array, cast_options)
1899        }
1900        (Duration(TimeUnit::Microsecond), Interval(IntervalUnit::MonthDayNano)) => {
1901            cast_duration_to_interval::<DurationMicrosecondType>(array, cast_options)
1902        }
1903        (Duration(TimeUnit::Nanosecond), Interval(IntervalUnit::MonthDayNano)) => {
1904            cast_duration_to_interval::<DurationNanosecondType>(array, cast_options)
1905        }
1906        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Second)) => {
1907            cast_month_day_nano_to_duration::<DurationSecondType>(array, cast_options)
1908        }
1909        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Millisecond)) => {
1910            cast_month_day_nano_to_duration::<DurationMillisecondType>(array, cast_options)
1911        }
1912        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Microsecond)) => {
1913            cast_month_day_nano_to_duration::<DurationMicrosecondType>(array, cast_options)
1914        }
1915        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Nanosecond)) => {
1916            cast_month_day_nano_to_duration::<DurationNanosecondType>(array, cast_options)
1917        }
1918        (Interval(IntervalUnit::YearMonth), Interval(IntervalUnit::MonthDayNano)) => {
1919            cast_interval_year_month_to_interval_month_day_nano(array, cast_options)
1920        }
1921        (Interval(IntervalUnit::DayTime), Interval(IntervalUnit::MonthDayNano)) => {
1922            cast_interval_day_time_to_interval_month_day_nano(array, cast_options)
1923        }
1924        (Int32, Interval(IntervalUnit::YearMonth)) => {
1925            cast_reinterpret_arrays::<Int32Type, IntervalYearMonthType>(array)
1926        }
1927        (_, _) => Err(ArrowError::CastError(format!(
1928            "Casting from {from_type:?} to {to_type:?} not supported",
1929        ))),
1930    }
1931}
1932
1933fn cast_from_decimal<D, F>(
1934    array: &dyn Array,
1935    base: D::Native,
1936    scale: &i8,
1937    from_type: &DataType,
1938    to_type: &DataType,
1939    as_float: F,
1940    cast_options: &CastOptions,
1941) -> Result<ArrayRef, ArrowError>
1942where
1943    D: DecimalType + ArrowPrimitiveType,
1944    <D as ArrowPrimitiveType>::Native: ArrowNativeTypeOp + ToPrimitive,
1945    F: Fn(D::Native) -> f64,
1946{
1947    use DataType::*;
1948    // cast decimal to other type
1949    match to_type {
1950        UInt8 => cast_decimal_to_integer::<D, UInt8Type>(array, base, *scale, cast_options),
1951        UInt16 => cast_decimal_to_integer::<D, UInt16Type>(array, base, *scale, cast_options),
1952        UInt32 => cast_decimal_to_integer::<D, UInt32Type>(array, base, *scale, cast_options),
1953        UInt64 => cast_decimal_to_integer::<D, UInt64Type>(array, base, *scale, cast_options),
1954        Int8 => cast_decimal_to_integer::<D, Int8Type>(array, base, *scale, cast_options),
1955        Int16 => cast_decimal_to_integer::<D, Int16Type>(array, base, *scale, cast_options),
1956        Int32 => cast_decimal_to_integer::<D, Int32Type>(array, base, *scale, cast_options),
1957        Int64 => cast_decimal_to_integer::<D, Int64Type>(array, base, *scale, cast_options),
1958        Float32 => cast_decimal_to_float::<D, Float32Type, _>(array, |x| {
1959            (as_float(x) / 10_f64.powi(*scale as i32)) as f32
1960        }),
1961        Float64 => cast_decimal_to_float::<D, Float64Type, _>(array, |x| {
1962            as_float(x) / 10_f64.powi(*scale as i32)
1963        }),
1964        Utf8View => value_to_string_view(array, cast_options),
1965        Utf8 => value_to_string::<i32>(array, cast_options),
1966        LargeUtf8 => value_to_string::<i64>(array, cast_options),
1967        Null => Ok(new_null_array(to_type, array.len())),
1968        _ => Err(ArrowError::CastError(format!(
1969            "Casting from {from_type:?} to {to_type:?} not supported"
1970        ))),
1971    }
1972}
1973
1974fn cast_to_decimal<D, M>(
1975    array: &dyn Array,
1976    base: M,
1977    precision: &u8,
1978    scale: &i8,
1979    from_type: &DataType,
1980    to_type: &DataType,
1981    cast_options: &CastOptions,
1982) -> Result<ArrayRef, ArrowError>
1983where
1984    D: DecimalType + ArrowPrimitiveType<Native = M>,
1985    M: ArrowNativeTypeOp + DecimalCast,
1986    u8: num::traits::AsPrimitive<M>,
1987    u16: num::traits::AsPrimitive<M>,
1988    u32: num::traits::AsPrimitive<M>,
1989    u64: num::traits::AsPrimitive<M>,
1990    i8: num::traits::AsPrimitive<M>,
1991    i16: num::traits::AsPrimitive<M>,
1992    i32: num::traits::AsPrimitive<M>,
1993    i64: num::traits::AsPrimitive<M>,
1994{
1995    use DataType::*;
1996    // cast data to decimal
1997    match from_type {
1998        UInt8 => cast_integer_to_decimal::<_, D, M>(
1999            array.as_primitive::<UInt8Type>(),
2000            *precision,
2001            *scale,
2002            base,
2003            cast_options,
2004        ),
2005        UInt16 => cast_integer_to_decimal::<_, D, _>(
2006            array.as_primitive::<UInt16Type>(),
2007            *precision,
2008            *scale,
2009            base,
2010            cast_options,
2011        ),
2012        UInt32 => cast_integer_to_decimal::<_, D, _>(
2013            array.as_primitive::<UInt32Type>(),
2014            *precision,
2015            *scale,
2016            base,
2017            cast_options,
2018        ),
2019        UInt64 => cast_integer_to_decimal::<_, D, _>(
2020            array.as_primitive::<UInt64Type>(),
2021            *precision,
2022            *scale,
2023            base,
2024            cast_options,
2025        ),
2026        Int8 => cast_integer_to_decimal::<_, D, _>(
2027            array.as_primitive::<Int8Type>(),
2028            *precision,
2029            *scale,
2030            base,
2031            cast_options,
2032        ),
2033        Int16 => cast_integer_to_decimal::<_, D, _>(
2034            array.as_primitive::<Int16Type>(),
2035            *precision,
2036            *scale,
2037            base,
2038            cast_options,
2039        ),
2040        Int32 => cast_integer_to_decimal::<_, D, _>(
2041            array.as_primitive::<Int32Type>(),
2042            *precision,
2043            *scale,
2044            base,
2045            cast_options,
2046        ),
2047        Int64 => cast_integer_to_decimal::<_, D, _>(
2048            array.as_primitive::<Int64Type>(),
2049            *precision,
2050            *scale,
2051            base,
2052            cast_options,
2053        ),
2054        Float32 => cast_floating_point_to_decimal::<_, D>(
2055            array.as_primitive::<Float32Type>(),
2056            *precision,
2057            *scale,
2058            cast_options,
2059        ),
2060        Float64 => cast_floating_point_to_decimal::<_, D>(
2061            array.as_primitive::<Float64Type>(),
2062            *precision,
2063            *scale,
2064            cast_options,
2065        ),
2066        Utf8View | Utf8 => {
2067            cast_string_to_decimal::<D, i32>(array, *precision, *scale, cast_options)
2068        }
2069        LargeUtf8 => cast_string_to_decimal::<D, i64>(array, *precision, *scale, cast_options),
2070        Null => Ok(new_null_array(to_type, array.len())),
2071        _ => Err(ArrowError::CastError(format!(
2072            "Casting from {from_type:?} to {to_type:?} not supported"
2073        ))),
2074    }
2075}
2076
2077/// Get the time unit as a multiple of a second
2078const fn time_unit_multiple(unit: &TimeUnit) -> i64 {
2079    match unit {
2080        TimeUnit::Second => 1,
2081        TimeUnit::Millisecond => MILLISECONDS,
2082        TimeUnit::Microsecond => MICROSECONDS,
2083        TimeUnit::Nanosecond => NANOSECONDS,
2084    }
2085}
2086
2087/// Convert Array into a PrimitiveArray of type, and apply numeric cast
2088fn cast_numeric_arrays<FROM, TO>(
2089    from: &dyn Array,
2090    cast_options: &CastOptions,
2091) -> Result<ArrayRef, ArrowError>
2092where
2093    FROM: ArrowPrimitiveType,
2094    TO: ArrowPrimitiveType,
2095    FROM::Native: NumCast,
2096    TO::Native: NumCast,
2097{
2098    if cast_options.safe {
2099        // If the value can't be casted to the `TO::Native`, return null
2100        Ok(Arc::new(numeric_cast::<FROM, TO>(
2101            from.as_primitive::<FROM>(),
2102        )))
2103    } else {
2104        // If the value can't be casted to the `TO::Native`, return error
2105        Ok(Arc::new(try_numeric_cast::<FROM, TO>(
2106            from.as_primitive::<FROM>(),
2107        )?))
2108    }
2109}
2110
2111// Natural cast between numeric types
2112// If the value of T can't be casted to R, will throw error
2113fn try_numeric_cast<T, R>(from: &PrimitiveArray<T>) -> Result<PrimitiveArray<R>, ArrowError>
2114where
2115    T: ArrowPrimitiveType,
2116    R: ArrowPrimitiveType,
2117    T::Native: NumCast,
2118    R::Native: NumCast,
2119{
2120    from.try_unary(|value| {
2121        num::cast::cast::<T::Native, R::Native>(value).ok_or_else(|| {
2122            ArrowError::CastError(format!(
2123                "Can't cast value {:?} to type {}",
2124                value,
2125                R::DATA_TYPE
2126            ))
2127        })
2128    })
2129}
2130
2131// Natural cast between numeric types
2132// If the value of T can't be casted to R, it will be converted to null
2133fn numeric_cast<T, R>(from: &PrimitiveArray<T>) -> PrimitiveArray<R>
2134where
2135    T: ArrowPrimitiveType,
2136    R: ArrowPrimitiveType,
2137    T::Native: NumCast,
2138    R::Native: NumCast,
2139{
2140    from.unary_opt::<_, R>(num::cast::cast::<T::Native, R::Native>)
2141}
2142
2143fn cast_numeric_to_binary<FROM: ArrowPrimitiveType, O: OffsetSizeTrait>(
2144    array: &dyn Array,
2145) -> Result<ArrayRef, ArrowError> {
2146    let array = array.as_primitive::<FROM>();
2147    let size = std::mem::size_of::<FROM::Native>();
2148    let offsets = OffsetBuffer::from_lengths(std::iter::repeat(size).take(array.len()));
2149    Ok(Arc::new(GenericBinaryArray::<O>::new(
2150        offsets,
2151        array.values().inner().clone(),
2152        array.nulls().cloned(),
2153    )))
2154}
2155
2156fn adjust_timestamp_to_timezone<T: ArrowTimestampType>(
2157    array: PrimitiveArray<Int64Type>,
2158    to_tz: &Tz,
2159    cast_options: &CastOptions,
2160) -> Result<PrimitiveArray<Int64Type>, ArrowError> {
2161    let adjust = |o| {
2162        let local = as_datetime::<T>(o)?;
2163        let offset = to_tz.offset_from_local_datetime(&local).single()?;
2164        T::make_value(local - offset.fix())
2165    };
2166    let adjusted = if cast_options.safe {
2167        array.unary_opt::<_, Int64Type>(adjust)
2168    } else {
2169        array.try_unary::<_, Int64Type, _>(|o| {
2170            adjust(o).ok_or_else(|| {
2171                ArrowError::CastError("Cannot cast timezone to different timezone".to_string())
2172            })
2173        })?
2174    };
2175    Ok(adjusted)
2176}
2177
2178/// Cast numeric types to Boolean
2179///
2180/// Any zero value returns `false` while non-zero returns `true`
2181fn cast_numeric_to_bool<FROM>(from: &dyn Array) -> Result<ArrayRef, ArrowError>
2182where
2183    FROM: ArrowPrimitiveType,
2184{
2185    numeric_to_bool_cast::<FROM>(from.as_primitive::<FROM>()).map(|to| Arc::new(to) as ArrayRef)
2186}
2187
2188fn numeric_to_bool_cast<T>(from: &PrimitiveArray<T>) -> Result<BooleanArray, ArrowError>
2189where
2190    T: ArrowPrimitiveType + ArrowPrimitiveType,
2191{
2192    let mut b = BooleanBuilder::with_capacity(from.len());
2193
2194    for i in 0..from.len() {
2195        if from.is_null(i) {
2196            b.append_null();
2197        } else if from.value(i) != T::default_value() {
2198            b.append_value(true);
2199        } else {
2200            b.append_value(false);
2201        }
2202    }
2203
2204    Ok(b.finish())
2205}
2206
2207/// Cast Boolean types to numeric
2208///
2209/// `false` returns 0 while `true` returns 1
2210fn cast_bool_to_numeric<TO>(
2211    from: &dyn Array,
2212    cast_options: &CastOptions,
2213) -> Result<ArrayRef, ArrowError>
2214where
2215    TO: ArrowPrimitiveType,
2216    TO::Native: num::cast::NumCast,
2217{
2218    Ok(Arc::new(bool_to_numeric_cast::<TO>(
2219        from.as_any().downcast_ref::<BooleanArray>().unwrap(),
2220        cast_options,
2221    )))
2222}
2223
2224fn bool_to_numeric_cast<T>(from: &BooleanArray, _cast_options: &CastOptions) -> PrimitiveArray<T>
2225where
2226    T: ArrowPrimitiveType,
2227    T::Native: num::NumCast,
2228{
2229    let iter = (0..from.len()).map(|i| {
2230        if from.is_null(i) {
2231            None
2232        } else if from.value(i) {
2233            // a workaround to cast a primitive to T::Native, infallible
2234            num::cast::cast(1)
2235        } else {
2236            Some(T::default_value())
2237        }
2238    });
2239    // Benefit:
2240    //     20% performance improvement
2241    // Soundness:
2242    //     The iterator is trustedLen because it comes from a Range
2243    unsafe { PrimitiveArray::<T>::from_trusted_len_iter(iter) }
2244}
2245
2246/// Helper function to cast from one `BinaryArray` or 'LargeBinaryArray' to 'FixedSizeBinaryArray'.
2247fn cast_binary_to_fixed_size_binary<O: OffsetSizeTrait>(
2248    array: &dyn Array,
2249    byte_width: i32,
2250    cast_options: &CastOptions,
2251) -> Result<ArrayRef, ArrowError> {
2252    let array = array.as_binary::<O>();
2253    let mut builder = FixedSizeBinaryBuilder::with_capacity(array.len(), byte_width);
2254
2255    for i in 0..array.len() {
2256        if array.is_null(i) {
2257            builder.append_null();
2258        } else {
2259            match builder.append_value(array.value(i)) {
2260                Ok(_) => {}
2261                Err(e) => match cast_options.safe {
2262                    true => builder.append_null(),
2263                    false => return Err(e),
2264                },
2265            }
2266        }
2267    }
2268
2269    Ok(Arc::new(builder.finish()))
2270}
2271
2272/// Helper function to cast from 'FixedSizeBinaryArray' to one `BinaryArray` or 'LargeBinaryArray'.
2273/// If the target one is too large for the source array it will return an Error.
2274fn cast_fixed_size_binary_to_binary<O: OffsetSizeTrait>(
2275    array: &dyn Array,
2276    byte_width: i32,
2277) -> Result<ArrayRef, ArrowError> {
2278    let array = array
2279        .as_any()
2280        .downcast_ref::<FixedSizeBinaryArray>()
2281        .unwrap();
2282
2283    let offsets: i128 = byte_width as i128 * array.len() as i128;
2284
2285    let is_binary = matches!(GenericBinaryType::<O>::DATA_TYPE, DataType::Binary);
2286    if is_binary && offsets > i32::MAX as i128 {
2287        return Err(ArrowError::ComputeError(
2288            "FixedSizeBinary array too large to cast to Binary array".to_string(),
2289        ));
2290    } else if !is_binary && offsets > i64::MAX as i128 {
2291        return Err(ArrowError::ComputeError(
2292            "FixedSizeBinary array too large to cast to LargeBinary array".to_string(),
2293        ));
2294    }
2295
2296    let mut builder = GenericBinaryBuilder::<O>::with_capacity(array.len(), array.len());
2297
2298    for i in 0..array.len() {
2299        if array.is_null(i) {
2300            builder.append_null();
2301        } else {
2302            builder.append_value(array.value(i));
2303        }
2304    }
2305
2306    Ok(Arc::new(builder.finish()))
2307}
2308
2309/// Helper function to cast from one `ByteArrayType` to another and vice versa.
2310/// If the target one (e.g., `LargeUtf8`) is too large for the source array it will return an Error.
2311fn cast_byte_container<FROM, TO>(array: &dyn Array) -> Result<ArrayRef, ArrowError>
2312where
2313    FROM: ByteArrayType,
2314    TO: ByteArrayType<Native = FROM::Native>,
2315    FROM::Offset: OffsetSizeTrait + ToPrimitive,
2316    TO::Offset: OffsetSizeTrait + NumCast,
2317{
2318    let data = array.to_data();
2319    assert_eq!(data.data_type(), &FROM::DATA_TYPE);
2320    let str_values_buf = data.buffers()[1].clone();
2321    let offsets = data.buffers()[0].typed_data::<FROM::Offset>();
2322
2323    let mut offset_builder = BufferBuilder::<TO::Offset>::new(offsets.len());
2324    offsets
2325        .iter()
2326        .try_for_each::<_, Result<_, ArrowError>>(|offset| {
2327            let offset =
2328                <<TO as ByteArrayType>::Offset as NumCast>::from(*offset).ok_or_else(|| {
2329                    ArrowError::ComputeError(format!(
2330                        "{}{} array too large to cast to {}{} array",
2331                        FROM::Offset::PREFIX,
2332                        FROM::PREFIX,
2333                        TO::Offset::PREFIX,
2334                        TO::PREFIX
2335                    ))
2336                })?;
2337            offset_builder.append(offset);
2338            Ok(())
2339        })?;
2340
2341    let offset_buffer = offset_builder.finish();
2342
2343    let dtype = TO::DATA_TYPE;
2344
2345    let builder = ArrayData::builder(dtype)
2346        .offset(array.offset())
2347        .len(array.len())
2348        .add_buffer(offset_buffer)
2349        .add_buffer(str_values_buf)
2350        .nulls(data.nulls().cloned());
2351
2352    let array_data = unsafe { builder.build_unchecked() };
2353
2354    Ok(Arc::new(GenericByteArray::<TO>::from(array_data)))
2355}
2356
2357/// Helper function to cast from one `ByteViewType` array to `ByteArrayType` array.
2358fn cast_view_to_byte<FROM, TO>(array: &dyn Array) -> Result<ArrayRef, ArrowError>
2359where
2360    FROM: ByteViewType,
2361    TO: ByteArrayType,
2362    FROM::Native: AsRef<TO::Native>,
2363{
2364    let data = array.to_data();
2365    let view_array = GenericByteViewArray::<FROM>::from(data);
2366
2367    let len = view_array.len();
2368    let bytes = view_array
2369        .views()
2370        .iter()
2371        .map(|v| ByteView::from(*v).length as usize)
2372        .sum::<usize>();
2373
2374    let mut byte_array_builder = GenericByteBuilder::<TO>::with_capacity(len, bytes);
2375
2376    for val in view_array.iter() {
2377        byte_array_builder.append_option(val);
2378    }
2379
2380    Ok(Arc::new(byte_array_builder.finish()))
2381}
2382
2383#[cfg(test)]
2384mod tests {
2385    use super::*;
2386    use arrow_buffer::{Buffer, IntervalDayTime, NullBuffer};
2387    use chrono::NaiveDate;
2388    use half::f16;
2389
2390    macro_rules! generate_cast_test_case {
2391        ($INPUT_ARRAY: expr, $OUTPUT_TYPE_ARRAY: ident, $OUTPUT_TYPE: expr, $OUTPUT_VALUES: expr) => {
2392            let output =
2393                $OUTPUT_TYPE_ARRAY::from($OUTPUT_VALUES).with_data_type($OUTPUT_TYPE.clone());
2394
2395            // assert cast type
2396            let input_array_type = $INPUT_ARRAY.data_type();
2397            assert!(can_cast_types(input_array_type, $OUTPUT_TYPE));
2398            let result = cast($INPUT_ARRAY, $OUTPUT_TYPE).unwrap();
2399            assert_eq!($OUTPUT_TYPE, result.data_type());
2400            assert_eq!(result.as_ref(), &output);
2401
2402            let cast_option = CastOptions {
2403                safe: false,
2404                format_options: FormatOptions::default(),
2405            };
2406            let result = cast_with_options($INPUT_ARRAY, $OUTPUT_TYPE, &cast_option).unwrap();
2407            assert_eq!($OUTPUT_TYPE, result.data_type());
2408            assert_eq!(result.as_ref(), &output);
2409        };
2410    }
2411
2412    fn create_decimal128_array(
2413        array: Vec<Option<i128>>,
2414        precision: u8,
2415        scale: i8,
2416    ) -> Result<Decimal128Array, ArrowError> {
2417        array
2418            .into_iter()
2419            .collect::<Decimal128Array>()
2420            .with_precision_and_scale(precision, scale)
2421    }
2422
2423    fn create_decimal256_array(
2424        array: Vec<Option<i256>>,
2425        precision: u8,
2426        scale: i8,
2427    ) -> Result<Decimal256Array, ArrowError> {
2428        array
2429            .into_iter()
2430            .collect::<Decimal256Array>()
2431            .with_precision_and_scale(precision, scale)
2432    }
2433
2434    #[test]
2435    #[cfg(not(feature = "force_validate"))]
2436    #[should_panic(
2437        expected = "Cannot cast to Decimal128(20, 3). Overflowing on 57896044618658097711785492504343953926634992332820282019728792003956564819967"
2438    )]
2439    fn test_cast_decimal_to_decimal_round_with_error() {
2440        // decimal256 to decimal128 overflow
2441        let array = vec![
2442            Some(i256::from_i128(1123454)),
2443            Some(i256::from_i128(2123456)),
2444            Some(i256::from_i128(-3123453)),
2445            Some(i256::from_i128(-3123456)),
2446            None,
2447            Some(i256::MAX),
2448            Some(i256::MIN),
2449        ];
2450        let input_decimal_array = create_decimal256_array(array, 76, 4).unwrap();
2451        let array = Arc::new(input_decimal_array) as ArrayRef;
2452        let input_type = DataType::Decimal256(76, 4);
2453        let output_type = DataType::Decimal128(20, 3);
2454        assert!(can_cast_types(&input_type, &output_type));
2455        generate_cast_test_case!(
2456            &array,
2457            Decimal128Array,
2458            &output_type,
2459            vec![
2460                Some(112345_i128),
2461                Some(212346_i128),
2462                Some(-312345_i128),
2463                Some(-312346_i128),
2464                None,
2465                None,
2466                None,
2467            ]
2468        );
2469    }
2470
2471    #[test]
2472    #[cfg(not(feature = "force_validate"))]
2473    fn test_cast_decimal_to_decimal_round() {
2474        let array = vec![
2475            Some(1123454),
2476            Some(2123456),
2477            Some(-3123453),
2478            Some(-3123456),
2479            None,
2480        ];
2481        let array = create_decimal128_array(array, 20, 4).unwrap();
2482        // decimal128 to decimal128
2483        let input_type = DataType::Decimal128(20, 4);
2484        let output_type = DataType::Decimal128(20, 3);
2485        assert!(can_cast_types(&input_type, &output_type));
2486        generate_cast_test_case!(
2487            &array,
2488            Decimal128Array,
2489            &output_type,
2490            vec![
2491                Some(112345_i128),
2492                Some(212346_i128),
2493                Some(-312345_i128),
2494                Some(-312346_i128),
2495                None
2496            ]
2497        );
2498
2499        // decimal128 to decimal256
2500        let input_type = DataType::Decimal128(20, 4);
2501        let output_type = DataType::Decimal256(20, 3);
2502        assert!(can_cast_types(&input_type, &output_type));
2503        generate_cast_test_case!(
2504            &array,
2505            Decimal256Array,
2506            &output_type,
2507            vec![
2508                Some(i256::from_i128(112345_i128)),
2509                Some(i256::from_i128(212346_i128)),
2510                Some(i256::from_i128(-312345_i128)),
2511                Some(i256::from_i128(-312346_i128)),
2512                None
2513            ]
2514        );
2515
2516        // decimal256
2517        let array = vec![
2518            Some(i256::from_i128(1123454)),
2519            Some(i256::from_i128(2123456)),
2520            Some(i256::from_i128(-3123453)),
2521            Some(i256::from_i128(-3123456)),
2522            None,
2523        ];
2524        let array = create_decimal256_array(array, 20, 4).unwrap();
2525
2526        // decimal256 to decimal256
2527        let input_type = DataType::Decimal256(20, 4);
2528        let output_type = DataType::Decimal256(20, 3);
2529        assert!(can_cast_types(&input_type, &output_type));
2530        generate_cast_test_case!(
2531            &array,
2532            Decimal256Array,
2533            &output_type,
2534            vec![
2535                Some(i256::from_i128(112345_i128)),
2536                Some(i256::from_i128(212346_i128)),
2537                Some(i256::from_i128(-312345_i128)),
2538                Some(i256::from_i128(-312346_i128)),
2539                None
2540            ]
2541        );
2542        // decimal256 to decimal128
2543        let input_type = DataType::Decimal256(20, 4);
2544        let output_type = DataType::Decimal128(20, 3);
2545        assert!(can_cast_types(&input_type, &output_type));
2546        generate_cast_test_case!(
2547            &array,
2548            Decimal128Array,
2549            &output_type,
2550            vec![
2551                Some(112345_i128),
2552                Some(212346_i128),
2553                Some(-312345_i128),
2554                Some(-312346_i128),
2555                None
2556            ]
2557        );
2558    }
2559
2560    #[test]
2561    fn test_cast_decimal128_to_decimal128() {
2562        let input_type = DataType::Decimal128(20, 3);
2563        let output_type = DataType::Decimal128(20, 4);
2564        assert!(can_cast_types(&input_type, &output_type));
2565        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
2566        let array = create_decimal128_array(array, 20, 3).unwrap();
2567        generate_cast_test_case!(
2568            &array,
2569            Decimal128Array,
2570            &output_type,
2571            vec![
2572                Some(11234560_i128),
2573                Some(21234560_i128),
2574                Some(31234560_i128),
2575                None
2576            ]
2577        );
2578        // negative test
2579        let array = vec![Some(123456), None];
2580        let array = create_decimal128_array(array, 10, 0).unwrap();
2581        let result_safe = cast(&array, &DataType::Decimal128(2, 2));
2582        assert!(result_safe.is_ok());
2583        let options = CastOptions {
2584            safe: false,
2585            ..Default::default()
2586        };
2587
2588        let result_unsafe = cast_with_options(&array, &DataType::Decimal128(2, 2), &options);
2589        assert_eq!("Invalid argument error: 12345600 is too large to store in a Decimal128 of precision 2. Max is 99",
2590                   result_unsafe.unwrap_err().to_string());
2591    }
2592
2593    #[test]
2594    fn test_cast_decimal128_to_decimal128_dict() {
2595        let p = 20;
2596        let s = 3;
2597        let input_type = DataType::Decimal128(p, s);
2598        let output_type = DataType::Dictionary(
2599            Box::new(DataType::Int32),
2600            Box::new(DataType::Decimal128(p, s)),
2601        );
2602        assert!(can_cast_types(&input_type, &output_type));
2603        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
2604        let array = create_decimal128_array(array, p, s).unwrap();
2605        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
2606        assert_eq!(cast_array.data_type(), &output_type);
2607    }
2608
2609    #[test]
2610    fn test_cast_decimal256_to_decimal256_dict() {
2611        let p = 20;
2612        let s = 3;
2613        let input_type = DataType::Decimal256(p, s);
2614        let output_type = DataType::Dictionary(
2615            Box::new(DataType::Int32),
2616            Box::new(DataType::Decimal256(p, s)),
2617        );
2618        assert!(can_cast_types(&input_type, &output_type));
2619        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
2620        let array = create_decimal128_array(array, p, s).unwrap();
2621        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
2622        assert_eq!(cast_array.data_type(), &output_type);
2623    }
2624
2625    #[test]
2626    fn test_cast_decimal128_to_decimal128_overflow() {
2627        let input_type = DataType::Decimal128(38, 3);
2628        let output_type = DataType::Decimal128(38, 38);
2629        assert!(can_cast_types(&input_type, &output_type));
2630
2631        let array = vec![Some(i128::MAX)];
2632        let array = create_decimal128_array(array, 38, 3).unwrap();
2633        let result = cast_with_options(
2634            &array,
2635            &output_type,
2636            &CastOptions {
2637                safe: false,
2638                format_options: FormatOptions::default(),
2639            },
2640        );
2641        assert_eq!("Cast error: Cannot cast to Decimal128(38, 38). Overflowing on 170141183460469231731687303715884105727",
2642                   result.unwrap_err().to_string());
2643    }
2644
2645    #[test]
2646    fn test_cast_decimal128_to_decimal256_overflow() {
2647        let input_type = DataType::Decimal128(38, 3);
2648        let output_type = DataType::Decimal256(76, 76);
2649        assert!(can_cast_types(&input_type, &output_type));
2650
2651        let array = vec![Some(i128::MAX)];
2652        let array = create_decimal128_array(array, 38, 3).unwrap();
2653        let result = cast_with_options(
2654            &array,
2655            &output_type,
2656            &CastOptions {
2657                safe: false,
2658                format_options: FormatOptions::default(),
2659            },
2660        );
2661        assert_eq!("Cast error: Cannot cast to Decimal256(76, 76). Overflowing on 170141183460469231731687303715884105727",
2662                   result.unwrap_err().to_string());
2663    }
2664
2665    #[test]
2666    fn test_cast_decimal128_to_decimal256() {
2667        let input_type = DataType::Decimal128(20, 3);
2668        let output_type = DataType::Decimal256(20, 4);
2669        assert!(can_cast_types(&input_type, &output_type));
2670        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
2671        let array = create_decimal128_array(array, 20, 3).unwrap();
2672        generate_cast_test_case!(
2673            &array,
2674            Decimal256Array,
2675            &output_type,
2676            vec![
2677                Some(i256::from_i128(11234560_i128)),
2678                Some(i256::from_i128(21234560_i128)),
2679                Some(i256::from_i128(31234560_i128)),
2680                None
2681            ]
2682        );
2683    }
2684
2685    #[test]
2686    fn test_cast_decimal256_to_decimal128_overflow() {
2687        let input_type = DataType::Decimal256(76, 5);
2688        let output_type = DataType::Decimal128(38, 7);
2689        assert!(can_cast_types(&input_type, &output_type));
2690        let array = vec![Some(i256::from_i128(i128::MAX))];
2691        let array = create_decimal256_array(array, 76, 5).unwrap();
2692        let result = cast_with_options(
2693            &array,
2694            &output_type,
2695            &CastOptions {
2696                safe: false,
2697                format_options: FormatOptions::default(),
2698            },
2699        );
2700        assert_eq!("Cast error: Cannot cast to Decimal128(38, 7). Overflowing on 170141183460469231731687303715884105727",
2701                   result.unwrap_err().to_string());
2702    }
2703
2704    #[test]
2705    fn test_cast_decimal256_to_decimal256_overflow() {
2706        let input_type = DataType::Decimal256(76, 5);
2707        let output_type = DataType::Decimal256(76, 55);
2708        assert!(can_cast_types(&input_type, &output_type));
2709        let array = vec![Some(i256::from_i128(i128::MAX))];
2710        let array = create_decimal256_array(array, 76, 5).unwrap();
2711        let result = cast_with_options(
2712            &array,
2713            &output_type,
2714            &CastOptions {
2715                safe: false,
2716                format_options: FormatOptions::default(),
2717            },
2718        );
2719        assert_eq!("Cast error: Cannot cast to Decimal256(76, 55). Overflowing on 170141183460469231731687303715884105727",
2720                   result.unwrap_err().to_string());
2721    }
2722
2723    #[test]
2724    fn test_cast_decimal256_to_decimal128() {
2725        let input_type = DataType::Decimal256(20, 3);
2726        let output_type = DataType::Decimal128(20, 4);
2727        assert!(can_cast_types(&input_type, &output_type));
2728        let array = vec![
2729            Some(i256::from_i128(1123456)),
2730            Some(i256::from_i128(2123456)),
2731            Some(i256::from_i128(3123456)),
2732            None,
2733        ];
2734        let array = create_decimal256_array(array, 20, 3).unwrap();
2735        generate_cast_test_case!(
2736            &array,
2737            Decimal128Array,
2738            &output_type,
2739            vec![
2740                Some(11234560_i128),
2741                Some(21234560_i128),
2742                Some(31234560_i128),
2743                None
2744            ]
2745        );
2746    }
2747
2748    #[test]
2749    fn test_cast_decimal256_to_decimal256() {
2750        let input_type = DataType::Decimal256(20, 3);
2751        let output_type = DataType::Decimal256(20, 4);
2752        assert!(can_cast_types(&input_type, &output_type));
2753        let array = vec![
2754            Some(i256::from_i128(1123456)),
2755            Some(i256::from_i128(2123456)),
2756            Some(i256::from_i128(3123456)),
2757            None,
2758        ];
2759        let array = create_decimal256_array(array, 20, 3).unwrap();
2760        generate_cast_test_case!(
2761            &array,
2762            Decimal256Array,
2763            &output_type,
2764            vec![
2765                Some(i256::from_i128(11234560_i128)),
2766                Some(i256::from_i128(21234560_i128)),
2767                Some(i256::from_i128(31234560_i128)),
2768                None
2769            ]
2770        );
2771    }
2772
2773    fn generate_decimal_to_numeric_cast_test_case<T>(array: &PrimitiveArray<T>)
2774    where
2775        T: ArrowPrimitiveType + DecimalType,
2776    {
2777        // u8
2778        generate_cast_test_case!(
2779            array,
2780            UInt8Array,
2781            &DataType::UInt8,
2782            vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
2783        );
2784        // u16
2785        generate_cast_test_case!(
2786            array,
2787            UInt16Array,
2788            &DataType::UInt16,
2789            vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
2790        );
2791        // u32
2792        generate_cast_test_case!(
2793            array,
2794            UInt32Array,
2795            &DataType::UInt32,
2796            vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
2797        );
2798        // u64
2799        generate_cast_test_case!(
2800            array,
2801            UInt64Array,
2802            &DataType::UInt64,
2803            vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
2804        );
2805        // i8
2806        generate_cast_test_case!(
2807            array,
2808            Int8Array,
2809            &DataType::Int8,
2810            vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
2811        );
2812        // i16
2813        generate_cast_test_case!(
2814            array,
2815            Int16Array,
2816            &DataType::Int16,
2817            vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
2818        );
2819        // i32
2820        generate_cast_test_case!(
2821            array,
2822            Int32Array,
2823            &DataType::Int32,
2824            vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
2825        );
2826        // i64
2827        generate_cast_test_case!(
2828            array,
2829            Int64Array,
2830            &DataType::Int64,
2831            vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
2832        );
2833        // f32
2834        generate_cast_test_case!(
2835            array,
2836            Float32Array,
2837            &DataType::Float32,
2838            vec![
2839                Some(1.25_f32),
2840                Some(2.25_f32),
2841                Some(3.25_f32),
2842                None,
2843                Some(5.25_f32)
2844            ]
2845        );
2846        // f64
2847        generate_cast_test_case!(
2848            array,
2849            Float64Array,
2850            &DataType::Float64,
2851            vec![
2852                Some(1.25_f64),
2853                Some(2.25_f64),
2854                Some(3.25_f64),
2855                None,
2856                Some(5.25_f64)
2857            ]
2858        );
2859    }
2860
2861    #[test]
2862    fn test_cast_decimal128_to_numeric() {
2863        let value_array: Vec<Option<i128>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
2864        let array = create_decimal128_array(value_array, 38, 2).unwrap();
2865
2866        generate_decimal_to_numeric_cast_test_case(&array);
2867
2868        // overflow test: out of range of max u8
2869        let value_array: Vec<Option<i128>> = vec![Some(51300)];
2870        let array = create_decimal128_array(value_array, 38, 2).unwrap();
2871        let casted_array = cast_with_options(
2872            &array,
2873            &DataType::UInt8,
2874            &CastOptions {
2875                safe: false,
2876                format_options: FormatOptions::default(),
2877            },
2878        );
2879        assert_eq!(
2880            "Cast error: value of 513 is out of range UInt8".to_string(),
2881            casted_array.unwrap_err().to_string()
2882        );
2883
2884        let casted_array = cast_with_options(
2885            &array,
2886            &DataType::UInt8,
2887            &CastOptions {
2888                safe: true,
2889                format_options: FormatOptions::default(),
2890            },
2891        );
2892        assert!(casted_array.is_ok());
2893        assert!(casted_array.unwrap().is_null(0));
2894
2895        // overflow test: out of range of max i8
2896        let value_array: Vec<Option<i128>> = vec![Some(24400)];
2897        let array = create_decimal128_array(value_array, 38, 2).unwrap();
2898        let casted_array = cast_with_options(
2899            &array,
2900            &DataType::Int8,
2901            &CastOptions {
2902                safe: false,
2903                format_options: FormatOptions::default(),
2904            },
2905        );
2906        assert_eq!(
2907            "Cast error: value of 244 is out of range Int8".to_string(),
2908            casted_array.unwrap_err().to_string()
2909        );
2910
2911        let casted_array = cast_with_options(
2912            &array,
2913            &DataType::Int8,
2914            &CastOptions {
2915                safe: true,
2916                format_options: FormatOptions::default(),
2917            },
2918        );
2919        assert!(casted_array.is_ok());
2920        assert!(casted_array.unwrap().is_null(0));
2921
2922        // loss the precision: convert decimal to f32、f64
2923        // f32
2924        // 112345678_f32 and 112345679_f32 are same, so the 112345679_f32 will lose precision.
2925        let value_array: Vec<Option<i128>> = vec![
2926            Some(125),
2927            Some(225),
2928            Some(325),
2929            None,
2930            Some(525),
2931            Some(112345678),
2932            Some(112345679),
2933        ];
2934        let array = create_decimal128_array(value_array, 38, 2).unwrap();
2935        generate_cast_test_case!(
2936            &array,
2937            Float32Array,
2938            &DataType::Float32,
2939            vec![
2940                Some(1.25_f32),
2941                Some(2.25_f32),
2942                Some(3.25_f32),
2943                None,
2944                Some(5.25_f32),
2945                Some(1_123_456.7_f32),
2946                Some(1_123_456.7_f32)
2947            ]
2948        );
2949
2950        // f64
2951        // 112345678901234568_f64 and 112345678901234560_f64 are same, so the 112345678901234568_f64 will lose precision.
2952        let value_array: Vec<Option<i128>> = vec![
2953            Some(125),
2954            Some(225),
2955            Some(325),
2956            None,
2957            Some(525),
2958            Some(112345678901234568),
2959            Some(112345678901234560),
2960        ];
2961        let array = create_decimal128_array(value_array, 38, 2).unwrap();
2962        generate_cast_test_case!(
2963            &array,
2964            Float64Array,
2965            &DataType::Float64,
2966            vec![
2967                Some(1.25_f64),
2968                Some(2.25_f64),
2969                Some(3.25_f64),
2970                None,
2971                Some(5.25_f64),
2972                Some(1_123_456_789_012_345.6_f64),
2973                Some(1_123_456_789_012_345.6_f64),
2974            ]
2975        );
2976    }
2977
2978    #[test]
2979    fn test_cast_decimal256_to_numeric() {
2980        let value_array: Vec<Option<i256>> = vec![
2981            Some(i256::from_i128(125)),
2982            Some(i256::from_i128(225)),
2983            Some(i256::from_i128(325)),
2984            None,
2985            Some(i256::from_i128(525)),
2986        ];
2987        let array = create_decimal256_array(value_array, 38, 2).unwrap();
2988        // u8
2989        generate_cast_test_case!(
2990            &array,
2991            UInt8Array,
2992            &DataType::UInt8,
2993            vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
2994        );
2995        // u16
2996        generate_cast_test_case!(
2997            &array,
2998            UInt16Array,
2999            &DataType::UInt16,
3000            vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
3001        );
3002        // u32
3003        generate_cast_test_case!(
3004            &array,
3005            UInt32Array,
3006            &DataType::UInt32,
3007            vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
3008        );
3009        // u64
3010        generate_cast_test_case!(
3011            &array,
3012            UInt64Array,
3013            &DataType::UInt64,
3014            vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
3015        );
3016        // i8
3017        generate_cast_test_case!(
3018            &array,
3019            Int8Array,
3020            &DataType::Int8,
3021            vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
3022        );
3023        // i16
3024        generate_cast_test_case!(
3025            &array,
3026            Int16Array,
3027            &DataType::Int16,
3028            vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
3029        );
3030        // i32
3031        generate_cast_test_case!(
3032            &array,
3033            Int32Array,
3034            &DataType::Int32,
3035            vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
3036        );
3037        // i64
3038        generate_cast_test_case!(
3039            &array,
3040            Int64Array,
3041            &DataType::Int64,
3042            vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
3043        );
3044        // f32
3045        generate_cast_test_case!(
3046            &array,
3047            Float32Array,
3048            &DataType::Float32,
3049            vec![
3050                Some(1.25_f32),
3051                Some(2.25_f32),
3052                Some(3.25_f32),
3053                None,
3054                Some(5.25_f32)
3055            ]
3056        );
3057        // f64
3058        generate_cast_test_case!(
3059            &array,
3060            Float64Array,
3061            &DataType::Float64,
3062            vec![
3063                Some(1.25_f64),
3064                Some(2.25_f64),
3065                Some(3.25_f64),
3066                None,
3067                Some(5.25_f64)
3068            ]
3069        );
3070
3071        // overflow test: out of range of max i8
3072        let value_array: Vec<Option<i256>> = vec![Some(i256::from_i128(24400))];
3073        let array = create_decimal256_array(value_array, 38, 2).unwrap();
3074        let casted_array = cast_with_options(
3075            &array,
3076            &DataType::Int8,
3077            &CastOptions {
3078                safe: false,
3079                format_options: FormatOptions::default(),
3080            },
3081        );
3082        assert_eq!(
3083            "Cast error: value of 244 is out of range Int8".to_string(),
3084            casted_array.unwrap_err().to_string()
3085        );
3086
3087        let casted_array = cast_with_options(
3088            &array,
3089            &DataType::Int8,
3090            &CastOptions {
3091                safe: true,
3092                format_options: FormatOptions::default(),
3093            },
3094        );
3095        assert!(casted_array.is_ok());
3096        assert!(casted_array.unwrap().is_null(0));
3097
3098        // loss the precision: convert decimal to f32、f64
3099        // f32
3100        // 112345678_f32 and 112345679_f32 are same, so the 112345679_f32 will lose precision.
3101        let value_array: Vec<Option<i256>> = vec![
3102            Some(i256::from_i128(125)),
3103            Some(i256::from_i128(225)),
3104            Some(i256::from_i128(325)),
3105            None,
3106            Some(i256::from_i128(525)),
3107            Some(i256::from_i128(112345678)),
3108            Some(i256::from_i128(112345679)),
3109        ];
3110        let array = create_decimal256_array(value_array, 76, 2).unwrap();
3111        generate_cast_test_case!(
3112            &array,
3113            Float32Array,
3114            &DataType::Float32,
3115            vec![
3116                Some(1.25_f32),
3117                Some(2.25_f32),
3118                Some(3.25_f32),
3119                None,
3120                Some(5.25_f32),
3121                Some(1_123_456.7_f32),
3122                Some(1_123_456.7_f32)
3123            ]
3124        );
3125
3126        // f64
3127        // 112345678901234568_f64 and 112345678901234560_f64 are same, so the 112345678901234568_f64 will lose precision.
3128        let value_array: Vec<Option<i256>> = vec![
3129            Some(i256::from_i128(125)),
3130            Some(i256::from_i128(225)),
3131            Some(i256::from_i128(325)),
3132            None,
3133            Some(i256::from_i128(525)),
3134            Some(i256::from_i128(112345678901234568)),
3135            Some(i256::from_i128(112345678901234560)),
3136        ];
3137        let array = create_decimal256_array(value_array, 76, 2).unwrap();
3138        generate_cast_test_case!(
3139            &array,
3140            Float64Array,
3141            &DataType::Float64,
3142            vec![
3143                Some(1.25_f64),
3144                Some(2.25_f64),
3145                Some(3.25_f64),
3146                None,
3147                Some(5.25_f64),
3148                Some(1_123_456_789_012_345.6_f64),
3149                Some(1_123_456_789_012_345.6_f64),
3150            ]
3151        );
3152    }
3153
3154    #[test]
3155    fn test_cast_numeric_to_decimal128() {
3156        let decimal_type = DataType::Decimal128(38, 6);
3157        // u8, u16, u32, u64
3158        let input_datas = vec![
3159            Arc::new(UInt8Array::from(vec![
3160                Some(1),
3161                Some(2),
3162                Some(3),
3163                None,
3164                Some(5),
3165            ])) as ArrayRef, // u8
3166            Arc::new(UInt16Array::from(vec![
3167                Some(1),
3168                Some(2),
3169                Some(3),
3170                None,
3171                Some(5),
3172            ])) as ArrayRef, // u16
3173            Arc::new(UInt32Array::from(vec![
3174                Some(1),
3175                Some(2),
3176                Some(3),
3177                None,
3178                Some(5),
3179            ])) as ArrayRef, // u32
3180            Arc::new(UInt64Array::from(vec![
3181                Some(1),
3182                Some(2),
3183                Some(3),
3184                None,
3185                Some(5),
3186            ])) as ArrayRef, // u64
3187        ];
3188
3189        for array in input_datas {
3190            generate_cast_test_case!(
3191                &array,
3192                Decimal128Array,
3193                &decimal_type,
3194                vec![
3195                    Some(1000000_i128),
3196                    Some(2000000_i128),
3197                    Some(3000000_i128),
3198                    None,
3199                    Some(5000000_i128)
3200                ]
3201            );
3202        }
3203
3204        // i8, i16, i32, i64
3205        let input_datas = vec![
3206            Arc::new(Int8Array::from(vec![
3207                Some(1),
3208                Some(2),
3209                Some(3),
3210                None,
3211                Some(5),
3212            ])) as ArrayRef, // i8
3213            Arc::new(Int16Array::from(vec![
3214                Some(1),
3215                Some(2),
3216                Some(3),
3217                None,
3218                Some(5),
3219            ])) as ArrayRef, // i16
3220            Arc::new(Int32Array::from(vec![
3221                Some(1),
3222                Some(2),
3223                Some(3),
3224                None,
3225                Some(5),
3226            ])) as ArrayRef, // i32
3227            Arc::new(Int64Array::from(vec![
3228                Some(1),
3229                Some(2),
3230                Some(3),
3231                None,
3232                Some(5),
3233            ])) as ArrayRef, // i64
3234        ];
3235        for array in input_datas {
3236            generate_cast_test_case!(
3237                &array,
3238                Decimal128Array,
3239                &decimal_type,
3240                vec![
3241                    Some(1000000_i128),
3242                    Some(2000000_i128),
3243                    Some(3000000_i128),
3244                    None,
3245                    Some(5000000_i128)
3246                ]
3247            );
3248        }
3249
3250        // test u8 to decimal type with overflow the result type
3251        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
3252        let array = UInt8Array::from(vec![1, 2, 3, 4, 100]);
3253        let casted_array = cast(&array, &DataType::Decimal128(3, 1));
3254        assert!(casted_array.is_ok());
3255        let array = casted_array.unwrap();
3256        let array: &Decimal128Array = array.as_primitive();
3257        assert!(array.is_null(4));
3258
3259        // test i8 to decimal type with overflow the result type
3260        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
3261        let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
3262        let casted_array = cast(&array, &DataType::Decimal128(3, 1));
3263        assert!(casted_array.is_ok());
3264        let array = casted_array.unwrap();
3265        let array: &Decimal128Array = array.as_primitive();
3266        assert!(array.is_null(4));
3267
3268        // test f32 to decimal type
3269        let array = Float32Array::from(vec![
3270            Some(1.1),
3271            Some(2.2),
3272            Some(4.4),
3273            None,
3274            Some(1.123_456_4), // round down
3275            Some(1.123_456_7), // round up
3276        ]);
3277        let array = Arc::new(array) as ArrayRef;
3278        generate_cast_test_case!(
3279            &array,
3280            Decimal128Array,
3281            &decimal_type,
3282            vec![
3283                Some(1100000_i128),
3284                Some(2200000_i128),
3285                Some(4400000_i128),
3286                None,
3287                Some(1123456_i128), // round down
3288                Some(1123457_i128), // round up
3289            ]
3290        );
3291
3292        // test f64 to decimal type
3293        let array = Float64Array::from(vec![
3294            Some(1.1),
3295            Some(2.2),
3296            Some(4.4),
3297            None,
3298            Some(1.123_456_489_123_4),     // round up
3299            Some(1.123_456_789_123_4),     // round up
3300            Some(1.123_456_489_012_345_6), // round down
3301            Some(1.123_456_789_012_345_6), // round up
3302        ]);
3303        generate_cast_test_case!(
3304            &array,
3305            Decimal128Array,
3306            &decimal_type,
3307            vec![
3308                Some(1100000_i128),
3309                Some(2200000_i128),
3310                Some(4400000_i128),
3311                None,
3312                Some(1123456_i128), // round down
3313                Some(1123457_i128), // round up
3314                Some(1123456_i128), // round down
3315                Some(1123457_i128), // round up
3316            ]
3317        );
3318    }
3319
3320    #[test]
3321    fn test_cast_numeric_to_decimal256() {
3322        let decimal_type = DataType::Decimal256(76, 6);
3323        // u8, u16, u32, u64
3324        let input_datas = vec![
3325            Arc::new(UInt8Array::from(vec![
3326                Some(1),
3327                Some(2),
3328                Some(3),
3329                None,
3330                Some(5),
3331            ])) as ArrayRef, // u8
3332            Arc::new(UInt16Array::from(vec![
3333                Some(1),
3334                Some(2),
3335                Some(3),
3336                None,
3337                Some(5),
3338            ])) as ArrayRef, // u16
3339            Arc::new(UInt32Array::from(vec![
3340                Some(1),
3341                Some(2),
3342                Some(3),
3343                None,
3344                Some(5),
3345            ])) as ArrayRef, // u32
3346            Arc::new(UInt64Array::from(vec![
3347                Some(1),
3348                Some(2),
3349                Some(3),
3350                None,
3351                Some(5),
3352            ])) as ArrayRef, // u64
3353        ];
3354
3355        for array in input_datas {
3356            generate_cast_test_case!(
3357                &array,
3358                Decimal256Array,
3359                &decimal_type,
3360                vec![
3361                    Some(i256::from_i128(1000000_i128)),
3362                    Some(i256::from_i128(2000000_i128)),
3363                    Some(i256::from_i128(3000000_i128)),
3364                    None,
3365                    Some(i256::from_i128(5000000_i128))
3366                ]
3367            );
3368        }
3369
3370        // i8, i16, i32, i64
3371        let input_datas = vec![
3372            Arc::new(Int8Array::from(vec![
3373                Some(1),
3374                Some(2),
3375                Some(3),
3376                None,
3377                Some(5),
3378            ])) as ArrayRef, // i8
3379            Arc::new(Int16Array::from(vec![
3380                Some(1),
3381                Some(2),
3382                Some(3),
3383                None,
3384                Some(5),
3385            ])) as ArrayRef, // i16
3386            Arc::new(Int32Array::from(vec![
3387                Some(1),
3388                Some(2),
3389                Some(3),
3390                None,
3391                Some(5),
3392            ])) as ArrayRef, // i32
3393            Arc::new(Int64Array::from(vec![
3394                Some(1),
3395                Some(2),
3396                Some(3),
3397                None,
3398                Some(5),
3399            ])) as ArrayRef, // i64
3400        ];
3401        for array in input_datas {
3402            generate_cast_test_case!(
3403                &array,
3404                Decimal256Array,
3405                &decimal_type,
3406                vec![
3407                    Some(i256::from_i128(1000000_i128)),
3408                    Some(i256::from_i128(2000000_i128)),
3409                    Some(i256::from_i128(3000000_i128)),
3410                    None,
3411                    Some(i256::from_i128(5000000_i128))
3412                ]
3413            );
3414        }
3415
3416        // test i8 to decimal type with overflow the result type
3417        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
3418        let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
3419        let array = Arc::new(array) as ArrayRef;
3420        let casted_array = cast(&array, &DataType::Decimal256(3, 1));
3421        assert!(casted_array.is_ok());
3422        let array = casted_array.unwrap();
3423        let array: &Decimal256Array = array.as_primitive();
3424        assert!(array.is_null(4));
3425
3426        // test f32 to decimal type
3427        let array = Float32Array::from(vec![
3428            Some(1.1),
3429            Some(2.2),
3430            Some(4.4),
3431            None,
3432            Some(1.123_456_4), // round down
3433            Some(1.123_456_7), // round up
3434        ]);
3435        generate_cast_test_case!(
3436            &array,
3437            Decimal256Array,
3438            &decimal_type,
3439            vec![
3440                Some(i256::from_i128(1100000_i128)),
3441                Some(i256::from_i128(2200000_i128)),
3442                Some(i256::from_i128(4400000_i128)),
3443                None,
3444                Some(i256::from_i128(1123456_i128)), // round down
3445                Some(i256::from_i128(1123457_i128)), // round up
3446            ]
3447        );
3448
3449        // test f64 to decimal type
3450        let array = Float64Array::from(vec![
3451            Some(1.1),
3452            Some(2.2),
3453            Some(4.4),
3454            None,
3455            Some(1.123_456_489_123_4),     // round down
3456            Some(1.123_456_789_123_4),     // round up
3457            Some(1.123_456_489_012_345_6), // round down
3458            Some(1.123_456_789_012_345_6), // round up
3459        ]);
3460        generate_cast_test_case!(
3461            &array,
3462            Decimal256Array,
3463            &decimal_type,
3464            vec![
3465                Some(i256::from_i128(1100000_i128)),
3466                Some(i256::from_i128(2200000_i128)),
3467                Some(i256::from_i128(4400000_i128)),
3468                None,
3469                Some(i256::from_i128(1123456_i128)), // round down
3470                Some(i256::from_i128(1123457_i128)), // round up
3471                Some(i256::from_i128(1123456_i128)), // round down
3472                Some(i256::from_i128(1123457_i128)), // round up
3473            ]
3474        );
3475    }
3476
3477    #[test]
3478    fn test_cast_i32_to_f64() {
3479        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
3480        let b = cast(&array, &DataType::Float64).unwrap();
3481        let c = b.as_primitive::<Float64Type>();
3482        assert_eq!(5.0, c.value(0));
3483        assert_eq!(6.0, c.value(1));
3484        assert_eq!(7.0, c.value(2));
3485        assert_eq!(8.0, c.value(3));
3486        assert_eq!(9.0, c.value(4));
3487    }
3488
3489    #[test]
3490    fn test_cast_i32_to_u8() {
3491        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
3492        let b = cast(&array, &DataType::UInt8).unwrap();
3493        let c = b.as_primitive::<UInt8Type>();
3494        assert!(!c.is_valid(0));
3495        assert_eq!(6, c.value(1));
3496        assert!(!c.is_valid(2));
3497        assert_eq!(8, c.value(3));
3498        // overflows return None
3499        assert!(!c.is_valid(4));
3500    }
3501
3502    #[test]
3503    #[should_panic(expected = "Can't cast value -5 to type UInt8")]
3504    fn test_cast_int32_to_u8_with_error() {
3505        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
3506        // overflow with the error
3507        let cast_option = CastOptions {
3508            safe: false,
3509            format_options: FormatOptions::default(),
3510        };
3511        let result = cast_with_options(&array, &DataType::UInt8, &cast_option);
3512        assert!(result.is_err());
3513        result.unwrap();
3514    }
3515
3516    #[test]
3517    fn test_cast_i32_to_u8_sliced() {
3518        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
3519        assert_eq!(0, array.offset());
3520        let array = array.slice(2, 3);
3521        let b = cast(&array, &DataType::UInt8).unwrap();
3522        assert_eq!(3, b.len());
3523        let c = b.as_primitive::<UInt8Type>();
3524        assert!(!c.is_valid(0));
3525        assert_eq!(8, c.value(1));
3526        // overflows return None
3527        assert!(!c.is_valid(2));
3528    }
3529
3530    #[test]
3531    fn test_cast_i32_to_i32() {
3532        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
3533        let b = cast(&array, &DataType::Int32).unwrap();
3534        let c = b.as_primitive::<Int32Type>();
3535        assert_eq!(5, c.value(0));
3536        assert_eq!(6, c.value(1));
3537        assert_eq!(7, c.value(2));
3538        assert_eq!(8, c.value(3));
3539        assert_eq!(9, c.value(4));
3540    }
3541
3542    #[test]
3543    fn test_cast_i32_to_list_i32() {
3544        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
3545        let b = cast(
3546            &array,
3547            &DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
3548        )
3549        .unwrap();
3550        assert_eq!(5, b.len());
3551        let arr = b.as_list::<i32>();
3552        assert_eq!(&[0, 1, 2, 3, 4, 5], arr.value_offsets());
3553        assert_eq!(1, arr.value_length(0));
3554        assert_eq!(1, arr.value_length(1));
3555        assert_eq!(1, arr.value_length(2));
3556        assert_eq!(1, arr.value_length(3));
3557        assert_eq!(1, arr.value_length(4));
3558        let c = arr.values().as_primitive::<Int32Type>();
3559        assert_eq!(5, c.value(0));
3560        assert_eq!(6, c.value(1));
3561        assert_eq!(7, c.value(2));
3562        assert_eq!(8, c.value(3));
3563        assert_eq!(9, c.value(4));
3564    }
3565
3566    #[test]
3567    fn test_cast_i32_to_list_i32_nullable() {
3568        let array = Int32Array::from(vec![Some(5), None, Some(7), Some(8), Some(9)]);
3569        let b = cast(
3570            &array,
3571            &DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
3572        )
3573        .unwrap();
3574        assert_eq!(5, b.len());
3575        assert_eq!(0, b.null_count());
3576        let arr = b.as_list::<i32>();
3577        assert_eq!(&[0, 1, 2, 3, 4, 5], arr.value_offsets());
3578        assert_eq!(1, arr.value_length(0));
3579        assert_eq!(1, arr.value_length(1));
3580        assert_eq!(1, arr.value_length(2));
3581        assert_eq!(1, arr.value_length(3));
3582        assert_eq!(1, arr.value_length(4));
3583
3584        let c = arr.values().as_primitive::<Int32Type>();
3585        assert_eq!(1, c.null_count());
3586        assert_eq!(5, c.value(0));
3587        assert!(!c.is_valid(1));
3588        assert_eq!(7, c.value(2));
3589        assert_eq!(8, c.value(3));
3590        assert_eq!(9, c.value(4));
3591    }
3592
3593    #[test]
3594    fn test_cast_i32_to_list_f64_nullable_sliced() {
3595        let array = Int32Array::from(vec![Some(5), None, Some(7), Some(8), None, Some(10)]);
3596        let array = array.slice(2, 4);
3597        let b = cast(
3598            &array,
3599            &DataType::List(Arc::new(Field::new_list_field(DataType::Float64, true))),
3600        )
3601        .unwrap();
3602        assert_eq!(4, b.len());
3603        assert_eq!(0, b.null_count());
3604        let arr = b.as_list::<i32>();
3605        assert_eq!(&[0, 1, 2, 3, 4], arr.value_offsets());
3606        assert_eq!(1, arr.value_length(0));
3607        assert_eq!(1, arr.value_length(1));
3608        assert_eq!(1, arr.value_length(2));
3609        assert_eq!(1, arr.value_length(3));
3610        let c = arr.values().as_primitive::<Float64Type>();
3611        assert_eq!(1, c.null_count());
3612        assert_eq!(7.0, c.value(0));
3613        assert_eq!(8.0, c.value(1));
3614        assert!(!c.is_valid(2));
3615        assert_eq!(10.0, c.value(3));
3616    }
3617
3618    #[test]
3619    fn test_cast_int_to_utf8view() {
3620        let inputs = vec![
3621            Arc::new(Int8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
3622            Arc::new(Int16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
3623            Arc::new(Int32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
3624            Arc::new(Int64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
3625            Arc::new(UInt8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
3626            Arc::new(UInt16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
3627            Arc::new(UInt32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
3628            Arc::new(UInt64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
3629        ];
3630        let expected: ArrayRef = Arc::new(StringViewArray::from(vec![
3631            None,
3632            Some("8"),
3633            Some("9"),
3634            Some("10"),
3635        ]));
3636
3637        for array in inputs {
3638            assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
3639            let arr = cast(&array, &DataType::Utf8View).unwrap();
3640            assert_eq!(expected.as_ref(), arr.as_ref());
3641        }
3642    }
3643
3644    #[test]
3645    fn test_cast_float_to_utf8view() {
3646        let inputs = vec![
3647            Arc::new(Float16Array::from(vec![
3648                Some(f16::from_f64(1.5)),
3649                Some(f16::from_f64(2.5)),
3650                None,
3651            ])) as ArrayRef,
3652            Arc::new(Float32Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
3653            Arc::new(Float64Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
3654        ];
3655
3656        let expected: ArrayRef =
3657            Arc::new(StringViewArray::from(vec![Some("1.5"), Some("2.5"), None]));
3658
3659        for array in inputs {
3660            assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
3661            let arr = cast(&array, &DataType::Utf8View).unwrap();
3662            assert_eq!(expected.as_ref(), arr.as_ref());
3663        }
3664    }
3665
3666    #[test]
3667    fn test_cast_utf8_to_i32() {
3668        let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
3669        let b = cast(&array, &DataType::Int32).unwrap();
3670        let c = b.as_primitive::<Int32Type>();
3671        assert_eq!(5, c.value(0));
3672        assert_eq!(6, c.value(1));
3673        assert!(!c.is_valid(2));
3674        assert_eq!(8, c.value(3));
3675        assert!(!c.is_valid(4));
3676    }
3677
3678    #[test]
3679    fn test_cast_utf8view_to_i32() {
3680        let array = StringViewArray::from(vec!["5", "6", "seven", "8", "9.1"]);
3681        let b = cast(&array, &DataType::Int32).unwrap();
3682        let c = b.as_primitive::<Int32Type>();
3683        assert_eq!(5, c.value(0));
3684        assert_eq!(6, c.value(1));
3685        assert!(!c.is_valid(2));
3686        assert_eq!(8, c.value(3));
3687        assert!(!c.is_valid(4));
3688    }
3689
3690    #[test]
3691    fn test_cast_utf8view_to_f32() {
3692        let array = StringViewArray::from(vec!["3", "4.56", "seven", "8.9"]);
3693        let b = cast(&array, &DataType::Float32).unwrap();
3694        let c = b.as_primitive::<Float32Type>();
3695        assert_eq!(3.0, c.value(0));
3696        assert_eq!(4.56, c.value(1));
3697        assert!(!c.is_valid(2));
3698        assert_eq!(8.9, c.value(3));
3699    }
3700
3701    #[test]
3702    fn test_cast_utf8view_to_decimal128() {
3703        let array = StringViewArray::from(vec![None, Some("4"), Some("5.6"), Some("7.89")]);
3704        let arr = Arc::new(array) as ArrayRef;
3705        generate_cast_test_case!(
3706            &arr,
3707            Decimal128Array,
3708            &DataType::Decimal128(4, 2),
3709            vec![None, Some(400_i128), Some(560_i128), Some(789_i128)]
3710        );
3711    }
3712
3713    #[test]
3714    fn test_cast_with_options_utf8_to_i32() {
3715        let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
3716        let result = cast_with_options(
3717            &array,
3718            &DataType::Int32,
3719            &CastOptions {
3720                safe: false,
3721                format_options: FormatOptions::default(),
3722            },
3723        );
3724        match result {
3725            Ok(_) => panic!("expected error"),
3726            Err(e) => {
3727                assert!(
3728                    e.to_string()
3729                        .contains("Cast error: Cannot cast string 'seven' to value of Int32 type",),
3730                    "Error: {e}"
3731                )
3732            }
3733        }
3734    }
3735
3736    #[test]
3737    fn test_cast_utf8_to_bool() {
3738        let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
3739        let casted = cast(&strings, &DataType::Boolean).unwrap();
3740        let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
3741        assert_eq!(*as_boolean_array(&casted), expected);
3742    }
3743
3744    #[test]
3745    fn test_cast_utf8view_to_bool() {
3746        let strings = StringViewArray::from(vec!["true", "false", "invalid", " Y ", ""]);
3747        let casted = cast(&strings, &DataType::Boolean).unwrap();
3748        let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
3749        assert_eq!(*as_boolean_array(&casted), expected);
3750    }
3751
3752    #[test]
3753    fn test_cast_with_options_utf8_to_bool() {
3754        let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
3755        let casted = cast_with_options(
3756            &strings,
3757            &DataType::Boolean,
3758            &CastOptions {
3759                safe: false,
3760                format_options: FormatOptions::default(),
3761            },
3762        );
3763        match casted {
3764            Ok(_) => panic!("expected error"),
3765            Err(e) => {
3766                assert!(e
3767                    .to_string()
3768                    .contains("Cast error: Cannot cast value 'invalid' to value of Boolean type"))
3769            }
3770        }
3771    }
3772
3773    #[test]
3774    fn test_cast_bool_to_i32() {
3775        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
3776        let b = cast(&array, &DataType::Int32).unwrap();
3777        let c = b.as_primitive::<Int32Type>();
3778        assert_eq!(1, c.value(0));
3779        assert_eq!(0, c.value(1));
3780        assert!(!c.is_valid(2));
3781    }
3782
3783    #[test]
3784    fn test_cast_bool_to_utf8view() {
3785        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
3786        let b = cast(&array, &DataType::Utf8View).unwrap();
3787        let c = b.as_any().downcast_ref::<StringViewArray>().unwrap();
3788        assert_eq!("true", c.value(0));
3789        assert_eq!("false", c.value(1));
3790        assert!(!c.is_valid(2));
3791    }
3792
3793    #[test]
3794    fn test_cast_bool_to_utf8() {
3795        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
3796        let b = cast(&array, &DataType::Utf8).unwrap();
3797        let c = b.as_any().downcast_ref::<StringArray>().unwrap();
3798        assert_eq!("true", c.value(0));
3799        assert_eq!("false", c.value(1));
3800        assert!(!c.is_valid(2));
3801    }
3802
3803    #[test]
3804    fn test_cast_bool_to_large_utf8() {
3805        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
3806        let b = cast(&array, &DataType::LargeUtf8).unwrap();
3807        let c = b.as_any().downcast_ref::<LargeStringArray>().unwrap();
3808        assert_eq!("true", c.value(0));
3809        assert_eq!("false", c.value(1));
3810        assert!(!c.is_valid(2));
3811    }
3812
3813    #[test]
3814    fn test_cast_bool_to_f64() {
3815        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
3816        let b = cast(&array, &DataType::Float64).unwrap();
3817        let c = b.as_primitive::<Float64Type>();
3818        assert_eq!(1.0, c.value(0));
3819        assert_eq!(0.0, c.value(1));
3820        assert!(!c.is_valid(2));
3821    }
3822
3823    #[test]
3824    fn test_cast_integer_to_timestamp() {
3825        let array = Int64Array::from(vec![Some(2), Some(10), None]);
3826        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3827
3828        let array = Int8Array::from(vec![Some(2), Some(10), None]);
3829        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3830
3831        assert_eq!(&actual, &expected);
3832
3833        let array = Int16Array::from(vec![Some(2), Some(10), None]);
3834        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3835
3836        assert_eq!(&actual, &expected);
3837
3838        let array = Int32Array::from(vec![Some(2), Some(10), None]);
3839        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3840
3841        assert_eq!(&actual, &expected);
3842
3843        let array = UInt8Array::from(vec![Some(2), Some(10), None]);
3844        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3845
3846        assert_eq!(&actual, &expected);
3847
3848        let array = UInt16Array::from(vec![Some(2), Some(10), None]);
3849        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3850
3851        assert_eq!(&actual, &expected);
3852
3853        let array = UInt32Array::from(vec![Some(2), Some(10), None]);
3854        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3855
3856        assert_eq!(&actual, &expected);
3857
3858        let array = UInt64Array::from(vec![Some(2), Some(10), None]);
3859        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3860
3861        assert_eq!(&actual, &expected);
3862    }
3863
3864    #[test]
3865    fn test_cast_timestamp_to_integer() {
3866        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
3867            .with_timezone("UTC".to_string());
3868        let expected = cast(&array, &DataType::Int64).unwrap();
3869
3870        let actual = cast(&cast(&array, &DataType::Int8).unwrap(), &DataType::Int64).unwrap();
3871        assert_eq!(&actual, &expected);
3872
3873        let actual = cast(&cast(&array, &DataType::Int16).unwrap(), &DataType::Int64).unwrap();
3874        assert_eq!(&actual, &expected);
3875
3876        let actual = cast(&cast(&array, &DataType::Int32).unwrap(), &DataType::Int64).unwrap();
3877        assert_eq!(&actual, &expected);
3878
3879        let actual = cast(&cast(&array, &DataType::UInt8).unwrap(), &DataType::Int64).unwrap();
3880        assert_eq!(&actual, &expected);
3881
3882        let actual = cast(&cast(&array, &DataType::UInt16).unwrap(), &DataType::Int64).unwrap();
3883        assert_eq!(&actual, &expected);
3884
3885        let actual = cast(&cast(&array, &DataType::UInt32).unwrap(), &DataType::Int64).unwrap();
3886        assert_eq!(&actual, &expected);
3887
3888        let actual = cast(&cast(&array, &DataType::UInt64).unwrap(), &DataType::Int64).unwrap();
3889        assert_eq!(&actual, &expected);
3890    }
3891
3892    #[test]
3893    fn test_cast_floating_to_timestamp() {
3894        let array = Int64Array::from(vec![Some(2), Some(10), None]);
3895        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3896
3897        let array = Float16Array::from(vec![
3898            Some(f16::from_f32(2.0)),
3899            Some(f16::from_f32(10.6)),
3900            None,
3901        ]);
3902        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3903
3904        assert_eq!(&actual, &expected);
3905
3906        let array = Float32Array::from(vec![Some(2.0), Some(10.6), None]);
3907        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3908
3909        assert_eq!(&actual, &expected);
3910
3911        let array = Float64Array::from(vec![Some(2.1), Some(10.2), None]);
3912        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3913
3914        assert_eq!(&actual, &expected);
3915    }
3916
3917    #[test]
3918    fn test_cast_timestamp_to_floating() {
3919        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
3920            .with_timezone("UTC".to_string());
3921        let expected = cast(&array, &DataType::Int64).unwrap();
3922
3923        let actual = cast(&cast(&array, &DataType::Float16).unwrap(), &DataType::Int64).unwrap();
3924        assert_eq!(&actual, &expected);
3925
3926        let actual = cast(&cast(&array, &DataType::Float32).unwrap(), &DataType::Int64).unwrap();
3927        assert_eq!(&actual, &expected);
3928
3929        let actual = cast(&cast(&array, &DataType::Float64).unwrap(), &DataType::Int64).unwrap();
3930        assert_eq!(&actual, &expected);
3931    }
3932
3933    #[test]
3934    fn test_cast_decimal_to_timestamp() {
3935        let array = Int64Array::from(vec![Some(2), Some(10), None]);
3936        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3937
3938        let array = Decimal128Array::from(vec![Some(200), Some(1000), None])
3939            .with_precision_and_scale(4, 2)
3940            .unwrap();
3941        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3942
3943        assert_eq!(&actual, &expected);
3944
3945        let array = Decimal256Array::from(vec![
3946            Some(i256::from_i128(2000)),
3947            Some(i256::from_i128(10000)),
3948            None,
3949        ])
3950        .with_precision_and_scale(5, 3)
3951        .unwrap();
3952        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3953
3954        assert_eq!(&actual, &expected);
3955    }
3956
3957    #[test]
3958    fn test_cast_timestamp_to_decimal() {
3959        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
3960            .with_timezone("UTC".to_string());
3961        let expected = cast(&array, &DataType::Int64).unwrap();
3962
3963        let actual = cast(
3964            &cast(&array, &DataType::Decimal128(5, 2)).unwrap(),
3965            &DataType::Int64,
3966        )
3967        .unwrap();
3968        assert_eq!(&actual, &expected);
3969
3970        let actual = cast(
3971            &cast(&array, &DataType::Decimal256(10, 5)).unwrap(),
3972            &DataType::Int64,
3973        )
3974        .unwrap();
3975        assert_eq!(&actual, &expected);
3976    }
3977
3978    #[test]
3979    fn test_cast_list_i32_to_list_u16() {
3980        let value_data = Int32Array::from(vec![0, 0, 0, -1, -2, -1, 2, 100000000]).into_data();
3981
3982        let value_offsets = Buffer::from_slice_ref([0, 3, 6, 8]);
3983
3984        // Construct a list array from the above two
3985        // [[0,0,0], [-1, -2, -1], [2, 100000000]]
3986        let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
3987        let list_data = ArrayData::builder(list_data_type)
3988            .len(3)
3989            .add_buffer(value_offsets)
3990            .add_child_data(value_data)
3991            .build()
3992            .unwrap();
3993        let list_array = ListArray::from(list_data);
3994
3995        let cast_array = cast(
3996            &list_array,
3997            &DataType::List(Arc::new(Field::new_list_field(DataType::UInt16, true))),
3998        )
3999        .unwrap();
4000
4001        // For the ListArray itself, there are no null values (as there were no nulls when they went in)
4002        //
4003        // 3 negative values should get lost when casting to unsigned,
4004        // 1 value should overflow
4005        assert_eq!(0, cast_array.null_count());
4006
4007        // offsets should be the same
4008        let array = cast_array.as_list::<i32>();
4009        assert_eq!(list_array.value_offsets(), array.value_offsets());
4010
4011        assert_eq!(DataType::UInt16, array.value_type());
4012        assert_eq!(3, array.value_length(0));
4013        assert_eq!(3, array.value_length(1));
4014        assert_eq!(2, array.value_length(2));
4015
4016        // expect 4 nulls: negative numbers and overflow
4017        let u16arr = array.values().as_primitive::<UInt16Type>();
4018        assert_eq!(4, u16arr.null_count());
4019
4020        // expect 4 nulls: negative numbers and overflow
4021        let expected: UInt16Array =
4022            vec![Some(0), Some(0), Some(0), None, None, None, Some(2), None]
4023                .into_iter()
4024                .collect();
4025
4026        assert_eq!(u16arr, &expected);
4027    }
4028
4029    #[test]
4030    fn test_cast_list_i32_to_list_timestamp() {
4031        // Construct a value array
4032        let value_data = Int32Array::from(vec![0, 0, 0, -1, -2, -1, 2, 8, 100000000]).into_data();
4033
4034        let value_offsets = Buffer::from_slice_ref([0, 3, 6, 9]);
4035
4036        // Construct a list array from the above two
4037        let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
4038        let list_data = ArrayData::builder(list_data_type)
4039            .len(3)
4040            .add_buffer(value_offsets)
4041            .add_child_data(value_data)
4042            .build()
4043            .unwrap();
4044        let list_array = Arc::new(ListArray::from(list_data)) as ArrayRef;
4045
4046        let actual = cast(
4047            &list_array,
4048            &DataType::List(Arc::new(Field::new_list_field(
4049                DataType::Timestamp(TimeUnit::Microsecond, None),
4050                true,
4051            ))),
4052        )
4053        .unwrap();
4054
4055        let expected = cast(
4056            &cast(
4057                &list_array,
4058                &DataType::List(Arc::new(Field::new_list_field(DataType::Int64, true))),
4059            )
4060            .unwrap(),
4061            &DataType::List(Arc::new(Field::new_list_field(
4062                DataType::Timestamp(TimeUnit::Microsecond, None),
4063                true,
4064            ))),
4065        )
4066        .unwrap();
4067
4068        assert_eq!(&actual, &expected);
4069    }
4070
4071    #[test]
4072    fn test_cast_date32_to_date64() {
4073        let a = Date32Array::from(vec![10000, 17890]);
4074        let array = Arc::new(a) as ArrayRef;
4075        let b = cast(&array, &DataType::Date64).unwrap();
4076        let c = b.as_primitive::<Date64Type>();
4077        assert_eq!(864000000000, c.value(0));
4078        assert_eq!(1545696000000, c.value(1));
4079    }
4080
4081    #[test]
4082    fn test_cast_date64_to_date32() {
4083        let a = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
4084        let array = Arc::new(a) as ArrayRef;
4085        let b = cast(&array, &DataType::Date32).unwrap();
4086        let c = b.as_primitive::<Date32Type>();
4087        assert_eq!(10000, c.value(0));
4088        assert_eq!(17890, c.value(1));
4089        assert!(c.is_null(2));
4090    }
4091
4092    #[test]
4093    fn test_cast_string_to_integral_overflow() {
4094        let str = Arc::new(StringArray::from(vec![
4095            Some("123"),
4096            Some("-123"),
4097            Some("86374"),
4098            None,
4099        ])) as ArrayRef;
4100
4101        let options = CastOptions {
4102            safe: true,
4103            format_options: FormatOptions::default(),
4104        };
4105        let res = cast_with_options(&str, &DataType::Int16, &options).expect("should cast to i16");
4106        let expected =
4107            Arc::new(Int16Array::from(vec![Some(123), Some(-123), None, None])) as ArrayRef;
4108        assert_eq!(&res, &expected);
4109    }
4110
4111    #[test]
4112    fn test_cast_string_to_timestamp() {
4113        let a0 = Arc::new(StringViewArray::from(vec![
4114            Some("2020-09-08T12:00:00.123456789+00:00"),
4115            Some("Not a valid date"),
4116            None,
4117        ])) as ArrayRef;
4118        let a1 = Arc::new(StringArray::from(vec![
4119            Some("2020-09-08T12:00:00.123456789+00:00"),
4120            Some("Not a valid date"),
4121            None,
4122        ])) as ArrayRef;
4123        let a2 = Arc::new(LargeStringArray::from(vec![
4124            Some("2020-09-08T12:00:00.123456789+00:00"),
4125            Some("Not a valid date"),
4126            None,
4127        ])) as ArrayRef;
4128        for array in &[a0, a1, a2] {
4129            for time_unit in &[
4130                TimeUnit::Second,
4131                TimeUnit::Millisecond,
4132                TimeUnit::Microsecond,
4133                TimeUnit::Nanosecond,
4134            ] {
4135                let to_type = DataType::Timestamp(*time_unit, None);
4136                let b = cast(array, &to_type).unwrap();
4137
4138                match time_unit {
4139                    TimeUnit::Second => {
4140                        let c = b.as_primitive::<TimestampSecondType>();
4141                        assert_eq!(1599566400, c.value(0));
4142                        assert!(c.is_null(1));
4143                        assert!(c.is_null(2));
4144                    }
4145                    TimeUnit::Millisecond => {
4146                        let c = b
4147                            .as_any()
4148                            .downcast_ref::<TimestampMillisecondArray>()
4149                            .unwrap();
4150                        assert_eq!(1599566400123, c.value(0));
4151                        assert!(c.is_null(1));
4152                        assert!(c.is_null(2));
4153                    }
4154                    TimeUnit::Microsecond => {
4155                        let c = b
4156                            .as_any()
4157                            .downcast_ref::<TimestampMicrosecondArray>()
4158                            .unwrap();
4159                        assert_eq!(1599566400123456, c.value(0));
4160                        assert!(c.is_null(1));
4161                        assert!(c.is_null(2));
4162                    }
4163                    TimeUnit::Nanosecond => {
4164                        let c = b
4165                            .as_any()
4166                            .downcast_ref::<TimestampNanosecondArray>()
4167                            .unwrap();
4168                        assert_eq!(1599566400123456789, c.value(0));
4169                        assert!(c.is_null(1));
4170                        assert!(c.is_null(2));
4171                    }
4172                }
4173
4174                let options = CastOptions {
4175                    safe: false,
4176                    format_options: FormatOptions::default(),
4177                };
4178                let err = cast_with_options(array, &to_type, &options).unwrap_err();
4179                assert_eq!(
4180                    err.to_string(),
4181                    "Parser error: Error parsing timestamp from 'Not a valid date': error parsing date"
4182                );
4183            }
4184        }
4185    }
4186
4187    #[test]
4188    fn test_cast_string_to_timestamp_overflow() {
4189        let array = StringArray::from(vec!["9800-09-08T12:00:00.123456789"]);
4190        let result = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
4191        let result = result.as_primitive::<TimestampSecondType>();
4192        assert_eq!(result.values(), &[247112596800]);
4193    }
4194
4195    #[test]
4196    fn test_cast_string_to_date32() {
4197        let a0 = Arc::new(StringViewArray::from(vec![
4198            Some("2018-12-25"),
4199            Some("Not a valid date"),
4200            None,
4201        ])) as ArrayRef;
4202        let a1 = Arc::new(StringArray::from(vec![
4203            Some("2018-12-25"),
4204            Some("Not a valid date"),
4205            None,
4206        ])) as ArrayRef;
4207        let a2 = Arc::new(LargeStringArray::from(vec![
4208            Some("2018-12-25"),
4209            Some("Not a valid date"),
4210            None,
4211        ])) as ArrayRef;
4212        for array in &[a0, a1, a2] {
4213            let to_type = DataType::Date32;
4214            let b = cast(array, &to_type).unwrap();
4215            let c = b.as_primitive::<Date32Type>();
4216            assert_eq!(17890, c.value(0));
4217            assert!(c.is_null(1));
4218            assert!(c.is_null(2));
4219
4220            let options = CastOptions {
4221                safe: false,
4222                format_options: FormatOptions::default(),
4223            };
4224            let err = cast_with_options(array, &to_type, &options).unwrap_err();
4225            assert_eq!(
4226                err.to_string(),
4227                "Cast error: Cannot cast string 'Not a valid date' to value of Date32 type"
4228            );
4229        }
4230    }
4231
4232    #[test]
4233    fn test_cast_string_with_large_date_to_date32() {
4234        let array = Arc::new(StringArray::from(vec![
4235            Some("+10999-12-31"),
4236            Some("-0010-02-28"),
4237            Some("0010-02-28"),
4238            Some("0000-01-01"),
4239            Some("-0000-01-01"),
4240            Some("-0001-01-01"),
4241        ])) as ArrayRef;
4242        let to_type = DataType::Date32;
4243        let options = CastOptions {
4244            safe: false,
4245            format_options: FormatOptions::default(),
4246        };
4247        let b = cast_with_options(&array, &to_type, &options).unwrap();
4248        let c = b.as_primitive::<Date32Type>();
4249        assert_eq!(3298139, c.value(0)); // 10999-12-31
4250        assert_eq!(-723122, c.value(1)); // -0010-02-28
4251        assert_eq!(-715817, c.value(2)); // 0010-02-28
4252        assert_eq!(c.value(3), c.value(4)); // Expect 0000-01-01 and -0000-01-01 to be parsed the same
4253        assert_eq!(-719528, c.value(3)); // 0000-01-01
4254        assert_eq!(-719528, c.value(4)); // -0000-01-01
4255        assert_eq!(-719893, c.value(5)); // -0001-01-01
4256    }
4257
4258    #[test]
4259    fn test_cast_invalid_string_with_large_date_to_date32() {
4260        // Large dates need to be prefixed with a + or - sign, otherwise they are not parsed correctly
4261        let array = Arc::new(StringArray::from(vec![Some("10999-12-31")])) as ArrayRef;
4262        let to_type = DataType::Date32;
4263        let options = CastOptions {
4264            safe: false,
4265            format_options: FormatOptions::default(),
4266        };
4267        let err = cast_with_options(&array, &to_type, &options).unwrap_err();
4268        assert_eq!(
4269            err.to_string(),
4270            "Cast error: Cannot cast string '10999-12-31' to value of Date32 type"
4271        );
4272    }
4273
4274    #[test]
4275    fn test_cast_string_format_yyyymmdd_to_date32() {
4276        let a0 = Arc::new(StringViewArray::from(vec![
4277            Some("2020-12-25"),
4278            Some("20201117"),
4279        ])) as ArrayRef;
4280        let a1 = Arc::new(StringArray::from(vec![
4281            Some("2020-12-25"),
4282            Some("20201117"),
4283        ])) as ArrayRef;
4284        let a2 = Arc::new(LargeStringArray::from(vec![
4285            Some("2020-12-25"),
4286            Some("20201117"),
4287        ])) as ArrayRef;
4288
4289        for array in &[a0, a1, a2] {
4290            let to_type = DataType::Date32;
4291            let options = CastOptions {
4292                safe: false,
4293                format_options: FormatOptions::default(),
4294            };
4295            let result = cast_with_options(&array, &to_type, &options).unwrap();
4296            let c = result.as_primitive::<Date32Type>();
4297            assert_eq!(
4298                chrono::NaiveDate::from_ymd_opt(2020, 12, 25),
4299                c.value_as_date(0)
4300            );
4301            assert_eq!(
4302                chrono::NaiveDate::from_ymd_opt(2020, 11, 17),
4303                c.value_as_date(1)
4304            );
4305        }
4306    }
4307
4308    #[test]
4309    fn test_cast_string_to_time32second() {
4310        let a0 = Arc::new(StringViewArray::from(vec![
4311            Some("08:08:35.091323414"),
4312            Some("08:08:60.091323414"), // leap second
4313            Some("08:08:61.091323414"), // not valid
4314            Some("Not a valid time"),
4315            None,
4316        ])) as ArrayRef;
4317        let a1 = Arc::new(StringArray::from(vec![
4318            Some("08:08:35.091323414"),
4319            Some("08:08:60.091323414"), // leap second
4320            Some("08:08:61.091323414"), // not valid
4321            Some("Not a valid time"),
4322            None,
4323        ])) as ArrayRef;
4324        let a2 = Arc::new(LargeStringArray::from(vec![
4325            Some("08:08:35.091323414"),
4326            Some("08:08:60.091323414"), // leap second
4327            Some("08:08:61.091323414"), // not valid
4328            Some("Not a valid time"),
4329            None,
4330        ])) as ArrayRef;
4331        for array in &[a0, a1, a2] {
4332            let to_type = DataType::Time32(TimeUnit::Second);
4333            let b = cast(array, &to_type).unwrap();
4334            let c = b.as_primitive::<Time32SecondType>();
4335            assert_eq!(29315, c.value(0));
4336            assert_eq!(29340, c.value(1));
4337            assert!(c.is_null(2));
4338            assert!(c.is_null(3));
4339            assert!(c.is_null(4));
4340
4341            let options = CastOptions {
4342                safe: false,
4343                format_options: FormatOptions::default(),
4344            };
4345            let err = cast_with_options(array, &to_type, &options).unwrap_err();
4346            assert_eq!(err.to_string(), "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(Second) type");
4347        }
4348    }
4349
4350    #[test]
4351    fn test_cast_string_to_time32millisecond() {
4352        let a0 = Arc::new(StringViewArray::from(vec![
4353            Some("08:08:35.091323414"),
4354            Some("08:08:60.091323414"), // leap second
4355            Some("08:08:61.091323414"), // not valid
4356            Some("Not a valid time"),
4357            None,
4358        ])) as ArrayRef;
4359        let a1 = Arc::new(StringArray::from(vec![
4360            Some("08:08:35.091323414"),
4361            Some("08:08:60.091323414"), // leap second
4362            Some("08:08:61.091323414"), // not valid
4363            Some("Not a valid time"),
4364            None,
4365        ])) as ArrayRef;
4366        let a2 = Arc::new(LargeStringArray::from(vec![
4367            Some("08:08:35.091323414"),
4368            Some("08:08:60.091323414"), // leap second
4369            Some("08:08:61.091323414"), // not valid
4370            Some("Not a valid time"),
4371            None,
4372        ])) as ArrayRef;
4373        for array in &[a0, a1, a2] {
4374            let to_type = DataType::Time32(TimeUnit::Millisecond);
4375            let b = cast(array, &to_type).unwrap();
4376            let c = b.as_primitive::<Time32MillisecondType>();
4377            assert_eq!(29315091, c.value(0));
4378            assert_eq!(29340091, c.value(1));
4379            assert!(c.is_null(2));
4380            assert!(c.is_null(3));
4381            assert!(c.is_null(4));
4382
4383            let options = CastOptions {
4384                safe: false,
4385                format_options: FormatOptions::default(),
4386            };
4387            let err = cast_with_options(array, &to_type, &options).unwrap_err();
4388            assert_eq!(err.to_string(), "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(Millisecond) type");
4389        }
4390    }
4391
4392    #[test]
4393    fn test_cast_string_to_time64microsecond() {
4394        let a0 = Arc::new(StringViewArray::from(vec![
4395            Some("08:08:35.091323414"),
4396            Some("Not a valid time"),
4397            None,
4398        ])) as ArrayRef;
4399        let a1 = Arc::new(StringArray::from(vec![
4400            Some("08:08:35.091323414"),
4401            Some("Not a valid time"),
4402            None,
4403        ])) as ArrayRef;
4404        let a2 = Arc::new(LargeStringArray::from(vec![
4405            Some("08:08:35.091323414"),
4406            Some("Not a valid time"),
4407            None,
4408        ])) as ArrayRef;
4409        for array in &[a0, a1, a2] {
4410            let to_type = DataType::Time64(TimeUnit::Microsecond);
4411            let b = cast(array, &to_type).unwrap();
4412            let c = b.as_primitive::<Time64MicrosecondType>();
4413            assert_eq!(29315091323, c.value(0));
4414            assert!(c.is_null(1));
4415            assert!(c.is_null(2));
4416
4417            let options = CastOptions {
4418                safe: false,
4419                format_options: FormatOptions::default(),
4420            };
4421            let err = cast_with_options(array, &to_type, &options).unwrap_err();
4422            assert_eq!(err.to_string(), "Cast error: Cannot cast string 'Not a valid time' to value of Time64(Microsecond) type");
4423        }
4424    }
4425
4426    #[test]
4427    fn test_cast_string_to_time64nanosecond() {
4428        let a0 = Arc::new(StringViewArray::from(vec![
4429            Some("08:08:35.091323414"),
4430            Some("Not a valid time"),
4431            None,
4432        ])) as ArrayRef;
4433        let a1 = Arc::new(StringArray::from(vec![
4434            Some("08:08:35.091323414"),
4435            Some("Not a valid time"),
4436            None,
4437        ])) as ArrayRef;
4438        let a2 = Arc::new(LargeStringArray::from(vec![
4439            Some("08:08:35.091323414"),
4440            Some("Not a valid time"),
4441            None,
4442        ])) as ArrayRef;
4443        for array in &[a0, a1, a2] {
4444            let to_type = DataType::Time64(TimeUnit::Nanosecond);
4445            let b = cast(array, &to_type).unwrap();
4446            let c = b.as_primitive::<Time64NanosecondType>();
4447            assert_eq!(29315091323414, c.value(0));
4448            assert!(c.is_null(1));
4449            assert!(c.is_null(2));
4450
4451            let options = CastOptions {
4452                safe: false,
4453                format_options: FormatOptions::default(),
4454            };
4455            let err = cast_with_options(array, &to_type, &options).unwrap_err();
4456            assert_eq!(err.to_string(), "Cast error: Cannot cast string 'Not a valid time' to value of Time64(Nanosecond) type");
4457        }
4458    }
4459
4460    #[test]
4461    fn test_cast_string_to_date64() {
4462        let a0 = Arc::new(StringViewArray::from(vec![
4463            Some("2020-09-08T12:00:00"),
4464            Some("Not a valid date"),
4465            None,
4466        ])) as ArrayRef;
4467        let a1 = Arc::new(StringArray::from(vec![
4468            Some("2020-09-08T12:00:00"),
4469            Some("Not a valid date"),
4470            None,
4471        ])) as ArrayRef;
4472        let a2 = Arc::new(LargeStringArray::from(vec![
4473            Some("2020-09-08T12:00:00"),
4474            Some("Not a valid date"),
4475            None,
4476        ])) as ArrayRef;
4477        for array in &[a0, a1, a2] {
4478            let to_type = DataType::Date64;
4479            let b = cast(array, &to_type).unwrap();
4480            let c = b.as_primitive::<Date64Type>();
4481            assert_eq!(1599566400000, c.value(0));
4482            assert!(c.is_null(1));
4483            assert!(c.is_null(2));
4484
4485            let options = CastOptions {
4486                safe: false,
4487                format_options: FormatOptions::default(),
4488            };
4489            let err = cast_with_options(array, &to_type, &options).unwrap_err();
4490            assert_eq!(
4491                err.to_string(),
4492                "Cast error: Cannot cast string 'Not a valid date' to value of Date64 type"
4493            );
4494        }
4495    }
4496
4497    macro_rules! test_safe_string_to_interval {
4498        ($data_vec:expr, $interval_unit:expr, $array_ty:ty, $expect_vec:expr) => {
4499            let source_string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
4500
4501            let options = CastOptions {
4502                safe: true,
4503                format_options: FormatOptions::default(),
4504            };
4505
4506            let target_interval_array = cast_with_options(
4507                &source_string_array.clone(),
4508                &DataType::Interval($interval_unit),
4509                &options,
4510            )
4511            .unwrap()
4512            .as_any()
4513            .downcast_ref::<$array_ty>()
4514            .unwrap()
4515            .clone() as $array_ty;
4516
4517            let target_string_array =
4518                cast_with_options(&target_interval_array, &DataType::Utf8, &options)
4519                    .unwrap()
4520                    .as_any()
4521                    .downcast_ref::<StringArray>()
4522                    .unwrap()
4523                    .clone();
4524
4525            let expect_string_array = StringArray::from($expect_vec);
4526
4527            assert_eq!(target_string_array, expect_string_array);
4528
4529            let target_large_string_array =
4530                cast_with_options(&target_interval_array, &DataType::LargeUtf8, &options)
4531                    .unwrap()
4532                    .as_any()
4533                    .downcast_ref::<LargeStringArray>()
4534                    .unwrap()
4535                    .clone();
4536
4537            let expect_large_string_array = LargeStringArray::from($expect_vec);
4538
4539            assert_eq!(target_large_string_array, expect_large_string_array);
4540        };
4541    }
4542
4543    #[test]
4544    fn test_cast_string_to_interval_year_month() {
4545        test_safe_string_to_interval!(
4546            vec![
4547                Some("1 year 1 month"),
4548                Some("1.5 years 13 month"),
4549                Some("30 days"),
4550                Some("31 days"),
4551                Some("2 months 31 days"),
4552                Some("2 months 31 days 1 second"),
4553                Some("foobar"),
4554            ],
4555            IntervalUnit::YearMonth,
4556            IntervalYearMonthArray,
4557            vec![
4558                Some("1 years 1 mons"),
4559                Some("2 years 7 mons"),
4560                None,
4561                None,
4562                None,
4563                None,
4564                None,
4565            ]
4566        );
4567    }
4568
4569    #[test]
4570    fn test_cast_string_to_interval_day_time() {
4571        test_safe_string_to_interval!(
4572            vec![
4573                Some("1 year 1 month"),
4574                Some("1.5 years 13 month"),
4575                Some("30 days"),
4576                Some("1 day 2 second 3.5 milliseconds"),
4577                Some("foobar"),
4578            ],
4579            IntervalUnit::DayTime,
4580            IntervalDayTimeArray,
4581            vec![
4582                Some("390 days"),
4583                Some("930 days"),
4584                Some("30 days"),
4585                None,
4586                None,
4587            ]
4588        );
4589    }
4590
4591    #[test]
4592    fn test_cast_string_to_interval_month_day_nano() {
4593        test_safe_string_to_interval!(
4594            vec![
4595                Some("1 year 1 month 1 day"),
4596                None,
4597                Some("1.5 years 13 month 35 days 1.4 milliseconds"),
4598                Some("3 days"),
4599                Some("8 seconds"),
4600                None,
4601                Some("1 day 29800 milliseconds"),
4602                Some("3 months 1 second"),
4603                Some("6 minutes 120 second"),
4604                Some("2 years 39 months 9 days 19 hours 1 minute 83 seconds 399222 milliseconds"),
4605                Some("foobar"),
4606            ],
4607            IntervalUnit::MonthDayNano,
4608            IntervalMonthDayNanoArray,
4609            vec![
4610                Some("13 mons 1 days"),
4611                None,
4612                Some("31 mons 35 days 0.001400000 secs"),
4613                Some("3 days"),
4614                Some("8.000000000 secs"),
4615                None,
4616                Some("1 days 29.800000000 secs"),
4617                Some("3 mons 1.000000000 secs"),
4618                Some("8 mins"),
4619                Some("63 mons 9 days 19 hours 9 mins 2.222000000 secs"),
4620                None,
4621            ]
4622        );
4623    }
4624
4625    macro_rules! test_unsafe_string_to_interval_err {
4626        ($data_vec:expr, $interval_unit:expr, $error_msg:expr) => {
4627            let string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
4628            let options = CastOptions {
4629                safe: false,
4630                format_options: FormatOptions::default(),
4631            };
4632            let arrow_err = cast_with_options(
4633                &string_array.clone(),
4634                &DataType::Interval($interval_unit),
4635                &options,
4636            )
4637            .unwrap_err();
4638            assert_eq!($error_msg, arrow_err.to_string());
4639        };
4640    }
4641
4642    #[test]
4643    fn test_cast_string_to_interval_err() {
4644        test_unsafe_string_to_interval_err!(
4645            vec![Some("foobar")],
4646            IntervalUnit::YearMonth,
4647            r#"Parser error: Invalid input syntax for type interval: "foobar""#
4648        );
4649        test_unsafe_string_to_interval_err!(
4650            vec![Some("foobar")],
4651            IntervalUnit::DayTime,
4652            r#"Parser error: Invalid input syntax for type interval: "foobar""#
4653        );
4654        test_unsafe_string_to_interval_err!(
4655            vec![Some("foobar")],
4656            IntervalUnit::MonthDayNano,
4657            r#"Parser error: Invalid input syntax for type interval: "foobar""#
4658        );
4659        test_unsafe_string_to_interval_err!(
4660            vec![Some("2 months 31 days 1 second")],
4661            IntervalUnit::YearMonth,
4662            r#"Cast error: Cannot cast 2 months 31 days 1 second to IntervalYearMonth. Only year and month fields are allowed."#
4663        );
4664        test_unsafe_string_to_interval_err!(
4665            vec![Some("1 day 1.5 milliseconds")],
4666            IntervalUnit::DayTime,
4667            r#"Cast error: Cannot cast 1 day 1.5 milliseconds to IntervalDayTime because the nanos part isn't multiple of milliseconds"#
4668        );
4669
4670        // overflow
4671        test_unsafe_string_to_interval_err!(
4672            vec![Some(format!(
4673                "{} century {} year {} month",
4674                i64::MAX - 2,
4675                i64::MAX - 2,
4676                i64::MAX - 2
4677            ))],
4678            IntervalUnit::DayTime,
4679            format!(
4680                "Arithmetic overflow: Overflow happened on: {} * 100",
4681                i64::MAX - 2
4682            )
4683        );
4684        test_unsafe_string_to_interval_err!(
4685            vec![Some(format!(
4686                "{} year {} month {} day",
4687                i64::MAX - 2,
4688                i64::MAX - 2,
4689                i64::MAX - 2
4690            ))],
4691            IntervalUnit::MonthDayNano,
4692            format!(
4693                "Arithmetic overflow: Overflow happened on: {} * 12",
4694                i64::MAX - 2
4695            )
4696        );
4697    }
4698
4699    #[test]
4700    fn test_cast_binary_to_fixed_size_binary() {
4701        let bytes_1 = "Hiiii".as_bytes();
4702        let bytes_2 = "Hello".as_bytes();
4703
4704        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
4705        let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
4706        let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
4707
4708        let array_ref = cast(&a1, &DataType::FixedSizeBinary(5)).unwrap();
4709        let down_cast = array_ref
4710            .as_any()
4711            .downcast_ref::<FixedSizeBinaryArray>()
4712            .unwrap();
4713        assert_eq!(bytes_1, down_cast.value(0));
4714        assert_eq!(bytes_2, down_cast.value(1));
4715        assert!(down_cast.is_null(2));
4716
4717        let array_ref = cast(&a2, &DataType::FixedSizeBinary(5)).unwrap();
4718        let down_cast = array_ref
4719            .as_any()
4720            .downcast_ref::<FixedSizeBinaryArray>()
4721            .unwrap();
4722        assert_eq!(bytes_1, down_cast.value(0));
4723        assert_eq!(bytes_2, down_cast.value(1));
4724        assert!(down_cast.is_null(2));
4725
4726        // test error cases when the length of binary are not same
4727        let bytes_1 = "Hi".as_bytes();
4728        let bytes_2 = "Hello".as_bytes();
4729
4730        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
4731        let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
4732        let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
4733
4734        let array_ref = cast_with_options(
4735            &a1,
4736            &DataType::FixedSizeBinary(5),
4737            &CastOptions {
4738                safe: false,
4739                format_options: FormatOptions::default(),
4740            },
4741        );
4742        assert!(array_ref.is_err());
4743
4744        let array_ref = cast_with_options(
4745            &a2,
4746            &DataType::FixedSizeBinary(5),
4747            &CastOptions {
4748                safe: false,
4749                format_options: FormatOptions::default(),
4750            },
4751        );
4752        assert!(array_ref.is_err());
4753    }
4754
4755    #[test]
4756    fn test_fixed_size_binary_to_binary() {
4757        let bytes_1 = "Hiiii".as_bytes();
4758        let bytes_2 = "Hello".as_bytes();
4759
4760        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
4761        let a1 = Arc::new(FixedSizeBinaryArray::from(binary_data.clone())) as ArrayRef;
4762
4763        let array_ref = cast(&a1, &DataType::Binary).unwrap();
4764        let down_cast = array_ref.as_binary::<i32>();
4765        assert_eq!(bytes_1, down_cast.value(0));
4766        assert_eq!(bytes_2, down_cast.value(1));
4767        assert!(down_cast.is_null(2));
4768
4769        let array_ref = cast(&a1, &DataType::LargeBinary).unwrap();
4770        let down_cast = array_ref.as_binary::<i64>();
4771        assert_eq!(bytes_1, down_cast.value(0));
4772        assert_eq!(bytes_2, down_cast.value(1));
4773        assert!(down_cast.is_null(2));
4774    }
4775
4776    #[test]
4777    fn test_numeric_to_binary() {
4778        let a = Int16Array::from(vec![Some(1), Some(511), None]);
4779
4780        let array_ref = cast(&a, &DataType::Binary).unwrap();
4781        let down_cast = array_ref.as_binary::<i32>();
4782        assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
4783        assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
4784        assert!(down_cast.is_null(2));
4785
4786        let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
4787
4788        let array_ref = cast(&a, &DataType::Binary).unwrap();
4789        let down_cast = array_ref.as_binary::<i32>();
4790        assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
4791        assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
4792        assert!(down_cast.is_null(2));
4793    }
4794
4795    #[test]
4796    fn test_numeric_to_large_binary() {
4797        let a = Int16Array::from(vec![Some(1), Some(511), None]);
4798
4799        let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
4800        let down_cast = array_ref.as_binary::<i64>();
4801        assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
4802        assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
4803        assert!(down_cast.is_null(2));
4804
4805        let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
4806
4807        let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
4808        let down_cast = array_ref.as_binary::<i64>();
4809        assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
4810        assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
4811        assert!(down_cast.is_null(2));
4812    }
4813
4814    #[test]
4815    fn test_cast_date32_to_int32() {
4816        let array = Date32Array::from(vec![10000, 17890]);
4817        let b = cast(&array, &DataType::Int32).unwrap();
4818        let c = b.as_primitive::<Int32Type>();
4819        assert_eq!(10000, c.value(0));
4820        assert_eq!(17890, c.value(1));
4821    }
4822
4823    #[test]
4824    fn test_cast_int32_to_date32() {
4825        let array = Int32Array::from(vec![10000, 17890]);
4826        let b = cast(&array, &DataType::Date32).unwrap();
4827        let c = b.as_primitive::<Date32Type>();
4828        assert_eq!(10000, c.value(0));
4829        assert_eq!(17890, c.value(1));
4830    }
4831
4832    #[test]
4833    fn test_cast_timestamp_to_date32() {
4834        let array =
4835            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
4836                .with_timezone("+00:00".to_string());
4837        let b = cast(&array, &DataType::Date32).unwrap();
4838        let c = b.as_primitive::<Date32Type>();
4839        assert_eq!(10000, c.value(0));
4840        assert_eq!(17890, c.value(1));
4841        assert!(c.is_null(2));
4842    }
4843    #[test]
4844    fn test_cast_timestamp_to_date32_zone() {
4845        let strings = StringArray::from_iter([
4846            Some("1970-01-01T00:00:01"),
4847            Some("1970-01-01T23:59:59"),
4848            None,
4849            Some("2020-03-01T02:00:23+00:00"),
4850        ]);
4851        let dt = DataType::Timestamp(TimeUnit::Millisecond, Some("-07:00".into()));
4852        let timestamps = cast(&strings, &dt).unwrap();
4853        let dates = cast(timestamps.as_ref(), &DataType::Date32).unwrap();
4854
4855        let c = dates.as_primitive::<Date32Type>();
4856        let expected = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap();
4857        assert_eq!(c.value_as_date(0).unwrap(), expected);
4858        assert_eq!(c.value_as_date(1).unwrap(), expected);
4859        assert!(c.is_null(2));
4860        let expected = NaiveDate::from_ymd_opt(2020, 2, 29).unwrap();
4861        assert_eq!(c.value_as_date(3).unwrap(), expected);
4862    }
4863    #[test]
4864    fn test_cast_timestamp_to_date64() {
4865        let array =
4866            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None]);
4867        let b = cast(&array, &DataType::Date64).unwrap();
4868        let c = b.as_primitive::<Date64Type>();
4869        assert_eq!(864000000005, c.value(0));
4870        assert_eq!(1545696000001, c.value(1));
4871        assert!(c.is_null(2));
4872
4873        let array = TimestampSecondArray::from(vec![Some(864000000005), Some(1545696000001)]);
4874        let b = cast(&array, &DataType::Date64).unwrap();
4875        let c = b.as_primitive::<Date64Type>();
4876        assert_eq!(864000000005000, c.value(0));
4877        assert_eq!(1545696000001000, c.value(1));
4878
4879        // test overflow, safe cast
4880        let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
4881        let b = cast(&array, &DataType::Date64).unwrap();
4882        assert!(b.is_null(0));
4883        // test overflow, unsafe cast
4884        let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
4885        let options = CastOptions {
4886            safe: false,
4887            format_options: FormatOptions::default(),
4888        };
4889        let b = cast_with_options(&array, &DataType::Date64, &options);
4890        assert!(b.is_err());
4891    }
4892
4893    #[test]
4894    fn test_cast_timestamp_to_time64() {
4895        // test timestamp secs
4896        let array = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
4897            .with_timezone("+01:00".to_string());
4898        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
4899        let c = b.as_primitive::<Time64MicrosecondType>();
4900        assert_eq!(3605000000, c.value(0));
4901        assert_eq!(3601000000, c.value(1));
4902        assert!(c.is_null(2));
4903        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
4904        let c = b.as_primitive::<Time64NanosecondType>();
4905        assert_eq!(3605000000000, c.value(0));
4906        assert_eq!(3601000000000, c.value(1));
4907        assert!(c.is_null(2));
4908
4909        // test timestamp milliseconds
4910        let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
4911            .with_timezone("+01:00".to_string());
4912        let array = Arc::new(a) as ArrayRef;
4913        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
4914        let c = b.as_primitive::<Time64MicrosecondType>();
4915        assert_eq!(3605000000, c.value(0));
4916        assert_eq!(3601000000, c.value(1));
4917        assert!(c.is_null(2));
4918        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
4919        let c = b.as_primitive::<Time64NanosecondType>();
4920        assert_eq!(3605000000000, c.value(0));
4921        assert_eq!(3601000000000, c.value(1));
4922        assert!(c.is_null(2));
4923
4924        // test timestamp microseconds
4925        let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
4926            .with_timezone("+01:00".to_string());
4927        let array = Arc::new(a) as ArrayRef;
4928        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
4929        let c = b.as_primitive::<Time64MicrosecondType>();
4930        assert_eq!(3605000000, c.value(0));
4931        assert_eq!(3601000000, c.value(1));
4932        assert!(c.is_null(2));
4933        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
4934        let c = b.as_primitive::<Time64NanosecondType>();
4935        assert_eq!(3605000000000, c.value(0));
4936        assert_eq!(3601000000000, c.value(1));
4937        assert!(c.is_null(2));
4938
4939        // test timestamp nanoseconds
4940        let a = TimestampNanosecondArray::from(vec![Some(86405000000000), Some(1000000000), None])
4941            .with_timezone("+01:00".to_string());
4942        let array = Arc::new(a) as ArrayRef;
4943        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
4944        let c = b.as_primitive::<Time64MicrosecondType>();
4945        assert_eq!(3605000000, c.value(0));
4946        assert_eq!(3601000000, c.value(1));
4947        assert!(c.is_null(2));
4948        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
4949        let c = b.as_primitive::<Time64NanosecondType>();
4950        assert_eq!(3605000000000, c.value(0));
4951        assert_eq!(3601000000000, c.value(1));
4952        assert!(c.is_null(2));
4953
4954        // test overflow
4955        let a =
4956            TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
4957        let array = Arc::new(a) as ArrayRef;
4958        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond));
4959        assert!(b.is_err());
4960        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond));
4961        assert!(b.is_err());
4962        let b = cast(&array, &DataType::Time64(TimeUnit::Millisecond));
4963        assert!(b.is_err());
4964    }
4965
4966    #[test]
4967    fn test_cast_timestamp_to_time32() {
4968        // test timestamp secs
4969        let a = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
4970            .with_timezone("+01:00".to_string());
4971        let array = Arc::new(a) as ArrayRef;
4972        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
4973        let c = b.as_primitive::<Time32SecondType>();
4974        assert_eq!(3605, c.value(0));
4975        assert_eq!(3601, c.value(1));
4976        assert!(c.is_null(2));
4977        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
4978        let c = b.as_primitive::<Time32MillisecondType>();
4979        assert_eq!(3605000, c.value(0));
4980        assert_eq!(3601000, c.value(1));
4981        assert!(c.is_null(2));
4982
4983        // test timestamp milliseconds
4984        let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
4985            .with_timezone("+01:00".to_string());
4986        let array = Arc::new(a) as ArrayRef;
4987        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
4988        let c = b.as_primitive::<Time32SecondType>();
4989        assert_eq!(3605, c.value(0));
4990        assert_eq!(3601, c.value(1));
4991        assert!(c.is_null(2));
4992        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
4993        let c = b.as_primitive::<Time32MillisecondType>();
4994        assert_eq!(3605000, c.value(0));
4995        assert_eq!(3601000, c.value(1));
4996        assert!(c.is_null(2));
4997
4998        // test timestamp microseconds
4999        let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
5000            .with_timezone("+01:00".to_string());
5001        let array = Arc::new(a) as ArrayRef;
5002        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
5003        let c = b.as_primitive::<Time32SecondType>();
5004        assert_eq!(3605, c.value(0));
5005        assert_eq!(3601, c.value(1));
5006        assert!(c.is_null(2));
5007        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
5008        let c = b.as_primitive::<Time32MillisecondType>();
5009        assert_eq!(3605000, c.value(0));
5010        assert_eq!(3601000, c.value(1));
5011        assert!(c.is_null(2));
5012
5013        // test timestamp nanoseconds
5014        let a = TimestampNanosecondArray::from(vec![Some(86405000000000), Some(1000000000), None])
5015            .with_timezone("+01:00".to_string());
5016        let array = Arc::new(a) as ArrayRef;
5017        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
5018        let c = b.as_primitive::<Time32SecondType>();
5019        assert_eq!(3605, c.value(0));
5020        assert_eq!(3601, c.value(1));
5021        assert!(c.is_null(2));
5022        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
5023        let c = b.as_primitive::<Time32MillisecondType>();
5024        assert_eq!(3605000, c.value(0));
5025        assert_eq!(3601000, c.value(1));
5026        assert!(c.is_null(2));
5027
5028        // test overflow
5029        let a =
5030            TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
5031        let array = Arc::new(a) as ArrayRef;
5032        let b = cast(&array, &DataType::Time32(TimeUnit::Second));
5033        assert!(b.is_err());
5034        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond));
5035        assert!(b.is_err());
5036    }
5037
5038    // Cast Timestamp(_, None) -> Timestamp(_, Some(timezone))
5039    #[test]
5040    fn test_cast_timestamp_with_timezone_1() {
5041        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
5042            Some("2000-01-01T00:00:00.123456789"),
5043            Some("2010-01-01T00:00:00.123456789"),
5044            None,
5045        ]));
5046        let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
5047        let timestamp_array = cast(&string_array, &to_type).unwrap();
5048
5049        let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
5050        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
5051
5052        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
5053        let result = string_array.as_string::<i32>();
5054        assert_eq!("2000-01-01T00:00:00.123456+07:00", result.value(0));
5055        assert_eq!("2010-01-01T00:00:00.123456+07:00", result.value(1));
5056        assert!(result.is_null(2));
5057    }
5058
5059    // Cast Timestamp(_, Some(timezone)) -> Timestamp(_, None)
5060    #[test]
5061    fn test_cast_timestamp_with_timezone_2() {
5062        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
5063            Some("2000-01-01T07:00:00.123456789"),
5064            Some("2010-01-01T07:00:00.123456789"),
5065            None,
5066        ]));
5067        let to_type = DataType::Timestamp(TimeUnit::Millisecond, Some("+0700".into()));
5068        let timestamp_array = cast(&string_array, &to_type).unwrap();
5069
5070        // Check intermediate representation is correct
5071        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
5072        let result = string_array.as_string::<i32>();
5073        assert_eq!("2000-01-01T07:00:00.123+07:00", result.value(0));
5074        assert_eq!("2010-01-01T07:00:00.123+07:00", result.value(1));
5075        assert!(result.is_null(2));
5076
5077        let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
5078        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
5079
5080        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
5081        let result = string_array.as_string::<i32>();
5082        assert_eq!("2000-01-01T00:00:00.123", result.value(0));
5083        assert_eq!("2010-01-01T00:00:00.123", result.value(1));
5084        assert!(result.is_null(2));
5085    }
5086
5087    // Cast Timestamp(_, Some(timezone)) -> Timestamp(_, Some(timezone))
5088    #[test]
5089    fn test_cast_timestamp_with_timezone_3() {
5090        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
5091            Some("2000-01-01T07:00:00.123456789"),
5092            Some("2010-01-01T07:00:00.123456789"),
5093            None,
5094        ]));
5095        let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
5096        let timestamp_array = cast(&string_array, &to_type).unwrap();
5097
5098        // Check intermediate representation is correct
5099        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
5100        let result = string_array.as_string::<i32>();
5101        assert_eq!("2000-01-01T07:00:00.123456+07:00", result.value(0));
5102        assert_eq!("2010-01-01T07:00:00.123456+07:00", result.value(1));
5103        assert!(result.is_null(2));
5104
5105        let to_type = DataType::Timestamp(TimeUnit::Second, Some("-08:00".into()));
5106        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
5107
5108        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
5109        let result = string_array.as_string::<i32>();
5110        assert_eq!("1999-12-31T16:00:00-08:00", result.value(0));
5111        assert_eq!("2009-12-31T16:00:00-08:00", result.value(1));
5112        assert!(result.is_null(2));
5113    }
5114
5115    #[test]
5116    fn test_cast_date64_to_timestamp() {
5117        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5118        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
5119        let c = b.as_primitive::<TimestampSecondType>();
5120        assert_eq!(864000000, c.value(0));
5121        assert_eq!(1545696000, c.value(1));
5122        assert!(c.is_null(2));
5123    }
5124
5125    #[test]
5126    fn test_cast_date64_to_timestamp_ms() {
5127        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5128        let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
5129        let c = b
5130            .as_any()
5131            .downcast_ref::<TimestampMillisecondArray>()
5132            .unwrap();
5133        assert_eq!(864000000005, c.value(0));
5134        assert_eq!(1545696000001, c.value(1));
5135        assert!(c.is_null(2));
5136    }
5137
5138    #[test]
5139    fn test_cast_date64_to_timestamp_us() {
5140        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5141        let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5142        let c = b
5143            .as_any()
5144            .downcast_ref::<TimestampMicrosecondArray>()
5145            .unwrap();
5146        assert_eq!(864000000005000, c.value(0));
5147        assert_eq!(1545696000001000, c.value(1));
5148        assert!(c.is_null(2));
5149    }
5150
5151    #[test]
5152    fn test_cast_date64_to_timestamp_ns() {
5153        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5154        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
5155        let c = b
5156            .as_any()
5157            .downcast_ref::<TimestampNanosecondArray>()
5158            .unwrap();
5159        assert_eq!(864000000005000000, c.value(0));
5160        assert_eq!(1545696000001000000, c.value(1));
5161        assert!(c.is_null(2));
5162    }
5163
5164    #[test]
5165    fn test_cast_timestamp_to_i64() {
5166        let array =
5167            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
5168                .with_timezone("UTC".to_string());
5169        let b = cast(&array, &DataType::Int64).unwrap();
5170        let c = b.as_primitive::<Int64Type>();
5171        assert_eq!(&DataType::Int64, c.data_type());
5172        assert_eq!(864000000005, c.value(0));
5173        assert_eq!(1545696000001, c.value(1));
5174        assert!(c.is_null(2));
5175    }
5176
5177    #[test]
5178    fn test_cast_date32_to_string() {
5179        let array = Date32Array::from(vec![10000, 17890]);
5180        let b = cast(&array, &DataType::Utf8).unwrap();
5181        let c = b.as_any().downcast_ref::<StringArray>().unwrap();
5182        assert_eq!(&DataType::Utf8, c.data_type());
5183        assert_eq!("1997-05-19", c.value(0));
5184        assert_eq!("2018-12-25", c.value(1));
5185    }
5186
5187    #[test]
5188    fn test_cast_date64_to_string() {
5189        let array = Date64Array::from(vec![10000 * 86400000, 17890 * 86400000]);
5190        let b = cast(&array, &DataType::Utf8).unwrap();
5191        let c = b.as_any().downcast_ref::<StringArray>().unwrap();
5192        assert_eq!(&DataType::Utf8, c.data_type());
5193        assert_eq!("1997-05-19T00:00:00", c.value(0));
5194        assert_eq!("2018-12-25T00:00:00", c.value(1));
5195    }
5196
5197    macro_rules! assert_cast_timestamp_to_string {
5198        ($array:expr, $datatype:expr, $output_array_type: ty, $expected:expr) => {{
5199            let out = cast(&$array, &$datatype).unwrap();
5200            let actual = out
5201                .as_any()
5202                .downcast_ref::<$output_array_type>()
5203                .unwrap()
5204                .into_iter()
5205                .collect::<Vec<_>>();
5206            assert_eq!(actual, $expected);
5207        }};
5208        ($array:expr, $datatype:expr, $output_array_type: ty, $options:expr, $expected:expr) => {{
5209            let out = cast_with_options(&$array, &$datatype, &$options).unwrap();
5210            let actual = out
5211                .as_any()
5212                .downcast_ref::<$output_array_type>()
5213                .unwrap()
5214                .into_iter()
5215                .collect::<Vec<_>>();
5216            assert_eq!(actual, $expected);
5217        }};
5218    }
5219
5220    #[test]
5221    fn test_cast_timestamp_to_strings() {
5222        // "2018-12-25T00:00:02.001", "1997-05-19T00:00:03.005", None
5223        let array =
5224            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
5225        let expected = vec![
5226            Some("1997-05-19T00:00:03.005"),
5227            Some("2018-12-25T00:00:02.001"),
5228            None,
5229        ];
5230
5231        assert_cast_timestamp_to_string!(array, DataType::Utf8View, StringViewArray, expected);
5232        assert_cast_timestamp_to_string!(array, DataType::Utf8, StringArray, expected);
5233        assert_cast_timestamp_to_string!(array, DataType::LargeUtf8, LargeStringArray, expected);
5234    }
5235
5236    #[test]
5237    fn test_cast_timestamp_to_strings_opt() {
5238        let ts_format = "%Y-%m-%d %H:%M:%S%.6f";
5239        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5240        let cast_options = CastOptions {
5241            safe: true,
5242            format_options: FormatOptions::default()
5243                .with_timestamp_format(Some(ts_format))
5244                .with_timestamp_tz_format(Some(ts_format)),
5245        };
5246
5247        // "2018-12-25T00:00:02.001", "1997-05-19T00:00:03.005", None
5248        let array_without_tz =
5249            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
5250        let expected = vec![
5251            Some("1997-05-19 00:00:03.005000"),
5252            Some("2018-12-25 00:00:02.001000"),
5253            None,
5254        ];
5255        assert_cast_timestamp_to_string!(
5256            array_without_tz,
5257            DataType::Utf8View,
5258            StringViewArray,
5259            cast_options,
5260            expected
5261        );
5262        assert_cast_timestamp_to_string!(
5263            array_without_tz,
5264            DataType::Utf8,
5265            StringArray,
5266            cast_options,
5267            expected
5268        );
5269        assert_cast_timestamp_to_string!(
5270            array_without_tz,
5271            DataType::LargeUtf8,
5272            LargeStringArray,
5273            cast_options,
5274            expected
5275        );
5276
5277        let array_with_tz =
5278            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None])
5279                .with_timezone(tz.to_string());
5280        let expected = vec![
5281            Some("1997-05-19 05:45:03.005000"),
5282            Some("2018-12-25 05:45:02.001000"),
5283            None,
5284        ];
5285        assert_cast_timestamp_to_string!(
5286            array_with_tz,
5287            DataType::Utf8View,
5288            StringViewArray,
5289            cast_options,
5290            expected
5291        );
5292        assert_cast_timestamp_to_string!(
5293            array_with_tz,
5294            DataType::Utf8,
5295            StringArray,
5296            cast_options,
5297            expected
5298        );
5299        assert_cast_timestamp_to_string!(
5300            array_with_tz,
5301            DataType::LargeUtf8,
5302            LargeStringArray,
5303            cast_options,
5304            expected
5305        );
5306    }
5307
5308    #[test]
5309    fn test_cast_between_timestamps() {
5310        let array =
5311            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
5312        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
5313        let c = b.as_primitive::<TimestampSecondType>();
5314        assert_eq!(864000003, c.value(0));
5315        assert_eq!(1545696002, c.value(1));
5316        assert!(c.is_null(2));
5317    }
5318
5319    #[test]
5320    fn test_cast_duration_to_i64() {
5321        let base = vec![5, 6, 7, 8, 100000000];
5322
5323        let duration_arrays = vec![
5324            Arc::new(DurationNanosecondArray::from(base.clone())) as ArrayRef,
5325            Arc::new(DurationMicrosecondArray::from(base.clone())) as ArrayRef,
5326            Arc::new(DurationMillisecondArray::from(base.clone())) as ArrayRef,
5327            Arc::new(DurationSecondArray::from(base.clone())) as ArrayRef,
5328        ];
5329
5330        for arr in duration_arrays {
5331            assert!(can_cast_types(arr.data_type(), &DataType::Int64));
5332            let result = cast(&arr, &DataType::Int64).unwrap();
5333            let result = result.as_primitive::<Int64Type>();
5334            assert_eq!(base.as_slice(), result.values());
5335        }
5336    }
5337
5338    #[test]
5339    fn test_cast_between_durations_and_numerics() {
5340        fn test_cast_between_durations<FromType, ToType>()
5341        where
5342            FromType: ArrowPrimitiveType<Native = i64>,
5343            ToType: ArrowPrimitiveType<Native = i64>,
5344            PrimitiveArray<FromType>: From<Vec<Option<i64>>>,
5345        {
5346            let from_unit = match FromType::DATA_TYPE {
5347                DataType::Duration(unit) => unit,
5348                _ => panic!("Expected a duration type"),
5349            };
5350            let to_unit = match ToType::DATA_TYPE {
5351                DataType::Duration(unit) => unit,
5352                _ => panic!("Expected a duration type"),
5353            };
5354            let from_size = time_unit_multiple(&from_unit);
5355            let to_size = time_unit_multiple(&to_unit);
5356
5357            let (v1_before, v2_before) = (8640003005, 1696002001);
5358            let (v1_after, v2_after) = if from_size >= to_size {
5359                (
5360                    v1_before / (from_size / to_size),
5361                    v2_before / (from_size / to_size),
5362                )
5363            } else {
5364                (
5365                    v1_before * (to_size / from_size),
5366                    v2_before * (to_size / from_size),
5367                )
5368            };
5369
5370            let array =
5371                PrimitiveArray::<FromType>::from(vec![Some(v1_before), Some(v2_before), None]);
5372            let b = cast(&array, &ToType::DATA_TYPE).unwrap();
5373            let c = b.as_primitive::<ToType>();
5374            assert_eq!(v1_after, c.value(0));
5375            assert_eq!(v2_after, c.value(1));
5376            assert!(c.is_null(2));
5377        }
5378
5379        // between each individual duration type
5380        test_cast_between_durations::<DurationSecondType, DurationMillisecondType>();
5381        test_cast_between_durations::<DurationSecondType, DurationMicrosecondType>();
5382        test_cast_between_durations::<DurationSecondType, DurationNanosecondType>();
5383        test_cast_between_durations::<DurationMillisecondType, DurationSecondType>();
5384        test_cast_between_durations::<DurationMillisecondType, DurationMicrosecondType>();
5385        test_cast_between_durations::<DurationMillisecondType, DurationNanosecondType>();
5386        test_cast_between_durations::<DurationMicrosecondType, DurationSecondType>();
5387        test_cast_between_durations::<DurationMicrosecondType, DurationMillisecondType>();
5388        test_cast_between_durations::<DurationMicrosecondType, DurationNanosecondType>();
5389        test_cast_between_durations::<DurationNanosecondType, DurationSecondType>();
5390        test_cast_between_durations::<DurationNanosecondType, DurationMillisecondType>();
5391        test_cast_between_durations::<DurationNanosecondType, DurationMicrosecondType>();
5392
5393        // cast failed
5394        let array = DurationSecondArray::from(vec![
5395            Some(i64::MAX),
5396            Some(8640203410378005),
5397            Some(10241096),
5398            None,
5399        ]);
5400        let b = cast(&array, &DataType::Duration(TimeUnit::Nanosecond)).unwrap();
5401        let c = b.as_primitive::<DurationNanosecondType>();
5402        assert!(c.is_null(0));
5403        assert!(c.is_null(1));
5404        assert_eq!(10241096000000000, c.value(2));
5405        assert!(c.is_null(3));
5406
5407        // durations to numerics
5408        let array = DurationSecondArray::from(vec![
5409            Some(i64::MAX),
5410            Some(8640203410378005),
5411            Some(10241096),
5412            None,
5413        ]);
5414        let b = cast(&array, &DataType::Int64).unwrap();
5415        let c = b.as_primitive::<Int64Type>();
5416        assert_eq!(i64::MAX, c.value(0));
5417        assert_eq!(8640203410378005, c.value(1));
5418        assert_eq!(10241096, c.value(2));
5419        assert!(c.is_null(3));
5420
5421        let b = cast(&array, &DataType::Int32).unwrap();
5422        let c = b.as_primitive::<Int32Type>();
5423        assert_eq!(0, c.value(0));
5424        assert_eq!(0, c.value(1));
5425        assert_eq!(10241096, c.value(2));
5426        assert!(c.is_null(3));
5427
5428        // numerics to durations
5429        let array = Int32Array::from(vec![Some(i32::MAX), Some(802034103), Some(10241096), None]);
5430        let b = cast(&array, &DataType::Duration(TimeUnit::Second)).unwrap();
5431        let c = b.as_any().downcast_ref::<DurationSecondArray>().unwrap();
5432        assert_eq!(i32::MAX as i64, c.value(0));
5433        assert_eq!(802034103, c.value(1));
5434        assert_eq!(10241096, c.value(2));
5435        assert!(c.is_null(3));
5436    }
5437
5438    #[test]
5439    fn test_cast_to_strings() {
5440        let a = Int32Array::from(vec![1, 2, 3]);
5441        let out = cast(&a, &DataType::Utf8).unwrap();
5442        let out = out
5443            .as_any()
5444            .downcast_ref::<StringArray>()
5445            .unwrap()
5446            .into_iter()
5447            .collect::<Vec<_>>();
5448        assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
5449        let out = cast(&a, &DataType::LargeUtf8).unwrap();
5450        let out = out
5451            .as_any()
5452            .downcast_ref::<LargeStringArray>()
5453            .unwrap()
5454            .into_iter()
5455            .collect::<Vec<_>>();
5456        assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
5457    }
5458
5459    #[test]
5460    fn test_str_to_str_casts() {
5461        for data in [
5462            vec![Some("foo"), Some("bar"), Some("ham")],
5463            vec![Some("foo"), None, Some("bar")],
5464        ] {
5465            let a = LargeStringArray::from(data.clone());
5466            let to = cast(&a, &DataType::Utf8).unwrap();
5467            let expect = a
5468                .as_any()
5469                .downcast_ref::<LargeStringArray>()
5470                .unwrap()
5471                .into_iter()
5472                .collect::<Vec<_>>();
5473            let out = to
5474                .as_any()
5475                .downcast_ref::<StringArray>()
5476                .unwrap()
5477                .into_iter()
5478                .collect::<Vec<_>>();
5479            assert_eq!(expect, out);
5480
5481            let a = StringArray::from(data);
5482            let to = cast(&a, &DataType::LargeUtf8).unwrap();
5483            let expect = a
5484                .as_any()
5485                .downcast_ref::<StringArray>()
5486                .unwrap()
5487                .into_iter()
5488                .collect::<Vec<_>>();
5489            let out = to
5490                .as_any()
5491                .downcast_ref::<LargeStringArray>()
5492                .unwrap()
5493                .into_iter()
5494                .collect::<Vec<_>>();
5495            assert_eq!(expect, out);
5496        }
5497    }
5498
5499    const VIEW_TEST_DATA: [Option<&str>; 5] = [
5500        Some("hello"),
5501        Some("repeated"),
5502        None,
5503        Some("large payload over 12 bytes"),
5504        Some("repeated"),
5505    ];
5506
5507    #[test]
5508    fn test_string_view_to_binary_view() {
5509        let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
5510
5511        assert!(can_cast_types(
5512            string_view_array.data_type(),
5513            &DataType::BinaryView
5514        ));
5515
5516        let binary_view_array = cast(&string_view_array, &DataType::BinaryView).unwrap();
5517        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
5518
5519        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
5520        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
5521    }
5522
5523    #[test]
5524    fn test_binary_view_to_string_view() {
5525        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
5526
5527        assert!(can_cast_types(
5528            binary_view_array.data_type(),
5529            &DataType::Utf8View
5530        ));
5531
5532        let string_view_array = cast(&binary_view_array, &DataType::Utf8View).unwrap();
5533        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
5534
5535        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
5536        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
5537    }
5538
5539    #[test]
5540    fn test_string_to_view() {
5541        _test_string_to_view::<i32>();
5542        _test_string_to_view::<i64>();
5543    }
5544
5545    fn _test_string_to_view<O>()
5546    where
5547        O: OffsetSizeTrait,
5548    {
5549        let string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
5550
5551        assert!(can_cast_types(
5552            string_array.data_type(),
5553            &DataType::Utf8View
5554        ));
5555
5556        assert!(can_cast_types(
5557            string_array.data_type(),
5558            &DataType::BinaryView
5559        ));
5560
5561        let string_view_array = cast(&string_array, &DataType::Utf8View).unwrap();
5562        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
5563
5564        let binary_view_array = cast(&string_array, &DataType::BinaryView).unwrap();
5565        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
5566
5567        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
5568        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
5569
5570        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
5571        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
5572    }
5573
5574    #[test]
5575    fn test_bianry_to_view() {
5576        _test_binary_to_view::<i32>();
5577        _test_binary_to_view::<i64>();
5578    }
5579
5580    fn _test_binary_to_view<O>()
5581    where
5582        O: OffsetSizeTrait,
5583    {
5584        let binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
5585
5586        assert!(can_cast_types(
5587            binary_array.data_type(),
5588            &DataType::Utf8View
5589        ));
5590
5591        assert!(can_cast_types(
5592            binary_array.data_type(),
5593            &DataType::BinaryView
5594        ));
5595
5596        let string_view_array = cast(&binary_array, &DataType::Utf8View).unwrap();
5597        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
5598
5599        let binary_view_array = cast(&binary_array, &DataType::BinaryView).unwrap();
5600        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
5601
5602        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
5603        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
5604
5605        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
5606        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
5607    }
5608
5609    #[test]
5610    fn test_dict_to_view() {
5611        let values = StringArray::from_iter(VIEW_TEST_DATA);
5612        let keys = Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
5613        let string_dict_array =
5614            DictionaryArray::<Int8Type>::try_new(keys, Arc::new(values)).unwrap();
5615        let typed_dict = string_dict_array.downcast_dict::<StringArray>().unwrap();
5616
5617        let string_view_array = {
5618            let mut builder = StringViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
5619            for v in typed_dict.into_iter() {
5620                builder.append_option(v);
5621            }
5622            builder.finish()
5623        };
5624        let expected_string_array_type = string_view_array.data_type();
5625        let casted_string_array = cast(&string_dict_array, expected_string_array_type).unwrap();
5626        assert_eq!(casted_string_array.data_type(), expected_string_array_type);
5627        assert_eq!(casted_string_array.as_ref(), &string_view_array);
5628
5629        let binary_buffer = cast(&typed_dict.values(), &DataType::Binary).unwrap();
5630        let binary_dict_array =
5631            DictionaryArray::<Int8Type>::new(typed_dict.keys().clone(), binary_buffer);
5632        let typed_binary_dict = binary_dict_array.downcast_dict::<BinaryArray>().unwrap();
5633
5634        let binary_view_array = {
5635            let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
5636            for v in typed_binary_dict.into_iter() {
5637                builder.append_option(v);
5638            }
5639            builder.finish()
5640        };
5641        let expected_binary_array_type = binary_view_array.data_type();
5642        let casted_binary_array = cast(&binary_dict_array, expected_binary_array_type).unwrap();
5643        assert_eq!(casted_binary_array.data_type(), expected_binary_array_type);
5644        assert_eq!(casted_binary_array.as_ref(), &binary_view_array);
5645    }
5646
5647    #[test]
5648    fn test_view_to_dict() {
5649        let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
5650        let string_dict_array: DictionaryArray<Int8Type> = VIEW_TEST_DATA.into_iter().collect();
5651        let casted_type = string_dict_array.data_type();
5652        let casted_dict_array = cast(&string_view_array, casted_type).unwrap();
5653        assert_eq!(casted_dict_array.data_type(), casted_type);
5654        assert_eq!(casted_dict_array.as_ref(), &string_dict_array);
5655
5656        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
5657        let binary_dict_array = string_dict_array.downcast_dict::<StringArray>().unwrap();
5658        let binary_buffer = cast(&binary_dict_array.values(), &DataType::Binary).unwrap();
5659        let binary_dict_array =
5660            DictionaryArray::<Int8Type>::new(binary_dict_array.keys().clone(), binary_buffer);
5661        let casted_type = binary_dict_array.data_type();
5662        let casted_binary_array = cast(&binary_view_array, casted_type).unwrap();
5663        assert_eq!(casted_binary_array.data_type(), casted_type);
5664        assert_eq!(casted_binary_array.as_ref(), &binary_dict_array);
5665    }
5666
5667    #[test]
5668    fn test_view_to_string() {
5669        _test_view_to_string::<i32>();
5670        _test_view_to_string::<i64>();
5671    }
5672
5673    fn _test_view_to_string<O>()
5674    where
5675        O: OffsetSizeTrait,
5676    {
5677        let string_view_array = {
5678            let mut builder = StringViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
5679            for s in VIEW_TEST_DATA.iter() {
5680                builder.append_option(*s);
5681            }
5682            builder.finish()
5683        };
5684
5685        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
5686
5687        let expected_string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
5688        let expected_type = expected_string_array.data_type();
5689
5690        assert!(can_cast_types(string_view_array.data_type(), expected_type));
5691        assert!(can_cast_types(binary_view_array.data_type(), expected_type));
5692
5693        let string_view_casted_array = cast(&string_view_array, expected_type).unwrap();
5694        assert_eq!(string_view_casted_array.data_type(), expected_type);
5695        assert_eq!(string_view_casted_array.as_ref(), &expected_string_array);
5696
5697        let binary_view_casted_array = cast(&binary_view_array, expected_type).unwrap();
5698        assert_eq!(binary_view_casted_array.data_type(), expected_type);
5699        assert_eq!(binary_view_casted_array.as_ref(), &expected_string_array);
5700    }
5701
5702    #[test]
5703    fn test_view_to_binary() {
5704        _test_view_to_binary::<i32>();
5705        _test_view_to_binary::<i64>();
5706    }
5707
5708    fn _test_view_to_binary<O>()
5709    where
5710        O: OffsetSizeTrait,
5711    {
5712        let view_array = {
5713            let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
5714            for s in VIEW_TEST_DATA.iter() {
5715                builder.append_option(*s);
5716            }
5717            builder.finish()
5718        };
5719
5720        let expected_binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
5721        let expected_type = expected_binary_array.data_type();
5722
5723        assert!(can_cast_types(view_array.data_type(), expected_type));
5724
5725        let binary_array = cast(&view_array, expected_type).unwrap();
5726        assert_eq!(binary_array.data_type(), expected_type);
5727
5728        assert_eq!(binary_array.as_ref(), &expected_binary_array);
5729    }
5730
5731    #[test]
5732    fn test_cast_from_f64() {
5733        let f64_values: Vec<f64> = vec![
5734            i64::MIN as f64,
5735            i32::MIN as f64,
5736            i16::MIN as f64,
5737            i8::MIN as f64,
5738            0_f64,
5739            u8::MAX as f64,
5740            u16::MAX as f64,
5741            u32::MAX as f64,
5742            u64::MAX as f64,
5743        ];
5744        let f64_array: ArrayRef = Arc::new(Float64Array::from(f64_values));
5745
5746        let f64_expected = vec![
5747            -9223372036854776000.0,
5748            -2147483648.0,
5749            -32768.0,
5750            -128.0,
5751            0.0,
5752            255.0,
5753            65535.0,
5754            4294967295.0,
5755            18446744073709552000.0,
5756        ];
5757        assert_eq!(
5758            f64_expected,
5759            get_cast_values::<Float64Type>(&f64_array, &DataType::Float64)
5760                .iter()
5761                .map(|i| i.parse::<f64>().unwrap())
5762                .collect::<Vec<f64>>()
5763        );
5764
5765        let f32_expected = vec![
5766            -9223372000000000000.0,
5767            -2147483600.0,
5768            -32768.0,
5769            -128.0,
5770            0.0,
5771            255.0,
5772            65535.0,
5773            4294967300.0,
5774            18446744000000000000.0,
5775        ];
5776        assert_eq!(
5777            f32_expected,
5778            get_cast_values::<Float32Type>(&f64_array, &DataType::Float32)
5779                .iter()
5780                .map(|i| i.parse::<f32>().unwrap())
5781                .collect::<Vec<f32>>()
5782        );
5783
5784        let f16_expected = vec![
5785            f16::from_f64(-9223372000000000000.0),
5786            f16::from_f64(-2147483600.0),
5787            f16::from_f64(-32768.0),
5788            f16::from_f64(-128.0),
5789            f16::from_f64(0.0),
5790            f16::from_f64(255.0),
5791            f16::from_f64(65535.0),
5792            f16::from_f64(4294967300.0),
5793            f16::from_f64(18446744000000000000.0),
5794        ];
5795        assert_eq!(
5796            f16_expected,
5797            get_cast_values::<Float16Type>(&f64_array, &DataType::Float16)
5798                .iter()
5799                .map(|i| i.parse::<f16>().unwrap())
5800                .collect::<Vec<f16>>()
5801        );
5802
5803        let i64_expected = vec![
5804            "-9223372036854775808",
5805            "-2147483648",
5806            "-32768",
5807            "-128",
5808            "0",
5809            "255",
5810            "65535",
5811            "4294967295",
5812            "null",
5813        ];
5814        assert_eq!(
5815            i64_expected,
5816            get_cast_values::<Int64Type>(&f64_array, &DataType::Int64)
5817        );
5818
5819        let i32_expected = vec![
5820            "null",
5821            "-2147483648",
5822            "-32768",
5823            "-128",
5824            "0",
5825            "255",
5826            "65535",
5827            "null",
5828            "null",
5829        ];
5830        assert_eq!(
5831            i32_expected,
5832            get_cast_values::<Int32Type>(&f64_array, &DataType::Int32)
5833        );
5834
5835        let i16_expected = vec![
5836            "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
5837        ];
5838        assert_eq!(
5839            i16_expected,
5840            get_cast_values::<Int16Type>(&f64_array, &DataType::Int16)
5841        );
5842
5843        let i8_expected = vec![
5844            "null", "null", "null", "-128", "0", "null", "null", "null", "null",
5845        ];
5846        assert_eq!(
5847            i8_expected,
5848            get_cast_values::<Int8Type>(&f64_array, &DataType::Int8)
5849        );
5850
5851        let u64_expected = vec![
5852            "null",
5853            "null",
5854            "null",
5855            "null",
5856            "0",
5857            "255",
5858            "65535",
5859            "4294967295",
5860            "null",
5861        ];
5862        assert_eq!(
5863            u64_expected,
5864            get_cast_values::<UInt64Type>(&f64_array, &DataType::UInt64)
5865        );
5866
5867        let u32_expected = vec![
5868            "null",
5869            "null",
5870            "null",
5871            "null",
5872            "0",
5873            "255",
5874            "65535",
5875            "4294967295",
5876            "null",
5877        ];
5878        assert_eq!(
5879            u32_expected,
5880            get_cast_values::<UInt32Type>(&f64_array, &DataType::UInt32)
5881        );
5882
5883        let u16_expected = vec![
5884            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
5885        ];
5886        assert_eq!(
5887            u16_expected,
5888            get_cast_values::<UInt16Type>(&f64_array, &DataType::UInt16)
5889        );
5890
5891        let u8_expected = vec![
5892            "null", "null", "null", "null", "0", "255", "null", "null", "null",
5893        ];
5894        assert_eq!(
5895            u8_expected,
5896            get_cast_values::<UInt8Type>(&f64_array, &DataType::UInt8)
5897        );
5898    }
5899
5900    #[test]
5901    fn test_cast_from_f32() {
5902        let f32_values: Vec<f32> = vec![
5903            i32::MIN as f32,
5904            i32::MIN as f32,
5905            i16::MIN as f32,
5906            i8::MIN as f32,
5907            0_f32,
5908            u8::MAX as f32,
5909            u16::MAX as f32,
5910            u32::MAX as f32,
5911            u32::MAX as f32,
5912        ];
5913        let f32_array: ArrayRef = Arc::new(Float32Array::from(f32_values));
5914
5915        let f64_expected = vec![
5916            "-2147483648.0",
5917            "-2147483648.0",
5918            "-32768.0",
5919            "-128.0",
5920            "0.0",
5921            "255.0",
5922            "65535.0",
5923            "4294967296.0",
5924            "4294967296.0",
5925        ];
5926        assert_eq!(
5927            f64_expected,
5928            get_cast_values::<Float64Type>(&f32_array, &DataType::Float64)
5929        );
5930
5931        let f32_expected = vec![
5932            "-2147483600.0",
5933            "-2147483600.0",
5934            "-32768.0",
5935            "-128.0",
5936            "0.0",
5937            "255.0",
5938            "65535.0",
5939            "4294967300.0",
5940            "4294967300.0",
5941        ];
5942        assert_eq!(
5943            f32_expected,
5944            get_cast_values::<Float32Type>(&f32_array, &DataType::Float32)
5945        );
5946
5947        let f16_expected = vec![
5948            "-inf", "-inf", "-32768.0", "-128.0", "0.0", "255.0", "inf", "inf", "inf",
5949        ];
5950        assert_eq!(
5951            f16_expected,
5952            get_cast_values::<Float16Type>(&f32_array, &DataType::Float16)
5953        );
5954
5955        let i64_expected = vec![
5956            "-2147483648",
5957            "-2147483648",
5958            "-32768",
5959            "-128",
5960            "0",
5961            "255",
5962            "65535",
5963            "4294967296",
5964            "4294967296",
5965        ];
5966        assert_eq!(
5967            i64_expected,
5968            get_cast_values::<Int64Type>(&f32_array, &DataType::Int64)
5969        );
5970
5971        let i32_expected = vec![
5972            "-2147483648",
5973            "-2147483648",
5974            "-32768",
5975            "-128",
5976            "0",
5977            "255",
5978            "65535",
5979            "null",
5980            "null",
5981        ];
5982        assert_eq!(
5983            i32_expected,
5984            get_cast_values::<Int32Type>(&f32_array, &DataType::Int32)
5985        );
5986
5987        let i16_expected = vec![
5988            "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
5989        ];
5990        assert_eq!(
5991            i16_expected,
5992            get_cast_values::<Int16Type>(&f32_array, &DataType::Int16)
5993        );
5994
5995        let i8_expected = vec![
5996            "null", "null", "null", "-128", "0", "null", "null", "null", "null",
5997        ];
5998        assert_eq!(
5999            i8_expected,
6000            get_cast_values::<Int8Type>(&f32_array, &DataType::Int8)
6001        );
6002
6003        let u64_expected = vec![
6004            "null",
6005            "null",
6006            "null",
6007            "null",
6008            "0",
6009            "255",
6010            "65535",
6011            "4294967296",
6012            "4294967296",
6013        ];
6014        assert_eq!(
6015            u64_expected,
6016            get_cast_values::<UInt64Type>(&f32_array, &DataType::UInt64)
6017        );
6018
6019        let u32_expected = vec![
6020            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
6021        ];
6022        assert_eq!(
6023            u32_expected,
6024            get_cast_values::<UInt32Type>(&f32_array, &DataType::UInt32)
6025        );
6026
6027        let u16_expected = vec![
6028            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
6029        ];
6030        assert_eq!(
6031            u16_expected,
6032            get_cast_values::<UInt16Type>(&f32_array, &DataType::UInt16)
6033        );
6034
6035        let u8_expected = vec![
6036            "null", "null", "null", "null", "0", "255", "null", "null", "null",
6037        ];
6038        assert_eq!(
6039            u8_expected,
6040            get_cast_values::<UInt8Type>(&f32_array, &DataType::UInt8)
6041        );
6042    }
6043
6044    #[test]
6045    fn test_cast_from_uint64() {
6046        let u64_values: Vec<u64> = vec![
6047            0,
6048            u8::MAX as u64,
6049            u16::MAX as u64,
6050            u32::MAX as u64,
6051            u64::MAX,
6052        ];
6053        let u64_array: ArrayRef = Arc::new(UInt64Array::from(u64_values));
6054
6055        let f64_expected = vec![0.0, 255.0, 65535.0, 4294967295.0, 18446744073709552000.0];
6056        assert_eq!(
6057            f64_expected,
6058            get_cast_values::<Float64Type>(&u64_array, &DataType::Float64)
6059                .iter()
6060                .map(|i| i.parse::<f64>().unwrap())
6061                .collect::<Vec<f64>>()
6062        );
6063
6064        let f32_expected = vec![0.0, 255.0, 65535.0, 4294967300.0, 18446744000000000000.0];
6065        assert_eq!(
6066            f32_expected,
6067            get_cast_values::<Float32Type>(&u64_array, &DataType::Float32)
6068                .iter()
6069                .map(|i| i.parse::<f32>().unwrap())
6070                .collect::<Vec<f32>>()
6071        );
6072
6073        let f16_expected = vec![
6074            f16::from_f64(0.0),
6075            f16::from_f64(255.0),
6076            f16::from_f64(65535.0),
6077            f16::from_f64(4294967300.0),
6078            f16::from_f64(18446744000000000000.0),
6079        ];
6080        assert_eq!(
6081            f16_expected,
6082            get_cast_values::<Float16Type>(&u64_array, &DataType::Float16)
6083                .iter()
6084                .map(|i| i.parse::<f16>().unwrap())
6085                .collect::<Vec<f16>>()
6086        );
6087
6088        let i64_expected = vec!["0", "255", "65535", "4294967295", "null"];
6089        assert_eq!(
6090            i64_expected,
6091            get_cast_values::<Int64Type>(&u64_array, &DataType::Int64)
6092        );
6093
6094        let i32_expected = vec!["0", "255", "65535", "null", "null"];
6095        assert_eq!(
6096            i32_expected,
6097            get_cast_values::<Int32Type>(&u64_array, &DataType::Int32)
6098        );
6099
6100        let i16_expected = vec!["0", "255", "null", "null", "null"];
6101        assert_eq!(
6102            i16_expected,
6103            get_cast_values::<Int16Type>(&u64_array, &DataType::Int16)
6104        );
6105
6106        let i8_expected = vec!["0", "null", "null", "null", "null"];
6107        assert_eq!(
6108            i8_expected,
6109            get_cast_values::<Int8Type>(&u64_array, &DataType::Int8)
6110        );
6111
6112        let u64_expected = vec!["0", "255", "65535", "4294967295", "18446744073709551615"];
6113        assert_eq!(
6114            u64_expected,
6115            get_cast_values::<UInt64Type>(&u64_array, &DataType::UInt64)
6116        );
6117
6118        let u32_expected = vec!["0", "255", "65535", "4294967295", "null"];
6119        assert_eq!(
6120            u32_expected,
6121            get_cast_values::<UInt32Type>(&u64_array, &DataType::UInt32)
6122        );
6123
6124        let u16_expected = vec!["0", "255", "65535", "null", "null"];
6125        assert_eq!(
6126            u16_expected,
6127            get_cast_values::<UInt16Type>(&u64_array, &DataType::UInt16)
6128        );
6129
6130        let u8_expected = vec!["0", "255", "null", "null", "null"];
6131        assert_eq!(
6132            u8_expected,
6133            get_cast_values::<UInt8Type>(&u64_array, &DataType::UInt8)
6134        );
6135    }
6136
6137    #[test]
6138    fn test_cast_from_uint32() {
6139        let u32_values: Vec<u32> = vec![0, u8::MAX as u32, u16::MAX as u32, u32::MAX];
6140        let u32_array: ArrayRef = Arc::new(UInt32Array::from(u32_values));
6141
6142        let f64_expected = vec!["0.0", "255.0", "65535.0", "4294967295.0"];
6143        assert_eq!(
6144            f64_expected,
6145            get_cast_values::<Float64Type>(&u32_array, &DataType::Float64)
6146        );
6147
6148        let f32_expected = vec!["0.0", "255.0", "65535.0", "4294967300.0"];
6149        assert_eq!(
6150            f32_expected,
6151            get_cast_values::<Float32Type>(&u32_array, &DataType::Float32)
6152        );
6153
6154        let f16_expected = vec!["0.0", "255.0", "inf", "inf"];
6155        assert_eq!(
6156            f16_expected,
6157            get_cast_values::<Float16Type>(&u32_array, &DataType::Float16)
6158        );
6159
6160        let i64_expected = vec!["0", "255", "65535", "4294967295"];
6161        assert_eq!(
6162            i64_expected,
6163            get_cast_values::<Int64Type>(&u32_array, &DataType::Int64)
6164        );
6165
6166        let i32_expected = vec!["0", "255", "65535", "null"];
6167        assert_eq!(
6168            i32_expected,
6169            get_cast_values::<Int32Type>(&u32_array, &DataType::Int32)
6170        );
6171
6172        let i16_expected = vec!["0", "255", "null", "null"];
6173        assert_eq!(
6174            i16_expected,
6175            get_cast_values::<Int16Type>(&u32_array, &DataType::Int16)
6176        );
6177
6178        let i8_expected = vec!["0", "null", "null", "null"];
6179        assert_eq!(
6180            i8_expected,
6181            get_cast_values::<Int8Type>(&u32_array, &DataType::Int8)
6182        );
6183
6184        let u64_expected = vec!["0", "255", "65535", "4294967295"];
6185        assert_eq!(
6186            u64_expected,
6187            get_cast_values::<UInt64Type>(&u32_array, &DataType::UInt64)
6188        );
6189
6190        let u32_expected = vec!["0", "255", "65535", "4294967295"];
6191        assert_eq!(
6192            u32_expected,
6193            get_cast_values::<UInt32Type>(&u32_array, &DataType::UInt32)
6194        );
6195
6196        let u16_expected = vec!["0", "255", "65535", "null"];
6197        assert_eq!(
6198            u16_expected,
6199            get_cast_values::<UInt16Type>(&u32_array, &DataType::UInt16)
6200        );
6201
6202        let u8_expected = vec!["0", "255", "null", "null"];
6203        assert_eq!(
6204            u8_expected,
6205            get_cast_values::<UInt8Type>(&u32_array, &DataType::UInt8)
6206        );
6207    }
6208
6209    #[test]
6210    fn test_cast_from_uint16() {
6211        let u16_values: Vec<u16> = vec![0, u8::MAX as u16, u16::MAX];
6212        let u16_array: ArrayRef = Arc::new(UInt16Array::from(u16_values));
6213
6214        let f64_expected = vec!["0.0", "255.0", "65535.0"];
6215        assert_eq!(
6216            f64_expected,
6217            get_cast_values::<Float64Type>(&u16_array, &DataType::Float64)
6218        );
6219
6220        let f32_expected = vec!["0.0", "255.0", "65535.0"];
6221        assert_eq!(
6222            f32_expected,
6223            get_cast_values::<Float32Type>(&u16_array, &DataType::Float32)
6224        );
6225
6226        let f16_expected = vec!["0.0", "255.0", "inf"];
6227        assert_eq!(
6228            f16_expected,
6229            get_cast_values::<Float16Type>(&u16_array, &DataType::Float16)
6230        );
6231
6232        let i64_expected = vec!["0", "255", "65535"];
6233        assert_eq!(
6234            i64_expected,
6235            get_cast_values::<Int64Type>(&u16_array, &DataType::Int64)
6236        );
6237
6238        let i32_expected = vec!["0", "255", "65535"];
6239        assert_eq!(
6240            i32_expected,
6241            get_cast_values::<Int32Type>(&u16_array, &DataType::Int32)
6242        );
6243
6244        let i16_expected = vec!["0", "255", "null"];
6245        assert_eq!(
6246            i16_expected,
6247            get_cast_values::<Int16Type>(&u16_array, &DataType::Int16)
6248        );
6249
6250        let i8_expected = vec!["0", "null", "null"];
6251        assert_eq!(
6252            i8_expected,
6253            get_cast_values::<Int8Type>(&u16_array, &DataType::Int8)
6254        );
6255
6256        let u64_expected = vec!["0", "255", "65535"];
6257        assert_eq!(
6258            u64_expected,
6259            get_cast_values::<UInt64Type>(&u16_array, &DataType::UInt64)
6260        );
6261
6262        let u32_expected = vec!["0", "255", "65535"];
6263        assert_eq!(
6264            u32_expected,
6265            get_cast_values::<UInt32Type>(&u16_array, &DataType::UInt32)
6266        );
6267
6268        let u16_expected = vec!["0", "255", "65535"];
6269        assert_eq!(
6270            u16_expected,
6271            get_cast_values::<UInt16Type>(&u16_array, &DataType::UInt16)
6272        );
6273
6274        let u8_expected = vec!["0", "255", "null"];
6275        assert_eq!(
6276            u8_expected,
6277            get_cast_values::<UInt8Type>(&u16_array, &DataType::UInt8)
6278        );
6279    }
6280
6281    #[test]
6282    fn test_cast_from_uint8() {
6283        let u8_values: Vec<u8> = vec![0, u8::MAX];
6284        let u8_array: ArrayRef = Arc::new(UInt8Array::from(u8_values));
6285
6286        let f64_expected = vec!["0.0", "255.0"];
6287        assert_eq!(
6288            f64_expected,
6289            get_cast_values::<Float64Type>(&u8_array, &DataType::Float64)
6290        );
6291
6292        let f32_expected = vec!["0.0", "255.0"];
6293        assert_eq!(
6294            f32_expected,
6295            get_cast_values::<Float32Type>(&u8_array, &DataType::Float32)
6296        );
6297
6298        let f16_expected = vec!["0.0", "255.0"];
6299        assert_eq!(
6300            f16_expected,
6301            get_cast_values::<Float16Type>(&u8_array, &DataType::Float16)
6302        );
6303
6304        let i64_expected = vec!["0", "255"];
6305        assert_eq!(
6306            i64_expected,
6307            get_cast_values::<Int64Type>(&u8_array, &DataType::Int64)
6308        );
6309
6310        let i32_expected = vec!["0", "255"];
6311        assert_eq!(
6312            i32_expected,
6313            get_cast_values::<Int32Type>(&u8_array, &DataType::Int32)
6314        );
6315
6316        let i16_expected = vec!["0", "255"];
6317        assert_eq!(
6318            i16_expected,
6319            get_cast_values::<Int16Type>(&u8_array, &DataType::Int16)
6320        );
6321
6322        let i8_expected = vec!["0", "null"];
6323        assert_eq!(
6324            i8_expected,
6325            get_cast_values::<Int8Type>(&u8_array, &DataType::Int8)
6326        );
6327
6328        let u64_expected = vec!["0", "255"];
6329        assert_eq!(
6330            u64_expected,
6331            get_cast_values::<UInt64Type>(&u8_array, &DataType::UInt64)
6332        );
6333
6334        let u32_expected = vec!["0", "255"];
6335        assert_eq!(
6336            u32_expected,
6337            get_cast_values::<UInt32Type>(&u8_array, &DataType::UInt32)
6338        );
6339
6340        let u16_expected = vec!["0", "255"];
6341        assert_eq!(
6342            u16_expected,
6343            get_cast_values::<UInt16Type>(&u8_array, &DataType::UInt16)
6344        );
6345
6346        let u8_expected = vec!["0", "255"];
6347        assert_eq!(
6348            u8_expected,
6349            get_cast_values::<UInt8Type>(&u8_array, &DataType::UInt8)
6350        );
6351    }
6352
6353    #[test]
6354    fn test_cast_from_int64() {
6355        let i64_values: Vec<i64> = vec![
6356            i64::MIN,
6357            i32::MIN as i64,
6358            i16::MIN as i64,
6359            i8::MIN as i64,
6360            0,
6361            i8::MAX as i64,
6362            i16::MAX as i64,
6363            i32::MAX as i64,
6364            i64::MAX,
6365        ];
6366        let i64_array: ArrayRef = Arc::new(Int64Array::from(i64_values));
6367
6368        let f64_expected = vec![
6369            -9223372036854776000.0,
6370            -2147483648.0,
6371            -32768.0,
6372            -128.0,
6373            0.0,
6374            127.0,
6375            32767.0,
6376            2147483647.0,
6377            9223372036854776000.0,
6378        ];
6379        assert_eq!(
6380            f64_expected,
6381            get_cast_values::<Float64Type>(&i64_array, &DataType::Float64)
6382                .iter()
6383                .map(|i| i.parse::<f64>().unwrap())
6384                .collect::<Vec<f64>>()
6385        );
6386
6387        let f32_expected = vec![
6388            -9223372000000000000.0,
6389            -2147483600.0,
6390            -32768.0,
6391            -128.0,
6392            0.0,
6393            127.0,
6394            32767.0,
6395            2147483600.0,
6396            9223372000000000000.0,
6397        ];
6398        assert_eq!(
6399            f32_expected,
6400            get_cast_values::<Float32Type>(&i64_array, &DataType::Float32)
6401                .iter()
6402                .map(|i| i.parse::<f32>().unwrap())
6403                .collect::<Vec<f32>>()
6404        );
6405
6406        let f16_expected = vec![
6407            f16::from_f64(-9223372000000000000.0),
6408            f16::from_f64(-2147483600.0),
6409            f16::from_f64(-32768.0),
6410            f16::from_f64(-128.0),
6411            f16::from_f64(0.0),
6412            f16::from_f64(127.0),
6413            f16::from_f64(32767.0),
6414            f16::from_f64(2147483600.0),
6415            f16::from_f64(9223372000000000000.0),
6416        ];
6417        assert_eq!(
6418            f16_expected,
6419            get_cast_values::<Float16Type>(&i64_array, &DataType::Float16)
6420                .iter()
6421                .map(|i| i.parse::<f16>().unwrap())
6422                .collect::<Vec<f16>>()
6423        );
6424
6425        let i64_expected = vec![
6426            "-9223372036854775808",
6427            "-2147483648",
6428            "-32768",
6429            "-128",
6430            "0",
6431            "127",
6432            "32767",
6433            "2147483647",
6434            "9223372036854775807",
6435        ];
6436        assert_eq!(
6437            i64_expected,
6438            get_cast_values::<Int64Type>(&i64_array, &DataType::Int64)
6439        );
6440
6441        let i32_expected = vec![
6442            "null",
6443            "-2147483648",
6444            "-32768",
6445            "-128",
6446            "0",
6447            "127",
6448            "32767",
6449            "2147483647",
6450            "null",
6451        ];
6452        assert_eq!(
6453            i32_expected,
6454            get_cast_values::<Int32Type>(&i64_array, &DataType::Int32)
6455        );
6456
6457        assert_eq!(
6458            i32_expected,
6459            get_cast_values::<Date32Type>(&i64_array, &DataType::Date32)
6460        );
6461
6462        let i16_expected = vec![
6463            "null", "null", "-32768", "-128", "0", "127", "32767", "null", "null",
6464        ];
6465        assert_eq!(
6466            i16_expected,
6467            get_cast_values::<Int16Type>(&i64_array, &DataType::Int16)
6468        );
6469
6470        let i8_expected = vec![
6471            "null", "null", "null", "-128", "0", "127", "null", "null", "null",
6472        ];
6473        assert_eq!(
6474            i8_expected,
6475            get_cast_values::<Int8Type>(&i64_array, &DataType::Int8)
6476        );
6477
6478        let u64_expected = vec![
6479            "null",
6480            "null",
6481            "null",
6482            "null",
6483            "0",
6484            "127",
6485            "32767",
6486            "2147483647",
6487            "9223372036854775807",
6488        ];
6489        assert_eq!(
6490            u64_expected,
6491            get_cast_values::<UInt64Type>(&i64_array, &DataType::UInt64)
6492        );
6493
6494        let u32_expected = vec![
6495            "null",
6496            "null",
6497            "null",
6498            "null",
6499            "0",
6500            "127",
6501            "32767",
6502            "2147483647",
6503            "null",
6504        ];
6505        assert_eq!(
6506            u32_expected,
6507            get_cast_values::<UInt32Type>(&i64_array, &DataType::UInt32)
6508        );
6509
6510        let u16_expected = vec![
6511            "null", "null", "null", "null", "0", "127", "32767", "null", "null",
6512        ];
6513        assert_eq!(
6514            u16_expected,
6515            get_cast_values::<UInt16Type>(&i64_array, &DataType::UInt16)
6516        );
6517
6518        let u8_expected = vec![
6519            "null", "null", "null", "null", "0", "127", "null", "null", "null",
6520        ];
6521        assert_eq!(
6522            u8_expected,
6523            get_cast_values::<UInt8Type>(&i64_array, &DataType::UInt8)
6524        );
6525    }
6526
6527    #[test]
6528    fn test_cast_from_int32() {
6529        let i32_values: Vec<i32> = vec![
6530            i32::MIN,
6531            i16::MIN as i32,
6532            i8::MIN as i32,
6533            0,
6534            i8::MAX as i32,
6535            i16::MAX as i32,
6536            i32::MAX,
6537        ];
6538        let i32_array: ArrayRef = Arc::new(Int32Array::from(i32_values));
6539
6540        let f64_expected = vec![
6541            "-2147483648.0",
6542            "-32768.0",
6543            "-128.0",
6544            "0.0",
6545            "127.0",
6546            "32767.0",
6547            "2147483647.0",
6548        ];
6549        assert_eq!(
6550            f64_expected,
6551            get_cast_values::<Float64Type>(&i32_array, &DataType::Float64)
6552        );
6553
6554        let f32_expected = vec![
6555            "-2147483600.0",
6556            "-32768.0",
6557            "-128.0",
6558            "0.0",
6559            "127.0",
6560            "32767.0",
6561            "2147483600.0",
6562        ];
6563        assert_eq!(
6564            f32_expected,
6565            get_cast_values::<Float32Type>(&i32_array, &DataType::Float32)
6566        );
6567
6568        let f16_expected = vec![
6569            f16::from_f64(-2147483600.0),
6570            f16::from_f64(-32768.0),
6571            f16::from_f64(-128.0),
6572            f16::from_f64(0.0),
6573            f16::from_f64(127.0),
6574            f16::from_f64(32767.0),
6575            f16::from_f64(2147483600.0),
6576        ];
6577        assert_eq!(
6578            f16_expected,
6579            get_cast_values::<Float16Type>(&i32_array, &DataType::Float16)
6580                .iter()
6581                .map(|i| i.parse::<f16>().unwrap())
6582                .collect::<Vec<f16>>()
6583        );
6584
6585        let i16_expected = vec!["null", "-32768", "-128", "0", "127", "32767", "null"];
6586        assert_eq!(
6587            i16_expected,
6588            get_cast_values::<Int16Type>(&i32_array, &DataType::Int16)
6589        );
6590
6591        let i8_expected = vec!["null", "null", "-128", "0", "127", "null", "null"];
6592        assert_eq!(
6593            i8_expected,
6594            get_cast_values::<Int8Type>(&i32_array, &DataType::Int8)
6595        );
6596
6597        let u64_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
6598        assert_eq!(
6599            u64_expected,
6600            get_cast_values::<UInt64Type>(&i32_array, &DataType::UInt64)
6601        );
6602
6603        let u32_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
6604        assert_eq!(
6605            u32_expected,
6606            get_cast_values::<UInt32Type>(&i32_array, &DataType::UInt32)
6607        );
6608
6609        let u16_expected = vec!["null", "null", "null", "0", "127", "32767", "null"];
6610        assert_eq!(
6611            u16_expected,
6612            get_cast_values::<UInt16Type>(&i32_array, &DataType::UInt16)
6613        );
6614
6615        let u8_expected = vec!["null", "null", "null", "0", "127", "null", "null"];
6616        assert_eq!(
6617            u8_expected,
6618            get_cast_values::<UInt8Type>(&i32_array, &DataType::UInt8)
6619        );
6620
6621        // The date32 to date64 cast increases the numerical values in order to keep the same dates.
6622        let i64_expected = vec![
6623            "-185542587187200000",
6624            "-2831155200000",
6625            "-11059200000",
6626            "0",
6627            "10972800000",
6628            "2831068800000",
6629            "185542587100800000",
6630        ];
6631        assert_eq!(
6632            i64_expected,
6633            get_cast_values::<Date64Type>(&i32_array, &DataType::Date64)
6634        );
6635    }
6636
6637    #[test]
6638    fn test_cast_from_int16() {
6639        let i16_values: Vec<i16> = vec![i16::MIN, i8::MIN as i16, 0, i8::MAX as i16, i16::MAX];
6640        let i16_array: ArrayRef = Arc::new(Int16Array::from(i16_values));
6641
6642        let f64_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
6643        assert_eq!(
6644            f64_expected,
6645            get_cast_values::<Float64Type>(&i16_array, &DataType::Float64)
6646        );
6647
6648        let f32_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
6649        assert_eq!(
6650            f32_expected,
6651            get_cast_values::<Float32Type>(&i16_array, &DataType::Float32)
6652        );
6653
6654        let f16_expected = vec![
6655            f16::from_f64(-32768.0),
6656            f16::from_f64(-128.0),
6657            f16::from_f64(0.0),
6658            f16::from_f64(127.0),
6659            f16::from_f64(32767.0),
6660        ];
6661        assert_eq!(
6662            f16_expected,
6663            get_cast_values::<Float16Type>(&i16_array, &DataType::Float16)
6664                .iter()
6665                .map(|i| i.parse::<f16>().unwrap())
6666                .collect::<Vec<f16>>()
6667        );
6668
6669        let i64_expected = vec!["-32768", "-128", "0", "127", "32767"];
6670        assert_eq!(
6671            i64_expected,
6672            get_cast_values::<Int64Type>(&i16_array, &DataType::Int64)
6673        );
6674
6675        let i32_expected = vec!["-32768", "-128", "0", "127", "32767"];
6676        assert_eq!(
6677            i32_expected,
6678            get_cast_values::<Int32Type>(&i16_array, &DataType::Int32)
6679        );
6680
6681        let i16_expected = vec!["-32768", "-128", "0", "127", "32767"];
6682        assert_eq!(
6683            i16_expected,
6684            get_cast_values::<Int16Type>(&i16_array, &DataType::Int16)
6685        );
6686
6687        let i8_expected = vec!["null", "-128", "0", "127", "null"];
6688        assert_eq!(
6689            i8_expected,
6690            get_cast_values::<Int8Type>(&i16_array, &DataType::Int8)
6691        );
6692
6693        let u64_expected = vec!["null", "null", "0", "127", "32767"];
6694        assert_eq!(
6695            u64_expected,
6696            get_cast_values::<UInt64Type>(&i16_array, &DataType::UInt64)
6697        );
6698
6699        let u32_expected = vec!["null", "null", "0", "127", "32767"];
6700        assert_eq!(
6701            u32_expected,
6702            get_cast_values::<UInt32Type>(&i16_array, &DataType::UInt32)
6703        );
6704
6705        let u16_expected = vec!["null", "null", "0", "127", "32767"];
6706        assert_eq!(
6707            u16_expected,
6708            get_cast_values::<UInt16Type>(&i16_array, &DataType::UInt16)
6709        );
6710
6711        let u8_expected = vec!["null", "null", "0", "127", "null"];
6712        assert_eq!(
6713            u8_expected,
6714            get_cast_values::<UInt8Type>(&i16_array, &DataType::UInt8)
6715        );
6716    }
6717
6718    #[test]
6719    fn test_cast_from_date32() {
6720        let i32_values: Vec<i32> = vec![
6721            i32::MIN,
6722            i16::MIN as i32,
6723            i8::MIN as i32,
6724            0,
6725            i8::MAX as i32,
6726            i16::MAX as i32,
6727            i32::MAX,
6728        ];
6729        let date32_array: ArrayRef = Arc::new(Date32Array::from(i32_values));
6730
6731        let i64_expected = vec![
6732            "-2147483648",
6733            "-32768",
6734            "-128",
6735            "0",
6736            "127",
6737            "32767",
6738            "2147483647",
6739        ];
6740        assert_eq!(
6741            i64_expected,
6742            get_cast_values::<Int64Type>(&date32_array, &DataType::Int64)
6743        );
6744    }
6745
6746    #[test]
6747    fn test_cast_from_int8() {
6748        let i8_values: Vec<i8> = vec![i8::MIN, 0, i8::MAX];
6749        let i8_array = Int8Array::from(i8_values);
6750
6751        let f64_expected = vec!["-128.0", "0.0", "127.0"];
6752        assert_eq!(
6753            f64_expected,
6754            get_cast_values::<Float64Type>(&i8_array, &DataType::Float64)
6755        );
6756
6757        let f32_expected = vec!["-128.0", "0.0", "127.0"];
6758        assert_eq!(
6759            f32_expected,
6760            get_cast_values::<Float32Type>(&i8_array, &DataType::Float32)
6761        );
6762
6763        let f16_expected = vec!["-128.0", "0.0", "127.0"];
6764        assert_eq!(
6765            f16_expected,
6766            get_cast_values::<Float16Type>(&i8_array, &DataType::Float16)
6767        );
6768
6769        let i64_expected = vec!["-128", "0", "127"];
6770        assert_eq!(
6771            i64_expected,
6772            get_cast_values::<Int64Type>(&i8_array, &DataType::Int64)
6773        );
6774
6775        let i32_expected = vec!["-128", "0", "127"];
6776        assert_eq!(
6777            i32_expected,
6778            get_cast_values::<Int32Type>(&i8_array, &DataType::Int32)
6779        );
6780
6781        let i16_expected = vec!["-128", "0", "127"];
6782        assert_eq!(
6783            i16_expected,
6784            get_cast_values::<Int16Type>(&i8_array, &DataType::Int16)
6785        );
6786
6787        let i8_expected = vec!["-128", "0", "127"];
6788        assert_eq!(
6789            i8_expected,
6790            get_cast_values::<Int8Type>(&i8_array, &DataType::Int8)
6791        );
6792
6793        let u64_expected = vec!["null", "0", "127"];
6794        assert_eq!(
6795            u64_expected,
6796            get_cast_values::<UInt64Type>(&i8_array, &DataType::UInt64)
6797        );
6798
6799        let u32_expected = vec!["null", "0", "127"];
6800        assert_eq!(
6801            u32_expected,
6802            get_cast_values::<UInt32Type>(&i8_array, &DataType::UInt32)
6803        );
6804
6805        let u16_expected = vec!["null", "0", "127"];
6806        assert_eq!(
6807            u16_expected,
6808            get_cast_values::<UInt16Type>(&i8_array, &DataType::UInt16)
6809        );
6810
6811        let u8_expected = vec!["null", "0", "127"];
6812        assert_eq!(
6813            u8_expected,
6814            get_cast_values::<UInt8Type>(&i8_array, &DataType::UInt8)
6815        );
6816    }
6817
6818    /// Convert `array` into a vector of strings by casting to data type dt
6819    fn get_cast_values<T>(array: &dyn Array, dt: &DataType) -> Vec<String>
6820    where
6821        T: ArrowPrimitiveType,
6822    {
6823        let c = cast(array, dt).unwrap();
6824        let a = c.as_primitive::<T>();
6825        let mut v: Vec<String> = vec![];
6826        for i in 0..array.len() {
6827            if a.is_null(i) {
6828                v.push("null".to_string())
6829            } else {
6830                v.push(format!("{:?}", a.value(i)));
6831            }
6832        }
6833        v
6834    }
6835
6836    #[test]
6837    fn test_cast_utf8_dict() {
6838        // FROM a dictionary with of Utf8 values
6839        use DataType::*;
6840
6841        let mut builder = StringDictionaryBuilder::<Int8Type>::new();
6842        builder.append("one").unwrap();
6843        builder.append_null();
6844        builder.append("three").unwrap();
6845        let array: ArrayRef = Arc::new(builder.finish());
6846
6847        let expected = vec!["one", "null", "three"];
6848
6849        // Test casting TO StringArray
6850        let cast_type = Utf8;
6851        let cast_array = cast(&array, &cast_type).expect("cast to UTF-8 failed");
6852        assert_eq!(cast_array.data_type(), &cast_type);
6853        assert_eq!(array_to_strings(&cast_array), expected);
6854
6855        // Test casting TO Dictionary (with different index sizes)
6856
6857        let cast_type = Dictionary(Box::new(Int16), Box::new(Utf8));
6858        let cast_array = cast(&array, &cast_type).expect("cast failed");
6859        assert_eq!(cast_array.data_type(), &cast_type);
6860        assert_eq!(array_to_strings(&cast_array), expected);
6861
6862        let cast_type = Dictionary(Box::new(Int32), Box::new(Utf8));
6863        let cast_array = cast(&array, &cast_type).expect("cast failed");
6864        assert_eq!(cast_array.data_type(), &cast_type);
6865        assert_eq!(array_to_strings(&cast_array), expected);
6866
6867        let cast_type = Dictionary(Box::new(Int64), Box::new(Utf8));
6868        let cast_array = cast(&array, &cast_type).expect("cast failed");
6869        assert_eq!(cast_array.data_type(), &cast_type);
6870        assert_eq!(array_to_strings(&cast_array), expected);
6871
6872        let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
6873        let cast_array = cast(&array, &cast_type).expect("cast failed");
6874        assert_eq!(cast_array.data_type(), &cast_type);
6875        assert_eq!(array_to_strings(&cast_array), expected);
6876
6877        let cast_type = Dictionary(Box::new(UInt16), Box::new(Utf8));
6878        let cast_array = cast(&array, &cast_type).expect("cast failed");
6879        assert_eq!(cast_array.data_type(), &cast_type);
6880        assert_eq!(array_to_strings(&cast_array), expected);
6881
6882        let cast_type = Dictionary(Box::new(UInt32), Box::new(Utf8));
6883        let cast_array = cast(&array, &cast_type).expect("cast failed");
6884        assert_eq!(cast_array.data_type(), &cast_type);
6885        assert_eq!(array_to_strings(&cast_array), expected);
6886
6887        let cast_type = Dictionary(Box::new(UInt64), Box::new(Utf8));
6888        let cast_array = cast(&array, &cast_type).expect("cast failed");
6889        assert_eq!(cast_array.data_type(), &cast_type);
6890        assert_eq!(array_to_strings(&cast_array), expected);
6891    }
6892
6893    #[test]
6894    fn test_cast_dict_to_dict_bad_index_value_primitive() {
6895        use DataType::*;
6896        // test converting from an array that has indexes of a type
6897        // that are out of bounds for a particular other kind of
6898        // index.
6899
6900        let mut builder = PrimitiveDictionaryBuilder::<Int32Type, Int64Type>::new();
6901
6902        // add 200 distinct values (which can be stored by a
6903        // dictionary indexed by int32, but not a dictionary indexed
6904        // with int8)
6905        for i in 0..200 {
6906            builder.append(i).unwrap();
6907        }
6908        let array: ArrayRef = Arc::new(builder.finish());
6909
6910        let cast_type = Dictionary(Box::new(Int8), Box::new(Utf8));
6911        let res = cast(&array, &cast_type);
6912        assert!(res.is_err());
6913        let actual_error = format!("{res:?}");
6914        let expected_error = "Could not convert 72 dictionary indexes from Int32 to Int8";
6915        assert!(
6916            actual_error.contains(expected_error),
6917            "did not find expected error '{actual_error}' in actual error '{expected_error}'"
6918        );
6919    }
6920
6921    #[test]
6922    fn test_cast_dict_to_dict_bad_index_value_utf8() {
6923        use DataType::*;
6924        // Same test as test_cast_dict_to_dict_bad_index_value but use
6925        // string values (and encode the expected behavior here);
6926
6927        let mut builder = StringDictionaryBuilder::<Int32Type>::new();
6928
6929        // add 200 distinct values (which can be stored by a
6930        // dictionary indexed by int32, but not a dictionary indexed
6931        // with int8)
6932        for i in 0..200 {
6933            let val = format!("val{i}");
6934            builder.append(&val).unwrap();
6935        }
6936        let array = builder.finish();
6937
6938        let cast_type = Dictionary(Box::new(Int8), Box::new(Utf8));
6939        let res = cast(&array, &cast_type);
6940        assert!(res.is_err());
6941        let actual_error = format!("{res:?}");
6942        let expected_error = "Could not convert 72 dictionary indexes from Int32 to Int8";
6943        assert!(
6944            actual_error.contains(expected_error),
6945            "did not find expected error '{actual_error}' in actual error '{expected_error}'"
6946        );
6947    }
6948
6949    #[test]
6950    fn test_cast_primitive_dict() {
6951        // FROM a dictionary with of INT32 values
6952        use DataType::*;
6953
6954        let mut builder = PrimitiveDictionaryBuilder::<Int8Type, Int32Type>::new();
6955        builder.append(1).unwrap();
6956        builder.append_null();
6957        builder.append(3).unwrap();
6958        let array: ArrayRef = Arc::new(builder.finish());
6959
6960        let expected = vec!["1", "null", "3"];
6961
6962        // Test casting TO PrimitiveArray, different dictionary type
6963        let cast_array = cast(&array, &Utf8).expect("cast to UTF-8 failed");
6964        assert_eq!(array_to_strings(&cast_array), expected);
6965        assert_eq!(cast_array.data_type(), &Utf8);
6966
6967        let cast_array = cast(&array, &Int64).expect("cast to int64 failed");
6968        assert_eq!(array_to_strings(&cast_array), expected);
6969        assert_eq!(cast_array.data_type(), &Int64);
6970    }
6971
6972    #[test]
6973    fn test_cast_primitive_array_to_dict() {
6974        use DataType::*;
6975
6976        let mut builder = PrimitiveBuilder::<Int32Type>::new();
6977        builder.append_value(1);
6978        builder.append_null();
6979        builder.append_value(3);
6980        let array: ArrayRef = Arc::new(builder.finish());
6981
6982        let expected = vec!["1", "null", "3"];
6983
6984        // Cast to a dictionary (same value type, Int32)
6985        let cast_type = Dictionary(Box::new(UInt8), Box::new(Int32));
6986        let cast_array = cast(&array, &cast_type).expect("cast failed");
6987        assert_eq!(cast_array.data_type(), &cast_type);
6988        assert_eq!(array_to_strings(&cast_array), expected);
6989
6990        // Cast to a dictionary (different value type, Int8)
6991        let cast_type = Dictionary(Box::new(UInt8), Box::new(Int8));
6992        let cast_array = cast(&array, &cast_type).expect("cast failed");
6993        assert_eq!(cast_array.data_type(), &cast_type);
6994        assert_eq!(array_to_strings(&cast_array), expected);
6995    }
6996
6997    #[test]
6998    fn test_cast_time_array_to_dict() {
6999        use DataType::*;
7000
7001        let array = Arc::new(Date32Array::from(vec![Some(1000), None, Some(2000)])) as ArrayRef;
7002
7003        let expected = vec!["1972-09-27", "null", "1975-06-24"];
7004
7005        let cast_type = Dictionary(Box::new(UInt8), Box::new(Date32));
7006        let cast_array = cast(&array, &cast_type).expect("cast failed");
7007        assert_eq!(cast_array.data_type(), &cast_type);
7008        assert_eq!(array_to_strings(&cast_array), expected);
7009    }
7010
7011    #[test]
7012    fn test_cast_timestamp_array_to_dict() {
7013        use DataType::*;
7014
7015        let array = Arc::new(
7016            TimestampSecondArray::from(vec![Some(1000), None, Some(2000)]).with_timezone_utc(),
7017        ) as ArrayRef;
7018
7019        let expected = vec!["1970-01-01T00:16:40", "null", "1970-01-01T00:33:20"];
7020
7021        let cast_type = Dictionary(Box::new(UInt8), Box::new(Timestamp(TimeUnit::Second, None)));
7022        let cast_array = cast(&array, &cast_type).expect("cast failed");
7023        assert_eq!(cast_array.data_type(), &cast_type);
7024        assert_eq!(array_to_strings(&cast_array), expected);
7025    }
7026
7027    #[test]
7028    fn test_cast_string_array_to_dict() {
7029        use DataType::*;
7030
7031        let array = Arc::new(StringArray::from(vec![Some("one"), None, Some("three")])) as ArrayRef;
7032
7033        let expected = vec!["one", "null", "three"];
7034
7035        // Cast to a dictionary (same value type, Utf8)
7036        let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
7037        let cast_array = cast(&array, &cast_type).expect("cast failed");
7038        assert_eq!(cast_array.data_type(), &cast_type);
7039        assert_eq!(array_to_strings(&cast_array), expected);
7040    }
7041
7042    #[test]
7043    fn test_cast_null_array_to_from_decimal_array() {
7044        let data_type = DataType::Decimal128(12, 4);
7045        let array = new_null_array(&DataType::Null, 4);
7046        assert_eq!(array.data_type(), &DataType::Null);
7047        let cast_array = cast(&array, &data_type).expect("cast failed");
7048        assert_eq!(cast_array.data_type(), &data_type);
7049        for i in 0..4 {
7050            assert!(cast_array.is_null(i));
7051        }
7052
7053        let array = new_null_array(&data_type, 4);
7054        assert_eq!(array.data_type(), &data_type);
7055        let cast_array = cast(&array, &DataType::Null).expect("cast failed");
7056        assert_eq!(cast_array.data_type(), &DataType::Null);
7057        assert_eq!(cast_array.len(), 4);
7058        assert_eq!(cast_array.logical_nulls().unwrap().null_count(), 4);
7059    }
7060
7061    #[test]
7062    fn test_cast_null_array_from_and_to_primitive_array() {
7063        macro_rules! typed_test {
7064            ($ARR_TYPE:ident, $DATATYPE:ident, $TYPE:tt) => {{
7065                {
7066                    let array = Arc::new(NullArray::new(6)) as ArrayRef;
7067                    let expected = $ARR_TYPE::from(vec![None; 6]);
7068                    let cast_type = DataType::$DATATYPE;
7069                    let cast_array = cast(&array, &cast_type).expect("cast failed");
7070                    let cast_array = cast_array.as_primitive::<$TYPE>();
7071                    assert_eq!(cast_array.data_type(), &cast_type);
7072                    assert_eq!(cast_array, &expected);
7073                }
7074            }};
7075        }
7076
7077        typed_test!(Int16Array, Int16, Int16Type);
7078        typed_test!(Int32Array, Int32, Int32Type);
7079        typed_test!(Int64Array, Int64, Int64Type);
7080
7081        typed_test!(UInt16Array, UInt16, UInt16Type);
7082        typed_test!(UInt32Array, UInt32, UInt32Type);
7083        typed_test!(UInt64Array, UInt64, UInt64Type);
7084
7085        typed_test!(Float32Array, Float32, Float32Type);
7086        typed_test!(Float64Array, Float64, Float64Type);
7087
7088        typed_test!(Date32Array, Date32, Date32Type);
7089        typed_test!(Date64Array, Date64, Date64Type);
7090    }
7091
7092    fn cast_from_null_to_other(data_type: &DataType) {
7093        // Cast from null to data_type
7094        {
7095            let array = new_null_array(&DataType::Null, 4);
7096            assert_eq!(array.data_type(), &DataType::Null);
7097            let cast_array = cast(&array, data_type).expect("cast failed");
7098            assert_eq!(cast_array.data_type(), data_type);
7099            for i in 0..4 {
7100                assert!(cast_array.is_null(i));
7101            }
7102        }
7103    }
7104
7105    #[test]
7106    fn test_cast_null_from_and_to_variable_sized() {
7107        cast_from_null_to_other(&DataType::Utf8);
7108        cast_from_null_to_other(&DataType::LargeUtf8);
7109        cast_from_null_to_other(&DataType::Binary);
7110        cast_from_null_to_other(&DataType::LargeBinary);
7111    }
7112
7113    #[test]
7114    fn test_cast_null_from_and_to_nested_type() {
7115        // Cast null from and to map
7116        let data_type = DataType::Map(
7117            Arc::new(Field::new_struct(
7118                "entry",
7119                vec![
7120                    Field::new("key", DataType::Utf8, false),
7121                    Field::new("value", DataType::Int32, true),
7122                ],
7123                false,
7124            )),
7125            false,
7126        );
7127        cast_from_null_to_other(&data_type);
7128
7129        // Cast null from and to list
7130        let data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
7131        cast_from_null_to_other(&data_type);
7132        let data_type = DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
7133        cast_from_null_to_other(&data_type);
7134        let data_type =
7135            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 4);
7136        cast_from_null_to_other(&data_type);
7137
7138        // Cast null from and to dictionary
7139        let values = vec![None, None, None, None] as Vec<Option<&str>>;
7140        let array: DictionaryArray<Int8Type> = values.into_iter().collect();
7141        let array = Arc::new(array) as ArrayRef;
7142        let data_type = array.data_type().to_owned();
7143        cast_from_null_to_other(&data_type);
7144
7145        // Cast null from and to struct
7146        let data_type = DataType::Struct(vec![Field::new("data", DataType::Int64, false)].into());
7147        cast_from_null_to_other(&data_type);
7148    }
7149
7150    /// Print the `DictionaryArray` `array` as a vector of strings
7151    fn array_to_strings(array: &ArrayRef) -> Vec<String> {
7152        let options = FormatOptions::new().with_null("null");
7153        let formatter = ArrayFormatter::try_new(array.as_ref(), &options).unwrap();
7154        (0..array.len())
7155            .map(|i| formatter.value(i).to_string())
7156            .collect()
7157    }
7158
7159    #[test]
7160    fn test_cast_utf8_to_date32() {
7161        use chrono::NaiveDate;
7162        let from_ymd = chrono::NaiveDate::from_ymd_opt;
7163        let since = chrono::NaiveDate::signed_duration_since;
7164
7165        let a = StringArray::from(vec![
7166            "2000-01-01",          // valid date with leading 0s
7167            "2000-01-01T12:00:00", // valid datetime, will throw away the time part
7168            "2000-2-2",            // valid date without leading 0s
7169            "2000-00-00",          // invalid month and day
7170            "2000",                // just a year is invalid
7171        ]);
7172        let array = Arc::new(a) as ArrayRef;
7173        let b = cast(&array, &DataType::Date32).unwrap();
7174        let c = b.as_primitive::<Date32Type>();
7175
7176        // test valid inputs
7177        let date_value = since(
7178            NaiveDate::from_ymd_opt(2000, 1, 1).unwrap(),
7179            from_ymd(1970, 1, 1).unwrap(),
7180        )
7181        .num_days() as i32;
7182        assert!(c.is_valid(0)); // "2000-01-01"
7183        assert_eq!(date_value, c.value(0));
7184
7185        assert!(c.is_valid(1)); // "2000-01-01T12:00:00"
7186        assert_eq!(date_value, c.value(1));
7187
7188        let date_value = since(
7189            NaiveDate::from_ymd_opt(2000, 2, 2).unwrap(),
7190            from_ymd(1970, 1, 1).unwrap(),
7191        )
7192        .num_days() as i32;
7193        assert!(c.is_valid(2)); // "2000-2-2"
7194        assert_eq!(date_value, c.value(2));
7195
7196        // test invalid inputs
7197        assert!(!c.is_valid(3)); // "2000-00-00"
7198        assert!(!c.is_valid(4)); // "2000"
7199    }
7200
7201    #[test]
7202    fn test_cast_utf8_to_date64() {
7203        let a = StringArray::from(vec![
7204            "2000-01-01T12:00:00", // date + time valid
7205            "2020-12-15T12:34:56", // date + time valid
7206            "2020-2-2T12:34:56",   // valid date time without leading 0s
7207            "2000-00-00T12:00:00", // invalid month and day
7208            "2000-01-01 12:00:00", // missing the 'T'
7209            "2000-01-01",          // just a date is invalid
7210        ]);
7211        let array = Arc::new(a) as ArrayRef;
7212        let b = cast(&array, &DataType::Date64).unwrap();
7213        let c = b.as_primitive::<Date64Type>();
7214
7215        // test valid inputs
7216        assert!(c.is_valid(0)); // "2000-01-01T12:00:00"
7217        assert_eq!(946728000000, c.value(0));
7218        assert!(c.is_valid(1)); // "2020-12-15T12:34:56"
7219        assert_eq!(1608035696000, c.value(1));
7220        assert!(!c.is_valid(2)); // "2020-2-2T12:34:56"
7221
7222        assert!(!c.is_valid(3)); // "2000-00-00T12:00:00"
7223        assert!(c.is_valid(4)); // "2000-01-01 12:00:00"
7224        assert_eq!(946728000000, c.value(4));
7225        assert!(c.is_valid(5)); // "2000-01-01"
7226        assert_eq!(946684800000, c.value(5));
7227    }
7228
7229    #[test]
7230    fn test_can_cast_fsl_to_fsl() {
7231        let from_array = Arc::new(
7232            FixedSizeListArray::from_iter_primitive::<Float32Type, _, _>(
7233                [Some([Some(1.0), Some(2.0)]), None],
7234                2,
7235            ),
7236        ) as ArrayRef;
7237        let to_array = Arc::new(
7238            FixedSizeListArray::from_iter_primitive::<Float16Type, _, _>(
7239                [
7240                    Some([Some(f16::from_f32(1.0)), Some(f16::from_f32(2.0))]),
7241                    None,
7242                ],
7243                2,
7244            ),
7245        ) as ArrayRef;
7246
7247        assert!(can_cast_types(from_array.data_type(), to_array.data_type()));
7248        let actual = cast(&from_array, to_array.data_type()).unwrap();
7249        assert_eq!(actual.data_type(), to_array.data_type());
7250
7251        let invalid_target =
7252            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Binary, true)), 2);
7253        assert!(!can_cast_types(from_array.data_type(), &invalid_target));
7254
7255        let invalid_size =
7256            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Float16, true)), 5);
7257        assert!(!can_cast_types(from_array.data_type(), &invalid_size));
7258    }
7259
7260    #[test]
7261    fn test_can_cast_types_fixed_size_list_to_list() {
7262        // DataType::List
7263        let array1 = Arc::new(make_fixed_size_list_array()) as ArrayRef;
7264        assert!(can_cast_types(
7265            array1.data_type(),
7266            &DataType::List(Arc::new(Field::new("", DataType::Int32, false)))
7267        ));
7268
7269        // DataType::LargeList
7270        let array2 = Arc::new(make_fixed_size_list_array_for_large_list()) as ArrayRef;
7271        assert!(can_cast_types(
7272            array2.data_type(),
7273            &DataType::LargeList(Arc::new(Field::new("", DataType::Int64, false)))
7274        ));
7275    }
7276
7277    #[test]
7278    fn test_cast_fixed_size_list_to_list() {
7279        // Important cases:
7280        // 1. With/without nulls
7281        // 2. LargeList and List
7282        // 3. With and without inner casts
7283
7284        let cases = [
7285            // fixed_size_list<i32, 2> => list<i32>
7286            (
7287                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
7288                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
7289                    2,
7290                )) as ArrayRef,
7291                Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>([
7292                    Some([Some(1), Some(1)]),
7293                    Some([Some(2), Some(2)]),
7294                ])) as ArrayRef,
7295            ),
7296            // fixed_size_list<i32, 2> => list<i32> (nullable)
7297            (
7298                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
7299                    [None, Some([Some(2), Some(2)])],
7300                    2,
7301                )) as ArrayRef,
7302                Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>([
7303                    None,
7304                    Some([Some(2), Some(2)]),
7305                ])) as ArrayRef,
7306            ),
7307            // fixed_size_list<i32, 2> => large_list<i64>
7308            (
7309                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
7310                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
7311                    2,
7312                )) as ArrayRef,
7313                Arc::new(LargeListArray::from_iter_primitive::<Int64Type, _, _>([
7314                    Some([Some(1), Some(1)]),
7315                    Some([Some(2), Some(2)]),
7316                ])) as ArrayRef,
7317            ),
7318            // fixed_size_list<i32, 2> => large_list<i64> (nullable)
7319            (
7320                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
7321                    [None, Some([Some(2), Some(2)])],
7322                    2,
7323                )) as ArrayRef,
7324                Arc::new(LargeListArray::from_iter_primitive::<Int64Type, _, _>([
7325                    None,
7326                    Some([Some(2), Some(2)]),
7327                ])) as ArrayRef,
7328            ),
7329        ];
7330
7331        for (array, expected) in cases {
7332            let array = Arc::new(array) as ArrayRef;
7333
7334            assert!(
7335                can_cast_types(array.data_type(), expected.data_type()),
7336                "can_cast_types claims we cannot cast {:?} to {:?}",
7337                array.data_type(),
7338                expected.data_type()
7339            );
7340
7341            let list_array = cast(&array, expected.data_type())
7342                .unwrap_or_else(|_| panic!("Failed to cast {:?} to {:?}", array, expected));
7343            assert_eq!(
7344                list_array.as_ref(),
7345                &expected,
7346                "Incorrect result from casting {:?} to {:?}",
7347                array,
7348                expected
7349            );
7350        }
7351    }
7352
7353    #[test]
7354    fn test_cast_utf8_to_list() {
7355        // DataType::List
7356        let array = Arc::new(StringArray::from(vec!["5"])) as ArrayRef;
7357        let field = Arc::new(Field::new("", DataType::Int32, false));
7358        let list_array = cast(&array, &DataType::List(field.clone())).unwrap();
7359        let actual = list_array.as_list_opt::<i32>().unwrap();
7360        let expect = ListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
7361        assert_eq!(&expect.value(0), &actual.value(0));
7362
7363        // DataType::LargeList
7364        let list_array = cast(&array, &DataType::LargeList(field.clone())).unwrap();
7365        let actual = list_array.as_list_opt::<i64>().unwrap();
7366        let expect = LargeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
7367        assert_eq!(&expect.value(0), &actual.value(0));
7368
7369        // DataType::FixedSizeList
7370        let list_array = cast(&array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
7371        let actual = list_array.as_fixed_size_list_opt().unwrap();
7372        let expect =
7373            FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])], 1);
7374        assert_eq!(&expect.value(0), &actual.value(0));
7375    }
7376
7377    #[test]
7378    fn test_cast_single_element_fixed_size_list() {
7379        // FixedSizeList<T>[1] => T
7380        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
7381            [(Some([Some(5)]))],
7382            1,
7383        )) as ArrayRef;
7384        let casted_array = cast(&from_array, &DataType::Int32).unwrap();
7385        let actual: &Int32Array = casted_array.as_primitive();
7386        let expected = Int32Array::from(vec![Some(5)]);
7387        assert_eq!(&expected, actual);
7388
7389        // FixedSizeList<T>[1] => FixedSizeList<U>[1]
7390        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
7391            [(Some([Some(5)]))],
7392            1,
7393        )) as ArrayRef;
7394        let to_field = Arc::new(Field::new("dummy", DataType::Float32, false));
7395        let actual = cast(&from_array, &DataType::FixedSizeList(to_field.clone(), 1)).unwrap();
7396        let expected = Arc::new(FixedSizeListArray::new(
7397            to_field.clone(),
7398            1,
7399            Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
7400            None,
7401        )) as ArrayRef;
7402        assert_eq!(*expected, *actual);
7403
7404        // FixedSizeList<T>[1] => FixedSizeList<FixdSizedList<U>[1]>[1]
7405        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
7406            [(Some([Some(5)]))],
7407            1,
7408        )) as ArrayRef;
7409        let to_field_inner = Arc::new(Field::new_list_field(DataType::Float32, false));
7410        let to_field = Arc::new(Field::new(
7411            "dummy",
7412            DataType::FixedSizeList(to_field_inner.clone(), 1),
7413            false,
7414        ));
7415        let actual = cast(&from_array, &DataType::FixedSizeList(to_field.clone(), 1)).unwrap();
7416        let expected = Arc::new(FixedSizeListArray::new(
7417            to_field.clone(),
7418            1,
7419            Arc::new(FixedSizeListArray::new(
7420                to_field_inner.clone(),
7421                1,
7422                Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
7423                None,
7424            )) as ArrayRef,
7425            None,
7426        )) as ArrayRef;
7427        assert_eq!(*expected, *actual);
7428
7429        // T => FixedSizeList<T>[1] (non-nullable)
7430        let field = Arc::new(Field::new("dummy", DataType::Float32, false));
7431        let from_array = Arc::new(Int8Array::from(vec![Some(5)])) as ArrayRef;
7432        let casted_array = cast(&from_array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
7433        let actual = casted_array.as_fixed_size_list();
7434        let expected = Arc::new(FixedSizeListArray::new(
7435            field.clone(),
7436            1,
7437            Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
7438            None,
7439        )) as ArrayRef;
7440        assert_eq!(expected.as_ref(), actual);
7441
7442        // T => FixedSizeList<T>[1] (nullable)
7443        let field = Arc::new(Field::new("nullable", DataType::Float32, true));
7444        let from_array = Arc::new(Int8Array::from(vec![None])) as ArrayRef;
7445        let casted_array = cast(&from_array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
7446        let actual = casted_array.as_fixed_size_list();
7447        let expected = Arc::new(FixedSizeListArray::new(
7448            field.clone(),
7449            1,
7450            Arc::new(Float32Array::from(vec![None])) as ArrayRef,
7451            None,
7452        )) as ArrayRef;
7453        assert_eq!(expected.as_ref(), actual);
7454    }
7455
7456    #[test]
7457    fn test_cast_list_containers() {
7458        // large-list to list
7459        let array = Arc::new(make_large_list_array()) as ArrayRef;
7460        let list_array = cast(
7461            &array,
7462            &DataType::List(Arc::new(Field::new("", DataType::Int32, false))),
7463        )
7464        .unwrap();
7465        let actual = list_array.as_any().downcast_ref::<ListArray>().unwrap();
7466        let expected = array.as_any().downcast_ref::<LargeListArray>().unwrap();
7467
7468        assert_eq!(&expected.value(0), &actual.value(0));
7469        assert_eq!(&expected.value(1), &actual.value(1));
7470        assert_eq!(&expected.value(2), &actual.value(2));
7471
7472        // list to large-list
7473        let array = Arc::new(make_list_array()) as ArrayRef;
7474        let large_list_array = cast(
7475            &array,
7476            &DataType::LargeList(Arc::new(Field::new("", DataType::Int32, false))),
7477        )
7478        .unwrap();
7479        let actual = large_list_array
7480            .as_any()
7481            .downcast_ref::<LargeListArray>()
7482            .unwrap();
7483        let expected = array.as_any().downcast_ref::<ListArray>().unwrap();
7484
7485        assert_eq!(&expected.value(0), &actual.value(0));
7486        assert_eq!(&expected.value(1), &actual.value(1));
7487        assert_eq!(&expected.value(2), &actual.value(2));
7488    }
7489
7490    #[test]
7491    fn test_cast_list_to_fsl() {
7492        // There four noteworthy cases we should handle:
7493        // 1. No nulls
7494        // 2. Nulls that are always empty
7495        // 3. Nulls that have varying lengths
7496        // 4. Nulls that are correctly sized (same as target list size)
7497
7498        // Non-null case
7499        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
7500        let values = vec![
7501            Some(vec![Some(1), Some(2), Some(3)]),
7502            Some(vec![Some(4), Some(5), Some(6)]),
7503        ];
7504        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(
7505            values.clone(),
7506        )) as ArrayRef;
7507        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
7508            values, 3,
7509        )) as ArrayRef;
7510        let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
7511        assert_eq!(expected.as_ref(), actual.as_ref());
7512
7513        // Null cases
7514        // Array is [[1, 2, 3], null, [4, 5, 6], null]
7515        let cases = [
7516            (
7517                // Zero-length nulls
7518                vec![1, 2, 3, 4, 5, 6],
7519                vec![3, 0, 3, 0],
7520            ),
7521            (
7522                // Varying-length nulls
7523                vec![1, 2, 3, 0, 0, 4, 5, 6, 0],
7524                vec![3, 2, 3, 1],
7525            ),
7526            (
7527                // Correctly-sized nulls
7528                vec![1, 2, 3, 0, 0, 0, 4, 5, 6, 0, 0, 0],
7529                vec![3, 3, 3, 3],
7530            ),
7531            (
7532                // Mixed nulls
7533                vec![1, 2, 3, 4, 5, 6, 0, 0, 0],
7534                vec![3, 0, 3, 3],
7535            ),
7536        ];
7537        let null_buffer = NullBuffer::from(vec![true, false, true, false]);
7538
7539        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
7540            vec![
7541                Some(vec![Some(1), Some(2), Some(3)]),
7542                None,
7543                Some(vec![Some(4), Some(5), Some(6)]),
7544                None,
7545            ],
7546            3,
7547        )) as ArrayRef;
7548
7549        for (values, lengths) in cases.iter() {
7550            let array = Arc::new(ListArray::new(
7551                field.clone(),
7552                OffsetBuffer::from_lengths(lengths.clone()),
7553                Arc::new(Int32Array::from(values.clone())),
7554                Some(null_buffer.clone()),
7555            )) as ArrayRef;
7556            let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
7557            assert_eq!(expected.as_ref(), actual.as_ref());
7558        }
7559    }
7560
7561    #[test]
7562    fn test_cast_list_to_fsl_safety() {
7563        let values = vec![
7564            Some(vec![Some(1), Some(2), Some(3)]),
7565            Some(vec![Some(4), Some(5)]),
7566            Some(vec![Some(6), Some(7), Some(8), Some(9)]),
7567            Some(vec![Some(3), Some(4), Some(5)]),
7568        ];
7569        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(
7570            values.clone(),
7571        )) as ArrayRef;
7572
7573        let res = cast_with_options(
7574            array.as_ref(),
7575            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
7576            &CastOptions {
7577                safe: false,
7578                ..Default::default()
7579            },
7580        );
7581        assert!(res.is_err());
7582        assert!(format!("{:?}", res)
7583            .contains("Cannot cast to FixedSizeList(3): value at index 1 has length 2"));
7584
7585        // When safe=true (default), the cast will fill nulls for lists that are
7586        // too short and truncate lists that are too long.
7587        let res = cast(
7588            array.as_ref(),
7589            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
7590        )
7591        .unwrap();
7592        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
7593            vec![
7594                Some(vec![Some(1), Some(2), Some(3)]),
7595                None, // Too short -> replaced with null
7596                None, // Too long -> replaced with null
7597                Some(vec![Some(3), Some(4), Some(5)]),
7598            ],
7599            3,
7600        )) as ArrayRef;
7601        assert_eq!(expected.as_ref(), res.as_ref());
7602
7603        // The safe option is false and the source array contains a null list.
7604        // issue: https://github.com/apache/arrow-rs/issues/5642
7605        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
7606            Some(vec![Some(1), Some(2), Some(3)]),
7607            None,
7608        ])) as ArrayRef;
7609        let res = cast_with_options(
7610            array.as_ref(),
7611            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
7612            &CastOptions {
7613                safe: false,
7614                ..Default::default()
7615            },
7616        )
7617        .unwrap();
7618        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
7619            vec![Some(vec![Some(1), Some(2), Some(3)]), None],
7620            3,
7621        )) as ArrayRef;
7622        assert_eq!(expected.as_ref(), res.as_ref());
7623    }
7624
7625    #[test]
7626    fn test_cast_large_list_to_fsl() {
7627        let values = vec![Some(vec![Some(1), Some(2)]), Some(vec![Some(3), Some(4)])];
7628        let array = Arc::new(LargeListArray::from_iter_primitive::<Int32Type, _, _>(
7629            values.clone(),
7630        )) as ArrayRef;
7631        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
7632            values, 2,
7633        )) as ArrayRef;
7634        let actual = cast(
7635            array.as_ref(),
7636            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 2),
7637        )
7638        .unwrap();
7639        assert_eq!(expected.as_ref(), actual.as_ref());
7640    }
7641
7642    #[test]
7643    fn test_cast_list_to_fsl_subcast() {
7644        let array = Arc::new(LargeListArray::from_iter_primitive::<Int32Type, _, _>(
7645            vec![
7646                Some(vec![Some(1), Some(2)]),
7647                Some(vec![Some(3), Some(i32::MAX)]),
7648            ],
7649        )) as ArrayRef;
7650        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
7651            vec![
7652                Some(vec![Some(1), Some(2)]),
7653                Some(vec![Some(3), Some(i32::MAX as i64)]),
7654            ],
7655            2,
7656        )) as ArrayRef;
7657        let actual = cast(
7658            array.as_ref(),
7659            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int64, true)), 2),
7660        )
7661        .unwrap();
7662        assert_eq!(expected.as_ref(), actual.as_ref());
7663
7664        let res = cast_with_options(
7665            array.as_ref(),
7666            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int16, true)), 2),
7667            &CastOptions {
7668                safe: false,
7669                ..Default::default()
7670            },
7671        );
7672        assert!(res.is_err());
7673        assert!(format!("{:?}", res).contains("Can't cast value 2147483647 to type Int16"));
7674    }
7675
7676    #[test]
7677    fn test_cast_list_to_fsl_empty() {
7678        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
7679        let array = new_empty_array(&DataType::List(field.clone()));
7680
7681        let target_type = DataType::FixedSizeList(field.clone(), 3);
7682        let expected = new_empty_array(&target_type);
7683
7684        let actual = cast(array.as_ref(), &target_type).unwrap();
7685        assert_eq!(expected.as_ref(), actual.as_ref());
7686    }
7687
7688    fn make_list_array() -> ListArray {
7689        // Construct a value array
7690        let value_data = ArrayData::builder(DataType::Int32)
7691            .len(8)
7692            .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7]))
7693            .build()
7694            .unwrap();
7695
7696        // Construct a buffer for value offsets, for the nested array:
7697        //  [[0, 1, 2], [3, 4, 5], [6, 7]]
7698        let value_offsets = Buffer::from_slice_ref([0, 3, 6, 8]);
7699
7700        // Construct a list array from the above two
7701        let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
7702        let list_data = ArrayData::builder(list_data_type)
7703            .len(3)
7704            .add_buffer(value_offsets)
7705            .add_child_data(value_data)
7706            .build()
7707            .unwrap();
7708        ListArray::from(list_data)
7709    }
7710
7711    fn make_large_list_array() -> LargeListArray {
7712        // Construct a value array
7713        let value_data = ArrayData::builder(DataType::Int32)
7714            .len(8)
7715            .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7]))
7716            .build()
7717            .unwrap();
7718
7719        // Construct a buffer for value offsets, for the nested array:
7720        //  [[0, 1, 2], [3, 4, 5], [6, 7]]
7721        let value_offsets = Buffer::from_slice_ref([0i64, 3, 6, 8]);
7722
7723        // Construct a list array from the above two
7724        let list_data_type =
7725            DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
7726        let list_data = ArrayData::builder(list_data_type)
7727            .len(3)
7728            .add_buffer(value_offsets)
7729            .add_child_data(value_data)
7730            .build()
7731            .unwrap();
7732        LargeListArray::from(list_data)
7733    }
7734
7735    fn make_fixed_size_list_array() -> FixedSizeListArray {
7736        // Construct a value array
7737        let value_data = ArrayData::builder(DataType::Int32)
7738            .len(8)
7739            .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7]))
7740            .build()
7741            .unwrap();
7742
7743        let list_data_type =
7744            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 4);
7745        let list_data = ArrayData::builder(list_data_type)
7746            .len(2)
7747            .add_child_data(value_data)
7748            .build()
7749            .unwrap();
7750        FixedSizeListArray::from(list_data)
7751    }
7752
7753    fn make_fixed_size_list_array_for_large_list() -> FixedSizeListArray {
7754        // Construct a value array
7755        let value_data = ArrayData::builder(DataType::Int64)
7756            .len(8)
7757            .add_buffer(Buffer::from_slice_ref([0i64, 1, 2, 3, 4, 5, 6, 7]))
7758            .build()
7759            .unwrap();
7760
7761        let list_data_type =
7762            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int64, true)), 4);
7763        let list_data = ArrayData::builder(list_data_type)
7764            .len(2)
7765            .add_child_data(value_data)
7766            .build()
7767            .unwrap();
7768        FixedSizeListArray::from(list_data)
7769    }
7770
7771    #[test]
7772    fn test_cast_map_dont_allow_change_of_order() {
7773        let string_builder = StringBuilder::new();
7774        let value_builder = StringBuilder::new();
7775        let mut builder = MapBuilder::new(
7776            Some(MapFieldNames {
7777                entry: "entries".to_string(),
7778                key: "key".to_string(),
7779                value: "value".to_string(),
7780            }),
7781            string_builder,
7782            value_builder,
7783        );
7784
7785        builder.keys().append_value("0");
7786        builder.values().append_value("test_val_1");
7787        builder.append(true).unwrap();
7788        builder.keys().append_value("1");
7789        builder.values().append_value("test_val_2");
7790        builder.append(true).unwrap();
7791
7792        // map builder returns unsorted map by default
7793        let array = builder.finish();
7794
7795        let new_ordered = true;
7796        let new_type = DataType::Map(
7797            Arc::new(Field::new(
7798                "entries",
7799                DataType::Struct(
7800                    vec![
7801                        Field::new("key", DataType::Utf8, false),
7802                        Field::new("value", DataType::Utf8, false),
7803                    ]
7804                    .into(),
7805                ),
7806                false,
7807            )),
7808            new_ordered,
7809        );
7810
7811        let new_array_result = cast(&array, &new_type.clone());
7812        assert!(!can_cast_types(array.data_type(), &new_type));
7813        assert!(
7814            matches!(new_array_result, Err(ArrowError::CastError(t)) if t == r#"Casting from Map(Field { name: "entries", data_type: Struct([Field { name: "key", data_type: Utf8, nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }, Field { name: "value", data_type: Utf8, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }]), nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }, false) to Map(Field { name: "entries", data_type: Struct([Field { name: "key", data_type: Utf8, nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }, Field { name: "value", data_type: Utf8, nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }]), nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }, true) not supported"#)
7815        );
7816    }
7817
7818    #[test]
7819    fn test_cast_map_dont_allow_when_container_cant_cast() {
7820        let string_builder = StringBuilder::new();
7821        let value_builder = IntervalDayTimeArray::builder(2);
7822        let mut builder = MapBuilder::new(
7823            Some(MapFieldNames {
7824                entry: "entries".to_string(),
7825                key: "key".to_string(),
7826                value: "value".to_string(),
7827            }),
7828            string_builder,
7829            value_builder,
7830        );
7831
7832        builder.keys().append_value("0");
7833        builder.values().append_value(IntervalDayTime::new(1, 1));
7834        builder.append(true).unwrap();
7835        builder.keys().append_value("1");
7836        builder.values().append_value(IntervalDayTime::new(2, 2));
7837        builder.append(true).unwrap();
7838
7839        // map builder returns unsorted map by default
7840        let array = builder.finish();
7841
7842        let new_ordered = true;
7843        let new_type = DataType::Map(
7844            Arc::new(Field::new(
7845                "entries",
7846                DataType::Struct(
7847                    vec![
7848                        Field::new("key", DataType::Utf8, false),
7849                        Field::new("value", DataType::Duration(TimeUnit::Second), false),
7850                    ]
7851                    .into(),
7852                ),
7853                false,
7854            )),
7855            new_ordered,
7856        );
7857
7858        let new_array_result = cast(&array, &new_type.clone());
7859        assert!(!can_cast_types(array.data_type(), &new_type));
7860        assert!(
7861            matches!(new_array_result, Err(ArrowError::CastError(t)) if t == r#"Casting from Map(Field { name: "entries", data_type: Struct([Field { name: "key", data_type: Utf8, nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }, Field { name: "value", data_type: Interval(DayTime), nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }]), nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }, false) to Map(Field { name: "entries", data_type: Struct([Field { name: "key", data_type: Utf8, nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }, Field { name: "value", data_type: Duration(Second), nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }]), nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }, true) not supported"#)
7862        );
7863    }
7864
7865    #[test]
7866    fn test_cast_map_field_names() {
7867        let string_builder = StringBuilder::new();
7868        let value_builder = StringBuilder::new();
7869        let mut builder = MapBuilder::new(
7870            Some(MapFieldNames {
7871                entry: "entries".to_string(),
7872                key: "key".to_string(),
7873                value: "value".to_string(),
7874            }),
7875            string_builder,
7876            value_builder,
7877        );
7878
7879        builder.keys().append_value("0");
7880        builder.values().append_value("test_val_1");
7881        builder.append(true).unwrap();
7882        builder.keys().append_value("1");
7883        builder.values().append_value("test_val_2");
7884        builder.append(true).unwrap();
7885        builder.append(false).unwrap();
7886
7887        let array = builder.finish();
7888
7889        let new_type = DataType::Map(
7890            Arc::new(Field::new(
7891                "entries_new",
7892                DataType::Struct(
7893                    vec![
7894                        Field::new("key_new", DataType::Utf8, false),
7895                        Field::new("value_values", DataType::Utf8, false),
7896                    ]
7897                    .into(),
7898                ),
7899                false,
7900            )),
7901            false,
7902        );
7903
7904        assert_ne!(new_type, array.data_type().clone());
7905
7906        let new_array = cast(&array, &new_type.clone()).unwrap();
7907        assert_eq!(new_type, new_array.data_type().clone());
7908        let map_array = new_array.as_map();
7909
7910        assert_ne!(new_type, array.data_type().clone());
7911        assert_eq!(new_type, map_array.data_type().clone());
7912
7913        let key_string = map_array
7914            .keys()
7915            .as_any()
7916            .downcast_ref::<StringArray>()
7917            .unwrap()
7918            .into_iter()
7919            .flatten()
7920            .collect::<Vec<_>>();
7921        assert_eq!(&key_string, &vec!["0", "1"]);
7922
7923        let values_string_array = cast(map_array.values(), &DataType::Utf8).unwrap();
7924        let values_string = values_string_array
7925            .as_any()
7926            .downcast_ref::<StringArray>()
7927            .unwrap()
7928            .into_iter()
7929            .flatten()
7930            .collect::<Vec<_>>();
7931        assert_eq!(&values_string, &vec!["test_val_1", "test_val_2"]);
7932
7933        assert_eq!(
7934            map_array.nulls(),
7935            Some(&NullBuffer::from(vec![true, true, false]))
7936        );
7937    }
7938
7939    #[test]
7940    fn test_cast_map_contained_values() {
7941        let string_builder = StringBuilder::new();
7942        let value_builder = Int8Builder::new();
7943        let mut builder = MapBuilder::new(
7944            Some(MapFieldNames {
7945                entry: "entries".to_string(),
7946                key: "key".to_string(),
7947                value: "value".to_string(),
7948            }),
7949            string_builder,
7950            value_builder,
7951        );
7952
7953        builder.keys().append_value("0");
7954        builder.values().append_value(44);
7955        builder.append(true).unwrap();
7956        builder.keys().append_value("1");
7957        builder.values().append_value(22);
7958        builder.append(true).unwrap();
7959
7960        let array = builder.finish();
7961
7962        let new_type = DataType::Map(
7963            Arc::new(Field::new(
7964                "entries",
7965                DataType::Struct(
7966                    vec![
7967                        Field::new("key", DataType::Utf8, false),
7968                        Field::new("value", DataType::Utf8, false),
7969                    ]
7970                    .into(),
7971                ),
7972                false,
7973            )),
7974            false,
7975        );
7976
7977        let new_array = cast(&array, &new_type.clone()).unwrap();
7978        assert_eq!(new_type, new_array.data_type().clone());
7979        let map_array = new_array.as_map();
7980
7981        assert_ne!(new_type, array.data_type().clone());
7982        assert_eq!(new_type, map_array.data_type().clone());
7983
7984        let key_string = map_array
7985            .keys()
7986            .as_any()
7987            .downcast_ref::<StringArray>()
7988            .unwrap()
7989            .into_iter()
7990            .flatten()
7991            .collect::<Vec<_>>();
7992        assert_eq!(&key_string, &vec!["0", "1"]);
7993
7994        let values_string_array = cast(map_array.values(), &DataType::Utf8).unwrap();
7995        let values_string = values_string_array
7996            .as_any()
7997            .downcast_ref::<StringArray>()
7998            .unwrap()
7999            .into_iter()
8000            .flatten()
8001            .collect::<Vec<_>>();
8002        assert_eq!(&values_string, &vec!["44", "22"]);
8003    }
8004
8005    #[test]
8006    fn test_utf8_cast_offsets() {
8007        // test if offset of the array is taken into account during cast
8008        let str_array = StringArray::from(vec!["a", "b", "c"]);
8009        let str_array = str_array.slice(1, 2);
8010
8011        let out = cast(&str_array, &DataType::LargeUtf8).unwrap();
8012
8013        let large_str_array = out.as_any().downcast_ref::<LargeStringArray>().unwrap();
8014        let strs = large_str_array.into_iter().flatten().collect::<Vec<_>>();
8015        assert_eq!(strs, &["b", "c"])
8016    }
8017
8018    #[test]
8019    fn test_list_cast_offsets() {
8020        // test if offset of the array is taken into account during cast
8021        let array1 = make_list_array().slice(1, 2);
8022        let array2 = Arc::new(make_list_array()) as ArrayRef;
8023
8024        let dt = DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
8025        let out1 = cast(&array1, &dt).unwrap();
8026        let out2 = cast(&array2, &dt).unwrap();
8027
8028        assert_eq!(&out1, &out2.slice(1, 2))
8029    }
8030
8031    #[test]
8032    fn test_list_to_string() {
8033        let str_array = StringArray::from(vec!["a", "b", "c", "d", "e", "f", "g", "h"]);
8034        let value_offsets = Buffer::from_slice_ref([0, 3, 6, 8]);
8035        let value_data = str_array.into_data();
8036
8037        let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Utf8, true)));
8038        let list_data = ArrayData::builder(list_data_type)
8039            .len(3)
8040            .add_buffer(value_offsets)
8041            .add_child_data(value_data)
8042            .build()
8043            .unwrap();
8044        let array = Arc::new(ListArray::from(list_data)) as ArrayRef;
8045
8046        let out = cast(&array, &DataType::Utf8).unwrap();
8047        let out = out
8048            .as_any()
8049            .downcast_ref::<StringArray>()
8050            .unwrap()
8051            .into_iter()
8052            .flatten()
8053            .collect::<Vec<_>>();
8054        assert_eq!(&out, &vec!["[a, b, c]", "[d, e, f]", "[g, h]"]);
8055
8056        let out = cast(&array, &DataType::LargeUtf8).unwrap();
8057        let out = out
8058            .as_any()
8059            .downcast_ref::<LargeStringArray>()
8060            .unwrap()
8061            .into_iter()
8062            .flatten()
8063            .collect::<Vec<_>>();
8064        assert_eq!(&out, &vec!["[a, b, c]", "[d, e, f]", "[g, h]"]);
8065
8066        let array = Arc::new(make_list_array()) as ArrayRef;
8067        let out = cast(&array, &DataType::Utf8).unwrap();
8068        let out = out
8069            .as_any()
8070            .downcast_ref::<StringArray>()
8071            .unwrap()
8072            .into_iter()
8073            .flatten()
8074            .collect::<Vec<_>>();
8075        assert_eq!(&out, &vec!["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
8076
8077        let array = Arc::new(make_large_list_array()) as ArrayRef;
8078        let out = cast(&array, &DataType::LargeUtf8).unwrap();
8079        let out = out
8080            .as_any()
8081            .downcast_ref::<LargeStringArray>()
8082            .unwrap()
8083            .into_iter()
8084            .flatten()
8085            .collect::<Vec<_>>();
8086        assert_eq!(&out, &vec!["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
8087    }
8088
8089    #[test]
8090    fn test_cast_f64_to_decimal128() {
8091        // to reproduce https://github.com/apache/arrow-rs/issues/2997
8092
8093        let decimal_type = DataType::Decimal128(18, 2);
8094        let array = Float64Array::from(vec![
8095            Some(0.0699999999),
8096            Some(0.0659999999),
8097            Some(0.0650000000),
8098            Some(0.0649999999),
8099        ]);
8100        let array = Arc::new(array) as ArrayRef;
8101        generate_cast_test_case!(
8102            &array,
8103            Decimal128Array,
8104            &decimal_type,
8105            vec![
8106                Some(7_i128), // round up
8107                Some(7_i128), // round up
8108                Some(7_i128), // round up
8109                Some(6_i128), // round down
8110            ]
8111        );
8112
8113        let decimal_type = DataType::Decimal128(18, 3);
8114        let array = Float64Array::from(vec![
8115            Some(0.0699999999),
8116            Some(0.0659999999),
8117            Some(0.0650000000),
8118            Some(0.0649999999),
8119        ]);
8120        let array = Arc::new(array) as ArrayRef;
8121        generate_cast_test_case!(
8122            &array,
8123            Decimal128Array,
8124            &decimal_type,
8125            vec![
8126                Some(70_i128), // round up
8127                Some(66_i128), // round up
8128                Some(65_i128), // round down
8129                Some(65_i128), // round up
8130            ]
8131        );
8132    }
8133
8134    #[test]
8135    fn test_cast_numeric_to_decimal128_overflow() {
8136        let array = Int64Array::from(vec![i64::MAX]);
8137        let array = Arc::new(array) as ArrayRef;
8138        let casted_array = cast_with_options(
8139            &array,
8140            &DataType::Decimal128(38, 30),
8141            &CastOptions {
8142                safe: true,
8143                format_options: FormatOptions::default(),
8144            },
8145        );
8146        assert!(casted_array.is_ok());
8147        assert!(casted_array.unwrap().is_null(0));
8148
8149        let casted_array = cast_with_options(
8150            &array,
8151            &DataType::Decimal128(38, 30),
8152            &CastOptions {
8153                safe: false,
8154                format_options: FormatOptions::default(),
8155            },
8156        );
8157        assert!(casted_array.is_err());
8158    }
8159
8160    #[test]
8161    fn test_cast_numeric_to_decimal256_overflow() {
8162        let array = Int64Array::from(vec![i64::MAX]);
8163        let array = Arc::new(array) as ArrayRef;
8164        let casted_array = cast_with_options(
8165            &array,
8166            &DataType::Decimal256(76, 76),
8167            &CastOptions {
8168                safe: true,
8169                format_options: FormatOptions::default(),
8170            },
8171        );
8172        assert!(casted_array.is_ok());
8173        assert!(casted_array.unwrap().is_null(0));
8174
8175        let casted_array = cast_with_options(
8176            &array,
8177            &DataType::Decimal256(76, 76),
8178            &CastOptions {
8179                safe: false,
8180                format_options: FormatOptions::default(),
8181            },
8182        );
8183        assert!(casted_array.is_err());
8184    }
8185
8186    #[test]
8187    fn test_cast_floating_point_to_decimal128_precision_overflow() {
8188        let array = Float64Array::from(vec![1.1]);
8189        let array = Arc::new(array) as ArrayRef;
8190        let casted_array = cast_with_options(
8191            &array,
8192            &DataType::Decimal128(2, 2),
8193            &CastOptions {
8194                safe: true,
8195                format_options: FormatOptions::default(),
8196            },
8197        );
8198        assert!(casted_array.is_ok());
8199        assert!(casted_array.unwrap().is_null(0));
8200
8201        let casted_array = cast_with_options(
8202            &array,
8203            &DataType::Decimal128(2, 2),
8204            &CastOptions {
8205                safe: false,
8206                format_options: FormatOptions::default(),
8207            },
8208        );
8209        let err = casted_array.unwrap_err().to_string();
8210        let expected_error = "Invalid argument error: 110 is too large to store in a Decimal128 of precision 2. Max is 99";
8211        assert!(
8212            err.contains(expected_error),
8213            "did not find expected error '{expected_error}' in actual error '{err}'"
8214        );
8215    }
8216
8217    #[test]
8218    fn test_cast_floating_point_to_decimal256_precision_overflow() {
8219        let array = Float64Array::from(vec![1.1]);
8220        let array = Arc::new(array) as ArrayRef;
8221        let casted_array = cast_with_options(
8222            &array,
8223            &DataType::Decimal256(2, 2),
8224            &CastOptions {
8225                safe: true,
8226                format_options: FormatOptions::default(),
8227            },
8228        );
8229        assert!(casted_array.is_ok());
8230        assert!(casted_array.unwrap().is_null(0));
8231
8232        let casted_array = cast_with_options(
8233            &array,
8234            &DataType::Decimal256(2, 2),
8235            &CastOptions {
8236                safe: false,
8237                format_options: FormatOptions::default(),
8238            },
8239        );
8240        let err = casted_array.unwrap_err().to_string();
8241        let expected_error = "Invalid argument error: 110 is too large to store in a Decimal256 of precision 2. Max is 99";
8242        assert!(
8243            err.contains(expected_error),
8244            "did not find expected error '{expected_error}' in actual error '{err}'"
8245        );
8246    }
8247
8248    #[test]
8249    fn test_cast_floating_point_to_decimal128_overflow() {
8250        let array = Float64Array::from(vec![f64::MAX]);
8251        let array = Arc::new(array) as ArrayRef;
8252        let casted_array = cast_with_options(
8253            &array,
8254            &DataType::Decimal128(38, 30),
8255            &CastOptions {
8256                safe: true,
8257                format_options: FormatOptions::default(),
8258            },
8259        );
8260        assert!(casted_array.is_ok());
8261        assert!(casted_array.unwrap().is_null(0));
8262
8263        let casted_array = cast_with_options(
8264            &array,
8265            &DataType::Decimal128(38, 30),
8266            &CastOptions {
8267                safe: false,
8268                format_options: FormatOptions::default(),
8269            },
8270        );
8271        let err = casted_array.unwrap_err().to_string();
8272        let expected_error = "Cast error: Cannot cast to Decimal128(38, 30)";
8273        assert!(
8274            err.contains(expected_error),
8275            "did not find expected error '{expected_error}' in actual error '{err}'"
8276        );
8277    }
8278
8279    #[test]
8280    fn test_cast_floating_point_to_decimal256_overflow() {
8281        let array = Float64Array::from(vec![f64::MAX]);
8282        let array = Arc::new(array) as ArrayRef;
8283        let casted_array = cast_with_options(
8284            &array,
8285            &DataType::Decimal256(76, 50),
8286            &CastOptions {
8287                safe: true,
8288                format_options: FormatOptions::default(),
8289            },
8290        );
8291        assert!(casted_array.is_ok());
8292        assert!(casted_array.unwrap().is_null(0));
8293
8294        let casted_array = cast_with_options(
8295            &array,
8296            &DataType::Decimal256(76, 50),
8297            &CastOptions {
8298                safe: false,
8299                format_options: FormatOptions::default(),
8300            },
8301        );
8302        let err = casted_array.unwrap_err().to_string();
8303        let expected_error = "Cast error: Cannot cast to Decimal256(76, 50)";
8304        assert!(
8305            err.contains(expected_error),
8306            "did not find expected error '{expected_error}' in actual error '{err}'"
8307        );
8308    }
8309
8310    #[test]
8311    fn test_cast_decimal128_to_decimal128_negative_scale() {
8312        let input_type = DataType::Decimal128(20, 0);
8313        let output_type = DataType::Decimal128(20, -1);
8314        assert!(can_cast_types(&input_type, &output_type));
8315        let array = vec![Some(1123450), Some(2123455), Some(3123456), None];
8316        let input_decimal_array = create_decimal128_array(array, 20, 0).unwrap();
8317        let array = Arc::new(input_decimal_array) as ArrayRef;
8318        generate_cast_test_case!(
8319            &array,
8320            Decimal128Array,
8321            &output_type,
8322            vec![
8323                Some(112345_i128),
8324                Some(212346_i128),
8325                Some(312346_i128),
8326                None
8327            ]
8328        );
8329
8330        let casted_array = cast(&array, &output_type).unwrap();
8331        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
8332
8333        assert_eq!("1123450", decimal_arr.value_as_string(0));
8334        assert_eq!("2123460", decimal_arr.value_as_string(1));
8335        assert_eq!("3123460", decimal_arr.value_as_string(2));
8336    }
8337
8338    #[test]
8339    fn test_cast_numeric_to_decimal128_negative() {
8340        let decimal_type = DataType::Decimal128(38, -1);
8341        let array = Arc::new(Int32Array::from(vec![
8342            Some(1123456),
8343            Some(2123456),
8344            Some(3123456),
8345        ])) as ArrayRef;
8346
8347        let casted_array = cast(&array, &decimal_type).unwrap();
8348        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
8349
8350        assert_eq!("1123450", decimal_arr.value_as_string(0));
8351        assert_eq!("2123450", decimal_arr.value_as_string(1));
8352        assert_eq!("3123450", decimal_arr.value_as_string(2));
8353
8354        let array = Arc::new(Float32Array::from(vec![
8355            Some(1123.456),
8356            Some(2123.456),
8357            Some(3123.456),
8358        ])) as ArrayRef;
8359
8360        let casted_array = cast(&array, &decimal_type).unwrap();
8361        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
8362
8363        assert_eq!("1120", decimal_arr.value_as_string(0));
8364        assert_eq!("2120", decimal_arr.value_as_string(1));
8365        assert_eq!("3120", decimal_arr.value_as_string(2));
8366    }
8367
8368    #[test]
8369    fn test_cast_decimal128_to_decimal128_negative() {
8370        let input_type = DataType::Decimal128(10, -1);
8371        let output_type = DataType::Decimal128(10, -2);
8372        assert!(can_cast_types(&input_type, &output_type));
8373        let array = vec![Some(123)];
8374        let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
8375        let array = Arc::new(input_decimal_array) as ArrayRef;
8376        generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(12_i128),]);
8377
8378        let casted_array = cast(&array, &output_type).unwrap();
8379        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
8380
8381        assert_eq!("1200", decimal_arr.value_as_string(0));
8382
8383        let array = vec![Some(125)];
8384        let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
8385        let array = Arc::new(input_decimal_array) as ArrayRef;
8386        generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(13_i128),]);
8387
8388        let casted_array = cast(&array, &output_type).unwrap();
8389        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
8390
8391        assert_eq!("1300", decimal_arr.value_as_string(0));
8392    }
8393
8394    #[test]
8395    fn test_cast_decimal128_to_decimal256_negative() {
8396        let input_type = DataType::Decimal128(10, 3);
8397        let output_type = DataType::Decimal256(10, 5);
8398        assert!(can_cast_types(&input_type, &output_type));
8399        let array = vec![Some(123456), Some(-123456)];
8400        let input_decimal_array = create_decimal128_array(array, 10, 3).unwrap();
8401        let array = Arc::new(input_decimal_array) as ArrayRef;
8402
8403        let hundred = i256::from_i128(100);
8404        generate_cast_test_case!(
8405            &array,
8406            Decimal256Array,
8407            &output_type,
8408            vec![
8409                Some(i256::from_i128(123456).mul_wrapping(hundred)),
8410                Some(i256::from_i128(-123456).mul_wrapping(hundred))
8411            ]
8412        );
8413    }
8414
8415    #[test]
8416    fn test_parse_string_to_decimal() {
8417        assert_eq!(
8418            Decimal128Type::format_decimal(
8419                parse_string_to_decimal_native::<Decimal128Type>("123.45", 2).unwrap(),
8420                38,
8421                2,
8422            ),
8423            "123.45"
8424        );
8425        assert_eq!(
8426            Decimal128Type::format_decimal(
8427                parse_string_to_decimal_native::<Decimal128Type>("12345", 2).unwrap(),
8428                38,
8429                2,
8430            ),
8431            "12345.00"
8432        );
8433        assert_eq!(
8434            Decimal128Type::format_decimal(
8435                parse_string_to_decimal_native::<Decimal128Type>("0.12345", 2).unwrap(),
8436                38,
8437                2,
8438            ),
8439            "0.12"
8440        );
8441        assert_eq!(
8442            Decimal128Type::format_decimal(
8443                parse_string_to_decimal_native::<Decimal128Type>(".12345", 2).unwrap(),
8444                38,
8445                2,
8446            ),
8447            "0.12"
8448        );
8449        assert_eq!(
8450            Decimal128Type::format_decimal(
8451                parse_string_to_decimal_native::<Decimal128Type>(".1265", 2).unwrap(),
8452                38,
8453                2,
8454            ),
8455            "0.13"
8456        );
8457        assert_eq!(
8458            Decimal128Type::format_decimal(
8459                parse_string_to_decimal_native::<Decimal128Type>(".1265", 2).unwrap(),
8460                38,
8461                2,
8462            ),
8463            "0.13"
8464        );
8465
8466        assert_eq!(
8467            Decimal256Type::format_decimal(
8468                parse_string_to_decimal_native::<Decimal256Type>("123.45", 3).unwrap(),
8469                38,
8470                3,
8471            ),
8472            "123.450"
8473        );
8474        assert_eq!(
8475            Decimal256Type::format_decimal(
8476                parse_string_to_decimal_native::<Decimal256Type>("12345", 3).unwrap(),
8477                38,
8478                3,
8479            ),
8480            "12345.000"
8481        );
8482        assert_eq!(
8483            Decimal256Type::format_decimal(
8484                parse_string_to_decimal_native::<Decimal256Type>("0.12345", 3).unwrap(),
8485                38,
8486                3,
8487            ),
8488            "0.123"
8489        );
8490        assert_eq!(
8491            Decimal256Type::format_decimal(
8492                parse_string_to_decimal_native::<Decimal256Type>(".12345", 3).unwrap(),
8493                38,
8494                3,
8495            ),
8496            "0.123"
8497        );
8498        assert_eq!(
8499            Decimal256Type::format_decimal(
8500                parse_string_to_decimal_native::<Decimal256Type>(".1265", 3).unwrap(),
8501                38,
8502                3,
8503            ),
8504            "0.127"
8505        );
8506    }
8507
8508    fn test_cast_string_to_decimal(array: ArrayRef) {
8509        // Decimal128
8510        let output_type = DataType::Decimal128(38, 2);
8511        assert!(can_cast_types(array.data_type(), &output_type));
8512
8513        let casted_array = cast(&array, &output_type).unwrap();
8514        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
8515
8516        assert_eq!("123.45", decimal_arr.value_as_string(0));
8517        assert_eq!("1.23", decimal_arr.value_as_string(1));
8518        assert_eq!("0.12", decimal_arr.value_as_string(2));
8519        assert_eq!("0.13", decimal_arr.value_as_string(3));
8520        assert_eq!("1.26", decimal_arr.value_as_string(4));
8521        assert_eq!("12345.00", decimal_arr.value_as_string(5));
8522        assert_eq!("12345.00", decimal_arr.value_as_string(6));
8523        assert_eq!("0.12", decimal_arr.value_as_string(7));
8524        assert_eq!("12.23", decimal_arr.value_as_string(8));
8525        assert!(decimal_arr.is_null(9));
8526        assert_eq!("0.00", decimal_arr.value_as_string(10));
8527        assert_eq!("0.00", decimal_arr.value_as_string(11));
8528        assert!(decimal_arr.is_null(12));
8529        assert_eq!("-1.23", decimal_arr.value_as_string(13));
8530        assert_eq!("-1.24", decimal_arr.value_as_string(14));
8531        assert_eq!("0.00", decimal_arr.value_as_string(15));
8532        assert_eq!("-123.00", decimal_arr.value_as_string(16));
8533        assert_eq!("-123.23", decimal_arr.value_as_string(17));
8534        assert_eq!("-0.12", decimal_arr.value_as_string(18));
8535        assert_eq!("1.23", decimal_arr.value_as_string(19));
8536        assert_eq!("1.24", decimal_arr.value_as_string(20));
8537        assert_eq!("0.00", decimal_arr.value_as_string(21));
8538        assert_eq!("123.00", decimal_arr.value_as_string(22));
8539        assert_eq!("123.23", decimal_arr.value_as_string(23));
8540        assert_eq!("0.12", decimal_arr.value_as_string(24));
8541        assert!(decimal_arr.is_null(25));
8542        assert!(decimal_arr.is_null(26));
8543        assert!(decimal_arr.is_null(27));
8544        assert_eq!("0.00", decimal_arr.value_as_string(28));
8545        assert_eq!("0.00", decimal_arr.value_as_string(29));
8546        assert_eq!("12345.00", decimal_arr.value_as_string(30));
8547        assert_eq!(decimal_arr.len(), 31);
8548
8549        // Decimal256
8550        let output_type = DataType::Decimal256(76, 3);
8551        assert!(can_cast_types(array.data_type(), &output_type));
8552
8553        let casted_array = cast(&array, &output_type).unwrap();
8554        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
8555
8556        assert_eq!("123.450", decimal_arr.value_as_string(0));
8557        assert_eq!("1.235", decimal_arr.value_as_string(1));
8558        assert_eq!("0.123", decimal_arr.value_as_string(2));
8559        assert_eq!("0.127", decimal_arr.value_as_string(3));
8560        assert_eq!("1.263", decimal_arr.value_as_string(4));
8561        assert_eq!("12345.000", decimal_arr.value_as_string(5));
8562        assert_eq!("12345.000", decimal_arr.value_as_string(6));
8563        assert_eq!("0.123", decimal_arr.value_as_string(7));
8564        assert_eq!("12.234", decimal_arr.value_as_string(8));
8565        assert!(decimal_arr.is_null(9));
8566        assert_eq!("0.000", decimal_arr.value_as_string(10));
8567        assert_eq!("0.000", decimal_arr.value_as_string(11));
8568        assert!(decimal_arr.is_null(12));
8569        assert_eq!("-1.235", decimal_arr.value_as_string(13));
8570        assert_eq!("-1.236", decimal_arr.value_as_string(14));
8571        assert_eq!("0.000", decimal_arr.value_as_string(15));
8572        assert_eq!("-123.000", decimal_arr.value_as_string(16));
8573        assert_eq!("-123.234", decimal_arr.value_as_string(17));
8574        assert_eq!("-0.123", decimal_arr.value_as_string(18));
8575        assert_eq!("1.235", decimal_arr.value_as_string(19));
8576        assert_eq!("1.236", decimal_arr.value_as_string(20));
8577        assert_eq!("0.000", decimal_arr.value_as_string(21));
8578        assert_eq!("123.000", decimal_arr.value_as_string(22));
8579        assert_eq!("123.234", decimal_arr.value_as_string(23));
8580        assert_eq!("0.123", decimal_arr.value_as_string(24));
8581        assert!(decimal_arr.is_null(25));
8582        assert!(decimal_arr.is_null(26));
8583        assert!(decimal_arr.is_null(27));
8584        assert_eq!("0.000", decimal_arr.value_as_string(28));
8585        assert_eq!("0.000", decimal_arr.value_as_string(29));
8586        assert_eq!("12345.000", decimal_arr.value_as_string(30));
8587        assert_eq!(decimal_arr.len(), 31);
8588    }
8589
8590    #[test]
8591    fn test_cast_utf8_to_decimal() {
8592        let str_array = StringArray::from(vec![
8593            Some("123.45"),
8594            Some("1.2345"),
8595            Some("0.12345"),
8596            Some("0.1267"),
8597            Some("1.263"),
8598            Some("12345.0"),
8599            Some("12345"),
8600            Some("000.123"),
8601            Some("12.234000"),
8602            None,
8603            Some(""),
8604            Some(" "),
8605            None,
8606            Some("-1.23499999"),
8607            Some("-1.23599999"),
8608            Some("-0.00001"),
8609            Some("-123"),
8610            Some("-123.234000"),
8611            Some("-000.123"),
8612            Some("+1.23499999"),
8613            Some("+1.23599999"),
8614            Some("+0.00001"),
8615            Some("+123"),
8616            Some("+123.234000"),
8617            Some("+000.123"),
8618            Some("1.-23499999"),
8619            Some("-1.-23499999"),
8620            Some("--1.23499999"),
8621            Some("0"),
8622            Some("000.000"),
8623            Some("0000000000000000012345.000"),
8624        ]);
8625        let array = Arc::new(str_array) as ArrayRef;
8626
8627        test_cast_string_to_decimal(array);
8628
8629        let test_cases = [
8630            (None, None),
8631            // (Some(""), None),
8632            // (Some("   "), None),
8633            (Some("0"), Some("0")),
8634            (Some("000.000"), Some("0")),
8635            (Some("12345"), Some("12345")),
8636            (Some("000000000000000000000000000012345"), Some("12345")),
8637            (Some("-123"), Some("-123")),
8638            (Some("+123"), Some("123")),
8639        ];
8640        let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
8641        let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
8642
8643        let array = Arc::new(StringArray::from(inputs)) as ArrayRef;
8644        test_cast_string_to_decimal_scale_zero(array, &expected);
8645    }
8646
8647    #[test]
8648    fn test_cast_large_utf8_to_decimal() {
8649        let str_array = LargeStringArray::from(vec![
8650            Some("123.45"),
8651            Some("1.2345"),
8652            Some("0.12345"),
8653            Some("0.1267"),
8654            Some("1.263"),
8655            Some("12345.0"),
8656            Some("12345"),
8657            Some("000.123"),
8658            Some("12.234000"),
8659            None,
8660            Some(""),
8661            Some(" "),
8662            None,
8663            Some("-1.23499999"),
8664            Some("-1.23599999"),
8665            Some("-0.00001"),
8666            Some("-123"),
8667            Some("-123.234000"),
8668            Some("-000.123"),
8669            Some("+1.23499999"),
8670            Some("+1.23599999"),
8671            Some("+0.00001"),
8672            Some("+123"),
8673            Some("+123.234000"),
8674            Some("+000.123"),
8675            Some("1.-23499999"),
8676            Some("-1.-23499999"),
8677            Some("--1.23499999"),
8678            Some("0"),
8679            Some("000.000"),
8680            Some("0000000000000000012345.000"),
8681        ]);
8682        let array = Arc::new(str_array) as ArrayRef;
8683
8684        test_cast_string_to_decimal(array);
8685
8686        let test_cases = [
8687            (None, None),
8688            (Some(""), None),
8689            (Some("   "), None),
8690            (Some("0"), Some("0")),
8691            (Some("000.000"), Some("0")),
8692            (Some("12345"), Some("12345")),
8693            (Some("000000000000000000000000000012345"), Some("12345")),
8694            (Some("-123"), Some("-123")),
8695            (Some("+123"), Some("123")),
8696        ];
8697        let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
8698        let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
8699
8700        let array = Arc::new(LargeStringArray::from(inputs)) as ArrayRef;
8701        test_cast_string_to_decimal_scale_zero(array, &expected);
8702    }
8703
8704    fn test_cast_string_to_decimal_scale_zero(
8705        array: ArrayRef,
8706        expected_as_string: &[Option<&str>],
8707    ) {
8708        // Decimal128
8709        let output_type = DataType::Decimal128(38, 0);
8710        assert!(can_cast_types(array.data_type(), &output_type));
8711        let casted_array = cast(&array, &output_type).unwrap();
8712        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
8713        assert_decimal_array_contents(decimal_arr, expected_as_string);
8714
8715        // Decimal256
8716        let output_type = DataType::Decimal256(76, 0);
8717        assert!(can_cast_types(array.data_type(), &output_type));
8718        let casted_array = cast(&array, &output_type).unwrap();
8719        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
8720        assert_decimal_array_contents(decimal_arr, expected_as_string);
8721    }
8722
8723    fn assert_decimal_array_contents<T>(
8724        array: &PrimitiveArray<T>,
8725        expected_as_string: &[Option<&str>],
8726    ) where
8727        T: DecimalType + ArrowPrimitiveType,
8728    {
8729        assert_eq!(array.len(), expected_as_string.len());
8730        for (i, expected) in expected_as_string.iter().enumerate() {
8731            let actual = if array.is_null(i) {
8732                None
8733            } else {
8734                Some(array.value_as_string(i))
8735            };
8736            let actual = actual.as_ref().map(|s| s.as_ref());
8737            assert_eq!(*expected, actual, "Expected at position {}", i);
8738        }
8739    }
8740
8741    #[test]
8742    fn test_cast_invalid_utf8_to_decimal() {
8743        let str_array = StringArray::from(vec!["4.4.5", ". 0.123"]);
8744        let array = Arc::new(str_array) as ArrayRef;
8745
8746        // Safe cast
8747        let output_type = DataType::Decimal128(38, 2);
8748        let casted_array = cast(&array, &output_type).unwrap();
8749        assert!(casted_array.is_null(0));
8750        assert!(casted_array.is_null(1));
8751
8752        let output_type = DataType::Decimal256(76, 2);
8753        let casted_array = cast(&array, &output_type).unwrap();
8754        assert!(casted_array.is_null(0));
8755        assert!(casted_array.is_null(1));
8756
8757        // Non-safe cast
8758        let output_type = DataType::Decimal128(38, 2);
8759        let str_array = StringArray::from(vec!["4.4.5"]);
8760        let array = Arc::new(str_array) as ArrayRef;
8761        let option = CastOptions {
8762            safe: false,
8763            format_options: FormatOptions::default(),
8764        };
8765        let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
8766        assert!(casted_err
8767            .to_string()
8768            .contains("Cannot cast string '4.4.5' to value of Decimal128(38, 10) type"));
8769
8770        let str_array = StringArray::from(vec![". 0.123"]);
8771        let array = Arc::new(str_array) as ArrayRef;
8772        let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
8773        assert!(casted_err
8774            .to_string()
8775            .contains("Cannot cast string '. 0.123' to value of Decimal128(38, 10) type"));
8776    }
8777
8778    fn test_cast_string_to_decimal128_overflow(overflow_array: ArrayRef) {
8779        let output_type = DataType::Decimal128(38, 2);
8780        let casted_array = cast(&overflow_array, &output_type).unwrap();
8781        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
8782
8783        assert!(decimal_arr.is_null(0));
8784        assert!(decimal_arr.is_null(1));
8785        assert!(decimal_arr.is_null(2));
8786        assert_eq!(
8787            "999999999999999999999999999999999999.99",
8788            decimal_arr.value_as_string(3)
8789        );
8790        assert_eq!(
8791            "100000000000000000000000000000000000.00",
8792            decimal_arr.value_as_string(4)
8793        );
8794    }
8795
8796    #[test]
8797    fn test_cast_string_to_decimal128_precision_overflow() {
8798        let array = StringArray::from(vec!["1000".to_string()]);
8799        let array = Arc::new(array) as ArrayRef;
8800        let casted_array = cast_with_options(
8801            &array,
8802            &DataType::Decimal128(10, 8),
8803            &CastOptions {
8804                safe: true,
8805                format_options: FormatOptions::default(),
8806            },
8807        );
8808        assert!(casted_array.is_ok());
8809        assert!(casted_array.unwrap().is_null(0));
8810
8811        let err = cast_with_options(
8812            &array,
8813            &DataType::Decimal128(10, 8),
8814            &CastOptions {
8815                safe: false,
8816                format_options: FormatOptions::default(),
8817            },
8818        );
8819        assert_eq!("Invalid argument error: 100000000000 is too large to store in a Decimal128 of precision 10. Max is 9999999999", err.unwrap_err().to_string());
8820    }
8821
8822    #[test]
8823    fn test_cast_utf8_to_decimal128_overflow() {
8824        let overflow_str_array = StringArray::from(vec![
8825            i128::MAX.to_string(),
8826            i128::MIN.to_string(),
8827            "99999999999999999999999999999999999999".to_string(),
8828            "999999999999999999999999999999999999.99".to_string(),
8829            "99999999999999999999999999999999999.999".to_string(),
8830        ]);
8831        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
8832
8833        test_cast_string_to_decimal128_overflow(overflow_array);
8834    }
8835
8836    #[test]
8837    fn test_cast_large_utf8_to_decimal128_overflow() {
8838        let overflow_str_array = LargeStringArray::from(vec![
8839            i128::MAX.to_string(),
8840            i128::MIN.to_string(),
8841            "99999999999999999999999999999999999999".to_string(),
8842            "999999999999999999999999999999999999.99".to_string(),
8843            "99999999999999999999999999999999999.999".to_string(),
8844        ]);
8845        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
8846
8847        test_cast_string_to_decimal128_overflow(overflow_array);
8848    }
8849
8850    fn test_cast_string_to_decimal256_overflow(overflow_array: ArrayRef) {
8851        let output_type = DataType::Decimal256(76, 2);
8852        let casted_array = cast(&overflow_array, &output_type).unwrap();
8853        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
8854
8855        assert_eq!(
8856            "170141183460469231731687303715884105727.00",
8857            decimal_arr.value_as_string(0)
8858        );
8859        assert_eq!(
8860            "-170141183460469231731687303715884105728.00",
8861            decimal_arr.value_as_string(1)
8862        );
8863        assert_eq!(
8864            "99999999999999999999999999999999999999.00",
8865            decimal_arr.value_as_string(2)
8866        );
8867        assert_eq!(
8868            "999999999999999999999999999999999999.99",
8869            decimal_arr.value_as_string(3)
8870        );
8871        assert_eq!(
8872            "100000000000000000000000000000000000.00",
8873            decimal_arr.value_as_string(4)
8874        );
8875        assert!(decimal_arr.is_null(5));
8876        assert!(decimal_arr.is_null(6));
8877    }
8878
8879    #[test]
8880    fn test_cast_string_to_decimal256_precision_overflow() {
8881        let array = StringArray::from(vec!["1000".to_string()]);
8882        let array = Arc::new(array) as ArrayRef;
8883        let casted_array = cast_with_options(
8884            &array,
8885            &DataType::Decimal256(10, 8),
8886            &CastOptions {
8887                safe: true,
8888                format_options: FormatOptions::default(),
8889            },
8890        );
8891        assert!(casted_array.is_ok());
8892        assert!(casted_array.unwrap().is_null(0));
8893
8894        let err = cast_with_options(
8895            &array,
8896            &DataType::Decimal256(10, 8),
8897            &CastOptions {
8898                safe: false,
8899                format_options: FormatOptions::default(),
8900            },
8901        );
8902        assert_eq!("Invalid argument error: 100000000000 is too large to store in a Decimal256 of precision 10. Max is 9999999999", err.unwrap_err().to_string());
8903    }
8904
8905    #[test]
8906    fn test_cast_utf8_to_decimal256_overflow() {
8907        let overflow_str_array = StringArray::from(vec![
8908            i128::MAX.to_string(),
8909            i128::MIN.to_string(),
8910            "99999999999999999999999999999999999999".to_string(),
8911            "999999999999999999999999999999999999.99".to_string(),
8912            "99999999999999999999999999999999999.999".to_string(),
8913            i256::MAX.to_string(),
8914            i256::MIN.to_string(),
8915        ]);
8916        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
8917
8918        test_cast_string_to_decimal256_overflow(overflow_array);
8919    }
8920
8921    #[test]
8922    fn test_cast_large_utf8_to_decimal256_overflow() {
8923        let overflow_str_array = LargeStringArray::from(vec![
8924            i128::MAX.to_string(),
8925            i128::MIN.to_string(),
8926            "99999999999999999999999999999999999999".to_string(),
8927            "999999999999999999999999999999999999.99".to_string(),
8928            "99999999999999999999999999999999999.999".to_string(),
8929            i256::MAX.to_string(),
8930            i256::MIN.to_string(),
8931        ]);
8932        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
8933
8934        test_cast_string_to_decimal256_overflow(overflow_array);
8935    }
8936
8937    #[test]
8938    fn test_cast_outside_supported_range_for_nanoseconds() {
8939        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";
8940
8941        let array = StringArray::from(vec![Some("1650-01-01 01:01:01.000001")]);
8942
8943        let cast_options = CastOptions {
8944            safe: false,
8945            format_options: FormatOptions::default(),
8946        };
8947
8948        let result = cast_string_to_timestamp::<i32, TimestampNanosecondType>(
8949            &array,
8950            &None::<Arc<str>>,
8951            &cast_options,
8952        );
8953
8954        let err = result.unwrap_err();
8955        assert_eq!(
8956            err.to_string(),
8957            format!(
8958                "Cast error: Overflow converting {} to Nanosecond. {}",
8959                array.value(0),
8960                EXPECTED_ERROR_MESSAGE
8961            )
8962        );
8963    }
8964
8965    #[test]
8966    fn test_cast_date32_to_timestamp() {
8967        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
8968        let array = Arc::new(a) as ArrayRef;
8969        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
8970        let c = b.as_primitive::<TimestampSecondType>();
8971        assert_eq!(1609459200, c.value(0));
8972        assert_eq!(1640995200, c.value(1));
8973        assert!(c.is_null(2));
8974    }
8975
8976    #[test]
8977    fn test_cast_date32_to_timestamp_ms() {
8978        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
8979        let array = Arc::new(a) as ArrayRef;
8980        let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
8981        let c = b
8982            .as_any()
8983            .downcast_ref::<TimestampMillisecondArray>()
8984            .unwrap();
8985        assert_eq!(1609459200000, c.value(0));
8986        assert_eq!(1640995200000, c.value(1));
8987        assert!(c.is_null(2));
8988    }
8989
8990    #[test]
8991    fn test_cast_date32_to_timestamp_us() {
8992        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
8993        let array = Arc::new(a) as ArrayRef;
8994        let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
8995        let c = b
8996            .as_any()
8997            .downcast_ref::<TimestampMicrosecondArray>()
8998            .unwrap();
8999        assert_eq!(1609459200000000, c.value(0));
9000        assert_eq!(1640995200000000, c.value(1));
9001        assert!(c.is_null(2));
9002    }
9003
9004    #[test]
9005    fn test_cast_date32_to_timestamp_ns() {
9006        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
9007        let array = Arc::new(a) as ArrayRef;
9008        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
9009        let c = b
9010            .as_any()
9011            .downcast_ref::<TimestampNanosecondArray>()
9012            .unwrap();
9013        assert_eq!(1609459200000000000, c.value(0));
9014        assert_eq!(1640995200000000000, c.value(1));
9015        assert!(c.is_null(2));
9016    }
9017
9018    #[test]
9019    fn test_timezone_cast() {
9020        let a = StringArray::from(vec![
9021            "2000-01-01T12:00:00", // date + time valid
9022            "2020-12-15T12:34:56", // date + time valid
9023        ]);
9024        let array = Arc::new(a) as ArrayRef;
9025        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
9026        let v = b.as_primitive::<TimestampNanosecondType>();
9027
9028        assert_eq!(v.value(0), 946728000000000000);
9029        assert_eq!(v.value(1), 1608035696000000000);
9030
9031        let b = cast(
9032            &b,
9033            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
9034        )
9035        .unwrap();
9036        let v = b.as_primitive::<TimestampNanosecondType>();
9037
9038        assert_eq!(v.value(0), 946728000000000000);
9039        assert_eq!(v.value(1), 1608035696000000000);
9040
9041        let b = cast(
9042            &b,
9043            &DataType::Timestamp(TimeUnit::Millisecond, Some("+02:00".into())),
9044        )
9045        .unwrap();
9046        let v = b.as_primitive::<TimestampMillisecondType>();
9047
9048        assert_eq!(v.value(0), 946728000000);
9049        assert_eq!(v.value(1), 1608035696000);
9050    }
9051
9052    #[test]
9053    fn test_cast_utf8_to_timestamp() {
9054        fn test_tz(tz: Arc<str>) {
9055            let valid = StringArray::from(vec![
9056                "2023-01-01 04:05:06.789000-08:00",
9057                "2023-01-01 04:05:06.789000-07:00",
9058                "2023-01-01 04:05:06.789 -0800",
9059                "2023-01-01 04:05:06.789 -08:00",
9060                "2023-01-01 040506 +0730",
9061                "2023-01-01 040506 +07:30",
9062                "2023-01-01 04:05:06.789",
9063                "2023-01-01 04:05:06",
9064                "2023-01-01",
9065            ]);
9066
9067            let array = Arc::new(valid) as ArrayRef;
9068            let b = cast_with_options(
9069                &array,
9070                &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.clone())),
9071                &CastOptions {
9072                    safe: false,
9073                    format_options: FormatOptions::default(),
9074                },
9075            )
9076            .unwrap();
9077
9078            let tz = tz.as_ref().parse().unwrap();
9079
9080            let as_tz =
9081                |v: i64| as_datetime_with_timezone::<TimestampNanosecondType>(v, tz).unwrap();
9082
9083            let as_utc = |v: &i64| as_tz(*v).naive_utc().to_string();
9084            let as_local = |v: &i64| as_tz(*v).naive_local().to_string();
9085
9086            let values = b.as_primitive::<TimestampNanosecondType>().values();
9087            let utc_results: Vec<_> = values.iter().map(as_utc).collect();
9088            let local_results: Vec<_> = values.iter().map(as_local).collect();
9089
9090            // Absolute timestamps should be parsed preserving the same UTC instant
9091            assert_eq!(
9092                &utc_results[..6],
9093                &[
9094                    "2023-01-01 12:05:06.789".to_string(),
9095                    "2023-01-01 11:05:06.789".to_string(),
9096                    "2023-01-01 12:05:06.789".to_string(),
9097                    "2023-01-01 12:05:06.789".to_string(),
9098                    "2022-12-31 20:35:06".to_string(),
9099                    "2022-12-31 20:35:06".to_string(),
9100                ]
9101            );
9102            // Non-absolute timestamps should be parsed preserving the same local instant
9103            assert_eq!(
9104                &local_results[6..],
9105                &[
9106                    "2023-01-01 04:05:06.789".to_string(),
9107                    "2023-01-01 04:05:06".to_string(),
9108                    "2023-01-01 00:00:00".to_string()
9109                ]
9110            )
9111        }
9112
9113        test_tz("+00:00".into());
9114        test_tz("+02:00".into());
9115    }
9116
9117    #[test]
9118    fn test_cast_invalid_utf8() {
9119        let v1: &[u8] = b"\xFF invalid";
9120        let v2: &[u8] = b"\x00 Foo";
9121        let s = BinaryArray::from(vec![v1, v2]);
9122        let options = CastOptions {
9123            safe: true,
9124            format_options: FormatOptions::default(),
9125        };
9126        let array = cast_with_options(&s, &DataType::Utf8, &options).unwrap();
9127        let a = array.as_string::<i32>();
9128        a.to_data().validate_full().unwrap();
9129
9130        assert_eq!(a.null_count(), 1);
9131        assert_eq!(a.len(), 2);
9132        assert!(a.is_null(0));
9133        assert_eq!(a.value(0), "");
9134        assert_eq!(a.value(1), "\x00 Foo");
9135    }
9136
9137    #[test]
9138    fn test_cast_utf8_to_timestamptz() {
9139        let valid = StringArray::from(vec!["2023-01-01"]);
9140
9141        let array = Arc::new(valid) as ArrayRef;
9142        let b = cast(
9143            &array,
9144            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
9145        )
9146        .unwrap();
9147
9148        let expect = DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into()));
9149
9150        assert_eq!(b.data_type(), &expect);
9151        let c = b
9152            .as_any()
9153            .downcast_ref::<TimestampNanosecondArray>()
9154            .unwrap();
9155        assert_eq!(1672531200000000000, c.value(0));
9156    }
9157
9158    #[test]
9159    fn test_cast_decimal_to_string() {
9160        assert!(can_cast_types(
9161            &DataType::Decimal128(10, 4),
9162            &DataType::Utf8View
9163        ));
9164        assert!(can_cast_types(
9165            &DataType::Decimal256(38, 10),
9166            &DataType::Utf8View
9167        ));
9168
9169        macro_rules! assert_decimal_values {
9170            ($array:expr) => {
9171                let c = $array;
9172                assert_eq!("1123.454", c.value(0));
9173                assert_eq!("2123.456", c.value(1));
9174                assert_eq!("-3123.453", c.value(2));
9175                assert_eq!("-3123.456", c.value(3));
9176                assert_eq!("0.000", c.value(4));
9177                assert_eq!("0.123", c.value(5));
9178                assert_eq!("1234.567", c.value(6));
9179                assert_eq!("-1234.567", c.value(7));
9180                assert!(c.is_null(8));
9181            };
9182        }
9183
9184        fn test_decimal_to_string<IN: ArrowPrimitiveType, OffsetSize: OffsetSizeTrait>(
9185            output_type: DataType,
9186            array: PrimitiveArray<IN>,
9187        ) {
9188            let b = cast(&array, &output_type).unwrap();
9189
9190            assert_eq!(b.data_type(), &output_type);
9191            match b.data_type() {
9192                DataType::Utf8View => {
9193                    let c = b.as_string_view();
9194                    assert_decimal_values!(c);
9195                }
9196                DataType::Utf8 | DataType::LargeUtf8 => {
9197                    let c = b.as_string::<OffsetSize>();
9198                    assert_decimal_values!(c);
9199                }
9200                _ => (),
9201            }
9202        }
9203
9204        let array128: Vec<Option<i128>> = vec![
9205            Some(1123454),
9206            Some(2123456),
9207            Some(-3123453),
9208            Some(-3123456),
9209            Some(0),
9210            Some(123),
9211            Some(123456789),
9212            Some(-123456789),
9213            None,
9214        ];
9215        let array256: Vec<Option<i256>> = array128
9216            .iter()
9217            .map(|num| num.map(i256::from_i128))
9218            .collect();
9219
9220        test_decimal_to_string::<Decimal128Type, i32>(
9221            DataType::Utf8View,
9222            create_decimal128_array(array128.clone(), 7, 3).unwrap(),
9223        );
9224        test_decimal_to_string::<Decimal128Type, i32>(
9225            DataType::Utf8,
9226            create_decimal128_array(array128.clone(), 7, 3).unwrap(),
9227        );
9228        test_decimal_to_string::<Decimal128Type, i64>(
9229            DataType::LargeUtf8,
9230            create_decimal128_array(array128, 7, 3).unwrap(),
9231        );
9232
9233        test_decimal_to_string::<Decimal256Type, i32>(
9234            DataType::Utf8View,
9235            create_decimal256_array(array256.clone(), 7, 3).unwrap(),
9236        );
9237        test_decimal_to_string::<Decimal256Type, i32>(
9238            DataType::Utf8,
9239            create_decimal256_array(array256.clone(), 7, 3).unwrap(),
9240        );
9241        test_decimal_to_string::<Decimal256Type, i64>(
9242            DataType::LargeUtf8,
9243            create_decimal256_array(array256, 7, 3).unwrap(),
9244        );
9245    }
9246
9247    #[test]
9248    fn test_cast_numeric_to_decimal128_precision_overflow() {
9249        let array = Int64Array::from(vec![1234567]);
9250        let array = Arc::new(array) as ArrayRef;
9251        let casted_array = cast_with_options(
9252            &array,
9253            &DataType::Decimal128(7, 3),
9254            &CastOptions {
9255                safe: true,
9256                format_options: FormatOptions::default(),
9257            },
9258        );
9259        assert!(casted_array.is_ok());
9260        assert!(casted_array.unwrap().is_null(0));
9261
9262        let err = cast_with_options(
9263            &array,
9264            &DataType::Decimal128(7, 3),
9265            &CastOptions {
9266                safe: false,
9267                format_options: FormatOptions::default(),
9268            },
9269        );
9270        assert_eq!("Invalid argument error: 1234567000 is too large to store in a Decimal128 of precision 7. Max is 9999999", err.unwrap_err().to_string());
9271    }
9272
9273    #[test]
9274    fn test_cast_numeric_to_decimal256_precision_overflow() {
9275        let array = Int64Array::from(vec![1234567]);
9276        let array = Arc::new(array) as ArrayRef;
9277        let casted_array = cast_with_options(
9278            &array,
9279            &DataType::Decimal256(7, 3),
9280            &CastOptions {
9281                safe: true,
9282                format_options: FormatOptions::default(),
9283            },
9284        );
9285        assert!(casted_array.is_ok());
9286        assert!(casted_array.unwrap().is_null(0));
9287
9288        let err = cast_with_options(
9289            &array,
9290            &DataType::Decimal256(7, 3),
9291            &CastOptions {
9292                safe: false,
9293                format_options: FormatOptions::default(),
9294            },
9295        );
9296        assert_eq!("Invalid argument error: 1234567000 is too large to store in a Decimal256 of precision 7. Max is 9999999", err.unwrap_err().to_string());
9297    }
9298
9299    /// helper function to test casting from duration to interval
9300    fn cast_from_duration_to_interval<T: ArrowTemporalType<Native = i64>>(
9301        array: Vec<i64>,
9302        cast_options: &CastOptions,
9303    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
9304        let array = PrimitiveArray::<T>::new(array.into(), None);
9305        let array = Arc::new(array) as ArrayRef;
9306        let interval = DataType::Interval(IntervalUnit::MonthDayNano);
9307        let out = cast_with_options(&array, &interval, cast_options)?;
9308        let out = out.as_primitive::<IntervalMonthDayNanoType>().clone();
9309        Ok(out)
9310    }
9311
9312    #[test]
9313    fn test_cast_from_duration_to_interval() {
9314        // from duration second to interval month day nano
9315        let array = vec![1234567];
9316        let casted_array =
9317            cast_from_duration_to_interval::<DurationSecondType>(array, &CastOptions::default())
9318                .unwrap();
9319        assert_eq!(
9320            casted_array.data_type(),
9321            &DataType::Interval(IntervalUnit::MonthDayNano)
9322        );
9323        assert_eq!(
9324            casted_array.value(0),
9325            IntervalMonthDayNano::new(0, 0, 1234567000000000)
9326        );
9327
9328        let array = vec![i64::MAX];
9329        let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
9330            array.clone(),
9331            &CastOptions::default(),
9332        )
9333        .unwrap();
9334        assert!(!casted_array.is_valid(0));
9335
9336        let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
9337            array,
9338            &CastOptions {
9339                safe: false,
9340                format_options: FormatOptions::default(),
9341            },
9342        );
9343        assert!(casted_array.is_err());
9344
9345        // from duration millisecond to interval month day nano
9346        let array = vec![1234567];
9347        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
9348            array,
9349            &CastOptions::default(),
9350        )
9351        .unwrap();
9352        assert_eq!(
9353            casted_array.data_type(),
9354            &DataType::Interval(IntervalUnit::MonthDayNano)
9355        );
9356        assert_eq!(
9357            casted_array.value(0),
9358            IntervalMonthDayNano::new(0, 0, 1234567000000)
9359        );
9360
9361        let array = vec![i64::MAX];
9362        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
9363            array.clone(),
9364            &CastOptions::default(),
9365        )
9366        .unwrap();
9367        assert!(!casted_array.is_valid(0));
9368
9369        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
9370            array,
9371            &CastOptions {
9372                safe: false,
9373                format_options: FormatOptions::default(),
9374            },
9375        );
9376        assert!(casted_array.is_err());
9377
9378        // from duration microsecond to interval month day nano
9379        let array = vec![1234567];
9380        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
9381            array,
9382            &CastOptions::default(),
9383        )
9384        .unwrap();
9385        assert_eq!(
9386            casted_array.data_type(),
9387            &DataType::Interval(IntervalUnit::MonthDayNano)
9388        );
9389        assert_eq!(
9390            casted_array.value(0),
9391            IntervalMonthDayNano::new(0, 0, 1234567000)
9392        );
9393
9394        let array = vec![i64::MAX];
9395        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
9396            array.clone(),
9397            &CastOptions::default(),
9398        )
9399        .unwrap();
9400        assert!(!casted_array.is_valid(0));
9401
9402        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
9403            array,
9404            &CastOptions {
9405                safe: false,
9406                format_options: FormatOptions::default(),
9407            },
9408        );
9409        assert!(casted_array.is_err());
9410
9411        // from duration nanosecond to interval month day nano
9412        let array = vec![1234567];
9413        let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
9414            array,
9415            &CastOptions::default(),
9416        )
9417        .unwrap();
9418        assert_eq!(
9419            casted_array.data_type(),
9420            &DataType::Interval(IntervalUnit::MonthDayNano)
9421        );
9422        assert_eq!(
9423            casted_array.value(0),
9424            IntervalMonthDayNano::new(0, 0, 1234567)
9425        );
9426
9427        let array = vec![i64::MAX];
9428        let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
9429            array,
9430            &CastOptions {
9431                safe: false,
9432                format_options: FormatOptions::default(),
9433            },
9434        )
9435        .unwrap();
9436        assert_eq!(
9437            casted_array.value(0),
9438            IntervalMonthDayNano::new(0, 0, i64::MAX)
9439        );
9440    }
9441
9442    /// helper function to test casting from interval to duration
9443    fn cast_from_interval_to_duration<T: ArrowTemporalType>(
9444        array: &IntervalMonthDayNanoArray,
9445        cast_options: &CastOptions,
9446    ) -> Result<PrimitiveArray<T>, ArrowError> {
9447        let casted_array = cast_with_options(&array, &T::DATA_TYPE, cast_options)?;
9448        casted_array
9449            .as_any()
9450            .downcast_ref::<PrimitiveArray<T>>()
9451            .ok_or_else(|| {
9452                ArrowError::ComputeError(format!("Failed to downcast to {}", T::DATA_TYPE))
9453            })
9454            .cloned()
9455    }
9456
9457    #[test]
9458    fn test_cast_from_interval_to_duration() {
9459        let nullable = CastOptions::default();
9460        let fallible = CastOptions {
9461            safe: false,
9462            format_options: FormatOptions::default(),
9463        };
9464        let v = IntervalMonthDayNano::new(0, 0, 1234567);
9465
9466        // from interval month day nano to duration second
9467        let array = vec![v].into();
9468        let casted_array: DurationSecondArray =
9469            cast_from_interval_to_duration(&array, &nullable).unwrap();
9470        assert_eq!(casted_array.value(0), 0);
9471
9472        let array = vec![IntervalMonthDayNano::MAX].into();
9473        let casted_array: DurationSecondArray =
9474            cast_from_interval_to_duration(&array, &nullable).unwrap();
9475        assert!(!casted_array.is_valid(0));
9476
9477        let res = cast_from_interval_to_duration::<DurationSecondType>(&array, &fallible);
9478        assert!(res.is_err());
9479
9480        // from interval month day nano to duration millisecond
9481        let array = vec![v].into();
9482        let casted_array: DurationMillisecondArray =
9483            cast_from_interval_to_duration(&array, &nullable).unwrap();
9484        assert_eq!(casted_array.value(0), 1);
9485
9486        let array = vec![IntervalMonthDayNano::MAX].into();
9487        let casted_array: DurationMillisecondArray =
9488            cast_from_interval_to_duration(&array, &nullable).unwrap();
9489        assert!(!casted_array.is_valid(0));
9490
9491        let res = cast_from_interval_to_duration::<DurationMillisecondType>(&array, &fallible);
9492        assert!(res.is_err());
9493
9494        // from interval month day nano to duration microsecond
9495        let array = vec![v].into();
9496        let casted_array: DurationMicrosecondArray =
9497            cast_from_interval_to_duration(&array, &nullable).unwrap();
9498        assert_eq!(casted_array.value(0), 1234);
9499
9500        let array = vec![IntervalMonthDayNano::MAX].into();
9501        let casted_array =
9502            cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &nullable).unwrap();
9503        assert!(!casted_array.is_valid(0));
9504
9505        let casted_array =
9506            cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &fallible);
9507        assert!(casted_array.is_err());
9508
9509        // from interval month day nano to duration nanosecond
9510        let array = vec![v].into();
9511        let casted_array: DurationNanosecondArray =
9512            cast_from_interval_to_duration(&array, &nullable).unwrap();
9513        assert_eq!(casted_array.value(0), 1234567);
9514
9515        let array = vec![IntervalMonthDayNano::MAX].into();
9516        let casted_array: DurationNanosecondArray =
9517            cast_from_interval_to_duration(&array, &nullable).unwrap();
9518        assert!(!casted_array.is_valid(0));
9519
9520        let casted_array =
9521            cast_from_interval_to_duration::<DurationNanosecondType>(&array, &fallible);
9522        assert!(casted_array.is_err());
9523
9524        let array = vec![
9525            IntervalMonthDayNanoType::make_value(0, 1, 0),
9526            IntervalMonthDayNanoType::make_value(-1, 0, 0),
9527            IntervalMonthDayNanoType::make_value(1, 1, 0),
9528            IntervalMonthDayNanoType::make_value(1, 0, 1),
9529            IntervalMonthDayNanoType::make_value(0, 0, -1),
9530        ]
9531        .into();
9532        let casted_array =
9533            cast_from_interval_to_duration::<DurationNanosecondType>(&array, &nullable).unwrap();
9534        assert!(!casted_array.is_valid(0));
9535        assert!(!casted_array.is_valid(1));
9536        assert!(!casted_array.is_valid(2));
9537        assert!(!casted_array.is_valid(3));
9538        assert!(casted_array.is_valid(4));
9539        assert_eq!(casted_array.value(4), -1);
9540    }
9541
9542    /// helper function to test casting from interval year month to interval month day nano
9543    fn cast_from_interval_year_month_to_interval_month_day_nano(
9544        array: Vec<i32>,
9545        cast_options: &CastOptions,
9546    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
9547        let array = PrimitiveArray::<IntervalYearMonthType>::from(array);
9548        let array = Arc::new(array) as ArrayRef;
9549        let casted_array = cast_with_options(
9550            &array,
9551            &DataType::Interval(IntervalUnit::MonthDayNano),
9552            cast_options,
9553        )?;
9554        casted_array
9555            .as_any()
9556            .downcast_ref::<IntervalMonthDayNanoArray>()
9557            .ok_or_else(|| {
9558                ArrowError::ComputeError(
9559                    "Failed to downcast to IntervalMonthDayNanoArray".to_string(),
9560                )
9561            })
9562            .cloned()
9563    }
9564
9565    #[test]
9566    fn test_cast_from_interval_year_month_to_interval_month_day_nano() {
9567        // from interval year month to interval month day nano
9568        let array = vec![1234567];
9569        let casted_array = cast_from_interval_year_month_to_interval_month_day_nano(
9570            array,
9571            &CastOptions::default(),
9572        )
9573        .unwrap();
9574        assert_eq!(
9575            casted_array.data_type(),
9576            &DataType::Interval(IntervalUnit::MonthDayNano)
9577        );
9578        assert_eq!(
9579            casted_array.value(0),
9580            IntervalMonthDayNano::new(1234567, 0, 0)
9581        );
9582    }
9583
9584    /// helper function to test casting from interval day time to interval month day nano
9585    fn cast_from_interval_day_time_to_interval_month_day_nano(
9586        array: Vec<IntervalDayTime>,
9587        cast_options: &CastOptions,
9588    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
9589        let array = PrimitiveArray::<IntervalDayTimeType>::from(array);
9590        let array = Arc::new(array) as ArrayRef;
9591        let casted_array = cast_with_options(
9592            &array,
9593            &DataType::Interval(IntervalUnit::MonthDayNano),
9594            cast_options,
9595        )?;
9596        Ok(casted_array
9597            .as_primitive::<IntervalMonthDayNanoType>()
9598            .clone())
9599    }
9600
9601    #[test]
9602    fn test_cast_from_interval_day_time_to_interval_month_day_nano() {
9603        // from interval day time to interval month day nano
9604        let array = vec![IntervalDayTime::new(123, 0)];
9605        let casted_array =
9606            cast_from_interval_day_time_to_interval_month_day_nano(array, &CastOptions::default())
9607                .unwrap();
9608        assert_eq!(
9609            casted_array.data_type(),
9610            &DataType::Interval(IntervalUnit::MonthDayNano)
9611        );
9612        assert_eq!(casted_array.value(0), IntervalMonthDayNano::new(0, 123, 0));
9613    }
9614
9615    #[test]
9616    fn test_cast_below_unixtimestamp() {
9617        let valid = StringArray::from(vec![
9618            "1900-01-03 23:59:59",
9619            "1969-12-31 00:00:01",
9620            "1989-12-31 00:00:01",
9621        ]);
9622
9623        let array = Arc::new(valid) as ArrayRef;
9624        let casted_array = cast_with_options(
9625            &array,
9626            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
9627            &CastOptions {
9628                safe: false,
9629                format_options: FormatOptions::default(),
9630            },
9631        )
9632        .unwrap();
9633
9634        let ts_array = casted_array
9635            .as_primitive::<TimestampNanosecondType>()
9636            .values()
9637            .iter()
9638            .map(|ts| ts / 1_000_000)
9639            .collect::<Vec<_>>();
9640
9641        let array = TimestampMillisecondArray::from(ts_array).with_timezone("+00:00".to_string());
9642        let casted_array = cast(&array, &DataType::Date32).unwrap();
9643        let date_array = casted_array.as_primitive::<Date32Type>();
9644        let casted_array = cast(&date_array, &DataType::Utf8).unwrap();
9645        let string_array = casted_array.as_string::<i32>();
9646        assert_eq!("1900-01-03", string_array.value(0));
9647        assert_eq!("1969-12-31", string_array.value(1));
9648        assert_eq!("1989-12-31", string_array.value(2));
9649    }
9650
9651    #[test]
9652    fn test_nested_list() {
9653        let mut list = ListBuilder::new(Int32Builder::new());
9654        list.append_value([Some(1), Some(2), Some(3)]);
9655        list.append_value([Some(4), None, Some(6)]);
9656        let list = list.finish();
9657
9658        let to_field = Field::new("nested", list.data_type().clone(), false);
9659        let to = DataType::List(Arc::new(to_field));
9660        let out = cast(&list, &to).unwrap();
9661        let opts = FormatOptions::default().with_null("null");
9662        let formatted = ArrayFormatter::try_new(out.as_ref(), &opts).unwrap();
9663
9664        assert_eq!(formatted.value(0).to_string(), "[[1], [2], [3]]");
9665        assert_eq!(formatted.value(1).to_string(), "[[4], [null], [6]]");
9666    }
9667
9668    #[test]
9669    fn test_nested_list_cast() {
9670        let mut builder = ListBuilder::new(ListBuilder::new(Int32Builder::new()));
9671        builder.append_value([Some([Some(1), Some(2), None]), None]);
9672        builder.append_value([None, Some([]), None]);
9673        builder.append_null();
9674        builder.append_value([Some([Some(2), Some(3)])]);
9675        let start = builder.finish();
9676
9677        let mut builder = LargeListBuilder::new(LargeListBuilder::new(Int8Builder::new()));
9678        builder.append_value([Some([Some(1), Some(2), None]), None]);
9679        builder.append_value([None, Some([]), None]);
9680        builder.append_null();
9681        builder.append_value([Some([Some(2), Some(3)])]);
9682        let expected = builder.finish();
9683
9684        let actual = cast(&start, expected.data_type()).unwrap();
9685        assert_eq!(actual.as_ref(), &expected);
9686    }
9687
9688    const CAST_OPTIONS: CastOptions<'static> = CastOptions {
9689        safe: true,
9690        format_options: FormatOptions::new(),
9691    };
9692
9693    #[test]
9694    #[allow(clippy::assertions_on_constants)]
9695    fn test_const_options() {
9696        assert!(CAST_OPTIONS.safe)
9697    }
9698
9699    #[test]
9700    fn test_list_format_options() {
9701        let options = CastOptions {
9702            safe: false,
9703            format_options: FormatOptions::default().with_null("null"),
9704        };
9705        let array = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
9706            Some(vec![Some(0), Some(1), Some(2)]),
9707            Some(vec![Some(0), None, Some(2)]),
9708        ]);
9709        let a = cast_with_options(&array, &DataType::Utf8, &options).unwrap();
9710        let r: Vec<_> = a.as_string::<i32>().iter().flatten().collect();
9711        assert_eq!(r, &["[0, 1, 2]", "[0, null, 2]"]);
9712    }
9713    #[test]
9714    fn test_cast_string_to_timestamp_invalid_tz() {
9715        // content after Z should be ignored
9716        let bad_timestamp = "2023-12-05T21:58:10.45ZZTOP";
9717        let array = StringArray::from(vec![Some(bad_timestamp)]);
9718
9719        let data_types = [
9720            DataType::Timestamp(TimeUnit::Second, None),
9721            DataType::Timestamp(TimeUnit::Millisecond, None),
9722            DataType::Timestamp(TimeUnit::Microsecond, None),
9723            DataType::Timestamp(TimeUnit::Nanosecond, None),
9724        ];
9725
9726        let cast_options = CastOptions {
9727            safe: false,
9728            ..Default::default()
9729        };
9730
9731        for dt in data_types {
9732            assert_eq!(
9733                cast_with_options(&array, &dt, &cast_options)
9734                    .unwrap_err()
9735                    .to_string(),
9736                "Parser error: Invalid timezone \"ZZTOP\": only offset based timezones supported without chrono-tz feature"
9737            );
9738        }
9739    }
9740    #[test]
9741    fn test_cast_struct_to_struct() {
9742        let struct_type = DataType::Struct(
9743            vec![
9744                Field::new("a", DataType::Boolean, false),
9745                Field::new("b", DataType::Int32, false),
9746            ]
9747            .into(),
9748        );
9749        let to_type = DataType::Struct(
9750            vec![
9751                Field::new("a", DataType::Utf8, false),
9752                Field::new("b", DataType::Utf8, false),
9753            ]
9754            .into(),
9755        );
9756        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
9757        let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
9758        let struct_array = StructArray::from(vec![
9759            (
9760                Arc::new(Field::new("b", DataType::Boolean, false)),
9761                boolean.clone() as ArrayRef,
9762            ),
9763            (
9764                Arc::new(Field::new("c", DataType::Int32, false)),
9765                int.clone() as ArrayRef,
9766            ),
9767        ]);
9768        let casted_array = cast(&struct_array, &to_type).unwrap();
9769        let casted_array = casted_array.as_struct();
9770        assert_eq!(casted_array.data_type(), &to_type);
9771        let casted_boolean_array = casted_array
9772            .column(0)
9773            .as_string::<i32>()
9774            .into_iter()
9775            .flatten()
9776            .collect::<Vec<_>>();
9777        let casted_int_array = casted_array
9778            .column(1)
9779            .as_string::<i32>()
9780            .into_iter()
9781            .flatten()
9782            .collect::<Vec<_>>();
9783        assert_eq!(casted_boolean_array, vec!["false", "false", "true", "true"]);
9784        assert_eq!(casted_int_array, vec!["42", "28", "19", "31"]);
9785
9786        // test for can't cast
9787        let to_type = DataType::Struct(
9788            vec![
9789                Field::new("a", DataType::Date32, false),
9790                Field::new("b", DataType::Utf8, false),
9791            ]
9792            .into(),
9793        );
9794        assert!(!can_cast_types(&struct_type, &to_type));
9795        let result = cast(&struct_array, &to_type);
9796        assert_eq!(
9797            "Cast error: Casting from Boolean to Date32 not supported",
9798            result.unwrap_err().to_string()
9799        );
9800    }
9801
9802    #[test]
9803    fn test_cast_struct_to_struct_nullability() {
9804        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
9805        let int = Arc::new(Int32Array::from(vec![Some(42), None, Some(19), None]));
9806        let struct_array = StructArray::from(vec![
9807            (
9808                Arc::new(Field::new("b", DataType::Boolean, false)),
9809                boolean.clone() as ArrayRef,
9810            ),
9811            (
9812                Arc::new(Field::new("c", DataType::Int32, true)),
9813                int.clone() as ArrayRef,
9814            ),
9815        ]);
9816
9817        // okay: nullable to nullable
9818        let to_type = DataType::Struct(
9819            vec![
9820                Field::new("a", DataType::Utf8, false),
9821                Field::new("b", DataType::Utf8, true),
9822            ]
9823            .into(),
9824        );
9825        cast(&struct_array, &to_type).expect("Cast nullable to nullable struct field should work");
9826
9827        // error: nullable to non-nullable
9828        let to_type = DataType::Struct(
9829            vec![
9830                Field::new("a", DataType::Utf8, false),
9831                Field::new("b", DataType::Utf8, false),
9832            ]
9833            .into(),
9834        );
9835        cast(&struct_array, &to_type)
9836            .expect_err("Cast nullable to non-nullable struct field should fail");
9837
9838        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
9839        let int = Arc::new(Int32Array::from(vec![i32::MAX, 25, 1, 100]));
9840        let struct_array = StructArray::from(vec![
9841            (
9842                Arc::new(Field::new("b", DataType::Boolean, false)),
9843                boolean.clone() as ArrayRef,
9844            ),
9845            (
9846                Arc::new(Field::new("c", DataType::Int32, false)),
9847                int.clone() as ArrayRef,
9848            ),
9849        ]);
9850
9851        // okay: non-nullable to non-nullable
9852        let to_type = DataType::Struct(
9853            vec![
9854                Field::new("a", DataType::Utf8, false),
9855                Field::new("b", DataType::Utf8, false),
9856            ]
9857            .into(),
9858        );
9859        cast(&struct_array, &to_type)
9860            .expect("Cast non-nullable to non-nullable struct field should work");
9861
9862        // err: non-nullable to non-nullable but overflowing return null during casting
9863        let to_type = DataType::Struct(
9864            vec![
9865                Field::new("a", DataType::Utf8, false),
9866                Field::new("b", DataType::Int8, false),
9867            ]
9868            .into(),
9869        );
9870        cast(&struct_array, &to_type).expect_err(
9871            "Cast non-nullable to non-nullable struct field returning null should fail",
9872        );
9873    }
9874
9875    #[test]
9876    fn test_cast_struct_to_non_struct() {
9877        let boolean = Arc::new(BooleanArray::from(vec![true, false]));
9878        let struct_array = StructArray::from(vec![(
9879            Arc::new(Field::new("a", DataType::Boolean, false)),
9880            boolean.clone() as ArrayRef,
9881        )]);
9882        let to_type = DataType::Utf8;
9883        let result = cast(&struct_array, &to_type);
9884        assert_eq!(
9885            r#"Cast error: Casting from Struct([Field { name: "a", data_type: Boolean, nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }]) to Utf8 not supported"#,
9886            result.unwrap_err().to_string()
9887        );
9888    }
9889
9890    #[test]
9891    fn test_cast_non_struct_to_struct() {
9892        let array = StringArray::from(vec!["a", "b"]);
9893        let to_type = DataType::Struct(vec![Field::new("a", DataType::Boolean, false)].into());
9894        let result = cast(&array, &to_type);
9895        assert_eq!(
9896            r#"Cast error: Casting from Utf8 to Struct([Field { name: "a", data_type: Boolean, nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }]) not supported"#,
9897            result.unwrap_err().to_string()
9898        );
9899    }
9900
9901    #[test]
9902    fn test_decimal_to_decimal_throw_error_on_precision_overflow_same_scale() {
9903        let array = vec![Some(123456789)];
9904        let array = create_decimal128_array(array, 24, 2).unwrap();
9905        let input_type = DataType::Decimal128(24, 2);
9906        let output_type = DataType::Decimal128(6, 2);
9907        assert!(can_cast_types(&input_type, &output_type));
9908
9909        let options = CastOptions {
9910            safe: false,
9911            ..Default::default()
9912        };
9913        let result = cast_with_options(&array, &output_type, &options);
9914        assert_eq!(result.unwrap_err().to_string(),
9915                   "Invalid argument error: 123456789 is too large to store in a Decimal128 of precision 6. Max is 999999");
9916    }
9917
9918    #[test]
9919    fn test_decimal_to_decimal_same_scale() {
9920        let array = vec![Some(520)];
9921        let array = create_decimal128_array(array, 4, 2).unwrap();
9922        let input_type = DataType::Decimal128(4, 2);
9923        let output_type = DataType::Decimal128(3, 2);
9924        assert!(can_cast_types(&input_type, &output_type));
9925
9926        let options = CastOptions {
9927            safe: false,
9928            ..Default::default()
9929        };
9930        let result = cast_with_options(&array, &output_type, &options);
9931        assert_eq!(
9932            result.unwrap().as_primitive::<Decimal128Type>().value(0),
9933            520
9934        );
9935
9936        // Cast 0 of decimal(3, 0) type to decimal(2, 0)
9937        assert_eq!(
9938            &cast(
9939                &create_decimal128_array(vec![Some(0)], 3, 0).unwrap(),
9940                &DataType::Decimal128(2, 0)
9941            )
9942            .unwrap(),
9943            &(Arc::new(create_decimal128_array(vec![Some(0)], 2, 0).unwrap()) as ArrayRef)
9944        );
9945    }
9946
9947    #[test]
9948    fn test_decimal_to_decimal_throw_error_on_precision_overflow_lower_scale() {
9949        let array = vec![Some(123456789)];
9950        let array = create_decimal128_array(array, 24, 4).unwrap();
9951        let input_type = DataType::Decimal128(24, 4);
9952        let output_type = DataType::Decimal128(6, 2);
9953        assert!(can_cast_types(&input_type, &output_type));
9954
9955        let options = CastOptions {
9956            safe: false,
9957            ..Default::default()
9958        };
9959        let result = cast_with_options(&array, &output_type, &options);
9960        assert_eq!(result.unwrap_err().to_string(),
9961                   "Invalid argument error: 1234568 is too large to store in a Decimal128 of precision 6. Max is 999999");
9962    }
9963
9964    #[test]
9965    fn test_decimal_to_decimal_throw_error_on_precision_overflow_greater_scale() {
9966        let array = vec![Some(123456789)];
9967        let array = create_decimal128_array(array, 24, 2).unwrap();
9968        let input_type = DataType::Decimal128(24, 2);
9969        let output_type = DataType::Decimal128(6, 3);
9970        assert!(can_cast_types(&input_type, &output_type));
9971
9972        let options = CastOptions {
9973            safe: false,
9974            ..Default::default()
9975        };
9976        let result = cast_with_options(&array, &output_type, &options);
9977        assert_eq!(result.unwrap_err().to_string(),
9978                   "Invalid argument error: 1234567890 is too large to store in a Decimal128 of precision 6. Max is 999999");
9979    }
9980
9981    #[test]
9982    fn test_decimal_to_decimal_throw_error_on_precision_overflow_diff_type() {
9983        let array = vec![Some(123456789)];
9984        let array = create_decimal128_array(array, 24, 2).unwrap();
9985        let input_type = DataType::Decimal128(24, 2);
9986        let output_type = DataType::Decimal256(6, 2);
9987        assert!(can_cast_types(&input_type, &output_type));
9988
9989        let options = CastOptions {
9990            safe: false,
9991            ..Default::default()
9992        };
9993        let result = cast_with_options(&array, &output_type, &options);
9994        assert_eq!(result.unwrap_err().to_string(),
9995                   "Invalid argument error: 123456789 is too large to store in a Decimal256 of precision 6. Max is 999999");
9996    }
9997
9998    #[test]
9999    fn test_first_none() {
10000        let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
10001            None,
10002            Some(vec![Some(1), Some(2)]),
10003        ])) as ArrayRef;
10004        let data_type =
10005            DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
10006        let opt = CastOptions::default();
10007        let r = cast_with_options(&array, &data_type, &opt).unwrap();
10008
10009        let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
10010            vec![None, Some(vec![Some(1), Some(2)])],
10011            2,
10012        )) as ArrayRef;
10013        assert_eq!(*fixed_array, *r);
10014    }
10015
10016    #[test]
10017    fn test_first_last_none() {
10018        let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
10019            None,
10020            Some(vec![Some(1), Some(2)]),
10021            None,
10022        ])) as ArrayRef;
10023        let data_type =
10024            DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
10025        let opt = CastOptions::default();
10026        let r = cast_with_options(&array, &data_type, &opt).unwrap();
10027
10028        let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
10029            vec![None, Some(vec![Some(1), Some(2)]), None],
10030            2,
10031        )) as ArrayRef;
10032        assert_eq!(*fixed_array, *r);
10033    }
10034}