1mod decimal;
41mod dictionary;
42mod list;
43mod map;
44mod run_array;
45mod string;
46mod structs;
47mod union;
48
49use crate::cast::decimal::*;
50use crate::cast::dictionary::*;
51use crate::cast::list::*;
52use crate::cast::map::*;
53use crate::cast::run_array::*;
54use crate::cast::string::*;
55use crate::cast::structs::*;
56pub use crate::cast::union::*;
57
58use arrow_buffer::IntervalMonthDayNano;
59use arrow_data::ByteView;
60use chrono::{NaiveTime, Offset, TimeZone, Utc};
61use std::cmp::Ordering;
62use std::sync::Arc;
63
64use crate::display::{ArrayFormatter, FormatOptions};
65use crate::parse::{
66 Parser, parse_interval_day_time, parse_interval_month_day_nano, parse_interval_year_month,
67 string_to_datetime,
68};
69use arrow_array::{builder::*, cast::*, temporal_conversions::*, timezone::Tz, types::*, *};
70use arrow_buffer::{ArrowNativeType, Buffer, OffsetBuffer, i256};
71use arrow_data::ArrayData;
72use arrow_data::transform::MutableArrayData;
73use arrow_schema::*;
74use arrow_select::take::take;
75use num_traits::{NumCast, ToPrimitive, cast::AsPrimitive};
76
77#[expect(deprecated)]
78pub use decimal::parse_string_to_decimal_native;
79pub use decimal::{DecimalCast, rescale_decimal, single_float_to_decimal};
80pub use string::cast_single_string_to_boolean_default;
81
82#[inline(always)]
88pub fn single_decimal_to_float_lossy<D, F>(f: &F, x: D::Native, scale: i32) -> f64
89where
90 D: DecimalType,
91 F: Fn(D::Native) -> f64,
92{
93 f(x) / 10_f64.powi(scale)
94}
95
96#[derive(Debug, Clone, PartialEq, Eq, Hash)]
98pub struct CastOptions<'a> {
99 pub safe: bool,
101 pub format_options: FormatOptions<'a>,
103}
104
105impl Default for CastOptions<'_> {
106 fn default() -> Self {
107 Self {
108 safe: true,
109 format_options: FormatOptions::default(),
110 }
111 }
112}
113
114pub fn can_cast_types(from_type: &DataType, to_type: &DataType) -> bool {
118 use self::DataType::*;
119 use self::IntervalUnit::*;
120 use self::TimeUnit::*;
121 if from_type == to_type {
122 return true;
123 }
124
125 match (from_type, to_type) {
126 (Null, _) => true,
127 (Dictionary(_, from_value_type), Dictionary(_, to_value_type)) => {
129 can_cast_types(from_value_type, to_value_type)
130 }
131 (Dictionary(_, value_type), _) => can_cast_types(value_type, to_type),
132 (Union(fields, _), _) => union::resolve_child_array(fields, to_type).is_some(),
133 (_, Union(_, _)) => false,
134 (RunEndEncoded(_, value_type), _) => can_cast_types(value_type.data_type(), to_type),
135 (_, RunEndEncoded(_, value_type)) => can_cast_types(from_type, value_type.data_type()),
136 (_, Dictionary(_, value_type)) => can_cast_types(from_type, value_type),
137 (
138 List(list_from) | LargeList(list_from) | ListView(list_from) | LargeListView(list_from),
139 List(list_to) | LargeList(list_to) | ListView(list_to) | LargeListView(list_to),
140 ) => can_cast_types(list_from.data_type(), list_to.data_type()),
141 (
142 List(list_from) | LargeList(list_from) | ListView(list_from) | LargeListView(list_from),
143 Utf8 | LargeUtf8 | Utf8View,
144 ) => can_cast_types(list_from.data_type(), to_type),
145 (
146 FixedSizeList(list_from, _),
147 List(list_to) | LargeList(list_to) | ListView(list_to) | LargeListView(list_to),
148 ) => can_cast_types(list_from.data_type(), list_to.data_type()),
149 (
150 List(list_from) | LargeList(list_from) | ListView(list_from) | LargeListView(list_from),
151 FixedSizeList(list_to, _),
152 ) => can_cast_types(list_from.data_type(), list_to.data_type()),
153 (FixedSizeList(inner, size), FixedSizeList(inner_to, size_to)) if size == size_to => {
154 can_cast_types(inner.data_type(), inner_to.data_type())
155 }
156 (_, List(list_to) | LargeList(list_to) | ListView(list_to) | LargeListView(list_to)) => {
157 can_cast_types(from_type, list_to.data_type())
158 }
159 (_, FixedSizeList(list_to, size)) if *size == 1 => {
160 can_cast_types(from_type, list_to.data_type())
161 }
162 (FixedSizeList(list_from, size), _) if *size == 1 => {
163 can_cast_types(list_from.data_type(), to_type)
164 }
165 (Map(from_entries, ordered_from), Map(to_entries, ordered_to))
166 if ordered_from == ordered_to =>
167 {
168 match (
169 key_field(from_entries),
170 key_field(to_entries),
171 value_field(from_entries),
172 value_field(to_entries),
173 ) {
174 (Some(from_key), Some(to_key), Some(from_value), Some(to_value)) => {
175 can_cast_types(from_key.data_type(), to_key.data_type())
176 && can_cast_types(from_value.data_type(), to_value.data_type())
177 }
178 _ => false,
179 }
180 }
181 (
183 Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
184 Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
185 ) => true,
186 (
188 UInt8 | UInt16 | UInt32 | UInt64,
189 Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
190 ) => true,
191 (
193 Int8 | Int16 | Int32 | Int64 | Float16 | Float32 | Float64,
194 Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
195 ) => true,
196 (
198 Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
199 UInt8 | UInt16 | UInt32 | UInt64,
200 ) => true,
201 (
203 Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
204 Null | Int8 | Int16 | Int32 | Int64 | Float16 | Float32 | Float64,
205 ) => true,
206 (
208 Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
209 Utf8View | Utf8 | LargeUtf8,
210 ) => true,
211 (
213 Utf8View | Utf8 | LargeUtf8,
214 Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
215 ) => true,
216 (Struct(from_fields), Struct(to_fields)) => {
217 if from_fields.len() != to_fields.len() {
218 return false;
219 }
220
221 if from_fields
223 .iter()
224 .zip(to_fields.iter())
225 .all(|(f1, f2)| f1.name() == f2.name())
226 {
227 return from_fields.iter().zip(to_fields.iter()).all(|(f1, f2)| {
228 can_cast_types(f1.data_type(), f2.data_type())
231 });
232 }
233
234 if to_fields.iter().all(|to_field| {
236 from_fields
237 .iter()
238 .find(|from_field| from_field.name() == to_field.name())
239 .is_some_and(|from_field| {
240 can_cast_types(from_field.data_type(), to_field.data_type())
243 })
244 }) {
245 return true;
246 }
247
248 from_fields
250 .iter()
251 .zip(to_fields.iter())
252 .all(|(f1, f2)| can_cast_types(f1.data_type(), f2.data_type()))
253 }
254 (Struct(_), _) => false,
255 (_, Struct(_)) => false,
256 (_, Boolean) => from_type.is_integer() || from_type.is_floating() || from_type.is_string(),
257 (Boolean, _) => to_type.is_integer() || to_type.is_floating() || to_type.is_string(),
258
259 (Binary, LargeBinary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView | Utf8View) => {
260 true
261 }
262 (LargeBinary, Binary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView | Utf8View) => {
263 true
264 }
265 (FixedSizeBinary(_), Binary | LargeBinary | BinaryView) => true,
266 (
267 Utf8 | LargeUtf8 | Utf8View,
268 Binary
269 | LargeBinary
270 | Utf8
271 | LargeUtf8
272 | Date32
273 | Date64
274 | Time32(Second | Millisecond)
275 | Time64(Microsecond | Nanosecond)
276 | Timestamp(Second | Millisecond | Microsecond | Nanosecond, _)
277 | Interval(_)
278 | BinaryView,
279 ) => true,
280 (Utf8 | LargeUtf8, Utf8View) => true,
281 (BinaryView, Binary | LargeBinary | Utf8 | LargeUtf8 | Utf8View) => true,
282 (Utf8View | Utf8 | LargeUtf8, _) => to_type.is_numeric(),
283 (_, Utf8 | Utf8View | LargeUtf8) => from_type.is_primitive(),
284
285 (_, Binary | LargeBinary) => from_type.is_integer(),
286
287 (
289 UInt8 | UInt16 | UInt32 | UInt64 | Int8 | Int16 | Int32 | Int64 | Float16 | Float32
290 | Float64,
291 UInt8 | UInt16 | UInt32 | UInt64 | Int8 | Int16 | Int32 | Int64 | Float16 | Float32
292 | Float64,
293 ) => true,
294 (Int32, Date32 | Date64 | Time32(_)) => true,
298 (Date32, Int32 | Int64) => true,
299 (Time32(_), Int32 | Int64) => true,
300 (Int64, Date64 | Date32 | Time64(_)) => true,
301 (Date64, Int64 | Int32) => true,
302 (Time64(_), Int64) => true,
303 (Date32 | Date64, Date32 | Date64) => true,
304 (Time32(_), Time32(_)) => true,
306 (Time32(_), Time64(_)) => true,
307 (Time64(_), Time64(_)) => true,
308 (Time64(_), Time32(to_unit)) => {
309 matches!(to_unit, Second | Millisecond)
310 }
311 (Timestamp(_, _), _) if to_type.is_numeric() => true,
312 (_, Timestamp(_, _)) if from_type.is_numeric() => true,
313 (Date64, Timestamp(_, _)) => true,
314 (Date32, Timestamp(_, _)) => true,
315 (
316 Timestamp(_, _),
317 Timestamp(_, _)
318 | Date32
319 | Date64
320 | Time32(Second | Millisecond)
321 | Time64(Microsecond | Nanosecond),
322 ) => true,
323 (_, Duration(_)) if from_type.is_numeric() => true,
324 (Duration(_), _) if to_type.is_numeric() => true,
325 (Duration(_), Duration(_)) => true,
326 (Int32, Interval(to_type)) => match to_type {
328 YearMonth => true,
329 DayTime => false,
330 MonthDayNano => false,
331 },
332 (Duration(_), Interval(MonthDayNano)) => true,
333 (Interval(MonthDayNano), Duration(_)) => true,
334 (Interval(YearMonth), Interval(MonthDayNano)) => true,
335 (Interval(DayTime), Interval(MonthDayNano)) => true,
336 (_, _) => false,
337 }
338}
339
340pub fn cast(array: &dyn Array, to_type: &DataType) -> Result<ArrayRef, ArrowError> {
344 cast_with_options(array, to_type, &CastOptions::default())
345}
346
347fn integer_to_decimal_native<I, M>(value: I) -> Option<M>
355where
356 I: Into<i128>,
357 M: DecimalCast,
358{
359 M::from_decimal(value.into())
360}
361
362fn cast_integer_to_decimal<
363 T: ArrowPrimitiveType,
364 D: DecimalType + ArrowPrimitiveType<Native = M>,
365 M,
366>(
367 array: &PrimitiveArray<T>,
368 precision: u8,
369 scale: i8,
370 base: M,
371 cast_options: &CastOptions,
372) -> Result<ArrayRef, ArrowError>
373where
374 <T as ArrowPrimitiveType>::Native: ArrowNativeTypeOp + Into<i128>,
375 M: ArrowNativeTypeOp + DecimalCast,
376{
377 let overflow = |v: T::Native| {
378 ArrowError::CastError(format!(
379 "Cannot cast to {}({precision}, {scale}). Overflowing on {v:?}",
380 D::PREFIX,
381 ))
382 };
383
384 let array = if scale < 0 {
385 let scale_factor = T::Native::usize_as(10)
389 .pow_checked(scale.unsigned_abs() as u32)
390 .ok();
391
392 match (scale_factor, cast_options.safe) {
393 (Some(scale_factor), true) => array.unary_opt::<_, D>(|v| {
394 let v = v
395 .div_checked(scale_factor)
396 .ok()
397 .and_then(integer_to_decimal_native::<_, M>)?;
398 (D::is_valid_decimal_precision(v, precision)).then_some(v)
399 }),
400 (Some(scale_factor), false) => array.try_unary::<_, D, _>(|v| {
401 let v = v
402 .div_checked(scale_factor)
403 .ok()
404 .and_then(integer_to_decimal_native::<_, M>)
405 .ok_or_else(|| overflow(v))?;
406 D::validate_decimal_precision(v, precision, scale).map(|()| v)
407 })?,
408 (None, _) => array.unary::<_, D>(|_| M::ZERO),
413 }
414 } else {
415 let scale_factor = base.pow_checked(scale.unsigned_abs() as u32).map_err(|_| {
416 ArrowError::CastError(format!(
417 "Cannot cast to {:?}({}, {}). The scale causes overflow.",
418 D::PREFIX,
419 precision,
420 scale,
421 ))
422 })?;
423
424 match cast_options.safe {
425 true => array.unary_opt::<_, D>(|v| {
426 let v = integer_to_decimal_native::<_, M>(v)
427 .and_then(|v| v.mul_checked(scale_factor).ok())?;
428 (D::is_valid_decimal_precision(v, precision)).then_some(v)
429 }),
430 false => array.try_unary::<_, D, _>(|v| {
431 let v = integer_to_decimal_native::<_, M>(v)
432 .ok_or_else(|| overflow(v))
433 .and_then(|v| v.mul_checked(scale_factor))?;
434 D::validate_decimal_precision(v, precision, scale).map(|()| v)
435 })?,
436 }
437 };
438
439 Ok(Arc::new(array.with_precision_and_scale(precision, scale)?))
440}
441
442fn cast_interval_year_month_to_interval_month_day_nano(
444 array: &dyn Array,
445 _cast_options: &CastOptions,
446) -> Result<ArrayRef, ArrowError> {
447 let array = array.as_primitive::<IntervalYearMonthType>();
448
449 Ok(Arc::new(array.unary::<_, IntervalMonthDayNanoType>(|v| {
450 let months = IntervalYearMonthType::to_months(v);
451 IntervalMonthDayNanoType::make_value(months, 0, 0)
452 })))
453}
454
455fn cast_interval_day_time_to_interval_month_day_nano(
457 array: &dyn Array,
458 _cast_options: &CastOptions,
459) -> Result<ArrayRef, ArrowError> {
460 let array = array.as_primitive::<IntervalDayTimeType>();
461 let mul = 1_000_000;
462
463 Ok(Arc::new(array.unary::<_, IntervalMonthDayNanoType>(|v| {
464 let (days, ms) = IntervalDayTimeType::to_parts(v);
465 IntervalMonthDayNanoType::make_value(0, days, ms as i64 * mul)
466 })))
467}
468
469fn cast_month_day_nano_to_duration<D: ArrowTemporalType<Native = i64>>(
471 array: &dyn Array,
472 cast_options: &CastOptions,
473) -> Result<ArrayRef, ArrowError> {
474 let array = array.as_primitive::<IntervalMonthDayNanoType>();
475 let scale = match D::DATA_TYPE {
476 DataType::Duration(TimeUnit::Second) => 1_000_000_000,
477 DataType::Duration(TimeUnit::Millisecond) => 1_000_000,
478 DataType::Duration(TimeUnit::Microsecond) => 1_000,
479 DataType::Duration(TimeUnit::Nanosecond) => 1,
480 _ => unreachable!(),
481 };
482
483 if cast_options.safe {
484 let iter = array.iter().map(|v| {
485 let v = v?;
486 (v.days == 0 && v.months == 0).then_some(v.nanoseconds / scale)
487 });
488 Ok(Arc::new(unsafe {
489 PrimitiveArray::<D>::from_trusted_len_iter(iter)
490 }))
491 } else {
492 let vec = array
493 .iter()
494 .map(|v| {
495 v.map(|v| match v.days == 0 && v.months == 0 {
496 true => Ok((v.nanoseconds) / scale),
497 _ => Err(ArrowError::ComputeError(
498 "Cannot convert interval containing non-zero months or days to duration"
499 .to_string(),
500 )),
501 })
502 .transpose()
503 })
504 .collect::<Result<Vec<_>, _>>()?;
505 Ok(Arc::new(unsafe {
506 PrimitiveArray::<D>::from_trusted_len_iter(vec.iter())
507 }))
508 }
509}
510
511fn cast_duration_to_interval<D: ArrowTemporalType<Native = i64>>(
513 array: &dyn Array,
514 cast_options: &CastOptions,
515) -> Result<ArrayRef, ArrowError> {
516 let array = array
517 .as_any()
518 .downcast_ref::<PrimitiveArray<D>>()
519 .ok_or_else(|| {
520 ArrowError::ComputeError(
521 "Internal Error: Cannot cast duration to DurationArray of expected type"
522 .to_string(),
523 )
524 })?;
525
526 let scale = match array.data_type() {
527 DataType::Duration(TimeUnit::Second) => 1_000_000_000,
528 DataType::Duration(TimeUnit::Millisecond) => 1_000_000,
529 DataType::Duration(TimeUnit::Microsecond) => 1_000,
530 DataType::Duration(TimeUnit::Nanosecond) => 1,
531 _ => unreachable!(),
532 };
533
534 if cast_options.safe {
535 let iter = array.iter().map(|v| {
536 v?.checked_mul(scale)
537 .map(|v| IntervalMonthDayNano::new(0, 0, v))
538 });
539 Ok(Arc::new(unsafe {
540 PrimitiveArray::<IntervalMonthDayNanoType>::from_trusted_len_iter(iter)
541 }))
542 } else {
543 let vec = array
544 .iter()
545 .map(|v| {
546 v.map(|v| {
547 if let Ok(v) = v.mul_checked(scale) {
548 Ok(IntervalMonthDayNano::new(0, 0, v))
549 } else {
550 Err(ArrowError::ComputeError(format!(
551 "Cannot cast to {:?}. Overflowing on {:?}",
552 IntervalMonthDayNanoType::DATA_TYPE,
553 v
554 )))
555 }
556 })
557 .transpose()
558 })
559 .collect::<Result<Vec<_>, _>>()?;
560 Ok(Arc::new(unsafe {
561 PrimitiveArray::<IntervalMonthDayNanoType>::from_trusted_len_iter(vec.iter())
562 }))
563 }
564}
565
566fn cast_reinterpret_arrays<I: ArrowPrimitiveType, O: ArrowPrimitiveType<Native = I::Native>>(
568 array: &dyn Array,
569) -> Result<ArrayRef, ArrowError> {
570 Ok(Arc::new(array.as_primitive::<I>().reinterpret_cast::<O>()))
571}
572
573fn make_timestamp_array(
574 array: &PrimitiveArray<Int64Type>,
575 unit: TimeUnit,
576 tz: Option<Arc<str>>,
577) -> ArrayRef {
578 match unit {
579 TimeUnit::Second => Arc::new(
580 array
581 .reinterpret_cast::<TimestampSecondType>()
582 .with_timezone_opt(tz),
583 ),
584 TimeUnit::Millisecond => Arc::new(
585 array
586 .reinterpret_cast::<TimestampMillisecondType>()
587 .with_timezone_opt(tz),
588 ),
589 TimeUnit::Microsecond => Arc::new(
590 array
591 .reinterpret_cast::<TimestampMicrosecondType>()
592 .with_timezone_opt(tz),
593 ),
594 TimeUnit::Nanosecond => Arc::new(
595 array
596 .reinterpret_cast::<TimestampNanosecondType>()
597 .with_timezone_opt(tz),
598 ),
599 }
600}
601
602fn make_duration_array(array: &PrimitiveArray<Int64Type>, unit: TimeUnit) -> ArrayRef {
603 match unit {
604 TimeUnit::Second => Arc::new(array.reinterpret_cast::<DurationSecondType>()),
605 TimeUnit::Millisecond => Arc::new(array.reinterpret_cast::<DurationMillisecondType>()),
606 TimeUnit::Microsecond => Arc::new(array.reinterpret_cast::<DurationMicrosecondType>()),
607 TimeUnit::Nanosecond => Arc::new(array.reinterpret_cast::<DurationNanosecondType>()),
608 }
609}
610
611fn as_time_res_with_timezone<T: ArrowPrimitiveType>(
612 v: i64,
613 tz: Option<Tz>,
614) -> Result<NaiveTime, ArrowError> {
615 let time = match tz {
616 Some(tz) => as_datetime_with_timezone::<T>(v, tz).map(|d| d.time()),
617 None => as_datetime::<T>(v).map(|d| d.time()),
618 };
619
620 time.ok_or_else(|| {
621 ArrowError::CastError(format!(
622 "Failed to create naive time with {} {}",
623 std::any::type_name::<T>(),
624 v
625 ))
626 })
627}
628
629fn timestamp_to_date32<T: ArrowTimestampType>(
630 array: &PrimitiveArray<T>,
631) -> Result<ArrayRef, ArrowError> {
632 let err = |x: i64| {
633 ArrowError::CastError(format!(
634 "Cannot convert {} {x} to datetime",
635 std::any::type_name::<T>()
636 ))
637 };
638
639 let array: Date32Array = match array.timezone() {
640 Some(tz) => {
641 let tz: Tz = tz.parse()?;
642 array.try_unary(|x| {
643 as_datetime_with_timezone::<T>(x, tz)
644 .ok_or_else(|| err(x))
645 .map(|d| Date32Type::from_naive_date(d.date_naive()))
646 })?
647 }
648 None => array.try_unary(|x| {
649 as_datetime::<T>(x)
650 .ok_or_else(|| err(x))
651 .map(|d| Date32Type::from_naive_date(d.date()))
652 })?,
653 };
654 Ok(Arc::new(array))
655}
656
657pub fn cast_with_options(
787 array: &dyn Array,
788 to_type: &DataType,
789 cast_options: &CastOptions,
790) -> Result<ArrayRef, ArrowError> {
791 use DataType::*;
792 let from_type = array.data_type();
793 if from_type == to_type {
795 return Ok(make_array(array.to_data()));
796 }
797 match (from_type, to_type) {
798 (Null, _) => Ok(new_null_array(to_type, array.len())),
799 (RunEndEncoded(index_type, _), _) => match index_type.data_type() {
800 Int16 => run_end_encoded_cast::<Int16Type>(array, to_type, cast_options),
801 Int32 => run_end_encoded_cast::<Int32Type>(array, to_type, cast_options),
802 Int64 => run_end_encoded_cast::<Int64Type>(array, to_type, cast_options),
803 _ => Err(ArrowError::CastError(format!(
804 "Casting from run end encoded type {from_type:?} to {to_type:?} not supported",
805 ))),
806 },
807 (_, RunEndEncoded(index_type, value_type)) => {
808 let array_ref = make_array(array.to_data());
809 match index_type.data_type() {
810 Int16 => cast_to_run_end_encoded::<Int16Type>(
811 &array_ref,
812 value_type.data_type(),
813 cast_options,
814 ),
815 Int32 => cast_to_run_end_encoded::<Int32Type>(
816 &array_ref,
817 value_type.data_type(),
818 cast_options,
819 ),
820 Int64 => cast_to_run_end_encoded::<Int64Type>(
821 &array_ref,
822 value_type.data_type(),
823 cast_options,
824 ),
825 _ => Err(ArrowError::CastError(format!(
826 "Casting from type {from_type:?} to run end encoded type {to_type:?} not supported",
827 ))),
828 }
829 }
830 (Union(_, _), _) => union_extract_by_type(
831 array.as_any().downcast_ref::<UnionArray>().unwrap(),
832 to_type,
833 cast_options,
834 ),
835 (_, Union(_, _)) => Err(ArrowError::CastError(format!(
836 "Casting from {from_type} to {to_type} not supported"
837 ))),
838 (Dictionary(index_type, _), _) => match **index_type {
839 Int8 => dictionary_cast::<Int8Type>(array, to_type, cast_options),
840 Int16 => dictionary_cast::<Int16Type>(array, to_type, cast_options),
841 Int32 => dictionary_cast::<Int32Type>(array, to_type, cast_options),
842 Int64 => dictionary_cast::<Int64Type>(array, to_type, cast_options),
843 UInt8 => dictionary_cast::<UInt8Type>(array, to_type, cast_options),
844 UInt16 => dictionary_cast::<UInt16Type>(array, to_type, cast_options),
845 UInt32 => dictionary_cast::<UInt32Type>(array, to_type, cast_options),
846 UInt64 => dictionary_cast::<UInt64Type>(array, to_type, cast_options),
847 _ => Err(ArrowError::CastError(format!(
848 "Casting from dictionary type {from_type} to {to_type} not supported",
849 ))),
850 },
851 (_, Dictionary(index_type, value_type)) => match **index_type {
852 Int8 => cast_to_dictionary::<Int8Type>(array, value_type, cast_options),
853 Int16 => cast_to_dictionary::<Int16Type>(array, value_type, cast_options),
854 Int32 => cast_to_dictionary::<Int32Type>(array, value_type, cast_options),
855 Int64 => cast_to_dictionary::<Int64Type>(array, value_type, cast_options),
856 UInt8 => cast_to_dictionary::<UInt8Type>(array, value_type, cast_options),
857 UInt16 => cast_to_dictionary::<UInt16Type>(array, value_type, cast_options),
858 UInt32 => cast_to_dictionary::<UInt32Type>(array, value_type, cast_options),
859 UInt64 => cast_to_dictionary::<UInt64Type>(array, value_type, cast_options),
860 _ => Err(ArrowError::CastError(format!(
861 "Casting from type {from_type} to dictionary type {to_type} not supported",
862 ))),
863 },
864 (List(_), List(to)) => cast_list_values::<i32>(array, to, cast_options),
866 (LargeList(_), LargeList(to)) => cast_list_values::<i64>(array, to, cast_options),
867 (FixedSizeList(_, size_from), FixedSizeList(list_to, size_to)) => {
868 if size_from != size_to {
869 return Err(ArrowError::CastError(
870 "cannot cast fixed-size-list to fixed-size-list with different size".into(),
871 ));
872 }
873 let array = array.as_fixed_size_list();
874 let values = cast_with_options(array.values(), list_to.data_type(), cast_options)?;
875 let nulls = array.nulls().cloned();
876 let len = array.len();
877 Ok(Arc::new(FixedSizeListArray::try_new_with_length(
878 list_to.clone(),
879 *size_from,
880 values,
881 nulls,
882 len,
883 )?))
884 }
885 (ListView(_), ListView(to)) => cast_list_view_values::<i32>(array, to, cast_options),
886 (LargeListView(_), LargeListView(to)) => {
887 cast_list_view_values::<i64>(array, to, cast_options)
888 }
889 (List(_), LargeList(list_to)) => cast_list::<i32, i64>(array, list_to, cast_options),
892 (List(_), FixedSizeList(field, size)) => {
893 cast_list_to_fixed_size_list::<i32>(array, field, *size, cast_options)
894 }
895 (List(_), ListView(list_to)) => {
896 cast_list_to_list_view::<i32, i32>(array, list_to, cast_options)
897 }
898 (List(_), LargeListView(list_to)) => {
899 cast_list_to_list_view::<i32, i64>(array, list_to, cast_options)
900 }
901 (LargeList(_), List(list_to)) => cast_list::<i64, i32>(array, list_to, cast_options),
903 (LargeList(_), FixedSizeList(field, size)) => {
904 cast_list_to_fixed_size_list::<i64>(array, field, *size, cast_options)
905 }
906 (LargeList(_), ListView(list_to)) => {
907 cast_list_to_list_view::<i64, i32>(array, list_to, cast_options)
908 }
909 (LargeList(_), LargeListView(list_to)) => {
910 cast_list_to_list_view::<i64, i64>(array, list_to, cast_options)
911 }
912 (ListView(_), List(list_to)) => {
914 cast_list_view_to_list::<i32, Int32Type>(array, list_to, cast_options)
915 }
916 (ListView(_), LargeList(list_to)) => {
917 cast_list_view_to_list::<i32, Int64Type>(array, list_to, cast_options)
918 }
919 (ListView(_), LargeListView(list_to)) => {
920 cast_list_view::<i32, i64>(array, list_to, cast_options)
921 }
922 (ListView(_), FixedSizeList(field, size)) => {
923 cast_list_view_to_fixed_size_list::<i32>(array, field, *size, cast_options)
924 }
925 (LargeListView(_), LargeList(list_to)) => {
927 cast_list_view_to_list::<i64, Int64Type>(array, list_to, cast_options)
928 }
929 (LargeListView(_), List(list_to)) => {
930 cast_list_view_to_list::<i64, Int32Type>(array, list_to, cast_options)
931 }
932 (LargeListView(_), ListView(list_to)) => {
933 cast_list_view::<i64, i32>(array, list_to, cast_options)
934 }
935 (LargeListView(_), FixedSizeList(field, size)) => {
936 cast_list_view_to_fixed_size_list::<i64>(array, field, *size, cast_options)
937 }
938 (FixedSizeList(_, _), List(list_to)) => {
940 cast_fixed_size_list_to_list::<i32>(array, list_to, cast_options)
941 }
942 (FixedSizeList(_, _), LargeList(list_to)) => {
943 cast_fixed_size_list_to_list::<i64>(array, list_to, cast_options)
944 }
945 (FixedSizeList(_, _), ListView(list_to)) => {
946 cast_fixed_size_list_to_list_view::<i32>(array, list_to, cast_options)
947 }
948 (FixedSizeList(_, _), LargeListView(list_to)) => {
949 cast_fixed_size_list_to_list_view::<i64>(array, list_to, cast_options)
950 }
951 (FixedSizeList(_, size), _) if *size == 1 => {
953 cast_single_element_fixed_size_list_to_values(array, to_type, cast_options)
954 }
955 (List(_) | LargeList(_) | ListView(_) | LargeListView(_), _) => match to_type {
958 Utf8 => value_to_string::<i32>(array, cast_options),
959 LargeUtf8 => value_to_string::<i64>(array, cast_options),
960 Utf8View => value_to_string_view(array, cast_options),
961 dt => Err(ArrowError::CastError(format!(
962 "Cannot cast LIST to non-list data type {dt}"
963 ))),
964 },
965 (_, List(to)) => cast_values_to_list::<i32>(array, to, cast_options),
966 (_, LargeList(to)) => cast_values_to_list::<i64>(array, to, cast_options),
967 (_, ListView(to)) => cast_values_to_list_view::<i32>(array, to, cast_options),
968 (_, LargeListView(to)) => cast_values_to_list_view::<i64>(array, to, cast_options),
969 (_, FixedSizeList(to, size)) if *size == 1 => {
970 let values = cast_with_options(array, to.data_type(), cast_options)?;
971 let list = FixedSizeListArray::try_new(to.clone(), 1, values, None)?;
972 Ok(Arc::new(list))
973 }
974 (Map(_, ordered1), Map(_, ordered2)) if ordered1 == ordered2 => {
976 cast_map_values(array.as_map(), to_type, cast_options, ordered1.to_owned())
977 }
978 (Decimal32(p1, s1), Decimal32(p2, s2)) => {
980 cast_decimal_to_decimal_same_type::<Decimal32Type>(
981 array.as_primitive(),
982 *p1,
983 *s1,
984 *p2,
985 *s2,
986 cast_options,
987 )
988 }
989 (Decimal64(p1, s1), Decimal64(p2, s2)) => {
990 cast_decimal_to_decimal_same_type::<Decimal64Type>(
991 array.as_primitive(),
992 *p1,
993 *s1,
994 *p2,
995 *s2,
996 cast_options,
997 )
998 }
999 (Decimal128(p1, s1), Decimal128(p2, s2)) => {
1000 cast_decimal_to_decimal_same_type::<Decimal128Type>(
1001 array.as_primitive(),
1002 *p1,
1003 *s1,
1004 *p2,
1005 *s2,
1006 cast_options,
1007 )
1008 }
1009 (Decimal256(p1, s1), Decimal256(p2, s2)) => {
1010 cast_decimal_to_decimal_same_type::<Decimal256Type>(
1011 array.as_primitive(),
1012 *p1,
1013 *s1,
1014 *p2,
1015 *s2,
1016 cast_options,
1017 )
1018 }
1019 (Decimal32(p1, s1), Decimal64(p2, s2)) => {
1021 cast_decimal_to_decimal::<Decimal32Type, Decimal64Type>(
1022 array.as_primitive(),
1023 *p1,
1024 *s1,
1025 *p2,
1026 *s2,
1027 cast_options,
1028 )
1029 }
1030 (Decimal32(p1, s1), Decimal128(p2, s2)) => {
1031 cast_decimal_to_decimal::<Decimal32Type, Decimal128Type>(
1032 array.as_primitive(),
1033 *p1,
1034 *s1,
1035 *p2,
1036 *s2,
1037 cast_options,
1038 )
1039 }
1040 (Decimal32(p1, s1), Decimal256(p2, s2)) => {
1041 cast_decimal_to_decimal::<Decimal32Type, Decimal256Type>(
1042 array.as_primitive(),
1043 *p1,
1044 *s1,
1045 *p2,
1046 *s2,
1047 cast_options,
1048 )
1049 }
1050 (Decimal64(p1, s1), Decimal32(p2, s2)) => {
1051 cast_decimal_to_decimal::<Decimal64Type, Decimal32Type>(
1052 array.as_primitive(),
1053 *p1,
1054 *s1,
1055 *p2,
1056 *s2,
1057 cast_options,
1058 )
1059 }
1060 (Decimal64(p1, s1), Decimal128(p2, s2)) => {
1061 cast_decimal_to_decimal::<Decimal64Type, Decimal128Type>(
1062 array.as_primitive(),
1063 *p1,
1064 *s1,
1065 *p2,
1066 *s2,
1067 cast_options,
1068 )
1069 }
1070 (Decimal64(p1, s1), Decimal256(p2, s2)) => {
1071 cast_decimal_to_decimal::<Decimal64Type, Decimal256Type>(
1072 array.as_primitive(),
1073 *p1,
1074 *s1,
1075 *p2,
1076 *s2,
1077 cast_options,
1078 )
1079 }
1080 (Decimal128(p1, s1), Decimal32(p2, s2)) => {
1081 cast_decimal_to_decimal::<Decimal128Type, Decimal32Type>(
1082 array.as_primitive(),
1083 *p1,
1084 *s1,
1085 *p2,
1086 *s2,
1087 cast_options,
1088 )
1089 }
1090 (Decimal128(p1, s1), Decimal64(p2, s2)) => {
1091 cast_decimal_to_decimal::<Decimal128Type, Decimal64Type>(
1092 array.as_primitive(),
1093 *p1,
1094 *s1,
1095 *p2,
1096 *s2,
1097 cast_options,
1098 )
1099 }
1100 (Decimal128(p1, s1), Decimal256(p2, s2)) => {
1101 cast_decimal_to_decimal::<Decimal128Type, Decimal256Type>(
1102 array.as_primitive(),
1103 *p1,
1104 *s1,
1105 *p2,
1106 *s2,
1107 cast_options,
1108 )
1109 }
1110 (Decimal256(p1, s1), Decimal32(p2, s2)) => {
1111 cast_decimal_to_decimal::<Decimal256Type, Decimal32Type>(
1112 array.as_primitive(),
1113 *p1,
1114 *s1,
1115 *p2,
1116 *s2,
1117 cast_options,
1118 )
1119 }
1120 (Decimal256(p1, s1), Decimal64(p2, s2)) => {
1121 cast_decimal_to_decimal::<Decimal256Type, Decimal64Type>(
1122 array.as_primitive(),
1123 *p1,
1124 *s1,
1125 *p2,
1126 *s2,
1127 cast_options,
1128 )
1129 }
1130 (Decimal256(p1, s1), Decimal128(p2, s2)) => {
1131 cast_decimal_to_decimal::<Decimal256Type, Decimal128Type>(
1132 array.as_primitive(),
1133 *p1,
1134 *s1,
1135 *p2,
1136 *s2,
1137 cast_options,
1138 )
1139 }
1140 (Decimal32(_, scale), _) if !to_type.is_temporal() => {
1142 cast_from_decimal::<Decimal32Type, _>(
1143 array,
1144 10_i32,
1145 scale,
1146 from_type,
1147 to_type,
1148 |x: i32| x as f64,
1149 cast_options,
1150 )
1151 }
1152 (Decimal64(_, scale), _) if !to_type.is_temporal() => {
1153 cast_from_decimal::<Decimal64Type, _>(
1154 array,
1155 10_i64,
1156 scale,
1157 from_type,
1158 to_type,
1159 |x: i64| x as f64,
1160 cast_options,
1161 )
1162 }
1163 (Decimal128(_, scale), _) if !to_type.is_temporal() => {
1164 cast_from_decimal::<Decimal128Type, _>(
1165 array,
1166 10_i128,
1167 scale,
1168 from_type,
1169 to_type,
1170 |x: i128| x as f64,
1171 cast_options,
1172 )
1173 }
1174 (Decimal256(_, scale), _) if !to_type.is_temporal() => {
1175 cast_from_decimal::<Decimal256Type, _>(
1176 array,
1177 i256::from_i128(10_i128),
1178 scale,
1179 from_type,
1180 to_type,
1181 |x: i256| x.to_f64().expect("All i256 values fit in f64"),
1182 cast_options,
1183 )
1184 }
1185 (_, Decimal32(precision, scale)) if !from_type.is_temporal() => {
1187 cast_to_decimal::<Decimal32Type, _>(
1188 array,
1189 10_i32,
1190 precision,
1191 scale,
1192 from_type,
1193 to_type,
1194 cast_options,
1195 )
1196 }
1197 (_, Decimal64(precision, scale)) if !from_type.is_temporal() => {
1198 cast_to_decimal::<Decimal64Type, _>(
1199 array,
1200 10_i64,
1201 precision,
1202 scale,
1203 from_type,
1204 to_type,
1205 cast_options,
1206 )
1207 }
1208 (_, Decimal128(precision, scale)) if !from_type.is_temporal() => {
1209 cast_to_decimal::<Decimal128Type, _>(
1210 array,
1211 10_i128,
1212 precision,
1213 scale,
1214 from_type,
1215 to_type,
1216 cast_options,
1217 )
1218 }
1219 (_, Decimal256(precision, scale)) if !from_type.is_temporal() => {
1220 cast_to_decimal::<Decimal256Type, _>(
1221 array,
1222 i256::from_i128(10_i128),
1223 precision,
1224 scale,
1225 from_type,
1226 to_type,
1227 cast_options,
1228 )
1229 }
1230 (Struct(from_fields), Struct(to_fields)) => cast_struct_to_struct(
1231 array.as_struct(),
1232 from_fields.clone(),
1233 to_fields.clone(),
1234 cast_options,
1235 ),
1236 (Struct(_), _) => Err(ArrowError::CastError(format!(
1237 "Casting from {from_type} to {to_type} not supported"
1238 ))),
1239 (_, Struct(_)) => Err(ArrowError::CastError(format!(
1240 "Casting from {from_type} to {to_type} not supported"
1241 ))),
1242 (_, Boolean) => match from_type {
1243 UInt8 => cast_numeric_to_bool::<UInt8Type>(array),
1244 UInt16 => cast_numeric_to_bool::<UInt16Type>(array),
1245 UInt32 => cast_numeric_to_bool::<UInt32Type>(array),
1246 UInt64 => cast_numeric_to_bool::<UInt64Type>(array),
1247 Int8 => cast_numeric_to_bool::<Int8Type>(array),
1248 Int16 => cast_numeric_to_bool::<Int16Type>(array),
1249 Int32 => cast_numeric_to_bool::<Int32Type>(array),
1250 Int64 => cast_numeric_to_bool::<Int64Type>(array),
1251 Float16 => cast_numeric_to_bool::<Float16Type>(array),
1252 Float32 => cast_numeric_to_bool::<Float32Type>(array),
1253 Float64 => cast_numeric_to_bool::<Float64Type>(array),
1254 Utf8View => cast_utf8view_to_boolean(array, cast_options),
1255 Utf8 => cast_utf8_to_boolean::<i32>(array, cast_options),
1256 LargeUtf8 => cast_utf8_to_boolean::<i64>(array, cast_options),
1257 _ => Err(ArrowError::CastError(format!(
1258 "Casting from {from_type} to {to_type} not supported",
1259 ))),
1260 },
1261 (Boolean, _) => match to_type {
1262 UInt8 => cast_bool_to_numeric::<UInt8Type>(array, cast_options),
1263 UInt16 => cast_bool_to_numeric::<UInt16Type>(array, cast_options),
1264 UInt32 => cast_bool_to_numeric::<UInt32Type>(array, cast_options),
1265 UInt64 => cast_bool_to_numeric::<UInt64Type>(array, cast_options),
1266 Int8 => cast_bool_to_numeric::<Int8Type>(array, cast_options),
1267 Int16 => cast_bool_to_numeric::<Int16Type>(array, cast_options),
1268 Int32 => cast_bool_to_numeric::<Int32Type>(array, cast_options),
1269 Int64 => cast_bool_to_numeric::<Int64Type>(array, cast_options),
1270 Float16 => cast_bool_to_numeric::<Float16Type>(array, cast_options),
1271 Float32 => cast_bool_to_numeric::<Float32Type>(array, cast_options),
1272 Float64 => cast_bool_to_numeric::<Float64Type>(array, cast_options),
1273 Utf8View => value_to_string_view(array, cast_options),
1274 Utf8 => value_to_string::<i32>(array, cast_options),
1275 LargeUtf8 => value_to_string::<i64>(array, cast_options),
1276 _ => Err(ArrowError::CastError(format!(
1277 "Casting from {from_type} to {to_type} not supported",
1278 ))),
1279 },
1280 (Utf8, _) => match to_type {
1281 UInt8 => parse_string::<UInt8Type, i32>(array, cast_options),
1282 UInt16 => parse_string::<UInt16Type, i32>(array, cast_options),
1283 UInt32 => parse_string::<UInt32Type, i32>(array, cast_options),
1284 UInt64 => parse_string::<UInt64Type, i32>(array, cast_options),
1285 Int8 => parse_string::<Int8Type, i32>(array, cast_options),
1286 Int16 => parse_string::<Int16Type, i32>(array, cast_options),
1287 Int32 => parse_string::<Int32Type, i32>(array, cast_options),
1288 Int64 => parse_string::<Int64Type, i32>(array, cast_options),
1289 Float16 => parse_string::<Float16Type, i32>(array, cast_options),
1290 Float32 => parse_string::<Float32Type, i32>(array, cast_options),
1291 Float64 => parse_string::<Float64Type, i32>(array, cast_options),
1292 Date32 => parse_string::<Date32Type, i32>(array, cast_options),
1293 Date64 => parse_string::<Date64Type, i32>(array, cast_options),
1294 Binary => Ok(Arc::new(BinaryArray::from(
1295 array.as_string::<i32>().clone(),
1296 ))),
1297 LargeBinary => {
1298 let binary = BinaryArray::from(array.as_string::<i32>().clone());
1299 cast_byte_container::<BinaryType, LargeBinaryType>(&binary)
1300 }
1301 Utf8View => Ok(Arc::new(StringViewArray::from(array.as_string::<i32>()))),
1302 BinaryView => Ok(Arc::new(
1303 StringViewArray::from(array.as_string::<i32>()).to_binary_view(),
1304 )),
1305 LargeUtf8 => cast_byte_container::<Utf8Type, LargeUtf8Type>(array),
1306 Time32(TimeUnit::Second) => parse_string::<Time32SecondType, i32>(array, cast_options),
1307 Time32(TimeUnit::Millisecond) => {
1308 parse_string::<Time32MillisecondType, i32>(array, cast_options)
1309 }
1310 Time64(TimeUnit::Microsecond) => {
1311 parse_string::<Time64MicrosecondType, i32>(array, cast_options)
1312 }
1313 Time64(TimeUnit::Nanosecond) => {
1314 parse_string::<Time64NanosecondType, i32>(array, cast_options)
1315 }
1316 Timestamp(TimeUnit::Second, to_tz) => cast_string_to_timestamp::<
1317 i32,
1318 TimestampSecondType,
1319 >(array, to_tz.as_ref(), cast_options),
1320 Timestamp(TimeUnit::Millisecond, to_tz) => cast_string_to_timestamp::<
1321 i32,
1322 TimestampMillisecondType,
1323 >(
1324 array, to_tz.as_ref(), cast_options
1325 ),
1326 Timestamp(TimeUnit::Microsecond, to_tz) => cast_string_to_timestamp::<
1327 i32,
1328 TimestampMicrosecondType,
1329 >(
1330 array, to_tz.as_ref(), cast_options
1331 ),
1332 Timestamp(TimeUnit::Nanosecond, to_tz) => cast_string_to_timestamp::<
1333 i32,
1334 TimestampNanosecondType,
1335 >(
1336 array, to_tz.as_ref(), cast_options
1337 ),
1338 Interval(IntervalUnit::YearMonth) => {
1339 cast_string_to_year_month_interval::<i32>(array, cast_options)
1340 }
1341 Interval(IntervalUnit::DayTime) => {
1342 cast_string_to_day_time_interval::<i32>(array, cast_options)
1343 }
1344 Interval(IntervalUnit::MonthDayNano) => {
1345 cast_string_to_month_day_nano_interval::<i32>(array, cast_options)
1346 }
1347 _ => Err(ArrowError::CastError(format!(
1348 "Casting from {from_type} to {to_type} not supported",
1349 ))),
1350 },
1351 (Utf8View, _) => match to_type {
1352 UInt8 => parse_string_view::<UInt8Type>(array, cast_options),
1353 UInt16 => parse_string_view::<UInt16Type>(array, cast_options),
1354 UInt32 => parse_string_view::<UInt32Type>(array, cast_options),
1355 UInt64 => parse_string_view::<UInt64Type>(array, cast_options),
1356 Int8 => parse_string_view::<Int8Type>(array, cast_options),
1357 Int16 => parse_string_view::<Int16Type>(array, cast_options),
1358 Int32 => parse_string_view::<Int32Type>(array, cast_options),
1359 Int64 => parse_string_view::<Int64Type>(array, cast_options),
1360 Float16 => parse_string_view::<Float16Type>(array, cast_options),
1361 Float32 => parse_string_view::<Float32Type>(array, cast_options),
1362 Float64 => parse_string_view::<Float64Type>(array, cast_options),
1363 Date32 => parse_string_view::<Date32Type>(array, cast_options),
1364 Date64 => parse_string_view::<Date64Type>(array, cast_options),
1365 Binary => cast_view_to_byte::<StringViewType, GenericBinaryType<i32>>(array),
1366 LargeBinary => cast_view_to_byte::<StringViewType, GenericBinaryType<i64>>(array),
1367 BinaryView => Ok(Arc::new(array.as_string_view().clone().to_binary_view())),
1368 Utf8 => cast_view_to_byte::<StringViewType, GenericStringType<i32>>(array),
1369 LargeUtf8 => cast_view_to_byte::<StringViewType, GenericStringType<i64>>(array),
1370 Time32(TimeUnit::Second) => parse_string_view::<Time32SecondType>(array, cast_options),
1371 Time32(TimeUnit::Millisecond) => {
1372 parse_string_view::<Time32MillisecondType>(array, cast_options)
1373 }
1374 Time64(TimeUnit::Microsecond) => {
1375 parse_string_view::<Time64MicrosecondType>(array, cast_options)
1376 }
1377 Time64(TimeUnit::Nanosecond) => {
1378 parse_string_view::<Time64NanosecondType>(array, cast_options)
1379 }
1380 Timestamp(TimeUnit::Second, to_tz) => {
1381 cast_view_to_timestamp::<TimestampSecondType>(array, to_tz.as_ref(), cast_options)
1382 }
1383 Timestamp(TimeUnit::Millisecond, to_tz) => cast_view_to_timestamp::<
1384 TimestampMillisecondType,
1385 >(
1386 array, to_tz.as_ref(), cast_options
1387 ),
1388 Timestamp(TimeUnit::Microsecond, to_tz) => cast_view_to_timestamp::<
1389 TimestampMicrosecondType,
1390 >(
1391 array, to_tz.as_ref(), cast_options
1392 ),
1393 Timestamp(TimeUnit::Nanosecond, to_tz) => cast_view_to_timestamp::<
1394 TimestampNanosecondType,
1395 >(
1396 array, to_tz.as_ref(), cast_options
1397 ),
1398 Interval(IntervalUnit::YearMonth) => {
1399 cast_view_to_year_month_interval(array, cast_options)
1400 }
1401 Interval(IntervalUnit::DayTime) => cast_view_to_day_time_interval(array, cast_options),
1402 Interval(IntervalUnit::MonthDayNano) => {
1403 cast_view_to_month_day_nano_interval(array, cast_options)
1404 }
1405 _ => Err(ArrowError::CastError(format!(
1406 "Casting from {from_type} to {to_type} not supported",
1407 ))),
1408 },
1409 (LargeUtf8, _) => match to_type {
1410 UInt8 => parse_string::<UInt8Type, i64>(array, cast_options),
1411 UInt16 => parse_string::<UInt16Type, i64>(array, cast_options),
1412 UInt32 => parse_string::<UInt32Type, i64>(array, cast_options),
1413 UInt64 => parse_string::<UInt64Type, i64>(array, cast_options),
1414 Int8 => parse_string::<Int8Type, i64>(array, cast_options),
1415 Int16 => parse_string::<Int16Type, i64>(array, cast_options),
1416 Int32 => parse_string::<Int32Type, i64>(array, cast_options),
1417 Int64 => parse_string::<Int64Type, i64>(array, cast_options),
1418 Float16 => parse_string::<Float16Type, i64>(array, cast_options),
1419 Float32 => parse_string::<Float32Type, i64>(array, cast_options),
1420 Float64 => parse_string::<Float64Type, i64>(array, cast_options),
1421 Date32 => parse_string::<Date32Type, i64>(array, cast_options),
1422 Date64 => parse_string::<Date64Type, i64>(array, cast_options),
1423 Utf8 => cast_byte_container::<LargeUtf8Type, Utf8Type>(array),
1424 Binary => {
1425 let large_binary = LargeBinaryArray::from(array.as_string::<i64>().clone());
1426 cast_byte_container::<LargeBinaryType, BinaryType>(&large_binary)
1427 }
1428 LargeBinary => Ok(Arc::new(LargeBinaryArray::from(
1429 array.as_string::<i64>().clone(),
1430 ))),
1431 Utf8View => Ok(Arc::new(StringViewArray::from(array.as_string::<i64>()))),
1432 BinaryView => Ok(Arc::new(BinaryViewArray::from(
1433 array
1434 .as_string::<i64>()
1435 .into_iter()
1436 .map(|x| x.map(|x| x.as_bytes()))
1437 .collect::<Vec<_>>(),
1438 ))),
1439 Time32(TimeUnit::Second) => parse_string::<Time32SecondType, i64>(array, cast_options),
1440 Time32(TimeUnit::Millisecond) => {
1441 parse_string::<Time32MillisecondType, i64>(array, cast_options)
1442 }
1443 Time64(TimeUnit::Microsecond) => {
1444 parse_string::<Time64MicrosecondType, i64>(array, cast_options)
1445 }
1446 Time64(TimeUnit::Nanosecond) => {
1447 parse_string::<Time64NanosecondType, i64>(array, cast_options)
1448 }
1449 Timestamp(TimeUnit::Second, to_tz) => cast_string_to_timestamp::<
1450 i64,
1451 TimestampSecondType,
1452 >(array, to_tz.as_ref(), cast_options),
1453 Timestamp(TimeUnit::Millisecond, to_tz) => cast_string_to_timestamp::<
1454 i64,
1455 TimestampMillisecondType,
1456 >(
1457 array, to_tz.as_ref(), cast_options
1458 ),
1459 Timestamp(TimeUnit::Microsecond, to_tz) => cast_string_to_timestamp::<
1460 i64,
1461 TimestampMicrosecondType,
1462 >(
1463 array, to_tz.as_ref(), cast_options
1464 ),
1465 Timestamp(TimeUnit::Nanosecond, to_tz) => cast_string_to_timestamp::<
1466 i64,
1467 TimestampNanosecondType,
1468 >(
1469 array, to_tz.as_ref(), cast_options
1470 ),
1471 Interval(IntervalUnit::YearMonth) => {
1472 cast_string_to_year_month_interval::<i64>(array, cast_options)
1473 }
1474 Interval(IntervalUnit::DayTime) => {
1475 cast_string_to_day_time_interval::<i64>(array, cast_options)
1476 }
1477 Interval(IntervalUnit::MonthDayNano) => {
1478 cast_string_to_month_day_nano_interval::<i64>(array, cast_options)
1479 }
1480 _ => Err(ArrowError::CastError(format!(
1481 "Casting from {from_type} to {to_type} not supported",
1482 ))),
1483 },
1484 (Binary, _) => match to_type {
1485 Utf8 => cast_binary_to_string::<i32>(array, cast_options),
1486 LargeUtf8 => {
1487 let array = cast_binary_to_string::<i32>(array, cast_options)?;
1488 cast_byte_container::<Utf8Type, LargeUtf8Type>(array.as_ref())
1489 }
1490 LargeBinary => cast_byte_container::<BinaryType, LargeBinaryType>(array),
1491 FixedSizeBinary(size) => {
1492 cast_binary_to_fixed_size_binary::<i32>(array, *size, cast_options)
1493 }
1494 BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i32>()))),
1495 Utf8View => Ok(Arc::new(StringViewArray::from(
1496 cast_binary_to_string::<i32>(array, cast_options)?.as_string::<i32>(),
1497 ))),
1498 _ => Err(ArrowError::CastError(format!(
1499 "Casting from {from_type} to {to_type} not supported",
1500 ))),
1501 },
1502 (LargeBinary, _) => match to_type {
1503 Utf8 => {
1504 let array = cast_binary_to_string::<i64>(array, cast_options)?;
1505 cast_byte_container::<LargeUtf8Type, Utf8Type>(array.as_ref())
1506 }
1507 LargeUtf8 => cast_binary_to_string::<i64>(array, cast_options),
1508 Binary => cast_byte_container::<LargeBinaryType, BinaryType>(array),
1509 FixedSizeBinary(size) => {
1510 cast_binary_to_fixed_size_binary::<i64>(array, *size, cast_options)
1511 }
1512 BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i64>()))),
1513 Utf8View => {
1514 let array = cast_binary_to_string::<i64>(array, cast_options)?;
1515 Ok(Arc::new(StringViewArray::from(array.as_string::<i64>())))
1516 }
1517 _ => Err(ArrowError::CastError(format!(
1518 "Casting from {from_type} to {to_type} not supported",
1519 ))),
1520 },
1521 (FixedSizeBinary(size), _) => match to_type {
1522 Binary => cast_fixed_size_binary_to_binary::<i32>(array, *size),
1523 LargeBinary => cast_fixed_size_binary_to_binary::<i64>(array, *size),
1524 BinaryView => cast_fixed_size_binary_to_binary_view(array, *size),
1525 _ => Err(ArrowError::CastError(format!(
1526 "Casting from {from_type} to {to_type} not supported",
1527 ))),
1528 },
1529 (BinaryView, Binary) => cast_view_to_byte::<BinaryViewType, GenericBinaryType<i32>>(array),
1530 (BinaryView, LargeBinary) => {
1531 cast_view_to_byte::<BinaryViewType, GenericBinaryType<i64>>(array)
1532 }
1533 (BinaryView, Utf8) => {
1534 let binary_arr = cast_view_to_byte::<BinaryViewType, GenericBinaryType<i32>>(array)?;
1535 cast_binary_to_string::<i32>(&binary_arr, cast_options)
1536 }
1537 (BinaryView, LargeUtf8) => {
1538 let binary_arr = cast_view_to_byte::<BinaryViewType, GenericBinaryType<i64>>(array)?;
1539 cast_binary_to_string::<i64>(&binary_arr, cast_options)
1540 }
1541 (BinaryView, Utf8View) => cast_binary_view_to_string_view(array, cast_options),
1542 (BinaryView, _) => Err(ArrowError::CastError(format!(
1543 "Casting from {from_type} to {to_type} not supported",
1544 ))),
1545 (from_type, Utf8View) if from_type.is_primitive() => {
1546 value_to_string_view(array, cast_options)
1547 }
1548 (from_type, LargeUtf8) if from_type.is_primitive() => {
1549 value_to_string::<i64>(array, cast_options)
1550 }
1551 (from_type, Utf8) if from_type.is_primitive() => {
1552 value_to_string::<i32>(array, cast_options)
1553 }
1554 (from_type, Binary) if from_type.is_integer() => match from_type {
1555 UInt8 => cast_numeric_to_binary::<UInt8Type, i32>(array),
1556 UInt16 => cast_numeric_to_binary::<UInt16Type, i32>(array),
1557 UInt32 => cast_numeric_to_binary::<UInt32Type, i32>(array),
1558 UInt64 => cast_numeric_to_binary::<UInt64Type, i32>(array),
1559 Int8 => cast_numeric_to_binary::<Int8Type, i32>(array),
1560 Int16 => cast_numeric_to_binary::<Int16Type, i32>(array),
1561 Int32 => cast_numeric_to_binary::<Int32Type, i32>(array),
1562 Int64 => cast_numeric_to_binary::<Int64Type, i32>(array),
1563 _ => unreachable!(),
1564 },
1565 (from_type, LargeBinary) if from_type.is_integer() => match from_type {
1566 UInt8 => cast_numeric_to_binary::<UInt8Type, i64>(array),
1567 UInt16 => cast_numeric_to_binary::<UInt16Type, i64>(array),
1568 UInt32 => cast_numeric_to_binary::<UInt32Type, i64>(array),
1569 UInt64 => cast_numeric_to_binary::<UInt64Type, i64>(array),
1570 Int8 => cast_numeric_to_binary::<Int8Type, i64>(array),
1571 Int16 => cast_numeric_to_binary::<Int16Type, i64>(array),
1572 Int32 => cast_numeric_to_binary::<Int32Type, i64>(array),
1573 Int64 => cast_numeric_to_binary::<Int64Type, i64>(array),
1574 _ => unreachable!(),
1575 },
1576 (UInt8, UInt16) => cast_numeric_arrays::<UInt8Type, UInt16Type>(array, cast_options),
1578 (UInt8, UInt32) => cast_numeric_arrays::<UInt8Type, UInt32Type>(array, cast_options),
1579 (UInt8, UInt64) => cast_numeric_arrays::<UInt8Type, UInt64Type>(array, cast_options),
1580 (UInt8, Int8) => cast_numeric_arrays::<UInt8Type, Int8Type>(array, cast_options),
1581 (UInt8, Int16) => cast_numeric_arrays::<UInt8Type, Int16Type>(array, cast_options),
1582 (UInt8, Int32) => cast_numeric_arrays::<UInt8Type, Int32Type>(array, cast_options),
1583 (UInt8, Int64) => cast_numeric_arrays::<UInt8Type, Int64Type>(array, cast_options),
1584 (UInt8, Float16) => cast_numeric_arrays::<UInt8Type, Float16Type>(array, cast_options),
1585 (UInt8, Float32) => cast_numeric_arrays::<UInt8Type, Float32Type>(array, cast_options),
1586 (UInt8, Float64) => cast_numeric_arrays::<UInt8Type, Float64Type>(array, cast_options),
1587
1588 (UInt16, UInt8) => cast_numeric_arrays::<UInt16Type, UInt8Type>(array, cast_options),
1589 (UInt16, UInt32) => cast_numeric_arrays::<UInt16Type, UInt32Type>(array, cast_options),
1590 (UInt16, UInt64) => cast_numeric_arrays::<UInt16Type, UInt64Type>(array, cast_options),
1591 (UInt16, Int8) => cast_numeric_arrays::<UInt16Type, Int8Type>(array, cast_options),
1592 (UInt16, Int16) => cast_numeric_arrays::<UInt16Type, Int16Type>(array, cast_options),
1593 (UInt16, Int32) => cast_numeric_arrays::<UInt16Type, Int32Type>(array, cast_options),
1594 (UInt16, Int64) => cast_numeric_arrays::<UInt16Type, Int64Type>(array, cast_options),
1595 (UInt16, Float16) => cast_numeric_arrays::<UInt16Type, Float16Type>(array, cast_options),
1596 (UInt16, Float32) => cast_numeric_arrays::<UInt16Type, Float32Type>(array, cast_options),
1597 (UInt16, Float64) => cast_numeric_arrays::<UInt16Type, Float64Type>(array, cast_options),
1598
1599 (UInt32, UInt8) => cast_numeric_arrays::<UInt32Type, UInt8Type>(array, cast_options),
1600 (UInt32, UInt16) => cast_numeric_arrays::<UInt32Type, UInt16Type>(array, cast_options),
1601 (UInt32, UInt64) => cast_numeric_arrays::<UInt32Type, UInt64Type>(array, cast_options),
1602 (UInt32, Int8) => cast_numeric_arrays::<UInt32Type, Int8Type>(array, cast_options),
1603 (UInt32, Int16) => cast_numeric_arrays::<UInt32Type, Int16Type>(array, cast_options),
1604 (UInt32, Int32) => cast_numeric_arrays::<UInt32Type, Int32Type>(array, cast_options),
1605 (UInt32, Int64) => cast_numeric_arrays::<UInt32Type, Int64Type>(array, cast_options),
1606 (UInt32, Float16) => cast_numeric_arrays::<UInt32Type, Float16Type>(array, cast_options),
1607 (UInt32, Float32) => cast_numeric_arrays::<UInt32Type, Float32Type>(array, cast_options),
1608 (UInt32, Float64) => cast_numeric_arrays::<UInt32Type, Float64Type>(array, cast_options),
1609
1610 (UInt64, UInt8) => cast_numeric_arrays::<UInt64Type, UInt8Type>(array, cast_options),
1611 (UInt64, UInt16) => cast_numeric_arrays::<UInt64Type, UInt16Type>(array, cast_options),
1612 (UInt64, UInt32) => cast_numeric_arrays::<UInt64Type, UInt32Type>(array, cast_options),
1613 (UInt64, Int8) => cast_numeric_arrays::<UInt64Type, Int8Type>(array, cast_options),
1614 (UInt64, Int16) => cast_numeric_arrays::<UInt64Type, Int16Type>(array, cast_options),
1615 (UInt64, Int32) => cast_numeric_arrays::<UInt64Type, Int32Type>(array, cast_options),
1616 (UInt64, Int64) => cast_numeric_arrays::<UInt64Type, Int64Type>(array, cast_options),
1617 (UInt64, Float16) => cast_numeric_arrays::<UInt64Type, Float16Type>(array, cast_options),
1618 (UInt64, Float32) => cast_numeric_arrays::<UInt64Type, Float32Type>(array, cast_options),
1619 (UInt64, Float64) => cast_numeric_arrays::<UInt64Type, Float64Type>(array, cast_options),
1620
1621 (Int8, UInt8) => cast_numeric_arrays::<Int8Type, UInt8Type>(array, cast_options),
1622 (Int8, UInt16) => cast_numeric_arrays::<Int8Type, UInt16Type>(array, cast_options),
1623 (Int8, UInt32) => cast_numeric_arrays::<Int8Type, UInt32Type>(array, cast_options),
1624 (Int8, UInt64) => cast_numeric_arrays::<Int8Type, UInt64Type>(array, cast_options),
1625 (Int8, Int16) => cast_numeric_arrays::<Int8Type, Int16Type>(array, cast_options),
1626 (Int8, Int32) => cast_numeric_arrays::<Int8Type, Int32Type>(array, cast_options),
1627 (Int8, Int64) => cast_numeric_arrays::<Int8Type, Int64Type>(array, cast_options),
1628 (Int8, Float16) => cast_numeric_arrays::<Int8Type, Float16Type>(array, cast_options),
1629 (Int8, Float32) => cast_numeric_arrays::<Int8Type, Float32Type>(array, cast_options),
1630 (Int8, Float64) => cast_numeric_arrays::<Int8Type, Float64Type>(array, cast_options),
1631
1632 (Int16, UInt8) => cast_numeric_arrays::<Int16Type, UInt8Type>(array, cast_options),
1633 (Int16, UInt16) => cast_numeric_arrays::<Int16Type, UInt16Type>(array, cast_options),
1634 (Int16, UInt32) => cast_numeric_arrays::<Int16Type, UInt32Type>(array, cast_options),
1635 (Int16, UInt64) => cast_numeric_arrays::<Int16Type, UInt64Type>(array, cast_options),
1636 (Int16, Int8) => cast_numeric_arrays::<Int16Type, Int8Type>(array, cast_options),
1637 (Int16, Int32) => cast_numeric_arrays::<Int16Type, Int32Type>(array, cast_options),
1638 (Int16, Int64) => cast_numeric_arrays::<Int16Type, Int64Type>(array, cast_options),
1639 (Int16, Float16) => cast_numeric_arrays::<Int16Type, Float16Type>(array, cast_options),
1640 (Int16, Float32) => cast_numeric_arrays::<Int16Type, Float32Type>(array, cast_options),
1641 (Int16, Float64) => cast_numeric_arrays::<Int16Type, Float64Type>(array, cast_options),
1642
1643 (Int32, UInt8) => cast_numeric_arrays::<Int32Type, UInt8Type>(array, cast_options),
1644 (Int32, UInt16) => cast_numeric_arrays::<Int32Type, UInt16Type>(array, cast_options),
1645 (Int32, UInt32) => cast_numeric_arrays::<Int32Type, UInt32Type>(array, cast_options),
1646 (Int32, UInt64) => cast_numeric_arrays::<Int32Type, UInt64Type>(array, cast_options),
1647 (Int32, Int8) => cast_numeric_arrays::<Int32Type, Int8Type>(array, cast_options),
1648 (Int32, Int16) => cast_numeric_arrays::<Int32Type, Int16Type>(array, cast_options),
1649 (Int32, Int64) => cast_numeric_arrays::<Int32Type, Int64Type>(array, cast_options),
1650 (Int32, Float16) => cast_numeric_arrays::<Int32Type, Float16Type>(array, cast_options),
1651 (Int32, Float32) => cast_numeric_arrays::<Int32Type, Float32Type>(array, cast_options),
1652 (Int32, Float64) => cast_numeric_arrays::<Int32Type, Float64Type>(array, cast_options),
1653
1654 (Int64, UInt8) => cast_numeric_arrays::<Int64Type, UInt8Type>(array, cast_options),
1655 (Int64, UInt16) => cast_numeric_arrays::<Int64Type, UInt16Type>(array, cast_options),
1656 (Int64, UInt32) => cast_numeric_arrays::<Int64Type, UInt32Type>(array, cast_options),
1657 (Int64, UInt64) => cast_numeric_arrays::<Int64Type, UInt64Type>(array, cast_options),
1658 (Int64, Int8) => cast_numeric_arrays::<Int64Type, Int8Type>(array, cast_options),
1659 (Int64, Int16) => cast_numeric_arrays::<Int64Type, Int16Type>(array, cast_options),
1660 (Int64, Int32) => cast_numeric_arrays::<Int64Type, Int32Type>(array, cast_options),
1661 (Int64, Float16) => cast_numeric_arrays::<Int64Type, Float16Type>(array, cast_options),
1662 (Int64, Float32) => cast_numeric_arrays::<Int64Type, Float32Type>(array, cast_options),
1663 (Int64, Float64) => cast_numeric_arrays::<Int64Type, Float64Type>(array, cast_options),
1664
1665 (Float16, UInt8) => cast_numeric_arrays::<Float16Type, UInt8Type>(array, cast_options),
1666 (Float16, UInt16) => cast_numeric_arrays::<Float16Type, UInt16Type>(array, cast_options),
1667 (Float16, UInt32) => cast_numeric_arrays::<Float16Type, UInt32Type>(array, cast_options),
1668 (Float16, UInt64) => cast_numeric_arrays::<Float16Type, UInt64Type>(array, cast_options),
1669 (Float16, Int8) => cast_numeric_arrays::<Float16Type, Int8Type>(array, cast_options),
1670 (Float16, Int16) => cast_numeric_arrays::<Float16Type, Int16Type>(array, cast_options),
1671 (Float16, Int32) => cast_numeric_arrays::<Float16Type, Int32Type>(array, cast_options),
1672 (Float16, Int64) => cast_numeric_arrays::<Float16Type, Int64Type>(array, cast_options),
1673 (Float16, Float32) => cast_numeric_arrays::<Float16Type, Float32Type>(array, cast_options),
1674 (Float16, Float64) => cast_numeric_arrays::<Float16Type, Float64Type>(array, cast_options),
1675
1676 (Float32, UInt8) => cast_numeric_arrays::<Float32Type, UInt8Type>(array, cast_options),
1677 (Float32, UInt16) => cast_numeric_arrays::<Float32Type, UInt16Type>(array, cast_options),
1678 (Float32, UInt32) => cast_numeric_arrays::<Float32Type, UInt32Type>(array, cast_options),
1679 (Float32, UInt64) => cast_numeric_arrays::<Float32Type, UInt64Type>(array, cast_options),
1680 (Float32, Int8) => cast_numeric_arrays::<Float32Type, Int8Type>(array, cast_options),
1681 (Float32, Int16) => cast_numeric_arrays::<Float32Type, Int16Type>(array, cast_options),
1682 (Float32, Int32) => cast_numeric_arrays::<Float32Type, Int32Type>(array, cast_options),
1683 (Float32, Int64) => cast_numeric_arrays::<Float32Type, Int64Type>(array, cast_options),
1684 (Float32, Float16) => cast_numeric_arrays::<Float32Type, Float16Type>(array, cast_options),
1685 (Float32, Float64) => cast_numeric_arrays::<Float32Type, Float64Type>(array, cast_options),
1686
1687 (Float64, UInt8) => cast_numeric_arrays::<Float64Type, UInt8Type>(array, cast_options),
1688 (Float64, UInt16) => cast_numeric_arrays::<Float64Type, UInt16Type>(array, cast_options),
1689 (Float64, UInt32) => cast_numeric_arrays::<Float64Type, UInt32Type>(array, cast_options),
1690 (Float64, UInt64) => cast_numeric_arrays::<Float64Type, UInt64Type>(array, cast_options),
1691 (Float64, Int8) => cast_numeric_arrays::<Float64Type, Int8Type>(array, cast_options),
1692 (Float64, Int16) => cast_numeric_arrays::<Float64Type, Int16Type>(array, cast_options),
1693 (Float64, Int32) => cast_numeric_arrays::<Float64Type, Int32Type>(array, cast_options),
1694 (Float64, Int64) => cast_numeric_arrays::<Float64Type, Int64Type>(array, cast_options),
1695 (Float64, Float16) => cast_numeric_arrays::<Float64Type, Float16Type>(array, cast_options),
1696 (Float64, Float32) => cast_numeric_arrays::<Float64Type, Float32Type>(array, cast_options),
1697 (Int32, Date32) => cast_reinterpret_arrays::<Int32Type, Date32Type>(array),
1701 (Int32, Date64) => cast_with_options(
1702 &cast_with_options(array, &Date32, cast_options)?,
1703 &Date64,
1704 cast_options,
1705 ),
1706 (Int32, Time32(TimeUnit::Second)) => {
1707 cast_reinterpret_arrays::<Int32Type, Time32SecondType>(array)
1708 }
1709 (Int32, Time32(TimeUnit::Millisecond)) => {
1710 cast_reinterpret_arrays::<Int32Type, Time32MillisecondType>(array)
1711 }
1712 (Date32, Int32) => cast_reinterpret_arrays::<Date32Type, Int32Type>(array),
1714 (Date32, Int64) => cast_with_options(
1715 &cast_with_options(array, &Int32, cast_options)?,
1716 &Int64,
1717 cast_options,
1718 ),
1719 (Time32(TimeUnit::Second), Int32) => {
1720 cast_reinterpret_arrays::<Time32SecondType, Int32Type>(array)
1721 }
1722 (Time32(TimeUnit::Millisecond), Int32) => {
1723 cast_reinterpret_arrays::<Time32MillisecondType, Int32Type>(array)
1724 }
1725 (Time32(TimeUnit::Second), Int64) => cast_with_options(
1726 &cast_with_options(array, &Int32, cast_options)?,
1727 &Int64,
1728 cast_options,
1729 ),
1730 (Time32(TimeUnit::Millisecond), Int64) => cast_with_options(
1731 &cast_with_options(array, &Int32, cast_options)?,
1732 &Int64,
1733 cast_options,
1734 ),
1735 (Int64, Date64) => cast_reinterpret_arrays::<Int64Type, Date64Type>(array),
1736 (Int64, Date32) => cast_with_options(
1737 &cast_with_options(array, &Int32, cast_options)?,
1738 &Date32,
1739 cast_options,
1740 ),
1741 (Int64, Time64(TimeUnit::Microsecond)) => {
1743 cast_reinterpret_arrays::<Int64Type, Time64MicrosecondType>(array)
1744 }
1745 (Int64, Time64(TimeUnit::Nanosecond)) => {
1746 cast_reinterpret_arrays::<Int64Type, Time64NanosecondType>(array)
1747 }
1748
1749 (Date64, Int64) => cast_reinterpret_arrays::<Date64Type, Int64Type>(array),
1750 (Date64, Int32) => cast_with_options(
1751 &cast_with_options(array, &Int64, cast_options)?,
1752 &Int32,
1753 cast_options,
1754 ),
1755 (Time64(TimeUnit::Microsecond), Int64) => {
1756 cast_reinterpret_arrays::<Time64MicrosecondType, Int64Type>(array)
1757 }
1758 (Time64(TimeUnit::Nanosecond), Int64) => {
1759 cast_reinterpret_arrays::<Time64NanosecondType, Int64Type>(array)
1760 }
1761 (Date32, Date64) => Ok(Arc::new(
1762 array
1763 .as_primitive::<Date32Type>()
1764 .unary::<_, Date64Type>(|x| x as i64 * MILLISECONDS_IN_DAY),
1765 )),
1766 (Date64, Date32) => {
1767 let array = array.as_primitive::<Date64Type>();
1768 let result = if cast_options.safe {
1769 array.unary_opt::<_, Date32Type>(|x| i32::try_from(x / MILLISECONDS_IN_DAY).ok())
1770 } else {
1771 array.try_unary::<_, Date32Type, _>(|x| {
1772 i32::try_from(x / MILLISECONDS_IN_DAY).map_err(|_| {
1773 ArrowError::CastError(format!(
1774 "Cannot cast Date64 value {x} to Date32 without overflow"
1775 ))
1776 })
1777 })?
1778 };
1779 Ok(Arc::new(result))
1780 }
1781
1782 (Time32(TimeUnit::Second), Time32(TimeUnit::Millisecond)) => {
1783 let array = array.as_primitive::<Time32SecondType>();
1784 let result = if cast_options.safe {
1785 array.unary_opt::<_, Time32MillisecondType>(|x| x.checked_mul(MILLISECONDS as i32))
1786 } else {
1787 array.try_unary::<_, Time32MillisecondType, _>(|x| {
1788 x.mul_checked(MILLISECONDS as i32)
1789 })?
1790 };
1791 Ok(Arc::new(result))
1792 }
1793 (Time32(TimeUnit::Second), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1794 array
1795 .as_primitive::<Time32SecondType>()
1796 .unary::<_, Time64MicrosecondType>(|x| x as i64 * MICROSECONDS),
1797 )),
1798 (Time32(TimeUnit::Second), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1799 array
1800 .as_primitive::<Time32SecondType>()
1801 .unary::<_, Time64NanosecondType>(|x| x as i64 * NANOSECONDS),
1802 )),
1803
1804 (Time32(TimeUnit::Millisecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1805 array
1806 .as_primitive::<Time32MillisecondType>()
1807 .unary::<_, Time32SecondType>(|x| x / MILLISECONDS as i32),
1808 )),
1809 (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1810 array
1811 .as_primitive::<Time32MillisecondType>()
1812 .unary::<_, Time64MicrosecondType>(|x| x as i64 * (MICROSECONDS / MILLISECONDS)),
1813 )),
1814 (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1815 array
1816 .as_primitive::<Time32MillisecondType>()
1817 .unary::<_, Time64NanosecondType>(|x| x as i64 * (NANOSECONDS / MILLISECONDS)),
1818 )),
1819
1820 (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Second)) => {
1821 cast_time64_to_time32::<Time64MicrosecondType, Time32SecondType>(
1822 array,
1823 MICROSECONDS,
1824 cast_options,
1825 )
1826 }
1827 (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Millisecond)) => {
1828 cast_time64_to_time32::<Time64MicrosecondType, Time32MillisecondType>(
1829 array,
1830 MICROSECONDS / MILLISECONDS,
1831 cast_options,
1832 )
1833 }
1834 (Time64(TimeUnit::Microsecond), Time64(TimeUnit::Nanosecond)) => {
1835 let array = array.as_primitive::<Time64MicrosecondType>();
1836 let result = if cast_options.safe {
1837 array.unary_opt::<_, Time64NanosecondType>(|x| {
1838 x.checked_mul(NANOSECONDS / MICROSECONDS)
1839 })
1840 } else {
1841 array.try_unary::<_, Time64NanosecondType, _>(|x| {
1842 x.mul_checked(NANOSECONDS / MICROSECONDS)
1843 })?
1844 };
1845 Ok(Arc::new(result))
1846 }
1847
1848 (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Second)) => {
1849 cast_time64_to_time32::<Time64NanosecondType, Time32SecondType>(
1850 array,
1851 NANOSECONDS,
1852 cast_options,
1853 )
1854 }
1855 (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Millisecond)) => {
1856 cast_time64_to_time32::<Time64NanosecondType, Time32MillisecondType>(
1857 array,
1858 NANOSECONDS / MILLISECONDS,
1859 cast_options,
1860 )
1861 }
1862 (Time64(TimeUnit::Nanosecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1863 array
1864 .as_primitive::<Time64NanosecondType>()
1865 .unary::<_, Time64MicrosecondType>(|x| x / (NANOSECONDS / MICROSECONDS)),
1866 )),
1867
1868 (Timestamp(TimeUnit::Second, _), _) if to_type.is_numeric() => {
1870 let array = cast_reinterpret_arrays::<TimestampSecondType, Int64Type>(array)?;
1871 cast_with_options(&array, to_type, cast_options)
1872 }
1873 (Timestamp(TimeUnit::Millisecond, _), _) if to_type.is_numeric() => {
1874 let array = cast_reinterpret_arrays::<TimestampMillisecondType, Int64Type>(array)?;
1875 cast_with_options(&array, to_type, cast_options)
1876 }
1877 (Timestamp(TimeUnit::Microsecond, _), _) if to_type.is_numeric() => {
1878 let array = cast_reinterpret_arrays::<TimestampMicrosecondType, Int64Type>(array)?;
1879 cast_with_options(&array, to_type, cast_options)
1880 }
1881 (Timestamp(TimeUnit::Nanosecond, _), _) if to_type.is_numeric() => {
1882 let array = cast_reinterpret_arrays::<TimestampNanosecondType, Int64Type>(array)?;
1883 cast_with_options(&array, to_type, cast_options)
1884 }
1885
1886 (_, Timestamp(unit, tz)) if from_type.is_numeric() => {
1887 let array = cast_with_options(array, &Int64, cast_options)?;
1888 Ok(make_timestamp_array(
1889 array.as_primitive(),
1890 *unit,
1891 tz.clone(),
1892 ))
1893 }
1894
1895 (Timestamp(from_unit, from_tz), Timestamp(to_unit, to_tz)) => {
1896 let array = cast_with_options(array, &Int64, cast_options)?;
1897 let time_array = array.as_primitive::<Int64Type>();
1898 let from_size = time_unit_multiple(from_unit);
1899 let to_size = time_unit_multiple(to_unit);
1900 let converted = match from_size.cmp(&to_size) {
1903 Ordering::Greater => {
1904 let divisor = from_size / to_size;
1905 time_array.unary::<_, Int64Type>(|o| o / divisor)
1906 }
1907 Ordering::Equal => time_array.clone(),
1908 Ordering::Less => {
1909 let mul = to_size / from_size;
1910 if cast_options.safe {
1911 time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
1912 } else {
1913 time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
1914 }
1915 }
1916 };
1917 let adjusted = match (from_tz, to_tz) {
1919 (None, Some(to_tz)) => {
1925 let to_tz: Tz = to_tz.parse()?;
1926 match to_unit {
1927 TimeUnit::Second => adjust_timestamp_to_timezone::<TimestampSecondType>(
1928 converted,
1929 &to_tz,
1930 cast_options,
1931 )?,
1932 TimeUnit::Millisecond => adjust_timestamp_to_timezone::<
1933 TimestampMillisecondType,
1934 >(
1935 converted, &to_tz, cast_options
1936 )?,
1937 TimeUnit::Microsecond => adjust_timestamp_to_timezone::<
1938 TimestampMicrosecondType,
1939 >(
1940 converted, &to_tz, cast_options
1941 )?,
1942 TimeUnit::Nanosecond => adjust_timestamp_to_timezone::<
1943 TimestampNanosecondType,
1944 >(
1945 converted, &to_tz, cast_options
1946 )?,
1947 }
1948 }
1949 _ => converted,
1950 };
1951 Ok(make_timestamp_array(&adjusted, *to_unit, to_tz.clone()))
1952 }
1953 (Timestamp(TimeUnit::Microsecond, _), Date32) => {
1954 timestamp_to_date32(array.as_primitive::<TimestampMicrosecondType>())
1955 }
1956 (Timestamp(TimeUnit::Millisecond, _), Date32) => {
1957 timestamp_to_date32(array.as_primitive::<TimestampMillisecondType>())
1958 }
1959 (Timestamp(TimeUnit::Second, _), Date32) => {
1960 timestamp_to_date32(array.as_primitive::<TimestampSecondType>())
1961 }
1962 (Timestamp(TimeUnit::Nanosecond, _), Date32) => {
1963 timestamp_to_date32(array.as_primitive::<TimestampNanosecondType>())
1964 }
1965 (Timestamp(TimeUnit::Second, _), Date64) => Ok(Arc::new(match cast_options.safe {
1966 true => {
1967 array
1969 .as_primitive::<TimestampSecondType>()
1970 .unary_opt::<_, Date64Type>(|x| x.checked_mul(MILLISECONDS))
1971 }
1972 false => array
1973 .as_primitive::<TimestampSecondType>()
1974 .try_unary::<_, Date64Type, _>(|x| x.mul_checked(MILLISECONDS))?,
1975 })),
1976 (Timestamp(TimeUnit::Millisecond, _), Date64) => {
1977 cast_reinterpret_arrays::<TimestampMillisecondType, Date64Type>(array)
1978 }
1979 (Timestamp(TimeUnit::Microsecond, _), Date64) => Ok(Arc::new(
1980 array
1981 .as_primitive::<TimestampMicrosecondType>()
1982 .unary::<_, Date64Type>(|x| x / (MICROSECONDS / MILLISECONDS)),
1983 )),
1984 (Timestamp(TimeUnit::Nanosecond, _), Date64) => Ok(Arc::new(
1985 array
1986 .as_primitive::<TimestampNanosecondType>()
1987 .unary::<_, Date64Type>(|x| x / (NANOSECONDS / MILLISECONDS)),
1988 )),
1989 (Timestamp(TimeUnit::Second, tz), Time64(TimeUnit::Microsecond)) => {
1990 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1991 Ok(Arc::new(
1992 array
1993 .as_primitive::<TimestampSecondType>()
1994 .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1995 Ok(time_to_time64us(as_time_res_with_timezone::<
1996 TimestampSecondType,
1997 >(x, tz)?))
1998 })?,
1999 ))
2000 }
2001 (Timestamp(TimeUnit::Second, tz), Time64(TimeUnit::Nanosecond)) => {
2002 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2003 Ok(Arc::new(
2004 array
2005 .as_primitive::<TimestampSecondType>()
2006 .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
2007 Ok(time_to_time64ns(as_time_res_with_timezone::<
2008 TimestampSecondType,
2009 >(x, tz)?))
2010 })?,
2011 ))
2012 }
2013 (Timestamp(TimeUnit::Millisecond, tz), Time64(TimeUnit::Microsecond)) => {
2014 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2015 Ok(Arc::new(
2016 array
2017 .as_primitive::<TimestampMillisecondType>()
2018 .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
2019 Ok(time_to_time64us(as_time_res_with_timezone::<
2020 TimestampMillisecondType,
2021 >(x, tz)?))
2022 })?,
2023 ))
2024 }
2025 (Timestamp(TimeUnit::Millisecond, tz), Time64(TimeUnit::Nanosecond)) => {
2026 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2027 Ok(Arc::new(
2028 array
2029 .as_primitive::<TimestampMillisecondType>()
2030 .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
2031 Ok(time_to_time64ns(as_time_res_with_timezone::<
2032 TimestampMillisecondType,
2033 >(x, tz)?))
2034 })?,
2035 ))
2036 }
2037 (Timestamp(TimeUnit::Microsecond, tz), Time64(TimeUnit::Microsecond)) => {
2038 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2039 Ok(Arc::new(
2040 array
2041 .as_primitive::<TimestampMicrosecondType>()
2042 .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
2043 Ok(time_to_time64us(as_time_res_with_timezone::<
2044 TimestampMicrosecondType,
2045 >(x, tz)?))
2046 })?,
2047 ))
2048 }
2049 (Timestamp(TimeUnit::Microsecond, tz), Time64(TimeUnit::Nanosecond)) => {
2050 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2051 Ok(Arc::new(
2052 array
2053 .as_primitive::<TimestampMicrosecondType>()
2054 .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
2055 Ok(time_to_time64ns(as_time_res_with_timezone::<
2056 TimestampMicrosecondType,
2057 >(x, tz)?))
2058 })?,
2059 ))
2060 }
2061 (Timestamp(TimeUnit::Nanosecond, tz), Time64(TimeUnit::Microsecond)) => {
2062 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2063 Ok(Arc::new(
2064 array
2065 .as_primitive::<TimestampNanosecondType>()
2066 .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
2067 Ok(time_to_time64us(as_time_res_with_timezone::<
2068 TimestampNanosecondType,
2069 >(x, tz)?))
2070 })?,
2071 ))
2072 }
2073 (Timestamp(TimeUnit::Nanosecond, tz), Time64(TimeUnit::Nanosecond)) => {
2074 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2075 Ok(Arc::new(
2076 array
2077 .as_primitive::<TimestampNanosecondType>()
2078 .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
2079 Ok(time_to_time64ns(as_time_res_with_timezone::<
2080 TimestampNanosecondType,
2081 >(x, tz)?))
2082 })?,
2083 ))
2084 }
2085 (Timestamp(TimeUnit::Second, tz), Time32(TimeUnit::Second)) => {
2086 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2087 Ok(Arc::new(
2088 array
2089 .as_primitive::<TimestampSecondType>()
2090 .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2091 Ok(time_to_time32s(as_time_res_with_timezone::<
2092 TimestampSecondType,
2093 >(x, tz)?))
2094 })?,
2095 ))
2096 }
2097 (Timestamp(TimeUnit::Second, tz), Time32(TimeUnit::Millisecond)) => {
2098 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2099 Ok(Arc::new(
2100 array
2101 .as_primitive::<TimestampSecondType>()
2102 .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2103 Ok(time_to_time32ms(as_time_res_with_timezone::<
2104 TimestampSecondType,
2105 >(x, tz)?))
2106 })?,
2107 ))
2108 }
2109 (Timestamp(TimeUnit::Millisecond, tz), Time32(TimeUnit::Second)) => {
2110 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2111 Ok(Arc::new(
2112 array
2113 .as_primitive::<TimestampMillisecondType>()
2114 .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2115 Ok(time_to_time32s(as_time_res_with_timezone::<
2116 TimestampMillisecondType,
2117 >(x, tz)?))
2118 })?,
2119 ))
2120 }
2121 (Timestamp(TimeUnit::Millisecond, tz), Time32(TimeUnit::Millisecond)) => {
2122 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2123 Ok(Arc::new(
2124 array
2125 .as_primitive::<TimestampMillisecondType>()
2126 .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2127 Ok(time_to_time32ms(as_time_res_with_timezone::<
2128 TimestampMillisecondType,
2129 >(x, tz)?))
2130 })?,
2131 ))
2132 }
2133 (Timestamp(TimeUnit::Microsecond, tz), Time32(TimeUnit::Second)) => {
2134 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2135 Ok(Arc::new(
2136 array
2137 .as_primitive::<TimestampMicrosecondType>()
2138 .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2139 Ok(time_to_time32s(as_time_res_with_timezone::<
2140 TimestampMicrosecondType,
2141 >(x, tz)?))
2142 })?,
2143 ))
2144 }
2145 (Timestamp(TimeUnit::Microsecond, tz), Time32(TimeUnit::Millisecond)) => {
2146 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2147 Ok(Arc::new(
2148 array
2149 .as_primitive::<TimestampMicrosecondType>()
2150 .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2151 Ok(time_to_time32ms(as_time_res_with_timezone::<
2152 TimestampMicrosecondType,
2153 >(x, tz)?))
2154 })?,
2155 ))
2156 }
2157 (Timestamp(TimeUnit::Nanosecond, tz), Time32(TimeUnit::Second)) => {
2158 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2159 Ok(Arc::new(
2160 array
2161 .as_primitive::<TimestampNanosecondType>()
2162 .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2163 Ok(time_to_time32s(as_time_res_with_timezone::<
2164 TimestampNanosecondType,
2165 >(x, tz)?))
2166 })?,
2167 ))
2168 }
2169 (Timestamp(TimeUnit::Nanosecond, tz), Time32(TimeUnit::Millisecond)) => {
2170 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2171 Ok(Arc::new(
2172 array
2173 .as_primitive::<TimestampNanosecondType>()
2174 .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2175 Ok(time_to_time32ms(as_time_res_with_timezone::<
2176 TimestampNanosecondType,
2177 >(x, tz)?))
2178 })?,
2179 ))
2180 }
2181 (Date64, Timestamp(TimeUnit::Second, _)) => {
2182 let array = array
2183 .as_primitive::<Date64Type>()
2184 .unary::<_, TimestampSecondType>(|x| x / MILLISECONDS);
2185
2186 cast_with_options(&array, to_type, cast_options)
2187 }
2188 (Date64, Timestamp(TimeUnit::Millisecond, _)) => {
2189 let array = array
2190 .as_primitive::<Date64Type>()
2191 .reinterpret_cast::<TimestampMillisecondType>();
2192
2193 cast_with_options(&array, to_type, cast_options)
2194 }
2195
2196 (Date64, Timestamp(TimeUnit::Microsecond, _)) => {
2197 let array = array
2198 .as_primitive::<Date64Type>()
2199 .reinterpret_cast::<TimestampMillisecondType>();
2200
2201 cast_with_options(&array, to_type, cast_options)
2202 }
2203 (Date64, Timestamp(TimeUnit::Nanosecond, _)) => {
2204 let array = array
2205 .as_primitive::<Date64Type>()
2206 .reinterpret_cast::<TimestampMillisecondType>();
2207
2208 cast_with_options(&array, to_type, cast_options)
2209 }
2210 (Date32, Timestamp(TimeUnit::Second, _)) => {
2211 let array = array
2212 .as_primitive::<Date32Type>()
2213 .unary::<_, TimestampSecondType>(|x| (x as i64) * SECONDS_IN_DAY);
2214
2215 cast_with_options(&array, to_type, cast_options)
2216 }
2217 (Date32, Timestamp(TimeUnit::Millisecond, _)) => {
2218 let array = array
2219 .as_primitive::<Date32Type>()
2220 .unary::<_, TimestampMillisecondType>(|x| (x as i64) * MILLISECONDS_IN_DAY);
2221
2222 cast_with_options(&array, to_type, cast_options)
2223 }
2224 (Date32, Timestamp(TimeUnit::Microsecond, _)) => {
2225 let date_array = array.as_primitive::<Date32Type>();
2226 let converted = if cast_options.safe {
2227 date_array.unary_opt::<_, TimestampMicrosecondType>(|x| {
2228 (x as i64).checked_mul(MICROSECONDS_IN_DAY)
2229 })
2230 } else {
2231 date_array.try_unary::<_, TimestampMicrosecondType, _>(|x| {
2232 (x as i64).mul_checked(MICROSECONDS_IN_DAY)
2233 })?
2234 };
2235 cast_with_options(&converted, to_type, cast_options)
2236 }
2237 (Date32, Timestamp(TimeUnit::Nanosecond, _)) => {
2238 let date_array = array.as_primitive::<Date32Type>();
2239 let converted = if cast_options.safe {
2240 date_array.unary_opt::<_, TimestampNanosecondType>(|x| {
2241 (x as i64).checked_mul(NANOSECONDS_IN_DAY)
2242 })
2243 } else {
2244 date_array.try_unary::<_, TimestampNanosecondType, _>(|x| {
2245 (x as i64).mul_checked(NANOSECONDS_IN_DAY)
2246 })?
2247 };
2248 cast_with_options(&converted, to_type, cast_options)
2249 }
2250
2251 (_, Duration(unit)) if from_type.is_numeric() => {
2252 let array = cast_with_options(array, &Int64, cast_options)?;
2253 Ok(make_duration_array(array.as_primitive(), *unit))
2254 }
2255 (Duration(TimeUnit::Second), _) if to_type.is_numeric() => {
2256 let array = cast_reinterpret_arrays::<DurationSecondType, Int64Type>(array)?;
2257 cast_with_options(&array, to_type, cast_options)
2258 }
2259 (Duration(TimeUnit::Millisecond), _) if to_type.is_numeric() => {
2260 let array = cast_reinterpret_arrays::<DurationMillisecondType, Int64Type>(array)?;
2261 cast_with_options(&array, to_type, cast_options)
2262 }
2263 (Duration(TimeUnit::Microsecond), _) if to_type.is_numeric() => {
2264 let array = cast_reinterpret_arrays::<DurationMicrosecondType, Int64Type>(array)?;
2265 cast_with_options(&array, to_type, cast_options)
2266 }
2267 (Duration(TimeUnit::Nanosecond), _) if to_type.is_numeric() => {
2268 let array = cast_reinterpret_arrays::<DurationNanosecondType, Int64Type>(array)?;
2269 cast_with_options(&array, to_type, cast_options)
2270 }
2271
2272 (Duration(from_unit), Duration(to_unit)) => {
2273 let array = cast_with_options(array, &Int64, cast_options)?;
2274 let time_array = array.as_primitive::<Int64Type>();
2275 let from_size = time_unit_multiple(from_unit);
2276 let to_size = time_unit_multiple(to_unit);
2277 let converted = match from_size.cmp(&to_size) {
2280 Ordering::Greater => {
2281 let divisor = from_size / to_size;
2282 time_array.unary::<_, Int64Type>(|o| o / divisor)
2283 }
2284 Ordering::Equal => time_array.clone(),
2285 Ordering::Less => {
2286 let mul = to_size / from_size;
2287 if cast_options.safe {
2288 time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
2289 } else {
2290 time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
2291 }
2292 }
2293 };
2294 Ok(make_duration_array(&converted, *to_unit))
2295 }
2296
2297 (Duration(TimeUnit::Second), Interval(IntervalUnit::MonthDayNano)) => {
2298 cast_duration_to_interval::<DurationSecondType>(array, cast_options)
2299 }
2300 (Duration(TimeUnit::Millisecond), Interval(IntervalUnit::MonthDayNano)) => {
2301 cast_duration_to_interval::<DurationMillisecondType>(array, cast_options)
2302 }
2303 (Duration(TimeUnit::Microsecond), Interval(IntervalUnit::MonthDayNano)) => {
2304 cast_duration_to_interval::<DurationMicrosecondType>(array, cast_options)
2305 }
2306 (Duration(TimeUnit::Nanosecond), Interval(IntervalUnit::MonthDayNano)) => {
2307 cast_duration_to_interval::<DurationNanosecondType>(array, cast_options)
2308 }
2309 (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Second)) => {
2310 cast_month_day_nano_to_duration::<DurationSecondType>(array, cast_options)
2311 }
2312 (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Millisecond)) => {
2313 cast_month_day_nano_to_duration::<DurationMillisecondType>(array, cast_options)
2314 }
2315 (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Microsecond)) => {
2316 cast_month_day_nano_to_duration::<DurationMicrosecondType>(array, cast_options)
2317 }
2318 (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Nanosecond)) => {
2319 cast_month_day_nano_to_duration::<DurationNanosecondType>(array, cast_options)
2320 }
2321 (Interval(IntervalUnit::YearMonth), Interval(IntervalUnit::MonthDayNano)) => {
2322 cast_interval_year_month_to_interval_month_day_nano(array, cast_options)
2323 }
2324 (Interval(IntervalUnit::DayTime), Interval(IntervalUnit::MonthDayNano)) => {
2325 cast_interval_day_time_to_interval_month_day_nano(array, cast_options)
2326 }
2327 (Int32, Interval(IntervalUnit::YearMonth)) => {
2328 cast_reinterpret_arrays::<Int32Type, IntervalYearMonthType>(array)
2329 }
2330 (_, _) => Err(ArrowError::CastError(format!(
2331 "Casting from {from_type} to {to_type} not supported",
2332 ))),
2333 }
2334}
2335
2336fn cast_from_decimal<D, F>(
2337 array: &dyn Array,
2338 base: D::Native,
2339 scale: &i8,
2340 from_type: &DataType,
2341 to_type: &DataType,
2342 as_float: F,
2343 cast_options: &CastOptions,
2344) -> Result<ArrayRef, ArrowError>
2345where
2346 D: DecimalType + ArrowPrimitiveType,
2347 <D as ArrowPrimitiveType>::Native: ToPrimitive,
2348 F: Fn(D::Native) -> f64,
2349{
2350 use DataType::*;
2351 match to_type {
2353 UInt8 => cast_decimal_to_integer::<D, UInt8Type>(array, base, *scale, cast_options),
2354 UInt16 => cast_decimal_to_integer::<D, UInt16Type>(array, base, *scale, cast_options),
2355 UInt32 => cast_decimal_to_integer::<D, UInt32Type>(array, base, *scale, cast_options),
2356 UInt64 => cast_decimal_to_integer::<D, UInt64Type>(array, base, *scale, cast_options),
2357 Int8 => cast_decimal_to_integer::<D, Int8Type>(array, base, *scale, cast_options),
2358 Int16 => cast_decimal_to_integer::<D, Int16Type>(array, base, *scale, cast_options),
2359 Int32 => cast_decimal_to_integer::<D, Int32Type>(array, base, *scale, cast_options),
2360 Int64 => cast_decimal_to_integer::<D, Int64Type>(array, base, *scale, cast_options),
2361 Float16 => cast_decimal_to_float::<D, Float16Type, _>(array, |x| {
2362 half::f16::from_f64(single_decimal_to_float_lossy::<D, F>(
2363 &as_float,
2364 x,
2365 <i32 as From<i8>>::from(*scale),
2366 ))
2367 }),
2368 Float32 => cast_decimal_to_float::<D, Float32Type, _>(array, |x| {
2369 single_decimal_to_float_lossy::<D, F>(&as_float, x, <i32 as From<i8>>::from(*scale))
2370 as f32
2371 }),
2372 Float64 => cast_decimal_to_float::<D, Float64Type, _>(array, |x| {
2373 single_decimal_to_float_lossy::<D, F>(&as_float, x, <i32 as From<i8>>::from(*scale))
2374 }),
2375 Utf8View => value_to_string_view(array, cast_options),
2376 Utf8 => value_to_string::<i32>(array, cast_options),
2377 LargeUtf8 => value_to_string::<i64>(array, cast_options),
2378 Null => Ok(new_null_array(to_type, array.len())),
2379 _ => Err(ArrowError::CastError(format!(
2380 "Casting from {from_type} to {to_type} not supported"
2381 ))),
2382 }
2383}
2384
2385fn cast_to_decimal<D, M>(
2386 array: &dyn Array,
2387 base: M,
2388 precision: &u8,
2389 scale: &i8,
2390 from_type: &DataType,
2391 to_type: &DataType,
2392 cast_options: &CastOptions,
2393) -> Result<ArrayRef, ArrowError>
2394where
2395 D: DecimalType + ArrowPrimitiveType<Native = M>,
2396 M: ArrowNativeTypeOp + DecimalCast,
2397{
2398 use DataType::*;
2399 match from_type {
2401 UInt8 => cast_integer_to_decimal::<_, D, M>(
2402 array.as_primitive::<UInt8Type>(),
2403 *precision,
2404 *scale,
2405 base,
2406 cast_options,
2407 ),
2408 UInt16 => cast_integer_to_decimal::<_, D, _>(
2409 array.as_primitive::<UInt16Type>(),
2410 *precision,
2411 *scale,
2412 base,
2413 cast_options,
2414 ),
2415 UInt32 => cast_integer_to_decimal::<_, D, _>(
2416 array.as_primitive::<UInt32Type>(),
2417 *precision,
2418 *scale,
2419 base,
2420 cast_options,
2421 ),
2422 UInt64 => cast_integer_to_decimal::<_, D, _>(
2423 array.as_primitive::<UInt64Type>(),
2424 *precision,
2425 *scale,
2426 base,
2427 cast_options,
2428 ),
2429 Int8 => cast_integer_to_decimal::<_, D, _>(
2430 array.as_primitive::<Int8Type>(),
2431 *precision,
2432 *scale,
2433 base,
2434 cast_options,
2435 ),
2436 Int16 => cast_integer_to_decimal::<_, D, _>(
2437 array.as_primitive::<Int16Type>(),
2438 *precision,
2439 *scale,
2440 base,
2441 cast_options,
2442 ),
2443 Int32 => cast_integer_to_decimal::<_, D, _>(
2444 array.as_primitive::<Int32Type>(),
2445 *precision,
2446 *scale,
2447 base,
2448 cast_options,
2449 ),
2450 Int64 => cast_integer_to_decimal::<_, D, _>(
2451 array.as_primitive::<Int64Type>(),
2452 *precision,
2453 *scale,
2454 base,
2455 cast_options,
2456 ),
2457 Float16 => cast_floating_point_to_decimal::<_, D>(
2458 array.as_primitive::<Float16Type>(),
2459 *precision,
2460 *scale,
2461 cast_options,
2462 ),
2463 Float32 => cast_floating_point_to_decimal::<_, D>(
2464 array.as_primitive::<Float32Type>(),
2465 *precision,
2466 *scale,
2467 cast_options,
2468 ),
2469 Float64 => cast_floating_point_to_decimal::<_, D>(
2470 array.as_primitive::<Float64Type>(),
2471 *precision,
2472 *scale,
2473 cast_options,
2474 ),
2475 Utf8View | Utf8 => {
2476 cast_string_to_decimal::<D, i32>(array, *precision, *scale, cast_options)
2477 }
2478 LargeUtf8 => cast_string_to_decimal::<D, i64>(array, *precision, *scale, cast_options),
2479 Null => Ok(new_null_array(to_type, array.len())),
2480 _ => Err(ArrowError::CastError(format!(
2481 "Casting from {from_type} to {to_type} not supported"
2482 ))),
2483 }
2484}
2485
2486const fn time_unit_multiple(unit: &TimeUnit) -> i64 {
2488 match unit {
2489 TimeUnit::Second => 1,
2490 TimeUnit::Millisecond => MILLISECONDS,
2491 TimeUnit::Microsecond => MICROSECONDS,
2492 TimeUnit::Nanosecond => NANOSECONDS,
2493 }
2494}
2495
2496fn cast_time64_to_time32<FROM, TO>(
2497 array: &dyn Array,
2498 divisor: i64,
2499 cast_options: &CastOptions,
2500) -> Result<ArrayRef, ArrowError>
2501where
2502 FROM: ArrowPrimitiveType<Native = i64>,
2503 TO: ArrowPrimitiveType<Native = i32>,
2504{
2505 let array = array.as_primitive::<FROM>();
2506 let result = if cast_options.safe {
2507 array.unary_opt::<_, TO>(|value| i32::try_from(value / divisor).ok())
2508 } else {
2509 array.try_unary::<_, TO, _>(|value| {
2510 let value = value / divisor;
2511 i32::try_from(value).map_err(|_| {
2512 ArrowError::CastError(format!(
2513 "Can't cast value {value:?} to type {}",
2514 TO::DATA_TYPE
2515 ))
2516 })
2517 })?
2518 };
2519
2520 Ok(Arc::new(result))
2521}
2522
2523fn cast_numeric_arrays<FROM, TO>(
2525 from: &dyn Array,
2526 cast_options: &CastOptions,
2527) -> Result<ArrayRef, ArrowError>
2528where
2529 FROM: ArrowPrimitiveType,
2530 TO: ArrowPrimitiveType,
2531 FROM::Native: NumCast,
2532 TO::Native: NumCast,
2533{
2534 if cast_options.safe {
2535 Ok(Arc::new(numeric_cast::<FROM, TO>(
2537 from.as_primitive::<FROM>(),
2538 )))
2539 } else {
2540 Ok(Arc::new(try_numeric_cast::<FROM, TO>(
2542 from.as_primitive::<FROM>(),
2543 )?))
2544 }
2545}
2546
2547fn try_numeric_cast<T, R>(from: &PrimitiveArray<T>) -> Result<PrimitiveArray<R>, ArrowError>
2550where
2551 T: ArrowPrimitiveType,
2552 R: ArrowPrimitiveType,
2553 T::Native: NumCast,
2554 R::Native: NumCast,
2555{
2556 from.try_unary(|value| {
2557 num_cast::<T::Native, R::Native>(value).ok_or_else(|| {
2558 ArrowError::CastError(format!(
2559 "Can't cast value {:?} to type {}",
2560 value,
2561 R::DATA_TYPE
2562 ))
2563 })
2564 })
2565}
2566
2567#[inline]
2570pub fn num_cast<I, O>(value: I) -> Option<O>
2571where
2572 I: NumCast,
2573 O: NumCast,
2574{
2575 num_traits::cast::cast::<I, O>(value)
2576}
2577
2578fn numeric_cast<T, R>(from: &PrimitiveArray<T>) -> PrimitiveArray<R>
2581where
2582 T: ArrowPrimitiveType,
2583 R: ArrowPrimitiveType,
2584 T::Native: NumCast,
2585 R::Native: NumCast,
2586{
2587 from.unary_opt::<_, R>(num_cast::<T::Native, R::Native>)
2588}
2589
2590fn cast_numeric_to_binary<FROM: ArrowPrimitiveType, O: OffsetSizeTrait>(
2591 array: &dyn Array,
2592) -> Result<ArrayRef, ArrowError> {
2593 let array = array.as_primitive::<FROM>();
2594 let size = std::mem::size_of::<FROM::Native>();
2595 let offsets = OffsetBuffer::from_repeated_length(size, array.len());
2596 Ok(Arc::new(GenericBinaryArray::<O>::try_new(
2597 offsets,
2598 array.values().inner().clone(),
2599 array.nulls().cloned(),
2600 )?))
2601}
2602
2603fn adjust_timestamp_to_timezone<T: ArrowTimestampType>(
2604 array: PrimitiveArray<Int64Type>,
2605 to_tz: &Tz,
2606 cast_options: &CastOptions,
2607) -> Result<PrimitiveArray<Int64Type>, ArrowError> {
2608 let adjust = |o| {
2609 let local = as_datetime::<T>(o)?;
2610 let offset = to_tz.offset_from_local_datetime(&local).single()?;
2611 T::from_naive_datetime(local - offset.fix(), None)
2612 };
2613 let adjusted = if cast_options.safe {
2614 array.unary_opt::<_, Int64Type>(adjust)
2615 } else {
2616 array.try_unary::<_, Int64Type, _>(|o| {
2617 adjust(o).ok_or_else(|| {
2618 ArrowError::CastError("Cannot cast timezone to different timezone".to_string())
2619 })
2620 })?
2621 };
2622 Ok(adjusted)
2623}
2624
2625fn cast_numeric_to_bool<FROM>(from: &dyn Array) -> Result<ArrayRef, ArrowError>
2629where
2630 FROM: ArrowPrimitiveType,
2631{
2632 numeric_to_bool_cast::<FROM>(from.as_primitive::<FROM>()).map(|to| Arc::new(to) as ArrayRef)
2633}
2634
2635fn numeric_to_bool_cast<T>(from: &PrimitiveArray<T>) -> Result<BooleanArray, ArrowError>
2636where
2637 T: ArrowPrimitiveType,
2638{
2639 let mut b = BooleanBuilder::with_capacity(from.len());
2640
2641 for i in 0..from.len() {
2642 if from.is_null(i) {
2643 b.append_null();
2644 } else {
2645 b.append_value(cast_num_to_bool::<T::Native>(from.value(i)));
2646 }
2647 }
2648
2649 Ok(b.finish())
2650}
2651
2652#[inline]
2654pub fn cast_num_to_bool<I>(value: I) -> bool
2655where
2656 I: Default + PartialEq,
2657{
2658 value != I::default()
2659}
2660
2661fn cast_bool_to_numeric<TO>(
2665 from: &dyn Array,
2666 cast_options: &CastOptions,
2667) -> Result<ArrayRef, ArrowError>
2668where
2669 TO: ArrowPrimitiveType,
2670 TO::Native: num_traits::cast::NumCast,
2671{
2672 Ok(Arc::new(bool_to_numeric_cast::<TO>(
2673 from.as_any().downcast_ref::<BooleanArray>().unwrap(),
2674 cast_options,
2675 )))
2676}
2677
2678fn bool_to_numeric_cast<T>(from: &BooleanArray, _cast_options: &CastOptions) -> PrimitiveArray<T>
2679where
2680 T: ArrowPrimitiveType,
2681 T::Native: num_traits::NumCast,
2682{
2683 let iter = (0..from.len()).map(|i| {
2684 if from.is_null(i) {
2685 None
2686 } else {
2687 single_bool_to_numeric::<T::Native>(from.value(i))
2688 }
2689 });
2690 unsafe { PrimitiveArray::<T>::from_trusted_len_iter(iter) }
2695}
2696
2697#[inline]
2699pub fn single_bool_to_numeric<O>(value: bool) -> Option<O>
2700where
2701 O: num_traits::NumCast + Default,
2702{
2703 if value {
2704 num_traits::cast::cast(1)
2706 } else {
2707 Some(O::default())
2708 }
2709}
2710
2711fn cast_binary_to_fixed_size_binary<O: OffsetSizeTrait>(
2713 array: &dyn Array,
2714 byte_width: i32,
2715 cast_options: &CastOptions,
2716) -> Result<ArrayRef, ArrowError> {
2717 let array = array.as_binary::<O>();
2718 let mut builder = FixedSizeBinaryBuilder::with_capacity(array.len(), byte_width);
2719
2720 for i in 0..array.len() {
2721 if array.is_null(i) {
2722 builder.append_null();
2723 } else {
2724 match builder.append_value(array.value(i)) {
2725 Ok(()) => {}
2726 Err(e) => match cast_options.safe {
2727 true => builder.append_null(),
2728 false => return Err(e),
2729 },
2730 }
2731 }
2732 }
2733
2734 Ok(Arc::new(builder.finish()))
2735}
2736
2737fn cast_fixed_size_binary_to_binary<O: OffsetSizeTrait>(
2740 array: &dyn Array,
2741 byte_width: i32,
2742) -> Result<ArrayRef, ArrowError> {
2743 let array = array
2744 .as_any()
2745 .downcast_ref::<FixedSizeBinaryArray>()
2746 .unwrap();
2747
2748 let offsets: i128 = byte_width as i128 * array.len() as i128;
2749
2750 let is_binary = matches!(GenericBinaryType::<O>::DATA_TYPE, DataType::Binary);
2751 if is_binary && offsets > i32::MAX as i128 {
2752 return Err(ArrowError::ComputeError(
2753 "FixedSizeBinary array too large to cast to Binary array".to_string(),
2754 ));
2755 } else if !is_binary && offsets > i64::MAX as i128 {
2756 return Err(ArrowError::ComputeError(
2757 "FixedSizeBinary array too large to cast to LargeBinary array".to_string(),
2758 ));
2759 }
2760
2761 let mut builder = GenericBinaryBuilder::<O>::with_capacity(array.len(), array.len());
2762
2763 for i in 0..array.len() {
2764 if array.is_null(i) {
2765 builder.append_null();
2766 } else {
2767 builder.append_value(array.value(i));
2768 }
2769 }
2770
2771 Ok(Arc::new(builder.finish()))
2772}
2773
2774fn cast_fixed_size_binary_to_binary_view(
2775 array: &dyn Array,
2776 _byte_width: i32,
2777) -> Result<ArrayRef, ArrowError> {
2778 let array = array
2779 .as_any()
2780 .downcast_ref::<FixedSizeBinaryArray>()
2781 .unwrap();
2782
2783 let mut builder = BinaryViewBuilder::with_capacity(array.len());
2784 for i in 0..array.len() {
2785 if array.is_null(i) {
2786 builder.append_null();
2787 } else {
2788 builder.append_value(array.value(i));
2789 }
2790 }
2791
2792 Ok(Arc::new(builder.finish()))
2793}
2794
2795fn cast_byte_container<FROM, TO>(array: &dyn Array) -> Result<ArrayRef, ArrowError>
2798where
2799 FROM: ByteArrayType,
2800 TO: ByteArrayType<Native = FROM::Native>,
2801 FROM::Offset: OffsetSizeTrait + ToPrimitive,
2802 TO::Offset: OffsetSizeTrait + NumCast,
2803{
2804 let data = array.to_data();
2805 assert_eq!(data.data_type(), &FROM::DATA_TYPE);
2806 let str_values_buf = data.buffers()[1].clone();
2807 let offsets = data.buffers()[0].typed_data::<FROM::Offset>();
2808
2809 let mut cast_offsets = Vec::<TO::Offset>::with_capacity(offsets.len());
2810 offsets
2811 .iter()
2812 .try_for_each::<_, Result<_, ArrowError>>(|offset| {
2813 let offset =
2814 <<TO as ByteArrayType>::Offset as NumCast>::from(*offset).ok_or_else(|| {
2815 ArrowError::ComputeError(format!(
2816 "{}{} array too large to cast to {}{} array",
2817 FROM::Offset::PREFIX,
2818 FROM::PREFIX,
2819 TO::Offset::PREFIX,
2820 TO::PREFIX
2821 ))
2822 })?;
2823 cast_offsets.push(offset);
2824 Ok(())
2825 })?;
2826
2827 let offset_buffer = Buffer::from_vec(cast_offsets);
2828
2829 let dtype = TO::DATA_TYPE;
2830
2831 let builder = ArrayData::builder(dtype)
2832 .offset(array.offset())
2833 .len(array.len())
2834 .add_buffer(offset_buffer)
2835 .add_buffer(str_values_buf)
2836 .nulls(data.nulls().cloned());
2837
2838 let array_data = unsafe { builder.build_unchecked() };
2839
2840 Ok(Arc::new(GenericByteArray::<TO>::from(array_data)))
2841}
2842
2843fn cast_view_to_byte<FROM, TO>(array: &dyn Array) -> Result<ArrayRef, ArrowError>
2845where
2846 FROM: ByteViewType,
2847 TO: ByteArrayType,
2848 FROM::Native: AsRef<TO::Native>,
2849{
2850 let data = array.to_data();
2851 let view_array = GenericByteViewArray::<FROM>::from(data);
2852
2853 let len = view_array.len();
2854 let bytes = view_array
2855 .views()
2856 .iter()
2857 .map(|v| ByteView::from(*v).length as usize)
2858 .sum::<usize>();
2859
2860 let mut byte_array_builder = GenericByteBuilder::<TO>::with_capacity(len, bytes);
2861
2862 for val in &view_array {
2863 byte_array_builder.append_option(val);
2864 }
2865
2866 Ok(Arc::new(byte_array_builder.finish()))
2867}
2868
2869#[cfg(test)]
2870mod tests {
2871 use super::*;
2872 use crate::parse::parse_decimal;
2873 use DataType::*;
2874 use arrow_array::{Int64Array, RunArray, StringArray};
2875 use arrow_buffer::{Buffer, IntervalDayTime, NullBuffer};
2876 use arrow_buffer::{ScalarBuffer, i256};
2877 use arrow_schema::{DataType, Field};
2878 use chrono::NaiveDate;
2879 use half::f16;
2880 use std::sync::Arc;
2881
2882 #[derive(Clone)]
2883 struct DecimalCastTestConfig {
2884 input_prec: u8,
2885 input_scale: i8,
2886 input_repr: i128,
2887 output_prec: u8,
2888 output_scale: i8,
2889 expected_output_repr: Result<i128, String>, }
2894
2895 macro_rules! generate_cast_test_case {
2896 ($INPUT_ARRAY: expr, $OUTPUT_TYPE_ARRAY: ident, $OUTPUT_TYPE: expr, $OUTPUT_VALUES: expr) => {
2897 let output =
2898 $OUTPUT_TYPE_ARRAY::from($OUTPUT_VALUES).with_data_type($OUTPUT_TYPE.clone());
2899
2900 let input_array_type = $INPUT_ARRAY.data_type();
2902 assert!(can_cast_types(input_array_type, $OUTPUT_TYPE));
2903 let result = cast($INPUT_ARRAY, $OUTPUT_TYPE).unwrap();
2904 assert_eq!($OUTPUT_TYPE, result.data_type());
2905 assert_eq!(result.as_ref(), &output);
2906
2907 let cast_option = CastOptions {
2908 safe: false,
2909 format_options: FormatOptions::default(),
2910 };
2911 let result = cast_with_options($INPUT_ARRAY, $OUTPUT_TYPE, &cast_option).unwrap();
2912 assert_eq!($OUTPUT_TYPE, result.data_type());
2913 assert_eq!(result.as_ref(), &output);
2914 };
2915 }
2916
2917 fn run_decimal_cast_test_case<I, O>(t: DecimalCastTestConfig)
2918 where
2919 I: DecimalType,
2920 O: DecimalType,
2921 I::Native: DecimalCast,
2922 O::Native: DecimalCast,
2923 {
2924 let array = vec![I::Native::from_decimal(t.input_repr)];
2925 let array = array
2926 .into_iter()
2927 .collect::<PrimitiveArray<I>>()
2928 .with_precision_and_scale(t.input_prec, t.input_scale)
2929 .unwrap();
2930 let input_type = array.data_type();
2931 let output_type = O::TYPE_CONSTRUCTOR(t.output_prec, t.output_scale);
2932 assert!(can_cast_types(input_type, &output_type));
2933
2934 let options = CastOptions {
2935 safe: false,
2936 ..Default::default()
2937 };
2938 let result = cast_with_options(&array, &output_type, &options);
2939
2940 match t.expected_output_repr {
2941 Ok(v) => {
2942 let expected_array = vec![O::Native::from_decimal(v)];
2943 let expected_array = expected_array
2944 .into_iter()
2945 .collect::<PrimitiveArray<O>>()
2946 .with_precision_and_scale(t.output_prec, t.output_scale)
2947 .unwrap();
2948 assert_eq!(*result.unwrap(), expected_array);
2949 }
2950 Err(expected_output_message_template) => {
2951 assert!(result.is_err());
2952 let expected_error_message =
2953 expected_output_message_template.replace("{}", O::PREFIX);
2954 assert_eq!(result.unwrap_err().to_string(), expected_error_message);
2955 }
2956 }
2957 }
2958
2959 fn create_decimal32_array(
2960 array: Vec<Option<i32>>,
2961 precision: u8,
2962 scale: i8,
2963 ) -> Result<Decimal32Array, ArrowError> {
2964 array
2965 .into_iter()
2966 .collect::<Decimal32Array>()
2967 .with_precision_and_scale(precision, scale)
2968 }
2969
2970 fn create_decimal64_array(
2971 array: Vec<Option<i64>>,
2972 precision: u8,
2973 scale: i8,
2974 ) -> Result<Decimal64Array, ArrowError> {
2975 array
2976 .into_iter()
2977 .collect::<Decimal64Array>()
2978 .with_precision_and_scale(precision, scale)
2979 }
2980
2981 fn create_decimal128_array(
2982 array: Vec<Option<i128>>,
2983 precision: u8,
2984 scale: i8,
2985 ) -> Result<Decimal128Array, ArrowError> {
2986 array
2987 .into_iter()
2988 .collect::<Decimal128Array>()
2989 .with_precision_and_scale(precision, scale)
2990 }
2991
2992 fn create_decimal256_array(
2993 array: Vec<Option<i256>>,
2994 precision: u8,
2995 scale: i8,
2996 ) -> Result<Decimal256Array, ArrowError> {
2997 array
2998 .into_iter()
2999 .collect::<Decimal256Array>()
3000 .with_precision_and_scale(precision, scale)
3001 }
3002
3003 #[test]
3004 #[should_panic(
3005 expected = "Cannot cast to Decimal128(20, 3). Overflowing on 57896044618658097711785492504343953926634992332820282019728792003956564819967"
3006 )]
3007 fn test_cast_decimal_to_decimal_round_with_error() {
3008 let array = vec![
3010 Some(i256::from_i128(1123454)),
3011 Some(i256::from_i128(2123456)),
3012 Some(i256::from_i128(-3123453)),
3013 Some(i256::from_i128(-3123456)),
3014 None,
3015 Some(i256::MAX),
3016 Some(i256::MIN),
3017 ];
3018 let input_decimal_array = create_decimal256_array(array, 76, 4).unwrap();
3019 let array = Arc::new(input_decimal_array) as ArrayRef;
3020 let input_type = DataType::Decimal256(76, 4);
3021 let output_type = DataType::Decimal128(20, 3);
3022 assert!(can_cast_types(&input_type, &output_type));
3023 generate_cast_test_case!(
3024 &array,
3025 Decimal128Array,
3026 &output_type,
3027 vec![
3028 Some(112345_i128),
3029 Some(212346_i128),
3030 Some(-312345_i128),
3031 Some(-312346_i128),
3032 None,
3033 None,
3034 None,
3035 ]
3036 );
3037 }
3038
3039 #[test]
3040 fn test_cast_decimal_to_decimal_round() {
3041 let array = vec![
3042 Some(1123454),
3043 Some(2123456),
3044 Some(-3123453),
3045 Some(-3123456),
3046 None,
3047 ];
3048 let array = create_decimal128_array(array, 20, 4).unwrap();
3049 let input_type = DataType::Decimal128(20, 4);
3051 let output_type = DataType::Decimal128(20, 3);
3052 assert!(can_cast_types(&input_type, &output_type));
3053 generate_cast_test_case!(
3054 &array,
3055 Decimal128Array,
3056 &output_type,
3057 vec![
3058 Some(112345_i128),
3059 Some(212346_i128),
3060 Some(-312345_i128),
3061 Some(-312346_i128),
3062 None
3063 ]
3064 );
3065
3066 let input_type = DataType::Decimal128(20, 4);
3068 let output_type = DataType::Decimal256(20, 3);
3069 assert!(can_cast_types(&input_type, &output_type));
3070 generate_cast_test_case!(
3071 &array,
3072 Decimal256Array,
3073 &output_type,
3074 vec![
3075 Some(i256::from_i128(112345_i128)),
3076 Some(i256::from_i128(212346_i128)),
3077 Some(i256::from_i128(-312345_i128)),
3078 Some(i256::from_i128(-312346_i128)),
3079 None
3080 ]
3081 );
3082
3083 let array = vec![
3085 Some(i256::from_i128(1123454)),
3086 Some(i256::from_i128(2123456)),
3087 Some(i256::from_i128(-3123453)),
3088 Some(i256::from_i128(-3123456)),
3089 None,
3090 ];
3091 let array = create_decimal256_array(array, 20, 4).unwrap();
3092
3093 let input_type = DataType::Decimal256(20, 4);
3095 let output_type = DataType::Decimal256(20, 3);
3096 assert!(can_cast_types(&input_type, &output_type));
3097 generate_cast_test_case!(
3098 &array,
3099 Decimal256Array,
3100 &output_type,
3101 vec![
3102 Some(i256::from_i128(112345_i128)),
3103 Some(i256::from_i128(212346_i128)),
3104 Some(i256::from_i128(-312345_i128)),
3105 Some(i256::from_i128(-312346_i128)),
3106 None
3107 ]
3108 );
3109 let input_type = DataType::Decimal256(20, 4);
3111 let output_type = DataType::Decimal128(20, 3);
3112 assert!(can_cast_types(&input_type, &output_type));
3113 generate_cast_test_case!(
3114 &array,
3115 Decimal128Array,
3116 &output_type,
3117 vec![
3118 Some(112345_i128),
3119 Some(212346_i128),
3120 Some(-312345_i128),
3121 Some(-312346_i128),
3122 None
3123 ]
3124 );
3125 }
3126
3127 #[test]
3128 fn test_cast_decimal32_to_decimal32() {
3129 let input_type = DataType::Decimal32(9, 3);
3131 let output_type = DataType::Decimal32(9, 4);
3132 assert!(can_cast_types(&input_type, &output_type));
3133 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3134 let array = create_decimal32_array(array, 9, 3).unwrap();
3135 generate_cast_test_case!(
3136 &array,
3137 Decimal32Array,
3138 &output_type,
3139 vec![
3140 Some(11234560_i32),
3141 Some(21234560_i32),
3142 Some(31234560_i32),
3143 None
3144 ]
3145 );
3146 let array = vec![Some(123456), None];
3148 let array = create_decimal32_array(array, 9, 0).unwrap();
3149 let result_safe = cast(&array, &DataType::Decimal32(2, 2));
3150 assert!(result_safe.is_ok());
3151 let options = CastOptions {
3152 safe: false,
3153 ..Default::default()
3154 };
3155
3156 let result_unsafe = cast_with_options(&array, &DataType::Decimal32(2, 2), &options);
3157 assert_eq!(
3158 "Invalid argument error: 123456.00 is too large to store in a Decimal32 of precision 2. Max is 0.99",
3159 result_unsafe.unwrap_err().to_string()
3160 );
3161 }
3162
3163 #[test]
3164 fn test_cast_decimal64_to_decimal64() {
3165 let input_type = DataType::Decimal64(17, 3);
3167 let output_type = DataType::Decimal64(17, 4);
3168 assert!(can_cast_types(&input_type, &output_type));
3169 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3170 let array = create_decimal64_array(array, 17, 3).unwrap();
3171 generate_cast_test_case!(
3172 &array,
3173 Decimal64Array,
3174 &output_type,
3175 vec![
3176 Some(11234560_i64),
3177 Some(21234560_i64),
3178 Some(31234560_i64),
3179 None
3180 ]
3181 );
3182 let array = vec![Some(123456), None];
3184 let array = create_decimal64_array(array, 9, 0).unwrap();
3185 let result_safe = cast(&array, &DataType::Decimal64(2, 2));
3186 assert!(result_safe.is_ok());
3187 let options = CastOptions {
3188 safe: false,
3189 ..Default::default()
3190 };
3191
3192 let result_unsafe = cast_with_options(&array, &DataType::Decimal64(2, 2), &options);
3193 assert_eq!(
3194 "Invalid argument error: 123456.00 is too large to store in a Decimal64 of precision 2. Max is 0.99",
3195 result_unsafe.unwrap_err().to_string()
3196 );
3197 }
3198
3199 #[test]
3200 fn test_cast_decimal128_to_decimal128() {
3201 let input_type = DataType::Decimal128(20, 3);
3203 let output_type = DataType::Decimal128(20, 4);
3204 assert!(can_cast_types(&input_type, &output_type));
3205 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3206 let array = create_decimal128_array(array, 20, 3).unwrap();
3207 generate_cast_test_case!(
3208 &array,
3209 Decimal128Array,
3210 &output_type,
3211 vec![
3212 Some(11234560_i128),
3213 Some(21234560_i128),
3214 Some(31234560_i128),
3215 None
3216 ]
3217 );
3218 let array = vec![Some(123456), None];
3220 let array = create_decimal128_array(array, 10, 0).unwrap();
3221 let result_safe = cast(&array, &DataType::Decimal128(2, 2));
3222 assert!(result_safe.is_ok());
3223 let options = CastOptions {
3224 safe: false,
3225 ..Default::default()
3226 };
3227
3228 let result_unsafe = cast_with_options(&array, &DataType::Decimal128(2, 2), &options);
3229 assert_eq!(
3230 "Invalid argument error: 123456.00 is too large to store in a Decimal128 of precision 2. Max is 0.99",
3231 result_unsafe.unwrap_err().to_string()
3232 );
3233 }
3234
3235 #[test]
3236 fn test_cast_decimal32_to_decimal32_dict() {
3237 let p = 9;
3238 let s = 3;
3239 let input_type = DataType::Decimal32(p, s);
3240 let output_type = DataType::Dictionary(
3241 Box::new(DataType::Int32),
3242 Box::new(DataType::Decimal32(p, s)),
3243 );
3244 assert!(can_cast_types(&input_type, &output_type));
3245 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3246 let array = create_decimal32_array(array, p, s).unwrap();
3247 let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3248 assert_eq!(cast_array.data_type(), &output_type);
3249 }
3250
3251 #[test]
3252 fn test_cast_decimal64_to_decimal64_dict() {
3253 let p = 15;
3254 let s = 3;
3255 let input_type = DataType::Decimal64(p, s);
3256 let output_type = DataType::Dictionary(
3257 Box::new(DataType::Int32),
3258 Box::new(DataType::Decimal64(p, s)),
3259 );
3260 assert!(can_cast_types(&input_type, &output_type));
3261 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3262 let array = create_decimal64_array(array, p, s).unwrap();
3263 let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3264 assert_eq!(cast_array.data_type(), &output_type);
3265 }
3266
3267 #[test]
3268 fn test_cast_decimal128_to_decimal128_dict() {
3269 let p = 20;
3270 let s = 3;
3271 let input_type = DataType::Decimal128(p, s);
3272 let output_type = DataType::Dictionary(
3273 Box::new(DataType::Int32),
3274 Box::new(DataType::Decimal128(p, s)),
3275 );
3276 assert!(can_cast_types(&input_type, &output_type));
3277 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3278 let array = create_decimal128_array(array, p, s).unwrap();
3279 let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3280 assert_eq!(cast_array.data_type(), &output_type);
3281 }
3282
3283 #[test]
3284 fn test_cast_decimal256_to_decimal256_dict() {
3285 let p = 20;
3286 let s = 3;
3287 let input_type = DataType::Decimal256(p, s);
3288 let output_type = DataType::Dictionary(
3289 Box::new(DataType::Int32),
3290 Box::new(DataType::Decimal256(p, s)),
3291 );
3292 assert!(can_cast_types(&input_type, &output_type));
3293 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3294 let array = create_decimal128_array(array, p, s).unwrap();
3295 let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3296 assert_eq!(cast_array.data_type(), &output_type);
3297 }
3298
3299 #[test]
3300 fn test_cast_decimal32_to_decimal32_overflow() {
3301 let input_type = DataType::Decimal32(9, 3);
3302 let output_type = DataType::Decimal32(9, 9);
3303 assert!(can_cast_types(&input_type, &output_type));
3304
3305 let array = vec![Some(i32::MAX)];
3306 let array = create_decimal32_array(array, 9, 3).unwrap();
3307 let result = cast_with_options(
3308 &array,
3309 &output_type,
3310 &CastOptions {
3311 safe: false,
3312 format_options: FormatOptions::default(),
3313 },
3314 );
3315 assert_eq!(
3316 "Cast error: Cannot cast to Decimal32(9, 9). Overflowing on 2147483647",
3317 result.unwrap_err().to_string()
3318 );
3319 }
3320
3321 #[test]
3322 fn test_cast_decimal32_to_decimal32_large_scale_reduction() {
3323 let array = vec![Some(-999999999), Some(0), Some(999999999), None];
3324 let array = create_decimal32_array(array, 9, 3).unwrap();
3325
3326 let output_type = DataType::Decimal32(9, -6);
3328 assert!(can_cast_types(array.data_type(), &output_type));
3329 generate_cast_test_case!(
3330 &array,
3331 Decimal32Array,
3332 &output_type,
3333 vec![Some(-1), Some(0), Some(1), None]
3334 );
3335
3336 let output_type = DataType::Decimal32(9, -7);
3338 assert!(can_cast_types(array.data_type(), &output_type));
3339 generate_cast_test_case!(
3340 &array,
3341 Decimal32Array,
3342 &output_type,
3343 vec![Some(0), Some(0), Some(0), None]
3344 );
3345 }
3346
3347 #[test]
3348 fn test_cast_decimal64_to_decimal64_overflow() {
3349 let input_type = DataType::Decimal64(18, 3);
3350 let output_type = DataType::Decimal64(18, 18);
3351 assert!(can_cast_types(&input_type, &output_type));
3352
3353 let array = vec![Some(i64::MAX)];
3354 let array = create_decimal64_array(array, 18, 3).unwrap();
3355 let result = cast_with_options(
3356 &array,
3357 &output_type,
3358 &CastOptions {
3359 safe: false,
3360 format_options: FormatOptions::default(),
3361 },
3362 );
3363 assert_eq!(
3364 "Cast error: Cannot cast to Decimal64(18, 18). Overflowing on 9223372036854775807",
3365 result.unwrap_err().to_string()
3366 );
3367 }
3368
3369 #[test]
3370 fn test_cast_decimal64_to_decimal64_large_scale_reduction() {
3371 let array = vec![
3372 Some(-999999999999999999),
3373 Some(0),
3374 Some(999999999999999999),
3375 None,
3376 ];
3377 let array = create_decimal64_array(array, 18, 3).unwrap();
3378
3379 let output_type = DataType::Decimal64(18, -15);
3381 assert!(can_cast_types(array.data_type(), &output_type));
3382 generate_cast_test_case!(
3383 &array,
3384 Decimal64Array,
3385 &output_type,
3386 vec![Some(-1), Some(0), Some(1), None]
3387 );
3388
3389 let output_type = DataType::Decimal64(18, -16);
3391 assert!(can_cast_types(array.data_type(), &output_type));
3392 generate_cast_test_case!(
3393 &array,
3394 Decimal64Array,
3395 &output_type,
3396 vec![Some(0), Some(0), Some(0), None]
3397 );
3398 }
3399
3400 #[test]
3401 fn test_cast_floating_to_decimals() {
3402 for output_type in [
3403 DataType::Decimal32(9, 3),
3404 DataType::Decimal64(9, 3),
3405 DataType::Decimal128(9, 3),
3406 DataType::Decimal256(9, 3),
3407 ] {
3408 let input_type = DataType::Float64;
3409 assert!(can_cast_types(&input_type, &output_type));
3410
3411 let array = vec![Some(1.1_f64)];
3412 let array = PrimitiveArray::<Float64Type>::from_iter(array);
3413 let result = cast_with_options(
3414 &array,
3415 &output_type,
3416 &CastOptions {
3417 safe: false,
3418 format_options: FormatOptions::default(),
3419 },
3420 );
3421 assert!(
3422 result.is_ok(),
3423 "Failed to cast to {output_type} with: {}",
3424 result.unwrap_err()
3425 );
3426 }
3427 }
3428
3429 #[test]
3430 #[cfg_attr(miri, ignore)] fn test_cast_float16_to_decimals() {
3432 let array = Float16Array::from(vec![
3433 Some(f16::from_f32(1.25)),
3434 Some(f16::from_f32(-2.5)),
3435 Some(f16::from_f32(1.125)),
3436 Some(f16::from_f32(-1.125)),
3437 Some(f16::from_f32(0.0)),
3438 None,
3439 ]);
3440
3441 generate_cast_test_case!(
3442 &array,
3443 Decimal32Array,
3444 &DataType::Decimal32(9, 2),
3445 vec![
3446 Some(125_i32),
3447 Some(-250_i32),
3448 Some(113_i32),
3449 Some(-113_i32),
3450 Some(0_i32),
3451 None
3452 ]
3453 );
3454 generate_cast_test_case!(
3455 &array,
3456 Decimal64Array,
3457 &DataType::Decimal64(18, 2),
3458 vec![
3459 Some(125_i64),
3460 Some(-250_i64),
3461 Some(113_i64),
3462 Some(-113_i64),
3463 Some(0_i64),
3464 None
3465 ]
3466 );
3467 generate_cast_test_case!(
3468 &array,
3469 Decimal128Array,
3470 &DataType::Decimal128(38, 2),
3471 vec![
3472 Some(125_i128),
3473 Some(-250_i128),
3474 Some(113_i128),
3475 Some(-113_i128),
3476 Some(0_i128),
3477 None
3478 ]
3479 );
3480 generate_cast_test_case!(
3481 &array,
3482 Decimal256Array,
3483 &DataType::Decimal256(76, 2),
3484 vec![
3485 Some(i256::from_i128(125_i128)),
3486 Some(i256::from_i128(-250_i128)),
3487 Some(i256::from_i128(113_i128)),
3488 Some(i256::from_i128(-113_i128)),
3489 Some(i256::from_i128(0_i128)),
3490 None
3491 ]
3492 );
3493
3494 let array = Float16Array::from(vec![
3495 Some(f16::from_f32(1250.0)),
3496 Some(f16::from_f32(-1250.0)),
3497 Some(f16::from_f32(1249.0)),
3498 None,
3499 ]);
3500 generate_cast_test_case!(
3501 &array,
3502 Decimal128Array,
3503 &DataType::Decimal128(5, -2),
3504 vec![Some(13_i128), Some(-13_i128), Some(12_i128), None]
3505 );
3506 }
3507
3508 #[test]
3509 fn test_cast_decimal128_to_decimal128_overflow() {
3510 let input_type = DataType::Decimal128(38, 3);
3511 let output_type = DataType::Decimal128(38, 38);
3512 assert!(can_cast_types(&input_type, &output_type));
3513
3514 let array = vec![Some(i128::MAX)];
3515 let array = create_decimal128_array(array, 38, 3).unwrap();
3516 let result = cast_with_options(
3517 &array,
3518 &output_type,
3519 &CastOptions {
3520 safe: false,
3521 format_options: FormatOptions::default(),
3522 },
3523 );
3524 assert_eq!(
3525 "Cast error: Cannot cast to Decimal128(38, 38). Overflowing on 170141183460469231731687303715884105727",
3526 result.unwrap_err().to_string()
3527 );
3528 }
3529
3530 #[test]
3531 fn test_cast_decimal128_to_decimal256_overflow() {
3532 let input_type = DataType::Decimal128(38, 3);
3533 let output_type = DataType::Decimal256(76, 76);
3534 assert!(can_cast_types(&input_type, &output_type));
3535
3536 let array = vec![Some(i128::MAX)];
3537 let array = create_decimal128_array(array, 38, 3).unwrap();
3538 let result = cast_with_options(
3539 &array,
3540 &output_type,
3541 &CastOptions {
3542 safe: false,
3543 format_options: FormatOptions::default(),
3544 },
3545 );
3546 assert_eq!(
3547 "Cast error: Cannot cast to Decimal256(76, 76). Overflowing on 170141183460469231731687303715884105727",
3548 result.unwrap_err().to_string()
3549 );
3550 }
3551
3552 #[test]
3553 fn test_cast_decimal32_to_decimal256() {
3554 let input_type = DataType::Decimal32(8, 3);
3555 let output_type = DataType::Decimal256(20, 4);
3556 assert!(can_cast_types(&input_type, &output_type));
3557 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3558 let array = create_decimal32_array(array, 8, 3).unwrap();
3559 generate_cast_test_case!(
3560 &array,
3561 Decimal256Array,
3562 &output_type,
3563 vec![
3564 Some(i256::from_i128(11234560_i128)),
3565 Some(i256::from_i128(21234560_i128)),
3566 Some(i256::from_i128(31234560_i128)),
3567 None
3568 ]
3569 );
3570 }
3571 #[test]
3572 fn test_cast_decimal64_to_decimal256() {
3573 let input_type = DataType::Decimal64(12, 3);
3574 let output_type = DataType::Decimal256(20, 4);
3575 assert!(can_cast_types(&input_type, &output_type));
3576 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3577 let array = create_decimal64_array(array, 12, 3).unwrap();
3578 generate_cast_test_case!(
3579 &array,
3580 Decimal256Array,
3581 &output_type,
3582 vec![
3583 Some(i256::from_i128(11234560_i128)),
3584 Some(i256::from_i128(21234560_i128)),
3585 Some(i256::from_i128(31234560_i128)),
3586 None
3587 ]
3588 );
3589 }
3590 #[test]
3591 fn test_cast_decimal128_to_decimal256() {
3592 let input_type = DataType::Decimal128(20, 3);
3593 let output_type = DataType::Decimal256(20, 4);
3594 assert!(can_cast_types(&input_type, &output_type));
3595 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3596 let array = create_decimal128_array(array, 20, 3).unwrap();
3597 generate_cast_test_case!(
3598 &array,
3599 Decimal256Array,
3600 &output_type,
3601 vec![
3602 Some(i256::from_i128(11234560_i128)),
3603 Some(i256::from_i128(21234560_i128)),
3604 Some(i256::from_i128(31234560_i128)),
3605 None
3606 ]
3607 );
3608 }
3609
3610 #[test]
3611 fn test_cast_decimal256_to_decimal128_overflow() {
3612 let input_type = DataType::Decimal256(76, 5);
3613 let output_type = DataType::Decimal128(38, 7);
3614 assert!(can_cast_types(&input_type, &output_type));
3615 let array = vec![Some(i256::from_i128(i128::MAX))];
3616 let array = create_decimal256_array(array, 76, 5).unwrap();
3617 let result = cast_with_options(
3618 &array,
3619 &output_type,
3620 &CastOptions {
3621 safe: false,
3622 format_options: FormatOptions::default(),
3623 },
3624 );
3625 assert_eq!(
3626 "Cast error: Cannot cast to Decimal128(38, 7). Overflowing on 170141183460469231731687303715884105727",
3627 result.unwrap_err().to_string()
3628 );
3629 }
3630
3631 #[test]
3632 fn test_cast_decimal256_to_decimal256_overflow() {
3633 let input_type = DataType::Decimal256(76, 5);
3634 let output_type = DataType::Decimal256(76, 55);
3635 assert!(can_cast_types(&input_type, &output_type));
3636 let array = vec![Some(i256::from_i128(i128::MAX))];
3637 let array = create_decimal256_array(array, 76, 5).unwrap();
3638 let result = cast_with_options(
3639 &array,
3640 &output_type,
3641 &CastOptions {
3642 safe: false,
3643 format_options: FormatOptions::default(),
3644 },
3645 );
3646 assert_eq!(
3647 "Cast error: Cannot cast to Decimal256(76, 55). Overflowing on 170141183460469231731687303715884105727",
3648 result.unwrap_err().to_string()
3649 );
3650 }
3651
3652 #[test]
3653 fn test_cast_decimal256_to_decimal128() {
3654 let input_type = DataType::Decimal256(20, 3);
3655 let output_type = DataType::Decimal128(20, 4);
3656 assert!(can_cast_types(&input_type, &output_type));
3657 let array = vec![
3658 Some(i256::from_i128(1123456)),
3659 Some(i256::from_i128(2123456)),
3660 Some(i256::from_i128(3123456)),
3661 None,
3662 ];
3663 let array = create_decimal256_array(array, 20, 3).unwrap();
3664 generate_cast_test_case!(
3665 &array,
3666 Decimal128Array,
3667 &output_type,
3668 vec![
3669 Some(11234560_i128),
3670 Some(21234560_i128),
3671 Some(31234560_i128),
3672 None
3673 ]
3674 );
3675 }
3676
3677 #[test]
3678 fn test_cast_decimal256_to_decimal256() {
3679 let input_type = DataType::Decimal256(20, 3);
3680 let output_type = DataType::Decimal256(20, 4);
3681 assert!(can_cast_types(&input_type, &output_type));
3682 let array = vec![
3683 Some(i256::from_i128(1123456)),
3684 Some(i256::from_i128(2123456)),
3685 Some(i256::from_i128(3123456)),
3686 None,
3687 ];
3688 let array = create_decimal256_array(array, 20, 3).unwrap();
3689 generate_cast_test_case!(
3690 &array,
3691 Decimal256Array,
3692 &output_type,
3693 vec![
3694 Some(i256::from_i128(11234560_i128)),
3695 Some(i256::from_i128(21234560_i128)),
3696 Some(i256::from_i128(31234560_i128)),
3697 None
3698 ]
3699 );
3700 }
3701
3702 fn generate_decimal_to_numeric_cast_test_case<T>(array: &PrimitiveArray<T>)
3703 where
3704 T: ArrowPrimitiveType + DecimalType,
3705 {
3706 generate_cast_test_case!(
3708 array,
3709 UInt8Array,
3710 &DataType::UInt8,
3711 vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
3712 );
3713 generate_cast_test_case!(
3715 array,
3716 UInt16Array,
3717 &DataType::UInt16,
3718 vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
3719 );
3720 generate_cast_test_case!(
3722 array,
3723 UInt32Array,
3724 &DataType::UInt32,
3725 vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
3726 );
3727 generate_cast_test_case!(
3729 array,
3730 UInt64Array,
3731 &DataType::UInt64,
3732 vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
3733 );
3734 generate_cast_test_case!(
3736 array,
3737 Int8Array,
3738 &DataType::Int8,
3739 vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
3740 );
3741 generate_cast_test_case!(
3743 array,
3744 Int16Array,
3745 &DataType::Int16,
3746 vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
3747 );
3748 generate_cast_test_case!(
3750 array,
3751 Int32Array,
3752 &DataType::Int32,
3753 vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
3754 );
3755 generate_cast_test_case!(
3757 array,
3758 Int64Array,
3759 &DataType::Int64,
3760 vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
3761 );
3762 generate_cast_test_case!(
3764 array,
3765 Float16Array,
3766 &DataType::Float16,
3767 vec![
3768 Some(f16::from_f32(1.25)),
3769 Some(f16::from_f32(2.25)),
3770 Some(f16::from_f32(3.25)),
3771 None,
3772 Some(f16::from_f32(5.25))
3773 ]
3774 );
3775 generate_cast_test_case!(
3777 array,
3778 Float32Array,
3779 &DataType::Float32,
3780 vec![
3781 Some(1.25_f32),
3782 Some(2.25_f32),
3783 Some(3.25_f32),
3784 None,
3785 Some(5.25_f32)
3786 ]
3787 );
3788 generate_cast_test_case!(
3790 array,
3791 Float64Array,
3792 &DataType::Float64,
3793 vec![
3794 Some(1.25_f64),
3795 Some(2.25_f64),
3796 Some(3.25_f64),
3797 None,
3798 Some(5.25_f64)
3799 ]
3800 );
3801 }
3802
3803 #[test]
3804 #[cfg_attr(miri, ignore)] fn test_cast_decimal32_to_numeric() {
3806 let value_array: Vec<Option<i32>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3807 let array = create_decimal32_array(value_array, 8, 2).unwrap();
3808
3809 generate_decimal_to_numeric_cast_test_case(&array);
3810 }
3811
3812 #[test]
3813 #[cfg_attr(miri, ignore)] fn test_cast_decimal64_to_numeric() {
3815 let value_array: Vec<Option<i64>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3816 let array = create_decimal64_array(value_array, 8, 2).unwrap();
3817
3818 generate_decimal_to_numeric_cast_test_case(&array);
3819 }
3820
3821 #[test]
3822 #[cfg_attr(miri, ignore)] fn test_cast_decimal128_to_numeric() {
3824 let value_array: Vec<Option<i128>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3825 let array = create_decimal128_array(value_array, 38, 2).unwrap();
3826
3827 generate_decimal_to_numeric_cast_test_case(&array);
3828
3829 let value_array: Vec<Option<i128>> = vec![Some(51300)];
3831 let array = create_decimal128_array(value_array, 38, 2).unwrap();
3832 let casted_array = cast_with_options(
3833 &array,
3834 &DataType::UInt8,
3835 &CastOptions {
3836 safe: false,
3837 format_options: FormatOptions::default(),
3838 },
3839 );
3840 assert_eq!(
3841 "Cast error: value of 513 is out of range UInt8".to_string(),
3842 casted_array.unwrap_err().to_string()
3843 );
3844
3845 let casted_array = cast_with_options(
3846 &array,
3847 &DataType::UInt8,
3848 &CastOptions {
3849 safe: true,
3850 format_options: FormatOptions::default(),
3851 },
3852 );
3853 assert!(casted_array.is_ok());
3854 assert!(casted_array.unwrap().is_null(0));
3855
3856 let value_array: Vec<Option<i128>> = vec![Some(24400)];
3858 let array = create_decimal128_array(value_array, 38, 2).unwrap();
3859 let casted_array = cast_with_options(
3860 &array,
3861 &DataType::Int8,
3862 &CastOptions {
3863 safe: false,
3864 format_options: FormatOptions::default(),
3865 },
3866 );
3867 assert_eq!(
3868 "Cast error: value of 244 is out of range Int8".to_string(),
3869 casted_array.unwrap_err().to_string()
3870 );
3871
3872 let casted_array = cast_with_options(
3873 &array,
3874 &DataType::Int8,
3875 &CastOptions {
3876 safe: true,
3877 format_options: FormatOptions::default(),
3878 },
3879 );
3880 assert!(casted_array.is_ok());
3881 assert!(casted_array.unwrap().is_null(0));
3882
3883 let value_array: Vec<Option<i128>> = vec![
3887 Some(125),
3888 Some(225),
3889 Some(325),
3890 None,
3891 Some(525),
3892 Some(112345678),
3893 Some(112345679),
3894 ];
3895 let array = create_decimal128_array(value_array, 38, 2).unwrap();
3896 generate_cast_test_case!(
3897 &array,
3898 Float32Array,
3899 &DataType::Float32,
3900 vec![
3901 Some(1.25_f32),
3902 Some(2.25_f32),
3903 Some(3.25_f32),
3904 None,
3905 Some(5.25_f32),
3906 Some(1_123_456.7_f32),
3907 Some(1_123_456.7_f32)
3908 ]
3909 );
3910
3911 let value_array: Vec<Option<i128>> = vec![
3914 Some(125),
3915 Some(225),
3916 Some(325),
3917 None,
3918 Some(525),
3919 Some(112345678901234568),
3920 Some(112345678901234560),
3921 ];
3922 let array = create_decimal128_array(value_array, 38, 2).unwrap();
3923 generate_cast_test_case!(
3924 &array,
3925 Float64Array,
3926 &DataType::Float64,
3927 vec![
3928 Some(1.25_f64),
3929 Some(2.25_f64),
3930 Some(3.25_f64),
3931 None,
3932 Some(5.25_f64),
3933 Some(1_123_456_789_012_345.6_f64),
3934 Some(1_123_456_789_012_345.6_f64),
3935 ]
3936 );
3937 }
3938
3939 #[test]
3940 #[cfg_attr(miri, ignore)] fn test_cast_decimal256_to_numeric() {
3942 let value_array: Vec<Option<i256>> = vec![
3943 Some(i256::from_i128(125)),
3944 Some(i256::from_i128(225)),
3945 Some(i256::from_i128(325)),
3946 None,
3947 Some(i256::from_i128(525)),
3948 ];
3949 let array = create_decimal256_array(value_array, 38, 2).unwrap();
3950 generate_cast_test_case!(
3952 &array,
3953 UInt8Array,
3954 &DataType::UInt8,
3955 vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
3956 );
3957 generate_cast_test_case!(
3959 &array,
3960 UInt16Array,
3961 &DataType::UInt16,
3962 vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
3963 );
3964 generate_cast_test_case!(
3966 &array,
3967 UInt32Array,
3968 &DataType::UInt32,
3969 vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
3970 );
3971 generate_cast_test_case!(
3973 &array,
3974 UInt64Array,
3975 &DataType::UInt64,
3976 vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
3977 );
3978 generate_cast_test_case!(
3980 &array,
3981 Int8Array,
3982 &DataType::Int8,
3983 vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
3984 );
3985 generate_cast_test_case!(
3987 &array,
3988 Int16Array,
3989 &DataType::Int16,
3990 vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
3991 );
3992 generate_cast_test_case!(
3994 &array,
3995 Int32Array,
3996 &DataType::Int32,
3997 vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
3998 );
3999 generate_cast_test_case!(
4001 &array,
4002 Int64Array,
4003 &DataType::Int64,
4004 vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
4005 );
4006 generate_cast_test_case!(
4008 &array,
4009 Float16Array,
4010 &DataType::Float16,
4011 vec![
4012 Some(f16::from_f32(1.25)),
4013 Some(f16::from_f32(2.25)),
4014 Some(f16::from_f32(3.25)),
4015 None,
4016 Some(f16::from_f32(5.25))
4017 ]
4018 );
4019 generate_cast_test_case!(
4021 &array,
4022 Float32Array,
4023 &DataType::Float32,
4024 vec![
4025 Some(1.25_f32),
4026 Some(2.25_f32),
4027 Some(3.25_f32),
4028 None,
4029 Some(5.25_f32)
4030 ]
4031 );
4032 generate_cast_test_case!(
4034 &array,
4035 Float64Array,
4036 &DataType::Float64,
4037 vec![
4038 Some(1.25_f64),
4039 Some(2.25_f64),
4040 Some(3.25_f64),
4041 None,
4042 Some(5.25_f64)
4043 ]
4044 );
4045
4046 let value_array: Vec<Option<i256>> = vec![Some(i256::from_i128(24400))];
4048 let array = create_decimal256_array(value_array, 38, 2).unwrap();
4049 let casted_array = cast_with_options(
4050 &array,
4051 &DataType::Int8,
4052 &CastOptions {
4053 safe: false,
4054 format_options: FormatOptions::default(),
4055 },
4056 );
4057 assert_eq!(
4058 "Cast error: value of 244 is out of range Int8".to_string(),
4059 casted_array.unwrap_err().to_string()
4060 );
4061
4062 let casted_array = cast_with_options(
4063 &array,
4064 &DataType::Int8,
4065 &CastOptions {
4066 safe: true,
4067 format_options: FormatOptions::default(),
4068 },
4069 );
4070 assert!(casted_array.is_ok());
4071 assert!(casted_array.unwrap().is_null(0));
4072
4073 let value_array: Vec<Option<i256>> = vec![Some(i256::from_i128((1i128 << 64) + 5))];
4076 let array = create_decimal256_array(value_array, 76, 0).unwrap();
4077 let casted_array = cast_with_options(
4078 &array,
4079 &DataType::Int64,
4080 &CastOptions {
4081 safe: false,
4082 format_options: FormatOptions::default(),
4083 },
4084 );
4085 assert_eq!(
4086 "Cast error: value of 18446744073709551621 is out of range Int64".to_string(),
4087 casted_array.unwrap_err().to_string()
4088 );
4089
4090 let casted_array = cast_with_options(
4091 &array,
4092 &DataType::Int64,
4093 &CastOptions {
4094 safe: true,
4095 format_options: FormatOptions::default(),
4096 },
4097 );
4098 assert!(casted_array.is_ok());
4099 assert!(casted_array.unwrap().is_null(0));
4100
4101 let value_array: Vec<Option<i256>> = vec![
4105 Some(i256::from_i128(125)),
4106 Some(i256::from_i128(225)),
4107 Some(i256::from_i128(325)),
4108 None,
4109 Some(i256::from_i128(525)),
4110 Some(i256::from_i128(112345678)),
4111 Some(i256::from_i128(112345679)),
4112 ];
4113 let array = create_decimal256_array(value_array, 76, 2).unwrap();
4114 generate_cast_test_case!(
4115 &array,
4116 Float32Array,
4117 &DataType::Float32,
4118 vec![
4119 Some(1.25_f32),
4120 Some(2.25_f32),
4121 Some(3.25_f32),
4122 None,
4123 Some(5.25_f32),
4124 Some(1_123_456.7_f32),
4125 Some(1_123_456.7_f32)
4126 ]
4127 );
4128
4129 let value_array: Vec<Option<i256>> = vec![
4132 Some(i256::from_i128(125)),
4133 Some(i256::from_i128(225)),
4134 Some(i256::from_i128(325)),
4135 None,
4136 Some(i256::from_i128(525)),
4137 Some(i256::from_i128(112345678901234568)),
4138 Some(i256::from_i128(112345678901234560)),
4139 ];
4140 let array = create_decimal256_array(value_array, 76, 2).unwrap();
4141 generate_cast_test_case!(
4142 &array,
4143 Float64Array,
4144 &DataType::Float64,
4145 vec![
4146 Some(1.25_f64),
4147 Some(2.25_f64),
4148 Some(3.25_f64),
4149 None,
4150 Some(5.25_f64),
4151 Some(1_123_456_789_012_345.6_f64),
4152 Some(1_123_456_789_012_345.6_f64),
4153 ]
4154 );
4155 }
4156
4157 #[test]
4158 #[cfg_attr(miri, ignore)] fn test_cast_decimal128_to_float16_overflow() {
4160 let array = create_decimal128_array(
4161 vec![
4162 Some(6_550_400_i128),
4163 Some(100_000_000_i128),
4164 Some(-100_000_000_i128),
4165 None,
4166 ],
4167 10,
4168 2,
4169 )
4170 .unwrap();
4171
4172 generate_cast_test_case!(
4173 &array,
4174 Float16Array,
4175 &DataType::Float16,
4176 vec![
4177 Some(f16::from_f64(65504.0)),
4178 Some(f16::INFINITY),
4179 Some(f16::NEG_INFINITY),
4180 None
4181 ]
4182 );
4183 }
4184
4185 #[test]
4186 #[cfg_attr(miri, ignore)] fn test_cast_decimal256_to_float16_overflow() {
4188 let array = create_decimal256_array(
4189 vec![
4190 Some(i256::from_i128(6_550_400_i128)),
4191 Some(i256::from_i128(100_000_000_i128)),
4192 Some(i256::from_i128(-100_000_000_i128)),
4193 None,
4194 ],
4195 10,
4196 2,
4197 )
4198 .unwrap();
4199
4200 generate_cast_test_case!(
4201 &array,
4202 Float16Array,
4203 &DataType::Float16,
4204 vec![
4205 Some(f16::from_f64(65504.0)),
4206 Some(f16::INFINITY),
4207 Some(f16::NEG_INFINITY),
4208 None
4209 ]
4210 );
4211 }
4212
4213 #[test]
4214 #[cfg_attr(miri, ignore)] fn test_cast_decimal_to_numeric_negative_scale() {
4216 let value_array: Vec<Option<i256>> = vec![
4217 Some(i256::from_i128(125)),
4218 Some(i256::from_i128(225)),
4219 Some(i256::from_i128(325)),
4220 None,
4221 Some(i256::from_i128(525)),
4222 ];
4223 let array = create_decimal256_array(value_array, 38, -1).unwrap();
4224
4225 generate_cast_test_case!(
4226 &array,
4227 Int64Array,
4228 &DataType::Int64,
4229 vec![Some(1_250), Some(2_250), Some(3_250), None, Some(5_250)]
4230 );
4231
4232 let value_array: Vec<Option<i128>> = vec![Some(12), Some(-12), None];
4233 let array = create_decimal128_array(value_array, 10, -2).unwrap();
4234 generate_cast_test_case!(
4235 &array,
4236 Float16Array,
4237 &DataType::Float16,
4238 vec![
4239 Some(f16::from_f32(1200.0)),
4240 Some(f16::from_f32(-1200.0)),
4241 None
4242 ]
4243 );
4244
4245 let value_array: Vec<Option<i32>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
4246 let array = create_decimal32_array(value_array, 8, -2).unwrap();
4247 generate_cast_test_case!(
4248 &array,
4249 Int64Array,
4250 &DataType::Int64,
4251 vec![Some(12_500), Some(22_500), Some(32_500), None, Some(52_500)]
4252 );
4253
4254 let value_array: Vec<Option<i32>> = vec![Some(2), Some(1), None];
4255 let array = create_decimal32_array(value_array, 9, -9).unwrap();
4256 generate_cast_test_case!(
4257 &array,
4258 Int64Array,
4259 &DataType::Int64,
4260 vec![Some(2_000_000_000), Some(1_000_000_000), None]
4261 );
4262
4263 let value_array: Vec<Option<i64>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
4264 let array = create_decimal64_array(value_array, 18, -3).unwrap();
4265 generate_cast_test_case!(
4266 &array,
4267 Int64Array,
4268 &DataType::Int64,
4269 vec![
4270 Some(125_000),
4271 Some(225_000),
4272 Some(325_000),
4273 None,
4274 Some(525_000)
4275 ]
4276 );
4277
4278 let value_array: Vec<Option<i64>> = vec![Some(12), Some(34), None];
4279 let array = create_decimal64_array(value_array, 18, -10).unwrap();
4280 generate_cast_test_case!(
4281 &array,
4282 Int64Array,
4283 &DataType::Int64,
4284 vec![Some(120_000_000_000), Some(340_000_000_000), None]
4285 );
4286
4287 let value_array: Vec<Option<i128>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
4288 let array = create_decimal128_array(value_array, 38, -4).unwrap();
4289 generate_cast_test_case!(
4290 &array,
4291 Int64Array,
4292 &DataType::Int64,
4293 vec![
4294 Some(1_250_000),
4295 Some(2_250_000),
4296 Some(3_250_000),
4297 None,
4298 Some(5_250_000)
4299 ]
4300 );
4301
4302 let value_array: Vec<Option<i128>> = vec![Some(9), Some(1), None];
4303 let array = create_decimal128_array(value_array, 38, -18).unwrap();
4304 generate_cast_test_case!(
4305 &array,
4306 Int64Array,
4307 &DataType::Int64,
4308 vec![
4309 Some(9_000_000_000_000_000_000),
4310 Some(1_000_000_000_000_000_000),
4311 None
4312 ]
4313 );
4314
4315 let array = create_decimal32_array(vec![Some(999_999_999)], 9, -1).unwrap();
4316 let casted_array = cast_with_options(
4317 &array,
4318 &DataType::Int64,
4319 &CastOptions {
4320 safe: false,
4321 format_options: FormatOptions::default(),
4322 },
4323 );
4324 assert_eq!(
4325 "Arithmetic overflow: Overflow happened on: 999999999 * 10".to_string(),
4326 casted_array.unwrap_err().to_string()
4327 );
4328
4329 let casted_array = cast_with_options(
4330 &array,
4331 &DataType::Int64,
4332 &CastOptions {
4333 safe: true,
4334 format_options: FormatOptions::default(),
4335 },
4336 );
4337 assert!(casted_array.is_ok());
4338 assert!(casted_array.unwrap().is_null(0));
4339
4340 let array = create_decimal64_array(vec![Some(13)], 18, -1).unwrap();
4341 let casted_array = cast_with_options(
4342 &array,
4343 &DataType::Int8,
4344 &CastOptions {
4345 safe: false,
4346 format_options: FormatOptions::default(),
4347 },
4348 );
4349 assert_eq!(
4350 "Cast error: value of 130 is out of range Int8".to_string(),
4351 casted_array.unwrap_err().to_string()
4352 );
4353
4354 let casted_array = cast_with_options(
4355 &array,
4356 &DataType::Int8,
4357 &CastOptions {
4358 safe: true,
4359 format_options: FormatOptions::default(),
4360 },
4361 );
4362 assert!(casted_array.is_ok());
4363 assert!(casted_array.unwrap().is_null(0));
4364 }
4365
4366 #[test]
4367 fn test_cast_numeric_to_decimal128() {
4368 let decimal_type = DataType::Decimal128(38, 6);
4369 let input_arrays = vec![
4371 Arc::new(UInt8Array::from(vec![
4372 Some(1),
4373 Some(2),
4374 Some(3),
4375 None,
4376 Some(5),
4377 ])) as ArrayRef, Arc::new(UInt16Array::from(vec![
4379 Some(1),
4380 Some(2),
4381 Some(3),
4382 None,
4383 Some(5),
4384 ])) as ArrayRef, Arc::new(UInt32Array::from(vec![
4386 Some(1),
4387 Some(2),
4388 Some(3),
4389 None,
4390 Some(5),
4391 ])) as ArrayRef, Arc::new(UInt64Array::from(vec![
4393 Some(1),
4394 Some(2),
4395 Some(3),
4396 None,
4397 Some(5),
4398 ])) as ArrayRef, ];
4400
4401 for array in input_arrays {
4402 generate_cast_test_case!(
4403 &array,
4404 Decimal128Array,
4405 &decimal_type,
4406 vec![
4407 Some(1000000_i128),
4408 Some(2000000_i128),
4409 Some(3000000_i128),
4410 None,
4411 Some(5000000_i128)
4412 ]
4413 );
4414 }
4415
4416 let input_arrays = vec![
4418 Arc::new(Int8Array::from(vec![
4419 Some(1),
4420 Some(2),
4421 Some(3),
4422 None,
4423 Some(5),
4424 ])) as ArrayRef, Arc::new(Int16Array::from(vec![
4426 Some(1),
4427 Some(2),
4428 Some(3),
4429 None,
4430 Some(5),
4431 ])) as ArrayRef, Arc::new(Int32Array::from(vec![
4433 Some(1),
4434 Some(2),
4435 Some(3),
4436 None,
4437 Some(5),
4438 ])) as ArrayRef, Arc::new(Int64Array::from(vec![
4440 Some(1),
4441 Some(2),
4442 Some(3),
4443 None,
4444 Some(5),
4445 ])) as ArrayRef, ];
4447 for array in input_arrays {
4448 generate_cast_test_case!(
4449 &array,
4450 Decimal128Array,
4451 &decimal_type,
4452 vec![
4453 Some(1000000_i128),
4454 Some(2000000_i128),
4455 Some(3000000_i128),
4456 None,
4457 Some(5000000_i128)
4458 ]
4459 );
4460 }
4461
4462 let array = UInt8Array::from(vec![1, 2, 3, 4, 100]);
4465 let casted_array = cast(&array, &DataType::Decimal128(3, 1));
4466 assert!(casted_array.is_ok());
4467 let array = casted_array.unwrap();
4468 let array: &Decimal128Array = array.as_primitive();
4469 assert!(array.is_null(4));
4470
4471 let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
4474 let casted_array = cast(&array, &DataType::Decimal128(3, 1));
4475 assert!(casted_array.is_ok());
4476 let array = casted_array.unwrap();
4477 let array: &Decimal128Array = array.as_primitive();
4478 assert!(array.is_null(4));
4479
4480 let array = Float32Array::from(vec![
4482 Some(1.1),
4483 Some(2.2),
4484 Some(4.4),
4485 None,
4486 Some(1.123_456_4), Some(1.123_456_7), ]);
4489 let array = Arc::new(array) as ArrayRef;
4490 generate_cast_test_case!(
4491 &array,
4492 Decimal128Array,
4493 &decimal_type,
4494 vec![
4495 Some(1100000_i128),
4496 Some(2200000_i128),
4497 Some(4400000_i128),
4498 None,
4499 Some(1123456_i128), Some(1123457_i128), ]
4502 );
4503
4504 let array = Float64Array::from(vec![
4506 Some(1.1),
4507 Some(2.2),
4508 Some(4.4),
4509 None,
4510 Some(1.123_456_489_123_4), Some(1.123_456_789_123_4), Some(1.123_456_489_012_345_6), Some(1.123_456_789_012_345_6), ]);
4515 generate_cast_test_case!(
4516 &array,
4517 Decimal128Array,
4518 &decimal_type,
4519 vec![
4520 Some(1100000_i128),
4521 Some(2200000_i128),
4522 Some(4400000_i128),
4523 None,
4524 Some(1123456_i128), Some(1123457_i128), Some(1123456_i128), Some(1123457_i128), ]
4529 );
4530 }
4531
4532 #[test]
4533 fn test_cast_numeric_to_decimal256() {
4534 let decimal_type = DataType::Decimal256(76, 6);
4535 let input_arrays = vec![
4537 Arc::new(UInt8Array::from(vec![
4538 Some(1),
4539 Some(2),
4540 Some(3),
4541 None,
4542 Some(5),
4543 ])) as ArrayRef, Arc::new(UInt16Array::from(vec![
4545 Some(1),
4546 Some(2),
4547 Some(3),
4548 None,
4549 Some(5),
4550 ])) as ArrayRef, Arc::new(UInt32Array::from(vec![
4552 Some(1),
4553 Some(2),
4554 Some(3),
4555 None,
4556 Some(5),
4557 ])) as ArrayRef, Arc::new(UInt64Array::from(vec![
4559 Some(1),
4560 Some(2),
4561 Some(3),
4562 None,
4563 Some(5),
4564 ])) as ArrayRef, ];
4566
4567 for array in input_arrays {
4568 generate_cast_test_case!(
4569 &array,
4570 Decimal256Array,
4571 &decimal_type,
4572 vec![
4573 Some(i256::from_i128(1000000_i128)),
4574 Some(i256::from_i128(2000000_i128)),
4575 Some(i256::from_i128(3000000_i128)),
4576 None,
4577 Some(i256::from_i128(5000000_i128))
4578 ]
4579 );
4580 }
4581
4582 let input_arrays = vec![
4584 Arc::new(Int8Array::from(vec![
4585 Some(1),
4586 Some(2),
4587 Some(3),
4588 None,
4589 Some(5),
4590 ])) as ArrayRef, Arc::new(Int16Array::from(vec![
4592 Some(1),
4593 Some(2),
4594 Some(3),
4595 None,
4596 Some(5),
4597 ])) as ArrayRef, Arc::new(Int32Array::from(vec![
4599 Some(1),
4600 Some(2),
4601 Some(3),
4602 None,
4603 Some(5),
4604 ])) as ArrayRef, Arc::new(Int64Array::from(vec![
4606 Some(1),
4607 Some(2),
4608 Some(3),
4609 None,
4610 Some(5),
4611 ])) as ArrayRef, ];
4613 for array in input_arrays {
4614 generate_cast_test_case!(
4615 &array,
4616 Decimal256Array,
4617 &decimal_type,
4618 vec![
4619 Some(i256::from_i128(1000000_i128)),
4620 Some(i256::from_i128(2000000_i128)),
4621 Some(i256::from_i128(3000000_i128)),
4622 None,
4623 Some(i256::from_i128(5000000_i128))
4624 ]
4625 );
4626 }
4627
4628 let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
4631 let array = Arc::new(array) as ArrayRef;
4632 let casted_array = cast(&array, &DataType::Decimal256(3, 1));
4633 assert!(casted_array.is_ok());
4634 let array = casted_array.unwrap();
4635 let array: &Decimal256Array = array.as_primitive();
4636 assert!(array.is_null(4));
4637
4638 let array = Float32Array::from(vec![
4640 Some(1.1),
4641 Some(2.2),
4642 Some(4.4),
4643 None,
4644 Some(1.123_456_4), Some(1.123_456_7), ]);
4647 generate_cast_test_case!(
4648 &array,
4649 Decimal256Array,
4650 &decimal_type,
4651 vec![
4652 Some(i256::from_i128(1100000_i128)),
4653 Some(i256::from_i128(2200000_i128)),
4654 Some(i256::from_i128(4400000_i128)),
4655 None,
4656 Some(i256::from_i128(1123456_i128)), Some(i256::from_i128(1123457_i128)), ]
4659 );
4660
4661 let array = Float64Array::from(vec![
4663 Some(1.1),
4664 Some(2.2),
4665 Some(4.4),
4666 None,
4667 Some(1.123_456_489_123_4), Some(1.123_456_789_123_4), Some(1.123_456_489_012_345_6), Some(1.123_456_789_012_345_6), ]);
4672 generate_cast_test_case!(
4673 &array,
4674 Decimal256Array,
4675 &decimal_type,
4676 vec![
4677 Some(i256::from_i128(1100000_i128)),
4678 Some(i256::from_i128(2200000_i128)),
4679 Some(i256::from_i128(4400000_i128)),
4680 None,
4681 Some(i256::from_i128(1123456_i128)), Some(i256::from_i128(1123457_i128)), Some(i256::from_i128(1123456_i128)), Some(i256::from_i128(1123457_i128)), ]
4686 );
4687 }
4688
4689 #[test]
4690 fn test_cast_i32_to_f64() {
4691 let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4692 let b = cast(&array, &DataType::Float64).unwrap();
4693 let c = b.as_primitive::<Float64Type>();
4694 assert_eq!(5.0, c.value(0));
4695 assert_eq!(6.0, c.value(1));
4696 assert_eq!(7.0, c.value(2));
4697 assert_eq!(8.0, c.value(3));
4698 assert_eq!(9.0, c.value(4));
4699 }
4700
4701 #[test]
4702 fn test_cast_i32_to_u8() {
4703 let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4704 let b = cast(&array, &DataType::UInt8).unwrap();
4705 let c = b.as_primitive::<UInt8Type>();
4706 assert!(!c.is_valid(0));
4707 assert_eq!(6, c.value(1));
4708 assert!(!c.is_valid(2));
4709 assert_eq!(8, c.value(3));
4710 assert!(!c.is_valid(4));
4712 }
4713
4714 #[test]
4715 #[should_panic(expected = "Can't cast value -5 to type UInt8")]
4716 fn test_cast_int32_to_u8_with_error() {
4717 let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4718 let cast_option = CastOptions {
4720 safe: false,
4721 format_options: FormatOptions::default(),
4722 };
4723 let result = cast_with_options(&array, &DataType::UInt8, &cast_option);
4724 assert!(result.is_err());
4725 result.unwrap();
4726 }
4727
4728 #[test]
4729 fn test_cast_i32_to_u8_sliced() {
4730 let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4731 assert_eq!(0, array.offset());
4732 let array = array.slice(2, 3);
4733 let b = cast(&array, &DataType::UInt8).unwrap();
4734 assert_eq!(3, b.len());
4735 let c = b.as_primitive::<UInt8Type>();
4736 assert!(!c.is_valid(0));
4737 assert_eq!(8, c.value(1));
4738 assert!(!c.is_valid(2));
4740 }
4741
4742 #[test]
4743 fn test_cast_i32_to_i32() {
4744 let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4745 let b = cast(&array, &DataType::Int32).unwrap();
4746 let c = b.as_primitive::<Int32Type>();
4747 assert_eq!(5, c.value(0));
4748 assert_eq!(6, c.value(1));
4749 assert_eq!(7, c.value(2));
4750 assert_eq!(8, c.value(3));
4751 assert_eq!(9, c.value(4));
4752 }
4753
4754 #[test]
4755 fn test_cast_i32_to_list_i32() {
4756 let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4757 let b = cast(
4758 &array,
4759 &DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
4760 )
4761 .unwrap();
4762 assert_eq!(5, b.len());
4763 let arr = b.as_list::<i32>();
4764 assert_eq!(&[0, 1, 2, 3, 4, 5], arr.value_offsets());
4765 assert_eq!(1, arr.value_length(0));
4766 assert_eq!(1, arr.value_length(1));
4767 assert_eq!(1, arr.value_length(2));
4768 assert_eq!(1, arr.value_length(3));
4769 assert_eq!(1, arr.value_length(4));
4770 let c = arr.values().as_primitive::<Int32Type>();
4771 assert_eq!(5, c.value(0));
4772 assert_eq!(6, c.value(1));
4773 assert_eq!(7, c.value(2));
4774 assert_eq!(8, c.value(3));
4775 assert_eq!(9, c.value(4));
4776 }
4777
4778 #[test]
4779 fn test_cast_i32_to_list_i32_nullable() {
4780 let array = Int32Array::from(vec![Some(5), None, Some(7), Some(8), Some(9)]);
4781 let b = cast(
4782 &array,
4783 &DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
4784 )
4785 .unwrap();
4786 assert_eq!(5, b.len());
4787 assert_eq!(0, b.null_count());
4788 let arr = b.as_list::<i32>();
4789 assert_eq!(&[0, 1, 2, 3, 4, 5], arr.value_offsets());
4790 assert_eq!(1, arr.value_length(0));
4791 assert_eq!(1, arr.value_length(1));
4792 assert_eq!(1, arr.value_length(2));
4793 assert_eq!(1, arr.value_length(3));
4794 assert_eq!(1, arr.value_length(4));
4795
4796 let c = arr.values().as_primitive::<Int32Type>();
4797 assert_eq!(1, c.null_count());
4798 assert_eq!(5, c.value(0));
4799 assert!(!c.is_valid(1));
4800 assert_eq!(7, c.value(2));
4801 assert_eq!(8, c.value(3));
4802 assert_eq!(9, c.value(4));
4803 }
4804
4805 #[test]
4806 fn test_cast_i32_to_list_f64_nullable_sliced() {
4807 let array = Int32Array::from(vec![Some(5), None, Some(7), Some(8), None, Some(10)]);
4808 let array = array.slice(2, 4);
4809 let b = cast(
4810 &array,
4811 &DataType::List(Arc::new(Field::new_list_field(DataType::Float64, true))),
4812 )
4813 .unwrap();
4814 assert_eq!(4, b.len());
4815 assert_eq!(0, b.null_count());
4816 let arr = b.as_list::<i32>();
4817 assert_eq!(&[0, 1, 2, 3, 4], arr.value_offsets());
4818 assert_eq!(1, arr.value_length(0));
4819 assert_eq!(1, arr.value_length(1));
4820 assert_eq!(1, arr.value_length(2));
4821 assert_eq!(1, arr.value_length(3));
4822 let c = arr.values().as_primitive::<Float64Type>();
4823 assert_eq!(1, c.null_count());
4824 assert_eq!(7.0, c.value(0));
4825 assert_eq!(8.0, c.value(1));
4826 assert!(!c.is_valid(2));
4827 assert_eq!(10.0, c.value(3));
4828 }
4829
4830 #[test]
4831 fn test_cast_int_to_utf8view() {
4832 let inputs = vec![
4833 Arc::new(Int8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4834 Arc::new(Int16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4835 Arc::new(Int32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4836 Arc::new(Int64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4837 Arc::new(UInt8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4838 Arc::new(UInt16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4839 Arc::new(UInt32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4840 Arc::new(UInt64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4841 ];
4842 let expected: ArrayRef = Arc::new(StringViewArray::from(vec![
4843 None,
4844 Some("8"),
4845 Some("9"),
4846 Some("10"),
4847 ]));
4848
4849 for array in inputs {
4850 assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
4851 let arr = cast(&array, &DataType::Utf8View).unwrap();
4852 assert_eq!(expected.as_ref(), arr.as_ref());
4853 }
4854 }
4855
4856 #[test]
4857 #[cfg_attr(miri, ignore)] fn test_cast_float_to_utf8view() {
4859 let inputs = vec![
4860 Arc::new(Float16Array::from(vec![
4861 Some(f16::from_f64(1.5)),
4862 Some(f16::from_f64(2.5)),
4863 None,
4864 ])) as ArrayRef,
4865 Arc::new(Float32Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
4866 Arc::new(Float64Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
4867 ];
4868
4869 let expected: ArrayRef =
4870 Arc::new(StringViewArray::from(vec![Some("1.5"), Some("2.5"), None]));
4871
4872 for array in inputs {
4873 assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
4874 let arr = cast(&array, &DataType::Utf8View).unwrap();
4875 assert_eq!(expected.as_ref(), arr.as_ref());
4876 }
4877 }
4878
4879 #[test]
4880 fn test_cast_utf8_to_i32() {
4881 let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4882 let b = cast(&array, &DataType::Int32).unwrap();
4883 let c = b.as_primitive::<Int32Type>();
4884 assert_eq!(5, c.value(0));
4885 assert_eq!(6, c.value(1));
4886 assert!(!c.is_valid(2));
4887 assert_eq!(8, c.value(3));
4888 assert!(!c.is_valid(4));
4889 }
4890
4891 #[test]
4892 fn test_cast_utf8view_to_i32() {
4893 let array = StringViewArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4894 let b = cast(&array, &DataType::Int32).unwrap();
4895 let c = b.as_primitive::<Int32Type>();
4896 assert_eq!(5, c.value(0));
4897 assert_eq!(6, c.value(1));
4898 assert!(!c.is_valid(2));
4899 assert_eq!(8, c.value(3));
4900 assert!(!c.is_valid(4));
4901 }
4902
4903 #[test]
4904 fn test_cast_utf8view_to_f32() {
4905 let array = StringViewArray::from(vec!["3", "4.56", "seven", "8.9"]);
4906 let b = cast(&array, &DataType::Float32).unwrap();
4907 let c = b.as_primitive::<Float32Type>();
4908 assert_eq!(3.0, c.value(0));
4909 assert_eq!(4.56, c.value(1));
4910 assert!(!c.is_valid(2));
4911 assert_eq!(8.9, c.value(3));
4912 }
4913
4914 #[test]
4915 #[cfg_attr(miri, ignore)] fn test_cast_string_to_f16() {
4917 let arrays = [
4918 Arc::new(StringViewArray::from(vec!["3", "4.56", "seven", "8.9"])) as ArrayRef,
4919 Arc::new(StringArray::from(vec!["3", "4.56", "seven", "8.9"])),
4920 Arc::new(LargeStringArray::from(vec!["3", "4.56", "seven", "8.9"])),
4921 ];
4922 for array in arrays {
4923 let b = cast(&array, &DataType::Float16).unwrap();
4924 let c = b.as_primitive::<Float16Type>();
4925 assert_eq!(half::f16::from_f32(3.0), c.value(0));
4926 assert_eq!(half::f16::from_f32(4.56), c.value(1));
4927 assert!(!c.is_valid(2));
4928 assert_eq!(half::f16::from_f32(8.9), c.value(3));
4929 }
4930 }
4931
4932 #[test]
4933 fn test_cast_utf8view_to_decimal128() {
4934 let array = StringViewArray::from(vec![None, Some("4"), Some("5.6"), Some("7.89")]);
4935 let arr = Arc::new(array) as ArrayRef;
4936 generate_cast_test_case!(
4937 &arr,
4938 Decimal128Array,
4939 &DataType::Decimal128(4, 2),
4940 vec![None, Some(400_i128), Some(560_i128), Some(789_i128)]
4941 );
4942 }
4943
4944 #[test]
4945 fn test_cast_with_options_utf8_to_i32() {
4946 let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4947 let result = cast_with_options(
4948 &array,
4949 &DataType::Int32,
4950 &CastOptions {
4951 safe: false,
4952 format_options: FormatOptions::default(),
4953 },
4954 );
4955 match result {
4956 Ok(_) => panic!("expected error"),
4957 Err(e) => {
4958 assert!(
4959 e.to_string()
4960 .contains("Cast error: Cannot cast string 'seven' to value of Int32 type",),
4961 "Error: {e}"
4962 )
4963 }
4964 }
4965 }
4966
4967 #[test]
4968 fn test_cast_utf8_to_bool() {
4969 let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4970 let casted = cast(&strings, &DataType::Boolean).unwrap();
4971 let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
4972 assert_eq!(*as_boolean_array(&casted), expected);
4973 }
4974
4975 #[test]
4976 fn test_cast_utf8view_to_bool() {
4977 let strings = StringViewArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4978 let casted = cast(&strings, &DataType::Boolean).unwrap();
4979 let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
4980 assert_eq!(*as_boolean_array(&casted), expected);
4981 }
4982
4983 #[test]
4984 fn test_cast_with_options_utf8_to_bool() {
4985 let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4986 let casted = cast_with_options(
4987 &strings,
4988 &DataType::Boolean,
4989 &CastOptions {
4990 safe: false,
4991 format_options: FormatOptions::default(),
4992 },
4993 );
4994 match casted {
4995 Ok(_) => panic!("expected error"),
4996 Err(e) => {
4997 assert!(
4998 e.to_string().contains(
4999 "Cast error: Cannot cast value 'invalid' to value of Boolean type"
5000 )
5001 )
5002 }
5003 }
5004 }
5005
5006 #[test]
5007 fn test_cast_bool_to_i32() {
5008 let array = BooleanArray::from(vec![Some(true), Some(false), None]);
5009 let b = cast(&array, &DataType::Int32).unwrap();
5010 let c = b.as_primitive::<Int32Type>();
5011 assert_eq!(1, c.value(0));
5012 assert_eq!(0, c.value(1));
5013 assert!(!c.is_valid(2));
5014 }
5015
5016 #[test]
5017 fn test_cast_bool_to_utf8view() {
5018 let array = BooleanArray::from(vec![Some(true), Some(false), None]);
5019 let b = cast(&array, &DataType::Utf8View).unwrap();
5020 let c = b.as_any().downcast_ref::<StringViewArray>().unwrap();
5021 assert_eq!("true", c.value(0));
5022 assert_eq!("false", c.value(1));
5023 assert!(!c.is_valid(2));
5024 }
5025
5026 #[test]
5027 fn test_cast_bool_to_utf8() {
5028 let array = BooleanArray::from(vec![Some(true), Some(false), None]);
5029 let b = cast(&array, &DataType::Utf8).unwrap();
5030 let c = b.as_any().downcast_ref::<StringArray>().unwrap();
5031 assert_eq!("true", c.value(0));
5032 assert_eq!("false", c.value(1));
5033 assert!(!c.is_valid(2));
5034 }
5035
5036 #[test]
5037 fn test_cast_bool_to_large_utf8() {
5038 let array = BooleanArray::from(vec![Some(true), Some(false), None]);
5039 let b = cast(&array, &DataType::LargeUtf8).unwrap();
5040 let c = b.as_any().downcast_ref::<LargeStringArray>().unwrap();
5041 assert_eq!("true", c.value(0));
5042 assert_eq!("false", c.value(1));
5043 assert!(!c.is_valid(2));
5044 }
5045
5046 #[test]
5047 fn test_cast_bool_to_f64() {
5048 let array = BooleanArray::from(vec![Some(true), Some(false), None]);
5049 let b = cast(&array, &DataType::Float64).unwrap();
5050 let c = b.as_primitive::<Float64Type>();
5051 assert_eq!(1.0, c.value(0));
5052 assert_eq!(0.0, c.value(1));
5053 assert!(!c.is_valid(2));
5054 }
5055
5056 #[test]
5057 fn test_cast_integer_to_timestamp() {
5058 let array = Int64Array::from(vec![Some(2), Some(10), None]);
5059 let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5060
5061 let array = Int8Array::from(vec![Some(2), Some(10), None]);
5062 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5063
5064 assert_eq!(&actual, &expected);
5065
5066 let array = Int16Array::from(vec![Some(2), Some(10), None]);
5067 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5068
5069 assert_eq!(&actual, &expected);
5070
5071 let array = Int32Array::from(vec![Some(2), Some(10), None]);
5072 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5073
5074 assert_eq!(&actual, &expected);
5075
5076 let array = UInt8Array::from(vec![Some(2), Some(10), None]);
5077 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5078
5079 assert_eq!(&actual, &expected);
5080
5081 let array = UInt16Array::from(vec![Some(2), Some(10), None]);
5082 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5083
5084 assert_eq!(&actual, &expected);
5085
5086 let array = UInt32Array::from(vec![Some(2), Some(10), None]);
5087 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5088
5089 assert_eq!(&actual, &expected);
5090
5091 let array = UInt64Array::from(vec![Some(2), Some(10), None]);
5092 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5093
5094 assert_eq!(&actual, &expected);
5095 }
5096
5097 #[test]
5098 fn test_cast_timestamp_to_integer() {
5099 let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
5100 .with_timezone("UTC".to_string());
5101 let expected = cast(&array, &DataType::Int64).unwrap();
5102
5103 let actual = cast(&cast(&array, &DataType::Int8).unwrap(), &DataType::Int64).unwrap();
5104 assert_eq!(&actual, &expected);
5105
5106 let actual = cast(&cast(&array, &DataType::Int16).unwrap(), &DataType::Int64).unwrap();
5107 assert_eq!(&actual, &expected);
5108
5109 let actual = cast(&cast(&array, &DataType::Int32).unwrap(), &DataType::Int64).unwrap();
5110 assert_eq!(&actual, &expected);
5111
5112 let actual = cast(&cast(&array, &DataType::UInt8).unwrap(), &DataType::Int64).unwrap();
5113 assert_eq!(&actual, &expected);
5114
5115 let actual = cast(&cast(&array, &DataType::UInt16).unwrap(), &DataType::Int64).unwrap();
5116 assert_eq!(&actual, &expected);
5117
5118 let actual = cast(&cast(&array, &DataType::UInt32).unwrap(), &DataType::Int64).unwrap();
5119 assert_eq!(&actual, &expected);
5120
5121 let actual = cast(&cast(&array, &DataType::UInt64).unwrap(), &DataType::Int64).unwrap();
5122 assert_eq!(&actual, &expected);
5123 }
5124
5125 #[test]
5126 #[cfg_attr(miri, ignore)] fn test_cast_floating_to_timestamp() {
5128 let array = Int64Array::from(vec![Some(2), Some(10), None]);
5129 let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5130
5131 let array = Float16Array::from(vec![
5132 Some(f16::from_f32(2.0)),
5133 Some(f16::from_f32(10.6)),
5134 None,
5135 ]);
5136 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5137
5138 assert_eq!(&actual, &expected);
5139
5140 let array = Float32Array::from(vec![Some(2.0), Some(10.6), None]);
5141 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5142
5143 assert_eq!(&actual, &expected);
5144
5145 let array = Float64Array::from(vec![Some(2.1), Some(10.2), None]);
5146 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5147
5148 assert_eq!(&actual, &expected);
5149 }
5150
5151 #[test]
5152 #[cfg_attr(miri, ignore)] fn test_cast_timestamp_to_floating() {
5154 let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
5155 .with_timezone("UTC".to_string());
5156 let expected = cast(&array, &DataType::Int64).unwrap();
5157
5158 let actual = cast(&cast(&array, &DataType::Float16).unwrap(), &DataType::Int64).unwrap();
5159 assert_eq!(&actual, &expected);
5160
5161 let actual = cast(&cast(&array, &DataType::Float32).unwrap(), &DataType::Int64).unwrap();
5162 assert_eq!(&actual, &expected);
5163
5164 let actual = cast(&cast(&array, &DataType::Float64).unwrap(), &DataType::Int64).unwrap();
5165 assert_eq!(&actual, &expected);
5166 }
5167
5168 #[test]
5169 fn test_cast_decimal_to_timestamp() {
5170 let array = Int64Array::from(vec![Some(2), Some(10), None]);
5171 let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5172
5173 let array = Decimal128Array::from(vec![Some(200), Some(1000), None])
5174 .with_precision_and_scale(4, 2)
5175 .unwrap();
5176 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5177
5178 assert_eq!(&actual, &expected);
5179
5180 let array = Decimal256Array::from(vec![
5181 Some(i256::from_i128(2000)),
5182 Some(i256::from_i128(10000)),
5183 None,
5184 ])
5185 .with_precision_and_scale(5, 3)
5186 .unwrap();
5187 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5188
5189 assert_eq!(&actual, &expected);
5190 }
5191
5192 #[test]
5193 fn test_cast_timestamp_to_decimal() {
5194 let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
5195 .with_timezone("UTC".to_string());
5196 let expected = cast(&array, &DataType::Int64).unwrap();
5197
5198 let actual = cast(
5199 &cast(&array, &DataType::Decimal128(5, 2)).unwrap(),
5200 &DataType::Int64,
5201 )
5202 .unwrap();
5203 assert_eq!(&actual, &expected);
5204
5205 let actual = cast(
5206 &cast(&array, &DataType::Decimal256(10, 5)).unwrap(),
5207 &DataType::Int64,
5208 )
5209 .unwrap();
5210 assert_eq!(&actual, &expected);
5211 }
5212
5213 #[test]
5214 fn test_cast_list_i32_to_list_u16() {
5215 let values = vec![
5216 Some(vec![Some(0), Some(0), Some(0)]),
5217 Some(vec![Some(-1), Some(-2), Some(-1)]),
5218 Some(vec![Some(2), Some(100000000)]),
5219 ];
5220 let list_array = ListArray::from_iter_primitive::<Int32Type, _, _>(values);
5221
5222 let target_type = DataType::List(Arc::new(Field::new("item", DataType::UInt16, true)));
5223 assert!(can_cast_types(list_array.data_type(), &target_type));
5224 let cast_array = cast(&list_array, &target_type).unwrap();
5225
5226 assert_eq!(0, cast_array.null_count());
5231
5232 let array = cast_array.as_list::<i32>();
5234 assert_eq!(list_array.value_offsets(), array.value_offsets());
5235
5236 assert_eq!(DataType::UInt16, array.value_type());
5237 assert_eq!(3, array.value_length(0));
5238 assert_eq!(3, array.value_length(1));
5239 assert_eq!(2, array.value_length(2));
5240
5241 let u16arr = array.values().as_primitive::<UInt16Type>();
5243 assert_eq!(4, u16arr.null_count());
5244
5245 let expected: UInt16Array =
5247 vec![Some(0), Some(0), Some(0), None, None, None, Some(2), None]
5248 .into_iter()
5249 .collect();
5250
5251 assert_eq!(u16arr, &expected);
5252 }
5253
5254 #[test]
5255 fn test_cast_list_i32_to_list_timestamp() {
5256 let value_data = Int32Array::from(vec![0, 0, 0, -1, -2, -1, 2, 8, 100000000]).into_data();
5258
5259 let value_offsets = Buffer::from_slice_ref([0, 3, 6, 9]);
5260
5261 let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
5263 let list_data = ArrayData::builder(list_data_type)
5264 .len(3)
5265 .add_buffer(value_offsets)
5266 .add_child_data(value_data)
5267 .build()
5268 .unwrap();
5269 let list_array = Arc::new(ListArray::from(list_data)) as ArrayRef;
5270
5271 let actual = cast(
5272 &list_array,
5273 &DataType::List(Arc::new(Field::new_list_field(
5274 DataType::Timestamp(TimeUnit::Microsecond, None),
5275 true,
5276 ))),
5277 )
5278 .unwrap();
5279
5280 let expected = cast(
5281 &cast(
5282 &list_array,
5283 &DataType::List(Arc::new(Field::new_list_field(DataType::Int64, true))),
5284 )
5285 .unwrap(),
5286 &DataType::List(Arc::new(Field::new_list_field(
5287 DataType::Timestamp(TimeUnit::Microsecond, None),
5288 true,
5289 ))),
5290 )
5291 .unwrap();
5292
5293 assert_eq!(&actual, &expected);
5294 }
5295
5296 #[test]
5297 fn test_cast_date32_to_date64() {
5298 let a = Date32Array::from(vec![10000, 17890]);
5299 let array = Arc::new(a) as ArrayRef;
5300 let b = cast(&array, &DataType::Date64).unwrap();
5301 let c = b.as_primitive::<Date64Type>();
5302 assert_eq!(864000000000, c.value(0));
5303 assert_eq!(1545696000000, c.value(1));
5304 }
5305
5306 #[test]
5307 fn test_cast_date64_to_date32() {
5308 let a = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5309 let array = Arc::new(a) as ArrayRef;
5310 let b = cast(&array, &DataType::Date32).unwrap();
5311 let c = b.as_primitive::<Date32Type>();
5312 assert_eq!(10000, c.value(0));
5313 assert_eq!(17890, c.value(1));
5314 assert!(c.is_null(2));
5315 }
5316
5317 #[test]
5318 fn test_cast_date64_to_date32_overflow() {
5319 let a = Date64Array::from(vec![i64::MAX]);
5320 let array = Arc::new(a) as ArrayRef;
5321
5322 let b = cast(&array, &DataType::Date32).unwrap();
5323 let c = b.as_primitive::<Date32Type>();
5324 assert!(c.is_null(0));
5325
5326 let options = CastOptions {
5327 safe: false,
5328 ..Default::default()
5329 };
5330 let err = cast_with_options(&array, &DataType::Date32, &options).unwrap_err();
5331 assert!(
5332 err.to_string().contains("Cannot cast Date64 value"),
5333 "{err}"
5334 );
5335 }
5336
5337 #[test]
5338 fn test_cast_string_to_integral_overflow() {
5339 let str = Arc::new(StringArray::from(vec![
5340 Some("123"),
5341 Some("-123"),
5342 Some("86374"),
5343 None,
5344 ])) as ArrayRef;
5345
5346 let options = CastOptions {
5347 safe: true,
5348 format_options: FormatOptions::default(),
5349 };
5350 let res = cast_with_options(&str, &DataType::Int16, &options).expect("should cast to i16");
5351 let expected =
5352 Arc::new(Int16Array::from(vec![Some(123), Some(-123), None, None])) as ArrayRef;
5353 assert_eq!(&res, &expected);
5354 }
5355
5356 #[test]
5357 fn test_cast_string_to_timestamp() {
5358 let a0 = Arc::new(StringViewArray::from(vec![
5359 Some("2020-09-08T12:00:00.123456789+00:00"),
5360 Some("Not a valid date"),
5361 None,
5362 ])) as ArrayRef;
5363 let a1 = Arc::new(StringArray::from(vec![
5364 Some("2020-09-08T12:00:00.123456789+00:00"),
5365 Some("Not a valid date"),
5366 None,
5367 ])) as ArrayRef;
5368 let a2 = Arc::new(LargeStringArray::from(vec![
5369 Some("2020-09-08T12:00:00.123456789+00:00"),
5370 Some("Not a valid date"),
5371 None,
5372 ])) as ArrayRef;
5373 for array in &[a0, a1, a2] {
5374 for time_unit in &[
5375 TimeUnit::Second,
5376 TimeUnit::Millisecond,
5377 TimeUnit::Microsecond,
5378 TimeUnit::Nanosecond,
5379 ] {
5380 let to_type = DataType::Timestamp(*time_unit, None);
5381 let b = cast(array, &to_type).unwrap();
5382
5383 match time_unit {
5384 TimeUnit::Second => {
5385 let c = b.as_primitive::<TimestampSecondType>();
5386 assert_eq!(1599566400, c.value(0));
5387 assert!(c.is_null(1));
5388 assert!(c.is_null(2));
5389 }
5390 TimeUnit::Millisecond => {
5391 let c = b
5392 .as_any()
5393 .downcast_ref::<TimestampMillisecondArray>()
5394 .unwrap();
5395 assert_eq!(1599566400123, c.value(0));
5396 assert!(c.is_null(1));
5397 assert!(c.is_null(2));
5398 }
5399 TimeUnit::Microsecond => {
5400 let c = b
5401 .as_any()
5402 .downcast_ref::<TimestampMicrosecondArray>()
5403 .unwrap();
5404 assert_eq!(1599566400123456, c.value(0));
5405 assert!(c.is_null(1));
5406 assert!(c.is_null(2));
5407 }
5408 TimeUnit::Nanosecond => {
5409 let c = b
5410 .as_any()
5411 .downcast_ref::<TimestampNanosecondArray>()
5412 .unwrap();
5413 assert_eq!(1599566400123456789, c.value(0));
5414 assert!(c.is_null(1));
5415 assert!(c.is_null(2));
5416 }
5417 }
5418
5419 let options = CastOptions {
5420 safe: false,
5421 format_options: FormatOptions::default(),
5422 };
5423 let err = cast_with_options(array, &to_type, &options).unwrap_err();
5424 assert_eq!(
5425 err.to_string(),
5426 "Parser error: Error parsing timestamp from 'Not a valid date': error parsing date"
5427 );
5428 }
5429 }
5430 }
5431
5432 #[test]
5433 fn test_cast_string_to_timestamp_overflow() {
5434 let array = StringArray::from(vec!["9800-09-08T12:00:00.123456789"]);
5435 let result = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
5436 let result = result.as_primitive::<TimestampSecondType>();
5437 assert_eq!(result.values(), &[247112596800]);
5438 }
5439
5440 #[test]
5441 fn test_cast_string_to_date32() {
5442 let a0 = Arc::new(StringViewArray::from(vec![
5443 Some("2018-12-25"),
5444 Some("Not a valid date"),
5445 None,
5446 ])) as ArrayRef;
5447 let a1 = Arc::new(StringArray::from(vec![
5448 Some("2018-12-25"),
5449 Some("Not a valid date"),
5450 None,
5451 ])) as ArrayRef;
5452 let a2 = Arc::new(LargeStringArray::from(vec![
5453 Some("2018-12-25"),
5454 Some("Not a valid date"),
5455 None,
5456 ])) as ArrayRef;
5457 for array in &[a0, a1, a2] {
5458 let to_type = DataType::Date32;
5459 let b = cast(array, &to_type).unwrap();
5460 let c = b.as_primitive::<Date32Type>();
5461 assert_eq!(17890, c.value(0));
5462 assert!(c.is_null(1));
5463 assert!(c.is_null(2));
5464
5465 let options = CastOptions {
5466 safe: false,
5467 format_options: FormatOptions::default(),
5468 };
5469 let err = cast_with_options(array, &to_type, &options).unwrap_err();
5470 assert_eq!(
5471 err.to_string(),
5472 "Cast error: Cannot cast string 'Not a valid date' to value of Date32 type"
5473 );
5474 }
5475 }
5476
5477 #[test]
5478 fn test_cast_string_with_large_date_to_date32() {
5479 let array = Arc::new(StringArray::from(vec![
5480 Some("+10999-12-31"),
5481 Some("-0010-02-28"),
5482 Some("0010-02-28"),
5483 Some("0000-01-01"),
5484 Some("-0000-01-01"),
5485 Some("-0001-01-01"),
5486 ])) as ArrayRef;
5487 let to_type = DataType::Date32;
5488 let options = CastOptions {
5489 safe: false,
5490 format_options: FormatOptions::default(),
5491 };
5492 let b = cast_with_options(&array, &to_type, &options).unwrap();
5493 let c = b.as_primitive::<Date32Type>();
5494 assert_eq!(3298139, c.value(0)); assert_eq!(-723122, c.value(1)); assert_eq!(-715817, c.value(2)); assert_eq!(c.value(3), c.value(4)); assert_eq!(-719528, c.value(3)); assert_eq!(-719528, c.value(4)); assert_eq!(-719893, c.value(5)); }
5502
5503 #[test]
5504 fn test_cast_invalid_string_with_large_date_to_date32() {
5505 let array = Arc::new(StringArray::from(vec![Some("10999-12-31")])) as ArrayRef;
5507 let to_type = DataType::Date32;
5508 let options = CastOptions {
5509 safe: false,
5510 format_options: FormatOptions::default(),
5511 };
5512 let err = cast_with_options(&array, &to_type, &options).unwrap_err();
5513 assert_eq!(
5514 err.to_string(),
5515 "Cast error: Cannot cast string '10999-12-31' to value of Date32 type"
5516 );
5517 }
5518
5519 #[test]
5520 fn test_cast_string_format_yyyymmdd_to_date32() {
5521 let a0 = Arc::new(StringViewArray::from(vec![
5522 Some("2020-12-25"),
5523 Some("20201117"),
5524 ])) as ArrayRef;
5525 let a1 = Arc::new(StringArray::from(vec![
5526 Some("2020-12-25"),
5527 Some("20201117"),
5528 ])) as ArrayRef;
5529 let a2 = Arc::new(LargeStringArray::from(vec![
5530 Some("2020-12-25"),
5531 Some("20201117"),
5532 ])) as ArrayRef;
5533
5534 for array in &[a0, a1, a2] {
5535 let to_type = DataType::Date32;
5536 let options = CastOptions {
5537 safe: false,
5538 format_options: FormatOptions::default(),
5539 };
5540 let result = cast_with_options(&array, &to_type, &options).unwrap();
5541 let c = result.as_primitive::<Date32Type>();
5542 assert_eq!(
5543 chrono::NaiveDate::from_ymd_opt(2020, 12, 25),
5544 c.value_as_date(0)
5545 );
5546 assert_eq!(
5547 chrono::NaiveDate::from_ymd_opt(2020, 11, 17),
5548 c.value_as_date(1)
5549 );
5550 }
5551 }
5552
5553 #[test]
5554 fn test_cast_string_to_time32second() {
5555 let a0 = Arc::new(StringViewArray::from(vec![
5556 Some("08:08:35.091323414"),
5557 Some("08:08:60.091323414"), Some("08:08:61.091323414"), Some("Not a valid time"),
5560 None,
5561 ])) as ArrayRef;
5562 let a1 = Arc::new(StringArray::from(vec![
5563 Some("08:08:35.091323414"),
5564 Some("08:08:60.091323414"), Some("08:08:61.091323414"), Some("Not a valid time"),
5567 None,
5568 ])) as ArrayRef;
5569 let a2 = Arc::new(LargeStringArray::from(vec![
5570 Some("08:08:35.091323414"),
5571 Some("08:08:60.091323414"), Some("08:08:61.091323414"), Some("Not a valid time"),
5574 None,
5575 ])) as ArrayRef;
5576 for array in &[a0, a1, a2] {
5577 let to_type = DataType::Time32(TimeUnit::Second);
5578 let b = cast(array, &to_type).unwrap();
5579 let c = b.as_primitive::<Time32SecondType>();
5580 assert_eq!(29315, c.value(0));
5581 assert_eq!(29340, c.value(1));
5582 assert!(c.is_null(2));
5583 assert!(c.is_null(3));
5584 assert!(c.is_null(4));
5585
5586 let options = CastOptions {
5587 safe: false,
5588 format_options: FormatOptions::default(),
5589 };
5590 let err = cast_with_options(array, &to_type, &options).unwrap_err();
5591 assert_eq!(
5592 err.to_string(),
5593 "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(s) type"
5594 );
5595 }
5596 }
5597
5598 #[test]
5599 fn test_cast_string_to_time32millisecond() {
5600 let a0 = Arc::new(StringViewArray::from(vec![
5601 Some("08:08:35.091323414"),
5602 Some("08:08:60.091323414"), Some("08:08:61.091323414"), Some("Not a valid time"),
5605 None,
5606 ])) as ArrayRef;
5607 let a1 = Arc::new(StringArray::from(vec![
5608 Some("08:08:35.091323414"),
5609 Some("08:08:60.091323414"), Some("08:08:61.091323414"), Some("Not a valid time"),
5612 None,
5613 ])) as ArrayRef;
5614 let a2 = Arc::new(LargeStringArray::from(vec![
5615 Some("08:08:35.091323414"),
5616 Some("08:08:60.091323414"), Some("08:08:61.091323414"), Some("Not a valid time"),
5619 None,
5620 ])) as ArrayRef;
5621 for array in &[a0, a1, a2] {
5622 let to_type = DataType::Time32(TimeUnit::Millisecond);
5623 let b = cast(array, &to_type).unwrap();
5624 let c = b.as_primitive::<Time32MillisecondType>();
5625 assert_eq!(29315091, c.value(0));
5626 assert_eq!(29340091, c.value(1));
5627 assert!(c.is_null(2));
5628 assert!(c.is_null(3));
5629 assert!(c.is_null(4));
5630
5631 let options = CastOptions {
5632 safe: false,
5633 format_options: FormatOptions::default(),
5634 };
5635 let err = cast_with_options(array, &to_type, &options).unwrap_err();
5636 assert_eq!(
5637 err.to_string(),
5638 "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(ms) type"
5639 );
5640 }
5641 }
5642
5643 #[test]
5644 fn test_cast_string_to_time64microsecond() {
5645 let a0 = Arc::new(StringViewArray::from(vec![
5646 Some("08:08:35.091323414"),
5647 Some("Not a valid time"),
5648 None,
5649 ])) as ArrayRef;
5650 let a1 = Arc::new(StringArray::from(vec![
5651 Some("08:08:35.091323414"),
5652 Some("Not a valid time"),
5653 None,
5654 ])) as ArrayRef;
5655 let a2 = Arc::new(LargeStringArray::from(vec![
5656 Some("08:08:35.091323414"),
5657 Some("Not a valid time"),
5658 None,
5659 ])) as ArrayRef;
5660 for array in &[a0, a1, a2] {
5661 let to_type = DataType::Time64(TimeUnit::Microsecond);
5662 let b = cast(array, &to_type).unwrap();
5663 let c = b.as_primitive::<Time64MicrosecondType>();
5664 assert_eq!(29315091323, c.value(0));
5665 assert!(c.is_null(1));
5666 assert!(c.is_null(2));
5667
5668 let options = CastOptions {
5669 safe: false,
5670 format_options: FormatOptions::default(),
5671 };
5672 let err = cast_with_options(array, &to_type, &options).unwrap_err();
5673 assert_eq!(
5674 err.to_string(),
5675 "Cast error: Cannot cast string 'Not a valid time' to value of Time64(µs) type"
5676 );
5677 }
5678 }
5679
5680 #[test]
5681 fn test_cast_string_to_time64nanosecond() {
5682 let a0 = Arc::new(StringViewArray::from(vec![
5683 Some("08:08:35.091323414"),
5684 Some("Not a valid time"),
5685 None,
5686 ])) as ArrayRef;
5687 let a1 = Arc::new(StringArray::from(vec![
5688 Some("08:08:35.091323414"),
5689 Some("Not a valid time"),
5690 None,
5691 ])) as ArrayRef;
5692 let a2 = Arc::new(LargeStringArray::from(vec![
5693 Some("08:08:35.091323414"),
5694 Some("Not a valid time"),
5695 None,
5696 ])) as ArrayRef;
5697 for array in &[a0, a1, a2] {
5698 let to_type = DataType::Time64(TimeUnit::Nanosecond);
5699 let b = cast(array, &to_type).unwrap();
5700 let c = b.as_primitive::<Time64NanosecondType>();
5701 assert_eq!(29315091323414, c.value(0));
5702 assert!(c.is_null(1));
5703 assert!(c.is_null(2));
5704
5705 let options = CastOptions {
5706 safe: false,
5707 format_options: FormatOptions::default(),
5708 };
5709 let err = cast_with_options(array, &to_type, &options).unwrap_err();
5710 assert_eq!(
5711 err.to_string(),
5712 "Cast error: Cannot cast string 'Not a valid time' to value of Time64(ns) type"
5713 );
5714 }
5715 }
5716
5717 #[test]
5718 fn test_cast_string_to_date64() {
5719 let a0 = Arc::new(StringViewArray::from(vec![
5720 Some("2020-09-08T12:00:00"),
5721 Some("Not a valid date"),
5722 None,
5723 ])) as ArrayRef;
5724 let a1 = Arc::new(StringArray::from(vec![
5725 Some("2020-09-08T12:00:00"),
5726 Some("Not a valid date"),
5727 None,
5728 ])) as ArrayRef;
5729 let a2 = Arc::new(LargeStringArray::from(vec![
5730 Some("2020-09-08T12:00:00"),
5731 Some("Not a valid date"),
5732 None,
5733 ])) as ArrayRef;
5734 for array in &[a0, a1, a2] {
5735 let to_type = DataType::Date64;
5736 let b = cast(array, &to_type).unwrap();
5737 let c = b.as_primitive::<Date64Type>();
5738 assert_eq!(1599566400000, c.value(0));
5739 assert!(c.is_null(1));
5740 assert!(c.is_null(2));
5741
5742 let options = CastOptions {
5743 safe: false,
5744 format_options: FormatOptions::default(),
5745 };
5746 let err = cast_with_options(array, &to_type, &options).unwrap_err();
5747 assert_eq!(
5748 err.to_string(),
5749 "Cast error: Cannot cast string 'Not a valid date' to value of Date64 type"
5750 );
5751 }
5752 }
5753
5754 macro_rules! test_safe_string_to_interval {
5755 ($data_vec:expr, $interval_unit:expr, $array_ty:ty, $expect_vec:expr) => {
5756 let source_string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
5757
5758 let options = CastOptions {
5759 safe: true,
5760 format_options: FormatOptions::default(),
5761 };
5762
5763 let target_interval_array = cast_with_options(
5764 &source_string_array.clone(),
5765 &DataType::Interval($interval_unit),
5766 &options,
5767 )
5768 .unwrap()
5769 .as_any()
5770 .downcast_ref::<$array_ty>()
5771 .unwrap()
5772 .clone() as $array_ty;
5773
5774 let target_string_array =
5775 cast_with_options(&target_interval_array, &DataType::Utf8, &options)
5776 .unwrap()
5777 .as_any()
5778 .downcast_ref::<StringArray>()
5779 .unwrap()
5780 .clone();
5781
5782 let expect_string_array = StringArray::from($expect_vec);
5783
5784 assert_eq!(target_string_array, expect_string_array);
5785
5786 let target_large_string_array =
5787 cast_with_options(&target_interval_array, &DataType::LargeUtf8, &options)
5788 .unwrap()
5789 .as_any()
5790 .downcast_ref::<LargeStringArray>()
5791 .unwrap()
5792 .clone();
5793
5794 let expect_large_string_array = LargeStringArray::from($expect_vec);
5795
5796 assert_eq!(target_large_string_array, expect_large_string_array);
5797 };
5798 }
5799
5800 #[test]
5801 fn test_cast_string_to_interval_year_month() {
5802 test_safe_string_to_interval!(
5803 vec![
5804 Some("1 year 1 month"),
5805 Some("1.5 years 13 month"),
5806 Some("30 days"),
5807 Some("31 days"),
5808 Some("2 months 31 days"),
5809 Some("2 months 31 days 1 second"),
5810 Some("foobar"),
5811 ],
5812 IntervalUnit::YearMonth,
5813 IntervalYearMonthArray,
5814 vec![
5815 Some("1 years 1 mons"),
5816 Some("2 years 7 mons"),
5817 None,
5818 None,
5819 None,
5820 None,
5821 None,
5822 ]
5823 );
5824 }
5825
5826 #[test]
5827 fn test_cast_string_to_interval_day_time() {
5828 test_safe_string_to_interval!(
5829 vec![
5830 Some("1 year 1 month"),
5831 Some("1.5 years 13 month"),
5832 Some("30 days"),
5833 Some("1 day 2 second 3.5 milliseconds"),
5834 Some("foobar"),
5835 ],
5836 IntervalUnit::DayTime,
5837 IntervalDayTimeArray,
5838 vec![
5839 Some("390 days"),
5840 Some("930 days"),
5841 Some("30 days"),
5842 None,
5843 None,
5844 ]
5845 );
5846 }
5847
5848 #[test]
5849 fn test_cast_string_to_interval_month_day_nano() {
5850 test_safe_string_to_interval!(
5851 vec![
5852 Some("1 year 1 month 1 day"),
5853 None,
5854 Some("1.5 years 13 month 35 days 1.4 milliseconds"),
5855 Some("3 days"),
5856 Some("8 seconds"),
5857 None,
5858 Some("1 day 29800 milliseconds"),
5859 Some("3 months 1 second"),
5860 Some("6 minutes 120 second"),
5861 Some("2 years 39 months 9 days 19 hours 1 minute 83 seconds 399222 milliseconds"),
5862 Some("foobar"),
5863 ],
5864 IntervalUnit::MonthDayNano,
5865 IntervalMonthDayNanoArray,
5866 vec![
5867 Some("13 mons 1 days"),
5868 None,
5869 Some("31 mons 35 days 0.001400000 secs"),
5870 Some("3 days"),
5871 Some("8.000000000 secs"),
5872 None,
5873 Some("1 days 29.800000000 secs"),
5874 Some("3 mons 1.000000000 secs"),
5875 Some("8 mins"),
5876 Some("63 mons 9 days 19 hours 9 mins 2.222000000 secs"),
5877 None,
5878 ]
5879 );
5880 }
5881
5882 macro_rules! test_unsafe_string_to_interval_err {
5883 ($data_vec:expr, $interval_unit:expr, $error_msg:expr) => {
5884 let string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
5885 let options = CastOptions {
5886 safe: false,
5887 format_options: FormatOptions::default(),
5888 };
5889 let arrow_err = cast_with_options(
5890 &string_array.clone(),
5891 &DataType::Interval($interval_unit),
5892 &options,
5893 )
5894 .unwrap_err();
5895 assert_eq!($error_msg, arrow_err.to_string());
5896 };
5897 }
5898
5899 #[test]
5900 fn test_cast_string_to_interval_err() {
5901 test_unsafe_string_to_interval_err!(
5902 vec![Some("foobar")],
5903 IntervalUnit::YearMonth,
5904 r#"Parser error: Invalid input syntax for type interval: "foobar""#
5905 );
5906 test_unsafe_string_to_interval_err!(
5907 vec![Some("foobar")],
5908 IntervalUnit::DayTime,
5909 r#"Parser error: Invalid input syntax for type interval: "foobar""#
5910 );
5911 test_unsafe_string_to_interval_err!(
5912 vec![Some("foobar")],
5913 IntervalUnit::MonthDayNano,
5914 r#"Parser error: Invalid input syntax for type interval: "foobar""#
5915 );
5916 test_unsafe_string_to_interval_err!(
5917 vec![Some("2 months 31 days 1 second")],
5918 IntervalUnit::YearMonth,
5919 "Cast error: Cannot cast 2 months 31 days 1 second to IntervalYearMonth. Only year and month fields are allowed."
5920 );
5921 test_unsafe_string_to_interval_err!(
5922 vec![Some("1 day 1.5 milliseconds")],
5923 IntervalUnit::DayTime,
5924 "Cast error: Cannot cast 1 day 1.5 milliseconds to IntervalDayTime because the nanos part isn't multiple of milliseconds"
5925 );
5926
5927 test_unsafe_string_to_interval_err!(
5929 vec![Some(format!(
5930 "{} century {} year {} month",
5931 i64::MAX - 2,
5932 i64::MAX - 2,
5933 i64::MAX - 2
5934 ))],
5935 IntervalUnit::DayTime,
5936 format!(
5937 "Arithmetic overflow: Overflow happened on: {} * 100",
5938 i64::MAX - 2
5939 )
5940 );
5941 test_unsafe_string_to_interval_err!(
5942 vec![Some(format!(
5943 "{} year {} month {} day",
5944 i64::MAX - 2,
5945 i64::MAX - 2,
5946 i64::MAX - 2
5947 ))],
5948 IntervalUnit::MonthDayNano,
5949 format!(
5950 "Arithmetic overflow: Overflow happened on: {} * 12",
5951 i64::MAX - 2
5952 )
5953 );
5954 }
5955
5956 #[test]
5957 fn test_cast_binary_to_fixed_size_binary() {
5958 let bytes_1 = b"Hiiii".as_slice();
5959 let bytes_2 = b"Hello".as_slice();
5960
5961 let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5962 let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
5963 let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
5964
5965 let array_ref = cast(&a1, &DataType::FixedSizeBinary(5)).unwrap();
5966 let down_cast = array_ref
5967 .as_any()
5968 .downcast_ref::<FixedSizeBinaryArray>()
5969 .unwrap();
5970 assert_eq!(bytes_1, down_cast.value(0));
5971 assert_eq!(bytes_2, down_cast.value(1));
5972 assert!(down_cast.is_null(2));
5973
5974 let array_ref = cast(&a2, &DataType::FixedSizeBinary(5)).unwrap();
5975 let down_cast = array_ref
5976 .as_any()
5977 .downcast_ref::<FixedSizeBinaryArray>()
5978 .unwrap();
5979 assert_eq!(bytes_1, down_cast.value(0));
5980 assert_eq!(bytes_2, down_cast.value(1));
5981 assert!(down_cast.is_null(2));
5982
5983 let bytes_1 = b"Hi".as_slice();
5985 let bytes_2 = b"Hello".as_slice();
5986
5987 let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5988 let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
5989 let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
5990
5991 let array_ref = cast_with_options(
5992 &a1,
5993 &DataType::FixedSizeBinary(5),
5994 &CastOptions {
5995 safe: false,
5996 format_options: FormatOptions::default(),
5997 },
5998 );
5999 assert!(array_ref.is_err());
6000
6001 let array_ref = cast_with_options(
6002 &a2,
6003 &DataType::FixedSizeBinary(5),
6004 &CastOptions {
6005 safe: false,
6006 format_options: FormatOptions::default(),
6007 },
6008 );
6009 assert!(array_ref.is_err());
6010 }
6011
6012 #[test]
6013 fn test_fixed_size_binary_to_binary() {
6014 let bytes_1 = b"Hiiii".as_slice();
6015 let bytes_2 = b"Hello".as_slice();
6016
6017 let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
6018 let a1 = Arc::new(FixedSizeBinaryArray::try_from(binary_data.clone()).unwrap()) as ArrayRef;
6019
6020 let array_ref = cast(&a1, &DataType::Binary).unwrap();
6021 let down_cast = array_ref.as_binary::<i32>();
6022 assert_eq!(bytes_1, down_cast.value(0));
6023 assert_eq!(bytes_2, down_cast.value(1));
6024 assert!(down_cast.is_null(2));
6025
6026 let array_ref = cast(&a1, &DataType::LargeBinary).unwrap();
6027 let down_cast = array_ref.as_binary::<i64>();
6028 assert_eq!(bytes_1, down_cast.value(0));
6029 assert_eq!(bytes_2, down_cast.value(1));
6030 assert!(down_cast.is_null(2));
6031
6032 let array_ref = cast(&a1, &DataType::BinaryView).unwrap();
6033 let down_cast = array_ref.as_binary_view();
6034 assert_eq!(bytes_1, down_cast.value(0));
6035 assert_eq!(bytes_2, down_cast.value(1));
6036 assert!(down_cast.is_null(2));
6037 }
6038
6039 #[test]
6040 fn test_fixed_size_binary_to_dictionary() {
6041 let bytes_1 = b"Hiiii".as_slice();
6042 let bytes_2 = b"Hello".as_slice();
6043
6044 let binary_data = vec![Some(bytes_1), Some(bytes_2), Some(bytes_1), None];
6045 let a1 = Arc::new(FixedSizeBinaryArray::try_from(binary_data.clone()).unwrap()) as ArrayRef;
6046
6047 let cast_type = DataType::Dictionary(
6048 Box::new(DataType::Int8),
6049 Box::new(DataType::FixedSizeBinary(5)),
6050 );
6051 let cast_array = cast(&a1, &cast_type).unwrap();
6052 assert_eq!(cast_array.data_type(), &cast_type);
6053 assert_eq!(
6054 array_to_strings(&cast_array),
6055 vec!["4869696969", "48656c6c6f", "4869696969", "null"]
6056 );
6057 let dict_array = cast_array.as_dictionary::<Int8Type>();
6059 assert_eq!(dict_array.values().len(), 2);
6060 }
6061
6062 #[test]
6063 fn test_binary_to_dictionary() {
6064 let mut builder = GenericBinaryBuilder::<i32>::new();
6065 builder.append_value(b"hello");
6066 builder.append_value(b"hiiii");
6067 builder.append_value(b"hiiii"); builder.append_null();
6069 builder.append_value(b"rustt");
6070
6071 let a1 = builder.finish();
6072
6073 let cast_type = DataType::Dictionary(
6074 Box::new(DataType::Int8),
6075 Box::new(DataType::FixedSizeBinary(5)),
6076 );
6077 let cast_array = cast(&a1, &cast_type).unwrap();
6078 assert_eq!(cast_array.data_type(), &cast_type);
6079 assert_eq!(
6080 array_to_strings(&cast_array),
6081 vec![
6082 "68656c6c6f",
6083 "6869696969",
6084 "6869696969",
6085 "null",
6086 "7275737474"
6087 ]
6088 );
6089 let dict_array = cast_array.as_dictionary::<Int8Type>();
6091 assert_eq!(dict_array.values().len(), 3);
6092 }
6093
6094 #[test]
6095 fn test_cast_string_array_to_dict_utf8_view() {
6096 let array = StringArray::from(vec![Some("one"), None, Some("three"), Some("one")]);
6097
6098 let cast_type =
6099 DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6100 assert!(can_cast_types(array.data_type(), &cast_type));
6101 let cast_array = cast(&array, &cast_type).unwrap();
6102 assert_eq!(cast_array.data_type(), &cast_type);
6103
6104 let dict_array = cast_array.as_dictionary::<UInt16Type>();
6105 assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6106 assert_eq!(dict_array.values().len(), 2); let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6109 let actual: Vec<Option<&str>> = typed.into_iter().collect();
6110 assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
6111
6112 let keys = dict_array.keys();
6113 assert!(keys.is_null(1));
6114 assert_eq!(keys.value(0), keys.value(3));
6115 assert_ne!(keys.value(0), keys.value(2));
6116 }
6117
6118 #[test]
6119 fn test_cast_string_array_to_dict_utf8_view_null_vs_literal_null() {
6120 let array = StringArray::from(vec![Some("one"), None, Some("null"), Some("one")]);
6121
6122 let cast_type =
6123 DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6124 assert!(can_cast_types(array.data_type(), &cast_type));
6125 let cast_array = cast(&array, &cast_type).unwrap();
6126 assert_eq!(cast_array.data_type(), &cast_type);
6127
6128 let dict_array = cast_array.as_dictionary::<UInt16Type>();
6129 assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6130 assert_eq!(dict_array.values().len(), 2);
6131
6132 let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6133 let actual: Vec<Option<&str>> = typed.into_iter().collect();
6134 assert_eq!(actual, vec![Some("one"), None, Some("null"), Some("one")]);
6135
6136 let keys = dict_array.keys();
6137 assert!(keys.is_null(1));
6138 assert_eq!(keys.value(0), keys.value(3));
6139 assert_ne!(keys.value(0), keys.value(2));
6140 }
6141
6142 #[test]
6143 fn test_cast_string_view_array_to_dict_utf8_view() {
6144 let array = StringViewArray::from(vec![Some("one"), None, Some("three"), Some("one")]);
6145
6146 let cast_type =
6147 DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6148 assert!(can_cast_types(array.data_type(), &cast_type));
6149 let cast_array = cast(&array, &cast_type).unwrap();
6150 assert_eq!(cast_array.data_type(), &cast_type);
6151
6152 let dict_array = cast_array.as_dictionary::<UInt16Type>();
6153 assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6154 assert_eq!(dict_array.values().len(), 2); let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6157 let actual: Vec<Option<&str>> = typed.into_iter().collect();
6158 assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
6159
6160 let keys = dict_array.keys();
6161 assert!(keys.is_null(1));
6162 assert_eq!(keys.value(0), keys.value(3));
6163 assert_ne!(keys.value(0), keys.value(2));
6164 }
6165
6166 #[test]
6167 fn test_cast_string_view_slice_to_dict_utf8_view() {
6168 let array = StringViewArray::from(vec![
6169 Some("zero"),
6170 Some("one"),
6171 None,
6172 Some("three"),
6173 Some("one"),
6174 ]);
6175 let view = array.slice(1, 4);
6176
6177 let cast_type =
6178 DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6179 assert!(can_cast_types(view.data_type(), &cast_type));
6180 let cast_array = cast(&view, &cast_type).unwrap();
6181 assert_eq!(cast_array.data_type(), &cast_type);
6182
6183 let dict_array = cast_array.as_dictionary::<UInt16Type>();
6184 assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6185 assert_eq!(dict_array.values().len(), 2);
6186
6187 let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6188 let actual: Vec<Option<&str>> = typed.into_iter().collect();
6189 assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
6190
6191 let keys = dict_array.keys();
6192 assert!(keys.is_null(1));
6193 assert_eq!(keys.value(0), keys.value(3));
6194 assert_ne!(keys.value(0), keys.value(2));
6195 }
6196
6197 #[test]
6198 fn test_cast_binary_array_to_dict_binary_view() {
6199 let mut builder = GenericBinaryBuilder::<i32>::new();
6200 builder.append_value(b"hello");
6201 builder.append_value(b"hiiii");
6202 builder.append_value(b"hiiii"); builder.append_null();
6204 builder.append_value(b"rustt");
6205
6206 let array = builder.finish();
6207
6208 let cast_type =
6209 DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6210 assert!(can_cast_types(array.data_type(), &cast_type));
6211 let cast_array = cast(&array, &cast_type).unwrap();
6212 assert_eq!(cast_array.data_type(), &cast_type);
6213
6214 let dict_array = cast_array.as_dictionary::<UInt16Type>();
6215 assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
6216 assert_eq!(dict_array.values().len(), 3);
6217
6218 let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
6219 let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6220 assert_eq!(
6221 actual,
6222 vec![
6223 Some(b"hello".as_slice()),
6224 Some(b"hiiii".as_slice()),
6225 Some(b"hiiii".as_slice()),
6226 None,
6227 Some(b"rustt".as_slice())
6228 ]
6229 );
6230
6231 let keys = dict_array.keys();
6232 assert!(keys.is_null(3));
6233 assert_eq!(keys.value(1), keys.value(2));
6234 assert_ne!(keys.value(0), keys.value(1));
6235 }
6236
6237 #[test]
6238 fn test_cast_binary_view_array_to_dict_binary_view() {
6239 let view = BinaryViewArray::from_iter([
6240 Some(b"hello".as_slice()),
6241 Some(b"hiiii".as_slice()),
6242 Some(b"hiiii".as_slice()), None,
6244 Some(b"rustt".as_slice()),
6245 ]);
6246
6247 let cast_type =
6248 DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6249 assert!(can_cast_types(view.data_type(), &cast_type));
6250 let cast_array = cast(&view, &cast_type).unwrap();
6251 assert_eq!(cast_array.data_type(), &cast_type);
6252
6253 let dict_array = cast_array.as_dictionary::<UInt16Type>();
6254 assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
6255 assert_eq!(dict_array.values().len(), 3);
6256
6257 let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
6258 let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6259 assert_eq!(
6260 actual,
6261 vec![
6262 Some(b"hello".as_slice()),
6263 Some(b"hiiii".as_slice()),
6264 Some(b"hiiii".as_slice()),
6265 None,
6266 Some(b"rustt".as_slice())
6267 ]
6268 );
6269
6270 let keys = dict_array.keys();
6271 assert!(keys.is_null(3));
6272 assert_eq!(keys.value(1), keys.value(2));
6273 assert_ne!(keys.value(0), keys.value(1));
6274 }
6275
6276 #[test]
6277 fn test_cast_binary_view_slice_to_dict_binary_view() {
6278 let view = BinaryViewArray::from_iter([
6279 Some(b"hello".as_slice()),
6280 Some(b"hiiii".as_slice()),
6281 Some(b"hiiii".as_slice()), None,
6283 Some(b"rustt".as_slice()),
6284 ]);
6285 let sliced = view.slice(1, 4);
6286
6287 let cast_type =
6288 DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6289 assert!(can_cast_types(sliced.data_type(), &cast_type));
6290 let cast_array = cast(&sliced, &cast_type).unwrap();
6291 assert_eq!(cast_array.data_type(), &cast_type);
6292
6293 let dict_array = cast_array.as_dictionary::<UInt16Type>();
6294 assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
6295 assert_eq!(dict_array.values().len(), 2);
6296
6297 let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
6298 let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6299 assert_eq!(
6300 actual,
6301 vec![
6302 Some(b"hiiii".as_slice()),
6303 Some(b"hiiii".as_slice()),
6304 None,
6305 Some(b"rustt".as_slice())
6306 ]
6307 );
6308
6309 let keys = dict_array.keys();
6310 assert!(keys.is_null(2));
6311 assert_eq!(keys.value(0), keys.value(1));
6312 assert_ne!(keys.value(0), keys.value(3));
6313 }
6314
6315 #[test]
6316 fn test_cast_string_array_to_dict_utf8_view_key_overflow_u8() {
6317 let array = StringArray::from_iter_values((0..257).map(|i| format!("v{i}")));
6318
6319 let cast_type =
6320 DataType::Dictionary(Box::new(DataType::UInt8), Box::new(DataType::Utf8View));
6321 assert!(can_cast_types(array.data_type(), &cast_type));
6322 let err = cast(&array, &cast_type).unwrap_err();
6323 assert!(matches!(err, ArrowError::DictionaryKeyOverflowError));
6324 }
6325
6326 #[test]
6327 fn test_cast_large_string_array_to_dict_utf8_view() {
6328 let array = LargeStringArray::from(vec![Some("one"), None, Some("three"), Some("one")]);
6329
6330 let cast_type =
6331 DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6332 assert!(can_cast_types(array.data_type(), &cast_type));
6333 let cast_array = cast(&array, &cast_type).unwrap();
6334 assert_eq!(cast_array.data_type(), &cast_type);
6335
6336 let dict_array = cast_array.as_dictionary::<UInt16Type>();
6337 assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6338 assert_eq!(dict_array.values().len(), 2); let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6341 let actual: Vec<Option<&str>> = typed.into_iter().collect();
6342 assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
6343
6344 let keys = dict_array.keys();
6345 assert!(keys.is_null(1));
6346 assert_eq!(keys.value(0), keys.value(3));
6347 assert_ne!(keys.value(0), keys.value(2));
6348 }
6349
6350 #[test]
6351 fn test_cast_large_binary_array_to_dict_binary_view() {
6352 let mut builder = GenericBinaryBuilder::<i64>::new();
6353 builder.append_value(b"hello");
6354 builder.append_value(b"world");
6355 builder.append_value(b"hello"); builder.append_null();
6357
6358 let array = builder.finish();
6359
6360 let cast_type =
6361 DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6362 assert!(can_cast_types(array.data_type(), &cast_type));
6363 let cast_array = cast(&array, &cast_type).unwrap();
6364 assert_eq!(cast_array.data_type(), &cast_type);
6365
6366 let dict_array = cast_array.as_dictionary::<UInt16Type>();
6367 assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
6368 assert_eq!(dict_array.values().len(), 2); let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
6371 let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6372 assert_eq!(
6373 actual,
6374 vec![
6375 Some(b"hello".as_slice()),
6376 Some(b"world".as_slice()),
6377 Some(b"hello".as_slice()),
6378 None
6379 ]
6380 );
6381
6382 let keys = dict_array.keys();
6383 assert!(keys.is_null(3));
6384 assert_eq!(keys.value(0), keys.value(2));
6385 assert_ne!(keys.value(0), keys.value(1));
6386 }
6387
6388 #[test]
6389 fn test_cast_struct_array_to_dict_struct() {
6390 let names = StringArray::from(vec![Some("alpha"), None, Some("gamma")]);
6396 let ids = Int32Array::from(vec![Some(1), Some(2), Some(3)]);
6397 let source = StructArray::from(vec![
6398 (
6399 Arc::new(Field::new("name", DataType::Utf8, true)),
6400 Arc::new(names) as ArrayRef,
6401 ),
6402 (
6403 Arc::new(Field::new("id", DataType::Int32, false)),
6404 Arc::new(ids) as ArrayRef,
6405 ),
6406 ]);
6407
6408 let target_value_type = DataType::Struct(
6409 vec![
6410 Field::new("name", DataType::Utf8View, true),
6411 Field::new("id", DataType::Int64, false),
6412 ]
6413 .into(),
6414 );
6415 let cast_type = DataType::Dictionary(
6416 Box::new(DataType::UInt32),
6417 Box::new(target_value_type.clone()),
6418 );
6419 assert!(can_cast_types(source.data_type(), &cast_type));
6420
6421 let cast_array = cast(&source, &cast_type).unwrap();
6422 assert_eq!(cast_array.data_type(), &cast_type);
6423 assert_eq!(cast_array.len(), 3);
6424
6425 let dict = cast_array.as_dictionary::<UInt32Type>();
6426 assert_eq!(dict.values().data_type(), &target_value_type);
6427 assert_eq!(dict.values().len(), 3);
6429
6430 let keys = dict.keys();
6435 assert_eq!(keys.values(), &[0u32, 1, 2]);
6436 assert_eq!(keys.null_count(), 0);
6437
6438 let struct_values = dict.values().as_struct();
6439 let names_out = struct_values
6440 .column_by_name("name")
6441 .unwrap()
6442 .as_string_view();
6443 assert_eq!(names_out.value(0), "alpha");
6444 assert!(names_out.is_null(1));
6445 assert_eq!(names_out.value(2), "gamma");
6446 let ids_out = struct_values
6447 .column_by_name("id")
6448 .unwrap()
6449 .as_primitive::<Int64Type>();
6450 assert_eq!(ids_out.values(), &[1i64, 2, 3]);
6451 }
6452
6453 #[test]
6454 fn test_cast_struct_array_to_dict_struct_row_nulls() {
6455 let names = StringArray::from(vec![Some("alpha"), Some("beta"), Some("gamma")]);
6459 let ids = Int32Array::from(vec![Some(1), Some(2), Some(3)]);
6460 let source = StructArray::try_new(
6461 vec![
6462 Field::new("name", DataType::Utf8, true),
6463 Field::new("id", DataType::Int32, false),
6464 ]
6465 .into(),
6466 vec![Arc::new(names) as ArrayRef, Arc::new(ids) as ArrayRef],
6467 Some(NullBuffer::from(vec![true, false, true])),
6468 )
6469 .unwrap();
6470
6471 let target_value_type = DataType::Struct(
6472 vec![
6473 Field::new("name", DataType::Utf8, true),
6474 Field::new("id", DataType::Int32, false),
6475 ]
6476 .into(),
6477 );
6478 let cast_type =
6479 DataType::Dictionary(Box::new(DataType::UInt32), Box::new(target_value_type));
6480
6481 let cast_array = cast(&source, &cast_type).unwrap();
6482 let dict = cast_array.as_dictionary::<UInt32Type>();
6483 assert_eq!(dict.len(), 3);
6484 let keys = dict.keys();
6485 assert!(!keys.is_null(0));
6486 assert!(keys.is_null(1));
6487 assert!(!keys.is_null(2));
6488 }
6489
6490 #[test]
6491 fn test_cast_struct_array_to_dict_struct_key_overflow() {
6492 let n = 300;
6495 let names = StringArray::from((0..n).map(|i| Some(format!("v{i}"))).collect::<Vec<_>>());
6496 let source = StructArray::from(vec![(
6497 Arc::new(Field::new("name", DataType::Utf8, true)),
6498 Arc::new(names) as ArrayRef,
6499 )]);
6500
6501 let cast_type = DataType::Dictionary(
6502 Box::new(DataType::UInt8),
6503 Box::new(DataType::Struct(
6504 vec![Field::new("name", DataType::Utf8, true)].into(),
6505 )),
6506 );
6507 let err = cast(&source, &cast_type).unwrap_err().to_string();
6508 assert!(
6509 err.contains("Cannot fit") && err.contains("dictionary keys"),
6510 "expected key-overflow error, got: {err}"
6511 );
6512 }
6513
6514 #[test]
6515 fn test_cast_empty_string_array_to_dict_utf8_view() {
6516 let array = StringArray::from(Vec::<Option<&str>>::new());
6517
6518 let cast_type =
6519 DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6520 assert!(can_cast_types(array.data_type(), &cast_type));
6521 let cast_array = cast(&array, &cast_type).unwrap();
6522 assert_eq!(cast_array.data_type(), &cast_type);
6523 assert_eq!(cast_array.len(), 0);
6524 }
6525
6526 #[test]
6527 fn test_cast_empty_binary_array_to_dict_binary_view() {
6528 let array = BinaryArray::from(Vec::<Option<&[u8]>>::new());
6529
6530 let cast_type =
6531 DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6532 assert!(can_cast_types(array.data_type(), &cast_type));
6533 let cast_array = cast(&array, &cast_type).unwrap();
6534 assert_eq!(cast_array.data_type(), &cast_type);
6535 assert_eq!(cast_array.len(), 0);
6536 }
6537
6538 #[test]
6539 fn test_cast_all_null_string_array_to_dict_utf8_view() {
6540 let array = StringArray::from(vec![None::<&str>, None, None]);
6541
6542 let cast_type =
6543 DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6544 assert!(can_cast_types(array.data_type(), &cast_type));
6545 let cast_array = cast(&array, &cast_type).unwrap();
6546 assert_eq!(cast_array.data_type(), &cast_type);
6547 assert_eq!(cast_array.null_count(), 3);
6548
6549 let dict_array = cast_array.as_dictionary::<UInt16Type>();
6550 assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6551 assert_eq!(dict_array.values().len(), 0);
6552 assert_eq!(dict_array.keys().null_count(), 3);
6553
6554 let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6555 let actual: Vec<Option<&str>> = typed.into_iter().collect();
6556 assert_eq!(actual, vec![None, None, None]);
6557 }
6558
6559 #[test]
6560 fn test_cast_all_null_binary_array_to_dict_binary_view() {
6561 let array = BinaryArray::from(vec![None::<&[u8]>, None, None]);
6562
6563 let cast_type =
6564 DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6565 assert!(can_cast_types(array.data_type(), &cast_type));
6566 let cast_array = cast(&array, &cast_type).unwrap();
6567 assert_eq!(cast_array.data_type(), &cast_type);
6568 assert_eq!(cast_array.null_count(), 3);
6569
6570 let dict_array = cast_array.as_dictionary::<UInt16Type>();
6571 assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
6572 assert_eq!(dict_array.values().len(), 0);
6573 assert_eq!(dict_array.keys().null_count(), 3);
6574
6575 let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
6576 let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6577 assert_eq!(actual, vec![None, None, None]);
6578 }
6579
6580 #[test]
6581 fn test_numeric_to_binary() {
6582 let a = Int16Array::from(vec![Some(1), Some(511), None]);
6583
6584 let array_ref = cast(&a, &DataType::Binary).unwrap();
6585 let down_cast = array_ref.as_binary::<i32>();
6586 assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
6587 assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
6588 assert!(down_cast.is_null(2));
6589
6590 let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
6591
6592 let array_ref = cast(&a, &DataType::Binary).unwrap();
6593 let down_cast = array_ref.as_binary::<i32>();
6594 assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
6595 assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
6596 assert!(down_cast.is_null(2));
6597 }
6598
6599 #[test]
6600 fn test_numeric_to_large_binary() {
6601 let a = Int16Array::from(vec![Some(1), Some(511), None]);
6602
6603 let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
6604 let down_cast = array_ref.as_binary::<i64>();
6605 assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
6606 assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
6607 assert!(down_cast.is_null(2));
6608
6609 let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
6610
6611 let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
6612 let down_cast = array_ref.as_binary::<i64>();
6613 assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
6614 assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
6615 assert!(down_cast.is_null(2));
6616 }
6617
6618 #[test]
6619 fn test_cast_date32_to_int32() {
6620 let array = Date32Array::from(vec![10000, 17890]);
6621 let b = cast(&array, &DataType::Int32).unwrap();
6622 let c = b.as_primitive::<Int32Type>();
6623 assert_eq!(10000, c.value(0));
6624 assert_eq!(17890, c.value(1));
6625 }
6626
6627 #[test]
6628 fn test_cast_int32_to_date32() {
6629 let array = Int32Array::from(vec![10000, 17890]);
6630 let b = cast(&array, &DataType::Date32).unwrap();
6631 let c = b.as_primitive::<Date32Type>();
6632 assert_eq!(10000, c.value(0));
6633 assert_eq!(17890, c.value(1));
6634 }
6635
6636 #[test]
6637 fn test_cast_timestamp_to_date32() {
6638 let array =
6639 TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
6640 .with_timezone("+00:00".to_string());
6641 let b = cast(&array, &DataType::Date32).unwrap();
6642 let c = b.as_primitive::<Date32Type>();
6643 assert_eq!(10000, c.value(0));
6644 assert_eq!(17890, c.value(1));
6645 assert!(c.is_null(2));
6646 }
6647 #[test]
6648 fn test_cast_timestamp_to_date32_zone() {
6649 let strings = StringArray::from_iter([
6650 Some("1970-01-01T00:00:01"),
6651 Some("1970-01-01T23:59:59"),
6652 None,
6653 Some("2020-03-01T02:00:23+00:00"),
6654 ]);
6655 let dt = DataType::Timestamp(TimeUnit::Millisecond, Some("-07:00".into()));
6656 let timestamps = cast(&strings, &dt).unwrap();
6657 let dates = cast(timestamps.as_ref(), &DataType::Date32).unwrap();
6658
6659 let c = dates.as_primitive::<Date32Type>();
6660 let expected = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap();
6661 assert_eq!(c.value_as_date(0).unwrap(), expected);
6662 assert_eq!(c.value_as_date(1).unwrap(), expected);
6663 assert!(c.is_null(2));
6664 let expected = NaiveDate::from_ymd_opt(2020, 2, 29).unwrap();
6665 assert_eq!(c.value_as_date(3).unwrap(), expected);
6666 }
6667 #[test]
6668 fn test_cast_timestamp_to_date64() {
6669 let array =
6670 TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None]);
6671 let b = cast(&array, &DataType::Date64).unwrap();
6672 let c = b.as_primitive::<Date64Type>();
6673 assert_eq!(864000000005, c.value(0));
6674 assert_eq!(1545696000001, c.value(1));
6675 assert!(c.is_null(2));
6676
6677 let array = TimestampSecondArray::from(vec![Some(864000000005), Some(1545696000001)]);
6678 let b = cast(&array, &DataType::Date64).unwrap();
6679 let c = b.as_primitive::<Date64Type>();
6680 assert_eq!(864000000005000, c.value(0));
6681 assert_eq!(1545696000001000, c.value(1));
6682
6683 let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
6685 let b = cast(&array, &DataType::Date64).unwrap();
6686 assert!(b.is_null(0));
6687 let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
6689 let options = CastOptions {
6690 safe: false,
6691 format_options: FormatOptions::default(),
6692 };
6693 let b = cast_with_options(&array, &DataType::Date64, &options);
6694 assert!(b.is_err());
6695 }
6696
6697 #[test]
6698 fn test_cast_timestamp_to_time64() {
6699 let array = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
6701 .with_timezone("+01:00".to_string());
6702 let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6703 let c = b.as_primitive::<Time64MicrosecondType>();
6704 assert_eq!(3605000000, c.value(0));
6705 assert_eq!(3601000000, c.value(1));
6706 assert!(c.is_null(2));
6707 let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6708 let c = b.as_primitive::<Time64NanosecondType>();
6709 assert_eq!(3605000000000, c.value(0));
6710 assert_eq!(3601000000000, c.value(1));
6711 assert!(c.is_null(2));
6712
6713 let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
6715 .with_timezone("+01:00".to_string());
6716 let array = Arc::new(a) as ArrayRef;
6717 let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6718 let c = b.as_primitive::<Time64MicrosecondType>();
6719 assert_eq!(3605000000, c.value(0));
6720 assert_eq!(3601000000, c.value(1));
6721 assert!(c.is_null(2));
6722 let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6723 let c = b.as_primitive::<Time64NanosecondType>();
6724 assert_eq!(3605000000000, c.value(0));
6725 assert_eq!(3601000000000, c.value(1));
6726 assert!(c.is_null(2));
6727
6728 let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
6730 .with_timezone("+01:00".to_string());
6731 let array = Arc::new(a) as ArrayRef;
6732 let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6733 let c = b.as_primitive::<Time64MicrosecondType>();
6734 assert_eq!(3605000000, c.value(0));
6735 assert_eq!(3601000000, c.value(1));
6736 assert!(c.is_null(2));
6737 let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6738 let c = b.as_primitive::<Time64NanosecondType>();
6739 assert_eq!(3605000000000, c.value(0));
6740 assert_eq!(3601000000000, c.value(1));
6741 assert!(c.is_null(2));
6742
6743 let a = TimestampNanosecondArray::from(vec![Some(86405000000000), Some(1000000000), None])
6745 .with_timezone("+01:00".to_string());
6746 let array = Arc::new(a) as ArrayRef;
6747 let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6748 let c = b.as_primitive::<Time64MicrosecondType>();
6749 assert_eq!(3605000000, c.value(0));
6750 assert_eq!(3601000000, c.value(1));
6751 assert!(c.is_null(2));
6752 let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6753 let c = b.as_primitive::<Time64NanosecondType>();
6754 assert_eq!(3605000000000, c.value(0));
6755 assert_eq!(3601000000000, c.value(1));
6756 assert!(c.is_null(2));
6757
6758 let a =
6760 TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
6761 let array = Arc::new(a) as ArrayRef;
6762 let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond));
6763 assert!(b.is_err());
6764 let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond));
6765 assert!(b.is_err());
6766 let b = cast(&array, &DataType::Time64(TimeUnit::Millisecond));
6767 assert!(b.is_err());
6768 }
6769
6770 #[test]
6771 fn test_cast_timestamp_to_time32() {
6772 let a = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
6774 .with_timezone("+01:00".to_string());
6775 let array = Arc::new(a) as ArrayRef;
6776 let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6777 let c = b.as_primitive::<Time32SecondType>();
6778 assert_eq!(3605, c.value(0));
6779 assert_eq!(3601, c.value(1));
6780 assert!(c.is_null(2));
6781 let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6782 let c = b.as_primitive::<Time32MillisecondType>();
6783 assert_eq!(3605000, c.value(0));
6784 assert_eq!(3601000, c.value(1));
6785 assert!(c.is_null(2));
6786
6787 let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
6789 .with_timezone("+01:00".to_string());
6790 let array = Arc::new(a) as ArrayRef;
6791 let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6792 let c = b.as_primitive::<Time32SecondType>();
6793 assert_eq!(3605, c.value(0));
6794 assert_eq!(3601, c.value(1));
6795 assert!(c.is_null(2));
6796 let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6797 let c = b.as_primitive::<Time32MillisecondType>();
6798 assert_eq!(3605000, c.value(0));
6799 assert_eq!(3601000, c.value(1));
6800 assert!(c.is_null(2));
6801
6802 let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
6804 .with_timezone("+01:00".to_string());
6805 let array = Arc::new(a) as ArrayRef;
6806 let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6807 let c = b.as_primitive::<Time32SecondType>();
6808 assert_eq!(3605, c.value(0));
6809 assert_eq!(3601, c.value(1));
6810 assert!(c.is_null(2));
6811 let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6812 let c = b.as_primitive::<Time32MillisecondType>();
6813 assert_eq!(3605000, c.value(0));
6814 assert_eq!(3601000, c.value(1));
6815 assert!(c.is_null(2));
6816
6817 let a = TimestampNanosecondArray::from(vec![Some(86405000000000), Some(1000000000), None])
6819 .with_timezone("+01:00".to_string());
6820 let array = Arc::new(a) as ArrayRef;
6821 let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6822 let c = b.as_primitive::<Time32SecondType>();
6823 assert_eq!(3605, c.value(0));
6824 assert_eq!(3601, c.value(1));
6825 assert!(c.is_null(2));
6826 let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6827 let c = b.as_primitive::<Time32MillisecondType>();
6828 assert_eq!(3605000, c.value(0));
6829 assert_eq!(3601000, c.value(1));
6830 assert!(c.is_null(2));
6831
6832 let a =
6834 TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
6835 let array = Arc::new(a) as ArrayRef;
6836 let b = cast(&array, &DataType::Time32(TimeUnit::Second));
6837 assert!(b.is_err());
6838 let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond));
6839 assert!(b.is_err());
6840 }
6841
6842 #[test]
6844 fn test_cast_timestamp_with_timezone_1() {
6845 let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
6846 Some("2000-01-01T00:00:00.123456789"),
6847 Some("2010-01-01T00:00:00.123456789"),
6848 None,
6849 ]));
6850 let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
6851 let timestamp_array = cast(&string_array, &to_type).unwrap();
6852
6853 let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
6854 let timestamp_array = cast(×tamp_array, &to_type).unwrap();
6855
6856 let string_array = cast(×tamp_array, &DataType::Utf8).unwrap();
6857 let result = string_array.as_string::<i32>();
6858 assert_eq!("2000-01-01T00:00:00.123456+07:00", result.value(0));
6859 assert_eq!("2010-01-01T00:00:00.123456+07:00", result.value(1));
6860 assert!(result.is_null(2));
6861 }
6862
6863 #[test]
6865 fn test_cast_timestamp_with_timezone_2() {
6866 let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
6867 Some("2000-01-01T07:00:00.123456789"),
6868 Some("2010-01-01T07:00:00.123456789"),
6869 None,
6870 ]));
6871 let to_type = DataType::Timestamp(TimeUnit::Millisecond, Some("+0700".into()));
6872 let timestamp_array = cast(&string_array, &to_type).unwrap();
6873
6874 let string_array = cast(×tamp_array, &DataType::Utf8).unwrap();
6876 let result = string_array.as_string::<i32>();
6877 assert_eq!("2000-01-01T07:00:00.123+07:00", result.value(0));
6878 assert_eq!("2010-01-01T07:00:00.123+07:00", result.value(1));
6879 assert!(result.is_null(2));
6880
6881 let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
6882 let timestamp_array = cast(×tamp_array, &to_type).unwrap();
6883
6884 let string_array = cast(×tamp_array, &DataType::Utf8).unwrap();
6885 let result = string_array.as_string::<i32>();
6886 assert_eq!("2000-01-01T00:00:00.123", result.value(0));
6887 assert_eq!("2010-01-01T00:00:00.123", result.value(1));
6888 assert!(result.is_null(2));
6889 }
6890
6891 #[test]
6893 fn test_cast_timestamp_with_timezone_3() {
6894 let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
6895 Some("2000-01-01T07:00:00.123456789"),
6896 Some("2010-01-01T07:00:00.123456789"),
6897 None,
6898 ]));
6899 let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
6900 let timestamp_array = cast(&string_array, &to_type).unwrap();
6901
6902 let string_array = cast(×tamp_array, &DataType::Utf8).unwrap();
6904 let result = string_array.as_string::<i32>();
6905 assert_eq!("2000-01-01T07:00:00.123456+07:00", result.value(0));
6906 assert_eq!("2010-01-01T07:00:00.123456+07:00", result.value(1));
6907 assert!(result.is_null(2));
6908
6909 let to_type = DataType::Timestamp(TimeUnit::Second, Some("-08:00".into()));
6910 let timestamp_array = cast(×tamp_array, &to_type).unwrap();
6911
6912 let string_array = cast(×tamp_array, &DataType::Utf8).unwrap();
6913 let result = string_array.as_string::<i32>();
6914 assert_eq!("1999-12-31T16:00:00-08:00", result.value(0));
6915 assert_eq!("2009-12-31T16:00:00-08:00", result.value(1));
6916 assert!(result.is_null(2));
6917 }
6918
6919 #[test]
6920 fn test_cast_date64_to_timestamp() {
6921 let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6922 let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
6923 let c = b.as_primitive::<TimestampSecondType>();
6924 assert_eq!(864000000, c.value(0));
6925 assert_eq!(1545696000, c.value(1));
6926 assert!(c.is_null(2));
6927 }
6928
6929 #[test]
6930 fn test_cast_date64_to_timestamp_ms() {
6931 let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6932 let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
6933 let c = b
6934 .as_any()
6935 .downcast_ref::<TimestampMillisecondArray>()
6936 .unwrap();
6937 assert_eq!(864000000005, c.value(0));
6938 assert_eq!(1545696000001, c.value(1));
6939 assert!(c.is_null(2));
6940 }
6941
6942 #[test]
6943 fn test_cast_date64_to_timestamp_us() {
6944 let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6945 let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
6946 let c = b
6947 .as_any()
6948 .downcast_ref::<TimestampMicrosecondArray>()
6949 .unwrap();
6950 assert_eq!(864000000005000, c.value(0));
6951 assert_eq!(1545696000001000, c.value(1));
6952 assert!(c.is_null(2));
6953 }
6954
6955 #[test]
6956 fn test_cast_date64_to_timestamp_ns() {
6957 let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6958 let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
6959 let c = b
6960 .as_any()
6961 .downcast_ref::<TimestampNanosecondArray>()
6962 .unwrap();
6963 assert_eq!(864000000005000000, c.value(0));
6964 assert_eq!(1545696000001000000, c.value(1));
6965 assert!(c.is_null(2));
6966 }
6967
6968 #[test]
6969 fn test_cast_timestamp_to_i64() {
6970 let array =
6971 TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
6972 .with_timezone("UTC".to_string());
6973 let b = cast(&array, &DataType::Int64).unwrap();
6974 let c = b.as_primitive::<Int64Type>();
6975 assert_eq!(&DataType::Int64, c.data_type());
6976 assert_eq!(864000000005, c.value(0));
6977 assert_eq!(1545696000001, c.value(1));
6978 assert!(c.is_null(2));
6979 }
6980
6981 macro_rules! assert_cast {
6982 ($array:expr, $datatype:expr, $output_array_type: ty, $expected:expr) => {{
6983 assert!(can_cast_types($array.data_type(), &$datatype));
6984 let out = cast(&$array, &$datatype).unwrap();
6985 let actual = out
6986 .as_any()
6987 .downcast_ref::<$output_array_type>()
6988 .unwrap()
6989 .into_iter()
6990 .collect::<Vec<_>>();
6991 assert_eq!(actual, $expected);
6992 }};
6993 ($array:expr, $datatype:expr, $output_array_type: ty, $options:expr, $expected:expr) => {{
6994 assert!(can_cast_types($array.data_type(), &$datatype));
6995 let out = cast_with_options(&$array, &$datatype, &$options).unwrap();
6996 let actual = out
6997 .as_any()
6998 .downcast_ref::<$output_array_type>()
6999 .unwrap()
7000 .into_iter()
7001 .collect::<Vec<_>>();
7002 assert_eq!(actual, $expected);
7003 }};
7004 }
7005
7006 #[test]
7007 fn test_cast_date32_to_string() {
7008 let array = Date32Array::from(vec![Some(0), Some(10000), Some(13036), Some(17890), None]);
7009 let expected = vec![
7010 Some("1970-01-01"),
7011 Some("1997-05-19"),
7012 Some("2005-09-10"),
7013 Some("2018-12-25"),
7014 None,
7015 ];
7016
7017 assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
7018 assert_cast!(array, DataType::Utf8, StringArray, expected);
7019 assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
7020 }
7021
7022 #[test]
7023 fn test_cast_date64_to_string() {
7024 let array = Date64Array::from(vec![
7025 Some(0),
7026 Some(10000 * 86400000),
7027 Some(13036 * 86400000),
7028 Some(17890 * 86400000),
7029 None,
7030 ]);
7031 let expected = vec![
7032 Some("1970-01-01T00:00:00"),
7033 Some("1997-05-19T00:00:00"),
7034 Some("2005-09-10T00:00:00"),
7035 Some("2018-12-25T00:00:00"),
7036 None,
7037 ];
7038
7039 assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
7040 assert_cast!(array, DataType::Utf8, StringArray, expected);
7041 assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
7042 }
7043
7044 #[test]
7045 fn test_cast_date32_to_timestamp_and_timestamp_with_timezone() {
7046 let tz = "+0545"; let a = Date32Array::from(vec![Some(18628), None, None]); let array = Arc::new(a) as ArrayRef;
7049
7050 let b = cast(
7051 &array,
7052 &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
7053 )
7054 .unwrap();
7055 let c = b.as_primitive::<TimestampSecondType>();
7056 let string_array = cast(&c, &DataType::Utf8).unwrap();
7057 let result = string_array.as_string::<i32>();
7058 assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
7059
7060 let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
7061 let c = b.as_primitive::<TimestampSecondType>();
7062 let string_array = cast(&c, &DataType::Utf8).unwrap();
7063 let result = string_array.as_string::<i32>();
7064 assert_eq!("2021-01-01T00:00:00", result.value(0));
7065 }
7066
7067 #[test]
7068 fn test_cast_date32_to_timestamp_with_timezone() {
7069 let tz = "+0545"; let a = Date32Array::from(vec![Some(18628), Some(18993), None]); let array = Arc::new(a) as ArrayRef;
7072 let b = cast(
7073 &array,
7074 &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
7075 )
7076 .unwrap();
7077 let c = b.as_primitive::<TimestampSecondType>();
7078 assert_eq!(1609438500, c.value(0));
7079 assert_eq!(1640974500, c.value(1));
7080 assert!(c.is_null(2));
7081
7082 let string_array = cast(&c, &DataType::Utf8).unwrap();
7083 let result = string_array.as_string::<i32>();
7084 assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
7085 assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
7086 }
7087
7088 #[test]
7089 fn test_cast_date32_to_timestamp_with_timezone_ms() {
7090 let tz = "+0545"; let a = Date32Array::from(vec![Some(18628), Some(18993), None]); let array = Arc::new(a) as ArrayRef;
7093 let b = cast(
7094 &array,
7095 &DataType::Timestamp(TimeUnit::Millisecond, Some(tz.into())),
7096 )
7097 .unwrap();
7098 let c = b.as_primitive::<TimestampMillisecondType>();
7099 assert_eq!(1609438500000, c.value(0));
7100 assert_eq!(1640974500000, c.value(1));
7101 assert!(c.is_null(2));
7102
7103 let string_array = cast(&c, &DataType::Utf8).unwrap();
7104 let result = string_array.as_string::<i32>();
7105 assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
7106 assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
7107 }
7108
7109 #[test]
7110 fn test_cast_date32_to_timestamp_with_timezone_us() {
7111 let tz = "+0545"; let a = Date32Array::from(vec![Some(18628), Some(18993), None]); let array = Arc::new(a) as ArrayRef;
7114 let b = cast(
7115 &array,
7116 &DataType::Timestamp(TimeUnit::Microsecond, Some(tz.into())),
7117 )
7118 .unwrap();
7119 let c = b.as_primitive::<TimestampMicrosecondType>();
7120 assert_eq!(1609438500000000, c.value(0));
7121 assert_eq!(1640974500000000, c.value(1));
7122 assert!(c.is_null(2));
7123
7124 let string_array = cast(&c, &DataType::Utf8).unwrap();
7125 let result = string_array.as_string::<i32>();
7126 assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
7127 assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
7128 }
7129
7130 #[test]
7131 fn test_cast_date32_to_timestamp_with_timezone_ns() {
7132 let tz = "+0545"; let a = Date32Array::from(vec![Some(18628), Some(18993), None]); let array = Arc::new(a) as ArrayRef;
7135 let b = cast(
7136 &array,
7137 &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.into())),
7138 )
7139 .unwrap();
7140 let c = b.as_primitive::<TimestampNanosecondType>();
7141 assert_eq!(1609438500000000000, c.value(0));
7142 assert_eq!(1640974500000000000, c.value(1));
7143 assert!(c.is_null(2));
7144
7145 let string_array = cast(&c, &DataType::Utf8).unwrap();
7146 let result = string_array.as_string::<i32>();
7147 assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
7148 assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
7149 }
7150
7151 #[test]
7152 fn test_cast_date64_to_timestamp_with_timezone() {
7153 let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
7154 let tz = "+0545"; let b = cast(
7156 &array,
7157 &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
7158 )
7159 .unwrap();
7160
7161 let c = b.as_primitive::<TimestampSecondType>();
7162 assert_eq!(863979300, c.value(0));
7163 assert_eq!(1545675300, c.value(1));
7164 assert!(c.is_null(2));
7165
7166 let string_array = cast(&c, &DataType::Utf8).unwrap();
7167 let result = string_array.as_string::<i32>();
7168 assert_eq!("1997-05-19T00:00:00+05:45", result.value(0));
7169 assert_eq!("2018-12-25T00:00:00+05:45", result.value(1));
7170 }
7171
7172 #[test]
7173 fn test_cast_date64_to_timestamp_with_timezone_ms() {
7174 let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
7175 let tz = "+0545"; let b = cast(
7177 &array,
7178 &DataType::Timestamp(TimeUnit::Millisecond, Some(tz.into())),
7179 )
7180 .unwrap();
7181
7182 let c = b.as_primitive::<TimestampMillisecondType>();
7183 assert_eq!(863979300005, c.value(0));
7184 assert_eq!(1545675300001, c.value(1));
7185 assert!(c.is_null(2));
7186
7187 let string_array = cast(&c, &DataType::Utf8).unwrap();
7188 let result = string_array.as_string::<i32>();
7189 assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
7190 assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
7191 }
7192
7193 #[test]
7194 fn test_cast_date64_to_timestamp_with_timezone_us() {
7195 let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
7196 let tz = "+0545"; let b = cast(
7198 &array,
7199 &DataType::Timestamp(TimeUnit::Microsecond, Some(tz.into())),
7200 )
7201 .unwrap();
7202
7203 let c = b.as_primitive::<TimestampMicrosecondType>();
7204 assert_eq!(863979300005000, c.value(0));
7205 assert_eq!(1545675300001000, c.value(1));
7206 assert!(c.is_null(2));
7207
7208 let string_array = cast(&c, &DataType::Utf8).unwrap();
7209 let result = string_array.as_string::<i32>();
7210 assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
7211 assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
7212 }
7213
7214 #[test]
7215 fn test_cast_date64_to_timestamp_with_timezone_ns() {
7216 let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
7217 let tz = "+0545"; let b = cast(
7219 &array,
7220 &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.into())),
7221 )
7222 .unwrap();
7223
7224 let c = b.as_primitive::<TimestampNanosecondType>();
7225 assert_eq!(863979300005000000, c.value(0));
7226 assert_eq!(1545675300001000000, c.value(1));
7227 assert!(c.is_null(2));
7228
7229 let string_array = cast(&c, &DataType::Utf8).unwrap();
7230 let result = string_array.as_string::<i32>();
7231 assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
7232 assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
7233 }
7234
7235 #[test]
7236 fn test_cast_timestamp_to_strings() {
7237 let array =
7239 TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
7240 let expected = vec![
7241 Some("1997-05-19T00:00:03.005"),
7242 Some("2018-12-25T00:00:02.001"),
7243 None,
7244 ];
7245
7246 assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
7247 assert_cast!(array, DataType::Utf8, StringArray, expected);
7248 assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
7249 }
7250
7251 #[test]
7252 fn test_cast_timestamp_to_strings_opt() {
7253 let ts_format = "%Y-%m-%d %H:%M:%S%.6f";
7254 let tz = "+0545"; let cast_options = CastOptions {
7256 safe: true,
7257 format_options: FormatOptions::default()
7258 .with_timestamp_format(Some(ts_format))
7259 .with_timestamp_tz_format(Some(ts_format)),
7260 };
7261
7262 let array_without_tz =
7264 TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
7265 let expected = vec![
7266 Some("1997-05-19 00:00:03.005000"),
7267 Some("2018-12-25 00:00:02.001000"),
7268 None,
7269 ];
7270 assert_cast!(
7271 array_without_tz,
7272 DataType::Utf8View,
7273 StringViewArray,
7274 cast_options,
7275 expected
7276 );
7277 assert_cast!(
7278 array_without_tz,
7279 DataType::Utf8,
7280 StringArray,
7281 cast_options,
7282 expected
7283 );
7284 assert_cast!(
7285 array_without_tz,
7286 DataType::LargeUtf8,
7287 LargeStringArray,
7288 cast_options,
7289 expected
7290 );
7291
7292 let array_with_tz =
7293 TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None])
7294 .with_timezone(tz.to_string());
7295 let expected = vec![
7296 Some("1997-05-19 05:45:03.005000"),
7297 Some("2018-12-25 05:45:02.001000"),
7298 None,
7299 ];
7300 assert_cast!(
7301 array_with_tz,
7302 DataType::Utf8View,
7303 StringViewArray,
7304 cast_options,
7305 expected
7306 );
7307 assert_cast!(
7308 array_with_tz,
7309 DataType::Utf8,
7310 StringArray,
7311 cast_options,
7312 expected
7313 );
7314 assert_cast!(
7315 array_with_tz,
7316 DataType::LargeUtf8,
7317 LargeStringArray,
7318 cast_options,
7319 expected
7320 );
7321 }
7322
7323 #[test]
7324 fn test_cast_between_timestamps() {
7325 let array =
7326 TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
7327 let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
7328 let c = b.as_primitive::<TimestampSecondType>();
7329 assert_eq!(864000003, c.value(0));
7330 assert_eq!(1545696002, c.value(1));
7331 assert!(c.is_null(2));
7332 }
7333
7334 #[test]
7335 fn test_cast_duration_to_i64() {
7336 let base = vec![5, 6, 7, 8, 100000000];
7337
7338 let duration_arrays = vec![
7339 Arc::new(DurationNanosecondArray::from(base.clone())) as ArrayRef,
7340 Arc::new(DurationMicrosecondArray::from(base.clone())) as ArrayRef,
7341 Arc::new(DurationMillisecondArray::from(base.clone())) as ArrayRef,
7342 Arc::new(DurationSecondArray::from(base.clone())) as ArrayRef,
7343 ];
7344
7345 for arr in duration_arrays {
7346 assert!(can_cast_types(arr.data_type(), &DataType::Int64));
7347 let result = cast(&arr, &DataType::Int64).unwrap();
7348 let result = result.as_primitive::<Int64Type>();
7349 assert_eq!(base.as_slice(), result.values());
7350 }
7351 }
7352
7353 #[test]
7354 fn test_cast_between_durations_and_numerics() {
7355 fn test_cast_between_durations<FromType, ToType>()
7356 where
7357 FromType: ArrowPrimitiveType<Native = i64>,
7358 ToType: ArrowPrimitiveType<Native = i64>,
7359 PrimitiveArray<FromType>: From<Vec<Option<i64>>>,
7360 {
7361 let DataType::Duration(from_unit) = FromType::DATA_TYPE else {
7362 panic!("Expected a duration type")
7363 };
7364 let DataType::Duration(to_unit) = ToType::DATA_TYPE else {
7365 panic!("Expected a duration type")
7366 };
7367 let from_size = time_unit_multiple(&from_unit);
7368 let to_size = time_unit_multiple(&to_unit);
7369
7370 let (v1_before, v2_before) = (8640003005, 1696002001);
7371 let (v1_after, v2_after) = if from_size >= to_size {
7372 (
7373 v1_before / (from_size / to_size),
7374 v2_before / (from_size / to_size),
7375 )
7376 } else {
7377 (
7378 v1_before * (to_size / from_size),
7379 v2_before * (to_size / from_size),
7380 )
7381 };
7382
7383 let array =
7384 PrimitiveArray::<FromType>::from(vec![Some(v1_before), Some(v2_before), None]);
7385 let b = cast(&array, &ToType::DATA_TYPE).unwrap();
7386 let c = b.as_primitive::<ToType>();
7387 assert_eq!(v1_after, c.value(0));
7388 assert_eq!(v2_after, c.value(1));
7389 assert!(c.is_null(2));
7390 }
7391
7392 test_cast_between_durations::<DurationSecondType, DurationMillisecondType>();
7394 test_cast_between_durations::<DurationSecondType, DurationMicrosecondType>();
7395 test_cast_between_durations::<DurationSecondType, DurationNanosecondType>();
7396 test_cast_between_durations::<DurationMillisecondType, DurationSecondType>();
7397 test_cast_between_durations::<DurationMillisecondType, DurationMicrosecondType>();
7398 test_cast_between_durations::<DurationMillisecondType, DurationNanosecondType>();
7399 test_cast_between_durations::<DurationMicrosecondType, DurationSecondType>();
7400 test_cast_between_durations::<DurationMicrosecondType, DurationMillisecondType>();
7401 test_cast_between_durations::<DurationMicrosecondType, DurationNanosecondType>();
7402 test_cast_between_durations::<DurationNanosecondType, DurationSecondType>();
7403 test_cast_between_durations::<DurationNanosecondType, DurationMillisecondType>();
7404 test_cast_between_durations::<DurationNanosecondType, DurationMicrosecondType>();
7405
7406 let array = DurationSecondArray::from(vec![
7408 Some(i64::MAX),
7409 Some(8640203410378005),
7410 Some(10241096),
7411 None,
7412 ]);
7413 let b = cast(&array, &DataType::Duration(TimeUnit::Nanosecond)).unwrap();
7414 let c = b.as_primitive::<DurationNanosecondType>();
7415 assert!(c.is_null(0));
7416 assert!(c.is_null(1));
7417 assert_eq!(10241096000000000, c.value(2));
7418 assert!(c.is_null(3));
7419
7420 let array = DurationSecondArray::from(vec![
7422 Some(i64::MAX),
7423 Some(8640203410378005),
7424 Some(10241096),
7425 None,
7426 ]);
7427 let b = cast(&array, &DataType::Int64).unwrap();
7428 let c = b.as_primitive::<Int64Type>();
7429 assert_eq!(i64::MAX, c.value(0));
7430 assert_eq!(8640203410378005, c.value(1));
7431 assert_eq!(10241096, c.value(2));
7432 assert!(c.is_null(3));
7433
7434 let b = cast(&array, &DataType::Int32).unwrap();
7435 let c = b.as_primitive::<Int32Type>();
7436 assert_eq!(0, c.value(0));
7437 assert_eq!(0, c.value(1));
7438 assert_eq!(10241096, c.value(2));
7439 assert!(c.is_null(3));
7440
7441 let array = Int32Array::from(vec![Some(i32::MAX), Some(802034103), Some(10241096), None]);
7443 let b = cast(&array, &DataType::Duration(TimeUnit::Second)).unwrap();
7444 let c = b.as_any().downcast_ref::<DurationSecondArray>().unwrap();
7445 assert_eq!(i32::MAX as i64, c.value(0));
7446 assert_eq!(802034103, c.value(1));
7447 assert_eq!(10241096, c.value(2));
7448 assert!(c.is_null(3));
7449 }
7450
7451 #[test]
7452 fn test_cast_to_strings() {
7453 let a = Int32Array::from(vec![1, 2, 3]);
7454 let out = cast(&a, &DataType::Utf8).unwrap();
7455 let out = out
7456 .as_any()
7457 .downcast_ref::<StringArray>()
7458 .unwrap()
7459 .into_iter()
7460 .collect::<Vec<_>>();
7461 assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
7462 let out = cast(&a, &DataType::LargeUtf8).unwrap();
7463 let out = out
7464 .as_any()
7465 .downcast_ref::<LargeStringArray>()
7466 .unwrap()
7467 .into_iter()
7468 .collect::<Vec<_>>();
7469 assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
7470 }
7471
7472 #[test]
7473 fn test_str_to_str_casts() {
7474 for data in [
7475 vec![Some("foo"), Some("bar"), Some("ham")],
7476 vec![Some("foo"), None, Some("bar")],
7477 ] {
7478 let a = LargeStringArray::from(data.clone());
7479 let to = cast(&a, &DataType::Utf8).unwrap();
7480 let expect = a
7481 .as_any()
7482 .downcast_ref::<LargeStringArray>()
7483 .unwrap()
7484 .into_iter()
7485 .collect::<Vec<_>>();
7486 let out = to
7487 .as_any()
7488 .downcast_ref::<StringArray>()
7489 .unwrap()
7490 .into_iter()
7491 .collect::<Vec<_>>();
7492 assert_eq!(expect, out);
7493
7494 let a = StringArray::from(data);
7495 let to = cast(&a, &DataType::LargeUtf8).unwrap();
7496 let expect = a
7497 .as_any()
7498 .downcast_ref::<StringArray>()
7499 .unwrap()
7500 .into_iter()
7501 .collect::<Vec<_>>();
7502 let out = to
7503 .as_any()
7504 .downcast_ref::<LargeStringArray>()
7505 .unwrap()
7506 .into_iter()
7507 .collect::<Vec<_>>();
7508 assert_eq!(expect, out);
7509 }
7510 }
7511
7512 const VIEW_TEST_DATA: [Option<&str>; 5] = [
7513 Some("hello"),
7514 Some("repeated"),
7515 None,
7516 Some("large payload over 12 bytes"),
7517 Some("repeated"),
7518 ];
7519
7520 #[test]
7521 fn test_string_view_to_binary_view() {
7522 let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7523
7524 assert!(can_cast_types(
7525 string_view_array.data_type(),
7526 &DataType::BinaryView
7527 ));
7528
7529 let binary_view_array = cast(&string_view_array, &DataType::BinaryView).unwrap();
7530 assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
7531
7532 let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7533 assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
7534 }
7535
7536 #[test]
7537 fn test_binary_view_to_string_view() {
7538 let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7539
7540 assert!(can_cast_types(
7541 binary_view_array.data_type(),
7542 &DataType::Utf8View
7543 ));
7544
7545 let string_view_array = cast(&binary_view_array, &DataType::Utf8View).unwrap();
7546 assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7547
7548 let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7549 assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
7550 }
7551
7552 #[test]
7553 fn test_binary_view_to_string_view_with_invalid_utf8() {
7554 let binary_view_array = BinaryViewArray::from_iter(vec![
7555 Some(b"valid".as_slice()),
7556 Some(&[0xff]),
7557 Some(b"utf8".as_slice()),
7558 None,
7559 ]);
7560
7561 let strict_options = CastOptions {
7562 safe: false,
7563 ..Default::default()
7564 };
7565
7566 assert!(
7567 cast_with_options(&binary_view_array, &DataType::Utf8View, &strict_options).is_err()
7568 );
7569
7570 let safe_options = CastOptions {
7571 safe: true,
7572 ..Default::default()
7573 };
7574
7575 let string_view_array =
7576 cast_with_options(&binary_view_array, &DataType::Utf8View, &safe_options).unwrap();
7577 assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7578
7579 let values: Vec<_> = string_view_array.as_string_view().iter().collect();
7580
7581 assert_eq!(values, vec![Some("valid"), None, Some("utf8"), None]);
7582 }
7583
7584 #[test]
7585 fn test_string_to_view() {
7586 _test_string_to_view::<i32>();
7587 _test_string_to_view::<i64>();
7588 }
7589
7590 fn _test_string_to_view<O>()
7591 where
7592 O: OffsetSizeTrait,
7593 {
7594 let string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
7595
7596 assert!(can_cast_types(
7597 string_array.data_type(),
7598 &DataType::Utf8View
7599 ));
7600
7601 assert!(can_cast_types(
7602 string_array.data_type(),
7603 &DataType::BinaryView
7604 ));
7605
7606 let string_view_array = cast(&string_array, &DataType::Utf8View).unwrap();
7607 assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7608
7609 let binary_view_array = cast(&string_array, &DataType::BinaryView).unwrap();
7610 assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
7611
7612 let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7613 assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
7614
7615 let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7616 assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
7617 }
7618
7619 #[test]
7620 fn test_binary_to_view() {
7621 _test_binary_to_view::<i32>();
7622 _test_binary_to_view::<i64>();
7623 }
7624
7625 fn _test_binary_to_view<O>()
7626 where
7627 O: OffsetSizeTrait,
7628 {
7629 let binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
7630
7631 assert!(can_cast_types(
7632 binary_array.data_type(),
7633 &DataType::Utf8View
7634 ));
7635
7636 assert!(can_cast_types(
7637 binary_array.data_type(),
7638 &DataType::BinaryView
7639 ));
7640
7641 let string_view_array = cast(&binary_array, &DataType::Utf8View).unwrap();
7642 assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7643
7644 let binary_view_array = cast(&binary_array, &DataType::BinaryView).unwrap();
7645 assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
7646
7647 let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7648 assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
7649
7650 let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7651 assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
7652 }
7653
7654 #[test]
7655 fn test_dict_to_view() {
7656 let values = StringArray::from_iter(VIEW_TEST_DATA);
7657 let keys = Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
7658 let string_dict_array =
7659 DictionaryArray::<Int8Type>::try_new(keys, Arc::new(values)).unwrap();
7660 let typed_dict = string_dict_array.downcast_dict::<StringArray>().unwrap();
7661
7662 let string_view_array = {
7663 let mut builder = StringViewBuilder::new().with_fixed_block_size(8); for v in typed_dict {
7665 builder.append_option(v);
7666 }
7667 builder.finish()
7668 };
7669 let expected_string_array_type = string_view_array.data_type();
7670 let casted_string_array = cast(&string_dict_array, expected_string_array_type).unwrap();
7671 assert_eq!(casted_string_array.data_type(), expected_string_array_type);
7672 assert_eq!(casted_string_array.as_ref(), &string_view_array);
7673
7674 let binary_buffer = cast(&typed_dict.values(), &DataType::Binary).unwrap();
7675 let binary_dict_array =
7676 DictionaryArray::<Int8Type>::new(typed_dict.keys().clone(), binary_buffer);
7677 let typed_binary_dict = binary_dict_array.downcast_dict::<BinaryArray>().unwrap();
7678
7679 let binary_view_array = {
7680 let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); for v in typed_binary_dict {
7682 builder.append_option(v);
7683 }
7684 builder.finish()
7685 };
7686 let expected_binary_array_type = binary_view_array.data_type();
7687 let casted_binary_array = cast(&binary_dict_array, expected_binary_array_type).unwrap();
7688 assert_eq!(casted_binary_array.data_type(), expected_binary_array_type);
7689 assert_eq!(casted_binary_array.as_ref(), &binary_view_array);
7690 }
7691
7692 #[test]
7693 fn test_dict_to_view_null_dictionary_value_is_null() {
7694 let keys = Int32Array::from_iter([Some(0), Some(1), Some(2), None, Some(1)]);
7696
7697 let values = StringArray::from(vec![Some("aa"), None, Some("a value over twelve bytes")]);
7698 let dict = DictionaryArray::<Int32Type>::try_new(keys.clone(), Arc::new(values)).unwrap();
7699 let casted = cast(&dict, &DataType::Utf8View).unwrap();
7700 assert_eq!(
7701 casted.as_string_view().iter().collect::<Vec<_>>(),
7702 vec![
7703 Some("aa"),
7704 None,
7705 Some("a value over twelve bytes"),
7706 None,
7707 None
7708 ]
7709 );
7710 let reference = cast(&dict, &DataType::Utf8).unwrap();
7712 assert_eq!(
7713 casted.as_string_view().iter().collect::<Vec<_>>(),
7714 reference.as_string::<i32>().iter().collect::<Vec<_>>()
7715 );
7716
7717 let values = BinaryArray::from_opt_vec(vec![
7718 Some(b"aa".as_slice()),
7719 None,
7720 Some(b"a value over twelve bytes"),
7721 ]);
7722 let dict = DictionaryArray::<Int32Type>::try_new(keys, Arc::new(values)).unwrap();
7723 let casted = cast(&dict, &DataType::BinaryView).unwrap();
7724 assert_eq!(
7725 casted.as_binary_view().iter().collect::<Vec<_>>(),
7726 vec![
7727 Some(b"aa".as_slice()),
7728 None,
7729 Some(b"a value over twelve bytes"),
7730 None,
7731 None
7732 ]
7733 );
7734 }
7735
7736 #[test]
7737 fn test_view_to_dict() {
7738 let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7739 let string_dict_array: DictionaryArray<Int8Type> = VIEW_TEST_DATA.into_iter().collect();
7740 let casted_type = string_dict_array.data_type();
7741 let casted_dict_array = cast(&string_view_array, casted_type).unwrap();
7742 assert_eq!(casted_dict_array.data_type(), casted_type);
7743 assert_eq!(casted_dict_array.as_ref(), &string_dict_array);
7744
7745 let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7746 let binary_dict_array = string_dict_array.downcast_dict::<StringArray>().unwrap();
7747 let binary_buffer = cast(&binary_dict_array.values(), &DataType::Binary).unwrap();
7748 let binary_dict_array =
7749 DictionaryArray::<Int8Type>::new(binary_dict_array.keys().clone(), binary_buffer);
7750 let casted_type = binary_dict_array.data_type();
7751 let casted_binary_array = cast(&binary_view_array, casted_type).unwrap();
7752 assert_eq!(casted_binary_array.data_type(), casted_type);
7753 assert_eq!(casted_binary_array.as_ref(), &binary_dict_array);
7754 }
7755
7756 #[test]
7757 fn test_view_to_string() {
7758 _test_view_to_string::<i32>();
7759 _test_view_to_string::<i64>();
7760 }
7761
7762 fn _test_view_to_string<O>()
7763 where
7764 O: OffsetSizeTrait,
7765 {
7766 let string_view_array = {
7767 let mut builder = StringViewBuilder::new().with_fixed_block_size(8); for s in &VIEW_TEST_DATA {
7769 builder.append_option(*s);
7770 }
7771 builder.finish()
7772 };
7773
7774 let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7775
7776 let expected_string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
7777 let expected_type = expected_string_array.data_type();
7778
7779 assert!(can_cast_types(string_view_array.data_type(), expected_type));
7780 assert!(can_cast_types(binary_view_array.data_type(), expected_type));
7781
7782 let string_view_casted_array = cast(&string_view_array, expected_type).unwrap();
7783 assert_eq!(string_view_casted_array.data_type(), expected_type);
7784 assert_eq!(string_view_casted_array.as_ref(), &expected_string_array);
7785
7786 let binary_view_casted_array = cast(&binary_view_array, expected_type).unwrap();
7787 assert_eq!(binary_view_casted_array.data_type(), expected_type);
7788 assert_eq!(binary_view_casted_array.as_ref(), &expected_string_array);
7789 }
7790
7791 #[test]
7792 fn test_view_to_binary() {
7793 _test_view_to_binary::<i32>();
7794 _test_view_to_binary::<i64>();
7795 }
7796
7797 fn _test_view_to_binary<O>()
7798 where
7799 O: OffsetSizeTrait,
7800 {
7801 let view_array = {
7802 let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); for s in &VIEW_TEST_DATA {
7804 builder.append_option(*s);
7805 }
7806 builder.finish()
7807 };
7808
7809 let expected_binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
7810 let expected_type = expected_binary_array.data_type();
7811
7812 assert!(can_cast_types(view_array.data_type(), expected_type));
7813
7814 let binary_array = cast(&view_array, expected_type).unwrap();
7815 assert_eq!(binary_array.data_type(), expected_type);
7816
7817 assert_eq!(binary_array.as_ref(), &expected_binary_array);
7818 }
7819
7820 #[test]
7821 #[cfg_attr(miri, ignore)] fn test_cast_from_f64() {
7823 let f64_values: Vec<f64> = vec![
7824 i64::MIN as f64,
7825 i32::MIN as f64,
7826 i16::MIN as f64,
7827 i8::MIN as f64,
7828 0_f64,
7829 u8::MAX as f64,
7830 u16::MAX as f64,
7831 u32::MAX as f64,
7832 u64::MAX as f64,
7833 ];
7834 let f64_array: ArrayRef = Arc::new(Float64Array::from(f64_values));
7835
7836 let f64_expected = vec![
7837 -9223372036854776000.0,
7838 -2147483648.0,
7839 -32768.0,
7840 -128.0,
7841 0.0,
7842 255.0,
7843 65535.0,
7844 4294967295.0,
7845 18446744073709552000.0,
7846 ];
7847 assert_eq!(
7848 f64_expected,
7849 get_cast_values::<Float64Type>(&f64_array, &DataType::Float64)
7850 .iter()
7851 .map(|i| i.parse::<f64>().unwrap())
7852 .collect::<Vec<f64>>()
7853 );
7854
7855 let f32_expected = vec![
7856 -9223372000000000000.0,
7857 -2147483600.0,
7858 -32768.0,
7859 -128.0,
7860 0.0,
7861 255.0,
7862 65535.0,
7863 4294967300.0,
7864 18446744000000000000.0,
7865 ];
7866 assert_eq!(
7867 f32_expected,
7868 get_cast_values::<Float32Type>(&f64_array, &DataType::Float32)
7869 .iter()
7870 .map(|i| i.parse::<f32>().unwrap())
7871 .collect::<Vec<f32>>()
7872 );
7873
7874 let f16_expected = vec![
7875 f16::from_f64(-9223372000000000000.0),
7876 f16::from_f64(-2147483600.0),
7877 f16::from_f64(-32768.0),
7878 f16::from_f64(-128.0),
7879 f16::from_f64(0.0),
7880 f16::from_f64(255.0),
7881 f16::from_f64(65535.0),
7882 f16::from_f64(4294967300.0),
7883 f16::from_f64(18446744000000000000.0),
7884 ];
7885 assert_eq!(
7886 f16_expected,
7887 get_cast_values::<Float16Type>(&f64_array, &DataType::Float16)
7888 .iter()
7889 .map(|i| i.parse::<f16>().unwrap())
7890 .collect::<Vec<f16>>()
7891 );
7892
7893 let i64_expected = vec![
7894 "-9223372036854775808",
7895 "-2147483648",
7896 "-32768",
7897 "-128",
7898 "0",
7899 "255",
7900 "65535",
7901 "4294967295",
7902 "null",
7903 ];
7904 assert_eq!(
7905 i64_expected,
7906 get_cast_values::<Int64Type>(&f64_array, &DataType::Int64)
7907 );
7908
7909 let i32_expected = vec![
7910 "null",
7911 "-2147483648",
7912 "-32768",
7913 "-128",
7914 "0",
7915 "255",
7916 "65535",
7917 "null",
7918 "null",
7919 ];
7920 assert_eq!(
7921 i32_expected,
7922 get_cast_values::<Int32Type>(&f64_array, &DataType::Int32)
7923 );
7924
7925 let i16_expected = vec![
7926 "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
7927 ];
7928 assert_eq!(
7929 i16_expected,
7930 get_cast_values::<Int16Type>(&f64_array, &DataType::Int16)
7931 );
7932
7933 let i8_expected = vec![
7934 "null", "null", "null", "-128", "0", "null", "null", "null", "null",
7935 ];
7936 assert_eq!(
7937 i8_expected,
7938 get_cast_values::<Int8Type>(&f64_array, &DataType::Int8)
7939 );
7940
7941 let u64_expected = vec![
7942 "null",
7943 "null",
7944 "null",
7945 "null",
7946 "0",
7947 "255",
7948 "65535",
7949 "4294967295",
7950 "null",
7951 ];
7952 assert_eq!(
7953 u64_expected,
7954 get_cast_values::<UInt64Type>(&f64_array, &DataType::UInt64)
7955 );
7956
7957 let u32_expected = vec![
7958 "null",
7959 "null",
7960 "null",
7961 "null",
7962 "0",
7963 "255",
7964 "65535",
7965 "4294967295",
7966 "null",
7967 ];
7968 assert_eq!(
7969 u32_expected,
7970 get_cast_values::<UInt32Type>(&f64_array, &DataType::UInt32)
7971 );
7972
7973 let u16_expected = vec![
7974 "null", "null", "null", "null", "0", "255", "65535", "null", "null",
7975 ];
7976 assert_eq!(
7977 u16_expected,
7978 get_cast_values::<UInt16Type>(&f64_array, &DataType::UInt16)
7979 );
7980
7981 let u8_expected = vec![
7982 "null", "null", "null", "null", "0", "255", "null", "null", "null",
7983 ];
7984 assert_eq!(
7985 u8_expected,
7986 get_cast_values::<UInt8Type>(&f64_array, &DataType::UInt8)
7987 );
7988 }
7989
7990 #[test]
7991 #[cfg_attr(miri, ignore)] fn test_cast_from_f32() {
7993 let f32_values: Vec<f32> = vec![
7994 i32::MIN as f32,
7995 i32::MIN as f32,
7996 i16::MIN as f32,
7997 i8::MIN as f32,
7998 0_f32,
7999 u8::MAX as f32,
8000 u16::MAX as f32,
8001 u32::MAX as f32,
8002 u32::MAX as f32,
8003 ];
8004 let f32_array: ArrayRef = Arc::new(Float32Array::from(f32_values));
8005
8006 let f64_expected = vec![
8007 "-2147483648.0",
8008 "-2147483648.0",
8009 "-32768.0",
8010 "-128.0",
8011 "0.0",
8012 "255.0",
8013 "65535.0",
8014 "4294967296.0",
8015 "4294967296.0",
8016 ];
8017 assert_eq!(
8018 f64_expected,
8019 get_cast_values::<Float64Type>(&f32_array, &DataType::Float64)
8020 );
8021
8022 let f32_expected = vec![
8023 "-2147483600.0",
8024 "-2147483600.0",
8025 "-32768.0",
8026 "-128.0",
8027 "0.0",
8028 "255.0",
8029 "65535.0",
8030 "4294967300.0",
8031 "4294967300.0",
8032 ];
8033 assert_eq!(
8034 f32_expected,
8035 get_cast_values::<Float32Type>(&f32_array, &DataType::Float32)
8036 );
8037
8038 let f16_expected = vec![
8039 "-inf", "-inf", "-32768.0", "-128.0", "0.0", "255.0", "inf", "inf", "inf",
8040 ];
8041 assert_eq!(
8042 f16_expected,
8043 get_cast_values::<Float16Type>(&f32_array, &DataType::Float16)
8044 );
8045
8046 let i64_expected = vec![
8047 "-2147483648",
8048 "-2147483648",
8049 "-32768",
8050 "-128",
8051 "0",
8052 "255",
8053 "65535",
8054 "4294967296",
8055 "4294967296",
8056 ];
8057 assert_eq!(
8058 i64_expected,
8059 get_cast_values::<Int64Type>(&f32_array, &DataType::Int64)
8060 );
8061
8062 let i32_expected = vec![
8063 "-2147483648",
8064 "-2147483648",
8065 "-32768",
8066 "-128",
8067 "0",
8068 "255",
8069 "65535",
8070 "null",
8071 "null",
8072 ];
8073 assert_eq!(
8074 i32_expected,
8075 get_cast_values::<Int32Type>(&f32_array, &DataType::Int32)
8076 );
8077
8078 let i16_expected = vec![
8079 "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
8080 ];
8081 assert_eq!(
8082 i16_expected,
8083 get_cast_values::<Int16Type>(&f32_array, &DataType::Int16)
8084 );
8085
8086 let i8_expected = vec![
8087 "null", "null", "null", "-128", "0", "null", "null", "null", "null",
8088 ];
8089 assert_eq!(
8090 i8_expected,
8091 get_cast_values::<Int8Type>(&f32_array, &DataType::Int8)
8092 );
8093
8094 let u64_expected = vec![
8095 "null",
8096 "null",
8097 "null",
8098 "null",
8099 "0",
8100 "255",
8101 "65535",
8102 "4294967296",
8103 "4294967296",
8104 ];
8105 assert_eq!(
8106 u64_expected,
8107 get_cast_values::<UInt64Type>(&f32_array, &DataType::UInt64)
8108 );
8109
8110 let u32_expected = vec![
8111 "null", "null", "null", "null", "0", "255", "65535", "null", "null",
8112 ];
8113 assert_eq!(
8114 u32_expected,
8115 get_cast_values::<UInt32Type>(&f32_array, &DataType::UInt32)
8116 );
8117
8118 let u16_expected = vec![
8119 "null", "null", "null", "null", "0", "255", "65535", "null", "null",
8120 ];
8121 assert_eq!(
8122 u16_expected,
8123 get_cast_values::<UInt16Type>(&f32_array, &DataType::UInt16)
8124 );
8125
8126 let u8_expected = vec![
8127 "null", "null", "null", "null", "0", "255", "null", "null", "null",
8128 ];
8129 assert_eq!(
8130 u8_expected,
8131 get_cast_values::<UInt8Type>(&f32_array, &DataType::UInt8)
8132 );
8133 }
8134
8135 #[test]
8136 #[cfg_attr(miri, ignore)] fn test_cast_from_uint64() {
8138 let u64_values: Vec<u64> = vec![
8139 0,
8140 u8::MAX as u64,
8141 u16::MAX as u64,
8142 u32::MAX as u64,
8143 u64::MAX,
8144 ];
8145 let u64_array: ArrayRef = Arc::new(UInt64Array::from(u64_values));
8146
8147 let f64_expected = vec![0.0, 255.0, 65535.0, 4294967295.0, 18446744073709552000.0];
8148 assert_eq!(
8149 f64_expected,
8150 get_cast_values::<Float64Type>(&u64_array, &DataType::Float64)
8151 .iter()
8152 .map(|i| i.parse::<f64>().unwrap())
8153 .collect::<Vec<f64>>()
8154 );
8155
8156 let f32_expected = vec![0.0, 255.0, 65535.0, 4294967300.0, 18446744000000000000.0];
8157 assert_eq!(
8158 f32_expected,
8159 get_cast_values::<Float32Type>(&u64_array, &DataType::Float32)
8160 .iter()
8161 .map(|i| i.parse::<f32>().unwrap())
8162 .collect::<Vec<f32>>()
8163 );
8164
8165 let f16_expected = vec![
8166 f16::from_f64(0.0),
8167 f16::from_f64(255.0),
8168 f16::from_f64(65535.0),
8169 f16::from_f64(4294967300.0),
8170 f16::from_f64(18446744000000000000.0),
8171 ];
8172 assert_eq!(
8173 f16_expected,
8174 get_cast_values::<Float16Type>(&u64_array, &DataType::Float16)
8175 .iter()
8176 .map(|i| i.parse::<f16>().unwrap())
8177 .collect::<Vec<f16>>()
8178 );
8179
8180 let i64_expected = vec!["0", "255", "65535", "4294967295", "null"];
8181 assert_eq!(
8182 i64_expected,
8183 get_cast_values::<Int64Type>(&u64_array, &DataType::Int64)
8184 );
8185
8186 let i32_expected = vec!["0", "255", "65535", "null", "null"];
8187 assert_eq!(
8188 i32_expected,
8189 get_cast_values::<Int32Type>(&u64_array, &DataType::Int32)
8190 );
8191
8192 let i16_expected = vec!["0", "255", "null", "null", "null"];
8193 assert_eq!(
8194 i16_expected,
8195 get_cast_values::<Int16Type>(&u64_array, &DataType::Int16)
8196 );
8197
8198 let i8_expected = vec!["0", "null", "null", "null", "null"];
8199 assert_eq!(
8200 i8_expected,
8201 get_cast_values::<Int8Type>(&u64_array, &DataType::Int8)
8202 );
8203
8204 let u64_expected = vec!["0", "255", "65535", "4294967295", "18446744073709551615"];
8205 assert_eq!(
8206 u64_expected,
8207 get_cast_values::<UInt64Type>(&u64_array, &DataType::UInt64)
8208 );
8209
8210 let u32_expected = vec!["0", "255", "65535", "4294967295", "null"];
8211 assert_eq!(
8212 u32_expected,
8213 get_cast_values::<UInt32Type>(&u64_array, &DataType::UInt32)
8214 );
8215
8216 let u16_expected = vec!["0", "255", "65535", "null", "null"];
8217 assert_eq!(
8218 u16_expected,
8219 get_cast_values::<UInt16Type>(&u64_array, &DataType::UInt16)
8220 );
8221
8222 let u8_expected = vec!["0", "255", "null", "null", "null"];
8223 assert_eq!(
8224 u8_expected,
8225 get_cast_values::<UInt8Type>(&u64_array, &DataType::UInt8)
8226 );
8227 }
8228
8229 #[test]
8230 #[cfg_attr(miri, ignore)] fn test_cast_from_uint32() {
8232 let u32_values: Vec<u32> = vec![0, u8::MAX as u32, u16::MAX as u32, u32::MAX];
8233 let u32_array: ArrayRef = Arc::new(UInt32Array::from(u32_values));
8234
8235 let f64_expected = vec!["0.0", "255.0", "65535.0", "4294967295.0"];
8236 assert_eq!(
8237 f64_expected,
8238 get_cast_values::<Float64Type>(&u32_array, &DataType::Float64)
8239 );
8240
8241 let f32_expected = vec!["0.0", "255.0", "65535.0", "4294967300.0"];
8242 assert_eq!(
8243 f32_expected,
8244 get_cast_values::<Float32Type>(&u32_array, &DataType::Float32)
8245 );
8246
8247 let f16_expected = vec!["0.0", "255.0", "inf", "inf"];
8248 assert_eq!(
8249 f16_expected,
8250 get_cast_values::<Float16Type>(&u32_array, &DataType::Float16)
8251 );
8252
8253 let i64_expected = vec!["0", "255", "65535", "4294967295"];
8254 assert_eq!(
8255 i64_expected,
8256 get_cast_values::<Int64Type>(&u32_array, &DataType::Int64)
8257 );
8258
8259 let i32_expected = vec!["0", "255", "65535", "null"];
8260 assert_eq!(
8261 i32_expected,
8262 get_cast_values::<Int32Type>(&u32_array, &DataType::Int32)
8263 );
8264
8265 let i16_expected = vec!["0", "255", "null", "null"];
8266 assert_eq!(
8267 i16_expected,
8268 get_cast_values::<Int16Type>(&u32_array, &DataType::Int16)
8269 );
8270
8271 let i8_expected = vec!["0", "null", "null", "null"];
8272 assert_eq!(
8273 i8_expected,
8274 get_cast_values::<Int8Type>(&u32_array, &DataType::Int8)
8275 );
8276
8277 let u64_expected = vec!["0", "255", "65535", "4294967295"];
8278 assert_eq!(
8279 u64_expected,
8280 get_cast_values::<UInt64Type>(&u32_array, &DataType::UInt64)
8281 );
8282
8283 let u32_expected = vec!["0", "255", "65535", "4294967295"];
8284 assert_eq!(
8285 u32_expected,
8286 get_cast_values::<UInt32Type>(&u32_array, &DataType::UInt32)
8287 );
8288
8289 let u16_expected = vec!["0", "255", "65535", "null"];
8290 assert_eq!(
8291 u16_expected,
8292 get_cast_values::<UInt16Type>(&u32_array, &DataType::UInt16)
8293 );
8294
8295 let u8_expected = vec!["0", "255", "null", "null"];
8296 assert_eq!(
8297 u8_expected,
8298 get_cast_values::<UInt8Type>(&u32_array, &DataType::UInt8)
8299 );
8300 }
8301
8302 #[test]
8303 #[cfg_attr(miri, ignore)] fn test_cast_from_uint16() {
8305 let u16_values: Vec<u16> = vec![0, u8::MAX as u16, u16::MAX];
8306 let u16_array: ArrayRef = Arc::new(UInt16Array::from(u16_values));
8307
8308 let f64_expected = vec!["0.0", "255.0", "65535.0"];
8309 assert_eq!(
8310 f64_expected,
8311 get_cast_values::<Float64Type>(&u16_array, &DataType::Float64)
8312 );
8313
8314 let f32_expected = vec!["0.0", "255.0", "65535.0"];
8315 assert_eq!(
8316 f32_expected,
8317 get_cast_values::<Float32Type>(&u16_array, &DataType::Float32)
8318 );
8319
8320 let f16_expected = vec!["0.0", "255.0", "inf"];
8321 assert_eq!(
8322 f16_expected,
8323 get_cast_values::<Float16Type>(&u16_array, &DataType::Float16)
8324 );
8325
8326 let i64_expected = vec!["0", "255", "65535"];
8327 assert_eq!(
8328 i64_expected,
8329 get_cast_values::<Int64Type>(&u16_array, &DataType::Int64)
8330 );
8331
8332 let i32_expected = vec!["0", "255", "65535"];
8333 assert_eq!(
8334 i32_expected,
8335 get_cast_values::<Int32Type>(&u16_array, &DataType::Int32)
8336 );
8337
8338 let i16_expected = vec!["0", "255", "null"];
8339 assert_eq!(
8340 i16_expected,
8341 get_cast_values::<Int16Type>(&u16_array, &DataType::Int16)
8342 );
8343
8344 let i8_expected = vec!["0", "null", "null"];
8345 assert_eq!(
8346 i8_expected,
8347 get_cast_values::<Int8Type>(&u16_array, &DataType::Int8)
8348 );
8349
8350 let u64_expected = vec!["0", "255", "65535"];
8351 assert_eq!(
8352 u64_expected,
8353 get_cast_values::<UInt64Type>(&u16_array, &DataType::UInt64)
8354 );
8355
8356 let u32_expected = vec!["0", "255", "65535"];
8357 assert_eq!(
8358 u32_expected,
8359 get_cast_values::<UInt32Type>(&u16_array, &DataType::UInt32)
8360 );
8361
8362 let u16_expected = vec!["0", "255", "65535"];
8363 assert_eq!(
8364 u16_expected,
8365 get_cast_values::<UInt16Type>(&u16_array, &DataType::UInt16)
8366 );
8367
8368 let u8_expected = vec!["0", "255", "null"];
8369 assert_eq!(
8370 u8_expected,
8371 get_cast_values::<UInt8Type>(&u16_array, &DataType::UInt8)
8372 );
8373 }
8374
8375 #[test]
8376 #[cfg_attr(miri, ignore)] fn test_cast_from_uint8() {
8378 let u8_values: Vec<u8> = vec![0, u8::MAX];
8379 let u8_array: ArrayRef = Arc::new(UInt8Array::from(u8_values));
8380
8381 let f64_expected = vec!["0.0", "255.0"];
8382 assert_eq!(
8383 f64_expected,
8384 get_cast_values::<Float64Type>(&u8_array, &DataType::Float64)
8385 );
8386
8387 let f32_expected = vec!["0.0", "255.0"];
8388 assert_eq!(
8389 f32_expected,
8390 get_cast_values::<Float32Type>(&u8_array, &DataType::Float32)
8391 );
8392
8393 let f16_expected = vec!["0.0", "255.0"];
8394 assert_eq!(
8395 f16_expected,
8396 get_cast_values::<Float16Type>(&u8_array, &DataType::Float16)
8397 );
8398
8399 let i64_expected = vec!["0", "255"];
8400 assert_eq!(
8401 i64_expected,
8402 get_cast_values::<Int64Type>(&u8_array, &DataType::Int64)
8403 );
8404
8405 let i32_expected = vec!["0", "255"];
8406 assert_eq!(
8407 i32_expected,
8408 get_cast_values::<Int32Type>(&u8_array, &DataType::Int32)
8409 );
8410
8411 let i16_expected = vec!["0", "255"];
8412 assert_eq!(
8413 i16_expected,
8414 get_cast_values::<Int16Type>(&u8_array, &DataType::Int16)
8415 );
8416
8417 let i8_expected = vec!["0", "null"];
8418 assert_eq!(
8419 i8_expected,
8420 get_cast_values::<Int8Type>(&u8_array, &DataType::Int8)
8421 );
8422
8423 let u64_expected = vec!["0", "255"];
8424 assert_eq!(
8425 u64_expected,
8426 get_cast_values::<UInt64Type>(&u8_array, &DataType::UInt64)
8427 );
8428
8429 let u32_expected = vec!["0", "255"];
8430 assert_eq!(
8431 u32_expected,
8432 get_cast_values::<UInt32Type>(&u8_array, &DataType::UInt32)
8433 );
8434
8435 let u16_expected = vec!["0", "255"];
8436 assert_eq!(
8437 u16_expected,
8438 get_cast_values::<UInt16Type>(&u8_array, &DataType::UInt16)
8439 );
8440
8441 let u8_expected = vec!["0", "255"];
8442 assert_eq!(
8443 u8_expected,
8444 get_cast_values::<UInt8Type>(&u8_array, &DataType::UInt8)
8445 );
8446 }
8447
8448 #[test]
8449 #[cfg_attr(miri, ignore)] fn test_cast_from_int64() {
8451 let i64_values: Vec<i64> = vec![
8452 i64::MIN,
8453 i32::MIN as i64,
8454 i16::MIN as i64,
8455 i8::MIN as i64,
8456 0,
8457 i8::MAX as i64,
8458 i16::MAX as i64,
8459 i32::MAX as i64,
8460 i64::MAX,
8461 ];
8462 let i64_array: ArrayRef = Arc::new(Int64Array::from(i64_values));
8463
8464 let f64_expected = vec![
8465 -9223372036854776000.0,
8466 -2147483648.0,
8467 -32768.0,
8468 -128.0,
8469 0.0,
8470 127.0,
8471 32767.0,
8472 2147483647.0,
8473 9223372036854776000.0,
8474 ];
8475 assert_eq!(
8476 f64_expected,
8477 get_cast_values::<Float64Type>(&i64_array, &DataType::Float64)
8478 .iter()
8479 .map(|i| i.parse::<f64>().unwrap())
8480 .collect::<Vec<f64>>()
8481 );
8482
8483 let f32_expected = vec![
8484 -9223372000000000000.0,
8485 -2147483600.0,
8486 -32768.0,
8487 -128.0,
8488 0.0,
8489 127.0,
8490 32767.0,
8491 2147483600.0,
8492 9223372000000000000.0,
8493 ];
8494 assert_eq!(
8495 f32_expected,
8496 get_cast_values::<Float32Type>(&i64_array, &DataType::Float32)
8497 .iter()
8498 .map(|i| i.parse::<f32>().unwrap())
8499 .collect::<Vec<f32>>()
8500 );
8501
8502 let f16_expected = vec![
8503 f16::from_f64(-9223372000000000000.0),
8504 f16::from_f64(-2147483600.0),
8505 f16::from_f64(-32768.0),
8506 f16::from_f64(-128.0),
8507 f16::from_f64(0.0),
8508 f16::from_f64(127.0),
8509 f16::from_f64(32767.0),
8510 f16::from_f64(2147483600.0),
8511 f16::from_f64(9223372000000000000.0),
8512 ];
8513 assert_eq!(
8514 f16_expected,
8515 get_cast_values::<Float16Type>(&i64_array, &DataType::Float16)
8516 .iter()
8517 .map(|i| i.parse::<f16>().unwrap())
8518 .collect::<Vec<f16>>()
8519 );
8520
8521 let i64_expected = vec![
8522 "-9223372036854775808",
8523 "-2147483648",
8524 "-32768",
8525 "-128",
8526 "0",
8527 "127",
8528 "32767",
8529 "2147483647",
8530 "9223372036854775807",
8531 ];
8532 assert_eq!(
8533 i64_expected,
8534 get_cast_values::<Int64Type>(&i64_array, &DataType::Int64)
8535 );
8536
8537 let i32_expected = vec![
8538 "null",
8539 "-2147483648",
8540 "-32768",
8541 "-128",
8542 "0",
8543 "127",
8544 "32767",
8545 "2147483647",
8546 "null",
8547 ];
8548 assert_eq!(
8549 i32_expected,
8550 get_cast_values::<Int32Type>(&i64_array, &DataType::Int32)
8551 );
8552
8553 assert_eq!(
8554 i32_expected,
8555 get_cast_values::<Date32Type>(&i64_array, &DataType::Date32)
8556 );
8557
8558 let i16_expected = vec![
8559 "null", "null", "-32768", "-128", "0", "127", "32767", "null", "null",
8560 ];
8561 assert_eq!(
8562 i16_expected,
8563 get_cast_values::<Int16Type>(&i64_array, &DataType::Int16)
8564 );
8565
8566 let i8_expected = vec![
8567 "null", "null", "null", "-128", "0", "127", "null", "null", "null",
8568 ];
8569 assert_eq!(
8570 i8_expected,
8571 get_cast_values::<Int8Type>(&i64_array, &DataType::Int8)
8572 );
8573
8574 let u64_expected = vec![
8575 "null",
8576 "null",
8577 "null",
8578 "null",
8579 "0",
8580 "127",
8581 "32767",
8582 "2147483647",
8583 "9223372036854775807",
8584 ];
8585 assert_eq!(
8586 u64_expected,
8587 get_cast_values::<UInt64Type>(&i64_array, &DataType::UInt64)
8588 );
8589
8590 let u32_expected = vec![
8591 "null",
8592 "null",
8593 "null",
8594 "null",
8595 "0",
8596 "127",
8597 "32767",
8598 "2147483647",
8599 "null",
8600 ];
8601 assert_eq!(
8602 u32_expected,
8603 get_cast_values::<UInt32Type>(&i64_array, &DataType::UInt32)
8604 );
8605
8606 let u16_expected = vec![
8607 "null", "null", "null", "null", "0", "127", "32767", "null", "null",
8608 ];
8609 assert_eq!(
8610 u16_expected,
8611 get_cast_values::<UInt16Type>(&i64_array, &DataType::UInt16)
8612 );
8613
8614 let u8_expected = vec![
8615 "null", "null", "null", "null", "0", "127", "null", "null", "null",
8616 ];
8617 assert_eq!(
8618 u8_expected,
8619 get_cast_values::<UInt8Type>(&i64_array, &DataType::UInt8)
8620 );
8621 }
8622
8623 #[test]
8624 #[cfg_attr(miri, ignore)] fn test_cast_from_int32() {
8626 let i32_values: Vec<i32> = vec![
8627 i32::MIN,
8628 i16::MIN as i32,
8629 i8::MIN as i32,
8630 0,
8631 i8::MAX as i32,
8632 i16::MAX as i32,
8633 i32::MAX,
8634 ];
8635 let i32_array: ArrayRef = Arc::new(Int32Array::from(i32_values));
8636
8637 let f64_expected = vec![
8638 "-2147483648.0",
8639 "-32768.0",
8640 "-128.0",
8641 "0.0",
8642 "127.0",
8643 "32767.0",
8644 "2147483647.0",
8645 ];
8646 assert_eq!(
8647 f64_expected,
8648 get_cast_values::<Float64Type>(&i32_array, &DataType::Float64)
8649 );
8650
8651 let f32_expected = vec![
8652 "-2147483600.0",
8653 "-32768.0",
8654 "-128.0",
8655 "0.0",
8656 "127.0",
8657 "32767.0",
8658 "2147483600.0",
8659 ];
8660 assert_eq!(
8661 f32_expected,
8662 get_cast_values::<Float32Type>(&i32_array, &DataType::Float32)
8663 );
8664
8665 let f16_expected = vec![
8666 f16::from_f64(-2147483600.0),
8667 f16::from_f64(-32768.0),
8668 f16::from_f64(-128.0),
8669 f16::from_f64(0.0),
8670 f16::from_f64(127.0),
8671 f16::from_f64(32767.0),
8672 f16::from_f64(2147483600.0),
8673 ];
8674 assert_eq!(
8675 f16_expected,
8676 get_cast_values::<Float16Type>(&i32_array, &DataType::Float16)
8677 .iter()
8678 .map(|i| i.parse::<f16>().unwrap())
8679 .collect::<Vec<f16>>()
8680 );
8681
8682 let i16_expected = vec!["null", "-32768", "-128", "0", "127", "32767", "null"];
8683 assert_eq!(
8684 i16_expected,
8685 get_cast_values::<Int16Type>(&i32_array, &DataType::Int16)
8686 );
8687
8688 let i8_expected = vec!["null", "null", "-128", "0", "127", "null", "null"];
8689 assert_eq!(
8690 i8_expected,
8691 get_cast_values::<Int8Type>(&i32_array, &DataType::Int8)
8692 );
8693
8694 let u64_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
8695 assert_eq!(
8696 u64_expected,
8697 get_cast_values::<UInt64Type>(&i32_array, &DataType::UInt64)
8698 );
8699
8700 let u32_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
8701 assert_eq!(
8702 u32_expected,
8703 get_cast_values::<UInt32Type>(&i32_array, &DataType::UInt32)
8704 );
8705
8706 let u16_expected = vec!["null", "null", "null", "0", "127", "32767", "null"];
8707 assert_eq!(
8708 u16_expected,
8709 get_cast_values::<UInt16Type>(&i32_array, &DataType::UInt16)
8710 );
8711
8712 let u8_expected = vec!["null", "null", "null", "0", "127", "null", "null"];
8713 assert_eq!(
8714 u8_expected,
8715 get_cast_values::<UInt8Type>(&i32_array, &DataType::UInt8)
8716 );
8717
8718 let i64_expected = vec![
8720 "-185542587187200000",
8721 "-2831155200000",
8722 "-11059200000",
8723 "0",
8724 "10972800000",
8725 "2831068800000",
8726 "185542587100800000",
8727 ];
8728 assert_eq!(
8729 i64_expected,
8730 get_cast_values::<Date64Type>(&i32_array, &DataType::Date64)
8731 );
8732 }
8733
8734 #[test]
8735 #[cfg_attr(miri, ignore)] fn test_cast_from_int16() {
8737 let i16_values: Vec<i16> = vec![i16::MIN, i8::MIN as i16, 0, i8::MAX as i16, i16::MAX];
8738 let i16_array: ArrayRef = Arc::new(Int16Array::from(i16_values));
8739
8740 let f64_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
8741 assert_eq!(
8742 f64_expected,
8743 get_cast_values::<Float64Type>(&i16_array, &DataType::Float64)
8744 );
8745
8746 let f32_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
8747 assert_eq!(
8748 f32_expected,
8749 get_cast_values::<Float32Type>(&i16_array, &DataType::Float32)
8750 );
8751
8752 let f16_expected = vec![
8753 f16::from_f64(-32768.0),
8754 f16::from_f64(-128.0),
8755 f16::from_f64(0.0),
8756 f16::from_f64(127.0),
8757 f16::from_f64(32767.0),
8758 ];
8759 assert_eq!(
8760 f16_expected,
8761 get_cast_values::<Float16Type>(&i16_array, &DataType::Float16)
8762 .iter()
8763 .map(|i| i.parse::<f16>().unwrap())
8764 .collect::<Vec<f16>>()
8765 );
8766
8767 let i64_expected = vec!["-32768", "-128", "0", "127", "32767"];
8768 assert_eq!(
8769 i64_expected,
8770 get_cast_values::<Int64Type>(&i16_array, &DataType::Int64)
8771 );
8772
8773 let i32_expected = vec!["-32768", "-128", "0", "127", "32767"];
8774 assert_eq!(
8775 i32_expected,
8776 get_cast_values::<Int32Type>(&i16_array, &DataType::Int32)
8777 );
8778
8779 let i16_expected = vec!["-32768", "-128", "0", "127", "32767"];
8780 assert_eq!(
8781 i16_expected,
8782 get_cast_values::<Int16Type>(&i16_array, &DataType::Int16)
8783 );
8784
8785 let i8_expected = vec!["null", "-128", "0", "127", "null"];
8786 assert_eq!(
8787 i8_expected,
8788 get_cast_values::<Int8Type>(&i16_array, &DataType::Int8)
8789 );
8790
8791 let u64_expected = vec!["null", "null", "0", "127", "32767"];
8792 assert_eq!(
8793 u64_expected,
8794 get_cast_values::<UInt64Type>(&i16_array, &DataType::UInt64)
8795 );
8796
8797 let u32_expected = vec!["null", "null", "0", "127", "32767"];
8798 assert_eq!(
8799 u32_expected,
8800 get_cast_values::<UInt32Type>(&i16_array, &DataType::UInt32)
8801 );
8802
8803 let u16_expected = vec!["null", "null", "0", "127", "32767"];
8804 assert_eq!(
8805 u16_expected,
8806 get_cast_values::<UInt16Type>(&i16_array, &DataType::UInt16)
8807 );
8808
8809 let u8_expected = vec!["null", "null", "0", "127", "null"];
8810 assert_eq!(
8811 u8_expected,
8812 get_cast_values::<UInt8Type>(&i16_array, &DataType::UInt8)
8813 );
8814 }
8815
8816 #[test]
8817 fn test_cast_from_date32() {
8818 let i32_values: Vec<i32> = vec![
8819 i32::MIN,
8820 i16::MIN as i32,
8821 i8::MIN as i32,
8822 0,
8823 i8::MAX as i32,
8824 i16::MAX as i32,
8825 i32::MAX,
8826 ];
8827 let date32_array: ArrayRef = Arc::new(Date32Array::from(i32_values));
8828
8829 let i64_expected = vec![
8830 "-2147483648",
8831 "-32768",
8832 "-128",
8833 "0",
8834 "127",
8835 "32767",
8836 "2147483647",
8837 ];
8838 assert_eq!(
8839 i64_expected,
8840 get_cast_values::<Int64Type>(&date32_array, &DataType::Int64)
8841 );
8842 }
8843
8844 #[test]
8845 #[cfg_attr(miri, ignore)] fn test_cast_from_int8() {
8847 let i8_values: Vec<i8> = vec![i8::MIN, 0, i8::MAX];
8848 let i8_array = Int8Array::from(i8_values);
8849
8850 let f64_expected = vec!["-128.0", "0.0", "127.0"];
8851 assert_eq!(
8852 f64_expected,
8853 get_cast_values::<Float64Type>(&i8_array, &DataType::Float64)
8854 );
8855
8856 let f32_expected = vec!["-128.0", "0.0", "127.0"];
8857 assert_eq!(
8858 f32_expected,
8859 get_cast_values::<Float32Type>(&i8_array, &DataType::Float32)
8860 );
8861
8862 let f16_expected = vec!["-128.0", "0.0", "127.0"];
8863 assert_eq!(
8864 f16_expected,
8865 get_cast_values::<Float16Type>(&i8_array, &DataType::Float16)
8866 );
8867
8868 let i64_expected = vec!["-128", "0", "127"];
8869 assert_eq!(
8870 i64_expected,
8871 get_cast_values::<Int64Type>(&i8_array, &DataType::Int64)
8872 );
8873
8874 let i32_expected = vec!["-128", "0", "127"];
8875 assert_eq!(
8876 i32_expected,
8877 get_cast_values::<Int32Type>(&i8_array, &DataType::Int32)
8878 );
8879
8880 let i16_expected = vec!["-128", "0", "127"];
8881 assert_eq!(
8882 i16_expected,
8883 get_cast_values::<Int16Type>(&i8_array, &DataType::Int16)
8884 );
8885
8886 let i8_expected = vec!["-128", "0", "127"];
8887 assert_eq!(
8888 i8_expected,
8889 get_cast_values::<Int8Type>(&i8_array, &DataType::Int8)
8890 );
8891
8892 let u64_expected = vec!["null", "0", "127"];
8893 assert_eq!(
8894 u64_expected,
8895 get_cast_values::<UInt64Type>(&i8_array, &DataType::UInt64)
8896 );
8897
8898 let u32_expected = vec!["null", "0", "127"];
8899 assert_eq!(
8900 u32_expected,
8901 get_cast_values::<UInt32Type>(&i8_array, &DataType::UInt32)
8902 );
8903
8904 let u16_expected = vec!["null", "0", "127"];
8905 assert_eq!(
8906 u16_expected,
8907 get_cast_values::<UInt16Type>(&i8_array, &DataType::UInt16)
8908 );
8909
8910 let u8_expected = vec!["null", "0", "127"];
8911 assert_eq!(
8912 u8_expected,
8913 get_cast_values::<UInt8Type>(&i8_array, &DataType::UInt8)
8914 );
8915 }
8916
8917 fn get_cast_values<T>(array: &dyn Array, dt: &DataType) -> Vec<String>
8919 where
8920 T: ArrowPrimitiveType,
8921 {
8922 let c = cast(array, dt).unwrap();
8923 let a = c.as_primitive::<T>();
8924 let mut v: Vec<String> = vec![];
8925 for i in 0..array.len() {
8926 if a.is_null(i) {
8927 v.push("null".to_string())
8928 } else {
8929 v.push(format!("{:?}", a.value(i)));
8930 }
8931 }
8932 v
8933 }
8934
8935 #[test]
8936 fn test_cast_utf8_dict() {
8937 let mut builder = StringDictionaryBuilder::<Int8Type>::new();
8939 builder.append("one").unwrap();
8940 builder.append_null();
8941 builder.append("three").unwrap();
8942 let array: ArrayRef = Arc::new(builder.finish());
8943
8944 let expected = vec!["one", "null", "three"];
8945
8946 let cast_type = Utf8;
8948 let cast_array = cast(&array, &cast_type).expect("cast to UTF-8 failed");
8949 assert_eq!(cast_array.data_type(), &cast_type);
8950 assert_eq!(array_to_strings(&cast_array), expected);
8951
8952 let cast_type = Dictionary(Box::new(Int16), Box::new(Utf8));
8955 let cast_array = cast(&array, &cast_type).expect("cast failed");
8956 assert_eq!(cast_array.data_type(), &cast_type);
8957 assert_eq!(array_to_strings(&cast_array), expected);
8958
8959 let cast_type = Dictionary(Box::new(Int32), Box::new(Utf8));
8960 let cast_array = cast(&array, &cast_type).expect("cast failed");
8961 assert_eq!(cast_array.data_type(), &cast_type);
8962 assert_eq!(array_to_strings(&cast_array), expected);
8963
8964 let cast_type = Dictionary(Box::new(Int64), Box::new(Utf8));
8965 let cast_array = cast(&array, &cast_type).expect("cast failed");
8966 assert_eq!(cast_array.data_type(), &cast_type);
8967 assert_eq!(array_to_strings(&cast_array), expected);
8968
8969 let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
8970 let cast_array = cast(&array, &cast_type).expect("cast failed");
8971 assert_eq!(cast_array.data_type(), &cast_type);
8972 assert_eq!(array_to_strings(&cast_array), expected);
8973
8974 let cast_type = Dictionary(Box::new(UInt16), Box::new(Utf8));
8975 let cast_array = cast(&array, &cast_type).expect("cast failed");
8976 assert_eq!(cast_array.data_type(), &cast_type);
8977 assert_eq!(array_to_strings(&cast_array), expected);
8978
8979 let cast_type = Dictionary(Box::new(UInt32), Box::new(Utf8));
8980 let cast_array = cast(&array, &cast_type).expect("cast failed");
8981 assert_eq!(cast_array.data_type(), &cast_type);
8982 assert_eq!(array_to_strings(&cast_array), expected);
8983
8984 let cast_type = Dictionary(Box::new(UInt64), Box::new(Utf8));
8985 let cast_array = cast(&array, &cast_type).expect("cast failed");
8986 assert_eq!(cast_array.data_type(), &cast_type);
8987 assert_eq!(array_to_strings(&cast_array), expected);
8988 }
8989
8990 #[test]
8991 fn test_cast_dict_to_dict_bad_index_value_primitive() {
8992 let mut builder = PrimitiveDictionaryBuilder::<Int32Type, Int64Type>::new();
8997
8998 for i in 0..200 {
9002 builder.append(i).unwrap();
9003 }
9004 let array: ArrayRef = Arc::new(builder.finish());
9005
9006 let cast_type = Dictionary(Box::new(Int8), Box::new(Utf8));
9007 let res = cast(&array, &cast_type);
9008 assert!(res.is_err());
9009 let actual_error = format!("{res:?}");
9010 let expected_error = "Could not convert 72 dictionary indexes from Int32 to Int8";
9011 assert!(
9012 actual_error.contains(expected_error),
9013 "did not find expected error '{actual_error}' in actual error '{expected_error}'"
9014 );
9015 }
9016
9017 #[test]
9018 fn test_cast_dict_to_dict_bad_index_value_utf8() {
9019 let mut builder = StringDictionaryBuilder::<Int32Type>::new();
9023
9024 for i in 0..200 {
9028 let val = format!("val{i}");
9029 builder.append(&val).unwrap();
9030 }
9031 let array = builder.finish();
9032
9033 let cast_type = Dictionary(Box::new(Int8), Box::new(Utf8));
9034 let res = cast(&array, &cast_type);
9035 assert!(res.is_err());
9036 let actual_error = format!("{res:?}");
9037 let expected_error = "Could not convert 72 dictionary indexes from Int32 to Int8";
9038 assert!(
9039 actual_error.contains(expected_error),
9040 "did not find expected error '{actual_error}' in actual error '{expected_error}'"
9041 );
9042 }
9043
9044 #[test]
9045 fn test_cast_nested_dictionary_to_dictionary_reuses_values() {
9046 let inner = DictionaryArray::<Int32Type>::new(
9047 Int32Array::from(vec![Some(0), None, Some(1)]),
9048 Arc::new(StringArray::from(vec!["x", "y"])),
9049 );
9050 let nested = DictionaryArray::<Int32Type>::new(
9051 Int32Array::from(vec![Some(0), Some(1), Some(2), None, Some(0)]),
9052 Arc::new(inner),
9053 );
9054
9055 let result = cast(&nested, &Dictionary(Box::new(Int32), Box::new(Utf8))).unwrap();
9056 let result = result.as_dictionary::<Int32Type>();
9057
9058 assert_eq!(
9059 result.keys(),
9060 &Int32Array::from(vec![Some(0), None, Some(1), None, Some(0)])
9061 );
9062 assert_eq!(
9063 result.values().as_string::<i32>(),
9064 &StringArray::from(vec!["x", "y"])
9065 );
9066 let logical: Vec<Option<&str>> = result
9067 .downcast_dict::<StringArray>()
9068 .unwrap()
9069 .into_iter()
9070 .collect();
9071 assert_eq!(logical, vec![Some("x"), None, Some("y"), None, Some("x")]);
9072 }
9073
9074 #[test]
9075 fn test_cast_primitive_dict() {
9076 let mut builder = PrimitiveDictionaryBuilder::<Int8Type, Int32Type>::new();
9078 builder.append(1).unwrap();
9079 builder.append_null();
9080 builder.append(3).unwrap();
9081 let array: ArrayRef = Arc::new(builder.finish());
9082
9083 let expected = vec!["1", "null", "3"];
9084
9085 let cast_array = cast(&array, &Utf8).expect("cast to UTF-8 failed");
9087 assert_eq!(array_to_strings(&cast_array), expected);
9088 assert_eq!(cast_array.data_type(), &Utf8);
9089
9090 let cast_array = cast(&array, &Int64).expect("cast to int64 failed");
9091 assert_eq!(array_to_strings(&cast_array), expected);
9092 assert_eq!(cast_array.data_type(), &Int64);
9093 }
9094
9095 #[test]
9096 fn test_cast_primitive_array_to_dict() {
9097 let mut builder = PrimitiveBuilder::<Int32Type>::new();
9098 builder.append_value(1);
9099 builder.append_null();
9100 builder.append_value(3);
9101 let array: ArrayRef = Arc::new(builder.finish());
9102
9103 let expected = vec!["1", "null", "3"];
9104
9105 let cast_type = Dictionary(Box::new(UInt8), Box::new(Int32));
9107 let cast_array = cast(&array, &cast_type).expect("cast failed");
9108 assert_eq!(cast_array.data_type(), &cast_type);
9109 assert_eq!(array_to_strings(&cast_array), expected);
9110
9111 let cast_type = Dictionary(Box::new(UInt8), Box::new(Int8));
9113 let cast_array = cast(&array, &cast_type).expect("cast failed");
9114 assert_eq!(cast_array.data_type(), &cast_type);
9115 assert_eq!(array_to_strings(&cast_array), expected);
9116 }
9117
9118 #[test]
9119 fn test_cast_time_array_to_dict() {
9120 use DataType::*;
9121
9122 let array = Arc::new(Date32Array::from(vec![Some(1000), None, Some(2000)])) as ArrayRef;
9123
9124 let expected = vec!["1972-09-27", "null", "1975-06-24"];
9125
9126 let cast_type = Dictionary(Box::new(UInt8), Box::new(Date32));
9127 let cast_array = cast(&array, &cast_type).expect("cast failed");
9128 assert_eq!(cast_array.data_type(), &cast_type);
9129 assert_eq!(array_to_strings(&cast_array), expected);
9130 }
9131
9132 #[test]
9133 fn test_cast_timestamp_array_to_dict() {
9134 use DataType::*;
9135
9136 let array = Arc::new(
9137 TimestampSecondArray::from(vec![Some(1000), None, Some(2000)]).with_timezone_utc(),
9138 ) as ArrayRef;
9139
9140 let expected = vec!["1970-01-01T00:16:40", "null", "1970-01-01T00:33:20"];
9141
9142 let cast_type = Dictionary(Box::new(UInt8), Box::new(Timestamp(TimeUnit::Second, None)));
9143 let cast_array = cast(&array, &cast_type).expect("cast failed");
9144 assert_eq!(cast_array.data_type(), &cast_type);
9145 assert_eq!(array_to_strings(&cast_array), expected);
9146 }
9147
9148 #[test]
9149 fn test_cast_string_array_to_dict() {
9150 use DataType::*;
9151
9152 let array = Arc::new(StringArray::from(vec![Some("one"), None, Some("three")])) as ArrayRef;
9153
9154 let expected = vec!["one", "null", "three"];
9155
9156 let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
9158 let cast_array = cast(&array, &cast_type).expect("cast failed");
9159 assert_eq!(cast_array.data_type(), &cast_type);
9160 assert_eq!(array_to_strings(&cast_array), expected);
9161 }
9162
9163 #[test]
9164 fn test_cast_null_array_to_from_decimal_array() {
9165 let data_type = DataType::Decimal128(12, 4);
9166 let array = new_null_array(&DataType::Null, 4);
9167 assert_eq!(array.data_type(), &DataType::Null);
9168 let cast_array = cast(&array, &data_type).expect("cast failed");
9169 assert_eq!(cast_array.data_type(), &data_type);
9170 for i in 0..4 {
9171 assert!(cast_array.is_null(i));
9172 }
9173
9174 let array = new_null_array(&data_type, 4);
9175 assert_eq!(array.data_type(), &data_type);
9176 let cast_array = cast(&array, &DataType::Null).expect("cast failed");
9177 assert_eq!(cast_array.data_type(), &DataType::Null);
9178 assert_eq!(cast_array.len(), 4);
9179 assert_eq!(cast_array.logical_nulls().unwrap().null_count(), 4);
9180 }
9181
9182 #[test]
9183 fn test_cast_null_array_from_and_to_primitive_array() {
9184 macro_rules! typed_test {
9185 ($ARR_TYPE:ident, $DATATYPE:ident, $TYPE:tt) => {{
9186 {
9187 let array = Arc::new(NullArray::new(6)) as ArrayRef;
9188 let expected = $ARR_TYPE::from(vec![None; 6]);
9189 let cast_type = DataType::$DATATYPE;
9190 let cast_array = cast(&array, &cast_type).expect("cast failed");
9191 let cast_array = cast_array.as_primitive::<$TYPE>();
9192 assert_eq!(cast_array.data_type(), &cast_type);
9193 assert_eq!(cast_array, &expected);
9194 }
9195 }};
9196 }
9197
9198 typed_test!(Int16Array, Int16, Int16Type);
9199 typed_test!(Int32Array, Int32, Int32Type);
9200 typed_test!(Int64Array, Int64, Int64Type);
9201
9202 typed_test!(UInt16Array, UInt16, UInt16Type);
9203 typed_test!(UInt32Array, UInt32, UInt32Type);
9204 typed_test!(UInt64Array, UInt64, UInt64Type);
9205
9206 typed_test!(Float16Array, Float16, Float16Type);
9207 typed_test!(Float32Array, Float32, Float32Type);
9208 typed_test!(Float64Array, Float64, Float64Type);
9209
9210 typed_test!(Date32Array, Date32, Date32Type);
9211 typed_test!(Date64Array, Date64, Date64Type);
9212 }
9213
9214 fn cast_from_null_to_other_base(data_type: &DataType, is_complex: bool) {
9215 let array = new_null_array(&DataType::Null, 4);
9217 assert_eq!(array.data_type(), &DataType::Null);
9218 let cast_array = cast(&array, data_type).expect("cast failed");
9219 assert_eq!(cast_array.data_type(), data_type);
9220 for i in 0..4 {
9221 if is_complex {
9222 assert!(cast_array.logical_nulls().unwrap().is_null(i));
9223 } else {
9224 assert!(cast_array.is_null(i));
9225 }
9226 }
9227 }
9228
9229 fn cast_from_null_to_other(data_type: &DataType) {
9230 cast_from_null_to_other_base(data_type, false);
9231 }
9232
9233 fn cast_from_null_to_other_complex(data_type: &DataType) {
9234 cast_from_null_to_other_base(data_type, true);
9235 }
9236
9237 #[test]
9238 fn test_cast_null_from_and_to_variable_sized() {
9239 cast_from_null_to_other(&DataType::Utf8);
9240 cast_from_null_to_other(&DataType::LargeUtf8);
9241 cast_from_null_to_other(&DataType::Binary);
9242 cast_from_null_to_other(&DataType::LargeBinary);
9243 }
9244
9245 #[test]
9246 fn test_cast_null_from_and_to_nested_type() {
9247 let data_type = DataType::Map(
9249 Arc::new(Field::new_struct(
9250 Field::MAP_ENTRIES_FIELD_DEFAULT_NAME,
9251 vec![
9252 Field::new(Field::MAP_KEY_FIELD_DEFAULT_NAME, DataType::Utf8, false),
9253 Field::new(Field::MAP_VALUE_FIELD_DEFAULT_NAME, DataType::Int32, true),
9254 ],
9255 false,
9256 )),
9257 false,
9258 );
9259 cast_from_null_to_other(&data_type);
9260
9261 let data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
9263 cast_from_null_to_other(&data_type);
9264 let data_type = DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
9265 cast_from_null_to_other(&data_type);
9266 let data_type =
9267 DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 4);
9268 cast_from_null_to_other(&data_type);
9269
9270 let values = vec![None, None, None, None] as Vec<Option<&str>>;
9272 let array: DictionaryArray<Int8Type> = values.into_iter().collect();
9273 let array = Arc::new(array) as ArrayRef;
9274 let data_type = array.data_type().to_owned();
9275 cast_from_null_to_other(&data_type);
9276
9277 let data_type = DataType::Struct(vec![Field::new("data", DataType::Int64, false)].into());
9279 cast_from_null_to_other(&data_type);
9280
9281 let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Int32, true)));
9282 cast_from_null_to_other(&target_type);
9283
9284 let target_type =
9285 DataType::LargeListView(Arc::new(Field::new("item", DataType::Int32, true)));
9286 cast_from_null_to_other(&target_type);
9287
9288 let fields = UnionFields::from_fields(vec![Field::new("a", DataType::Int64, false)]);
9289 let target_type = DataType::Union(fields, UnionMode::Sparse);
9290 cast_from_null_to_other_complex(&target_type);
9291
9292 let target_type = DataType::RunEndEncoded(
9293 Arc::new(Field::new("item", DataType::Int32, true)),
9294 Arc::new(Field::new("item", DataType::Int32, true)),
9295 );
9296 cast_from_null_to_other_complex(&target_type);
9297 }
9298
9299 fn array_to_strings(array: &ArrayRef) -> Vec<String> {
9301 let options = FormatOptions::new().with_null("null");
9302 let formatter = ArrayFormatter::try_new(array.as_ref(), &options).unwrap();
9303 (0..array.len())
9304 .map(|i| formatter.value(i).to_string())
9305 .collect()
9306 }
9307
9308 #[test]
9309 fn test_cast_utf8_to_date32() {
9310 use chrono::NaiveDate;
9311 let from_ymd = chrono::NaiveDate::from_ymd_opt;
9312 let since = chrono::NaiveDate::signed_duration_since;
9313
9314 let a = StringArray::from(vec![
9315 "2000-01-01", "2000-01-01T12:00:00", "2000-2-2", "2000-00-00", "2000", ]);
9321 let array = Arc::new(a) as ArrayRef;
9322 let b = cast(&array, &DataType::Date32).unwrap();
9323 let c = b.as_primitive::<Date32Type>();
9324
9325 let date_value = since(
9327 NaiveDate::from_ymd_opt(2000, 1, 1).unwrap(),
9328 from_ymd(1970, 1, 1).unwrap(),
9329 )
9330 .num_days() as i32;
9331 assert!(c.is_valid(0)); assert_eq!(date_value, c.value(0));
9333
9334 assert!(c.is_valid(1)); assert_eq!(date_value, c.value(1));
9336
9337 let date_value = since(
9338 NaiveDate::from_ymd_opt(2000, 2, 2).unwrap(),
9339 from_ymd(1970, 1, 1).unwrap(),
9340 )
9341 .num_days() as i32;
9342 assert!(c.is_valid(2)); assert_eq!(date_value, c.value(2));
9344
9345 assert!(!c.is_valid(3)); assert!(!c.is_valid(4)); }
9349
9350 #[test]
9351 fn test_cast_utf8_to_date64() {
9352 let a = StringArray::from(vec![
9353 "2000-01-01T12:00:00", "2020-12-15T12:34:56", "2020-2-2T12:34:56", "2000-00-00T12:00:00", "2000-01-01 12:00:00", "2000-01-01", ]);
9360 let array = Arc::new(a) as ArrayRef;
9361 let b = cast(&array, &DataType::Date64).unwrap();
9362 let c = b.as_primitive::<Date64Type>();
9363
9364 assert!(c.is_valid(0)); assert_eq!(946728000000, c.value(0));
9367 assert!(c.is_valid(1)); assert_eq!(1608035696000, c.value(1));
9369 assert!(!c.is_valid(2)); assert!(!c.is_valid(3)); assert!(c.is_valid(4)); assert_eq!(946728000000, c.value(4));
9374 assert!(c.is_valid(5)); assert_eq!(946684800000, c.value(5));
9376 }
9377
9378 #[test]
9379 fn test_cast_zero_width_fsl_to_fsl() {
9380 let field = Arc::new(Field::new_list_field(DataType::Int32, true));
9383 let input = FixedSizeListArray::try_new_with_length(
9384 field,
9385 0,
9386 Arc::new(Int32Array::new_null(0)),
9387 None,
9388 3,
9389 )
9390 .unwrap();
9391 let to_type =
9392 DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int64, true)), 0);
9393 let result = cast(&(Arc::new(input) as ArrayRef), &to_type).unwrap();
9394 assert_eq!(result.len(), 3);
9395 assert_eq!(result.data_type(), &to_type);
9396 }
9397
9398 #[test]
9399 #[cfg_attr(miri, ignore)] fn test_can_cast_fsl_to_fsl() {
9401 let from_array = Arc::new(
9402 FixedSizeListArray::from_iter_primitive::<Float32Type, _, _>(
9403 [Some([Some(1.0), Some(2.0)]), None],
9404 2,
9405 ),
9406 ) as ArrayRef;
9407 let to_array = Arc::new(
9408 FixedSizeListArray::from_iter_primitive::<Float16Type, _, _>(
9409 [
9410 Some([Some(f16::from_f32(1.0)), Some(f16::from_f32(2.0))]),
9411 None,
9412 ],
9413 2,
9414 ),
9415 ) as ArrayRef;
9416
9417 assert!(can_cast_types(from_array.data_type(), to_array.data_type()));
9418 let actual = cast(&from_array, to_array.data_type()).unwrap();
9419 assert_eq!(actual.data_type(), to_array.data_type());
9420
9421 let invalid_target =
9422 DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Binary, true)), 2);
9423 assert!(!can_cast_types(from_array.data_type(), &invalid_target));
9424
9425 let invalid_size =
9426 DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Float16, true)), 5);
9427 assert!(!can_cast_types(from_array.data_type(), &invalid_size));
9428 }
9429
9430 #[test]
9431 fn test_can_cast_types_fixed_size_list_to_list() {
9432 let array1 = make_fixed_size_list_array();
9434 assert!(can_cast_types(
9435 array1.data_type(),
9436 &DataType::List(Arc::new(Field::new("", DataType::Int32, false)))
9437 ));
9438
9439 let array2 = make_fixed_size_list_array_for_large_list();
9441 assert!(can_cast_types(
9442 array2.data_type(),
9443 &DataType::LargeList(Arc::new(Field::new("", DataType::Int64, false)))
9444 ));
9445 }
9446
9447 #[test]
9448 fn test_cast_fixed_size_list_to_list() {
9449 let cases = [
9455 (
9457 Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9458 [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
9459 2,
9460 )) as ArrayRef,
9461 Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>([
9462 Some([Some(1), Some(1)]),
9463 Some([Some(2), Some(2)]),
9464 ])) as ArrayRef,
9465 ),
9466 (
9468 Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9469 [None, Some([Some(2), Some(2)])],
9470 2,
9471 )) as ArrayRef,
9472 Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>([
9473 None,
9474 Some([Some(2), Some(2)]),
9475 ])) as ArrayRef,
9476 ),
9477 (
9479 Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9480 [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
9481 2,
9482 )) as ArrayRef,
9483 Arc::new(LargeListArray::from_iter_primitive::<Int64Type, _, _>([
9484 Some([Some(1), Some(1)]),
9485 Some([Some(2), Some(2)]),
9486 ])) as ArrayRef,
9487 ),
9488 (
9490 Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9491 [None, Some([Some(2), Some(2)])],
9492 2,
9493 )) as ArrayRef,
9494 Arc::new(LargeListArray::from_iter_primitive::<Int64Type, _, _>([
9495 None,
9496 Some([Some(2), Some(2)]),
9497 ])) as ArrayRef,
9498 ),
9499 (
9501 Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9502 [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
9503 2,
9504 )) as ArrayRef,
9505 Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>([
9506 Some([Some(1), Some(1)]),
9507 Some([Some(2), Some(2)]),
9508 ])) as ArrayRef,
9509 ),
9510 (
9512 Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9513 [None, Some([Some(2), Some(2)])],
9514 2,
9515 )) as ArrayRef,
9516 Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>([
9517 None,
9518 Some([Some(2), Some(2)]),
9519 ])) as ArrayRef,
9520 ),
9521 (
9523 Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9524 [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
9525 2,
9526 )) as ArrayRef,
9527 Arc::new(LargeListViewArray::from_iter_primitive::<Int64Type, _, _>(
9528 [Some([Some(1), Some(1)]), Some([Some(2), Some(2)])],
9529 )) as ArrayRef,
9530 ),
9531 (
9533 Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9534 [None, Some([Some(2), Some(2)])],
9535 2,
9536 )) as ArrayRef,
9537 Arc::new(LargeListViewArray::from_iter_primitive::<Int64Type, _, _>(
9538 [None, Some([Some(2), Some(2)])],
9539 )) as ArrayRef,
9540 ),
9541 ];
9542
9543 for (array, expected) in cases {
9544 assert!(
9545 can_cast_types(array.data_type(), expected.data_type()),
9546 "can_cast_types claims we cannot cast {:?} to {:?}",
9547 array.data_type(),
9548 expected.data_type()
9549 );
9550
9551 let list_array = cast(&array, expected.data_type())
9552 .unwrap_or_else(|_| panic!("Failed to cast {array:?} to {expected:?}"));
9553 assert_eq!(
9554 list_array.as_ref(),
9555 &expected,
9556 "Incorrect result from casting {array:?} to {expected:?}",
9557 );
9558 }
9559 }
9560
9561 #[test]
9562 fn test_cast_fixed_size_list_to_list_preserves_field_metadata() {
9563 use std::collections::HashMap;
9564
9565 let metadata: HashMap<String, String> =
9566 HashMap::from([("PARQUET:field_id".to_string(), "89".to_string())]);
9567
9568 let src = Arc::new(
9569 FixedSizeListArray::from_iter_primitive::<Float32Type, _, _>(
9570 [[1.0_f32, 2.0].map(Some), [3.0, 4.0].map(Some)].map(Some),
9571 2,
9572 ),
9573 ) as ArrayRef;
9574
9575 let target_field = Arc::new(
9576 Field::new("element", DataType::Float32, true).with_metadata(metadata.clone()),
9577 );
9578
9579 let target_types = [
9580 DataType::List(target_field.clone()),
9581 DataType::LargeList(target_field.clone()),
9582 DataType::ListView(target_field.clone()),
9583 DataType::LargeListView(target_field.clone()),
9584 ];
9585
9586 for target_type in &target_types {
9587 let result = cast(&src, target_type).unwrap();
9588 assert_eq!(
9589 result.data_type(),
9590 target_type,
9591 "Cast to {target_type:?} should preserve field metadata"
9592 );
9593 }
9594 }
9595
9596 #[test]
9597 fn test_cast_utf8_to_list() {
9598 let array = Arc::new(StringArray::from(vec!["5"])) as ArrayRef;
9600 let field = Arc::new(Field::new("", DataType::Int32, false));
9601 let list_array = cast(&array, &DataType::List(field.clone())).unwrap();
9602 let actual = list_array.as_list_opt::<i32>().unwrap();
9603 let expect = ListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
9604 assert_eq!(&expect.value(0), &actual.value(0));
9605
9606 let list_array = cast(&array, &DataType::LargeList(field.clone())).unwrap();
9608 let actual = list_array.as_list_opt::<i64>().unwrap();
9609 let expect = LargeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
9610 assert_eq!(&expect.value(0), &actual.value(0));
9611
9612 let list_array = cast(&array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
9614 let actual = list_array.as_fixed_size_list_opt().unwrap();
9615 let expect =
9616 FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])], 1);
9617 assert_eq!(&expect.value(0), &actual.value(0));
9618 }
9619
9620 #[test]
9621 fn test_cast_single_element_fixed_size_list() {
9622 let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
9624 [(Some([Some(5)]))],
9625 1,
9626 )) as ArrayRef;
9627 let casted_array = cast(&from_array, &DataType::Int32).unwrap();
9628 let actual: &Int32Array = casted_array.as_primitive();
9629 let expected = Int32Array::from(vec![Some(5)]);
9630 assert_eq!(&expected, actual);
9631
9632 let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
9634 [(Some([Some(5)]))],
9635 1,
9636 )) as ArrayRef;
9637 let to_field = Arc::new(Field::new("dummy", DataType::Float32, false));
9638 let actual = cast(&from_array, &DataType::FixedSizeList(to_field.clone(), 1)).unwrap();
9639 let expected = Arc::new(FixedSizeListArray::new(
9640 to_field.clone(),
9641 1,
9642 Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
9643 None,
9644 )) as ArrayRef;
9645 assert_eq!(*expected, *actual);
9646
9647 let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
9649 [(Some([Some(5)]))],
9650 1,
9651 )) as ArrayRef;
9652 let to_field_inner = Arc::new(Field::new_list_field(DataType::Float32, false));
9653 let to_field = Arc::new(Field::new(
9654 "dummy",
9655 DataType::FixedSizeList(to_field_inner.clone(), 1),
9656 false,
9657 ));
9658 let actual = cast(&from_array, &DataType::FixedSizeList(to_field.clone(), 1)).unwrap();
9659 let expected = Arc::new(FixedSizeListArray::new(
9660 to_field.clone(),
9661 1,
9662 Arc::new(FixedSizeListArray::new(
9663 to_field_inner.clone(),
9664 1,
9665 Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
9666 None,
9667 )) as ArrayRef,
9668 None,
9669 )) as ArrayRef;
9670 assert_eq!(*expected, *actual);
9671
9672 let field = Arc::new(Field::new("dummy", DataType::Float32, false));
9674 let from_array = Arc::new(Int8Array::from(vec![Some(5)])) as ArrayRef;
9675 let casted_array = cast(&from_array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
9676 let actual = casted_array.as_fixed_size_list();
9677 let expected = Arc::new(FixedSizeListArray::new(
9678 field.clone(),
9679 1,
9680 Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
9681 None,
9682 )) as ArrayRef;
9683 assert_eq!(expected.as_ref(), actual);
9684
9685 let field = Arc::new(Field::new("nullable", DataType::Float32, true));
9687 let from_array = Arc::new(Int8Array::from(vec![None])) as ArrayRef;
9688 let casted_array = cast(&from_array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
9689 let actual = casted_array.as_fixed_size_list();
9690 let expected = Arc::new(FixedSizeListArray::new(
9691 field.clone(),
9692 1,
9693 Arc::new(Float32Array::from(vec![None])) as ArrayRef,
9694 None,
9695 )) as ArrayRef;
9696 assert_eq!(expected.as_ref(), actual);
9697 }
9698
9699 #[test]
9700 fn test_cast_list_containers() {
9701 let array = make_large_list_array();
9703 let list_array = cast(
9704 &array,
9705 &DataType::List(Arc::new(Field::new("", DataType::Int32, false))),
9706 )
9707 .unwrap();
9708 let actual = list_array.as_any().downcast_ref::<ListArray>().unwrap();
9709 let expected = array.as_any().downcast_ref::<LargeListArray>().unwrap();
9710
9711 assert_eq!(&expected.value(0), &actual.value(0));
9712 assert_eq!(&expected.value(1), &actual.value(1));
9713 assert_eq!(&expected.value(2), &actual.value(2));
9714
9715 let array = make_list_array();
9717 let large_list_array = cast(
9718 &array,
9719 &DataType::LargeList(Arc::new(Field::new("", DataType::Int32, false))),
9720 )
9721 .unwrap();
9722 let actual = large_list_array
9723 .as_any()
9724 .downcast_ref::<LargeListArray>()
9725 .unwrap();
9726 let expected = array.as_any().downcast_ref::<ListArray>().unwrap();
9727
9728 assert_eq!(&expected.value(0), &actual.value(0));
9729 assert_eq!(&expected.value(1), &actual.value(1));
9730 assert_eq!(&expected.value(2), &actual.value(2));
9731 }
9732
9733 #[test]
9734 fn test_cast_list_view() {
9735 let array = make_list_view_array();
9737 let to = DataType::ListView(Field::new_list_field(DataType::Float32, true).into());
9738 assert!(can_cast_types(array.data_type(), &to));
9739 let actual = cast(&array, &to).unwrap();
9740 let actual = actual.as_list_view::<i32>();
9741
9742 assert_eq!(
9743 &Float32Array::from(vec![0.0, 1.0, 2.0]) as &dyn Array,
9744 actual.value(0).as_ref()
9745 );
9746 assert_eq!(
9747 &Float32Array::from(vec![3.0, 4.0, 5.0]) as &dyn Array,
9748 actual.value(1).as_ref()
9749 );
9750 assert_eq!(
9751 &Float32Array::from(vec![6.0, 7.0]) as &dyn Array,
9752 actual.value(2).as_ref()
9753 );
9754
9755 let array = make_large_list_view_array();
9757 let to = DataType::LargeListView(Field::new_list_field(DataType::Float32, true).into());
9758 assert!(can_cast_types(array.data_type(), &to));
9759 let actual = cast(&array, &to).unwrap();
9760 let actual = actual.as_list_view::<i64>();
9761
9762 assert_eq!(
9763 &Float32Array::from(vec![0.0, 1.0, 2.0]) as &dyn Array,
9764 actual.value(0).as_ref()
9765 );
9766 assert_eq!(
9767 &Float32Array::from(vec![3.0, 4.0, 5.0]) as &dyn Array,
9768 actual.value(1).as_ref()
9769 );
9770 assert_eq!(
9771 &Float32Array::from(vec![6.0, 7.0]) as &dyn Array,
9772 actual.value(2).as_ref()
9773 );
9774 }
9775
9776 #[test]
9777 fn test_non_list_to_list_view() {
9778 let input = Arc::new(Int32Array::from(vec![Some(0), None, Some(2)])) as ArrayRef;
9779 let expected_primitive =
9780 Arc::new(Float32Array::from(vec![Some(0.0), None, Some(2.0)])) as ArrayRef;
9781
9782 let expected = ListViewArray::new(
9784 Field::new_list_field(DataType::Float32, true).into(),
9785 vec![0, 1, 2].into(),
9786 vec![1, 1, 1].into(),
9787 expected_primitive.clone(),
9788 None,
9789 );
9790 assert!(can_cast_types(input.data_type(), expected.data_type()));
9791 let actual = cast(&input, expected.data_type()).unwrap();
9792 assert_eq!(actual.as_ref(), &expected);
9793
9794 let expected = LargeListViewArray::new(
9796 Field::new_list_field(DataType::Float32, true).into(),
9797 vec![0, 1, 2].into(),
9798 vec![1, 1, 1].into(),
9799 expected_primitive.clone(),
9800 None,
9801 );
9802 assert!(can_cast_types(input.data_type(), expected.data_type()));
9803 let actual = cast(&input, expected.data_type()).unwrap();
9804 assert_eq!(actual.as_ref(), &expected);
9805 }
9806
9807 #[test]
9808 fn test_cast_list_to_zero_size_fsl() {
9809 let field = Arc::new(Field::new("a", DataType::Null, true));
9810 let length = 2;
9811 let expected = Arc::new(
9812 FixedSizeListArray::try_new_with_length(
9813 field.clone(),
9814 0,
9815 new_empty_array(&DataType::Null),
9816 None,
9817 2,
9818 )
9819 .unwrap(),
9820 ) as ArrayRef;
9821
9822 let list = Arc::new(ListArray::new(
9823 field.clone(),
9824 OffsetBuffer::from_repeated_length(0, length),
9825 new_empty_array(&DataType::Null),
9826 None,
9827 ));
9828 let fsl = cast(list.as_ref(), expected.data_type()).unwrap();
9829 assert_eq!(&expected, &fsl);
9830
9831 let list = Arc::new(ListViewArray::new(
9832 field.clone(),
9833 vec![0; length].into(),
9834 vec![0; length].into(),
9835 new_empty_array(&DataType::Null),
9836 None,
9837 ));
9838 let fsl = cast(list.as_ref(), expected.data_type()).unwrap();
9839 assert_eq!(&expected, &fsl);
9840
9841 let field = Arc::new(Field::new_list_field(DataType::Int32, true));
9843 let target = DataType::FixedSizeList(field.clone(), 0);
9844 let strict = CastOptions {
9845 safe: false,
9846 ..Default::default()
9847 };
9848 for nulls in [None, Some(NullBuffer::from(vec![true, false]))] {
9849 let values = Arc::new(Int32Array::from(vec![1, 2, 3]));
9850 let inputs: [ArrayRef; 2] = [
9851 Arc::new(ListArray::new(
9852 field.clone(),
9853 OffsetBuffer::new(vec![3; 3].into()),
9854 values.clone(),
9855 nulls.clone(),
9856 )),
9857 Arc::new(LargeListArray::new(
9858 field.clone(),
9859 OffsetBuffer::new(vec![3; 3].into()),
9860 values,
9861 nulls.clone(),
9862 )),
9863 ];
9864 for input in inputs {
9865 let actual = cast_with_options(input.as_ref(), &target, &strict).unwrap();
9866 assert_eq!(actual.len(), 2);
9867 assert_eq!(actual.data_type(), &target);
9868 assert_eq!(actual.nulls(), nulls.as_ref());
9869 assert_eq!(actual.as_fixed_size_list().values().len(), 0);
9870 }
9871 }
9872 }
9873
9874 #[test]
9875 fn test_issue_10975_sliced_list_to_fsl() {
9876 fn test<O: OffsetSizeTrait>() {
9877 let input = GenericListArray::<O>::from_iter_primitive::<Int32Type, _, _>([
9878 Some(vec![Some(1), Some(2)]),
9879 Some(vec![Some(3), Some(4)]),
9880 Some(vec![Some(5), Some(6)]),
9881 ]);
9882 let expected = FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9883 [Some([Some(3), Some(4)]), Some([Some(5), Some(6)])],
9884 2,
9885 );
9886 for safe in [true, false] {
9887 let options = CastOptions {
9888 safe,
9889 ..Default::default()
9890 };
9891 let actual =
9892 cast_with_options(&input.slice(1, 2), expected.data_type(), &options).unwrap();
9893 assert_eq!(actual.as_ref(), &expected as &dyn Array);
9894 }
9895 }
9896 test::<i32>();
9897 test::<i64>();
9898 }
9899
9900 #[test]
9901 fn test_issue_10975_sliced_list_to_fsl_subcast() {
9902 fn test<O: OffsetSizeTrait>() {
9903 let input = GenericListArray::<O>::from_iter_primitive::<Int32Type, _, _>([
9906 Some(vec![Some(i32::MAX); 3]),
9907 Some(vec![Some(3), None]),
9908 Some(vec![Some(5), Some(6)]),
9909 Some(vec![Some(i32::MAX); 2]),
9910 ]);
9911 let selected = input.slice(1, 3).slice(0, 2);
9912 let expected = FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9913 [Some([Some(3), None]), Some([Some(5), Some(6)])],
9914 2,
9915 );
9916 for safe in [true, false] {
9917 let options = CastOptions {
9918 safe,
9919 ..Default::default()
9920 };
9921 for child_type in [DataType::Int32, DataType::Int64, DataType::Int16] {
9922 let target = DataType::FixedSizeList(
9923 Arc::new(Field::new_list_field(child_type, true)),
9924 2,
9925 );
9926 let actual = cast_with_options(&selected, &target, &options).unwrap();
9927 let expected = cast_with_options(&expected, &target, &options).unwrap();
9928 assert_eq!(actual.as_ref(), expected.as_ref());
9929 assert_eq!(actual.as_fixed_size_list().values().len(), 4);
9930 }
9931 }
9932 }
9933 test::<i32>();
9934 test::<i64>();
9935 }
9936
9937 #[test]
9938 fn test_issue_10975_sliced_list_to_fsl_padding() {
9939 fn test<O: OffsetSizeTrait>() {
9940 let field = Arc::new(Field::new_list_field(DataType::Int32, true));
9941 let lengths = [3, 0, 0, 2, 1, 3, 2, 2, 0, 2];
9942 let values = Int32Array::from_iter_values(0..16).slice(1, 15);
9943 let input = GenericListArray::<O>::new(
9944 field.clone(),
9945 OffsetBuffer::from_lengths(lengths),
9946 Arc::new(values),
9947 Some(NullBuffer::from(vec![
9948 false, false, false, true, false, false, true, false, false, true,
9949 ])),
9950 );
9951 let target = DataType::FixedSizeList(field, 2);
9952 for safe in [true, false] {
9953 let options = CastOptions {
9954 safe,
9955 ..Default::default()
9956 };
9957 let full = cast_with_options(&input, &target, &options).unwrap();
9958 for (start, len) in [
9959 (1, 8), (1, 2), (3, 4), (6, 2), (8, 1), ] {
9965 let selected = input.slice(start, len);
9966 let actual = cast_with_options(&selected, &target, &options).unwrap();
9967 assert_eq!(actual.as_ref(), full.slice(start, len).as_ref());
9968 assert_eq!(actual.as_fixed_size_list().values().len(), len * 2);
9969 }
9970 }
9971 }
9972 test::<i32>();
9973 test::<i64>();
9974 }
9975
9976 #[test]
9977 fn test_issue_10975_sliced_list_to_fsl_safety() {
9978 fn test<O: OffsetSizeTrait>() {
9979 let input = GenericListArray::<O>::from_iter_primitive::<Int32Type, _, _>([
9980 Some(vec![Some(99); 3]),
9981 Some(vec![Some(1), Some(2)]),
9982 Some(vec![]),
9983 Some(vec![Some(3)]),
9984 Some(vec![Some(4); 3]),
9985 Some(vec![Some(5), Some(6)]),
9986 ]);
9987 let expected = FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9988 [
9989 Some([Some(1), Some(2)]),
9990 None,
9991 None,
9992 None,
9993 Some([Some(5), Some(6)]),
9994 ],
9995 2,
9996 );
9997 let actual = cast(&input.slice(1, 5), expected.data_type()).unwrap();
9998 assert_eq!(actual.as_ref(), &expected as &dyn Array);
9999 assert_eq!(actual.as_fixed_size_list().values().len(), 10);
10000 let strict = CastOptions {
10001 safe: false,
10002 ..Default::default()
10003 };
10004 let error =
10005 cast_with_options(&input.slice(1, 5), expected.data_type(), &strict).unwrap_err();
10006 assert_eq!(
10007 error.to_string(),
10008 "Cast error: Cannot cast to FixedSizeList(2): value at index 1 has length 0"
10009 );
10010 }
10011 test::<i32>();
10012 test::<i64>();
10013 }
10014
10015 #[test]
10016 fn test_cast_list_to_fsl() {
10017 let field = Arc::new(Field::new_list_field(DataType::Int32, true));
10025 let values = vec![
10026 Some(vec![Some(1), Some(2), Some(3)]),
10027 Some(vec![Some(4), Some(5), Some(6)]),
10028 ];
10029 let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(
10030 values.clone(),
10031 )) as ArrayRef;
10032 let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
10033 values, 3,
10034 )) as ArrayRef;
10035 let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
10036 assert_eq!(expected.as_ref(), actual.as_ref());
10037
10038 let cases = [
10041 (
10042 vec![1, 2, 3, 4, 5, 6],
10044 vec![3, 0, 3, 0],
10045 ),
10046 (
10047 vec![1, 2, 3, 0, 0, 4, 5, 6, 0],
10049 vec![3, 2, 3, 1],
10050 ),
10051 (
10052 vec![1, 2, 3, 0, 0, 0, 4, 5, 6, 0, 0, 0],
10054 vec![3, 3, 3, 3],
10055 ),
10056 (
10057 vec![1, 2, 3, 4, 5, 6, 0, 0, 0],
10059 vec![3, 0, 3, 3],
10060 ),
10061 ];
10062 let null_buffer = NullBuffer::from(vec![true, false, true, false]);
10063
10064 let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
10065 vec![
10066 Some(vec![Some(1), Some(2), Some(3)]),
10067 None,
10068 Some(vec![Some(4), Some(5), Some(6)]),
10069 None,
10070 ],
10071 3,
10072 )) as ArrayRef;
10073
10074 for (values, lengths) in &cases {
10075 let array = Arc::new(ListArray::new(
10076 field.clone(),
10077 OffsetBuffer::from_lengths(lengths.clone()),
10078 Arc::new(Int32Array::from(values.clone())),
10079 Some(null_buffer.clone()),
10080 )) as ArrayRef;
10081 let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
10082 assert_eq!(expected.as_ref(), actual.as_ref());
10083 }
10084 }
10085
10086 #[test]
10087 fn test_cast_list_view_to_fsl() {
10088 let field = Arc::new(Field::new_list_field(DataType::Int32, true));
10096 let values = vec![
10097 Some(vec![Some(1), Some(2), Some(3)]),
10098 Some(vec![Some(4), Some(5), Some(6)]),
10099 ];
10100 let array = Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>(
10101 values.clone(),
10102 )) as ArrayRef;
10103 let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
10104 values, 3,
10105 )) as ArrayRef;
10106 let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
10107 assert_eq!(expected.as_ref(), actual.as_ref());
10108
10109 let cases = [
10112 (
10113 vec![1, 2, 3, 4, 5, 6],
10115 vec![0, 0, 3, 0],
10116 vec![3, 0, 3, 0],
10117 ),
10118 (
10119 vec![1, 2, 3, 0, 0, 4, 5, 6, 0],
10121 vec![0, 1, 5, 0],
10122 vec![3, 2, 3, 1],
10123 ),
10124 (
10125 vec![1, 2, 3, 0, 0, 0, 4, 5, 6, 0, 0, 0],
10127 vec![0, 3, 6, 9],
10128 vec![3, 3, 3, 3],
10129 ),
10130 (
10131 vec![1, 2, 3, 4, 5, 6, 0, 0, 0],
10133 vec![0, 0, 3, 6],
10134 vec![3, 0, 3, 3],
10135 ),
10136 ];
10137 let null_buffer = NullBuffer::from(vec![true, false, true, false]);
10138
10139 let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
10140 vec![
10141 Some(vec![Some(1), Some(2), Some(3)]),
10142 None,
10143 Some(vec![Some(4), Some(5), Some(6)]),
10144 None,
10145 ],
10146 3,
10147 )) as ArrayRef;
10148
10149 for (values, offsets, lengths) in &cases {
10150 let array = Arc::new(ListViewArray::new(
10151 field.clone(),
10152 offsets.clone().into(),
10153 lengths.clone().into(),
10154 Arc::new(Int32Array::from(values.clone())),
10155 Some(null_buffer.clone()),
10156 )) as ArrayRef;
10157 let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
10158 assert_eq!(expected.as_ref(), actual.as_ref());
10159 }
10160 }
10161
10162 #[test]
10163 fn test_cast_list_to_fsl_safety() {
10164 let values = vec![
10165 Some(vec![Some(1), Some(2), Some(3)]),
10166 Some(vec![Some(4), Some(5)]),
10167 Some(vec![Some(6), Some(7), Some(8), Some(9)]),
10168 Some(vec![Some(3), Some(4), Some(5)]),
10169 ];
10170 let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(
10171 values.clone(),
10172 )) as ArrayRef;
10173
10174 let res = cast_with_options(
10175 array.as_ref(),
10176 &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
10177 &CastOptions {
10178 safe: false,
10179 ..Default::default()
10180 },
10181 );
10182 assert!(res.is_err());
10183 assert!(
10184 format!("{res:?}")
10185 .contains("Cannot cast to FixedSizeList(3): value at index 1 has length 2")
10186 );
10187
10188 let res = cast(
10191 array.as_ref(),
10192 &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
10193 )
10194 .unwrap();
10195 let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
10196 vec![
10197 Some(vec![Some(1), Some(2), Some(3)]),
10198 None, None, Some(vec![Some(3), Some(4), Some(5)]),
10201 ],
10202 3,
10203 )) as ArrayRef;
10204 assert_eq!(expected.as_ref(), res.as_ref());
10205
10206 let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
10209 Some(vec![Some(1), Some(2), Some(3)]),
10210 None,
10211 ])) as ArrayRef;
10212 let res = cast_with_options(
10213 array.as_ref(),
10214 &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
10215 &CastOptions {
10216 safe: false,
10217 ..Default::default()
10218 },
10219 )
10220 .unwrap();
10221 let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
10222 vec![Some(vec![Some(1), Some(2), Some(3)]), None],
10223 3,
10224 )) as ArrayRef;
10225 assert_eq!(expected.as_ref(), res.as_ref());
10226 }
10227
10228 #[test]
10229 fn test_cast_list_view_to_fsl_safety() {
10230 let values = vec![
10231 Some(vec![Some(1), Some(2), Some(3)]),
10232 Some(vec![Some(4), Some(5)]),
10233 Some(vec![Some(6), Some(7), Some(8), Some(9)]),
10234 Some(vec![Some(3), Some(4), Some(5)]),
10235 ];
10236 let array = Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>(
10237 values.clone(),
10238 )) as ArrayRef;
10239
10240 let res = cast_with_options(
10241 array.as_ref(),
10242 &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
10243 &CastOptions {
10244 safe: false,
10245 ..Default::default()
10246 },
10247 );
10248 assert!(res.is_err());
10249 assert!(
10250 format!("{res:?}")
10251 .contains("Cannot cast to FixedSizeList(3): value at index 1 has length 2")
10252 );
10253
10254 let res = cast(
10257 array.as_ref(),
10258 &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
10259 )
10260 .unwrap();
10261 let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
10262 vec![
10263 Some(vec![Some(1), Some(2), Some(3)]),
10264 None, None, Some(vec![Some(3), Some(4), Some(5)]),
10267 ],
10268 3,
10269 )) as ArrayRef;
10270 assert_eq!(expected.as_ref(), res.as_ref());
10271
10272 let array = Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>(vec![
10275 Some(vec![Some(1), Some(2), Some(3)]),
10276 None,
10277 ])) as ArrayRef;
10278 let res = cast_with_options(
10279 array.as_ref(),
10280 &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
10281 &CastOptions {
10282 safe: false,
10283 ..Default::default()
10284 },
10285 )
10286 .unwrap();
10287 let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
10288 vec![Some(vec![Some(1), Some(2), Some(3)]), None],
10289 3,
10290 )) as ArrayRef;
10291 assert_eq!(expected.as_ref(), res.as_ref());
10292 }
10293
10294 #[test]
10295 fn test_cast_large_list_to_fsl() {
10296 let values = vec![Some(vec![Some(1), Some(2)]), Some(vec![Some(3), Some(4)])];
10297 let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
10298 values.clone(),
10299 2,
10300 )) as ArrayRef;
10301 let target_type =
10302 DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 2);
10303
10304 let array = Arc::new(LargeListArray::from_iter_primitive::<Int32Type, _, _>(
10305 values.clone(),
10306 )) as ArrayRef;
10307 let actual = cast(array.as_ref(), &target_type).unwrap();
10308 assert_eq!(expected.as_ref(), actual.as_ref());
10309
10310 let array = Arc::new(LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(
10311 values.clone(),
10312 )) as ArrayRef;
10313 let actual = cast(array.as_ref(), &target_type).unwrap();
10314 assert_eq!(expected.as_ref(), actual.as_ref());
10315 }
10316
10317 #[test]
10318 fn test_cast_list_to_fsl_subcast() {
10319 let array = Arc::new(LargeListArray::from_iter_primitive::<Int32Type, _, _>(
10320 vec![
10321 Some(vec![Some(1), Some(2)]),
10322 Some(vec![Some(3), Some(i32::MAX)]),
10323 ],
10324 )) as ArrayRef;
10325 let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
10326 vec![
10327 Some(vec![Some(1), Some(2)]),
10328 Some(vec![Some(3), Some(i32::MAX as i64)]),
10329 ],
10330 2,
10331 )) as ArrayRef;
10332 let actual = cast(
10333 array.as_ref(),
10334 &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int64, true)), 2),
10335 )
10336 .unwrap();
10337 assert_eq!(expected.as_ref(), actual.as_ref());
10338
10339 let res = cast_with_options(
10340 array.as_ref(),
10341 &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int16, true)), 2),
10342 &CastOptions {
10343 safe: false,
10344 ..Default::default()
10345 },
10346 );
10347 assert!(res.is_err());
10348 assert!(format!("{res:?}").contains("Can't cast value 2147483647 to type Int16"));
10349 }
10350
10351 #[test]
10352 fn test_cast_list_to_fsl_empty() {
10353 let inner_field = Arc::new(Field::new_list_field(DataType::Int32, true));
10354 let target_type = DataType::FixedSizeList(inner_field.clone(), 3);
10355 let expected = new_empty_array(&target_type);
10356
10357 let cases = [
10358 new_empty_array(&DataType::List(inner_field.clone())),
10359 new_empty_array(&DataType::LargeList(inner_field.clone())),
10360 new_empty_array(&DataType::ListView(inner_field.clone())),
10361 new_empty_array(&DataType::LargeListView(inner_field.clone())),
10362 make_list_array().slice(2, 0),
10364 make_large_list_array().slice(2, 0),
10365 ];
10366 for array in cases {
10367 assert!(can_cast_types(array.data_type(), &target_type));
10368 for safe in [true, false] {
10369 let options = CastOptions {
10370 safe,
10371 ..Default::default()
10372 };
10373 let actual = cast_with_options(array.as_ref(), &target_type, &options).unwrap();
10374 assert_eq!(expected.as_ref(), actual.as_ref());
10375 }
10376 }
10377 }
10378
10379 fn make_list_array() -> ArrayRef {
10380 Arc::new(ListArray::new(
10382 Field::new_list_field(DataType::Int32, true).into(),
10383 OffsetBuffer::from_lengths(vec![3, 3, 2]),
10384 Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
10385 None,
10386 ))
10387 }
10388
10389 fn make_large_list_array() -> ArrayRef {
10390 Arc::new(LargeListArray::new(
10392 Field::new_list_field(DataType::Int32, true).into(),
10393 OffsetBuffer::from_lengths(vec![3, 3, 2]),
10394 Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
10395 None,
10396 ))
10397 }
10398
10399 fn make_list_view_array() -> ArrayRef {
10400 Arc::new(ListViewArray::new(
10402 Field::new_list_field(DataType::Int32, true).into(),
10403 vec![0, 3, 6].into(),
10404 vec![3, 3, 2].into(),
10405 Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
10406 None,
10407 ))
10408 }
10409
10410 fn make_large_list_view_array() -> ArrayRef {
10411 Arc::new(LargeListViewArray::new(
10413 Field::new_list_field(DataType::Int32, true).into(),
10414 vec![0, 3, 6].into(),
10415 vec![3, 3, 2].into(),
10416 Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
10417 None,
10418 ))
10419 }
10420
10421 fn make_fixed_size_list_array() -> ArrayRef {
10422 Arc::new(FixedSizeListArray::new(
10424 Field::new_list_field(DataType::Int32, true).into(),
10425 4,
10426 Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
10427 None,
10428 ))
10429 }
10430
10431 fn make_fixed_size_list_array_for_large_list() -> ArrayRef {
10432 Arc::new(FixedSizeListArray::new(
10434 Field::new_list_field(DataType::Int64, true).into(),
10435 4,
10436 Arc::new(Int64Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
10437 None,
10438 ))
10439 }
10440
10441 #[test]
10442 fn test_utf8_cast_offsets() {
10443 let str_array = StringArray::from(vec!["a", "b", "c"]);
10445 let str_array = str_array.slice(1, 2);
10446
10447 let out = cast(&str_array, &DataType::LargeUtf8).unwrap();
10448
10449 let large_str_array = out.as_any().downcast_ref::<LargeStringArray>().unwrap();
10450 let strs = large_str_array.into_iter().flatten().collect::<Vec<_>>();
10451 assert_eq!(strs, &["b", "c"])
10452 }
10453
10454 #[test]
10455 fn test_list_cast_offsets() {
10456 let array1 = make_list_array().slice(1, 2);
10458 let array2 = make_list_array();
10459
10460 let dt = DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
10461 let out1 = cast(&array1, &dt).unwrap();
10462 let out2 = cast(&array2, &dt).unwrap();
10463
10464 assert_eq!(&out1, &out2.slice(1, 2))
10465 }
10466
10467 #[test]
10468 fn test_list_to_string() {
10469 fn assert_cast(array: &ArrayRef, expected: &[&str]) {
10470 assert!(can_cast_types(array.data_type(), &DataType::Utf8));
10471 let out = cast(array, &DataType::Utf8).unwrap();
10472 let out = out
10473 .as_string::<i32>()
10474 .into_iter()
10475 .flatten()
10476 .collect::<Vec<_>>();
10477 assert_eq!(out, expected);
10478
10479 assert!(can_cast_types(array.data_type(), &DataType::LargeUtf8));
10480 let out = cast(array, &DataType::LargeUtf8).unwrap();
10481 let out = out
10482 .as_string::<i64>()
10483 .into_iter()
10484 .flatten()
10485 .collect::<Vec<_>>();
10486 assert_eq!(out, expected);
10487
10488 assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
10489 let out = cast(array, &DataType::Utf8View).unwrap();
10490 let out = out
10491 .as_string_view()
10492 .into_iter()
10493 .flatten()
10494 .collect::<Vec<_>>();
10495 assert_eq!(out, expected);
10496 }
10497
10498 let array = Arc::new(ListArray::new(
10499 Field::new_list_field(DataType::Utf8, true).into(),
10500 OffsetBuffer::from_lengths(vec![3, 3, 2]),
10501 Arc::new(StringArray::from(vec![
10502 "a", "b", "c", "d", "e", "f", "g", "h",
10503 ])),
10504 None,
10505 )) as ArrayRef;
10506
10507 assert_cast(&array, &["[a, b, c]", "[d, e, f]", "[g, h]"]);
10508
10509 let array = make_list_array();
10510 assert_cast(&array, &["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
10511
10512 let array = make_large_list_array();
10513 assert_cast(&array, &["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
10514
10515 let array = make_list_view_array();
10516 assert_cast(&array, &["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
10517
10518 let array = make_large_list_view_array();
10519 assert_cast(&array, &["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
10520 }
10521
10522 #[test]
10523 #[cfg_attr(miri, ignore)] fn test_cast_f64_to_decimal128() {
10525 let decimal_type = DataType::Decimal128(18, 2);
10528 let array = Float64Array::from(vec![
10529 Some(0.0699999999),
10530 Some(0.0659999999),
10531 Some(0.0650000000),
10532 Some(0.0649999999),
10533 ]);
10534 let array = Arc::new(array) as ArrayRef;
10535 generate_cast_test_case!(
10536 &array,
10537 Decimal128Array,
10538 &decimal_type,
10539 vec![
10540 Some(7_i128), Some(7_i128), Some(7_i128), Some(6_i128), ]
10545 );
10546
10547 let decimal_type = DataType::Decimal128(18, 3);
10548 let array = Float64Array::from(vec![
10549 Some(0.0699999999),
10550 Some(0.0659999999),
10551 Some(0.0650000000),
10552 Some(0.0649999999),
10553 ]);
10554 let array = Arc::new(array) as ArrayRef;
10555 generate_cast_test_case!(
10556 &array,
10557 Decimal128Array,
10558 &decimal_type,
10559 vec![
10560 Some(70_i128), Some(66_i128), Some(65_i128), Some(65_i128), ]
10565 );
10566 }
10567
10568 #[test]
10569 fn test_cast_numeric_to_decimal128_overflow() {
10570 let array = Int64Array::from(vec![i64::MAX]);
10571 let array = Arc::new(array) as ArrayRef;
10572 let casted_array = cast_with_options(
10573 &array,
10574 &DataType::Decimal128(38, 30),
10575 &CastOptions {
10576 safe: true,
10577 format_options: FormatOptions::default(),
10578 },
10579 );
10580 assert!(casted_array.is_ok());
10581 assert!(casted_array.unwrap().is_null(0));
10582
10583 let casted_array = cast_with_options(
10584 &array,
10585 &DataType::Decimal128(38, 30),
10586 &CastOptions {
10587 safe: false,
10588 format_options: FormatOptions::default(),
10589 },
10590 );
10591 assert!(casted_array.is_err());
10592 }
10593
10594 #[test]
10595 fn test_cast_numeric_to_decimal256_overflow() {
10596 let array = Int64Array::from(vec![i64::MAX]);
10597 let array = Arc::new(array) as ArrayRef;
10598 let casted_array = cast_with_options(
10599 &array,
10600 &DataType::Decimal256(76, 76),
10601 &CastOptions {
10602 safe: true,
10603 format_options: FormatOptions::default(),
10604 },
10605 );
10606 assert!(casted_array.is_ok());
10607 assert!(casted_array.unwrap().is_null(0));
10608
10609 let casted_array = cast_with_options(
10610 &array,
10611 &DataType::Decimal256(76, 76),
10612 &CastOptions {
10613 safe: false,
10614 format_options: FormatOptions::default(),
10615 },
10616 );
10617 assert!(casted_array.is_err());
10618 }
10619
10620 #[test]
10621 fn test_cast_integer_to_decimal32_does_not_truncate() {
10622 let array = Int64Array::from(vec![5_000_000_000i64, 10_000_000_000, 42]);
10623 let safe = CastOptions {
10624 safe: true,
10625 format_options: FormatOptions::default(),
10626 };
10627 let unsafe_opts = CastOptions {
10628 safe: false,
10629 format_options: FormatOptions::default(),
10630 };
10631
10632 let result = cast_with_options(&array, &DataType::Decimal32(9, 0), &safe).unwrap();
10633 let result = result.as_primitive::<Decimal32Type>();
10634 assert!(
10635 result.is_null(0),
10636 "5e9 must not wrap to {}",
10637 result.value(0)
10638 );
10639 assert!(result.is_null(1));
10640 assert_eq!(result.value(2), 42);
10641
10642 let err = cast_with_options(&array, &DataType::Decimal32(9, 0), &unsafe_opts)
10643 .unwrap_err()
10644 .to_string();
10645 assert_eq!(
10646 err,
10647 "Cast error: Cannot cast to Decimal32(9, 0). Overflowing on 5000000000"
10648 );
10649
10650 let result = cast_with_options(&array, &DataType::Decimal128(9, 0), &safe).unwrap();
10651 let result = result.as_primitive::<Decimal128Type>();
10652 assert!(result.is_null(0));
10653 assert!(result.is_null(1));
10654 assert_eq!(result.value(2), 42);
10655 }
10656
10657 #[test]
10658 fn test_cast_integer_to_decimal32_scales_before_narrowing() {
10659 let array = Int64Array::from(vec![5_000_000_000i64]);
10660 let safe = CastOptions {
10661 safe: true,
10662 format_options: FormatOptions::default(),
10663 };
10664 let unsafe_opts = CastOptions {
10665 safe: false,
10666 format_options: FormatOptions::default(),
10667 };
10668 let data_type = DataType::Decimal32(9, -1);
10669
10670 let result = cast_with_options(&array, &data_type, &safe).unwrap();
10671 let result = result.as_primitive::<Decimal32Type>();
10672 assert_eq!(result.value(0), 500_000_000);
10673
10674 let result = cast_with_options(&array, &data_type, &unsafe_opts).unwrap();
10675 let result = result.as_primitive::<Decimal32Type>();
10676 assert_eq!(result.value(0), 500_000_000);
10677 }
10678
10679 #[test]
10680 fn test_cast_uint_to_decimal32_does_not_wrap() {
10681 let array = UInt32Array::from(vec![4_000_000_000u32]);
10682 let safe = CastOptions {
10683 safe: true,
10684 format_options: FormatOptions::default(),
10685 };
10686 let unsafe_opts = CastOptions {
10687 safe: false,
10688 format_options: FormatOptions::default(),
10689 };
10690
10691 let result = cast_with_options(&array, &DataType::Decimal32(9, 0), &safe).unwrap();
10692 let result = result.as_primitive::<Decimal32Type>();
10693 assert!(
10694 result.is_null(0),
10695 "u32 4e9 must not wrap to {}",
10696 result.value(0)
10697 );
10698
10699 let err = cast_with_options(&array, &DataType::Decimal32(9, 0), &unsafe_opts)
10700 .unwrap_err()
10701 .to_string();
10702 assert_eq!(
10703 err,
10704 "Cast error: Cannot cast to Decimal32(9, 0). Overflowing on 4000000000"
10705 );
10706
10707 let result = cast_with_options(&array, &DataType::Decimal128(9, 0), &safe).unwrap();
10708 assert!(result.is_null(0));
10709 assert!(cast_with_options(&array, &DataType::Decimal128(9, 0), &unsafe_opts).is_err());
10710 }
10711
10712 #[test]
10713 fn test_cast_uint64_max_to_decimal64_does_not_wrap() {
10714 let array = UInt64Array::from(vec![u64::MAX]);
10715 let unsafe_opts = CastOptions {
10716 safe: false,
10717 format_options: FormatOptions::default(),
10718 };
10719
10720 let err = cast_with_options(&array, &DataType::Decimal64(18, 0), &unsafe_opts)
10721 .unwrap_err()
10722 .to_string();
10723 assert_eq!(
10724 err,
10725 "Cast error: Cannot cast to Decimal64(18, 0). Overflowing on 18446744073709551615"
10726 );
10727
10728 assert!(cast_with_options(&array, &DataType::Decimal128(18, 0), &unsafe_opts).is_err());
10729 }
10730
10731 #[test]
10732 fn test_cast_floating_point_to_decimal128_precision_overflow() {
10733 let array = Float64Array::from(vec![1.1]);
10734 let array = Arc::new(array) as ArrayRef;
10735 let casted_array = cast_with_options(
10736 &array,
10737 &DataType::Decimal128(2, 2),
10738 &CastOptions {
10739 safe: true,
10740 format_options: FormatOptions::default(),
10741 },
10742 );
10743 assert!(casted_array.is_ok());
10744 assert!(casted_array.unwrap().is_null(0));
10745
10746 let casted_array = cast_with_options(
10747 &array,
10748 &DataType::Decimal128(2, 2),
10749 &CastOptions {
10750 safe: false,
10751 format_options: FormatOptions::default(),
10752 },
10753 );
10754 let err = casted_array.unwrap_err().to_string();
10755 let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal128 of precision 2. Max is 0.99";
10756 assert!(
10757 err.contains(expected_error),
10758 "did not find expected error '{expected_error}' in actual error '{err}'"
10759 );
10760 }
10761
10762 #[test]
10763 #[cfg_attr(miri, ignore)] fn test_cast_float16_to_decimal128_precision_overflow() {
10765 let array = Float16Array::from(vec![f16::from_f32(1.1)]);
10766 let array = Arc::new(array) as ArrayRef;
10767 let casted_array = cast_with_options(
10768 &array,
10769 &DataType::Decimal128(2, 2),
10770 &CastOptions {
10771 safe: true,
10772 format_options: FormatOptions::default(),
10773 },
10774 );
10775 assert!(casted_array.is_ok());
10776 assert!(casted_array.unwrap().is_null(0));
10777
10778 let casted_array = cast_with_options(
10779 &array,
10780 &DataType::Decimal128(2, 2),
10781 &CastOptions {
10782 safe: false,
10783 format_options: FormatOptions::default(),
10784 },
10785 );
10786 let err = casted_array.unwrap_err().to_string();
10787 let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal128 of precision 2. Max is 0.99";
10788 assert_eq!(err, expected_error);
10789 }
10790
10791 #[test]
10792 #[cfg_attr(miri, ignore)] fn test_cast_float16_to_decimal256_precision_overflow() {
10794 let array = Float16Array::from(vec![f16::from_f32(1.1)]);
10795 let array = Arc::new(array) as ArrayRef;
10796 let casted_array = cast_with_options(
10797 &array,
10798 &DataType::Decimal256(2, 2),
10799 &CastOptions {
10800 safe: true,
10801 format_options: FormatOptions::default(),
10802 },
10803 );
10804 assert!(casted_array.is_ok());
10805 assert!(casted_array.unwrap().is_null(0));
10806
10807 let casted_array = cast_with_options(
10808 &array,
10809 &DataType::Decimal256(2, 2),
10810 &CastOptions {
10811 safe: false,
10812 format_options: FormatOptions::default(),
10813 },
10814 );
10815 let err = casted_array.unwrap_err().to_string();
10816 let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal256 of precision 2. Max is 0.99";
10817 assert_eq!(err, expected_error);
10818 }
10819
10820 #[test]
10821 #[cfg_attr(miri, ignore)] fn test_cast_float16_to_decimal128_non_finite() {
10823 let array = Float16Array::from(vec![f16::NAN, f16::INFINITY, f16::NEG_INFINITY]);
10824 let array = Arc::new(array) as ArrayRef;
10825 let casted_array = cast_with_options(
10826 &array,
10827 &DataType::Decimal128(38, 2),
10828 &CastOptions {
10829 safe: true,
10830 format_options: FormatOptions::default(),
10831 },
10832 )
10833 .unwrap();
10834
10835 assert!(casted_array.is_null(0));
10836 assert!(casted_array.is_null(1));
10837 assert!(casted_array.is_null(2));
10838
10839 let casted_array = cast_with_options(
10840 &array,
10841 &DataType::Decimal128(38, 2),
10842 &CastOptions {
10843 safe: false,
10844 format_options: FormatOptions::default(),
10845 },
10846 );
10847 let err = casted_array.unwrap_err().to_string();
10848 let expected_error = "Cannot cast to Decimal128(38, 2)";
10849 assert!(
10850 err.contains(expected_error),
10851 "did not find expected error '{expected_error}' in actual error '{err}'"
10852 );
10853 }
10854
10855 #[test]
10856 fn test_cast_floating_point_to_decimal256_precision_overflow() {
10857 let array = Float64Array::from(vec![1.1]);
10858 let array = Arc::new(array) as ArrayRef;
10859 let casted_array = cast_with_options(
10860 &array,
10861 &DataType::Decimal256(2, 2),
10862 &CastOptions {
10863 safe: true,
10864 format_options: FormatOptions::default(),
10865 },
10866 );
10867 assert!(casted_array.is_ok());
10868 assert!(casted_array.unwrap().is_null(0));
10869
10870 let casted_array = cast_with_options(
10871 &array,
10872 &DataType::Decimal256(2, 2),
10873 &CastOptions {
10874 safe: false,
10875 format_options: FormatOptions::default(),
10876 },
10877 );
10878 let err = casted_array.unwrap_err().to_string();
10879 let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal256 of precision 2. Max is 0.99";
10880 assert_eq!(err, expected_error);
10881 }
10882
10883 #[test]
10884 fn test_cast_floating_point_to_decimal128_overflow() {
10885 let array = Float64Array::from(vec![f64::MAX]);
10886 let array = Arc::new(array) as ArrayRef;
10887 let casted_array = cast_with_options(
10888 &array,
10889 &DataType::Decimal128(38, 30),
10890 &CastOptions {
10891 safe: true,
10892 format_options: FormatOptions::default(),
10893 },
10894 );
10895 assert!(casted_array.is_ok());
10896 assert!(casted_array.unwrap().is_null(0));
10897
10898 let casted_array = cast_with_options(
10899 &array,
10900 &DataType::Decimal128(38, 30),
10901 &CastOptions {
10902 safe: false,
10903 format_options: FormatOptions::default(),
10904 },
10905 );
10906 let err = casted_array.unwrap_err().to_string();
10907 let expected_error = "Cast error: Cannot cast to Decimal128(38, 30)";
10908 assert!(
10909 err.contains(expected_error),
10910 "did not find expected error '{expected_error}' in actual error '{err}'"
10911 );
10912 }
10913
10914 #[test]
10915 fn test_cast_floating_point_to_decimal256_overflow() {
10916 let array = Float64Array::from(vec![f64::MAX]);
10917 let array = Arc::new(array) as ArrayRef;
10918 let casted_array = cast_with_options(
10919 &array,
10920 &DataType::Decimal256(76, 50),
10921 &CastOptions {
10922 safe: true,
10923 format_options: FormatOptions::default(),
10924 },
10925 );
10926 assert!(casted_array.is_ok());
10927 assert!(casted_array.unwrap().is_null(0));
10928
10929 let casted_array = cast_with_options(
10930 &array,
10931 &DataType::Decimal256(76, 50),
10932 &CastOptions {
10933 safe: false,
10934 format_options: FormatOptions::default(),
10935 },
10936 );
10937 let err = casted_array.unwrap_err().to_string();
10938 let expected_error = "Cast error: Cannot cast to Decimal256(76, 50)";
10939 assert!(
10940 err.contains(expected_error),
10941 "did not find expected error '{expected_error}' in actual error '{err}'"
10942 );
10943 }
10944 #[test]
10945 fn test_cast_decimal256_to_f64_no_overflow() {
10946 let array = vec![Some(i256::MAX)];
10948 let array = create_decimal256_array(array, 76, 2).unwrap();
10949 let array = Arc::new(array) as ArrayRef;
10950
10951 let result = cast(&array, &DataType::Float64).unwrap();
10952 let result = result.as_primitive::<Float64Type>();
10953 assert!(result.value(0).is_finite());
10954 assert!(result.value(0) > 0.0); let array = vec![Some(i256::MIN)];
10958 let array = create_decimal256_array(array, 76, 2).unwrap();
10959 let array = Arc::new(array) as ArrayRef;
10960
10961 let result = cast(&array, &DataType::Float64).unwrap();
10962 let result = result.as_primitive::<Float64Type>();
10963 assert!(result.value(0).is_finite());
10964 assert!(result.value(0) < 0.0); }
10966
10967 #[test]
10968 fn test_cast_decimal128_to_decimal128_negative_scale() {
10969 let input_type = DataType::Decimal128(20, 0);
10970 let output_type = DataType::Decimal128(20, -1);
10971 assert!(can_cast_types(&input_type, &output_type));
10972 let array = vec![Some(1123450), Some(2123455), Some(3123456), None];
10973 let input_decimal_array = create_decimal128_array(array, 20, 0).unwrap();
10974 let array = Arc::new(input_decimal_array) as ArrayRef;
10975 generate_cast_test_case!(
10976 &array,
10977 Decimal128Array,
10978 &output_type,
10979 vec![
10980 Some(112345_i128),
10981 Some(212346_i128),
10982 Some(312346_i128),
10983 None
10984 ]
10985 );
10986
10987 let casted_array = cast(&array, &output_type).unwrap();
10988 let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10989
10990 assert_eq!("1123450", decimal_arr.value_as_string(0));
10991 assert_eq!("2123460", decimal_arr.value_as_string(1));
10992 assert_eq!("3123460", decimal_arr.value_as_string(2));
10993 }
10994
10995 #[test]
10996 fn decimal128_min_max_to_f64() {
10997 let min128 = i128::MIN;
10999 let max128 = i128::MAX;
11000 assert_eq!(min128 as f64, min128 as f64);
11001 assert_eq!(max128 as f64, max128 as f64);
11002 }
11003
11004 #[test]
11005 fn test_cast_numeric_to_decimal128_negative() {
11006 let decimal_type = DataType::Decimal128(38, -1);
11007 let array = Arc::new(Int32Array::from(vec![
11008 Some(1123456),
11009 Some(2123456),
11010 Some(3123456),
11011 ])) as ArrayRef;
11012
11013 let casted_array = cast(&array, &decimal_type).unwrap();
11014 let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
11015
11016 assert_eq!("1123450", decimal_arr.value_as_string(0));
11017 assert_eq!("2123450", decimal_arr.value_as_string(1));
11018 assert_eq!("3123450", decimal_arr.value_as_string(2));
11019
11020 let array = Arc::new(Float32Array::from(vec![
11021 Some(1123.456),
11022 Some(2123.456),
11023 Some(3123.456),
11024 ])) as ArrayRef;
11025
11026 let casted_array = cast(&array, &decimal_type).unwrap();
11027 let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
11028
11029 assert_eq!("1120", decimal_arr.value_as_string(0));
11030 assert_eq!("2120", decimal_arr.value_as_string(1));
11031 assert_eq!("3120", decimal_arr.value_as_string(2));
11032 }
11033
11034 #[test]
11035 fn test_cast_decimal128_to_decimal128_negative() {
11036 let input_type = DataType::Decimal128(10, -1);
11037 let output_type = DataType::Decimal128(10, -2);
11038 assert!(can_cast_types(&input_type, &output_type));
11039 let array = vec![Some(123)];
11040 let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
11041 let array = Arc::new(input_decimal_array) as ArrayRef;
11042 generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(12_i128),]);
11043
11044 let casted_array = cast(&array, &output_type).unwrap();
11045 let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
11046
11047 assert_eq!("1200", decimal_arr.value_as_string(0));
11048
11049 let array = vec![Some(125)];
11050 let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
11051 let array = Arc::new(input_decimal_array) as ArrayRef;
11052 generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(13_i128),]);
11053
11054 let casted_array = cast(&array, &output_type).unwrap();
11055 let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
11056
11057 assert_eq!("1300", decimal_arr.value_as_string(0));
11058 }
11059
11060 #[test]
11061 fn test_cast_decimal128_to_decimal256_negative() {
11062 let input_type = DataType::Decimal128(10, 3);
11063 let output_type = DataType::Decimal256(10, 5);
11064 assert!(can_cast_types(&input_type, &output_type));
11065 let array = vec![Some(123456), Some(-123456)];
11066 let input_decimal_array = create_decimal128_array(array, 10, 3).unwrap();
11067 let array = Arc::new(input_decimal_array) as ArrayRef;
11068
11069 let hundred = i256::from_i128(100);
11070 generate_cast_test_case!(
11071 &array,
11072 Decimal256Array,
11073 &output_type,
11074 vec![
11075 Some(i256::from_i128(123456).mul_wrapping(hundred)),
11076 Some(i256::from_i128(-123456).mul_wrapping(hundred))
11077 ]
11078 );
11079 }
11080
11081 #[test]
11082 fn test_parse_decimal_and_format() {
11083 assert_eq!(
11084 Decimal128Type::format_decimal(
11085 parse_decimal::<Decimal128Type>("123.45", 38, 2).unwrap(),
11086 38,
11087 2,
11088 ),
11089 "123.45"
11090 );
11091 assert_eq!(
11092 Decimal128Type::format_decimal(
11093 parse_decimal::<Decimal128Type>("12345", 38, 2).unwrap(),
11094 38,
11095 2,
11096 ),
11097 "12345.00"
11098 );
11099 assert_eq!(
11100 Decimal128Type::format_decimal(
11101 parse_decimal::<Decimal128Type>("0.12345", 38, 2).unwrap(),
11102 38,
11103 2,
11104 ),
11105 "0.12"
11106 );
11107 assert_eq!(
11108 Decimal128Type::format_decimal(
11109 parse_decimal::<Decimal128Type>(".12345", 38, 2).unwrap(),
11110 38,
11111 2,
11112 ),
11113 "0.12"
11114 );
11115 assert_eq!(
11116 Decimal128Type::format_decimal(
11117 parse_decimal::<Decimal128Type>(".1265", 38, 2).unwrap(),
11118 38,
11119 2,
11120 ),
11121 "0.13"
11122 );
11123 assert_eq!(
11124 Decimal128Type::format_decimal(
11125 parse_decimal::<Decimal128Type>(".1265", 38, 2).unwrap(),
11126 38,
11127 2,
11128 ),
11129 "0.13"
11130 );
11131
11132 assert_eq!(
11133 Decimal256Type::format_decimal(
11134 parse_decimal::<Decimal256Type>("123.45", 76, 3).unwrap(),
11135 38,
11136 3,
11137 ),
11138 "123.450"
11139 );
11140 assert_eq!(
11141 Decimal256Type::format_decimal(
11142 parse_decimal::<Decimal256Type>("12345", 76, 3).unwrap(),
11143 38,
11144 3,
11145 ),
11146 "12345.000"
11147 );
11148 assert_eq!(
11149 Decimal256Type::format_decimal(
11150 parse_decimal::<Decimal256Type>("0.12345", 76, 3).unwrap(),
11151 38,
11152 3,
11153 ),
11154 "0.123"
11155 );
11156 assert_eq!(
11157 Decimal256Type::format_decimal(
11158 parse_decimal::<Decimal256Type>(".12345", 76, 3).unwrap(),
11159 38,
11160 3,
11161 ),
11162 "0.123"
11163 );
11164 assert_eq!(
11165 Decimal256Type::format_decimal(
11166 parse_decimal::<Decimal256Type>(".1265", 76, 3).unwrap(),
11167 38,
11168 3,
11169 ),
11170 "0.127"
11171 );
11172 }
11173
11174 fn test_cast_string_to_decimal(array: ArrayRef) {
11175 let output_type = DataType::Decimal128(38, 2);
11177 assert!(can_cast_types(array.data_type(), &output_type));
11178
11179 let casted_array = cast(&array, &output_type).unwrap();
11180 let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
11181
11182 assert_eq!("123.45", decimal_arr.value_as_string(0));
11183 assert_eq!("1.23", decimal_arr.value_as_string(1));
11184 assert_eq!("0.12", decimal_arr.value_as_string(2));
11185 assert_eq!("0.13", decimal_arr.value_as_string(3));
11186 assert_eq!("1.26", decimal_arr.value_as_string(4));
11187 assert_eq!("12345.00", decimal_arr.value_as_string(5));
11188 assert_eq!("12345.00", decimal_arr.value_as_string(6));
11189 assert_eq!("0.12", decimal_arr.value_as_string(7));
11190 assert_eq!("12.23", decimal_arr.value_as_string(8));
11191 assert!(decimal_arr.is_null(9));
11192 assert!(decimal_arr.is_null(10));
11193 assert!(decimal_arr.is_null(11));
11194 assert!(decimal_arr.is_null(12));
11195 assert_eq!("-1.23", decimal_arr.value_as_string(13));
11196 assert_eq!("-1.24", decimal_arr.value_as_string(14));
11197 assert_eq!("0.00", decimal_arr.value_as_string(15));
11198 assert_eq!("-123.00", decimal_arr.value_as_string(16));
11199 assert_eq!("-123.23", decimal_arr.value_as_string(17));
11200 assert_eq!("-0.12", decimal_arr.value_as_string(18));
11201 assert_eq!("1.23", decimal_arr.value_as_string(19));
11202 assert_eq!("1.24", decimal_arr.value_as_string(20));
11203 assert_eq!("0.00", decimal_arr.value_as_string(21));
11204 assert_eq!("123.00", decimal_arr.value_as_string(22));
11205 assert_eq!("123.23", decimal_arr.value_as_string(23));
11206 assert_eq!("0.12", decimal_arr.value_as_string(24));
11207 assert!(decimal_arr.is_null(25));
11208 assert!(decimal_arr.is_null(26));
11209 assert!(decimal_arr.is_null(27));
11210 assert_eq!("0.00", decimal_arr.value_as_string(28));
11211 assert_eq!("0.00", decimal_arr.value_as_string(29));
11212 assert_eq!("12345.00", decimal_arr.value_as_string(30));
11213 assert_eq!(decimal_arr.len(), 31);
11214
11215 let output_type = DataType::Decimal256(76, 3);
11217 assert!(can_cast_types(array.data_type(), &output_type));
11218
11219 let casted_array = cast(&array, &output_type).unwrap();
11220 let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
11221
11222 assert_eq!("123.450", decimal_arr.value_as_string(0));
11223 assert_eq!("1.235", decimal_arr.value_as_string(1));
11224 assert_eq!("0.123", decimal_arr.value_as_string(2));
11225 assert_eq!("0.127", decimal_arr.value_as_string(3));
11226 assert_eq!("1.263", decimal_arr.value_as_string(4));
11227 assert_eq!("12345.000", decimal_arr.value_as_string(5));
11228 assert_eq!("12345.000", decimal_arr.value_as_string(6));
11229 assert_eq!("0.123", decimal_arr.value_as_string(7));
11230 assert_eq!("12.234", decimal_arr.value_as_string(8));
11231 assert!(decimal_arr.is_null(9));
11232 assert!(decimal_arr.is_null(10));
11233 assert!(decimal_arr.is_null(11));
11234 assert!(decimal_arr.is_null(12));
11235 assert_eq!("-1.235", decimal_arr.value_as_string(13));
11236 assert_eq!("-1.236", decimal_arr.value_as_string(14));
11237 assert_eq!("0.000", decimal_arr.value_as_string(15));
11238 assert_eq!("-123.000", decimal_arr.value_as_string(16));
11239 assert_eq!("-123.234", decimal_arr.value_as_string(17));
11240 assert_eq!("-0.123", decimal_arr.value_as_string(18));
11241 assert_eq!("1.235", decimal_arr.value_as_string(19));
11242 assert_eq!("1.236", decimal_arr.value_as_string(20));
11243 assert_eq!("0.000", decimal_arr.value_as_string(21));
11244 assert_eq!("123.000", decimal_arr.value_as_string(22));
11245 assert_eq!("123.234", decimal_arr.value_as_string(23));
11246 assert_eq!("0.123", decimal_arr.value_as_string(24));
11247 assert!(decimal_arr.is_null(25));
11248 assert!(decimal_arr.is_null(26));
11249 assert!(decimal_arr.is_null(27));
11250 assert_eq!("0.000", decimal_arr.value_as_string(28));
11251 assert_eq!("0.000", decimal_arr.value_as_string(29));
11252 assert_eq!("12345.000", decimal_arr.value_as_string(30));
11253 assert_eq!(decimal_arr.len(), 31);
11254 }
11255
11256 #[test]
11257 fn test_cast_utf8_to_decimal() {
11258 let str_array = StringArray::from(vec![
11259 Some("123.45"),
11260 Some("1.2345"),
11261 Some("0.12345"),
11262 Some("0.1267"),
11263 Some("1.263"),
11264 Some("12345.0"),
11265 Some("12345"),
11266 Some("000.123"),
11267 Some("12.234000"),
11268 None,
11269 Some(""),
11270 Some(" "),
11271 None,
11272 Some("-1.23499999"),
11273 Some("-1.23599999"),
11274 Some("-0.00001"),
11275 Some("-123"),
11276 Some("-123.234000"),
11277 Some("-000.123"),
11278 Some("+1.23499999"),
11279 Some("+1.23599999"),
11280 Some("+0.00001"),
11281 Some("+123"),
11282 Some("+123.234000"),
11283 Some("+000.123"),
11284 Some("1.-23499999"),
11285 Some("-1.-23499999"),
11286 Some("--1.23499999"),
11287 Some("0"),
11288 Some("000.000"),
11289 Some("0000000000000000012345.000"),
11290 ]);
11291 let array = Arc::new(str_array) as ArrayRef;
11292
11293 test_cast_string_to_decimal(array);
11294
11295 let test_cases = [
11296 (None, None),
11297 (Some(""), None),
11298 (Some(" "), None),
11299 (Some("0"), Some("0")),
11300 (Some("000.000"), Some("0")),
11301 (Some("12345"), Some("12345")),
11302 (Some("000000000000000000000000000012345"), Some("12345")),
11303 (Some("-123"), Some("-123")),
11304 (Some("+123"), Some("123")),
11305 ];
11306 let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
11307 let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
11308
11309 let array = Arc::new(StringArray::from(inputs)) as ArrayRef;
11310 test_cast_string_to_decimal_scale_zero(array, &expected);
11311 }
11312
11313 #[test]
11314 fn test_cast_large_utf8_to_decimal() {
11315 let str_array = LargeStringArray::from(vec![
11316 Some("123.45"),
11317 Some("1.2345"),
11318 Some("0.12345"),
11319 Some("0.1267"),
11320 Some("1.263"),
11321 Some("12345.0"),
11322 Some("12345"),
11323 Some("000.123"),
11324 Some("12.234000"),
11325 None,
11326 Some(""),
11327 Some(" "),
11328 None,
11329 Some("-1.23499999"),
11330 Some("-1.23599999"),
11331 Some("-0.00001"),
11332 Some("-123"),
11333 Some("-123.234000"),
11334 Some("-000.123"),
11335 Some("+1.23499999"),
11336 Some("+1.23599999"),
11337 Some("+0.00001"),
11338 Some("+123"),
11339 Some("+123.234000"),
11340 Some("+000.123"),
11341 Some("1.-23499999"),
11342 Some("-1.-23499999"),
11343 Some("--1.23499999"),
11344 Some("0"),
11345 Some("000.000"),
11346 Some("0000000000000000012345.000"),
11347 ]);
11348 let array = Arc::new(str_array) as ArrayRef;
11349
11350 test_cast_string_to_decimal(array);
11351
11352 let test_cases = [
11353 (None, None),
11354 (Some(""), None),
11355 (Some(" "), None),
11356 (Some("0"), Some("0")),
11357 (Some("000.000"), Some("0")),
11358 (Some("12345"), Some("12345")),
11359 (Some("000000000000000000000000000012345"), Some("12345")),
11360 (Some("-123"), Some("-123")),
11361 (Some("+123"), Some("123")),
11362 ];
11363 let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
11364 let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
11365
11366 let array = Arc::new(LargeStringArray::from(inputs)) as ArrayRef;
11367 test_cast_string_to_decimal_scale_zero(array, &expected);
11368 }
11369
11370 fn test_cast_string_to_decimal_scale_zero(
11371 array: ArrayRef,
11372 expected_as_string: &[Option<&str>],
11373 ) {
11374 let output_type = DataType::Decimal128(38, 0);
11376 assert!(can_cast_types(array.data_type(), &output_type));
11377 let casted_array = cast(&array, &output_type).unwrap();
11378 let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
11379 assert_decimal_array_contents(decimal_arr, expected_as_string);
11380
11381 let output_type = DataType::Decimal256(76, 0);
11383 assert!(can_cast_types(array.data_type(), &output_type));
11384 let casted_array = cast(&array, &output_type).unwrap();
11385 let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
11386 assert_decimal_array_contents(decimal_arr, expected_as_string);
11387 }
11388
11389 fn assert_decimal_array_contents<T>(
11390 array: &PrimitiveArray<T>,
11391 expected_as_string: &[Option<&str>],
11392 ) where
11393 T: DecimalType + ArrowPrimitiveType,
11394 {
11395 assert_eq!(array.len(), expected_as_string.len());
11396 for (i, expected) in expected_as_string.iter().enumerate() {
11397 let actual = if array.is_null(i) {
11398 None
11399 } else {
11400 Some(array.value_as_string(i))
11401 };
11402 let actual = actual.as_ref().map(|s| s.as_ref());
11403 assert_eq!(*expected, actual, "Expected at position {i}");
11404 }
11405 }
11406
11407 #[test]
11408 fn test_cast_invalid_utf8_to_decimal() {
11409 let str_array = StringArray::from(vec!["4.4.5", ". 0.123"]);
11410 let array = Arc::new(str_array) as ArrayRef;
11411
11412 let output_type = DataType::Decimal128(38, 2);
11414 let casted_array = cast(&array, &output_type).unwrap();
11415 assert!(casted_array.is_null(0));
11416 assert!(casted_array.is_null(1));
11417
11418 let output_type = DataType::Decimal256(76, 2);
11419 let casted_array = cast(&array, &output_type).unwrap();
11420 assert!(casted_array.is_null(0));
11421 assert!(casted_array.is_null(1));
11422
11423 let output_type = DataType::Decimal128(38, 2);
11425 let str_array = StringArray::from(vec!["4.4.5"]);
11426 let array = Arc::new(str_array) as ArrayRef;
11427 let option = CastOptions {
11428 safe: false,
11429 format_options: FormatOptions::default(),
11430 };
11431 let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
11432 assert!(
11433 casted_err
11434 .to_string()
11435 .contains("Cannot cast string '4.4.5' to value of Decimal128(38, 2) type")
11436 );
11437
11438 let str_array = StringArray::from(vec![". 0.123"]);
11439 let array = Arc::new(str_array) as ArrayRef;
11440 let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
11441 assert!(
11442 casted_err
11443 .to_string()
11444 .contains("Cannot cast string '. 0.123' to value of Decimal128(38, 2) type")
11445 );
11446
11447 let str_array = StringArray::from(vec![""]);
11448 let array = Arc::new(str_array) as ArrayRef;
11449 let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
11450 assert!(
11451 casted_err
11452 .to_string()
11453 .contains("Cannot cast string '' to value of Decimal128(38, 2) type")
11454 );
11455 }
11456
11457 fn test_cast_string_to_decimal128_overflow(overflow_array: ArrayRef) {
11458 let output_type = DataType::Decimal128(38, 2);
11459 let casted_array = cast(&overflow_array, &output_type).unwrap();
11460 let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
11461
11462 assert!(decimal_arr.is_null(0));
11463 assert!(decimal_arr.is_null(1));
11464 assert!(decimal_arr.is_null(2));
11465 assert_eq!(
11466 "999999999999999999999999999999999999.99",
11467 decimal_arr.value_as_string(3)
11468 );
11469 assert_eq!(
11470 "100000000000000000000000000000000000.00",
11471 decimal_arr.value_as_string(4)
11472 );
11473 }
11474
11475 #[test]
11476 fn test_cast_string_to_decimal128_precision_overflow() {
11477 let array = StringArray::from(vec!["1000".to_string()]);
11478 let array = Arc::new(array) as ArrayRef;
11479 let casted_array = cast_with_options(
11480 &array,
11481 &DataType::Decimal128(10, 8),
11482 &CastOptions {
11483 safe: true,
11484 format_options: FormatOptions::default(),
11485 },
11486 );
11487 assert!(casted_array.is_ok());
11488 assert!(casted_array.unwrap().is_null(0));
11489
11490 let err = cast_with_options(
11491 &array,
11492 &DataType::Decimal128(10, 8),
11493 &CastOptions {
11494 safe: false,
11495 format_options: FormatOptions::default(),
11496 },
11497 );
11498 assert_eq!(
11499 "Cast error: Cannot cast string '1000' to value of Decimal128(10, 8) type: value does not fit",
11500 err.unwrap_err().to_string()
11501 );
11502 }
11503
11504 #[test]
11505 fn test_cast_utf8_to_decimal128_overflow() {
11506 let overflow_str_array = StringArray::from(vec![
11507 i128::MAX.to_string(),
11508 i128::MIN.to_string(),
11509 "99999999999999999999999999999999999999".to_string(),
11510 "999999999999999999999999999999999999.99".to_string(),
11511 "99999999999999999999999999999999999.999".to_string(),
11512 ]);
11513 let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
11514
11515 test_cast_string_to_decimal128_overflow(overflow_array);
11516 }
11517
11518 #[test]
11519 fn test_cast_large_utf8_to_decimal128_overflow() {
11520 let overflow_str_array = LargeStringArray::from(vec![
11521 i128::MAX.to_string(),
11522 i128::MIN.to_string(),
11523 "99999999999999999999999999999999999999".to_string(),
11524 "999999999999999999999999999999999999.99".to_string(),
11525 "99999999999999999999999999999999999.999".to_string(),
11526 ]);
11527 let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
11528
11529 test_cast_string_to_decimal128_overflow(overflow_array);
11530 }
11531
11532 fn test_cast_string_to_decimal256_overflow(overflow_array: ArrayRef) {
11533 let output_type = DataType::Decimal256(76, 2);
11534 let casted_array = cast(&overflow_array, &output_type).unwrap();
11535 let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
11536
11537 assert_eq!(
11538 "170141183460469231731687303715884105727.00",
11539 decimal_arr.value_as_string(0)
11540 );
11541 assert_eq!(
11542 "-170141183460469231731687303715884105728.00",
11543 decimal_arr.value_as_string(1)
11544 );
11545 assert_eq!(
11546 "99999999999999999999999999999999999999.00",
11547 decimal_arr.value_as_string(2)
11548 );
11549 assert_eq!(
11550 "999999999999999999999999999999999999.99",
11551 decimal_arr.value_as_string(3)
11552 );
11553 assert_eq!(
11554 "100000000000000000000000000000000000.00",
11555 decimal_arr.value_as_string(4)
11556 );
11557 assert!(decimal_arr.is_null(5));
11558 assert!(decimal_arr.is_null(6));
11559 }
11560
11561 #[test]
11562 fn test_cast_string_to_decimal256_precision_overflow() {
11563 let array = StringArray::from(vec!["1000".to_string()]);
11564 let array = Arc::new(array) as ArrayRef;
11565 let casted_array = cast_with_options(
11566 &array,
11567 &DataType::Decimal256(10, 8),
11568 &CastOptions {
11569 safe: true,
11570 format_options: FormatOptions::default(),
11571 },
11572 );
11573 assert!(casted_array.is_ok());
11574 assert!(casted_array.unwrap().is_null(0));
11575
11576 let err = cast_with_options(
11577 &array,
11578 &DataType::Decimal256(10, 8),
11579 &CastOptions {
11580 safe: false,
11581 format_options: FormatOptions::default(),
11582 },
11583 );
11584 assert_eq!(
11585 "Cast error: Cannot cast string '1000' to value of Decimal256(10, 8) type: value does not fit",
11586 err.unwrap_err().to_string()
11587 );
11588 }
11589
11590 #[test]
11591 fn test_cast_utf8_to_decimal256_overflow() {
11592 let overflow_str_array = StringArray::from(vec![
11593 i128::MAX.to_string(),
11594 i128::MIN.to_string(),
11595 "99999999999999999999999999999999999999".to_string(),
11596 "999999999999999999999999999999999999.99".to_string(),
11597 "99999999999999999999999999999999999.999".to_string(),
11598 i256::MAX.to_string(),
11599 i256::MIN.to_string(),
11600 ]);
11601 let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
11602
11603 test_cast_string_to_decimal256_overflow(overflow_array);
11604 }
11605
11606 #[test]
11607 fn test_cast_large_utf8_to_decimal256_overflow() {
11608 let overflow_str_array = LargeStringArray::from(vec![
11609 i128::MAX.to_string(),
11610 i128::MIN.to_string(),
11611 "99999999999999999999999999999999999999".to_string(),
11612 "999999999999999999999999999999999999.99".to_string(),
11613 "99999999999999999999999999999999999.999".to_string(),
11614 i256::MAX.to_string(),
11615 i256::MIN.to_string(),
11616 ]);
11617 let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
11618
11619 test_cast_string_to_decimal256_overflow(overflow_array);
11620 }
11621
11622 #[test]
11623 fn test_cast_outside_supported_range_for_nanoseconds() {
11624 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";
11625
11626 let array = StringArray::from(vec![Some("1650-01-01 01:01:01.000001")]);
11627
11628 let cast_options = CastOptions {
11629 safe: false,
11630 format_options: FormatOptions::default(),
11631 };
11632
11633 let result =
11634 cast_string_to_timestamp::<i32, TimestampNanosecondType>(&array, None, &cast_options);
11635
11636 let err = result.unwrap_err();
11637 assert_eq!(
11638 err.to_string(),
11639 format!(
11640 "Cast error: Overflow converting {} to Nanosecond. {}",
11641 array.value(0),
11642 EXPECTED_ERROR_MESSAGE
11643 )
11644 );
11645 }
11646
11647 #[test]
11648 fn test_cast_date32_to_timestamp() {
11649 let a = Date32Array::from(vec![Some(18628), Some(18993), None]); let array = Arc::new(a) as ArrayRef;
11651 let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
11652 let c = b.as_primitive::<TimestampSecondType>();
11653 assert_eq!(1609459200, c.value(0));
11654 assert_eq!(1640995200, c.value(1));
11655 assert!(c.is_null(2));
11656 }
11657
11658 #[test]
11659 fn test_cast_date32_to_timestamp_ms() {
11660 let a = Date32Array::from(vec![Some(18628), Some(18993), None]); let array = Arc::new(a) as ArrayRef;
11662 let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
11663 let c = b
11664 .as_any()
11665 .downcast_ref::<TimestampMillisecondArray>()
11666 .unwrap();
11667 assert_eq!(1609459200000, c.value(0));
11668 assert_eq!(1640995200000, c.value(1));
11669 assert!(c.is_null(2));
11670 }
11671
11672 #[test]
11673 fn test_cast_date32_to_timestamp_us() {
11674 let a = Date32Array::from(vec![Some(18628), Some(18993), None]); let array = Arc::new(a) as ArrayRef;
11676 let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
11677 let c = b
11678 .as_any()
11679 .downcast_ref::<TimestampMicrosecondArray>()
11680 .unwrap();
11681 assert_eq!(1609459200000000, c.value(0));
11682 assert_eq!(1640995200000000, c.value(1));
11683 assert!(c.is_null(2));
11684 }
11685
11686 #[test]
11687 fn test_cast_date32_to_timestamp_ns() {
11688 let a = Date32Array::from(vec![Some(18628), Some(18993), None]); let array = Arc::new(a) as ArrayRef;
11690 let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
11691 let c = b
11692 .as_any()
11693 .downcast_ref::<TimestampNanosecondArray>()
11694 .unwrap();
11695 assert_eq!(1609459200000000000, c.value(0));
11696 assert_eq!(1640995200000000000, c.value(1));
11697 assert!(c.is_null(2));
11698 }
11699
11700 #[test]
11701 fn test_cast_date32_to_timestamp_us_overflow() {
11702 const MAX_DAYS_MICROS: i32 = (i64::MAX / MICROSECONDS_IN_DAY) as i32;
11703 let a = Date32Array::from(vec![Some(MAX_DAYS_MICROS), Some(MAX_DAYS_MICROS + 1), None]);
11704 let array = Arc::new(a) as ArrayRef;
11705 let err = cast_with_options(
11706 &array,
11707 &DataType::Timestamp(TimeUnit::Microsecond, None),
11708 &CastOptions {
11709 safe: false,
11710 format_options: FormatOptions::default(),
11711 },
11712 );
11713 assert!(err.is_err());
11714
11715 let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
11716 let c = b.as_primitive::<TimestampMicrosecondType>();
11717 assert_eq!(MAX_DAYS_MICROS as i64 * MICROSECONDS_IN_DAY, c.value(0));
11718 assert!(c.is_null(1));
11719 assert!(c.is_null(2));
11720 }
11721
11722 #[test]
11723 fn test_cast_date32_to_timestamp_ns_overflow() {
11724 let upper_limit = 106_751;
11726 let a = Date32Array::from(vec![Some(upper_limit), Some(upper_limit + 1), None]);
11727 let array = Arc::new(a) as ArrayRef;
11728 let err = cast_with_options(
11729 &array,
11730 &DataType::Timestamp(TimeUnit::Nanosecond, None),
11731 &CastOptions {
11732 safe: false,
11733 format_options: FormatOptions::default(),
11734 },
11735 );
11736 assert!(err.is_err());
11737
11738 let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
11739 let c = b.as_primitive::<TimestampNanosecondType>();
11740 assert_eq!(upper_limit as i64 * NANOSECONDS_IN_DAY, c.value(0));
11741 assert!(c.is_null(1));
11742 assert!(c.is_null(2));
11743 }
11744
11745 #[test]
11746 fn test_timezone_cast() {
11747 let a = StringArray::from(vec![
11748 "2000-01-01T12:00:00", "2020-12-15T12:34:56", ]);
11751 let array = Arc::new(a) as ArrayRef;
11752 let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
11753 let v = b.as_primitive::<TimestampNanosecondType>();
11754
11755 assert_eq!(v.value(0), 946728000000000000);
11756 assert_eq!(v.value(1), 1608035696000000000);
11757
11758 let b = cast(
11759 &b,
11760 &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
11761 )
11762 .unwrap();
11763 let v = b.as_primitive::<TimestampNanosecondType>();
11764
11765 assert_eq!(v.value(0), 946728000000000000);
11766 assert_eq!(v.value(1), 1608035696000000000);
11767
11768 let b = cast(
11769 &b,
11770 &DataType::Timestamp(TimeUnit::Millisecond, Some("+02:00".into())),
11771 )
11772 .unwrap();
11773 let v = b.as_primitive::<TimestampMillisecondType>();
11774
11775 assert_eq!(v.value(0), 946728000000);
11776 assert_eq!(v.value(1), 1608035696000);
11777 }
11778
11779 #[test]
11780 fn test_cast_utf8_to_timestamp() {
11781 fn test_tz(tz: Arc<str>) {
11782 let valid = StringArray::from(vec![
11783 "2023-01-01 04:05:06.789000-08:00",
11784 "2023-01-01 04:05:06.789000-07:00",
11785 "2023-01-01 04:05:06.789 -0800",
11786 "2023-01-01 04:05:06.789 -08:00",
11787 "2023-01-01 040506 +0730",
11788 "2023-01-01 040506 +07:30",
11789 "2023-01-01 04:05:06.789",
11790 "2023-01-01 04:05:06",
11791 "2023-01-01",
11792 ]);
11793
11794 let array = Arc::new(valid) as ArrayRef;
11795 let b = cast_with_options(
11796 &array,
11797 &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.clone())),
11798 &CastOptions {
11799 safe: false,
11800 format_options: FormatOptions::default(),
11801 },
11802 )
11803 .unwrap();
11804
11805 let tz = tz.as_ref().parse().unwrap();
11806
11807 let as_tz =
11808 |v: i64| as_datetime_with_timezone::<TimestampNanosecondType>(v, tz).unwrap();
11809
11810 let as_utc = |v: &i64| as_tz(*v).naive_utc().to_string();
11811 let as_local = |v: &i64| as_tz(*v).naive_local().to_string();
11812
11813 let values = b.as_primitive::<TimestampNanosecondType>().values();
11814 let utc_results: Vec<_> = values.iter().map(as_utc).collect();
11815 let local_results: Vec<_> = values.iter().map(as_local).collect();
11816
11817 assert_eq!(
11819 &utc_results[..6],
11820 &[
11821 "2023-01-01 12:05:06.789".to_string(),
11822 "2023-01-01 11:05:06.789".to_string(),
11823 "2023-01-01 12:05:06.789".to_string(),
11824 "2023-01-01 12:05:06.789".to_string(),
11825 "2022-12-31 20:35:06".to_string(),
11826 "2022-12-31 20:35:06".to_string(),
11827 ]
11828 );
11829 assert_eq!(
11831 &local_results[6..],
11832 &[
11833 "2023-01-01 04:05:06.789".to_string(),
11834 "2023-01-01 04:05:06".to_string(),
11835 "2023-01-01 00:00:00".to_string()
11836 ]
11837 )
11838 }
11839
11840 test_tz("+00:00".into());
11841 test_tz("+02:00".into());
11842 }
11843
11844 #[test]
11845 fn test_cast_invalid_utf8() {
11846 let v1: &[u8] = b"\xFF invalid";
11847 let v2: &[u8] = b"\x00 Foo";
11848 let s = BinaryArray::from(vec![v1, v2]);
11849 let options = CastOptions {
11850 safe: true,
11851 format_options: FormatOptions::default(),
11852 };
11853 let array = cast_with_options(&s, &DataType::Utf8, &options).unwrap();
11854 let a = array.as_string::<i32>();
11855 a.to_data().validate_full().unwrap();
11856
11857 assert_eq!(a.null_count(), 1);
11858 assert_eq!(a.len(), 2);
11859 assert!(a.is_null(0));
11860 assert_eq!(a.value(0), "");
11861 assert_eq!(a.value(1), "\x00 Foo");
11862 }
11863
11864 #[test]
11865 fn test_cast_utf8_to_timestamptz() {
11866 let valid = StringArray::from(vec!["2023-01-01"]);
11867
11868 let array = Arc::new(valid) as ArrayRef;
11869 let b = cast(
11870 &array,
11871 &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
11872 )
11873 .unwrap();
11874
11875 let expect = DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into()));
11876
11877 assert_eq!(b.data_type(), &expect);
11878 let c = b
11879 .as_any()
11880 .downcast_ref::<TimestampNanosecondArray>()
11881 .unwrap();
11882 assert_eq!(1672531200000000000, c.value(0));
11883 }
11884
11885 #[test]
11886 fn test_cast_out_of_precision_decimal_to_string() {
11887 let array = create_decimal128_array(vec![Some(123456789), Some(-123456789)], 7, 3).unwrap();
11892 let b = cast(&array, &DataType::Utf8).unwrap();
11893 let c = b.as_string::<i32>();
11894 assert_eq!("123456.789", c.value(0));
11895 assert_eq!("-123456.789", c.value(1));
11896 }
11897
11898 #[test]
11899 fn test_cast_decimal_to_string() {
11900 assert!(can_cast_types(
11901 &DataType::Decimal32(9, 4),
11902 &DataType::Utf8View
11903 ));
11904 assert!(can_cast_types(
11905 &DataType::Decimal64(16, 4),
11906 &DataType::Utf8View
11907 ));
11908 assert!(can_cast_types(
11909 &DataType::Decimal128(10, 4),
11910 &DataType::Utf8View
11911 ));
11912 assert!(can_cast_types(
11913 &DataType::Decimal256(38, 10),
11914 &DataType::Utf8View
11915 ));
11916
11917 macro_rules! assert_decimal_values {
11918 ($array:expr) => {
11919 let c = $array;
11920 assert_eq!("1123.454", c.value(0));
11921 assert_eq!("2123.456", c.value(1));
11922 assert_eq!("-3123.453", c.value(2));
11923 assert_eq!("-3123.456", c.value(3));
11924 assert_eq!("0.000", c.value(4));
11925 assert_eq!("0.123", c.value(5));
11926 assert!(c.is_null(6));
11927 };
11928 }
11929
11930 fn test_decimal_to_string<IN: ArrowPrimitiveType, OffsetSize: OffsetSizeTrait>(
11931 output_type: DataType,
11932 array: PrimitiveArray<IN>,
11933 ) {
11934 let b = cast(&array, &output_type).unwrap();
11935
11936 assert_eq!(b.data_type(), &output_type);
11937 match b.data_type() {
11938 DataType::Utf8View => {
11939 let c = b.as_string_view();
11940 assert_decimal_values!(c);
11941 }
11942 DataType::Utf8 | DataType::LargeUtf8 => {
11943 let c = b.as_string::<OffsetSize>();
11944 assert_decimal_values!(c);
11945 }
11946 _ => (),
11947 }
11948 }
11949
11950 let array32: Vec<Option<i32>> = vec![
11951 Some(1123454),
11952 Some(2123456),
11953 Some(-3123453),
11954 Some(-3123456),
11955 Some(0),
11956 Some(123),
11957 None,
11958 ];
11959 let array64: Vec<Option<i64>> = array32.iter().map(|num| num.map(|x| x as i64)).collect();
11960 let array128: Vec<Option<i128>> =
11961 array64.iter().map(|num| num.map(|x| x as i128)).collect();
11962 let array256: Vec<Option<i256>> = array128
11963 .iter()
11964 .map(|num| num.map(i256::from_i128))
11965 .collect();
11966
11967 test_decimal_to_string::<Decimal32Type, i32>(
11968 DataType::Utf8View,
11969 create_decimal32_array(array32.clone(), 7, 3).unwrap(),
11970 );
11971 test_decimal_to_string::<Decimal32Type, i32>(
11972 DataType::Utf8,
11973 create_decimal32_array(array32.clone(), 7, 3).unwrap(),
11974 );
11975 test_decimal_to_string::<Decimal32Type, i64>(
11976 DataType::LargeUtf8,
11977 create_decimal32_array(array32, 7, 3).unwrap(),
11978 );
11979
11980 test_decimal_to_string::<Decimal64Type, i32>(
11981 DataType::Utf8View,
11982 create_decimal64_array(array64.clone(), 7, 3).unwrap(),
11983 );
11984 test_decimal_to_string::<Decimal64Type, i32>(
11985 DataType::Utf8,
11986 create_decimal64_array(array64.clone(), 7, 3).unwrap(),
11987 );
11988 test_decimal_to_string::<Decimal64Type, i64>(
11989 DataType::LargeUtf8,
11990 create_decimal64_array(array64, 7, 3).unwrap(),
11991 );
11992
11993 test_decimal_to_string::<Decimal128Type, i32>(
11994 DataType::Utf8View,
11995 create_decimal128_array(array128.clone(), 7, 3).unwrap(),
11996 );
11997 test_decimal_to_string::<Decimal128Type, i32>(
11998 DataType::Utf8,
11999 create_decimal128_array(array128.clone(), 7, 3).unwrap(),
12000 );
12001 test_decimal_to_string::<Decimal128Type, i64>(
12002 DataType::LargeUtf8,
12003 create_decimal128_array(array128, 7, 3).unwrap(),
12004 );
12005
12006 test_decimal_to_string::<Decimal256Type, i32>(
12007 DataType::Utf8View,
12008 create_decimal256_array(array256.clone(), 7, 3).unwrap(),
12009 );
12010 test_decimal_to_string::<Decimal256Type, i32>(
12011 DataType::Utf8,
12012 create_decimal256_array(array256.clone(), 7, 3).unwrap(),
12013 );
12014 test_decimal_to_string::<Decimal256Type, i64>(
12015 DataType::LargeUtf8,
12016 create_decimal256_array(array256, 7, 3).unwrap(),
12017 );
12018 }
12019
12020 #[test]
12021 fn test_cast_numeric_to_decimal128_precision_overflow() {
12022 let array = Int64Array::from(vec![1234567]);
12023 let array = Arc::new(array) as ArrayRef;
12024 let casted_array = cast_with_options(
12025 &array,
12026 &DataType::Decimal128(7, 3),
12027 &CastOptions {
12028 safe: true,
12029 format_options: FormatOptions::default(),
12030 },
12031 );
12032 assert!(casted_array.is_ok());
12033 assert!(casted_array.unwrap().is_null(0));
12034
12035 let err = cast_with_options(
12036 &array,
12037 &DataType::Decimal128(7, 3),
12038 &CastOptions {
12039 safe: false,
12040 format_options: FormatOptions::default(),
12041 },
12042 );
12043 assert_eq!(
12044 "Invalid argument error: 1234567.000 is too large to store in a Decimal128 of precision 7. Max is 9999.999",
12045 err.unwrap_err().to_string()
12046 );
12047 }
12048
12049 #[test]
12050 fn test_cast_numeric_to_decimal256_precision_overflow() {
12051 let array = Int64Array::from(vec![1234567]);
12052 let array = Arc::new(array) as ArrayRef;
12053 let casted_array = cast_with_options(
12054 &array,
12055 &DataType::Decimal256(7, 3),
12056 &CastOptions {
12057 safe: true,
12058 format_options: FormatOptions::default(),
12059 },
12060 );
12061 assert!(casted_array.is_ok());
12062 assert!(casted_array.unwrap().is_null(0));
12063
12064 let err = cast_with_options(
12065 &array,
12066 &DataType::Decimal256(7, 3),
12067 &CastOptions {
12068 safe: false,
12069 format_options: FormatOptions::default(),
12070 },
12071 );
12072 assert_eq!(
12073 "Invalid argument error: 1234567.000 is too large to store in a Decimal256 of precision 7. Max is 9999.999",
12074 err.unwrap_err().to_string()
12075 );
12076 }
12077
12078 fn cast_from_duration_to_interval<T: ArrowTemporalType<Native = i64>>(
12080 array: Vec<i64>,
12081 cast_options: &CastOptions,
12082 ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
12083 let array = PrimitiveArray::<T>::new(array.into(), None);
12084 let array = Arc::new(array) as ArrayRef;
12085 let interval = DataType::Interval(IntervalUnit::MonthDayNano);
12086 let out = cast_with_options(&array, &interval, cast_options)?;
12087 let out = out.as_primitive::<IntervalMonthDayNanoType>().clone();
12088 Ok(out)
12089 }
12090
12091 #[test]
12092 fn test_cast_from_duration_to_interval() {
12093 let array = vec![1234567];
12095 let casted_array =
12096 cast_from_duration_to_interval::<DurationSecondType>(array, &CastOptions::default())
12097 .unwrap();
12098 assert_eq!(
12099 casted_array.data_type(),
12100 &DataType::Interval(IntervalUnit::MonthDayNano)
12101 );
12102 assert_eq!(
12103 casted_array.value(0),
12104 IntervalMonthDayNano::new(0, 0, 1234567000000000)
12105 );
12106
12107 let array = vec![i64::MAX];
12108 let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
12109 array.clone(),
12110 &CastOptions::default(),
12111 )
12112 .unwrap();
12113 assert!(!casted_array.is_valid(0));
12114
12115 let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
12116 array,
12117 &CastOptions {
12118 safe: false,
12119 format_options: FormatOptions::default(),
12120 },
12121 );
12122 assert!(casted_array.is_err());
12123
12124 let array = vec![1234567];
12126 let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
12127 array,
12128 &CastOptions::default(),
12129 )
12130 .unwrap();
12131 assert_eq!(
12132 casted_array.data_type(),
12133 &DataType::Interval(IntervalUnit::MonthDayNano)
12134 );
12135 assert_eq!(
12136 casted_array.value(0),
12137 IntervalMonthDayNano::new(0, 0, 1234567000000)
12138 );
12139
12140 let array = vec![i64::MAX];
12141 let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
12142 array.clone(),
12143 &CastOptions::default(),
12144 )
12145 .unwrap();
12146 assert!(!casted_array.is_valid(0));
12147
12148 let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
12149 array,
12150 &CastOptions {
12151 safe: false,
12152 format_options: FormatOptions::default(),
12153 },
12154 );
12155 assert!(casted_array.is_err());
12156
12157 let array = vec![1234567];
12159 let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
12160 array,
12161 &CastOptions::default(),
12162 )
12163 .unwrap();
12164 assert_eq!(
12165 casted_array.data_type(),
12166 &DataType::Interval(IntervalUnit::MonthDayNano)
12167 );
12168 assert_eq!(
12169 casted_array.value(0),
12170 IntervalMonthDayNano::new(0, 0, 1234567000)
12171 );
12172
12173 let array = vec![i64::MAX];
12174 let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
12175 array.clone(),
12176 &CastOptions::default(),
12177 )
12178 .unwrap();
12179 assert!(!casted_array.is_valid(0));
12180
12181 let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
12182 array,
12183 &CastOptions {
12184 safe: false,
12185 format_options: FormatOptions::default(),
12186 },
12187 );
12188 assert!(casted_array.is_err());
12189
12190 let array = vec![1234567];
12192 let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
12193 array,
12194 &CastOptions::default(),
12195 )
12196 .unwrap();
12197 assert_eq!(
12198 casted_array.data_type(),
12199 &DataType::Interval(IntervalUnit::MonthDayNano)
12200 );
12201 assert_eq!(
12202 casted_array.value(0),
12203 IntervalMonthDayNano::new(0, 0, 1234567)
12204 );
12205
12206 let array = vec![i64::MAX];
12207 let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
12208 array,
12209 &CastOptions {
12210 safe: false,
12211 format_options: FormatOptions::default(),
12212 },
12213 )
12214 .unwrap();
12215 assert_eq!(
12216 casted_array.value(0),
12217 IntervalMonthDayNano::new(0, 0, i64::MAX)
12218 );
12219 }
12220
12221 fn cast_from_interval_to_duration<T: ArrowTemporalType>(
12223 array: &IntervalMonthDayNanoArray,
12224 cast_options: &CastOptions,
12225 ) -> Result<PrimitiveArray<T>, ArrowError> {
12226 let casted_array = cast_with_options(&array, &T::DATA_TYPE, cast_options)?;
12227 casted_array
12228 .as_any()
12229 .downcast_ref::<PrimitiveArray<T>>()
12230 .ok_or_else(|| {
12231 ArrowError::ComputeError(format!("Failed to downcast to {}", T::DATA_TYPE))
12232 })
12233 .cloned()
12234 }
12235
12236 #[test]
12237 fn test_cast_from_interval_to_duration() {
12238 let nullable = CastOptions::default();
12239 let fallible = CastOptions {
12240 safe: false,
12241 format_options: FormatOptions::default(),
12242 };
12243 let v = IntervalMonthDayNano::new(0, 0, 1234567);
12244
12245 let array = vec![v].into();
12247 let casted_array: DurationSecondArray =
12248 cast_from_interval_to_duration(&array, &nullable).unwrap();
12249 assert_eq!(casted_array.value(0), 0);
12250
12251 let array = vec![IntervalMonthDayNano::MAX].into();
12252 let casted_array: DurationSecondArray =
12253 cast_from_interval_to_duration(&array, &nullable).unwrap();
12254 assert!(!casted_array.is_valid(0));
12255
12256 let res = cast_from_interval_to_duration::<DurationSecondType>(&array, &fallible);
12257 assert!(res.is_err());
12258
12259 let array = vec![v].into();
12261 let casted_array: DurationMillisecondArray =
12262 cast_from_interval_to_duration(&array, &nullable).unwrap();
12263 assert_eq!(casted_array.value(0), 1);
12264
12265 let array = vec![IntervalMonthDayNano::MAX].into();
12266 let casted_array: DurationMillisecondArray =
12267 cast_from_interval_to_duration(&array, &nullable).unwrap();
12268 assert!(!casted_array.is_valid(0));
12269
12270 let res = cast_from_interval_to_duration::<DurationMillisecondType>(&array, &fallible);
12271 assert!(res.is_err());
12272
12273 let array = vec![v].into();
12275 let casted_array: DurationMicrosecondArray =
12276 cast_from_interval_to_duration(&array, &nullable).unwrap();
12277 assert_eq!(casted_array.value(0), 1234);
12278
12279 let array = vec![IntervalMonthDayNano::MAX].into();
12280 let casted_array =
12281 cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &nullable).unwrap();
12282 assert!(!casted_array.is_valid(0));
12283
12284 let casted_array =
12285 cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &fallible);
12286 assert!(casted_array.is_err());
12287
12288 let array = vec![v].into();
12290 let casted_array: DurationNanosecondArray =
12291 cast_from_interval_to_duration(&array, &nullable).unwrap();
12292 assert_eq!(casted_array.value(0), 1234567);
12293
12294 let array = vec![IntervalMonthDayNano::MAX].into();
12295 let casted_array: DurationNanosecondArray =
12296 cast_from_interval_to_duration(&array, &nullable).unwrap();
12297 assert!(!casted_array.is_valid(0));
12298
12299 let casted_array =
12300 cast_from_interval_to_duration::<DurationNanosecondType>(&array, &fallible);
12301 assert!(casted_array.is_err());
12302
12303 let array = vec![
12304 IntervalMonthDayNanoType::make_value(0, 1, 0),
12305 IntervalMonthDayNanoType::make_value(-1, 0, 0),
12306 IntervalMonthDayNanoType::make_value(1, 1, 0),
12307 IntervalMonthDayNanoType::make_value(1, 0, 1),
12308 IntervalMonthDayNanoType::make_value(0, 0, -1),
12309 ]
12310 .into();
12311 let casted_array =
12312 cast_from_interval_to_duration::<DurationNanosecondType>(&array, &nullable).unwrap();
12313 assert!(!casted_array.is_valid(0));
12314 assert!(!casted_array.is_valid(1));
12315 assert!(!casted_array.is_valid(2));
12316 assert!(!casted_array.is_valid(3));
12317 assert!(casted_array.is_valid(4));
12318 assert_eq!(casted_array.value(4), -1);
12319 }
12320
12321 fn cast_from_interval_year_month_to_interval_month_day_nano(
12323 array: Vec<i32>,
12324 cast_options: &CastOptions,
12325 ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
12326 let array = PrimitiveArray::<IntervalYearMonthType>::from(array);
12327 let array = Arc::new(array) as ArrayRef;
12328 let casted_array = cast_with_options(
12329 &array,
12330 &DataType::Interval(IntervalUnit::MonthDayNano),
12331 cast_options,
12332 )?;
12333 casted_array
12334 .as_any()
12335 .downcast_ref::<IntervalMonthDayNanoArray>()
12336 .ok_or_else(|| {
12337 ArrowError::ComputeError(
12338 "Failed to downcast to IntervalMonthDayNanoArray".to_string(),
12339 )
12340 })
12341 .cloned()
12342 }
12343
12344 #[test]
12345 fn test_cast_from_interval_year_month_to_interval_month_day_nano() {
12346 let array = vec![1234567];
12348 let casted_array = cast_from_interval_year_month_to_interval_month_day_nano(
12349 array,
12350 &CastOptions::default(),
12351 )
12352 .unwrap();
12353 assert_eq!(
12354 casted_array.data_type(),
12355 &DataType::Interval(IntervalUnit::MonthDayNano)
12356 );
12357 assert_eq!(
12358 casted_array.value(0),
12359 IntervalMonthDayNano::new(1234567, 0, 0)
12360 );
12361 }
12362
12363 fn cast_from_interval_day_time_to_interval_month_day_nano(
12365 array: Vec<IntervalDayTime>,
12366 cast_options: &CastOptions,
12367 ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
12368 let array = PrimitiveArray::<IntervalDayTimeType>::from(array);
12369 let array = Arc::new(array) as ArrayRef;
12370 let casted_array = cast_with_options(
12371 &array,
12372 &DataType::Interval(IntervalUnit::MonthDayNano),
12373 cast_options,
12374 )?;
12375 Ok(casted_array
12376 .as_primitive::<IntervalMonthDayNanoType>()
12377 .clone())
12378 }
12379
12380 #[test]
12381 fn test_cast_from_interval_day_time_to_interval_month_day_nano() {
12382 let array = vec![IntervalDayTime::new(123, 0)];
12384 let casted_array =
12385 cast_from_interval_day_time_to_interval_month_day_nano(array, &CastOptions::default())
12386 .unwrap();
12387 assert_eq!(
12388 casted_array.data_type(),
12389 &DataType::Interval(IntervalUnit::MonthDayNano)
12390 );
12391 assert_eq!(casted_array.value(0), IntervalMonthDayNano::new(0, 123, 0));
12392 }
12393
12394 #[test]
12395 fn test_can_cast_interval_to_int64_matches_cast() {
12396 let arrays: Vec<ArrayRef> = vec![
12398 Arc::new(IntervalYearMonthArray::from(vec![12])),
12399 Arc::new(IntervalDayTimeArray::from(vec![IntervalDayTime::new(1, 2)])),
12400 Arc::new(IntervalMonthDayNanoArray::from(vec![
12401 IntervalMonthDayNano::new(1, 2, 3),
12402 ])),
12403 ];
12404 for array in arrays {
12405 let from = array.data_type();
12406 assert_eq!(
12407 can_cast_types(from, &DataType::Int64),
12408 cast(&array, &DataType::Int64).is_ok(),
12409 "can_cast_types disagrees with cast for {from} -> Int64"
12410 );
12411 }
12412 }
12413
12414 #[test]
12415 fn test_cast_union_to_int64_skips_uncastable_interval_child() {
12416 let fields = UnionFields::try_new(
12419 [0, 1],
12420 [
12421 Field::new("iv", DataType::Interval(IntervalUnit::YearMonth), true),
12422 Field::new("s", DataType::Utf8, true),
12423 ],
12424 )
12425 .unwrap();
12426 let union = UnionArray::try_new(
12427 fields,
12428 vec![0, 1, 0].into(),
12429 None,
12430 vec![
12431 Arc::new(IntervalYearMonthArray::from(vec![Some(12), None, Some(24)])),
12432 Arc::new(StringArray::from(vec![None, Some("77"), None])),
12433 ],
12434 )
12435 .unwrap();
12436
12437 let casted = cast(&union, &DataType::Int64).unwrap();
12438 let casted = casted.as_primitive::<Int64Type>();
12439 assert!(casted.is_null(0));
12440 assert_eq!(casted.value(1), 77);
12441 assert!(casted.is_null(2));
12442 }
12443
12444 #[test]
12445 fn test_cast_below_unixtimestamp() {
12446 let valid = StringArray::from(vec![
12447 "1900-01-03 23:59:59",
12448 "1969-12-31 00:00:01",
12449 "1989-12-31 00:00:01",
12450 ]);
12451
12452 let array = Arc::new(valid) as ArrayRef;
12453 let casted_array = cast_with_options(
12454 &array,
12455 &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
12456 &CastOptions {
12457 safe: false,
12458 format_options: FormatOptions::default(),
12459 },
12460 )
12461 .unwrap();
12462
12463 let ts_array = casted_array
12464 .as_primitive::<TimestampNanosecondType>()
12465 .values()
12466 .iter()
12467 .map(|ts| ts / 1_000_000)
12468 .collect::<Vec<_>>();
12469
12470 let array = TimestampMillisecondArray::from(ts_array).with_timezone("+00:00".to_string());
12471 let casted_array = cast(&array, &DataType::Date32).unwrap();
12472 let date_array = casted_array.as_primitive::<Date32Type>();
12473 let casted_array = cast(&date_array, &DataType::Utf8).unwrap();
12474 let string_array = casted_array.as_string::<i32>();
12475 assert_eq!("1900-01-03", string_array.value(0));
12476 assert_eq!("1969-12-31", string_array.value(1));
12477 assert_eq!("1989-12-31", string_array.value(2));
12478 }
12479
12480 #[test]
12481 fn test_nested_list() {
12482 let mut list = ListBuilder::new(Int32Builder::new());
12483 list.append_value([Some(1), Some(2), Some(3)]);
12484 list.append_value([Some(4), None, Some(6)]);
12485 let list = list.finish();
12486
12487 let to_field = Field::new("nested", list.data_type().clone(), false);
12488 let to = DataType::List(Arc::new(to_field));
12489 let out = cast(&list, &to).unwrap();
12490 let opts = FormatOptions::default().with_null("null");
12491 let formatted = ArrayFormatter::try_new(out.as_ref(), &opts).unwrap();
12492
12493 assert_eq!(formatted.value(0).to_string(), "[[1], [2], [3]]");
12494 assert_eq!(formatted.value(1).to_string(), "[[4], [null], [6]]");
12495 }
12496
12497 #[test]
12498 fn test_nested_list_cast() {
12499 let mut builder = ListBuilder::new(ListBuilder::new(Int32Builder::new()));
12500 builder.append_value([Some([Some(1), Some(2), None]), None]);
12501 builder.append_value([None, Some([]), None]);
12502 builder.append_null();
12503 builder.append_value([Some([Some(2), Some(3)])]);
12504 let start = builder.finish();
12505
12506 let mut builder = LargeListBuilder::new(LargeListBuilder::new(Int8Builder::new()));
12507 builder.append_value([Some([Some(1), Some(2), None]), None]);
12508 builder.append_value([None, Some([]), None]);
12509 builder.append_null();
12510 builder.append_value([Some([Some(2), Some(3)])]);
12511 let expected = builder.finish();
12512
12513 let actual = cast(&start, expected.data_type()).unwrap();
12514 assert_eq!(actual.as_ref(), &expected);
12515 }
12516
12517 const CAST_OPTIONS: CastOptions<'static> = CastOptions {
12518 safe: true,
12519 format_options: FormatOptions::new(),
12520 };
12521
12522 #[test]
12523 #[expect(clippy::assertions_on_constants)]
12524 fn test_const_options() {
12525 assert!(CAST_OPTIONS.safe)
12526 }
12527
12528 #[test]
12529 fn test_list_format_options() {
12530 let options = CastOptions {
12531 safe: false,
12532 format_options: FormatOptions::default().with_null("null"),
12533 };
12534 let array = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
12535 Some(vec![Some(0), Some(1), Some(2)]),
12536 Some(vec![Some(0), None, Some(2)]),
12537 ]);
12538 let a = cast_with_options(&array, &DataType::Utf8, &options).unwrap();
12539 let r: Vec<_> = a.as_string::<i32>().iter().flatten().collect();
12540 assert_eq!(r, &["[0, 1, 2]", "[0, null, 2]"]);
12541 }
12542 #[test]
12543 fn test_cast_string_to_timestamp_invalid_tz() {
12544 let bad_timestamp = "2023-12-05T21:58:10.45ZZTOP";
12546 let array = StringArray::from(vec![Some(bad_timestamp)]);
12547
12548 let data_types = [
12549 DataType::Timestamp(TimeUnit::Second, None),
12550 DataType::Timestamp(TimeUnit::Millisecond, None),
12551 DataType::Timestamp(TimeUnit::Microsecond, None),
12552 DataType::Timestamp(TimeUnit::Nanosecond, None),
12553 ];
12554
12555 let cast_options = CastOptions {
12556 safe: false,
12557 ..Default::default()
12558 };
12559
12560 for dt in data_types {
12561 assert_eq!(
12562 cast_with_options(&array, &dt, &cast_options)
12563 .unwrap_err()
12564 .to_string(),
12565 "Parser error: Invalid timezone \"ZZTOP\": only offset based timezones supported without chrono-tz feature"
12566 );
12567 }
12568 }
12569 #[test]
12570 fn test_cast_struct_to_struct() {
12571 let struct_type = DataType::Struct(
12572 vec![
12573 Field::new("a", DataType::Boolean, false),
12574 Field::new("b", DataType::Int32, false),
12575 ]
12576 .into(),
12577 );
12578 let to_type = DataType::Struct(
12579 vec![
12580 Field::new("a", DataType::Utf8, false),
12581 Field::new("b", DataType::Utf8, false),
12582 ]
12583 .into(),
12584 );
12585 let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
12586 let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
12587 let struct_array = StructArray::from(vec![
12588 (
12589 Arc::new(Field::new("b", DataType::Boolean, false)),
12590 boolean.clone() as ArrayRef,
12591 ),
12592 (
12593 Arc::new(Field::new("c", DataType::Int32, false)),
12594 int.clone() as ArrayRef,
12595 ),
12596 ]);
12597 let casted_array = cast(&struct_array, &to_type).unwrap();
12598 let casted_array = casted_array.as_struct();
12599 assert_eq!(casted_array.data_type(), &to_type);
12600 let casted_boolean_array = casted_array
12601 .column(0)
12602 .as_string::<i32>()
12603 .into_iter()
12604 .flatten()
12605 .collect::<Vec<_>>();
12606 let casted_int_array = casted_array
12607 .column(1)
12608 .as_string::<i32>()
12609 .into_iter()
12610 .flatten()
12611 .collect::<Vec<_>>();
12612 assert_eq!(casted_boolean_array, vec!["false", "false", "true", "true"]);
12613 assert_eq!(casted_int_array, vec!["42", "28", "19", "31"]);
12614
12615 let to_type = DataType::Struct(
12617 vec![
12618 Field::new("a", DataType::Date32, false),
12619 Field::new("b", DataType::Utf8, false),
12620 ]
12621 .into(),
12622 );
12623 assert!(!can_cast_types(&struct_type, &to_type));
12624 let result = cast(&struct_array, &to_type);
12625 assert_eq!(
12626 "Cast error: Casting from Boolean to Date32 not supported",
12627 result.unwrap_err().to_string()
12628 );
12629 }
12630
12631 #[test]
12632 fn test_cast_struct_to_struct_nullability() {
12633 let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
12634 let int = Arc::new(Int32Array::from(vec![Some(42), None, Some(19), None]));
12635 let struct_array = StructArray::from(vec![
12636 (
12637 Arc::new(Field::new("b", DataType::Boolean, false)),
12638 boolean.clone() as ArrayRef,
12639 ),
12640 (
12641 Arc::new(Field::new("c", DataType::Int32, true)),
12642 int.clone() as ArrayRef,
12643 ),
12644 ]);
12645
12646 let to_type = DataType::Struct(
12648 vec![
12649 Field::new("a", DataType::Utf8, false),
12650 Field::new("b", DataType::Utf8, true),
12651 ]
12652 .into(),
12653 );
12654 cast(&struct_array, &to_type).expect("Cast nullable to nullable struct field should work");
12655
12656 let to_type = DataType::Struct(
12658 vec![
12659 Field::new("a", DataType::Utf8, false),
12660 Field::new("b", DataType::Utf8, false),
12661 ]
12662 .into(),
12663 );
12664 cast(&struct_array, &to_type)
12665 .expect_err("Cast nullable to non-nullable struct field should fail");
12666
12667 let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
12668 let int = Arc::new(Int32Array::from(vec![i32::MAX, 25, 1, 100]));
12669 let struct_array = StructArray::from(vec![
12670 (
12671 Arc::new(Field::new("b", DataType::Boolean, false)),
12672 boolean.clone() as ArrayRef,
12673 ),
12674 (
12675 Arc::new(Field::new("c", DataType::Int32, false)),
12676 int.clone() as ArrayRef,
12677 ),
12678 ]);
12679
12680 let to_type = DataType::Struct(
12682 vec![
12683 Field::new("a", DataType::Utf8, false),
12684 Field::new("b", DataType::Utf8, false),
12685 ]
12686 .into(),
12687 );
12688 cast(&struct_array, &to_type)
12689 .expect("Cast non-nullable to non-nullable struct field should work");
12690
12691 let to_type = DataType::Struct(
12693 vec![
12694 Field::new("a", DataType::Utf8, false),
12695 Field::new("b", DataType::Int8, false),
12696 ]
12697 .into(),
12698 );
12699 cast(&struct_array, &to_type).expect_err(
12700 "Cast non-nullable to non-nullable struct field returning null should fail",
12701 );
12702 }
12703
12704 #[test]
12705 fn test_cast_struct_to_non_struct() {
12706 let boolean = Arc::new(BooleanArray::from(vec![true, false]));
12707 let struct_array = StructArray::from(vec![(
12708 Arc::new(Field::new("a", DataType::Boolean, false)),
12709 boolean.clone() as ArrayRef,
12710 )]);
12711 let to_type = DataType::Utf8;
12712 let result = cast(&struct_array, &to_type);
12713 assert_eq!(
12714 r#"Cast error: Casting from Struct("a": non-null Boolean) to Utf8 not supported"#,
12715 result.unwrap_err().to_string()
12716 );
12717 }
12718
12719 #[test]
12720 fn test_cast_non_struct_to_struct() {
12721 let array = StringArray::from(vec!["a", "b"]);
12722 let to_type = DataType::Struct(vec![Field::new("a", DataType::Boolean, false)].into());
12723 let result = cast(&array, &to_type);
12724 assert_eq!(
12725 r#"Cast error: Casting from Utf8 to Struct("a": non-null Boolean) not supported"#,
12726 result.unwrap_err().to_string()
12727 );
12728 }
12729
12730 #[test]
12731 fn test_cast_struct_with_different_field_order() {
12732 let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
12734 let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
12735 let string = Arc::new(StringArray::from(vec!["foo", "bar", "baz", "qux"]));
12736
12737 let struct_array = StructArray::from(vec![
12738 (
12739 Arc::new(Field::new("a", DataType::Boolean, false)),
12740 boolean.clone() as ArrayRef,
12741 ),
12742 (
12743 Arc::new(Field::new("b", DataType::Int32, false)),
12744 int.clone() as ArrayRef,
12745 ),
12746 (
12747 Arc::new(Field::new("c", DataType::Utf8, false)),
12748 string.clone() as ArrayRef,
12749 ),
12750 ]);
12751
12752 let to_type = DataType::Struct(
12754 vec![
12755 Field::new("c", DataType::Utf8, false),
12756 Field::new("a", DataType::Utf8, false), Field::new("b", DataType::Utf8, false), ]
12759 .into(),
12760 );
12761
12762 let result = cast(&struct_array, &to_type).unwrap();
12763 let result_struct = result.as_struct();
12764
12765 assert_eq!(result_struct.data_type(), &to_type);
12766 assert_eq!(result_struct.num_columns(), 3);
12767
12768 let c_column = result_struct.column(0).as_string::<i32>();
12770 assert_eq!(
12771 c_column.into_iter().flatten().collect::<Vec<_>>(),
12772 vec!["foo", "bar", "baz", "qux"]
12773 );
12774
12775 let a_column = result_struct.column(1).as_string::<i32>();
12777 assert_eq!(
12778 a_column.into_iter().flatten().collect::<Vec<_>>(),
12779 vec!["false", "false", "true", "true"]
12780 );
12781
12782 let b_column = result_struct.column(2).as_string::<i32>();
12784 assert_eq!(
12785 b_column.into_iter().flatten().collect::<Vec<_>>(),
12786 vec!["42", "28", "19", "31"]
12787 );
12788 }
12789
12790 #[test]
12791 fn test_cast_struct_with_missing_field() {
12792 let boolean = Arc::new(BooleanArray::from(vec![false, true]));
12794 let struct_array = StructArray::from(vec![(
12795 Arc::new(Field::new("a", DataType::Boolean, false)),
12796 boolean.clone() as ArrayRef,
12797 )]);
12798
12799 let to_type = DataType::Struct(
12800 vec![
12801 Field::new("a", DataType::Utf8, false),
12802 Field::new("b", DataType::Int32, false), ]
12804 .into(),
12805 );
12806
12807 let result = cast(&struct_array, &to_type);
12808 assert!(result.is_err());
12809 assert_eq!(
12810 result.unwrap_err().to_string(),
12811 "Invalid argument error: Incorrect number of arrays for StructArray fields, expected 2 got 1"
12812 );
12813 }
12814
12815 #[test]
12816 fn test_cast_struct_with_subset_of_fields() {
12817 let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
12819 let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
12820 let string = Arc::new(StringArray::from(vec!["foo", "bar", "baz", "qux"]));
12821
12822 let struct_array = StructArray::from(vec![
12823 (
12824 Arc::new(Field::new("a", DataType::Boolean, false)),
12825 boolean.clone() as ArrayRef,
12826 ),
12827 (
12828 Arc::new(Field::new("b", DataType::Int32, false)),
12829 int.clone() as ArrayRef,
12830 ),
12831 (
12832 Arc::new(Field::new("c", DataType::Utf8, false)),
12833 string.clone() as ArrayRef,
12834 ),
12835 ]);
12836
12837 let to_type = DataType::Struct(
12839 vec![
12840 Field::new("c", DataType::Utf8, false),
12841 Field::new("a", DataType::Utf8, false),
12842 ]
12843 .into(),
12844 );
12845
12846 let result = cast(&struct_array, &to_type).unwrap();
12847 let result_struct = result.as_struct();
12848
12849 assert_eq!(result_struct.data_type(), &to_type);
12850 assert_eq!(result_struct.num_columns(), 2);
12851
12852 let c_column = result_struct.column(0).as_string::<i32>();
12854 assert_eq!(
12855 c_column.into_iter().flatten().collect::<Vec<_>>(),
12856 vec!["foo", "bar", "baz", "qux"]
12857 );
12858
12859 let a_column = result_struct.column(1).as_string::<i32>();
12861 assert_eq!(
12862 a_column.into_iter().flatten().collect::<Vec<_>>(),
12863 vec!["false", "false", "true", "true"]
12864 );
12865 }
12866
12867 #[test]
12868 fn test_can_cast_struct_rename_field() {
12869 let from_type = DataType::Struct(
12871 vec![
12872 Field::new("a", DataType::Int32, false),
12873 Field::new("b", DataType::Utf8, false),
12874 ]
12875 .into(),
12876 );
12877
12878 let to_type = DataType::Struct(
12879 vec![
12880 Field::new("a", DataType::Int64, false),
12881 Field::new("c", DataType::Boolean, false), ]
12883 .into(),
12884 );
12885
12886 assert!(can_cast_types(&from_type, &to_type));
12887 }
12888
12889 fn run_decimal_cast_test_case_between_multiple_types(t: DecimalCastTestConfig) {
12890 run_decimal_cast_test_case::<Decimal128Type, Decimal128Type>(t.clone());
12891 run_decimal_cast_test_case::<Decimal128Type, Decimal256Type>(t.clone());
12892 run_decimal_cast_test_case::<Decimal256Type, Decimal128Type>(t.clone());
12893 run_decimal_cast_test_case::<Decimal256Type, Decimal256Type>(t.clone());
12894 }
12895
12896 #[test]
12897 fn test_decimal_to_decimal_coverage() {
12898 let test_cases = [
12899 DecimalCastTestConfig {
12901 input_prec: 5,
12902 input_scale: 1,
12903 input_repr: 99999, output_prec: 10,
12905 output_scale: 6,
12906 expected_output_repr: Ok(9999900000), },
12908 DecimalCastTestConfig {
12910 input_prec: 5,
12911 input_scale: 1,
12912 input_repr: 99, output_prec: 7,
12914 output_scale: 6,
12915 expected_output_repr: Ok(9900000), },
12917 DecimalCastTestConfig {
12919 input_prec: 5,
12920 input_scale: 1,
12921 input_repr: 99999, output_prec: 7,
12923 output_scale: 6,
12924 expected_output_repr: Err("Invalid argument error: 9999.900000 is too large to store in a {} of precision 7. Max is 9.999999".to_string()) },
12926 DecimalCastTestConfig {
12928 input_prec: 5,
12929 input_scale: 3,
12930 input_repr: 99999, output_prec: 10,
12932 output_scale: 2,
12933 expected_output_repr: Ok(10000), },
12935 DecimalCastTestConfig {
12937 input_prec: 5,
12938 input_scale: 3,
12939 input_repr: 99994, output_prec: 10,
12941 output_scale: 2,
12942 expected_output_repr: Ok(9999), },
12944 DecimalCastTestConfig {
12946 input_prec: 5,
12947 input_scale: 3,
12948 input_repr: 99999, output_prec: 10,
12950 output_scale: 3,
12951 expected_output_repr: Ok(99999), },
12953 DecimalCastTestConfig {
12955 input_prec: 10,
12956 input_scale: 5,
12957 input_repr: 999999, output_prec: 8,
12959 output_scale: 7,
12960 expected_output_repr: Ok(99999900), },
12962 DecimalCastTestConfig {
12964 input_prec: 10,
12965 input_scale: 5,
12966 input_repr: 9999999, output_prec: 8,
12968 output_scale: 7,
12969 expected_output_repr: Err("Invalid argument error: 99.9999900 is too large to store in a {} of precision 8. Max is 9.9999999".to_string()) },
12971 DecimalCastTestConfig {
12973 input_prec: 7,
12974 input_scale: 4,
12975 input_repr: 9999999, output_prec: 6,
12977 output_scale: 2,
12978 expected_output_repr: Ok(100000),
12979 },
12980 DecimalCastTestConfig {
12982 input_prec: 10,
12983 input_scale: 5,
12984 input_repr: 12345678, output_prec: 8,
12986 output_scale: 3,
12987 expected_output_repr: Ok(123457), },
12989 DecimalCastTestConfig {
12991 input_prec: 10,
12992 input_scale: 5,
12993 input_repr: 9999999, output_prec: 4,
12995 output_scale: 3,
12996 expected_output_repr: Err("Invalid argument error: 100.000 is too large to store in a {} of precision 4. Max is 9.999".to_string()) },
12998 DecimalCastTestConfig {
13000 input_prec: 10,
13001 input_scale: 5,
13002 input_repr: 999999, output_prec: 6,
13004 output_scale: 5,
13005 expected_output_repr: Ok(999999), },
13007 DecimalCastTestConfig {
13009 input_prec: 10,
13010 input_scale: 5,
13011 input_repr: 9999999, output_prec: 6,
13013 output_scale: 5,
13014 expected_output_repr: Err("Invalid argument error: 99.99999 is too large to store in a {} of precision 6. Max is 9.99999".to_string()) },
13016 DecimalCastTestConfig {
13018 input_prec: 7,
13019 input_scale: 4,
13020 input_repr: 12345, output_prec: 7,
13022 output_scale: 6,
13023 expected_output_repr: Ok(1234500), },
13025 DecimalCastTestConfig {
13027 input_prec: 7,
13028 input_scale: 4,
13029 input_repr: 123456, output_prec: 7,
13031 output_scale: 6,
13032 expected_output_repr: Err("Invalid argument error: 12.345600 is too large to store in a {} of precision 7. Max is 9.999999".to_string()) },
13034 DecimalCastTestConfig {
13036 input_prec: 7,
13037 input_scale: 5,
13038 input_repr: 1234567, output_prec: 7,
13040 output_scale: 4,
13041 expected_output_repr: Ok(123457), },
13043 DecimalCastTestConfig {
13045 input_prec: 7,
13046 input_scale: 5,
13047 input_repr: 9999999, output_prec: 7,
13049 output_scale: 5,
13050 expected_output_repr: Ok(9999999), },
13052 DecimalCastTestConfig {
13054 input_prec: 7,
13055 input_scale: 0,
13056 input_repr: 1234567, output_prec: 8,
13058 output_scale: 0,
13059 expected_output_repr: Ok(1234567), },
13061 DecimalCastTestConfig {
13063 input_prec: 7,
13064 input_scale: 0,
13065 input_repr: 1234567, output_prec: 6,
13067 output_scale: 0,
13068 expected_output_repr: Err("Invalid argument error: 1234567 is too large to store in a {} of precision 6. Max is 999999".to_string())
13069 },
13070 DecimalCastTestConfig {
13072 input_prec: 7,
13073 input_scale: 0,
13074 input_repr: 123456, output_prec: 6,
13076 output_scale: 0,
13077 expected_output_repr: Ok(123456), },
13079 ];
13080
13081 for t in test_cases {
13082 run_decimal_cast_test_case_between_multiple_types(t);
13083 }
13084 }
13085
13086 #[test]
13087 fn test_decimal_to_decimal_increase_scale_and_precision_unchecked() {
13088 let test_cases = [
13089 DecimalCastTestConfig {
13090 input_prec: 5,
13091 input_scale: 0,
13092 input_repr: 99999,
13093 output_prec: 10,
13094 output_scale: 5,
13095 expected_output_repr: Ok(9999900000),
13096 },
13097 DecimalCastTestConfig {
13098 input_prec: 5,
13099 input_scale: 0,
13100 input_repr: -99999,
13101 output_prec: 10,
13102 output_scale: 5,
13103 expected_output_repr: Ok(-9999900000),
13104 },
13105 DecimalCastTestConfig {
13106 input_prec: 5,
13107 input_scale: 2,
13108 input_repr: 99999,
13109 output_prec: 10,
13110 output_scale: 5,
13111 expected_output_repr: Ok(99999000),
13112 },
13113 DecimalCastTestConfig {
13114 input_prec: 5,
13115 input_scale: -2,
13116 input_repr: -99999,
13117 output_prec: 10,
13118 output_scale: 3,
13119 expected_output_repr: Ok(-9999900000),
13120 },
13121 DecimalCastTestConfig {
13122 input_prec: 5,
13123 input_scale: 3,
13124 input_repr: -12345,
13125 output_prec: 6,
13126 output_scale: 5,
13127 expected_output_repr: Err("Invalid argument error: -12.34500 is too small to store in a {} of precision 6. Min is -9.99999".to_string())
13128 },
13129 ];
13130
13131 for t in test_cases {
13132 run_decimal_cast_test_case_between_multiple_types(t);
13133 }
13134 }
13135
13136 #[test]
13137 fn test_decimal_to_decimal_decrease_scale_and_precision_unchecked() {
13138 let test_cases = [
13139 DecimalCastTestConfig {
13140 input_prec: 5,
13141 input_scale: 0,
13142 input_repr: 99999,
13143 output_scale: -3,
13144 output_prec: 3,
13145 expected_output_repr: Ok(100),
13146 },
13147 DecimalCastTestConfig {
13148 input_prec: 5,
13149 input_scale: 0,
13150 input_repr: -99999,
13151 output_prec: 1,
13152 output_scale: -5,
13153 expected_output_repr: Ok(-1),
13154 },
13155 DecimalCastTestConfig {
13156 input_prec: 10,
13157 input_scale: 2,
13158 input_repr: 123456789,
13159 output_prec: 5,
13160 output_scale: -2,
13161 expected_output_repr: Ok(12346),
13162 },
13163 DecimalCastTestConfig {
13164 input_prec: 10,
13165 input_scale: 4,
13166 input_repr: -9876543210,
13167 output_prec: 7,
13168 output_scale: 0,
13169 expected_output_repr: Ok(-987654),
13170 },
13171 DecimalCastTestConfig {
13172 input_prec: 7,
13173 input_scale: 4,
13174 input_repr: 9999999,
13175 output_prec: 6,
13176 output_scale: 3,
13177 expected_output_repr:
13178 Err("Invalid argument error: 1000.000 is too large to store in a {} of precision 6. Max is 999.999".to_string()),
13179 },
13180 ];
13181 for t in test_cases {
13182 run_decimal_cast_test_case_between_multiple_types(t);
13183 }
13184 }
13185
13186 #[test]
13187 fn test_decimal_to_decimal_throw_error_on_precision_overflow_same_scale() {
13188 let array = vec![Some(123456789)];
13189 let array = create_decimal128_array(array, 24, 2).unwrap();
13190 let input_type = DataType::Decimal128(24, 2);
13191 let output_type = DataType::Decimal128(6, 2);
13192 assert!(can_cast_types(&input_type, &output_type));
13193
13194 let options = CastOptions {
13195 safe: false,
13196 ..Default::default()
13197 };
13198 let result = cast_with_options(&array, &output_type, &options);
13199 assert_eq!(
13200 result.unwrap_err().to_string(),
13201 "Invalid argument error: 1234567.89 is too large to store in a Decimal128 of precision 6. Max is 9999.99"
13202 );
13203 }
13204
13205 #[test]
13206 fn test_decimal_to_decimal_same_scale() {
13207 let array = vec![Some(520)];
13208 let array = create_decimal128_array(array, 4, 2).unwrap();
13209 let input_type = DataType::Decimal128(4, 2);
13210 let output_type = DataType::Decimal128(3, 2);
13211 assert!(can_cast_types(&input_type, &output_type));
13212
13213 let options = CastOptions {
13214 safe: false,
13215 ..Default::default()
13216 };
13217 let result = cast_with_options(&array, &output_type, &options);
13218 assert_eq!(
13219 result.unwrap().as_primitive::<Decimal128Type>().value(0),
13220 520
13221 );
13222
13223 assert_eq!(
13225 &cast(
13226 &create_decimal128_array(vec![Some(0)], 3, 0).unwrap(),
13227 &DataType::Decimal128(2, 0)
13228 )
13229 .unwrap(),
13230 &(Arc::new(create_decimal128_array(vec![Some(0)], 2, 0).unwrap()) as ArrayRef)
13231 );
13232 }
13233
13234 #[test]
13235 fn test_decimal_to_decimal_throw_error_on_precision_overflow_lower_scale() {
13236 let array = vec![Some(123456789)];
13237 let array = create_decimal128_array(array, 24, 4).unwrap();
13238 let input_type = DataType::Decimal128(24, 4);
13239 let output_type = DataType::Decimal128(6, 2);
13240 assert!(can_cast_types(&input_type, &output_type));
13241
13242 let options = CastOptions {
13243 safe: false,
13244 ..Default::default()
13245 };
13246 let result = cast_with_options(&array, &output_type, &options);
13247 assert_eq!(
13248 result.unwrap_err().to_string(),
13249 "Invalid argument error: 12345.68 is too large to store in a Decimal128 of precision 6. Max is 9999.99"
13250 );
13251 }
13252
13253 #[test]
13254 fn test_decimal_to_decimal_throw_error_on_precision_overflow_greater_scale() {
13255 let array = vec![Some(123456789)];
13256 let array = create_decimal128_array(array, 24, 2).unwrap();
13257 let input_type = DataType::Decimal128(24, 2);
13258 let output_type = DataType::Decimal128(6, 3);
13259 assert!(can_cast_types(&input_type, &output_type));
13260
13261 let options = CastOptions {
13262 safe: false,
13263 ..Default::default()
13264 };
13265 let result = cast_with_options(&array, &output_type, &options);
13266 assert_eq!(
13267 result.unwrap_err().to_string(),
13268 "Invalid argument error: 1234567.890 is too large to store in a Decimal128 of precision 6. Max is 999.999"
13269 );
13270 }
13271
13272 #[test]
13273 fn test_decimal_to_decimal_throw_error_on_precision_overflow_diff_type() {
13274 let array = vec![Some(123456789)];
13275 let array = create_decimal128_array(array, 24, 2).unwrap();
13276 let input_type = DataType::Decimal128(24, 2);
13277 let output_type = DataType::Decimal256(6, 2);
13278 assert!(can_cast_types(&input_type, &output_type));
13279
13280 let options = CastOptions {
13281 safe: false,
13282 ..Default::default()
13283 };
13284 let result = cast_with_options(&array, &output_type, &options).unwrap_err();
13285 assert_eq!(
13286 result.to_string(),
13287 "Invalid argument error: 1234567.89 is too large to store in a Decimal256 of precision 6. Max is 9999.99"
13288 );
13289 }
13290
13291 #[test]
13292 fn test_first_none() {
13293 let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
13294 None,
13295 Some(vec![Some(1), Some(2)]),
13296 ])) as ArrayRef;
13297 let data_type =
13298 DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
13299 let opt = CastOptions::default();
13300 let r = cast_with_options(&array, &data_type, &opt).unwrap();
13301
13302 let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
13303 vec![None, Some(vec![Some(1), Some(2)])],
13304 2,
13305 )) as ArrayRef;
13306 assert_eq!(*fixed_array, *r);
13307 }
13308
13309 #[test]
13310 fn test_first_last_none() {
13311 let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
13312 None,
13313 Some(vec![Some(1), Some(2)]),
13314 None,
13315 ])) as ArrayRef;
13316 let data_type =
13317 DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
13318 let opt = CastOptions::default();
13319 let r = cast_with_options(&array, &data_type, &opt).unwrap();
13320
13321 let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
13322 vec![None, Some(vec![Some(1), Some(2)]), None],
13323 2,
13324 )) as ArrayRef;
13325 assert_eq!(*fixed_array, *r);
13326 }
13327
13328 #[test]
13329 fn test_cast_decimal_error_output() {
13330 let array = Int64Array::from(vec![1]);
13331 let error = cast_with_options(
13332 &array,
13333 &DataType::Decimal32(1, 1),
13334 &CastOptions {
13335 safe: false,
13336 format_options: FormatOptions::default(),
13337 },
13338 )
13339 .unwrap_err();
13340 assert_eq!(
13341 error.to_string(),
13342 "Invalid argument error: 1.0 is too large to store in a Decimal32 of precision 1. Max is 0.9"
13343 );
13344
13345 let array = Int64Array::from(vec![-1]);
13346 let error = cast_with_options(
13347 &array,
13348 &DataType::Decimal32(1, 1),
13349 &CastOptions {
13350 safe: false,
13351 format_options: FormatOptions::default(),
13352 },
13353 )
13354 .unwrap_err();
13355 assert_eq!(
13356 error.to_string(),
13357 "Invalid argument error: -1.0 is too small to store in a Decimal32 of precision 1. Min is -0.9"
13358 );
13359 }
13360
13361 #[test]
13362 fn test_run_end_encoded_to_primitive() {
13363 let run_ends = Int32Array::from(vec![2, 5, 6]);
13365 let values = Int32Array::from(vec![1, 2, 3]);
13366 let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
13367 let array_ref = Arc::new(run_array) as ArrayRef;
13368 let cast_result = cast(&array_ref, &DataType::Int64).unwrap();
13370 let result_run_array = cast_result.as_any().downcast_ref::<Int64Array>().unwrap();
13372 assert_eq!(
13373 result_run_array.values(),
13374 &[1i64, 1i64, 2i64, 2i64, 2i64, 3i64]
13375 );
13376 }
13377
13378 #[test]
13379 fn test_sliced_run_end_encoded_to_primitive() {
13380 let run_ends = Int32Array::from(vec![2, 5, 6]);
13381 let values = Int32Array::from(vec![1, 2, 3]);
13382 let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
13384 let run_array = run_array.slice(3, 3); let array_ref = Arc::new(run_array) as ArrayRef;
13386
13387 let cast_result = cast(&array_ref, &DataType::Int64).unwrap();
13388 let result_run_array = cast_result.as_primitive::<Int64Type>();
13389 assert_eq!(result_run_array.values(), &[2, 2, 3]);
13390 }
13391
13392 #[test]
13393 fn test_run_end_encoded_to_string() {
13394 let run_ends = Int32Array::from(vec![2, 3, 5]);
13395 let values = Int32Array::from(vec![10, 20, 30]);
13396 let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
13397 let array_ref = Arc::new(run_array) as ArrayRef;
13398
13399 let cast_result = cast(&array_ref, &DataType::Utf8).unwrap();
13401
13402 let result_array = cast_result.as_any().downcast_ref::<StringArray>().unwrap();
13404 assert_eq!(result_array.value(0), "10");
13406 assert_eq!(result_array.value(1), "10");
13407 assert_eq!(result_array.value(2), "20");
13408 }
13409
13410 #[test]
13411 fn test_primitive_to_run_end_encoded() {
13412 let source_array = Int32Array::from(vec![1, 1, 2, 2, 2, 3]);
13414 let array_ref = Arc::new(source_array) as ArrayRef;
13415
13416 let target_type = DataType::RunEndEncoded(
13418 Arc::new(Field::new(
13419 Field::REE_RUN_ENDS_FIELD_DEFAULT_NAME,
13420 DataType::Int32,
13421 false,
13422 )),
13423 Arc::new(Field::new(
13424 Field::REE_VALUES_FIELD_DEFAULT_NAME,
13425 DataType::Int32,
13426 true,
13427 )),
13428 );
13429 let cast_result = cast(&array_ref, &target_type).unwrap();
13430
13431 let result_run_array = cast_result
13433 .as_any()
13434 .downcast_ref::<RunArray<Int32Type>>()
13435 .unwrap();
13436
13437 assert_eq!(result_run_array.run_ends().values(), &[2, 5, 6]);
13439
13440 let values_array = result_run_array.values().as_primitive::<Int32Type>();
13442 assert_eq!(values_array.values(), &[1, 2, 3]);
13443 }
13444
13445 #[test]
13446 fn test_primitive_to_run_end_encoded_with_nulls() {
13447 let source_array = Int32Array::from(vec![
13448 Some(1),
13449 Some(1),
13450 None,
13451 None,
13452 Some(2),
13453 Some(2),
13454 Some(3),
13455 Some(3),
13456 None,
13457 None,
13458 Some(4),
13459 Some(4),
13460 Some(5),
13461 Some(5),
13462 None,
13463 None,
13464 ]);
13465 let array_ref = Arc::new(source_array) as ArrayRef;
13466 let target_type = DataType::RunEndEncoded(
13467 Arc::new(Field::new(
13468 Field::REE_RUN_ENDS_FIELD_DEFAULT_NAME,
13469 DataType::Int32,
13470 false,
13471 )),
13472 Arc::new(Field::new(
13473 Field::REE_VALUES_FIELD_DEFAULT_NAME,
13474 DataType::Int32,
13475 true,
13476 )),
13477 );
13478 let cast_result = cast(&array_ref, &target_type).unwrap();
13479 let result_run_array = cast_result
13480 .as_any()
13481 .downcast_ref::<RunArray<Int32Type>>()
13482 .unwrap();
13483 assert_eq!(
13484 result_run_array.run_ends().values(),
13485 &[2, 4, 6, 8, 10, 12, 14, 16]
13486 );
13487 assert_eq!(
13488 result_run_array
13489 .values()
13490 .as_primitive::<Int32Type>()
13491 .values(),
13492 &[1, 0, 2, 3, 0, 4, 5, 0]
13493 );
13494 assert_eq!(result_run_array.values().null_count(), 3);
13495 }
13496
13497 #[test]
13498 fn test_primitive_to_run_end_encoded_with_nulls_consecutive() {
13499 let source_array = Int64Array::from(vec![
13500 Some(1),
13501 Some(1),
13502 None,
13503 None,
13504 None,
13505 None,
13506 None,
13507 None,
13508 None,
13509 None,
13510 Some(4),
13511 Some(20),
13512 Some(500),
13513 Some(500),
13514 None,
13515 None,
13516 ]);
13517 let array_ref = Arc::new(source_array) as ArrayRef;
13518 let target_type = DataType::RunEndEncoded(
13519 Arc::new(Field::new(
13520 Field::REE_RUN_ENDS_FIELD_DEFAULT_NAME,
13521 DataType::Int16,
13522 false,
13523 )),
13524 Arc::new(Field::new(
13525 Field::REE_VALUES_FIELD_DEFAULT_NAME,
13526 DataType::Int64,
13527 true,
13528 )),
13529 );
13530 let cast_result = cast(&array_ref, &target_type).unwrap();
13531 let result_run_array = cast_result
13532 .as_any()
13533 .downcast_ref::<RunArray<Int16Type>>()
13534 .unwrap();
13535 assert_eq!(
13536 result_run_array.run_ends().values(),
13537 &[2, 10, 11, 12, 14, 16]
13538 );
13539 assert_eq!(
13540 result_run_array
13541 .values()
13542 .as_primitive::<Int64Type>()
13543 .values(),
13544 &[1, 0, 4, 20, 500, 0]
13545 );
13546 assert_eq!(result_run_array.values().null_count(), 2);
13547 }
13548
13549 #[test]
13550 fn test_string_to_run_end_encoded() {
13551 let source_array = StringArray::from(vec!["a", "a", "b", "c", "c"]);
13553 let array_ref = Arc::new(source_array) as ArrayRef;
13554
13555 let target_type = DataType::RunEndEncoded(
13557 Arc::new(Field::new(
13558 Field::REE_RUN_ENDS_FIELD_DEFAULT_NAME,
13559 DataType::Int32,
13560 false,
13561 )),
13562 Arc::new(Field::new(
13563 Field::REE_VALUES_FIELD_DEFAULT_NAME,
13564 DataType::Utf8,
13565 true,
13566 )),
13567 );
13568 let cast_result = cast(&array_ref, &target_type).unwrap();
13569
13570 let result_run_array = cast_result
13572 .as_any()
13573 .downcast_ref::<RunArray<Int32Type>>()
13574 .unwrap();
13575
13576 assert_eq!(result_run_array.run_ends().values(), &[2, 3, 5]);
13578
13579 let values_array = result_run_array.values().as_string::<i32>();
13581 assert_eq!(values_array.value(0), "a");
13582 assert_eq!(values_array.value(1), "b");
13583 assert_eq!(values_array.value(2), "c");
13584 }
13585
13586 #[test]
13587 fn test_empty_array_to_run_end_encoded() {
13588 let source_array = Int32Array::from(Vec::<i32>::new());
13590 let array_ref = Arc::new(source_array) as ArrayRef;
13591
13592 let target_type = DataType::RunEndEncoded(
13594 Arc::new(Field::new(
13595 Field::REE_RUN_ENDS_FIELD_DEFAULT_NAME,
13596 DataType::Int32,
13597 false,
13598 )),
13599 Arc::new(Field::new(
13600 Field::REE_VALUES_FIELD_DEFAULT_NAME,
13601 DataType::Int32,
13602 true,
13603 )),
13604 );
13605 let cast_result = cast(&array_ref, &target_type).unwrap();
13606
13607 let result_run_array = cast_result
13609 .as_any()
13610 .downcast_ref::<RunArray<Int32Type>>()
13611 .unwrap();
13612
13613 assert_eq!(result_run_array.run_ends().len(), 0);
13615 assert_eq!(result_run_array.values().len(), 0);
13616 }
13617
13618 #[test]
13619 fn test_run_end_encoded_with_nulls() {
13620 let run_ends = Int32Array::from(vec![2, 3, 5]);
13622 let values = Int32Array::from(vec![Some(1), None, Some(2)]);
13623 let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
13624 let array_ref = Arc::new(run_array) as ArrayRef;
13625
13626 let cast_result = cast(&array_ref, &DataType::Utf8).unwrap();
13628
13629 let result_run_array = cast_result.as_any().downcast_ref::<StringArray>().unwrap();
13631 assert_eq!(result_run_array.value(0), "1");
13632 assert!(result_run_array.is_null(2));
13633 assert_eq!(result_run_array.value(4), "2");
13634 }
13635
13636 #[test]
13637 fn test_different_index_types() {
13638 let source_array = Int32Array::from(vec![1, 1, 2, 3, 3]);
13640 let array_ref = Arc::new(source_array) as ArrayRef;
13641
13642 let target_type = DataType::RunEndEncoded(
13643 Arc::new(Field::new(
13644 Field::REE_RUN_ENDS_FIELD_DEFAULT_NAME,
13645 DataType::Int16,
13646 false,
13647 )),
13648 Arc::new(Field::new(
13649 Field::REE_VALUES_FIELD_DEFAULT_NAME,
13650 DataType::Int32,
13651 true,
13652 )),
13653 );
13654 let cast_result = cast(&array_ref, &target_type).unwrap();
13655 assert_eq!(cast_result.data_type(), &target_type);
13656
13657 let run_array = cast_result
13660 .as_any()
13661 .downcast_ref::<RunArray<Int16Type>>()
13662 .unwrap();
13663 assert_eq!(run_array.values().as_primitive::<Int32Type>().value(0), 1);
13664 assert_eq!(run_array.values().as_primitive::<Int32Type>().value(1), 2);
13665 assert_eq!(run_array.values().as_primitive::<Int32Type>().value(2), 3);
13666 assert_eq!(run_array.run_ends().values(), &[2i16, 3i16, 5i16]);
13667
13668 let target_type = DataType::RunEndEncoded(
13670 Arc::new(Field::new(
13671 Field::REE_RUN_ENDS_FIELD_DEFAULT_NAME,
13672 DataType::Int64,
13673 false,
13674 )),
13675 Arc::new(Field::new(
13676 Field::REE_VALUES_FIELD_DEFAULT_NAME,
13677 DataType::Int32,
13678 true,
13679 )),
13680 );
13681 let cast_result = cast(&array_ref, &target_type).unwrap();
13682 assert_eq!(cast_result.data_type(), &target_type);
13683
13684 let run_array = cast_result
13687 .as_any()
13688 .downcast_ref::<RunArray<Int64Type>>()
13689 .unwrap();
13690 assert_eq!(run_array.values().as_primitive::<Int32Type>().value(0), 1);
13691 assert_eq!(run_array.values().as_primitive::<Int32Type>().value(1), 2);
13692 assert_eq!(run_array.values().as_primitive::<Int32Type>().value(2), 3);
13693 assert_eq!(run_array.run_ends().values(), &[2i64, 3i64, 5i64]);
13694 }
13695
13696 #[test]
13697 fn test_unsupported_cast_to_run_end_encoded() {
13698 let field = Field::new("item", DataType::Int32, false);
13700 let struct_array = StructArray::from(vec![(
13701 Arc::new(field),
13702 Arc::new(Int32Array::from(vec![1, 2, 3])) as ArrayRef,
13703 )]);
13704 let array_ref = Arc::new(struct_array) as ArrayRef;
13705
13706 let cast_result = cast(&array_ref, &DataType::FixedSizeBinary(10));
13710
13711 assert!(cast_result.is_err());
13713 }
13714
13715 #[test]
13717 fn test_cast_run_end_encoded_int64_to_int16_should_fail() {
13718 let run_ends = Int64Array::from(vec![100_000, 400_000, 700_000]); let values = StringArray::from(vec!["a", "b", "c"]);
13721
13722 let ree_array = RunArray::<Int64Type>::try_new(&run_ends, &values).unwrap();
13723 let array_ref = Arc::new(ree_array) as ArrayRef;
13724
13725 let target_type = DataType::RunEndEncoded(
13727 Arc::new(Field::new(
13728 Field::REE_RUN_ENDS_FIELD_DEFAULT_NAME,
13729 DataType::Int16,
13730 false,
13731 )),
13732 Arc::new(Field::new(
13733 Field::REE_VALUES_FIELD_DEFAULT_NAME,
13734 DataType::Utf8,
13735 true,
13736 )),
13737 );
13738 let cast_options = CastOptions {
13739 safe: false, format_options: FormatOptions::default(),
13741 };
13742
13743 let result: Result<Arc<dyn Array + 'static>, ArrowError> =
13745 cast_with_options(&array_ref, &target_type, &cast_options);
13746
13747 let e = result.expect_err("Cast should have failed but succeeded");
13748 assert!(
13749 e.to_string()
13750 .contains("Cast error: Can't cast value 100000 to type Int16")
13751 );
13752 }
13753
13754 #[test]
13755 fn test_cast_run_end_encoded_int64_to_int16_with_safe_should_fail_with_null_invalid_error() {
13756 let run_ends = Int64Array::from(vec![100_000, 400_000, 700_000]); let values = StringArray::from(vec!["a", "b", "c"]);
13759
13760 let ree_array = RunArray::<Int64Type>::try_new(&run_ends, &values).unwrap();
13761 let array_ref = Arc::new(ree_array) as ArrayRef;
13762
13763 let target_type = DataType::RunEndEncoded(
13765 Arc::new(Field::new(
13766 Field::REE_RUN_ENDS_FIELD_DEFAULT_NAME,
13767 DataType::Int16,
13768 false,
13769 )),
13770 Arc::new(Field::new(
13771 Field::REE_VALUES_FIELD_DEFAULT_NAME,
13772 DataType::Utf8,
13773 true,
13774 )),
13775 );
13776 let cast_options = CastOptions {
13777 safe: true,
13778 format_options: FormatOptions::default(),
13779 };
13780
13781 let result: Result<Arc<dyn Array + 'static>, ArrowError> =
13783 cast_with_options(&array_ref, &target_type, &cast_options);
13784 let e = result.expect_err("Cast should have failed but succeeded");
13785 assert!(
13786 e.to_string()
13787 .contains("Invalid argument error: Found null values in run_ends array. The run_ends array should not have null values.")
13788 );
13789 }
13790
13791 #[test]
13793 fn test_cast_run_end_encoded_int16_to_int64_should_succeed() {
13794 let run_ends = Int16Array::from(vec![2, 5, 8]); let values = StringArray::from(vec!["a", "b", "c"]);
13797
13798 let ree_array = RunArray::<Int16Type>::try_new(&run_ends, &values).unwrap();
13799 let array_ref = Arc::new(ree_array) as ArrayRef;
13800
13801 let target_type = DataType::RunEndEncoded(
13803 Arc::new(Field::new(
13804 Field::REE_RUN_ENDS_FIELD_DEFAULT_NAME,
13805 DataType::Int64,
13806 false,
13807 )),
13808 Arc::new(Field::new(
13809 Field::REE_VALUES_FIELD_DEFAULT_NAME,
13810 DataType::Utf8,
13811 true,
13812 )),
13813 );
13814 let cast_options = CastOptions {
13815 safe: false,
13816 format_options: FormatOptions::default(),
13817 };
13818
13819 let result: Result<Arc<dyn Array + 'static>, ArrowError> =
13821 cast_with_options(&array_ref, &target_type, &cast_options);
13822
13823 let array_ref = result.expect("Cast should have succeeded but failed");
13824 let run_array = array_ref
13826 .as_any()
13827 .downcast_ref::<RunArray<Int64Type>>()
13828 .unwrap();
13829
13830 assert_eq!(run_array.run_ends().values(), &[2i64, 5i64, 8i64]);
13833 assert_eq!(run_array.values().as_string::<i32>().value(0), "a");
13834 assert_eq!(run_array.values().as_string::<i32>().value(1), "b");
13835 assert_eq!(run_array.values().as_string::<i32>().value(2), "c");
13836 }
13837
13838 #[test]
13839 fn test_cast_run_end_encoded_dictionary_to_run_end_encoded() {
13840 let values = StringArray::from_iter([Some("a"), Some("b"), Some("c")]);
13842 let keys = UInt64Array::from_iter(vec![1, 1, 1, 0, 0, 0, 2, 2, 2]);
13843 let array_ref = Arc::new(DictionaryArray::new(keys, Arc::new(values))) as ArrayRef;
13844
13845 let target_type = DataType::RunEndEncoded(
13847 Arc::new(Field::new(
13848 Field::REE_RUN_ENDS_FIELD_DEFAULT_NAME,
13849 DataType::Int64,
13850 false,
13851 )),
13852 Arc::new(Field::new(
13853 Field::REE_VALUES_FIELD_DEFAULT_NAME,
13854 DataType::Utf8,
13855 true,
13856 )),
13857 );
13858 let cast_options = CastOptions {
13859 safe: false,
13860 format_options: FormatOptions::default(),
13861 };
13862
13863 let result = cast_with_options(&array_ref, &target_type, &cast_options)
13865 .expect("Cast should have succeeded but failed");
13866
13867 let run_array = result
13870 .as_any()
13871 .downcast_ref::<RunArray<Int64Type>>()
13872 .unwrap();
13873 assert_eq!(run_array.values().as_string::<i32>().value(0), "b");
13874 assert_eq!(run_array.values().as_string::<i32>().value(1), "a");
13875 assert_eq!(run_array.values().as_string::<i32>().value(2), "c");
13876
13877 assert_eq!(run_array.run_ends().values(), &[3i64, 6i64, 9i64]);
13879 }
13880
13881 fn int32_list_values() -> Vec<Option<Vec<Option<i32>>>> {
13882 vec![
13883 Some(vec![Some(1), Some(2), Some(3)]),
13884 Some(vec![Some(4), Some(5), Some(6)]),
13885 None,
13886 Some(vec![Some(7), Some(8), Some(9)]),
13887 Some(vec![None, Some(10)]),
13888 ]
13889 }
13890
13891 #[test]
13892 fn test_cast_list_view_to_list() {
13893 let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13894 let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13895 assert!(can_cast_types(list_view.data_type(), &target_type));
13896 let cast_result = cast(&list_view, &target_type).unwrap();
13897 let got_list = cast_result.as_list::<i32>();
13898 let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13899 assert_eq!(got_list, &expected_list);
13900 }
13901
13902 #[test]
13903 fn test_cast_list_view_to_large_list() {
13904 let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13905 let target_type = DataType::LargeList(Arc::new(Field::new("item", DataType::Int32, true)));
13906 assert!(can_cast_types(list_view.data_type(), &target_type));
13907 let cast_result = cast(&list_view, &target_type).unwrap();
13908 let got_list = cast_result.as_list::<i64>();
13909 let expected_list =
13910 LargeListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13911 assert_eq!(got_list, &expected_list);
13912 }
13913
13914 #[test]
13915 fn test_cast_list_to_list_view() {
13916 let list = ListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13917 let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Int32, true)));
13918 assert!(can_cast_types(list.data_type(), &target_type));
13919 let cast_result = cast(&list, &target_type).unwrap();
13920
13921 let got_list_view = cast_result.as_list_view::<i32>();
13922 let expected_list_view =
13923 ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13924 assert_eq!(got_list_view, &expected_list_view);
13925
13926 let list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13928 Some(vec![Some(1), Some(2)]),
13929 None,
13930 Some(vec![None, Some(3)]),
13931 ]);
13932 let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Float32, true)));
13933 assert!(can_cast_types(list.data_type(), &target_type));
13934 let cast_result = cast(&list, &target_type).unwrap();
13935
13936 let got_list_view = cast_result.as_list_view::<i32>();
13937 let expected_list_view = ListViewArray::from_iter_primitive::<Float32Type, _, _>(vec![
13938 Some(vec![Some(1.0), Some(2.0)]),
13939 None,
13940 Some(vec![None, Some(3.0)]),
13941 ]);
13942 assert_eq!(got_list_view, &expected_list_view);
13943 }
13944
13945 #[test]
13946 fn test_cast_list_to_large_list_view() {
13947 let list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13948 Some(vec![Some(1), Some(2)]),
13949 None,
13950 Some(vec![None, Some(3)]),
13951 ]);
13952 let target_type =
13953 DataType::LargeListView(Arc::new(Field::new("item", DataType::Float32, true)));
13954 assert!(can_cast_types(list.data_type(), &target_type));
13955 let cast_result = cast(&list, &target_type).unwrap();
13956
13957 let got_list_view = cast_result.as_list_view::<i64>();
13958 let expected_list_view =
13959 LargeListViewArray::from_iter_primitive::<Float32Type, _, _>(vec![
13960 Some(vec![Some(1.0), Some(2.0)]),
13961 None,
13962 Some(vec![None, Some(3.0)]),
13963 ]);
13964 assert_eq!(got_list_view, &expected_list_view);
13965 }
13966
13967 #[test]
13968 fn test_cast_large_list_view_to_large_list() {
13969 let list_view =
13970 LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13971 let target_type = DataType::LargeList(Arc::new(Field::new("item", DataType::Int32, true)));
13972 assert!(can_cast_types(list_view.data_type(), &target_type));
13973 let cast_result = cast(&list_view, &target_type).unwrap();
13974 let got_list = cast_result.as_list::<i64>();
13975
13976 let expected_list =
13977 LargeListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13978 assert_eq!(got_list, &expected_list);
13979 }
13980
13981 #[test]
13982 fn test_cast_large_list_view_to_list() {
13983 let list_view =
13984 LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13985 let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13986 assert!(can_cast_types(list_view.data_type(), &target_type));
13987 let cast_result = cast(&list_view, &target_type).unwrap();
13988 let got_list = cast_result.as_list::<i32>();
13989
13990 let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13991 assert_eq!(got_list, &expected_list);
13992 }
13993
13994 #[test]
13995 fn test_cast_large_list_to_large_list_view() {
13996 let list = LargeListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13997 let target_type =
13998 DataType::LargeListView(Arc::new(Field::new("item", DataType::Int32, true)));
13999 assert!(can_cast_types(list.data_type(), &target_type));
14000 let cast_result = cast(&list, &target_type).unwrap();
14001
14002 let got_list_view = cast_result.as_list_view::<i64>();
14003 let expected_list_view =
14004 LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
14005 assert_eq!(got_list_view, &expected_list_view);
14006
14007 let list = LargeListArray::from_iter_primitive::<Int32Type, _, _>(vec![
14009 Some(vec![Some(1), Some(2)]),
14010 None,
14011 Some(vec![None, Some(3)]),
14012 ]);
14013 let target_type =
14014 DataType::LargeListView(Arc::new(Field::new("item", DataType::Float32, true)));
14015 assert!(can_cast_types(list.data_type(), &target_type));
14016 let cast_result = cast(&list, &target_type).unwrap();
14017
14018 let got_list_view = cast_result.as_list_view::<i64>();
14019 let expected_list_view =
14020 LargeListViewArray::from_iter_primitive::<Float32Type, _, _>(vec![
14021 Some(vec![Some(1.0), Some(2.0)]),
14022 None,
14023 Some(vec![None, Some(3.0)]),
14024 ]);
14025 assert_eq!(got_list_view, &expected_list_view);
14026 }
14027
14028 #[test]
14029 fn test_cast_large_list_to_list_view() {
14030 let list = LargeListArray::from_iter_primitive::<Int32Type, _, _>(vec![
14031 Some(vec![Some(1), Some(2)]),
14032 None,
14033 Some(vec![None, Some(3)]),
14034 ]);
14035 let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Float32, true)));
14036 assert!(can_cast_types(list.data_type(), &target_type));
14037 let cast_result = cast(&list, &target_type).unwrap();
14038
14039 let got_list_view = cast_result.as_list_view::<i32>();
14040 let expected_list_view = ListViewArray::from_iter_primitive::<Float32Type, _, _>(vec![
14041 Some(vec![Some(1.0), Some(2.0)]),
14042 None,
14043 Some(vec![None, Some(3.0)]),
14044 ]);
14045 assert_eq!(got_list_view, &expected_list_view);
14046 }
14047
14048 #[test]
14049 fn test_cast_list_view_to_list_out_of_order() {
14050 let list_view = ListViewArray::new(
14051 Arc::new(Field::new("item", DataType::Int32, true)),
14052 ScalarBuffer::from(vec![0, 6, 3]),
14053 ScalarBuffer::from(vec![3, 3, 3]),
14054 Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5, 6, 7, 8, 9])),
14055 None,
14056 );
14057 let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
14058 assert!(can_cast_types(list_view.data_type(), &target_type));
14059 let cast_result = cast(&list_view, &target_type).unwrap();
14060 let got_list = cast_result.as_list::<i32>();
14061 let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
14062 Some(vec![Some(1), Some(2), Some(3)]),
14063 Some(vec![Some(7), Some(8), Some(9)]),
14064 Some(vec![Some(4), Some(5), Some(6)]),
14065 ]);
14066 assert_eq!(got_list, &expected_list);
14067 }
14068
14069 #[test]
14070 fn test_cast_list_view_to_list_overlapping() {
14071 let list_view = ListViewArray::new(
14072 Arc::new(Field::new("item", DataType::Int32, true)),
14073 ScalarBuffer::from(vec![0, 0]),
14074 ScalarBuffer::from(vec![1, 2]),
14075 Arc::new(Int32Array::from(vec![1, 2])),
14076 None,
14077 );
14078 let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
14079 assert!(can_cast_types(list_view.data_type(), &target_type));
14080 let cast_result = cast(&list_view, &target_type).unwrap();
14081 let got_list = cast_result.as_list::<i32>();
14082 let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
14083 Some(vec![Some(1)]),
14084 Some(vec![Some(1), Some(2)]),
14085 ]);
14086 assert_eq!(got_list, &expected_list);
14087 }
14088
14089 #[test]
14090 fn test_cast_list_view_to_list_empty() {
14091 let values: Vec<Option<Vec<Option<i32>>>> = vec![];
14092 let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(values.clone());
14093 let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
14094 assert!(can_cast_types(list_view.data_type(), &target_type));
14095 let cast_result = cast(&list_view, &target_type).unwrap();
14096 let got_list = cast_result.as_list::<i32>();
14097 let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(values);
14098 assert_eq!(got_list, &expected_list);
14099 }
14100
14101 #[test]
14102 fn test_cast_list_view_to_list_different_inner_type() {
14103 let values = int32_list_values();
14104 let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(values.clone());
14105 let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int64, true)));
14106 assert!(can_cast_types(list_view.data_type(), &target_type));
14107 let cast_result = cast(&list_view, &target_type).unwrap();
14108 let got_list = cast_result.as_list::<i32>();
14109
14110 let expected_list =
14111 ListArray::from_iter_primitive::<Int64Type, _, _>(values.into_iter().map(|list| {
14112 list.map(|list| {
14113 list.into_iter()
14114 .map(|v| v.map(|v| v as i64))
14115 .collect::<Vec<_>>()
14116 })
14117 }));
14118 assert_eq!(got_list, &expected_list);
14119 }
14120
14121 #[test]
14122 fn test_cast_list_view_to_list_out_of_order_with_nulls() {
14123 let list_view = ListViewArray::new(
14124 Arc::new(Field::new("item", DataType::Int32, true)),
14125 ScalarBuffer::from(vec![0, 6, 3]),
14126 ScalarBuffer::from(vec![3, 3, 3]),
14127 Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5, 6, 7, 8, 9])),
14128 Some(NullBuffer::from(vec![false, true, false])),
14129 );
14130 let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
14131 assert!(can_cast_types(list_view.data_type(), &target_type));
14132 let cast_result = cast(&list_view, &target_type).unwrap();
14133 let got_list = cast_result.as_list::<i32>();
14134 let expected_list = ListArray::new(
14135 Arc::new(Field::new("item", DataType::Int32, true)),
14136 OffsetBuffer::from_lengths([3, 3, 3]),
14137 Arc::new(Int32Array::from(vec![1, 2, 3, 7, 8, 9, 4, 5, 6])),
14138 Some(NullBuffer::from(vec![false, true, false])),
14139 );
14140 assert_eq!(got_list, &expected_list);
14141 }
14142
14143 #[test]
14144 fn test_cast_list_view_to_large_list_view() {
14145 let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
14146 let target_type =
14147 DataType::LargeListView(Arc::new(Field::new("item", DataType::Int32, true)));
14148 assert!(can_cast_types(list_view.data_type(), &target_type));
14149 let cast_result = cast(&list_view, &target_type).unwrap();
14150 let got = cast_result.as_list_view::<i64>();
14151
14152 let expected =
14153 LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
14154 assert_eq!(got, &expected);
14155 }
14156
14157 #[test]
14158 fn test_cast_large_list_view_to_list_view() {
14159 let list_view =
14160 LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
14161 let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Int32, true)));
14162 assert!(can_cast_types(list_view.data_type(), &target_type));
14163 let cast_result = cast(&list_view, &target_type).unwrap();
14164 let got = cast_result.as_list_view::<i32>();
14165
14166 let expected = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
14167 assert_eq!(got, &expected);
14168 }
14169
14170 #[test]
14171 fn test_cast_time32_second_to_int64() {
14172 let array = Time32SecondArray::from(vec![1000, 2000, 3000]);
14173 let array = Arc::new(array) as Arc<dyn Array>;
14174 let to_type = DataType::Int64;
14175 let cast_options = CastOptions::default();
14176
14177 assert!(can_cast_types(array.data_type(), &to_type));
14178
14179 let result = cast_with_options(&array, &to_type, &cast_options);
14180 assert!(
14181 result.is_ok(),
14182 "Failed to cast Time32(Second) to Int64: {:?}",
14183 result.err()
14184 );
14185
14186 let cast_array = result.unwrap();
14187 let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
14188
14189 assert_eq!(cast_array.value(0), 1000);
14190 assert_eq!(cast_array.value(1), 2000);
14191 assert_eq!(cast_array.value(2), 3000);
14192 }
14193
14194 #[test]
14195 fn test_cast_time32_millisecond_to_int64() {
14196 let array = Time32MillisecondArray::from(vec![1000, 2000, 3000]);
14197 let array = Arc::new(array) as Arc<dyn Array>;
14198 let to_type = DataType::Int64;
14199 let cast_options = CastOptions::default();
14200
14201 assert!(can_cast_types(array.data_type(), &to_type));
14202
14203 let result = cast_with_options(&array, &to_type, &cast_options);
14204 assert!(
14205 result.is_ok(),
14206 "Failed to cast Time32(Millisecond) to Int64: {:?}",
14207 result.err()
14208 );
14209
14210 let cast_array = result.unwrap();
14211 let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
14212
14213 assert_eq!(cast_array.value(0), 1000);
14214 assert_eq!(cast_array.value(1), 2000);
14215 assert_eq!(cast_array.value(2), 3000);
14216 }
14217
14218 #[test]
14219 fn test_cast_time32_millisecond_to_time64_nanosecond() {
14220 let array =
14221 Time32MillisecondArray::from(vec![Some(1_000), Some(2_000), None, Some(43_200_000)]);
14222 let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
14223 let c = b.as_primitive::<Time64NanosecondType>();
14224 assert_eq!(c.value(0), 1_000_000_000);
14225 assert_eq!(c.value(1), 2_000_000_000);
14226 assert!(c.is_null(2));
14227 assert_eq!(c.value(3), 43_200_000_000_000);
14228 }
14229
14230 #[test]
14231 fn test_cast_time32_millisecond_to_time64_microsecond() {
14232 let array =
14233 Time32MillisecondArray::from(vec![Some(1_000), Some(2_000), None, Some(43_200_000)]);
14234 let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
14235 let c = b.as_primitive::<Time64MicrosecondType>();
14236 assert_eq!(c.value(0), 1_000_000);
14237 assert_eq!(c.value(1), 2_000_000);
14238 assert!(c.is_null(2));
14239 assert_eq!(c.value(3), 43_200_000_000);
14240 }
14241
14242 #[test]
14243 fn test_cast_time32_second_to_time64_nanosecond() {
14244 let array = Time32SecondArray::from(vec![Some(1), Some(60), None, Some(43_200)]);
14245 let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
14246 let c = b.as_primitive::<Time64NanosecondType>();
14247 assert_eq!(c.value(0), 1_000_000_000);
14248 assert_eq!(c.value(1), 60_000_000_000);
14249 assert!(c.is_null(2));
14250 assert_eq!(c.value(3), 43_200_000_000_000);
14251 }
14252
14253 #[test]
14254 fn test_cast_time32_second_to_time64_microsecond() {
14255 let array = Time32SecondArray::from(vec![Some(1), Some(60), None, Some(43_200)]);
14256 let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
14257 let c = b.as_primitive::<Time64MicrosecondType>();
14258 assert_eq!(c.value(0), 1_000_000);
14259 assert_eq!(c.value(1), 60_000_000);
14260 assert!(c.is_null(2));
14261 assert_eq!(c.value(3), 43_200_000_000);
14262 }
14263
14264 #[test]
14265 fn test_cast_time32_second_to_time32_millisecond_overflow() {
14266 let array = Time32SecondArray::from(vec![i32::MAX]);
14267
14268 let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
14269 let c = b.as_primitive::<Time32MillisecondType>();
14270 assert!(c.is_null(0));
14271
14272 let options = CastOptions {
14273 safe: false,
14274 ..Default::default()
14275 };
14276 let err = cast_with_options(&array, &DataType::Time32(TimeUnit::Millisecond), &options)
14277 .unwrap_err();
14278 assert!(err.to_string().contains("Overflow"), "{err}");
14279 }
14280
14281 fn assert_temporal_overflow_is_safe(array: &dyn Array, to_type: &DataType) {
14282 let result = cast(array, to_type).unwrap();
14283 assert_eq!(result.null_count(), array.len());
14284
14285 let options = CastOptions {
14286 safe: false,
14287 ..Default::default()
14288 };
14289 assert!(cast_with_options(array, to_type, &options).is_err());
14290 }
14291
14292 #[test]
14293 fn test_cast_time64_overflow() {
14294 let microseconds = Time64MicrosecondArray::from(vec![i64::MIN, i64::MAX]);
14295 for to_type in [
14296 DataType::Time32(TimeUnit::Second),
14297 DataType::Time32(TimeUnit::Millisecond),
14298 DataType::Time64(TimeUnit::Nanosecond),
14299 ] {
14300 assert_temporal_overflow_is_safe(µseconds, &to_type);
14301 }
14302
14303 let nanoseconds = Time64NanosecondArray::from(vec![i64::MIN, i64::MAX]);
14304 for to_type in [
14305 DataType::Time32(TimeUnit::Second),
14306 DataType::Time32(TimeUnit::Millisecond),
14307 ] {
14308 assert_temporal_overflow_is_safe(&nanoseconds, &to_type);
14309 }
14310 }
14311
14312 #[test]
14313 fn test_cast_date64_to_timestamp_overflow() {
14314 let array = Date64Array::from(vec![i64::MIN, i64::MAX]);
14315 for to_type in [
14316 DataType::Timestamp(TimeUnit::Microsecond, None),
14317 DataType::Timestamp(TimeUnit::Nanosecond, None),
14318 ] {
14319 assert_temporal_overflow_is_safe(&array, &to_type);
14320 }
14321 }
14322
14323 #[test]
14324 fn test_cast_time64_to_time32_boundaries() {
14325 fn check<FROM, TO>(divisor: i64)
14326 where
14327 FROM: ArrowPrimitiveType<Native = i64>,
14328 TO: ArrowPrimitiveType<Native = i32>,
14329 {
14330 let min = <i64 as From<i32>>::from(i32::MIN) * divisor - (divisor - 1);
14333 let max = <i64 as From<i32>>::from(i32::MAX) * divisor + (divisor - 1);
14334 let array = PrimitiveArray::<FROM>::new(
14335 vec![i64::MAX, min - 1, min, -divisor + 1, i64::MAX, max, max + 1].into(),
14336 Some(vec![true, true, true, true, false, true, true].into()),
14337 )
14338 .slice(1, 6);
14339 let expected = PrimitiveArray::<TO>::from_iter([
14340 None,
14341 Some(i32::MIN),
14342 Some(0),
14343 None,
14344 Some(i32::MAX),
14345 None,
14346 ]);
14347 let result = cast(&array, &TO::DATA_TYPE).unwrap();
14348 assert_eq!(result.as_primitive::<TO>(), &expected);
14349
14350 let options = CastOptions {
14351 safe: false,
14352 ..Default::default()
14353 };
14354 assert!(cast_with_options(&array, &TO::DATA_TYPE, &options).is_err());
14355 let result = cast_with_options(&array.slice(1, 4), &TO::DATA_TYPE, &options).unwrap();
14357 assert_eq!(result.as_primitive::<TO>(), &expected.slice(1, 4));
14358 }
14359 check::<Time64MicrosecondType, Time32SecondType>(MICROSECONDS);
14360 check::<Time64MicrosecondType, Time32MillisecondType>(MICROSECONDS / MILLISECONDS);
14361 check::<Time64NanosecondType, Time32SecondType>(NANOSECONDS);
14362 check::<Time64NanosecondType, Time32MillisecondType>(NANOSECONDS / MILLISECONDS);
14363 }
14364
14365 #[test]
14366 fn test_cast_temporal_scaling_boundaries() {
14367 fn check<FROM, TO>(multiplier: i64)
14368 where
14369 FROM: ArrowPrimitiveType<Native = i64>,
14370 TO: ArrowPrimitiveType<Native = i64>,
14371 {
14372 let min = i64::MIN / multiplier;
14373 let max = i64::MAX / multiplier;
14374 let array = PrimitiveArray::<FROM>::new(
14375 vec![i64::MAX, min - 1, min, -1, i64::MAX, max, max + 1].into(),
14376 Some(vec![true, true, true, true, false, true, true].into()),
14377 )
14378 .slice(1, 6);
14379 let expected = PrimitiveArray::<TO>::from_iter([
14380 None,
14381 Some(min * multiplier),
14382 Some(-multiplier),
14383 None,
14384 Some(max * multiplier),
14385 None,
14386 ]);
14387 let result = cast(&array, &TO::DATA_TYPE).unwrap();
14388 assert_eq!(result.as_primitive::<TO>(), &expected);
14389 let options = CastOptions {
14390 safe: false,
14391 ..Default::default()
14392 };
14393 assert!(cast_with_options(&array, &TO::DATA_TYPE, &options).is_err());
14394 let result = cast_with_options(&array.slice(1, 4), &TO::DATA_TYPE, &options).unwrap();
14395 assert_eq!(result.as_primitive::<TO>(), &expected.slice(1, 4));
14396 }
14397 check::<Time64MicrosecondType, Time64NanosecondType>(NANOSECONDS / MICROSECONDS);
14398 check::<Date64Type, TimestampMicrosecondType>(MICROSECONDS / MILLISECONDS);
14399 check::<Date64Type, TimestampNanosecondType>(NANOSECONDS / MILLISECONDS);
14400 }
14401
14402 #[test]
14403 fn test_cast_string_to_time32_second_to_int64() {
14404 let array = StringArray::from(vec!["03:12:44"]);
14407 let array = Arc::new(array) as Arc<dyn Array>;
14408 let cast_options = CastOptions::default();
14409
14410 let time32_type = DataType::Time32(TimeUnit::Second);
14412 let time32_array = cast_with_options(&array, &time32_type, &cast_options).unwrap();
14413
14414 let int64_type = DataType::Int64;
14416 assert!(can_cast_types(time32_array.data_type(), &int64_type));
14417
14418 let result = cast_with_options(&time32_array, &int64_type, &cast_options);
14419
14420 assert!(
14421 result.is_ok(),
14422 "Failed to cast Time32(Second) to Int64: {:?}",
14423 result.err()
14424 );
14425
14426 let cast_array = result.unwrap();
14427 let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
14428
14429 assert_eq!(cast_array.value(0), 11564);
14431 }
14432 #[test]
14433 fn test_string_dicts_to_binary_view() {
14434 let expected = BinaryViewArray::from_iter(vec![
14435 VIEW_TEST_DATA[1],
14436 VIEW_TEST_DATA[0],
14437 None,
14438 VIEW_TEST_DATA[3],
14439 None,
14440 VIEW_TEST_DATA[1],
14441 VIEW_TEST_DATA[4],
14442 ]);
14443
14444 let values_arrays: [ArrayRef; _] = [
14445 Arc::new(StringArray::from_iter(VIEW_TEST_DATA)),
14446 Arc::new(StringViewArray::from_iter(VIEW_TEST_DATA)),
14447 Arc::new(LargeStringArray::from_iter(VIEW_TEST_DATA)),
14448 ];
14449 for values in values_arrays {
14450 let keys =
14451 Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
14452 let string_dict_array = DictionaryArray::<Int8Type>::try_new(keys, values).unwrap();
14453
14454 let casted = cast(&string_dict_array, &DataType::BinaryView).unwrap();
14455 assert_eq!(casted.as_ref(), &expected);
14456 }
14457 }
14458
14459 #[test]
14460 fn test_binary_dicts_to_string_view() {
14461 let expected = StringViewArray::from_iter(vec![
14462 VIEW_TEST_DATA[1],
14463 VIEW_TEST_DATA[0],
14464 None,
14465 VIEW_TEST_DATA[3],
14466 None,
14467 VIEW_TEST_DATA[1],
14468 VIEW_TEST_DATA[4],
14469 ]);
14470
14471 let values_arrays: [ArrayRef; _] = [
14472 Arc::new(BinaryArray::from_iter(VIEW_TEST_DATA)),
14473 Arc::new(BinaryViewArray::from_iter(VIEW_TEST_DATA)),
14474 Arc::new(LargeBinaryArray::from_iter(VIEW_TEST_DATA)),
14475 ];
14476 for values in values_arrays {
14477 let keys =
14478 Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
14479 let string_dict_array = DictionaryArray::<Int8Type>::try_new(keys, values).unwrap();
14480
14481 let casted = cast(&string_dict_array, &DataType::Utf8View).unwrap();
14482 assert_eq!(casted.as_ref(), &expected);
14483 }
14484 }
14485
14486 #[test]
14487 fn test_cast_between_sliced_run_end_encoded() {
14488 let run_ends = Int16Array::from(vec![2, 5, 8]);
14489 let values = StringArray::from(vec!["a", "b", "c"]);
14490
14491 let ree_array = RunArray::<Int16Type>::try_new(&run_ends, &values).unwrap();
14492 let ree_array = ree_array.slice(1, 2);
14493 let array_ref = Arc::new(ree_array) as ArrayRef;
14494
14495 let target_type = DataType::RunEndEncoded(
14496 Arc::new(Field::new(
14497 Field::REE_RUN_ENDS_FIELD_DEFAULT_NAME,
14498 DataType::Int64,
14499 false,
14500 )),
14501 Arc::new(Field::new(
14502 Field::REE_VALUES_FIELD_DEFAULT_NAME,
14503 DataType::Utf8,
14504 true,
14505 )),
14506 );
14507 let cast_options = CastOptions {
14508 safe: false,
14509 format_options: FormatOptions::default(),
14510 };
14511
14512 let result = cast_with_options(&array_ref, &target_type, &cast_options).unwrap();
14513 let run_array = result.as_run::<Int64Type>();
14514 let run_array = run_array.downcast::<StringArray>().unwrap();
14515
14516 let expected = vec!["a", "b"];
14517 let actual = run_array.into_iter().flatten().collect::<Vec<_>>();
14518
14519 assert_eq!(expected, actual);
14520 }
14521}