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