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