1mod decimal;
41mod dictionary;
42mod list;
43mod map;
44mod run_array;
45mod string;
46
47use crate::cast::decimal::*;
48use crate::cast::dictionary::*;
49use crate::cast::list::*;
50use crate::cast::map::*;
51use crate::cast::run_array::*;
52use crate::cast::string::*;
53
54use arrow_buffer::IntervalMonthDayNano;
55use arrow_data::ByteView;
56use chrono::{NaiveTime, Offset, TimeZone, Utc};
57use std::cmp::Ordering;
58use std::sync::Arc;
59
60use crate::display::{ArrayFormatter, FormatOptions};
61use crate::parse::{
62 Parser, parse_interval_day_time, parse_interval_month_day_nano, parse_interval_year_month,
63 string_to_datetime,
64};
65use arrow_array::{builder::*, cast::*, temporal_conversions::*, timezone::Tz, types::*, *};
66use arrow_buffer::{ArrowNativeType, OffsetBuffer, i256};
67use arrow_data::ArrayData;
68use arrow_data::transform::MutableArrayData;
69use arrow_schema::*;
70use arrow_select::take::take;
71use num_traits::{NumCast, ToPrimitive, cast::AsPrimitive};
72
73pub use decimal::{DecimalCast, rescale_decimal};
74
75#[derive(Debug, Clone, PartialEq, Eq, Hash)]
77pub struct CastOptions<'a> {
78 pub safe: bool,
80 pub format_options: FormatOptions<'a>,
82}
83
84impl Default for CastOptions<'_> {
85 fn default() -> Self {
86 Self {
87 safe: true,
88 format_options: FormatOptions::default(),
89 }
90 }
91}
92
93pub fn can_cast_types(from_type: &DataType, to_type: &DataType) -> bool {
97 use self::DataType::*;
98 use self::IntervalUnit::*;
99 use self::TimeUnit::*;
100 if from_type == to_type {
101 return true;
102 }
103
104 match (from_type, to_type) {
105 (Null, _) => true,
106 (Dictionary(_, from_value_type), Dictionary(_, to_value_type)) => {
108 can_cast_types(from_value_type, to_value_type)
109 }
110 (Dictionary(_, value_type), _) => can_cast_types(value_type, to_type),
111 (RunEndEncoded(_, value_type), _) => can_cast_types(value_type.data_type(), to_type),
112 (_, RunEndEncoded(_, value_type)) => can_cast_types(from_type, value_type.data_type()),
113 (_, Dictionary(_, value_type)) => can_cast_types(from_type, value_type),
114 (
115 List(list_from) | LargeList(list_from) | ListView(list_from) | LargeListView(list_from),
116 List(list_to) | LargeList(list_to) | ListView(list_to) | LargeListView(list_to),
117 ) => can_cast_types(list_from.data_type(), list_to.data_type()),
118 (
119 List(list_from) | LargeList(list_from) | ListView(list_from) | LargeListView(list_from),
120 Utf8 | LargeUtf8 | Utf8View,
121 ) => can_cast_types(list_from.data_type(), to_type),
122 (
123 FixedSizeList(list_from, _),
124 List(list_to) | LargeList(list_to) | ListView(list_to) | LargeListView(list_to),
125 ) => can_cast_types(list_from.data_type(), list_to.data_type()),
126 (
127 List(list_from) | LargeList(list_from) | ListView(list_from) | LargeListView(list_from),
128 FixedSizeList(list_to, _),
129 ) => can_cast_types(list_from.data_type(), list_to.data_type()),
130 (FixedSizeList(inner, size), FixedSizeList(inner_to, size_to)) if size == size_to => {
131 can_cast_types(inner.data_type(), inner_to.data_type())
132 }
133 (_, List(list_to) | LargeList(list_to) | ListView(list_to) | LargeListView(list_to)) => {
134 can_cast_types(from_type, list_to.data_type())
135 }
136 (_, FixedSizeList(list_to, size)) if *size == 1 => {
137 can_cast_types(from_type, list_to.data_type())
138 }
139 (FixedSizeList(list_from, size), _) if *size == 1 => {
140 can_cast_types(list_from.data_type(), to_type)
141 }
142 (Map(from_entries, ordered_from), Map(to_entries, ordered_to))
143 if ordered_from == ordered_to =>
144 {
145 match (
146 key_field(from_entries),
147 key_field(to_entries),
148 value_field(from_entries),
149 value_field(to_entries),
150 ) {
151 (Some(from_key), Some(to_key), Some(from_value), Some(to_value)) => {
152 can_cast_types(from_key.data_type(), to_key.data_type())
153 && can_cast_types(from_value.data_type(), to_value.data_type())
154 }
155 _ => false,
156 }
157 }
158 (
160 Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
161 Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
162 ) => true,
163 (
165 UInt8 | UInt16 | UInt32 | UInt64,
166 Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
167 ) => true,
168 (
170 Int8 | Int16 | Int32 | Int64 | Float32 | Float64,
171 Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
172 ) => true,
173 (
175 Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
176 UInt8 | UInt16 | UInt32 | UInt64,
177 ) => true,
178 (
180 Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
181 Null | Int8 | Int16 | Int32 | Int64 | Float32 | Float64,
182 ) => true,
183 (
185 Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
186 Utf8View | Utf8 | LargeUtf8,
187 ) => true,
188 (
190 Utf8View | Utf8 | LargeUtf8,
191 Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
192 ) => true,
193 (Struct(from_fields), Struct(to_fields)) => {
194 if from_fields.len() != to_fields.len() {
195 return false;
196 }
197
198 if from_fields
200 .iter()
201 .zip(to_fields.iter())
202 .all(|(f1, f2)| f1.name() == f2.name())
203 {
204 return from_fields.iter().zip(to_fields.iter()).all(|(f1, f2)| {
205 can_cast_types(f1.data_type(), f2.data_type())
208 });
209 }
210
211 if to_fields.iter().all(|to_field| {
213 from_fields
214 .iter()
215 .find(|from_field| from_field.name() == to_field.name())
216 .is_some_and(|from_field| {
217 can_cast_types(from_field.data_type(), to_field.data_type())
220 })
221 }) {
222 return true;
223 }
224
225 from_fields
227 .iter()
228 .zip(to_fields.iter())
229 .all(|(f1, f2)| can_cast_types(f1.data_type(), f2.data_type()))
230 }
231 (Struct(_), _) => false,
232 (_, Struct(_)) => false,
233
234 (_, Boolean) => from_type.is_integer() || from_type.is_floating() || from_type.is_string(),
235 (Boolean, _) => to_type.is_integer() || to_type.is_floating() || to_type.is_string(),
236
237 (Binary, LargeBinary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView | Utf8View) => {
238 true
239 }
240 (LargeBinary, Binary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView | Utf8View) => {
241 true
242 }
243 (FixedSizeBinary(_), Binary | LargeBinary | BinaryView) => true,
244 (
245 Utf8 | LargeUtf8 | Utf8View,
246 Binary
247 | LargeBinary
248 | Utf8
249 | LargeUtf8
250 | Date32
251 | Date64
252 | Time32(Second)
253 | Time32(Millisecond)
254 | Time64(Microsecond)
255 | Time64(Nanosecond)
256 | Timestamp(Second, _)
257 | Timestamp(Millisecond, _)
258 | Timestamp(Microsecond, _)
259 | Timestamp(Nanosecond, _)
260 | Interval(_)
261 | BinaryView,
262 ) => true,
263 (Utf8 | LargeUtf8, Utf8View) => true,
264 (BinaryView, Binary | LargeBinary | Utf8 | LargeUtf8 | Utf8View) => true,
265 (Utf8View | Utf8 | LargeUtf8, _) => to_type.is_numeric(),
266 (_, Utf8 | Utf8View | LargeUtf8) => from_type.is_primitive(),
267
268 (_, Binary | LargeBinary) => from_type.is_integer(),
269
270 (
272 UInt8 | UInt16 | UInt32 | UInt64 | Int8 | Int16 | Int32 | Int64 | Float16 | Float32
273 | Float64,
274 UInt8 | UInt16 | UInt32 | UInt64 | Int8 | Int16 | Int32 | Int64 | Float16 | Float32
275 | Float64,
276 ) => true,
277 (Int32, Date32 | Date64 | Time32(_)) => true,
281 (Date32, Int32 | Int64) => true,
282 (Time32(_), Int32 | Int64) => true,
283 (Int64, Date64 | Date32 | Time64(_)) => true,
284 (Date64, Int64 | Int32) => true,
285 (Time64(_), Int64) => true,
286 (Date32 | Date64, Date32 | Date64) => true,
287 (Time32(_), Time32(_)) => true,
289 (Time32(_), Time64(_)) => true,
290 (Time64(_), Time64(_)) => true,
291 (Time64(_), Time32(to_unit)) => {
292 matches!(to_unit, Second | Millisecond)
293 }
294 (Timestamp(_, _), _) if to_type.is_numeric() => true,
295 (_, Timestamp(_, _)) if from_type.is_numeric() => true,
296 (Date64, Timestamp(_, _)) => true,
297 (Date32, Timestamp(_, _)) => true,
298 (
299 Timestamp(_, _),
300 Timestamp(_, _)
301 | Date32
302 | Date64
303 | Time32(Second)
304 | Time32(Millisecond)
305 | Time64(Microsecond)
306 | Time64(Nanosecond),
307 ) => true,
308 (_, Duration(_)) if from_type.is_numeric() => true,
309 (Duration(_), _) if to_type.is_numeric() => true,
310 (Duration(_), Duration(_)) => true,
311 (Interval(from_type), Int64) => {
312 match from_type {
313 YearMonth => true,
314 DayTime => true,
315 MonthDayNano => false, }
317 }
318 (Int32, Interval(to_type)) => match to_type {
319 YearMonth => true,
320 DayTime => false,
321 MonthDayNano => false,
322 },
323 (Duration(_), Interval(MonthDayNano)) => true,
324 (Interval(MonthDayNano), Duration(_)) => true,
325 (Interval(YearMonth), Interval(MonthDayNano)) => true,
326 (Interval(DayTime), Interval(MonthDayNano)) => true,
327 (_, _) => false,
328 }
329}
330
331pub fn cast(array: &dyn Array, to_type: &DataType) -> Result<ArrayRef, ArrowError> {
335 cast_with_options(array, to_type, &CastOptions::default())
336}
337
338fn cast_integer_to_decimal<
339 T: ArrowPrimitiveType,
340 D: DecimalType + ArrowPrimitiveType<Native = M>,
341 M,
342>(
343 array: &PrimitiveArray<T>,
344 precision: u8,
345 scale: i8,
346 base: M,
347 cast_options: &CastOptions,
348) -> Result<ArrayRef, ArrowError>
349where
350 <T as ArrowPrimitiveType>::Native: AsPrimitive<M>,
351 M: ArrowNativeTypeOp,
352{
353 let scale_factor = base.pow_checked(scale.unsigned_abs() as u32).map_err(|_| {
354 ArrowError::CastError(format!(
355 "Cannot cast to {:?}({}, {}). The scale causes overflow.",
356 D::PREFIX,
357 precision,
358 scale,
359 ))
360 })?;
361
362 let array = if scale < 0 {
363 match cast_options.safe {
364 true => array.unary_opt::<_, D>(|v| {
365 v.as_()
366 .div_checked(scale_factor)
367 .ok()
368 .and_then(|v| (D::is_valid_decimal_precision(v, precision)).then_some(v))
369 }),
370 false => array.try_unary::<_, D, _>(|v| {
371 v.as_()
372 .div_checked(scale_factor)
373 .and_then(|v| D::validate_decimal_precision(v, precision, scale).map(|_| v))
374 })?,
375 }
376 } else {
377 match cast_options.safe {
378 true => array.unary_opt::<_, D>(|v| {
379 v.as_()
380 .mul_checked(scale_factor)
381 .ok()
382 .and_then(|v| (D::is_valid_decimal_precision(v, precision)).then_some(v))
383 }),
384 false => array.try_unary::<_, D, _>(|v| {
385 v.as_()
386 .mul_checked(scale_factor)
387 .and_then(|v| D::validate_decimal_precision(v, precision, scale).map(|_| v))
388 })?,
389 }
390 };
391
392 Ok(Arc::new(array.with_precision_and_scale(precision, scale)?))
393}
394
395fn cast_interval_year_month_to_interval_month_day_nano(
397 array: &dyn Array,
398 _cast_options: &CastOptions,
399) -> Result<ArrayRef, ArrowError> {
400 let array = array.as_primitive::<IntervalYearMonthType>();
401
402 Ok(Arc::new(array.unary::<_, IntervalMonthDayNanoType>(|v| {
403 let months = IntervalYearMonthType::to_months(v);
404 IntervalMonthDayNanoType::make_value(months, 0, 0)
405 })))
406}
407
408fn cast_interval_day_time_to_interval_month_day_nano(
410 array: &dyn Array,
411 _cast_options: &CastOptions,
412) -> Result<ArrayRef, ArrowError> {
413 let array = array.as_primitive::<IntervalDayTimeType>();
414 let mul = 1_000_000;
415
416 Ok(Arc::new(array.unary::<_, IntervalMonthDayNanoType>(|v| {
417 let (days, ms) = IntervalDayTimeType::to_parts(v);
418 IntervalMonthDayNanoType::make_value(0, days, ms as i64 * mul)
419 })))
420}
421
422fn cast_month_day_nano_to_duration<D: ArrowTemporalType<Native = i64>>(
424 array: &dyn Array,
425 cast_options: &CastOptions,
426) -> Result<ArrayRef, ArrowError> {
427 let array = array.as_primitive::<IntervalMonthDayNanoType>();
428 let scale = match D::DATA_TYPE {
429 DataType::Duration(TimeUnit::Second) => 1_000_000_000,
430 DataType::Duration(TimeUnit::Millisecond) => 1_000_000,
431 DataType::Duration(TimeUnit::Microsecond) => 1_000,
432 DataType::Duration(TimeUnit::Nanosecond) => 1,
433 _ => unreachable!(),
434 };
435
436 if cast_options.safe {
437 let iter = array.iter().map(|v| {
438 v.and_then(|v| (v.days == 0 && v.months == 0).then_some(v.nanoseconds / scale))
439 });
440 Ok(Arc::new(unsafe {
441 PrimitiveArray::<D>::from_trusted_len_iter(iter)
442 }))
443 } else {
444 let vec = array
445 .iter()
446 .map(|v| {
447 v.map(|v| match v.days == 0 && v.months == 0 {
448 true => Ok((v.nanoseconds) / scale),
449 _ => Err(ArrowError::ComputeError(
450 "Cannot convert interval containing non-zero months or days to duration"
451 .to_string(),
452 )),
453 })
454 .transpose()
455 })
456 .collect::<Result<Vec<_>, _>>()?;
457 Ok(Arc::new(unsafe {
458 PrimitiveArray::<D>::from_trusted_len_iter(vec.iter())
459 }))
460 }
461}
462
463fn cast_duration_to_interval<D: ArrowTemporalType<Native = i64>>(
465 array: &dyn Array,
466 cast_options: &CastOptions,
467) -> Result<ArrayRef, ArrowError> {
468 let array = array
469 .as_any()
470 .downcast_ref::<PrimitiveArray<D>>()
471 .ok_or_else(|| {
472 ArrowError::ComputeError(
473 "Internal Error: Cannot cast duration to DurationArray of expected type"
474 .to_string(),
475 )
476 })?;
477
478 let scale = match array.data_type() {
479 DataType::Duration(TimeUnit::Second) => 1_000_000_000,
480 DataType::Duration(TimeUnit::Millisecond) => 1_000_000,
481 DataType::Duration(TimeUnit::Microsecond) => 1_000,
482 DataType::Duration(TimeUnit::Nanosecond) => 1,
483 _ => unreachable!(),
484 };
485
486 if cast_options.safe {
487 let iter = array.iter().map(|v| {
488 v.and_then(|v| {
489 v.checked_mul(scale)
490 .map(|v| IntervalMonthDayNano::new(0, 0, v))
491 })
492 });
493 Ok(Arc::new(unsafe {
494 PrimitiveArray::<IntervalMonthDayNanoType>::from_trusted_len_iter(iter)
495 }))
496 } else {
497 let vec = array
498 .iter()
499 .map(|v| {
500 v.map(|v| {
501 if let Ok(v) = v.mul_checked(scale) {
502 Ok(IntervalMonthDayNano::new(0, 0, v))
503 } else {
504 Err(ArrowError::ComputeError(format!(
505 "Cannot cast to {:?}. Overflowing on {:?}",
506 IntervalMonthDayNanoType::DATA_TYPE,
507 v
508 )))
509 }
510 })
511 .transpose()
512 })
513 .collect::<Result<Vec<_>, _>>()?;
514 Ok(Arc::new(unsafe {
515 PrimitiveArray::<IntervalMonthDayNanoType>::from_trusted_len_iter(vec.iter())
516 }))
517 }
518}
519
520fn cast_reinterpret_arrays<I: ArrowPrimitiveType, O: ArrowPrimitiveType<Native = I::Native>>(
522 array: &dyn Array,
523) -> Result<ArrayRef, ArrowError> {
524 Ok(Arc::new(array.as_primitive::<I>().reinterpret_cast::<O>()))
525}
526
527fn make_timestamp_array(
528 array: &PrimitiveArray<Int64Type>,
529 unit: TimeUnit,
530 tz: Option<Arc<str>>,
531) -> ArrayRef {
532 match unit {
533 TimeUnit::Second => Arc::new(
534 array
535 .reinterpret_cast::<TimestampSecondType>()
536 .with_timezone_opt(tz),
537 ),
538 TimeUnit::Millisecond => Arc::new(
539 array
540 .reinterpret_cast::<TimestampMillisecondType>()
541 .with_timezone_opt(tz),
542 ),
543 TimeUnit::Microsecond => Arc::new(
544 array
545 .reinterpret_cast::<TimestampMicrosecondType>()
546 .with_timezone_opt(tz),
547 ),
548 TimeUnit::Nanosecond => Arc::new(
549 array
550 .reinterpret_cast::<TimestampNanosecondType>()
551 .with_timezone_opt(tz),
552 ),
553 }
554}
555
556fn make_duration_array(array: &PrimitiveArray<Int64Type>, unit: TimeUnit) -> ArrayRef {
557 match unit {
558 TimeUnit::Second => Arc::new(array.reinterpret_cast::<DurationSecondType>()),
559 TimeUnit::Millisecond => Arc::new(array.reinterpret_cast::<DurationMillisecondType>()),
560 TimeUnit::Microsecond => Arc::new(array.reinterpret_cast::<DurationMicrosecondType>()),
561 TimeUnit::Nanosecond => Arc::new(array.reinterpret_cast::<DurationNanosecondType>()),
562 }
563}
564
565fn as_time_res_with_timezone<T: ArrowPrimitiveType>(
566 v: i64,
567 tz: Option<Tz>,
568) -> Result<NaiveTime, ArrowError> {
569 let time = match tz {
570 Some(tz) => as_datetime_with_timezone::<T>(v, tz).map(|d| d.time()),
571 None => as_datetime::<T>(v).map(|d| d.time()),
572 };
573
574 time.ok_or_else(|| {
575 ArrowError::CastError(format!(
576 "Failed to create naive time with {} {}",
577 std::any::type_name::<T>(),
578 v
579 ))
580 })
581}
582
583fn timestamp_to_date32<T: ArrowTimestampType>(
584 array: &PrimitiveArray<T>,
585) -> Result<ArrayRef, ArrowError> {
586 let err = |x: i64| {
587 ArrowError::CastError(format!(
588 "Cannot convert {} {x} to datetime",
589 std::any::type_name::<T>()
590 ))
591 };
592
593 let array: Date32Array = match array.timezone() {
594 Some(tz) => {
595 let tz: Tz = tz.parse()?;
596 array.try_unary(|x| {
597 as_datetime_with_timezone::<T>(x, tz)
598 .ok_or_else(|| err(x))
599 .map(|d| Date32Type::from_naive_date(d.date_naive()))
600 })?
601 }
602 None => array.try_unary(|x| {
603 as_datetime::<T>(x)
604 .ok_or_else(|| err(x))
605 .map(|d| Date32Type::from_naive_date(d.date()))
606 })?,
607 };
608 Ok(Arc::new(array))
609}
610
611pub fn cast_with_options(
741 array: &dyn Array,
742 to_type: &DataType,
743 cast_options: &CastOptions,
744) -> Result<ArrayRef, ArrowError> {
745 use DataType::*;
746 let from_type = array.data_type();
747 if from_type == to_type {
749 return Ok(make_array(array.to_data()));
750 }
751 match (from_type, to_type) {
752 (Null, _) => Ok(new_null_array(to_type, array.len())),
753 (RunEndEncoded(index_type, _), _) => match index_type.data_type() {
754 Int16 => run_end_encoded_cast::<Int16Type>(array, to_type, cast_options),
755 Int32 => run_end_encoded_cast::<Int32Type>(array, to_type, cast_options),
756 Int64 => run_end_encoded_cast::<Int64Type>(array, to_type, cast_options),
757 _ => Err(ArrowError::CastError(format!(
758 "Casting from run end encoded type {from_type:?} to {to_type:?} not supported",
759 ))),
760 },
761 (_, RunEndEncoded(index_type, value_type)) => {
762 let array_ref = make_array(array.to_data());
763 match index_type.data_type() {
764 Int16 => cast_to_run_end_encoded::<Int16Type>(
765 &array_ref,
766 value_type.data_type(),
767 cast_options,
768 ),
769 Int32 => cast_to_run_end_encoded::<Int32Type>(
770 &array_ref,
771 value_type.data_type(),
772 cast_options,
773 ),
774 Int64 => cast_to_run_end_encoded::<Int64Type>(
775 &array_ref,
776 value_type.data_type(),
777 cast_options,
778 ),
779 _ => Err(ArrowError::CastError(format!(
780 "Casting from type {from_type:?} to run end encoded type {to_type:?} not supported",
781 ))),
782 }
783 }
784 (Dictionary(index_type, _), _) => match **index_type {
785 Int8 => dictionary_cast::<Int8Type>(array, to_type, cast_options),
786 Int16 => dictionary_cast::<Int16Type>(array, to_type, cast_options),
787 Int32 => dictionary_cast::<Int32Type>(array, to_type, cast_options),
788 Int64 => dictionary_cast::<Int64Type>(array, to_type, cast_options),
789 UInt8 => dictionary_cast::<UInt8Type>(array, to_type, cast_options),
790 UInt16 => dictionary_cast::<UInt16Type>(array, to_type, cast_options),
791 UInt32 => dictionary_cast::<UInt32Type>(array, to_type, cast_options),
792 UInt64 => dictionary_cast::<UInt64Type>(array, to_type, cast_options),
793 _ => Err(ArrowError::CastError(format!(
794 "Casting from dictionary type {from_type} to {to_type} not supported",
795 ))),
796 },
797 (_, Dictionary(index_type, value_type)) => match **index_type {
798 Int8 => cast_to_dictionary::<Int8Type>(array, value_type, cast_options),
799 Int16 => cast_to_dictionary::<Int16Type>(array, value_type, cast_options),
800 Int32 => cast_to_dictionary::<Int32Type>(array, value_type, cast_options),
801 Int64 => cast_to_dictionary::<Int64Type>(array, value_type, cast_options),
802 UInt8 => cast_to_dictionary::<UInt8Type>(array, value_type, cast_options),
803 UInt16 => cast_to_dictionary::<UInt16Type>(array, value_type, cast_options),
804 UInt32 => cast_to_dictionary::<UInt32Type>(array, value_type, cast_options),
805 UInt64 => cast_to_dictionary::<UInt64Type>(array, value_type, cast_options),
806 _ => Err(ArrowError::CastError(format!(
807 "Casting from type {from_type} to dictionary type {to_type} not supported",
808 ))),
809 },
810 (List(_), List(to)) => cast_list_values::<i32>(array, to, cast_options),
812 (LargeList(_), LargeList(to)) => cast_list_values::<i64>(array, to, cast_options),
813 (FixedSizeList(_, size_from), FixedSizeList(list_to, size_to)) => {
814 if size_from != size_to {
815 return Err(ArrowError::CastError(
816 "cannot cast fixed-size-list to fixed-size-list with different size".into(),
817 ));
818 }
819 let array = array.as_fixed_size_list();
820 let values = cast_with_options(array.values(), list_to.data_type(), cast_options)?;
821 Ok(Arc::new(FixedSizeListArray::try_new(
822 list_to.clone(),
823 *size_from,
824 values,
825 array.nulls().cloned(),
826 )?))
827 }
828 (ListView(_), ListView(to)) => cast_list_view_values::<i32>(array, to, cast_options),
829 (LargeListView(_), LargeListView(to)) => {
830 cast_list_view_values::<i64>(array, to, cast_options)
831 }
832 (List(_), LargeList(list_to)) => cast_list::<i32, i64>(array, list_to, cast_options),
835 (List(_), FixedSizeList(field, size)) => {
836 cast_list_to_fixed_size_list::<i32>(array, field, *size, cast_options)
837 }
838 (List(_), ListView(list_to)) => {
839 cast_list_to_list_view::<i32, i32>(array, list_to, cast_options)
840 }
841 (List(_), LargeListView(list_to)) => {
842 cast_list_to_list_view::<i32, i64>(array, list_to, cast_options)
843 }
844 (LargeList(_), List(list_to)) => cast_list::<i64, i32>(array, list_to, cast_options),
846 (LargeList(_), FixedSizeList(field, size)) => {
847 cast_list_to_fixed_size_list::<i64>(array, field, *size, cast_options)
848 }
849 (LargeList(_), ListView(list_to)) => {
850 cast_list_to_list_view::<i64, i32>(array, list_to, cast_options)
851 }
852 (LargeList(_), LargeListView(list_to)) => {
853 cast_list_to_list_view::<i64, i64>(array, list_to, cast_options)
854 }
855 (ListView(_), List(list_to)) => {
857 cast_list_view_to_list::<i32, Int32Type>(array, list_to, cast_options)
858 }
859 (ListView(_), LargeList(list_to)) => {
860 cast_list_view_to_list::<i32, Int64Type>(array, list_to, cast_options)
861 }
862 (ListView(_), LargeListView(list_to)) => {
863 cast_list_view::<i32, i64>(array, list_to, cast_options)
864 }
865 (ListView(_), FixedSizeList(field, size)) => {
866 cast_list_view_to_fixed_size_list::<i32>(array, field, *size, cast_options)
867 }
868 (LargeListView(_), LargeList(list_to)) => {
870 cast_list_view_to_list::<i64, Int64Type>(array, list_to, cast_options)
871 }
872 (LargeListView(_), List(list_to)) => {
873 cast_list_view_to_list::<i64, Int32Type>(array, list_to, cast_options)
874 }
875 (LargeListView(_), ListView(list_to)) => {
876 cast_list_view::<i64, i32>(array, list_to, cast_options)
877 }
878 (LargeListView(_), FixedSizeList(field, size)) => {
879 cast_list_view_to_fixed_size_list::<i64>(array, field, *size, cast_options)
880 }
881 (FixedSizeList(_, _), List(list_to)) => {
883 cast_fixed_size_list_to_list::<i32>(array, list_to, cast_options)
884 }
885 (FixedSizeList(_, _), LargeList(list_to)) => {
886 cast_fixed_size_list_to_list::<i64>(array, list_to, cast_options)
887 }
888 (FixedSizeList(_, _), ListView(list_to)) => {
889 cast_fixed_size_list_to_list_view::<i32>(array, list_to, cast_options)
890 }
891 (FixedSizeList(_, _), LargeListView(list_to)) => {
892 cast_fixed_size_list_to_list_view::<i64>(array, list_to, cast_options)
893 }
894 (FixedSizeList(_, size), _) if *size == 1 => {
896 cast_single_element_fixed_size_list_to_values(array, to_type, cast_options)
897 }
898 (List(_) | LargeList(_) | ListView(_) | LargeListView(_), _) => match to_type {
901 Utf8 => value_to_string::<i32>(array, cast_options),
902 LargeUtf8 => value_to_string::<i64>(array, cast_options),
903 Utf8View => value_to_string_view(array, cast_options),
904 _ => Err(ArrowError::CastError(
905 "Cannot cast list to non-list data types".to_string(),
906 )),
907 },
908 (_, List(to)) => cast_values_to_list::<i32>(array, to, cast_options),
909 (_, LargeList(to)) => cast_values_to_list::<i64>(array, to, cast_options),
910 (_, ListView(to)) => cast_values_to_list_view::<i32>(array, to, cast_options),
911 (_, LargeListView(to)) => cast_values_to_list_view::<i64>(array, to, cast_options),
912 (_, FixedSizeList(to, size)) if *size == 1 => {
913 cast_values_to_fixed_size_list(array, to, *size, cast_options)
914 }
915 (Map(_, ordered1), Map(_, ordered2)) if ordered1 == ordered2 => {
917 cast_map_values(array.as_map(), to_type, cast_options, ordered1.to_owned())
918 }
919 (Decimal32(p1, s1), Decimal32(p2, s2)) => {
921 cast_decimal_to_decimal_same_type::<Decimal32Type>(
922 array.as_primitive(),
923 *p1,
924 *s1,
925 *p2,
926 *s2,
927 cast_options,
928 )
929 }
930 (Decimal64(p1, s1), Decimal64(p2, s2)) => {
931 cast_decimal_to_decimal_same_type::<Decimal64Type>(
932 array.as_primitive(),
933 *p1,
934 *s1,
935 *p2,
936 *s2,
937 cast_options,
938 )
939 }
940 (Decimal128(p1, s1), Decimal128(p2, s2)) => {
941 cast_decimal_to_decimal_same_type::<Decimal128Type>(
942 array.as_primitive(),
943 *p1,
944 *s1,
945 *p2,
946 *s2,
947 cast_options,
948 )
949 }
950 (Decimal256(p1, s1), Decimal256(p2, s2)) => {
951 cast_decimal_to_decimal_same_type::<Decimal256Type>(
952 array.as_primitive(),
953 *p1,
954 *s1,
955 *p2,
956 *s2,
957 cast_options,
958 )
959 }
960 (Decimal32(p1, s1), Decimal64(p2, s2)) => {
962 cast_decimal_to_decimal::<Decimal32Type, Decimal64Type>(
963 array.as_primitive(),
964 *p1,
965 *s1,
966 *p2,
967 *s2,
968 cast_options,
969 )
970 }
971 (Decimal32(p1, s1), Decimal128(p2, s2)) => {
972 cast_decimal_to_decimal::<Decimal32Type, Decimal128Type>(
973 array.as_primitive(),
974 *p1,
975 *s1,
976 *p2,
977 *s2,
978 cast_options,
979 )
980 }
981 (Decimal32(p1, s1), Decimal256(p2, s2)) => {
982 cast_decimal_to_decimal::<Decimal32Type, Decimal256Type>(
983 array.as_primitive(),
984 *p1,
985 *s1,
986 *p2,
987 *s2,
988 cast_options,
989 )
990 }
991 (Decimal64(p1, s1), Decimal32(p2, s2)) => {
992 cast_decimal_to_decimal::<Decimal64Type, Decimal32Type>(
993 array.as_primitive(),
994 *p1,
995 *s1,
996 *p2,
997 *s2,
998 cast_options,
999 )
1000 }
1001 (Decimal64(p1, s1), Decimal128(p2, s2)) => {
1002 cast_decimal_to_decimal::<Decimal64Type, Decimal128Type>(
1003 array.as_primitive(),
1004 *p1,
1005 *s1,
1006 *p2,
1007 *s2,
1008 cast_options,
1009 )
1010 }
1011 (Decimal64(p1, s1), Decimal256(p2, s2)) => {
1012 cast_decimal_to_decimal::<Decimal64Type, Decimal256Type>(
1013 array.as_primitive(),
1014 *p1,
1015 *s1,
1016 *p2,
1017 *s2,
1018 cast_options,
1019 )
1020 }
1021 (Decimal128(p1, s1), Decimal32(p2, s2)) => {
1022 cast_decimal_to_decimal::<Decimal128Type, Decimal32Type>(
1023 array.as_primitive(),
1024 *p1,
1025 *s1,
1026 *p2,
1027 *s2,
1028 cast_options,
1029 )
1030 }
1031 (Decimal128(p1, s1), Decimal64(p2, s2)) => {
1032 cast_decimal_to_decimal::<Decimal128Type, Decimal64Type>(
1033 array.as_primitive(),
1034 *p1,
1035 *s1,
1036 *p2,
1037 *s2,
1038 cast_options,
1039 )
1040 }
1041 (Decimal128(p1, s1), Decimal256(p2, s2)) => {
1042 cast_decimal_to_decimal::<Decimal128Type, Decimal256Type>(
1043 array.as_primitive(),
1044 *p1,
1045 *s1,
1046 *p2,
1047 *s2,
1048 cast_options,
1049 )
1050 }
1051 (Decimal256(p1, s1), Decimal32(p2, s2)) => {
1052 cast_decimal_to_decimal::<Decimal256Type, Decimal32Type>(
1053 array.as_primitive(),
1054 *p1,
1055 *s1,
1056 *p2,
1057 *s2,
1058 cast_options,
1059 )
1060 }
1061 (Decimal256(p1, s1), Decimal64(p2, s2)) => {
1062 cast_decimal_to_decimal::<Decimal256Type, Decimal64Type>(
1063 array.as_primitive(),
1064 *p1,
1065 *s1,
1066 *p2,
1067 *s2,
1068 cast_options,
1069 )
1070 }
1071 (Decimal256(p1, s1), Decimal128(p2, s2)) => {
1072 cast_decimal_to_decimal::<Decimal256Type, Decimal128Type>(
1073 array.as_primitive(),
1074 *p1,
1075 *s1,
1076 *p2,
1077 *s2,
1078 cast_options,
1079 )
1080 }
1081 (Decimal32(_, scale), _) if !to_type.is_temporal() => {
1083 cast_from_decimal::<Decimal32Type, _>(
1084 array,
1085 10_i32,
1086 scale,
1087 from_type,
1088 to_type,
1089 |x: i32| x as f64,
1090 cast_options,
1091 )
1092 }
1093 (Decimal64(_, scale), _) if !to_type.is_temporal() => {
1094 cast_from_decimal::<Decimal64Type, _>(
1095 array,
1096 10_i64,
1097 scale,
1098 from_type,
1099 to_type,
1100 |x: i64| x as f64,
1101 cast_options,
1102 )
1103 }
1104 (Decimal128(_, scale), _) if !to_type.is_temporal() => {
1105 cast_from_decimal::<Decimal128Type, _>(
1106 array,
1107 10_i128,
1108 scale,
1109 from_type,
1110 to_type,
1111 |x: i128| x as f64,
1112 cast_options,
1113 )
1114 }
1115 (Decimal256(_, scale), _) if !to_type.is_temporal() => {
1116 cast_from_decimal::<Decimal256Type, _>(
1117 array,
1118 i256::from_i128(10_i128),
1119 scale,
1120 from_type,
1121 to_type,
1122 |x: i256| x.to_f64().expect("All i256 values fit in f64"),
1123 cast_options,
1124 )
1125 }
1126 (_, Decimal32(precision, scale)) if !from_type.is_temporal() => {
1128 cast_to_decimal::<Decimal32Type, _>(
1129 array,
1130 10_i32,
1131 precision,
1132 scale,
1133 from_type,
1134 to_type,
1135 cast_options,
1136 )
1137 }
1138 (_, Decimal64(precision, scale)) if !from_type.is_temporal() => {
1139 cast_to_decimal::<Decimal64Type, _>(
1140 array,
1141 10_i64,
1142 precision,
1143 scale,
1144 from_type,
1145 to_type,
1146 cast_options,
1147 )
1148 }
1149 (_, Decimal128(precision, scale)) if !from_type.is_temporal() => {
1150 cast_to_decimal::<Decimal128Type, _>(
1151 array,
1152 10_i128,
1153 precision,
1154 scale,
1155 from_type,
1156 to_type,
1157 cast_options,
1158 )
1159 }
1160 (_, Decimal256(precision, scale)) if !from_type.is_temporal() => {
1161 cast_to_decimal::<Decimal256Type, _>(
1162 array,
1163 i256::from_i128(10_i128),
1164 precision,
1165 scale,
1166 from_type,
1167 to_type,
1168 cast_options,
1169 )
1170 }
1171 (Struct(from_fields), Struct(to_fields)) => cast_struct_to_struct(
1172 array.as_struct(),
1173 from_fields.clone(),
1174 to_fields.clone(),
1175 cast_options,
1176 ),
1177 (Struct(_), _) => Err(ArrowError::CastError(format!(
1178 "Casting from {from_type} to {to_type} not supported"
1179 ))),
1180 (_, Struct(_)) => Err(ArrowError::CastError(format!(
1181 "Casting from {from_type} to {to_type} not supported"
1182 ))),
1183 (_, Boolean) => match from_type {
1184 UInt8 => cast_numeric_to_bool::<UInt8Type>(array),
1185 UInt16 => cast_numeric_to_bool::<UInt16Type>(array),
1186 UInt32 => cast_numeric_to_bool::<UInt32Type>(array),
1187 UInt64 => cast_numeric_to_bool::<UInt64Type>(array),
1188 Int8 => cast_numeric_to_bool::<Int8Type>(array),
1189 Int16 => cast_numeric_to_bool::<Int16Type>(array),
1190 Int32 => cast_numeric_to_bool::<Int32Type>(array),
1191 Int64 => cast_numeric_to_bool::<Int64Type>(array),
1192 Float16 => cast_numeric_to_bool::<Float16Type>(array),
1193 Float32 => cast_numeric_to_bool::<Float32Type>(array),
1194 Float64 => cast_numeric_to_bool::<Float64Type>(array),
1195 Utf8View => cast_utf8view_to_boolean(array, cast_options),
1196 Utf8 => cast_utf8_to_boolean::<i32>(array, cast_options),
1197 LargeUtf8 => cast_utf8_to_boolean::<i64>(array, cast_options),
1198 _ => Err(ArrowError::CastError(format!(
1199 "Casting from {from_type} to {to_type} not supported",
1200 ))),
1201 },
1202 (Boolean, _) => match to_type {
1203 UInt8 => cast_bool_to_numeric::<UInt8Type>(array, cast_options),
1204 UInt16 => cast_bool_to_numeric::<UInt16Type>(array, cast_options),
1205 UInt32 => cast_bool_to_numeric::<UInt32Type>(array, cast_options),
1206 UInt64 => cast_bool_to_numeric::<UInt64Type>(array, cast_options),
1207 Int8 => cast_bool_to_numeric::<Int8Type>(array, cast_options),
1208 Int16 => cast_bool_to_numeric::<Int16Type>(array, cast_options),
1209 Int32 => cast_bool_to_numeric::<Int32Type>(array, cast_options),
1210 Int64 => cast_bool_to_numeric::<Int64Type>(array, cast_options),
1211 Float16 => cast_bool_to_numeric::<Float16Type>(array, cast_options),
1212 Float32 => cast_bool_to_numeric::<Float32Type>(array, cast_options),
1213 Float64 => cast_bool_to_numeric::<Float64Type>(array, cast_options),
1214 Utf8View => value_to_string_view(array, cast_options),
1215 Utf8 => value_to_string::<i32>(array, cast_options),
1216 LargeUtf8 => value_to_string::<i64>(array, cast_options),
1217 _ => Err(ArrowError::CastError(format!(
1218 "Casting from {from_type} to {to_type} not supported",
1219 ))),
1220 },
1221 (Utf8, _) => match to_type {
1222 UInt8 => parse_string::<UInt8Type, i32>(array, cast_options),
1223 UInt16 => parse_string::<UInt16Type, i32>(array, cast_options),
1224 UInt32 => parse_string::<UInt32Type, i32>(array, cast_options),
1225 UInt64 => parse_string::<UInt64Type, i32>(array, cast_options),
1226 Int8 => parse_string::<Int8Type, i32>(array, cast_options),
1227 Int16 => parse_string::<Int16Type, i32>(array, cast_options),
1228 Int32 => parse_string::<Int32Type, i32>(array, cast_options),
1229 Int64 => parse_string::<Int64Type, i32>(array, cast_options),
1230 Float16 => parse_string::<Float16Type, i32>(array, cast_options),
1231 Float32 => parse_string::<Float32Type, i32>(array, cast_options),
1232 Float64 => parse_string::<Float64Type, i32>(array, cast_options),
1233 Date32 => parse_string::<Date32Type, i32>(array, cast_options),
1234 Date64 => parse_string::<Date64Type, i32>(array, cast_options),
1235 Binary => Ok(Arc::new(BinaryArray::from(
1236 array.as_string::<i32>().clone(),
1237 ))),
1238 LargeBinary => {
1239 let binary = BinaryArray::from(array.as_string::<i32>().clone());
1240 cast_byte_container::<BinaryType, LargeBinaryType>(&binary)
1241 }
1242 Utf8View => Ok(Arc::new(StringViewArray::from(array.as_string::<i32>()))),
1243 BinaryView => Ok(Arc::new(
1244 StringViewArray::from(array.as_string::<i32>()).to_binary_view(),
1245 )),
1246 LargeUtf8 => cast_byte_container::<Utf8Type, LargeUtf8Type>(array),
1247 Time32(TimeUnit::Second) => parse_string::<Time32SecondType, i32>(array, cast_options),
1248 Time32(TimeUnit::Millisecond) => {
1249 parse_string::<Time32MillisecondType, i32>(array, cast_options)
1250 }
1251 Time64(TimeUnit::Microsecond) => {
1252 parse_string::<Time64MicrosecondType, i32>(array, cast_options)
1253 }
1254 Time64(TimeUnit::Nanosecond) => {
1255 parse_string::<Time64NanosecondType, i32>(array, cast_options)
1256 }
1257 Timestamp(TimeUnit::Second, to_tz) => {
1258 cast_string_to_timestamp::<i32, TimestampSecondType>(array, to_tz, cast_options)
1259 }
1260 Timestamp(TimeUnit::Millisecond, to_tz) => cast_string_to_timestamp::<
1261 i32,
1262 TimestampMillisecondType,
1263 >(array, to_tz, cast_options),
1264 Timestamp(TimeUnit::Microsecond, to_tz) => cast_string_to_timestamp::<
1265 i32,
1266 TimestampMicrosecondType,
1267 >(array, to_tz, cast_options),
1268 Timestamp(TimeUnit::Nanosecond, to_tz) => {
1269 cast_string_to_timestamp::<i32, TimestampNanosecondType>(array, to_tz, cast_options)
1270 }
1271 Interval(IntervalUnit::YearMonth) => {
1272 cast_string_to_year_month_interval::<i32>(array, cast_options)
1273 }
1274 Interval(IntervalUnit::DayTime) => {
1275 cast_string_to_day_time_interval::<i32>(array, cast_options)
1276 }
1277 Interval(IntervalUnit::MonthDayNano) => {
1278 cast_string_to_month_day_nano_interval::<i32>(array, cast_options)
1279 }
1280 _ => Err(ArrowError::CastError(format!(
1281 "Casting from {from_type} to {to_type} not supported",
1282 ))),
1283 },
1284 (Utf8View, _) => match to_type {
1285 UInt8 => parse_string_view::<UInt8Type>(array, cast_options),
1286 UInt16 => parse_string_view::<UInt16Type>(array, cast_options),
1287 UInt32 => parse_string_view::<UInt32Type>(array, cast_options),
1288 UInt64 => parse_string_view::<UInt64Type>(array, cast_options),
1289 Int8 => parse_string_view::<Int8Type>(array, cast_options),
1290 Int16 => parse_string_view::<Int16Type>(array, cast_options),
1291 Int32 => parse_string_view::<Int32Type>(array, cast_options),
1292 Int64 => parse_string_view::<Int64Type>(array, cast_options),
1293 Float16 => parse_string_view::<Float16Type>(array, cast_options),
1294 Float32 => parse_string_view::<Float32Type>(array, cast_options),
1295 Float64 => parse_string_view::<Float64Type>(array, cast_options),
1296 Date32 => parse_string_view::<Date32Type>(array, cast_options),
1297 Date64 => parse_string_view::<Date64Type>(array, cast_options),
1298 Binary => cast_view_to_byte::<StringViewType, GenericBinaryType<i32>>(array),
1299 LargeBinary => cast_view_to_byte::<StringViewType, GenericBinaryType<i64>>(array),
1300 BinaryView => Ok(Arc::new(array.as_string_view().clone().to_binary_view())),
1301 Utf8 => cast_view_to_byte::<StringViewType, GenericStringType<i32>>(array),
1302 LargeUtf8 => cast_view_to_byte::<StringViewType, GenericStringType<i64>>(array),
1303 Time32(TimeUnit::Second) => parse_string_view::<Time32SecondType>(array, cast_options),
1304 Time32(TimeUnit::Millisecond) => {
1305 parse_string_view::<Time32MillisecondType>(array, cast_options)
1306 }
1307 Time64(TimeUnit::Microsecond) => {
1308 parse_string_view::<Time64MicrosecondType>(array, cast_options)
1309 }
1310 Time64(TimeUnit::Nanosecond) => {
1311 parse_string_view::<Time64NanosecondType>(array, cast_options)
1312 }
1313 Timestamp(TimeUnit::Second, to_tz) => {
1314 cast_view_to_timestamp::<TimestampSecondType>(array, to_tz, cast_options)
1315 }
1316 Timestamp(TimeUnit::Millisecond, to_tz) => {
1317 cast_view_to_timestamp::<TimestampMillisecondType>(array, to_tz, cast_options)
1318 }
1319 Timestamp(TimeUnit::Microsecond, to_tz) => {
1320 cast_view_to_timestamp::<TimestampMicrosecondType>(array, to_tz, cast_options)
1321 }
1322 Timestamp(TimeUnit::Nanosecond, to_tz) => {
1323 cast_view_to_timestamp::<TimestampNanosecondType>(array, to_tz, cast_options)
1324 }
1325 Interval(IntervalUnit::YearMonth) => {
1326 cast_view_to_year_month_interval(array, cast_options)
1327 }
1328 Interval(IntervalUnit::DayTime) => cast_view_to_day_time_interval(array, cast_options),
1329 Interval(IntervalUnit::MonthDayNano) => {
1330 cast_view_to_month_day_nano_interval(array, cast_options)
1331 }
1332 _ => Err(ArrowError::CastError(format!(
1333 "Casting from {from_type} to {to_type} not supported",
1334 ))),
1335 },
1336 (LargeUtf8, _) => match to_type {
1337 UInt8 => parse_string::<UInt8Type, i64>(array, cast_options),
1338 UInt16 => parse_string::<UInt16Type, i64>(array, cast_options),
1339 UInt32 => parse_string::<UInt32Type, i64>(array, cast_options),
1340 UInt64 => parse_string::<UInt64Type, i64>(array, cast_options),
1341 Int8 => parse_string::<Int8Type, i64>(array, cast_options),
1342 Int16 => parse_string::<Int16Type, i64>(array, cast_options),
1343 Int32 => parse_string::<Int32Type, i64>(array, cast_options),
1344 Int64 => parse_string::<Int64Type, i64>(array, cast_options),
1345 Float16 => parse_string::<Float16Type, i64>(array, cast_options),
1346 Float32 => parse_string::<Float32Type, i64>(array, cast_options),
1347 Float64 => parse_string::<Float64Type, i64>(array, cast_options),
1348 Date32 => parse_string::<Date32Type, i64>(array, cast_options),
1349 Date64 => parse_string::<Date64Type, i64>(array, cast_options),
1350 Utf8 => cast_byte_container::<LargeUtf8Type, Utf8Type>(array),
1351 Binary => {
1352 let large_binary = LargeBinaryArray::from(array.as_string::<i64>().clone());
1353 cast_byte_container::<LargeBinaryType, BinaryType>(&large_binary)
1354 }
1355 LargeBinary => Ok(Arc::new(LargeBinaryArray::from(
1356 array.as_string::<i64>().clone(),
1357 ))),
1358 Utf8View => Ok(Arc::new(StringViewArray::from(array.as_string::<i64>()))),
1359 BinaryView => Ok(Arc::new(BinaryViewArray::from(
1360 array
1361 .as_string::<i64>()
1362 .into_iter()
1363 .map(|x| x.map(|x| x.as_bytes()))
1364 .collect::<Vec<_>>(),
1365 ))),
1366 Time32(TimeUnit::Second) => parse_string::<Time32SecondType, i64>(array, cast_options),
1367 Time32(TimeUnit::Millisecond) => {
1368 parse_string::<Time32MillisecondType, i64>(array, cast_options)
1369 }
1370 Time64(TimeUnit::Microsecond) => {
1371 parse_string::<Time64MicrosecondType, i64>(array, cast_options)
1372 }
1373 Time64(TimeUnit::Nanosecond) => {
1374 parse_string::<Time64NanosecondType, i64>(array, cast_options)
1375 }
1376 Timestamp(TimeUnit::Second, to_tz) => {
1377 cast_string_to_timestamp::<i64, TimestampSecondType>(array, to_tz, cast_options)
1378 }
1379 Timestamp(TimeUnit::Millisecond, to_tz) => cast_string_to_timestamp::<
1380 i64,
1381 TimestampMillisecondType,
1382 >(array, to_tz, cast_options),
1383 Timestamp(TimeUnit::Microsecond, to_tz) => cast_string_to_timestamp::<
1384 i64,
1385 TimestampMicrosecondType,
1386 >(array, to_tz, cast_options),
1387 Timestamp(TimeUnit::Nanosecond, to_tz) => {
1388 cast_string_to_timestamp::<i64, TimestampNanosecondType>(array, to_tz, cast_options)
1389 }
1390 Interval(IntervalUnit::YearMonth) => {
1391 cast_string_to_year_month_interval::<i64>(array, cast_options)
1392 }
1393 Interval(IntervalUnit::DayTime) => {
1394 cast_string_to_day_time_interval::<i64>(array, cast_options)
1395 }
1396 Interval(IntervalUnit::MonthDayNano) => {
1397 cast_string_to_month_day_nano_interval::<i64>(array, cast_options)
1398 }
1399 _ => Err(ArrowError::CastError(format!(
1400 "Casting from {from_type} to {to_type} not supported",
1401 ))),
1402 },
1403 (Binary, _) => match to_type {
1404 Utf8 => cast_binary_to_string::<i32>(array, cast_options),
1405 LargeUtf8 => {
1406 let array = cast_binary_to_string::<i32>(array, cast_options)?;
1407 cast_byte_container::<Utf8Type, LargeUtf8Type>(array.as_ref())
1408 }
1409 LargeBinary => cast_byte_container::<BinaryType, LargeBinaryType>(array),
1410 FixedSizeBinary(size) => {
1411 cast_binary_to_fixed_size_binary::<i32>(array, *size, cast_options)
1412 }
1413 BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i32>()))),
1414 Utf8View => Ok(Arc::new(StringViewArray::from(
1415 cast_binary_to_string::<i32>(array, cast_options)?.as_string::<i32>(),
1416 ))),
1417 _ => Err(ArrowError::CastError(format!(
1418 "Casting from {from_type} to {to_type} not supported",
1419 ))),
1420 },
1421 (LargeBinary, _) => match to_type {
1422 Utf8 => {
1423 let array = cast_binary_to_string::<i64>(array, cast_options)?;
1424 cast_byte_container::<LargeUtf8Type, Utf8Type>(array.as_ref())
1425 }
1426 LargeUtf8 => cast_binary_to_string::<i64>(array, cast_options),
1427 Binary => cast_byte_container::<LargeBinaryType, BinaryType>(array),
1428 FixedSizeBinary(size) => {
1429 cast_binary_to_fixed_size_binary::<i64>(array, *size, cast_options)
1430 }
1431 BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i64>()))),
1432 Utf8View => {
1433 let array = cast_binary_to_string::<i64>(array, cast_options)?;
1434 Ok(Arc::new(StringViewArray::from(array.as_string::<i64>())))
1435 }
1436 _ => Err(ArrowError::CastError(format!(
1437 "Casting from {from_type} to {to_type} not supported",
1438 ))),
1439 },
1440 (FixedSizeBinary(size), _) => match to_type {
1441 Binary => cast_fixed_size_binary_to_binary::<i32>(array, *size),
1442 LargeBinary => cast_fixed_size_binary_to_binary::<i64>(array, *size),
1443 BinaryView => cast_fixed_size_binary_to_binary_view(array, *size),
1444 _ => Err(ArrowError::CastError(format!(
1445 "Casting from {from_type} to {to_type} not supported",
1446 ))),
1447 },
1448 (BinaryView, Binary) => cast_view_to_byte::<BinaryViewType, GenericBinaryType<i32>>(array),
1449 (BinaryView, LargeBinary) => {
1450 cast_view_to_byte::<BinaryViewType, GenericBinaryType<i64>>(array)
1451 }
1452 (BinaryView, Utf8) => {
1453 let binary_arr = cast_view_to_byte::<BinaryViewType, GenericBinaryType<i32>>(array)?;
1454 cast_binary_to_string::<i32>(&binary_arr, cast_options)
1455 }
1456 (BinaryView, LargeUtf8) => {
1457 let binary_arr = cast_view_to_byte::<BinaryViewType, GenericBinaryType<i64>>(array)?;
1458 cast_binary_to_string::<i64>(&binary_arr, cast_options)
1459 }
1460 (BinaryView, Utf8View) => cast_binary_view_to_string_view(array, cast_options),
1461 (BinaryView, _) => Err(ArrowError::CastError(format!(
1462 "Casting from {from_type} to {to_type} not supported",
1463 ))),
1464 (from_type, Utf8View) if from_type.is_primitive() => {
1465 value_to_string_view(array, cast_options)
1466 }
1467 (from_type, LargeUtf8) if from_type.is_primitive() => {
1468 value_to_string::<i64>(array, cast_options)
1469 }
1470 (from_type, Utf8) if from_type.is_primitive() => {
1471 value_to_string::<i32>(array, cast_options)
1472 }
1473 (from_type, Binary) if from_type.is_integer() => match from_type {
1474 UInt8 => cast_numeric_to_binary::<UInt8Type, i32>(array),
1475 UInt16 => cast_numeric_to_binary::<UInt16Type, i32>(array),
1476 UInt32 => cast_numeric_to_binary::<UInt32Type, i32>(array),
1477 UInt64 => cast_numeric_to_binary::<UInt64Type, i32>(array),
1478 Int8 => cast_numeric_to_binary::<Int8Type, i32>(array),
1479 Int16 => cast_numeric_to_binary::<Int16Type, i32>(array),
1480 Int32 => cast_numeric_to_binary::<Int32Type, i32>(array),
1481 Int64 => cast_numeric_to_binary::<Int64Type, i32>(array),
1482 _ => unreachable!(),
1483 },
1484 (from_type, LargeBinary) if from_type.is_integer() => match from_type {
1485 UInt8 => cast_numeric_to_binary::<UInt8Type, i64>(array),
1486 UInt16 => cast_numeric_to_binary::<UInt16Type, i64>(array),
1487 UInt32 => cast_numeric_to_binary::<UInt32Type, i64>(array),
1488 UInt64 => cast_numeric_to_binary::<UInt64Type, i64>(array),
1489 Int8 => cast_numeric_to_binary::<Int8Type, i64>(array),
1490 Int16 => cast_numeric_to_binary::<Int16Type, i64>(array),
1491 Int32 => cast_numeric_to_binary::<Int32Type, i64>(array),
1492 Int64 => cast_numeric_to_binary::<Int64Type, i64>(array),
1493 _ => unreachable!(),
1494 },
1495 (UInt8, UInt16) => cast_numeric_arrays::<UInt8Type, UInt16Type>(array, cast_options),
1497 (UInt8, UInt32) => cast_numeric_arrays::<UInt8Type, UInt32Type>(array, cast_options),
1498 (UInt8, UInt64) => cast_numeric_arrays::<UInt8Type, UInt64Type>(array, cast_options),
1499 (UInt8, Int8) => cast_numeric_arrays::<UInt8Type, Int8Type>(array, cast_options),
1500 (UInt8, Int16) => cast_numeric_arrays::<UInt8Type, Int16Type>(array, cast_options),
1501 (UInt8, Int32) => cast_numeric_arrays::<UInt8Type, Int32Type>(array, cast_options),
1502 (UInt8, Int64) => cast_numeric_arrays::<UInt8Type, Int64Type>(array, cast_options),
1503 (UInt8, Float16) => cast_numeric_arrays::<UInt8Type, Float16Type>(array, cast_options),
1504 (UInt8, Float32) => cast_numeric_arrays::<UInt8Type, Float32Type>(array, cast_options),
1505 (UInt8, Float64) => cast_numeric_arrays::<UInt8Type, Float64Type>(array, cast_options),
1506
1507 (UInt16, UInt8) => cast_numeric_arrays::<UInt16Type, UInt8Type>(array, cast_options),
1508 (UInt16, UInt32) => cast_numeric_arrays::<UInt16Type, UInt32Type>(array, cast_options),
1509 (UInt16, UInt64) => cast_numeric_arrays::<UInt16Type, UInt64Type>(array, cast_options),
1510 (UInt16, Int8) => cast_numeric_arrays::<UInt16Type, Int8Type>(array, cast_options),
1511 (UInt16, Int16) => cast_numeric_arrays::<UInt16Type, Int16Type>(array, cast_options),
1512 (UInt16, Int32) => cast_numeric_arrays::<UInt16Type, Int32Type>(array, cast_options),
1513 (UInt16, Int64) => cast_numeric_arrays::<UInt16Type, Int64Type>(array, cast_options),
1514 (UInt16, Float16) => cast_numeric_arrays::<UInt16Type, Float16Type>(array, cast_options),
1515 (UInt16, Float32) => cast_numeric_arrays::<UInt16Type, Float32Type>(array, cast_options),
1516 (UInt16, Float64) => cast_numeric_arrays::<UInt16Type, Float64Type>(array, cast_options),
1517
1518 (UInt32, UInt8) => cast_numeric_arrays::<UInt32Type, UInt8Type>(array, cast_options),
1519 (UInt32, UInt16) => cast_numeric_arrays::<UInt32Type, UInt16Type>(array, cast_options),
1520 (UInt32, UInt64) => cast_numeric_arrays::<UInt32Type, UInt64Type>(array, cast_options),
1521 (UInt32, Int8) => cast_numeric_arrays::<UInt32Type, Int8Type>(array, cast_options),
1522 (UInt32, Int16) => cast_numeric_arrays::<UInt32Type, Int16Type>(array, cast_options),
1523 (UInt32, Int32) => cast_numeric_arrays::<UInt32Type, Int32Type>(array, cast_options),
1524 (UInt32, Int64) => cast_numeric_arrays::<UInt32Type, Int64Type>(array, cast_options),
1525 (UInt32, Float16) => cast_numeric_arrays::<UInt32Type, Float16Type>(array, cast_options),
1526 (UInt32, Float32) => cast_numeric_arrays::<UInt32Type, Float32Type>(array, cast_options),
1527 (UInt32, Float64) => cast_numeric_arrays::<UInt32Type, Float64Type>(array, cast_options),
1528
1529 (UInt64, UInt8) => cast_numeric_arrays::<UInt64Type, UInt8Type>(array, cast_options),
1530 (UInt64, UInt16) => cast_numeric_arrays::<UInt64Type, UInt16Type>(array, cast_options),
1531 (UInt64, UInt32) => cast_numeric_arrays::<UInt64Type, UInt32Type>(array, cast_options),
1532 (UInt64, Int8) => cast_numeric_arrays::<UInt64Type, Int8Type>(array, cast_options),
1533 (UInt64, Int16) => cast_numeric_arrays::<UInt64Type, Int16Type>(array, cast_options),
1534 (UInt64, Int32) => cast_numeric_arrays::<UInt64Type, Int32Type>(array, cast_options),
1535 (UInt64, Int64) => cast_numeric_arrays::<UInt64Type, Int64Type>(array, cast_options),
1536 (UInt64, Float16) => cast_numeric_arrays::<UInt64Type, Float16Type>(array, cast_options),
1537 (UInt64, Float32) => cast_numeric_arrays::<UInt64Type, Float32Type>(array, cast_options),
1538 (UInt64, Float64) => cast_numeric_arrays::<UInt64Type, Float64Type>(array, cast_options),
1539
1540 (Int8, UInt8) => cast_numeric_arrays::<Int8Type, UInt8Type>(array, cast_options),
1541 (Int8, UInt16) => cast_numeric_arrays::<Int8Type, UInt16Type>(array, cast_options),
1542 (Int8, UInt32) => cast_numeric_arrays::<Int8Type, UInt32Type>(array, cast_options),
1543 (Int8, UInt64) => cast_numeric_arrays::<Int8Type, UInt64Type>(array, cast_options),
1544 (Int8, Int16) => cast_numeric_arrays::<Int8Type, Int16Type>(array, cast_options),
1545 (Int8, Int32) => cast_numeric_arrays::<Int8Type, Int32Type>(array, cast_options),
1546 (Int8, Int64) => cast_numeric_arrays::<Int8Type, Int64Type>(array, cast_options),
1547 (Int8, Float16) => cast_numeric_arrays::<Int8Type, Float16Type>(array, cast_options),
1548 (Int8, Float32) => cast_numeric_arrays::<Int8Type, Float32Type>(array, cast_options),
1549 (Int8, Float64) => cast_numeric_arrays::<Int8Type, Float64Type>(array, cast_options),
1550
1551 (Int16, UInt8) => cast_numeric_arrays::<Int16Type, UInt8Type>(array, cast_options),
1552 (Int16, UInt16) => cast_numeric_arrays::<Int16Type, UInt16Type>(array, cast_options),
1553 (Int16, UInt32) => cast_numeric_arrays::<Int16Type, UInt32Type>(array, cast_options),
1554 (Int16, UInt64) => cast_numeric_arrays::<Int16Type, UInt64Type>(array, cast_options),
1555 (Int16, Int8) => cast_numeric_arrays::<Int16Type, Int8Type>(array, cast_options),
1556 (Int16, Int32) => cast_numeric_arrays::<Int16Type, Int32Type>(array, cast_options),
1557 (Int16, Int64) => cast_numeric_arrays::<Int16Type, Int64Type>(array, cast_options),
1558 (Int16, Float16) => cast_numeric_arrays::<Int16Type, Float16Type>(array, cast_options),
1559 (Int16, Float32) => cast_numeric_arrays::<Int16Type, Float32Type>(array, cast_options),
1560 (Int16, Float64) => cast_numeric_arrays::<Int16Type, Float64Type>(array, cast_options),
1561
1562 (Int32, UInt8) => cast_numeric_arrays::<Int32Type, UInt8Type>(array, cast_options),
1563 (Int32, UInt16) => cast_numeric_arrays::<Int32Type, UInt16Type>(array, cast_options),
1564 (Int32, UInt32) => cast_numeric_arrays::<Int32Type, UInt32Type>(array, cast_options),
1565 (Int32, UInt64) => cast_numeric_arrays::<Int32Type, UInt64Type>(array, cast_options),
1566 (Int32, Int8) => cast_numeric_arrays::<Int32Type, Int8Type>(array, cast_options),
1567 (Int32, Int16) => cast_numeric_arrays::<Int32Type, Int16Type>(array, cast_options),
1568 (Int32, Int64) => cast_numeric_arrays::<Int32Type, Int64Type>(array, cast_options),
1569 (Int32, Float16) => cast_numeric_arrays::<Int32Type, Float16Type>(array, cast_options),
1570 (Int32, Float32) => cast_numeric_arrays::<Int32Type, Float32Type>(array, cast_options),
1571 (Int32, Float64) => cast_numeric_arrays::<Int32Type, Float64Type>(array, cast_options),
1572
1573 (Int64, UInt8) => cast_numeric_arrays::<Int64Type, UInt8Type>(array, cast_options),
1574 (Int64, UInt16) => cast_numeric_arrays::<Int64Type, UInt16Type>(array, cast_options),
1575 (Int64, UInt32) => cast_numeric_arrays::<Int64Type, UInt32Type>(array, cast_options),
1576 (Int64, UInt64) => cast_numeric_arrays::<Int64Type, UInt64Type>(array, cast_options),
1577 (Int64, Int8) => cast_numeric_arrays::<Int64Type, Int8Type>(array, cast_options),
1578 (Int64, Int16) => cast_numeric_arrays::<Int64Type, Int16Type>(array, cast_options),
1579 (Int64, Int32) => cast_numeric_arrays::<Int64Type, Int32Type>(array, cast_options),
1580 (Int64, Float16) => cast_numeric_arrays::<Int64Type, Float16Type>(array, cast_options),
1581 (Int64, Float32) => cast_numeric_arrays::<Int64Type, Float32Type>(array, cast_options),
1582 (Int64, Float64) => cast_numeric_arrays::<Int64Type, Float64Type>(array, cast_options),
1583
1584 (Float16, UInt8) => cast_numeric_arrays::<Float16Type, UInt8Type>(array, cast_options),
1585 (Float16, UInt16) => cast_numeric_arrays::<Float16Type, UInt16Type>(array, cast_options),
1586 (Float16, UInt32) => cast_numeric_arrays::<Float16Type, UInt32Type>(array, cast_options),
1587 (Float16, UInt64) => cast_numeric_arrays::<Float16Type, UInt64Type>(array, cast_options),
1588 (Float16, Int8) => cast_numeric_arrays::<Float16Type, Int8Type>(array, cast_options),
1589 (Float16, Int16) => cast_numeric_arrays::<Float16Type, Int16Type>(array, cast_options),
1590 (Float16, Int32) => cast_numeric_arrays::<Float16Type, Int32Type>(array, cast_options),
1591 (Float16, Int64) => cast_numeric_arrays::<Float16Type, Int64Type>(array, cast_options),
1592 (Float16, Float32) => cast_numeric_arrays::<Float16Type, Float32Type>(array, cast_options),
1593 (Float16, Float64) => cast_numeric_arrays::<Float16Type, Float64Type>(array, cast_options),
1594
1595 (Float32, UInt8) => cast_numeric_arrays::<Float32Type, UInt8Type>(array, cast_options),
1596 (Float32, UInt16) => cast_numeric_arrays::<Float32Type, UInt16Type>(array, cast_options),
1597 (Float32, UInt32) => cast_numeric_arrays::<Float32Type, UInt32Type>(array, cast_options),
1598 (Float32, UInt64) => cast_numeric_arrays::<Float32Type, UInt64Type>(array, cast_options),
1599 (Float32, Int8) => cast_numeric_arrays::<Float32Type, Int8Type>(array, cast_options),
1600 (Float32, Int16) => cast_numeric_arrays::<Float32Type, Int16Type>(array, cast_options),
1601 (Float32, Int32) => cast_numeric_arrays::<Float32Type, Int32Type>(array, cast_options),
1602 (Float32, Int64) => cast_numeric_arrays::<Float32Type, Int64Type>(array, cast_options),
1603 (Float32, Float16) => cast_numeric_arrays::<Float32Type, Float16Type>(array, cast_options),
1604 (Float32, Float64) => cast_numeric_arrays::<Float32Type, Float64Type>(array, cast_options),
1605
1606 (Float64, UInt8) => cast_numeric_arrays::<Float64Type, UInt8Type>(array, cast_options),
1607 (Float64, UInt16) => cast_numeric_arrays::<Float64Type, UInt16Type>(array, cast_options),
1608 (Float64, UInt32) => cast_numeric_arrays::<Float64Type, UInt32Type>(array, cast_options),
1609 (Float64, UInt64) => cast_numeric_arrays::<Float64Type, UInt64Type>(array, cast_options),
1610 (Float64, Int8) => cast_numeric_arrays::<Float64Type, Int8Type>(array, cast_options),
1611 (Float64, Int16) => cast_numeric_arrays::<Float64Type, Int16Type>(array, cast_options),
1612 (Float64, Int32) => cast_numeric_arrays::<Float64Type, Int32Type>(array, cast_options),
1613 (Float64, Int64) => cast_numeric_arrays::<Float64Type, Int64Type>(array, cast_options),
1614 (Float64, Float16) => cast_numeric_arrays::<Float64Type, Float16Type>(array, cast_options),
1615 (Float64, Float32) => cast_numeric_arrays::<Float64Type, Float32Type>(array, cast_options),
1616 (Int32, Date32) => cast_reinterpret_arrays::<Int32Type, Date32Type>(array),
1620 (Int32, Date64) => cast_with_options(
1621 &cast_with_options(array, &Date32, cast_options)?,
1622 &Date64,
1623 cast_options,
1624 ),
1625 (Int32, Time32(TimeUnit::Second)) => {
1626 cast_reinterpret_arrays::<Int32Type, Time32SecondType>(array)
1627 }
1628 (Int32, Time32(TimeUnit::Millisecond)) => {
1629 cast_reinterpret_arrays::<Int32Type, Time32MillisecondType>(array)
1630 }
1631 (Date32, Int32) => cast_reinterpret_arrays::<Date32Type, Int32Type>(array),
1633 (Date32, Int64) => cast_with_options(
1634 &cast_with_options(array, &Int32, cast_options)?,
1635 &Int64,
1636 cast_options,
1637 ),
1638 (Time32(TimeUnit::Second), Int32) => {
1639 cast_reinterpret_arrays::<Time32SecondType, Int32Type>(array)
1640 }
1641 (Time32(TimeUnit::Millisecond), Int32) => {
1642 cast_reinterpret_arrays::<Time32MillisecondType, Int32Type>(array)
1643 }
1644 (Time32(TimeUnit::Second), Int64) => cast_with_options(
1645 &cast_with_options(array, &Int32, cast_options)?,
1646 &Int64,
1647 cast_options,
1648 ),
1649 (Time32(TimeUnit::Millisecond), Int64) => cast_with_options(
1650 &cast_with_options(array, &Int32, cast_options)?,
1651 &Int64,
1652 cast_options,
1653 ),
1654 (Int64, Date64) => cast_reinterpret_arrays::<Int64Type, Date64Type>(array),
1655 (Int64, Date32) => cast_with_options(
1656 &cast_with_options(array, &Int32, cast_options)?,
1657 &Date32,
1658 cast_options,
1659 ),
1660 (Int64, Time64(TimeUnit::Microsecond)) => {
1662 cast_reinterpret_arrays::<Int64Type, Time64MicrosecondType>(array)
1663 }
1664 (Int64, Time64(TimeUnit::Nanosecond)) => {
1665 cast_reinterpret_arrays::<Int64Type, Time64NanosecondType>(array)
1666 }
1667
1668 (Date64, Int64) => cast_reinterpret_arrays::<Date64Type, Int64Type>(array),
1669 (Date64, Int32) => cast_with_options(
1670 &cast_with_options(array, &Int64, cast_options)?,
1671 &Int32,
1672 cast_options,
1673 ),
1674 (Time64(TimeUnit::Microsecond), Int64) => {
1675 cast_reinterpret_arrays::<Time64MicrosecondType, Int64Type>(array)
1676 }
1677 (Time64(TimeUnit::Nanosecond), Int64) => {
1678 cast_reinterpret_arrays::<Time64NanosecondType, Int64Type>(array)
1679 }
1680 (Date32, Date64) => Ok(Arc::new(
1681 array
1682 .as_primitive::<Date32Type>()
1683 .unary::<_, Date64Type>(|x| x as i64 * MILLISECONDS_IN_DAY),
1684 )),
1685 (Date64, Date32) => Ok(Arc::new(
1686 array
1687 .as_primitive::<Date64Type>()
1688 .unary::<_, Date32Type>(|x| (x / MILLISECONDS_IN_DAY) as i32),
1689 )),
1690
1691 (Time32(TimeUnit::Second), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1692 array
1693 .as_primitive::<Time32SecondType>()
1694 .unary::<_, Time32MillisecondType>(|x| x * MILLISECONDS as i32),
1695 )),
1696 (Time32(TimeUnit::Second), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1697 array
1698 .as_primitive::<Time32SecondType>()
1699 .unary::<_, Time64MicrosecondType>(|x| x as i64 * MICROSECONDS),
1700 )),
1701 (Time32(TimeUnit::Second), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1702 array
1703 .as_primitive::<Time32SecondType>()
1704 .unary::<_, Time64NanosecondType>(|x| x as i64 * NANOSECONDS),
1705 )),
1706
1707 (Time32(TimeUnit::Millisecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1708 array
1709 .as_primitive::<Time32MillisecondType>()
1710 .unary::<_, Time32SecondType>(|x| x / MILLISECONDS as i32),
1711 )),
1712 (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1713 array
1714 .as_primitive::<Time32MillisecondType>()
1715 .unary::<_, Time64MicrosecondType>(|x| x as i64 * (MICROSECONDS / MILLISECONDS)),
1716 )),
1717 (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1718 array
1719 .as_primitive::<Time32MillisecondType>()
1720 .unary::<_, Time64NanosecondType>(|x| x as i64 * (MICROSECONDS / NANOSECONDS)),
1721 )),
1722
1723 (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1724 array
1725 .as_primitive::<Time64MicrosecondType>()
1726 .unary::<_, Time32SecondType>(|x| (x / MICROSECONDS) as i32),
1727 )),
1728 (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1729 array
1730 .as_primitive::<Time64MicrosecondType>()
1731 .unary::<_, Time32MillisecondType>(|x| (x / (MICROSECONDS / MILLISECONDS)) as i32),
1732 )),
1733 (Time64(TimeUnit::Microsecond), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1734 array
1735 .as_primitive::<Time64MicrosecondType>()
1736 .unary::<_, Time64NanosecondType>(|x| x * (NANOSECONDS / MICROSECONDS)),
1737 )),
1738
1739 (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1740 array
1741 .as_primitive::<Time64NanosecondType>()
1742 .unary::<_, Time32SecondType>(|x| (x / NANOSECONDS) as i32),
1743 )),
1744 (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1745 array
1746 .as_primitive::<Time64NanosecondType>()
1747 .unary::<_, Time32MillisecondType>(|x| (x / (NANOSECONDS / MILLISECONDS)) as i32),
1748 )),
1749 (Time64(TimeUnit::Nanosecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1750 array
1751 .as_primitive::<Time64NanosecondType>()
1752 .unary::<_, Time64MicrosecondType>(|x| x / (NANOSECONDS / MICROSECONDS)),
1753 )),
1754
1755 (Timestamp(TimeUnit::Second, _), _) if to_type.is_numeric() => {
1757 let array = cast_reinterpret_arrays::<TimestampSecondType, Int64Type>(array)?;
1758 cast_with_options(&array, to_type, cast_options)
1759 }
1760 (Timestamp(TimeUnit::Millisecond, _), _) if to_type.is_numeric() => {
1761 let array = cast_reinterpret_arrays::<TimestampMillisecondType, Int64Type>(array)?;
1762 cast_with_options(&array, to_type, cast_options)
1763 }
1764 (Timestamp(TimeUnit::Microsecond, _), _) if to_type.is_numeric() => {
1765 let array = cast_reinterpret_arrays::<TimestampMicrosecondType, Int64Type>(array)?;
1766 cast_with_options(&array, to_type, cast_options)
1767 }
1768 (Timestamp(TimeUnit::Nanosecond, _), _) if to_type.is_numeric() => {
1769 let array = cast_reinterpret_arrays::<TimestampNanosecondType, Int64Type>(array)?;
1770 cast_with_options(&array, to_type, cast_options)
1771 }
1772
1773 (_, Timestamp(unit, tz)) if from_type.is_numeric() => {
1774 let array = cast_with_options(array, &Int64, cast_options)?;
1775 Ok(make_timestamp_array(
1776 array.as_primitive(),
1777 *unit,
1778 tz.clone(),
1779 ))
1780 }
1781
1782 (Timestamp(from_unit, from_tz), Timestamp(to_unit, to_tz)) => {
1783 let array = cast_with_options(array, &Int64, cast_options)?;
1784 let time_array = array.as_primitive::<Int64Type>();
1785 let from_size = time_unit_multiple(from_unit);
1786 let to_size = time_unit_multiple(to_unit);
1787 let converted = match from_size.cmp(&to_size) {
1790 Ordering::Greater => {
1791 let divisor = from_size / to_size;
1792 time_array.unary::<_, Int64Type>(|o| o / divisor)
1793 }
1794 Ordering::Equal => time_array.clone(),
1795 Ordering::Less => {
1796 let mul = to_size / from_size;
1797 if cast_options.safe {
1798 time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
1799 } else {
1800 time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
1801 }
1802 }
1803 };
1804 let adjusted = match (from_tz, to_tz) {
1806 (None, Some(to_tz)) => {
1812 let to_tz: Tz = to_tz.parse()?;
1813 match to_unit {
1814 TimeUnit::Second => adjust_timestamp_to_timezone::<TimestampSecondType>(
1815 converted,
1816 &to_tz,
1817 cast_options,
1818 )?,
1819 TimeUnit::Millisecond => adjust_timestamp_to_timezone::<
1820 TimestampMillisecondType,
1821 >(
1822 converted, &to_tz, cast_options
1823 )?,
1824 TimeUnit::Microsecond => adjust_timestamp_to_timezone::<
1825 TimestampMicrosecondType,
1826 >(
1827 converted, &to_tz, cast_options
1828 )?,
1829 TimeUnit::Nanosecond => adjust_timestamp_to_timezone::<
1830 TimestampNanosecondType,
1831 >(
1832 converted, &to_tz, cast_options
1833 )?,
1834 }
1835 }
1836 _ => converted,
1837 };
1838 Ok(make_timestamp_array(&adjusted, *to_unit, to_tz.clone()))
1839 }
1840 (Timestamp(TimeUnit::Microsecond, _), Date32) => {
1841 timestamp_to_date32(array.as_primitive::<TimestampMicrosecondType>())
1842 }
1843 (Timestamp(TimeUnit::Millisecond, _), Date32) => {
1844 timestamp_to_date32(array.as_primitive::<TimestampMillisecondType>())
1845 }
1846 (Timestamp(TimeUnit::Second, _), Date32) => {
1847 timestamp_to_date32(array.as_primitive::<TimestampSecondType>())
1848 }
1849 (Timestamp(TimeUnit::Nanosecond, _), Date32) => {
1850 timestamp_to_date32(array.as_primitive::<TimestampNanosecondType>())
1851 }
1852 (Timestamp(TimeUnit::Second, _), Date64) => Ok(Arc::new(match cast_options.safe {
1853 true => {
1854 array
1856 .as_primitive::<TimestampSecondType>()
1857 .unary_opt::<_, Date64Type>(|x| x.checked_mul(MILLISECONDS))
1858 }
1859 false => array
1860 .as_primitive::<TimestampSecondType>()
1861 .try_unary::<_, Date64Type, _>(|x| x.mul_checked(MILLISECONDS))?,
1862 })),
1863 (Timestamp(TimeUnit::Millisecond, _), Date64) => {
1864 cast_reinterpret_arrays::<TimestampMillisecondType, Date64Type>(array)
1865 }
1866 (Timestamp(TimeUnit::Microsecond, _), Date64) => Ok(Arc::new(
1867 array
1868 .as_primitive::<TimestampMicrosecondType>()
1869 .unary::<_, Date64Type>(|x| x / (MICROSECONDS / MILLISECONDS)),
1870 )),
1871 (Timestamp(TimeUnit::Nanosecond, _), Date64) => Ok(Arc::new(
1872 array
1873 .as_primitive::<TimestampNanosecondType>()
1874 .unary::<_, Date64Type>(|x| x / (NANOSECONDS / MILLISECONDS)),
1875 )),
1876 (Timestamp(TimeUnit::Second, tz), Time64(TimeUnit::Microsecond)) => {
1877 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1878 Ok(Arc::new(
1879 array
1880 .as_primitive::<TimestampSecondType>()
1881 .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1882 Ok(time_to_time64us(as_time_res_with_timezone::<
1883 TimestampSecondType,
1884 >(x, tz)?))
1885 })?,
1886 ))
1887 }
1888 (Timestamp(TimeUnit::Second, tz), Time64(TimeUnit::Nanosecond)) => {
1889 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1890 Ok(Arc::new(
1891 array
1892 .as_primitive::<TimestampSecondType>()
1893 .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1894 Ok(time_to_time64ns(as_time_res_with_timezone::<
1895 TimestampSecondType,
1896 >(x, tz)?))
1897 })?,
1898 ))
1899 }
1900 (Timestamp(TimeUnit::Millisecond, tz), Time64(TimeUnit::Microsecond)) => {
1901 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1902 Ok(Arc::new(
1903 array
1904 .as_primitive::<TimestampMillisecondType>()
1905 .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1906 Ok(time_to_time64us(as_time_res_with_timezone::<
1907 TimestampMillisecondType,
1908 >(x, tz)?))
1909 })?,
1910 ))
1911 }
1912 (Timestamp(TimeUnit::Millisecond, tz), Time64(TimeUnit::Nanosecond)) => {
1913 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1914 Ok(Arc::new(
1915 array
1916 .as_primitive::<TimestampMillisecondType>()
1917 .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1918 Ok(time_to_time64ns(as_time_res_with_timezone::<
1919 TimestampMillisecondType,
1920 >(x, tz)?))
1921 })?,
1922 ))
1923 }
1924 (Timestamp(TimeUnit::Microsecond, tz), Time64(TimeUnit::Microsecond)) => {
1925 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1926 Ok(Arc::new(
1927 array
1928 .as_primitive::<TimestampMicrosecondType>()
1929 .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1930 Ok(time_to_time64us(as_time_res_with_timezone::<
1931 TimestampMicrosecondType,
1932 >(x, tz)?))
1933 })?,
1934 ))
1935 }
1936 (Timestamp(TimeUnit::Microsecond, tz), Time64(TimeUnit::Nanosecond)) => {
1937 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1938 Ok(Arc::new(
1939 array
1940 .as_primitive::<TimestampMicrosecondType>()
1941 .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1942 Ok(time_to_time64ns(as_time_res_with_timezone::<
1943 TimestampMicrosecondType,
1944 >(x, tz)?))
1945 })?,
1946 ))
1947 }
1948 (Timestamp(TimeUnit::Nanosecond, tz), Time64(TimeUnit::Microsecond)) => {
1949 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1950 Ok(Arc::new(
1951 array
1952 .as_primitive::<TimestampNanosecondType>()
1953 .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1954 Ok(time_to_time64us(as_time_res_with_timezone::<
1955 TimestampNanosecondType,
1956 >(x, tz)?))
1957 })?,
1958 ))
1959 }
1960 (Timestamp(TimeUnit::Nanosecond, tz), Time64(TimeUnit::Nanosecond)) => {
1961 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1962 Ok(Arc::new(
1963 array
1964 .as_primitive::<TimestampNanosecondType>()
1965 .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1966 Ok(time_to_time64ns(as_time_res_with_timezone::<
1967 TimestampNanosecondType,
1968 >(x, tz)?))
1969 })?,
1970 ))
1971 }
1972 (Timestamp(TimeUnit::Second, tz), Time32(TimeUnit::Second)) => {
1973 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1974 Ok(Arc::new(
1975 array
1976 .as_primitive::<TimestampSecondType>()
1977 .try_unary::<_, Time32SecondType, ArrowError>(|x| {
1978 Ok(time_to_time32s(as_time_res_with_timezone::<
1979 TimestampSecondType,
1980 >(x, tz)?))
1981 })?,
1982 ))
1983 }
1984 (Timestamp(TimeUnit::Second, tz), Time32(TimeUnit::Millisecond)) => {
1985 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1986 Ok(Arc::new(
1987 array
1988 .as_primitive::<TimestampSecondType>()
1989 .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
1990 Ok(time_to_time32ms(as_time_res_with_timezone::<
1991 TimestampSecondType,
1992 >(x, tz)?))
1993 })?,
1994 ))
1995 }
1996 (Timestamp(TimeUnit::Millisecond, tz), Time32(TimeUnit::Second)) => {
1997 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1998 Ok(Arc::new(
1999 array
2000 .as_primitive::<TimestampMillisecondType>()
2001 .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2002 Ok(time_to_time32s(as_time_res_with_timezone::<
2003 TimestampMillisecondType,
2004 >(x, tz)?))
2005 })?,
2006 ))
2007 }
2008 (Timestamp(TimeUnit::Millisecond, tz), Time32(TimeUnit::Millisecond)) => {
2009 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2010 Ok(Arc::new(
2011 array
2012 .as_primitive::<TimestampMillisecondType>()
2013 .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2014 Ok(time_to_time32ms(as_time_res_with_timezone::<
2015 TimestampMillisecondType,
2016 >(x, tz)?))
2017 })?,
2018 ))
2019 }
2020 (Timestamp(TimeUnit::Microsecond, tz), Time32(TimeUnit::Second)) => {
2021 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2022 Ok(Arc::new(
2023 array
2024 .as_primitive::<TimestampMicrosecondType>()
2025 .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2026 Ok(time_to_time32s(as_time_res_with_timezone::<
2027 TimestampMicrosecondType,
2028 >(x, tz)?))
2029 })?,
2030 ))
2031 }
2032 (Timestamp(TimeUnit::Microsecond, tz), Time32(TimeUnit::Millisecond)) => {
2033 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2034 Ok(Arc::new(
2035 array
2036 .as_primitive::<TimestampMicrosecondType>()
2037 .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2038 Ok(time_to_time32ms(as_time_res_with_timezone::<
2039 TimestampMicrosecondType,
2040 >(x, tz)?))
2041 })?,
2042 ))
2043 }
2044 (Timestamp(TimeUnit::Nanosecond, tz), Time32(TimeUnit::Second)) => {
2045 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2046 Ok(Arc::new(
2047 array
2048 .as_primitive::<TimestampNanosecondType>()
2049 .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2050 Ok(time_to_time32s(as_time_res_with_timezone::<
2051 TimestampNanosecondType,
2052 >(x, tz)?))
2053 })?,
2054 ))
2055 }
2056 (Timestamp(TimeUnit::Nanosecond, tz), Time32(TimeUnit::Millisecond)) => {
2057 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2058 Ok(Arc::new(
2059 array
2060 .as_primitive::<TimestampNanosecondType>()
2061 .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2062 Ok(time_to_time32ms(as_time_res_with_timezone::<
2063 TimestampNanosecondType,
2064 >(x, tz)?))
2065 })?,
2066 ))
2067 }
2068 (Date64, Timestamp(TimeUnit::Second, _)) => {
2069 let array = array
2070 .as_primitive::<Date64Type>()
2071 .unary::<_, TimestampSecondType>(|x| x / MILLISECONDS);
2072
2073 cast_with_options(&array, to_type, cast_options)
2074 }
2075 (Date64, Timestamp(TimeUnit::Millisecond, _)) => {
2076 let array = array
2077 .as_primitive::<Date64Type>()
2078 .reinterpret_cast::<TimestampMillisecondType>();
2079
2080 cast_with_options(&array, to_type, cast_options)
2081 }
2082
2083 (Date64, Timestamp(TimeUnit::Microsecond, _)) => {
2084 let array = array
2085 .as_primitive::<Date64Type>()
2086 .unary::<_, TimestampMicrosecondType>(|x| x * (MICROSECONDS / MILLISECONDS));
2087
2088 cast_with_options(&array, to_type, cast_options)
2089 }
2090 (Date64, Timestamp(TimeUnit::Nanosecond, _)) => {
2091 let array = array
2092 .as_primitive::<Date64Type>()
2093 .unary::<_, TimestampNanosecondType>(|x| x * (NANOSECONDS / MILLISECONDS));
2094
2095 cast_with_options(&array, to_type, cast_options)
2096 }
2097 (Date32, Timestamp(TimeUnit::Second, _)) => {
2098 let array = array
2099 .as_primitive::<Date32Type>()
2100 .unary::<_, TimestampSecondType>(|x| (x as i64) * SECONDS_IN_DAY);
2101
2102 cast_with_options(&array, to_type, cast_options)
2103 }
2104 (Date32, Timestamp(TimeUnit::Millisecond, _)) => {
2105 let array = array
2106 .as_primitive::<Date32Type>()
2107 .unary::<_, TimestampMillisecondType>(|x| (x as i64) * MILLISECONDS_IN_DAY);
2108
2109 cast_with_options(&array, to_type, cast_options)
2110 }
2111 (Date32, Timestamp(TimeUnit::Microsecond, _)) => {
2112 let array = array
2113 .as_primitive::<Date32Type>()
2114 .unary::<_, TimestampMicrosecondType>(|x| (x as i64) * MICROSECONDS_IN_DAY);
2115
2116 cast_with_options(&array, to_type, cast_options)
2117 }
2118 (Date32, Timestamp(TimeUnit::Nanosecond, _)) => {
2119 let array = array
2120 .as_primitive::<Date32Type>()
2121 .unary::<_, TimestampNanosecondType>(|x| (x as i64) * NANOSECONDS_IN_DAY);
2122
2123 cast_with_options(&array, to_type, cast_options)
2124 }
2125
2126 (_, Duration(unit)) if from_type.is_numeric() => {
2127 let array = cast_with_options(array, &Int64, cast_options)?;
2128 Ok(make_duration_array(array.as_primitive(), *unit))
2129 }
2130 (Duration(TimeUnit::Second), _) if to_type.is_numeric() => {
2131 let array = cast_reinterpret_arrays::<DurationSecondType, Int64Type>(array)?;
2132 cast_with_options(&array, to_type, cast_options)
2133 }
2134 (Duration(TimeUnit::Millisecond), _) if to_type.is_numeric() => {
2135 let array = cast_reinterpret_arrays::<DurationMillisecondType, Int64Type>(array)?;
2136 cast_with_options(&array, to_type, cast_options)
2137 }
2138 (Duration(TimeUnit::Microsecond), _) if to_type.is_numeric() => {
2139 let array = cast_reinterpret_arrays::<DurationMicrosecondType, Int64Type>(array)?;
2140 cast_with_options(&array, to_type, cast_options)
2141 }
2142 (Duration(TimeUnit::Nanosecond), _) if to_type.is_numeric() => {
2143 let array = cast_reinterpret_arrays::<DurationNanosecondType, Int64Type>(array)?;
2144 cast_with_options(&array, to_type, cast_options)
2145 }
2146
2147 (Duration(from_unit), Duration(to_unit)) => {
2148 let array = cast_with_options(array, &Int64, cast_options)?;
2149 let time_array = array.as_primitive::<Int64Type>();
2150 let from_size = time_unit_multiple(from_unit);
2151 let to_size = time_unit_multiple(to_unit);
2152 let converted = match from_size.cmp(&to_size) {
2155 Ordering::Greater => {
2156 let divisor = from_size / to_size;
2157 time_array.unary::<_, Int64Type>(|o| o / divisor)
2158 }
2159 Ordering::Equal => time_array.clone(),
2160 Ordering::Less => {
2161 let mul = to_size / from_size;
2162 if cast_options.safe {
2163 time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
2164 } else {
2165 time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
2166 }
2167 }
2168 };
2169 Ok(make_duration_array(&converted, *to_unit))
2170 }
2171
2172 (Duration(TimeUnit::Second), Interval(IntervalUnit::MonthDayNano)) => {
2173 cast_duration_to_interval::<DurationSecondType>(array, cast_options)
2174 }
2175 (Duration(TimeUnit::Millisecond), Interval(IntervalUnit::MonthDayNano)) => {
2176 cast_duration_to_interval::<DurationMillisecondType>(array, cast_options)
2177 }
2178 (Duration(TimeUnit::Microsecond), Interval(IntervalUnit::MonthDayNano)) => {
2179 cast_duration_to_interval::<DurationMicrosecondType>(array, cast_options)
2180 }
2181 (Duration(TimeUnit::Nanosecond), Interval(IntervalUnit::MonthDayNano)) => {
2182 cast_duration_to_interval::<DurationNanosecondType>(array, cast_options)
2183 }
2184 (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Second)) => {
2185 cast_month_day_nano_to_duration::<DurationSecondType>(array, cast_options)
2186 }
2187 (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Millisecond)) => {
2188 cast_month_day_nano_to_duration::<DurationMillisecondType>(array, cast_options)
2189 }
2190 (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Microsecond)) => {
2191 cast_month_day_nano_to_duration::<DurationMicrosecondType>(array, cast_options)
2192 }
2193 (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Nanosecond)) => {
2194 cast_month_day_nano_to_duration::<DurationNanosecondType>(array, cast_options)
2195 }
2196 (Interval(IntervalUnit::YearMonth), Interval(IntervalUnit::MonthDayNano)) => {
2197 cast_interval_year_month_to_interval_month_day_nano(array, cast_options)
2198 }
2199 (Interval(IntervalUnit::DayTime), Interval(IntervalUnit::MonthDayNano)) => {
2200 cast_interval_day_time_to_interval_month_day_nano(array, cast_options)
2201 }
2202 (Int32, Interval(IntervalUnit::YearMonth)) => {
2203 cast_reinterpret_arrays::<Int32Type, IntervalYearMonthType>(array)
2204 }
2205 (_, _) => Err(ArrowError::CastError(format!(
2206 "Casting from {from_type} to {to_type} not supported",
2207 ))),
2208 }
2209}
2210
2211fn cast_struct_to_struct(
2212 array: &StructArray,
2213 from_fields: Fields,
2214 to_fields: Fields,
2215 cast_options: &CastOptions,
2216) -> Result<ArrayRef, ArrowError> {
2217 let fields_match_order = from_fields.len() == to_fields.len()
2219 && from_fields
2220 .iter()
2221 .zip(to_fields.iter())
2222 .all(|(f1, f2)| f1.name() == f2.name());
2223
2224 let fields = if fields_match_order {
2225 cast_struct_fields_in_order(array, to_fields.clone(), cast_options)?
2227 } else {
2228 let all_fields_match_by_name = to_fields.iter().all(|to_field| {
2229 from_fields
2230 .iter()
2231 .any(|from_field| from_field.name() == to_field.name())
2232 });
2233
2234 if all_fields_match_by_name {
2235 cast_struct_fields_by_name(array, from_fields.clone(), to_fields.clone(), cast_options)?
2237 } else {
2238 cast_struct_fields_in_order(array, to_fields.clone(), cast_options)?
2240 }
2241 };
2242
2243 let array = StructArray::try_new(to_fields.clone(), fields, array.nulls().cloned())?;
2244 Ok(Arc::new(array) as ArrayRef)
2245}
2246
2247fn cast_struct_fields_by_name(
2248 array: &StructArray,
2249 from_fields: Fields,
2250 to_fields: Fields,
2251 cast_options: &CastOptions,
2252) -> Result<Vec<ArrayRef>, ArrowError> {
2253 to_fields
2254 .iter()
2255 .map(|to_field| {
2256 let from_field_idx = from_fields
2257 .iter()
2258 .position(|from_field| from_field.name() == to_field.name())
2259 .unwrap(); let column = array.column(from_field_idx);
2261 cast_with_options(column, to_field.data_type(), cast_options)
2262 })
2263 .collect::<Result<Vec<ArrayRef>, ArrowError>>()
2264}
2265
2266fn cast_struct_fields_in_order(
2267 array: &StructArray,
2268 to_fields: Fields,
2269 cast_options: &CastOptions,
2270) -> Result<Vec<ArrayRef>, ArrowError> {
2271 array
2272 .columns()
2273 .iter()
2274 .zip(to_fields.iter())
2275 .map(|(l, field)| cast_with_options(l, field.data_type(), cast_options))
2276 .collect::<Result<Vec<ArrayRef>, ArrowError>>()
2277}
2278
2279fn cast_from_decimal<D, F>(
2280 array: &dyn Array,
2281 base: D::Native,
2282 scale: &i8,
2283 from_type: &DataType,
2284 to_type: &DataType,
2285 as_float: F,
2286 cast_options: &CastOptions,
2287) -> Result<ArrayRef, ArrowError>
2288where
2289 D: DecimalType + ArrowPrimitiveType,
2290 <D as ArrowPrimitiveType>::Native: ArrowNativeTypeOp + ToPrimitive,
2291 F: Fn(D::Native) -> f64,
2292{
2293 use DataType::*;
2294 match to_type {
2296 UInt8 => cast_decimal_to_integer::<D, UInt8Type>(array, base, *scale, cast_options),
2297 UInt16 => cast_decimal_to_integer::<D, UInt16Type>(array, base, *scale, cast_options),
2298 UInt32 => cast_decimal_to_integer::<D, UInt32Type>(array, base, *scale, cast_options),
2299 UInt64 => cast_decimal_to_integer::<D, UInt64Type>(array, base, *scale, cast_options),
2300 Int8 => cast_decimal_to_integer::<D, Int8Type>(array, base, *scale, cast_options),
2301 Int16 => cast_decimal_to_integer::<D, Int16Type>(array, base, *scale, cast_options),
2302 Int32 => cast_decimal_to_integer::<D, Int32Type>(array, base, *scale, cast_options),
2303 Int64 => cast_decimal_to_integer::<D, Int64Type>(array, base, *scale, cast_options),
2304 Float32 => cast_decimal_to_float::<D, Float32Type, _>(array, |x| {
2305 (as_float(x) / 10_f64.powi(*scale as i32)) as f32
2306 }),
2307 Float64 => cast_decimal_to_float::<D, Float64Type, _>(array, |x| {
2308 as_float(x) / 10_f64.powi(*scale as i32)
2309 }),
2310 Utf8View => value_to_string_view(array, cast_options),
2311 Utf8 => value_to_string::<i32>(array, cast_options),
2312 LargeUtf8 => value_to_string::<i64>(array, cast_options),
2313 Null => Ok(new_null_array(to_type, array.len())),
2314 _ => Err(ArrowError::CastError(format!(
2315 "Casting from {from_type} to {to_type} not supported"
2316 ))),
2317 }
2318}
2319
2320fn cast_to_decimal<D, M>(
2321 array: &dyn Array,
2322 base: M,
2323 precision: &u8,
2324 scale: &i8,
2325 from_type: &DataType,
2326 to_type: &DataType,
2327 cast_options: &CastOptions,
2328) -> Result<ArrayRef, ArrowError>
2329where
2330 D: DecimalType + ArrowPrimitiveType<Native = M>,
2331 M: ArrowNativeTypeOp + DecimalCast,
2332 u8: num_traits::AsPrimitive<M>,
2333 u16: num_traits::AsPrimitive<M>,
2334 u32: num_traits::AsPrimitive<M>,
2335 u64: num_traits::AsPrimitive<M>,
2336 i8: num_traits::AsPrimitive<M>,
2337 i16: num_traits::AsPrimitive<M>,
2338 i32: num_traits::AsPrimitive<M>,
2339 i64: num_traits::AsPrimitive<M>,
2340{
2341 use DataType::*;
2342 match from_type {
2344 UInt8 => cast_integer_to_decimal::<_, D, M>(
2345 array.as_primitive::<UInt8Type>(),
2346 *precision,
2347 *scale,
2348 base,
2349 cast_options,
2350 ),
2351 UInt16 => cast_integer_to_decimal::<_, D, _>(
2352 array.as_primitive::<UInt16Type>(),
2353 *precision,
2354 *scale,
2355 base,
2356 cast_options,
2357 ),
2358 UInt32 => cast_integer_to_decimal::<_, D, _>(
2359 array.as_primitive::<UInt32Type>(),
2360 *precision,
2361 *scale,
2362 base,
2363 cast_options,
2364 ),
2365 UInt64 => cast_integer_to_decimal::<_, D, _>(
2366 array.as_primitive::<UInt64Type>(),
2367 *precision,
2368 *scale,
2369 base,
2370 cast_options,
2371 ),
2372 Int8 => cast_integer_to_decimal::<_, D, _>(
2373 array.as_primitive::<Int8Type>(),
2374 *precision,
2375 *scale,
2376 base,
2377 cast_options,
2378 ),
2379 Int16 => cast_integer_to_decimal::<_, D, _>(
2380 array.as_primitive::<Int16Type>(),
2381 *precision,
2382 *scale,
2383 base,
2384 cast_options,
2385 ),
2386 Int32 => cast_integer_to_decimal::<_, D, _>(
2387 array.as_primitive::<Int32Type>(),
2388 *precision,
2389 *scale,
2390 base,
2391 cast_options,
2392 ),
2393 Int64 => cast_integer_to_decimal::<_, D, _>(
2394 array.as_primitive::<Int64Type>(),
2395 *precision,
2396 *scale,
2397 base,
2398 cast_options,
2399 ),
2400 Float32 => cast_floating_point_to_decimal::<_, D>(
2401 array.as_primitive::<Float32Type>(),
2402 *precision,
2403 *scale,
2404 cast_options,
2405 ),
2406 Float64 => cast_floating_point_to_decimal::<_, D>(
2407 array.as_primitive::<Float64Type>(),
2408 *precision,
2409 *scale,
2410 cast_options,
2411 ),
2412 Utf8View | Utf8 => {
2413 cast_string_to_decimal::<D, i32>(array, *precision, *scale, cast_options)
2414 }
2415 LargeUtf8 => cast_string_to_decimal::<D, i64>(array, *precision, *scale, cast_options),
2416 Null => Ok(new_null_array(to_type, array.len())),
2417 _ => Err(ArrowError::CastError(format!(
2418 "Casting from {from_type} to {to_type} not supported"
2419 ))),
2420 }
2421}
2422
2423const fn time_unit_multiple(unit: &TimeUnit) -> i64 {
2425 match unit {
2426 TimeUnit::Second => 1,
2427 TimeUnit::Millisecond => MILLISECONDS,
2428 TimeUnit::Microsecond => MICROSECONDS,
2429 TimeUnit::Nanosecond => NANOSECONDS,
2430 }
2431}
2432
2433fn cast_numeric_arrays<FROM, TO>(
2435 from: &dyn Array,
2436 cast_options: &CastOptions,
2437) -> Result<ArrayRef, ArrowError>
2438where
2439 FROM: ArrowPrimitiveType,
2440 TO: ArrowPrimitiveType,
2441 FROM::Native: NumCast,
2442 TO::Native: NumCast,
2443{
2444 if cast_options.safe {
2445 Ok(Arc::new(numeric_cast::<FROM, TO>(
2447 from.as_primitive::<FROM>(),
2448 )))
2449 } else {
2450 Ok(Arc::new(try_numeric_cast::<FROM, TO>(
2452 from.as_primitive::<FROM>(),
2453 )?))
2454 }
2455}
2456
2457fn try_numeric_cast<T, R>(from: &PrimitiveArray<T>) -> Result<PrimitiveArray<R>, ArrowError>
2460where
2461 T: ArrowPrimitiveType,
2462 R: ArrowPrimitiveType,
2463 T::Native: NumCast,
2464 R::Native: NumCast,
2465{
2466 from.try_unary(|value| {
2467 num_traits::cast::cast::<T::Native, R::Native>(value).ok_or_else(|| {
2468 ArrowError::CastError(format!(
2469 "Can't cast value {:?} to type {}",
2470 value,
2471 R::DATA_TYPE
2472 ))
2473 })
2474 })
2475}
2476
2477fn numeric_cast<T, R>(from: &PrimitiveArray<T>) -> PrimitiveArray<R>
2480where
2481 T: ArrowPrimitiveType,
2482 R: ArrowPrimitiveType,
2483 T::Native: NumCast,
2484 R::Native: NumCast,
2485{
2486 from.unary_opt::<_, R>(num_traits::cast::cast::<T::Native, R::Native>)
2487}
2488
2489fn cast_numeric_to_binary<FROM: ArrowPrimitiveType, O: OffsetSizeTrait>(
2490 array: &dyn Array,
2491) -> Result<ArrayRef, ArrowError> {
2492 let array = array.as_primitive::<FROM>();
2493 let size = std::mem::size_of::<FROM::Native>();
2494 let offsets = OffsetBuffer::from_repeated_length(size, array.len());
2495 Ok(Arc::new(GenericBinaryArray::<O>::try_new(
2496 offsets,
2497 array.values().inner().clone(),
2498 array.nulls().cloned(),
2499 )?))
2500}
2501
2502fn adjust_timestamp_to_timezone<T: ArrowTimestampType>(
2503 array: PrimitiveArray<Int64Type>,
2504 to_tz: &Tz,
2505 cast_options: &CastOptions,
2506) -> Result<PrimitiveArray<Int64Type>, ArrowError> {
2507 let adjust = |o| {
2508 let local = as_datetime::<T>(o)?;
2509 let offset = to_tz.offset_from_local_datetime(&local).single()?;
2510 T::make_value(local - offset.fix())
2511 };
2512 let adjusted = if cast_options.safe {
2513 array.unary_opt::<_, Int64Type>(adjust)
2514 } else {
2515 array.try_unary::<_, Int64Type, _>(|o| {
2516 adjust(o).ok_or_else(|| {
2517 ArrowError::CastError("Cannot cast timezone to different timezone".to_string())
2518 })
2519 })?
2520 };
2521 Ok(adjusted)
2522}
2523
2524fn cast_numeric_to_bool<FROM>(from: &dyn Array) -> Result<ArrayRef, ArrowError>
2528where
2529 FROM: ArrowPrimitiveType,
2530{
2531 numeric_to_bool_cast::<FROM>(from.as_primitive::<FROM>()).map(|to| Arc::new(to) as ArrayRef)
2532}
2533
2534fn numeric_to_bool_cast<T>(from: &PrimitiveArray<T>) -> Result<BooleanArray, ArrowError>
2535where
2536 T: ArrowPrimitiveType + ArrowPrimitiveType,
2537{
2538 let mut b = BooleanBuilder::with_capacity(from.len());
2539
2540 for i in 0..from.len() {
2541 if from.is_null(i) {
2542 b.append_null();
2543 } else if from.value(i) != T::default_value() {
2544 b.append_value(true);
2545 } else {
2546 b.append_value(false);
2547 }
2548 }
2549
2550 Ok(b.finish())
2551}
2552
2553fn cast_bool_to_numeric<TO>(
2557 from: &dyn Array,
2558 cast_options: &CastOptions,
2559) -> Result<ArrayRef, ArrowError>
2560where
2561 TO: ArrowPrimitiveType,
2562 TO::Native: num_traits::cast::NumCast,
2563{
2564 Ok(Arc::new(bool_to_numeric_cast::<TO>(
2565 from.as_any().downcast_ref::<BooleanArray>().unwrap(),
2566 cast_options,
2567 )))
2568}
2569
2570fn bool_to_numeric_cast<T>(from: &BooleanArray, _cast_options: &CastOptions) -> PrimitiveArray<T>
2571where
2572 T: ArrowPrimitiveType,
2573 T::Native: num_traits::NumCast,
2574{
2575 let iter = (0..from.len()).map(|i| {
2576 if from.is_null(i) {
2577 None
2578 } else if from.value(i) {
2579 num_traits::cast::cast(1)
2581 } else {
2582 Some(T::default_value())
2583 }
2584 });
2585 unsafe { PrimitiveArray::<T>::from_trusted_len_iter(iter) }
2590}
2591
2592fn cast_binary_to_fixed_size_binary<O: OffsetSizeTrait>(
2594 array: &dyn Array,
2595 byte_width: i32,
2596 cast_options: &CastOptions,
2597) -> Result<ArrayRef, ArrowError> {
2598 let array = array.as_binary::<O>();
2599 let mut builder = FixedSizeBinaryBuilder::with_capacity(array.len(), byte_width);
2600
2601 for i in 0..array.len() {
2602 if array.is_null(i) {
2603 builder.append_null();
2604 } else {
2605 match builder.append_value(array.value(i)) {
2606 Ok(_) => {}
2607 Err(e) => match cast_options.safe {
2608 true => builder.append_null(),
2609 false => return Err(e),
2610 },
2611 }
2612 }
2613 }
2614
2615 Ok(Arc::new(builder.finish()))
2616}
2617
2618fn cast_fixed_size_binary_to_binary<O: OffsetSizeTrait>(
2621 array: &dyn Array,
2622 byte_width: i32,
2623) -> Result<ArrayRef, ArrowError> {
2624 let array = array
2625 .as_any()
2626 .downcast_ref::<FixedSizeBinaryArray>()
2627 .unwrap();
2628
2629 let offsets: i128 = byte_width as i128 * array.len() as i128;
2630
2631 let is_binary = matches!(GenericBinaryType::<O>::DATA_TYPE, DataType::Binary);
2632 if is_binary && offsets > i32::MAX as i128 {
2633 return Err(ArrowError::ComputeError(
2634 "FixedSizeBinary array too large to cast to Binary array".to_string(),
2635 ));
2636 } else if !is_binary && offsets > i64::MAX as i128 {
2637 return Err(ArrowError::ComputeError(
2638 "FixedSizeBinary array too large to cast to LargeBinary array".to_string(),
2639 ));
2640 }
2641
2642 let mut builder = GenericBinaryBuilder::<O>::with_capacity(array.len(), array.len());
2643
2644 for i in 0..array.len() {
2645 if array.is_null(i) {
2646 builder.append_null();
2647 } else {
2648 builder.append_value(array.value(i));
2649 }
2650 }
2651
2652 Ok(Arc::new(builder.finish()))
2653}
2654
2655fn cast_fixed_size_binary_to_binary_view(
2656 array: &dyn Array,
2657 _byte_width: i32,
2658) -> Result<ArrayRef, ArrowError> {
2659 let array = array
2660 .as_any()
2661 .downcast_ref::<FixedSizeBinaryArray>()
2662 .unwrap();
2663
2664 let mut builder = BinaryViewBuilder::with_capacity(array.len());
2665 for i in 0..array.len() {
2666 if array.is_null(i) {
2667 builder.append_null();
2668 } else {
2669 builder.append_value(array.value(i));
2670 }
2671 }
2672
2673 Ok(Arc::new(builder.finish()))
2674}
2675
2676fn cast_byte_container<FROM, TO>(array: &dyn Array) -> Result<ArrayRef, ArrowError>
2679where
2680 FROM: ByteArrayType,
2681 TO: ByteArrayType<Native = FROM::Native>,
2682 FROM::Offset: OffsetSizeTrait + ToPrimitive,
2683 TO::Offset: OffsetSizeTrait + NumCast,
2684{
2685 let data = array.to_data();
2686 assert_eq!(data.data_type(), &FROM::DATA_TYPE);
2687 let str_values_buf = data.buffers()[1].clone();
2688 let offsets = data.buffers()[0].typed_data::<FROM::Offset>();
2689
2690 let mut offset_builder = BufferBuilder::<TO::Offset>::new(offsets.len());
2691 offsets
2692 .iter()
2693 .try_for_each::<_, Result<_, ArrowError>>(|offset| {
2694 let offset =
2695 <<TO as ByteArrayType>::Offset as NumCast>::from(*offset).ok_or_else(|| {
2696 ArrowError::ComputeError(format!(
2697 "{}{} array too large to cast to {}{} array",
2698 FROM::Offset::PREFIX,
2699 FROM::PREFIX,
2700 TO::Offset::PREFIX,
2701 TO::PREFIX
2702 ))
2703 })?;
2704 offset_builder.append(offset);
2705 Ok(())
2706 })?;
2707
2708 let offset_buffer = offset_builder.finish();
2709
2710 let dtype = TO::DATA_TYPE;
2711
2712 let builder = ArrayData::builder(dtype)
2713 .offset(array.offset())
2714 .len(array.len())
2715 .add_buffer(offset_buffer)
2716 .add_buffer(str_values_buf)
2717 .nulls(data.nulls().cloned());
2718
2719 let array_data = unsafe { builder.build_unchecked() };
2720
2721 Ok(Arc::new(GenericByteArray::<TO>::from(array_data)))
2722}
2723
2724fn cast_view_to_byte<FROM, TO>(array: &dyn Array) -> Result<ArrayRef, ArrowError>
2726where
2727 FROM: ByteViewType,
2728 TO: ByteArrayType,
2729 FROM::Native: AsRef<TO::Native>,
2730{
2731 let data = array.to_data();
2732 let view_array = GenericByteViewArray::<FROM>::from(data);
2733
2734 let len = view_array.len();
2735 let bytes = view_array
2736 .views()
2737 .iter()
2738 .map(|v| ByteView::from(*v).length as usize)
2739 .sum::<usize>();
2740
2741 let mut byte_array_builder = GenericByteBuilder::<TO>::with_capacity(len, bytes);
2742
2743 for val in view_array.iter() {
2744 byte_array_builder.append_option(val);
2745 }
2746
2747 Ok(Arc::new(byte_array_builder.finish()))
2748}
2749
2750#[cfg(test)]
2751mod tests {
2752 use super::*;
2753 use DataType::*;
2754 use arrow_array::{Int64Array, RunArray, StringArray};
2755 use arrow_buffer::{Buffer, IntervalDayTime, NullBuffer};
2756 use arrow_buffer::{ScalarBuffer, i256};
2757 use arrow_schema::{DataType, Field};
2758 use chrono::NaiveDate;
2759 use half::f16;
2760 use std::sync::Arc;
2761
2762 #[derive(Clone)]
2763 struct DecimalCastTestConfig {
2764 input_prec: u8,
2765 input_scale: i8,
2766 input_repr: i128,
2767 output_prec: u8,
2768 output_scale: i8,
2769 expected_output_repr: Result<i128, String>, }
2774
2775 macro_rules! generate_cast_test_case {
2776 ($INPUT_ARRAY: expr, $OUTPUT_TYPE_ARRAY: ident, $OUTPUT_TYPE: expr, $OUTPUT_VALUES: expr) => {
2777 let output =
2778 $OUTPUT_TYPE_ARRAY::from($OUTPUT_VALUES).with_data_type($OUTPUT_TYPE.clone());
2779
2780 let input_array_type = $INPUT_ARRAY.data_type();
2782 assert!(can_cast_types(input_array_type, $OUTPUT_TYPE));
2783 let result = cast($INPUT_ARRAY, $OUTPUT_TYPE).unwrap();
2784 assert_eq!($OUTPUT_TYPE, result.data_type());
2785 assert_eq!(result.as_ref(), &output);
2786
2787 let cast_option = CastOptions {
2788 safe: false,
2789 format_options: FormatOptions::default(),
2790 };
2791 let result = cast_with_options($INPUT_ARRAY, $OUTPUT_TYPE, &cast_option).unwrap();
2792 assert_eq!($OUTPUT_TYPE, result.data_type());
2793 assert_eq!(result.as_ref(), &output);
2794 };
2795 }
2796
2797 fn run_decimal_cast_test_case<I, O>(t: DecimalCastTestConfig)
2798 where
2799 I: DecimalType,
2800 O: DecimalType,
2801 I::Native: DecimalCast,
2802 O::Native: DecimalCast,
2803 {
2804 let array = vec![I::Native::from_decimal(t.input_repr)];
2805 let array = array
2806 .into_iter()
2807 .collect::<PrimitiveArray<I>>()
2808 .with_precision_and_scale(t.input_prec, t.input_scale)
2809 .unwrap();
2810 let input_type = array.data_type();
2811 let output_type = O::TYPE_CONSTRUCTOR(t.output_prec, t.output_scale);
2812 assert!(can_cast_types(input_type, &output_type));
2813
2814 let options = CastOptions {
2815 safe: false,
2816 ..Default::default()
2817 };
2818 let result = cast_with_options(&array, &output_type, &options);
2819
2820 match t.expected_output_repr {
2821 Ok(v) => {
2822 let expected_array = vec![O::Native::from_decimal(v)];
2823 let expected_array = expected_array
2824 .into_iter()
2825 .collect::<PrimitiveArray<O>>()
2826 .with_precision_and_scale(t.output_prec, t.output_scale)
2827 .unwrap();
2828 assert_eq!(*result.unwrap(), expected_array);
2829 }
2830 Err(expected_output_message_template) => {
2831 assert!(result.is_err());
2832 let expected_error_message =
2833 expected_output_message_template.replace("{}", O::PREFIX);
2834 assert_eq!(result.unwrap_err().to_string(), expected_error_message);
2835 }
2836 }
2837 }
2838
2839 fn create_decimal32_array(
2840 array: Vec<Option<i32>>,
2841 precision: u8,
2842 scale: i8,
2843 ) -> Result<Decimal32Array, ArrowError> {
2844 array
2845 .into_iter()
2846 .collect::<Decimal32Array>()
2847 .with_precision_and_scale(precision, scale)
2848 }
2849
2850 fn create_decimal64_array(
2851 array: Vec<Option<i64>>,
2852 precision: u8,
2853 scale: i8,
2854 ) -> Result<Decimal64Array, ArrowError> {
2855 array
2856 .into_iter()
2857 .collect::<Decimal64Array>()
2858 .with_precision_and_scale(precision, scale)
2859 }
2860
2861 fn create_decimal128_array(
2862 array: Vec<Option<i128>>,
2863 precision: u8,
2864 scale: i8,
2865 ) -> Result<Decimal128Array, ArrowError> {
2866 array
2867 .into_iter()
2868 .collect::<Decimal128Array>()
2869 .with_precision_and_scale(precision, scale)
2870 }
2871
2872 fn create_decimal256_array(
2873 array: Vec<Option<i256>>,
2874 precision: u8,
2875 scale: i8,
2876 ) -> Result<Decimal256Array, ArrowError> {
2877 array
2878 .into_iter()
2879 .collect::<Decimal256Array>()
2880 .with_precision_and_scale(precision, scale)
2881 }
2882
2883 #[test]
2884 #[cfg(not(feature = "force_validate"))]
2885 #[should_panic(
2886 expected = "Cannot cast to Decimal128(20, 3). Overflowing on 57896044618658097711785492504343953926634992332820282019728792003956564819967"
2887 )]
2888 fn test_cast_decimal_to_decimal_round_with_error() {
2889 let array = vec![
2891 Some(i256::from_i128(1123454)),
2892 Some(i256::from_i128(2123456)),
2893 Some(i256::from_i128(-3123453)),
2894 Some(i256::from_i128(-3123456)),
2895 None,
2896 Some(i256::MAX),
2897 Some(i256::MIN),
2898 ];
2899 let input_decimal_array = create_decimal256_array(array, 76, 4).unwrap();
2900 let array = Arc::new(input_decimal_array) as ArrayRef;
2901 let input_type = DataType::Decimal256(76, 4);
2902 let output_type = DataType::Decimal128(20, 3);
2903 assert!(can_cast_types(&input_type, &output_type));
2904 generate_cast_test_case!(
2905 &array,
2906 Decimal128Array,
2907 &output_type,
2908 vec![
2909 Some(112345_i128),
2910 Some(212346_i128),
2911 Some(-312345_i128),
2912 Some(-312346_i128),
2913 None,
2914 None,
2915 None,
2916 ]
2917 );
2918 }
2919
2920 #[test]
2921 #[cfg(not(feature = "force_validate"))]
2922 fn test_cast_decimal_to_decimal_round() {
2923 let array = vec![
2924 Some(1123454),
2925 Some(2123456),
2926 Some(-3123453),
2927 Some(-3123456),
2928 None,
2929 ];
2930 let array = create_decimal128_array(array, 20, 4).unwrap();
2931 let input_type = DataType::Decimal128(20, 4);
2933 let output_type = DataType::Decimal128(20, 3);
2934 assert!(can_cast_types(&input_type, &output_type));
2935 generate_cast_test_case!(
2936 &array,
2937 Decimal128Array,
2938 &output_type,
2939 vec![
2940 Some(112345_i128),
2941 Some(212346_i128),
2942 Some(-312345_i128),
2943 Some(-312346_i128),
2944 None
2945 ]
2946 );
2947
2948 let input_type = DataType::Decimal128(20, 4);
2950 let output_type = DataType::Decimal256(20, 3);
2951 assert!(can_cast_types(&input_type, &output_type));
2952 generate_cast_test_case!(
2953 &array,
2954 Decimal256Array,
2955 &output_type,
2956 vec![
2957 Some(i256::from_i128(112345_i128)),
2958 Some(i256::from_i128(212346_i128)),
2959 Some(i256::from_i128(-312345_i128)),
2960 Some(i256::from_i128(-312346_i128)),
2961 None
2962 ]
2963 );
2964
2965 let array = vec![
2967 Some(i256::from_i128(1123454)),
2968 Some(i256::from_i128(2123456)),
2969 Some(i256::from_i128(-3123453)),
2970 Some(i256::from_i128(-3123456)),
2971 None,
2972 ];
2973 let array = create_decimal256_array(array, 20, 4).unwrap();
2974
2975 let input_type = DataType::Decimal256(20, 4);
2977 let output_type = DataType::Decimal256(20, 3);
2978 assert!(can_cast_types(&input_type, &output_type));
2979 generate_cast_test_case!(
2980 &array,
2981 Decimal256Array,
2982 &output_type,
2983 vec![
2984 Some(i256::from_i128(112345_i128)),
2985 Some(i256::from_i128(212346_i128)),
2986 Some(i256::from_i128(-312345_i128)),
2987 Some(i256::from_i128(-312346_i128)),
2988 None
2989 ]
2990 );
2991 let input_type = DataType::Decimal256(20, 4);
2993 let output_type = DataType::Decimal128(20, 3);
2994 assert!(can_cast_types(&input_type, &output_type));
2995 generate_cast_test_case!(
2996 &array,
2997 Decimal128Array,
2998 &output_type,
2999 vec![
3000 Some(112345_i128),
3001 Some(212346_i128),
3002 Some(-312345_i128),
3003 Some(-312346_i128),
3004 None
3005 ]
3006 );
3007 }
3008
3009 #[test]
3010 fn test_cast_decimal32_to_decimal32() {
3011 let input_type = DataType::Decimal32(9, 3);
3013 let output_type = DataType::Decimal32(9, 4);
3014 assert!(can_cast_types(&input_type, &output_type));
3015 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3016 let array = create_decimal32_array(array, 9, 3).unwrap();
3017 generate_cast_test_case!(
3018 &array,
3019 Decimal32Array,
3020 &output_type,
3021 vec![
3022 Some(11234560_i32),
3023 Some(21234560_i32),
3024 Some(31234560_i32),
3025 None
3026 ]
3027 );
3028 let array = vec![Some(123456), None];
3030 let array = create_decimal32_array(array, 9, 0).unwrap();
3031 let result_safe = cast(&array, &DataType::Decimal32(2, 2));
3032 assert!(result_safe.is_ok());
3033 let options = CastOptions {
3034 safe: false,
3035 ..Default::default()
3036 };
3037
3038 let result_unsafe = cast_with_options(&array, &DataType::Decimal32(2, 2), &options);
3039 assert_eq!(
3040 "Invalid argument error: 123456.00 is too large to store in a Decimal32 of precision 2. Max is 0.99",
3041 result_unsafe.unwrap_err().to_string()
3042 );
3043 }
3044
3045 #[test]
3046 fn test_cast_decimal64_to_decimal64() {
3047 let input_type = DataType::Decimal64(17, 3);
3049 let output_type = DataType::Decimal64(17, 4);
3050 assert!(can_cast_types(&input_type, &output_type));
3051 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3052 let array = create_decimal64_array(array, 17, 3).unwrap();
3053 generate_cast_test_case!(
3054 &array,
3055 Decimal64Array,
3056 &output_type,
3057 vec![
3058 Some(11234560_i64),
3059 Some(21234560_i64),
3060 Some(31234560_i64),
3061 None
3062 ]
3063 );
3064 let array = vec![Some(123456), None];
3066 let array = create_decimal64_array(array, 9, 0).unwrap();
3067 let result_safe = cast(&array, &DataType::Decimal64(2, 2));
3068 assert!(result_safe.is_ok());
3069 let options = CastOptions {
3070 safe: false,
3071 ..Default::default()
3072 };
3073
3074 let result_unsafe = cast_with_options(&array, &DataType::Decimal64(2, 2), &options);
3075 assert_eq!(
3076 "Invalid argument error: 123456.00 is too large to store in a Decimal64 of precision 2. Max is 0.99",
3077 result_unsafe.unwrap_err().to_string()
3078 );
3079 }
3080
3081 #[test]
3082 fn test_cast_decimal128_to_decimal128() {
3083 let input_type = DataType::Decimal128(20, 3);
3085 let output_type = DataType::Decimal128(20, 4);
3086 assert!(can_cast_types(&input_type, &output_type));
3087 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3088 let array = create_decimal128_array(array, 20, 3).unwrap();
3089 generate_cast_test_case!(
3090 &array,
3091 Decimal128Array,
3092 &output_type,
3093 vec![
3094 Some(11234560_i128),
3095 Some(21234560_i128),
3096 Some(31234560_i128),
3097 None
3098 ]
3099 );
3100 let array = vec![Some(123456), None];
3102 let array = create_decimal128_array(array, 10, 0).unwrap();
3103 let result_safe = cast(&array, &DataType::Decimal128(2, 2));
3104 assert!(result_safe.is_ok());
3105 let options = CastOptions {
3106 safe: false,
3107 ..Default::default()
3108 };
3109
3110 let result_unsafe = cast_with_options(&array, &DataType::Decimal128(2, 2), &options);
3111 assert_eq!(
3112 "Invalid argument error: 123456.00 is too large to store in a Decimal128 of precision 2. Max is 0.99",
3113 result_unsafe.unwrap_err().to_string()
3114 );
3115 }
3116
3117 #[test]
3118 fn test_cast_decimal32_to_decimal32_dict() {
3119 let p = 9;
3120 let s = 3;
3121 let input_type = DataType::Decimal32(p, s);
3122 let output_type = DataType::Dictionary(
3123 Box::new(DataType::Int32),
3124 Box::new(DataType::Decimal32(p, s)),
3125 );
3126 assert!(can_cast_types(&input_type, &output_type));
3127 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3128 let array = create_decimal32_array(array, p, s).unwrap();
3129 let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3130 assert_eq!(cast_array.data_type(), &output_type);
3131 }
3132
3133 #[test]
3134 fn test_cast_decimal64_to_decimal64_dict() {
3135 let p = 15;
3136 let s = 3;
3137 let input_type = DataType::Decimal64(p, s);
3138 let output_type = DataType::Dictionary(
3139 Box::new(DataType::Int32),
3140 Box::new(DataType::Decimal64(p, s)),
3141 );
3142 assert!(can_cast_types(&input_type, &output_type));
3143 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3144 let array = create_decimal64_array(array, p, s).unwrap();
3145 let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3146 assert_eq!(cast_array.data_type(), &output_type);
3147 }
3148
3149 #[test]
3150 fn test_cast_decimal128_to_decimal128_dict() {
3151 let p = 20;
3152 let s = 3;
3153 let input_type = DataType::Decimal128(p, s);
3154 let output_type = DataType::Dictionary(
3155 Box::new(DataType::Int32),
3156 Box::new(DataType::Decimal128(p, s)),
3157 );
3158 assert!(can_cast_types(&input_type, &output_type));
3159 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3160 let array = create_decimal128_array(array, p, s).unwrap();
3161 let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3162 assert_eq!(cast_array.data_type(), &output_type);
3163 }
3164
3165 #[test]
3166 fn test_cast_decimal256_to_decimal256_dict() {
3167 let p = 20;
3168 let s = 3;
3169 let input_type = DataType::Decimal256(p, s);
3170 let output_type = DataType::Dictionary(
3171 Box::new(DataType::Int32),
3172 Box::new(DataType::Decimal256(p, s)),
3173 );
3174 assert!(can_cast_types(&input_type, &output_type));
3175 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3176 let array = create_decimal128_array(array, p, s).unwrap();
3177 let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3178 assert_eq!(cast_array.data_type(), &output_type);
3179 }
3180
3181 #[test]
3182 fn test_cast_decimal32_to_decimal32_overflow() {
3183 let input_type = DataType::Decimal32(9, 3);
3184 let output_type = DataType::Decimal32(9, 9);
3185 assert!(can_cast_types(&input_type, &output_type));
3186
3187 let array = vec![Some(i32::MAX)];
3188 let array = create_decimal32_array(array, 9, 3).unwrap();
3189 let result = cast_with_options(
3190 &array,
3191 &output_type,
3192 &CastOptions {
3193 safe: false,
3194 format_options: FormatOptions::default(),
3195 },
3196 );
3197 assert_eq!(
3198 "Cast error: Cannot cast to Decimal32(9, 9). Overflowing on 2147483647",
3199 result.unwrap_err().to_string()
3200 );
3201 }
3202
3203 #[test]
3204 fn test_cast_decimal32_to_decimal32_large_scale_reduction() {
3205 let array = vec![Some(-999999999), Some(0), Some(999999999), None];
3206 let array = create_decimal32_array(array, 9, 3).unwrap();
3207
3208 let output_type = DataType::Decimal32(9, -6);
3210 assert!(can_cast_types(array.data_type(), &output_type));
3211 generate_cast_test_case!(
3212 &array,
3213 Decimal32Array,
3214 &output_type,
3215 vec![Some(-1), Some(0), Some(1), None]
3216 );
3217
3218 let output_type = DataType::Decimal32(9, -7);
3220 assert!(can_cast_types(array.data_type(), &output_type));
3221 generate_cast_test_case!(
3222 &array,
3223 Decimal32Array,
3224 &output_type,
3225 vec![Some(0), Some(0), Some(0), None]
3226 );
3227 }
3228
3229 #[test]
3230 fn test_cast_decimal64_to_decimal64_overflow() {
3231 let input_type = DataType::Decimal64(18, 3);
3232 let output_type = DataType::Decimal64(18, 18);
3233 assert!(can_cast_types(&input_type, &output_type));
3234
3235 let array = vec![Some(i64::MAX)];
3236 let array = create_decimal64_array(array, 18, 3).unwrap();
3237 let result = cast_with_options(
3238 &array,
3239 &output_type,
3240 &CastOptions {
3241 safe: false,
3242 format_options: FormatOptions::default(),
3243 },
3244 );
3245 assert_eq!(
3246 "Cast error: Cannot cast to Decimal64(18, 18). Overflowing on 9223372036854775807",
3247 result.unwrap_err().to_string()
3248 );
3249 }
3250
3251 #[test]
3252 fn test_cast_decimal64_to_decimal64_large_scale_reduction() {
3253 let array = vec![
3254 Some(-999999999999999999),
3255 Some(0),
3256 Some(999999999999999999),
3257 None,
3258 ];
3259 let array = create_decimal64_array(array, 18, 3).unwrap();
3260
3261 let output_type = DataType::Decimal64(18, -15);
3263 assert!(can_cast_types(array.data_type(), &output_type));
3264 generate_cast_test_case!(
3265 &array,
3266 Decimal64Array,
3267 &output_type,
3268 vec![Some(-1), Some(0), Some(1), None]
3269 );
3270
3271 let output_type = DataType::Decimal64(18, -16);
3273 assert!(can_cast_types(array.data_type(), &output_type));
3274 generate_cast_test_case!(
3275 &array,
3276 Decimal64Array,
3277 &output_type,
3278 vec![Some(0), Some(0), Some(0), None]
3279 );
3280 }
3281
3282 #[test]
3283 fn test_cast_floating_to_decimals() {
3284 for output_type in [
3285 DataType::Decimal32(9, 3),
3286 DataType::Decimal64(9, 3),
3287 DataType::Decimal128(9, 3),
3288 DataType::Decimal256(9, 3),
3289 ] {
3290 let input_type = DataType::Float64;
3291 assert!(can_cast_types(&input_type, &output_type));
3292
3293 let array = vec![Some(1.1_f64)];
3294 let array = PrimitiveArray::<Float64Type>::from_iter(array);
3295 let result = cast_with_options(
3296 &array,
3297 &output_type,
3298 &CastOptions {
3299 safe: false,
3300 format_options: FormatOptions::default(),
3301 },
3302 );
3303 assert!(
3304 result.is_ok(),
3305 "Failed to cast to {output_type} with: {}",
3306 result.unwrap_err()
3307 );
3308 }
3309 }
3310
3311 #[test]
3312 fn test_cast_decimal128_to_decimal128_overflow() {
3313 let input_type = DataType::Decimal128(38, 3);
3314 let output_type = DataType::Decimal128(38, 38);
3315 assert!(can_cast_types(&input_type, &output_type));
3316
3317 let array = vec![Some(i128::MAX)];
3318 let array = create_decimal128_array(array, 38, 3).unwrap();
3319 let result = cast_with_options(
3320 &array,
3321 &output_type,
3322 &CastOptions {
3323 safe: false,
3324 format_options: FormatOptions::default(),
3325 },
3326 );
3327 assert_eq!(
3328 "Cast error: Cannot cast to Decimal128(38, 38). Overflowing on 170141183460469231731687303715884105727",
3329 result.unwrap_err().to_string()
3330 );
3331 }
3332
3333 #[test]
3334 fn test_cast_decimal128_to_decimal256_overflow() {
3335 let input_type = DataType::Decimal128(38, 3);
3336 let output_type = DataType::Decimal256(76, 76);
3337 assert!(can_cast_types(&input_type, &output_type));
3338
3339 let array = vec![Some(i128::MAX)];
3340 let array = create_decimal128_array(array, 38, 3).unwrap();
3341 let result = cast_with_options(
3342 &array,
3343 &output_type,
3344 &CastOptions {
3345 safe: false,
3346 format_options: FormatOptions::default(),
3347 },
3348 );
3349 assert_eq!(
3350 "Cast error: Cannot cast to Decimal256(76, 76). Overflowing on 170141183460469231731687303715884105727",
3351 result.unwrap_err().to_string()
3352 );
3353 }
3354
3355 #[test]
3356 fn test_cast_decimal32_to_decimal256() {
3357 let input_type = DataType::Decimal32(8, 3);
3358 let output_type = DataType::Decimal256(20, 4);
3359 assert!(can_cast_types(&input_type, &output_type));
3360 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3361 let array = create_decimal32_array(array, 8, 3).unwrap();
3362 generate_cast_test_case!(
3363 &array,
3364 Decimal256Array,
3365 &output_type,
3366 vec![
3367 Some(i256::from_i128(11234560_i128)),
3368 Some(i256::from_i128(21234560_i128)),
3369 Some(i256::from_i128(31234560_i128)),
3370 None
3371 ]
3372 );
3373 }
3374 #[test]
3375 fn test_cast_decimal64_to_decimal256() {
3376 let input_type = DataType::Decimal64(12, 3);
3377 let output_type = DataType::Decimal256(20, 4);
3378 assert!(can_cast_types(&input_type, &output_type));
3379 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3380 let array = create_decimal64_array(array, 12, 3).unwrap();
3381 generate_cast_test_case!(
3382 &array,
3383 Decimal256Array,
3384 &output_type,
3385 vec![
3386 Some(i256::from_i128(11234560_i128)),
3387 Some(i256::from_i128(21234560_i128)),
3388 Some(i256::from_i128(31234560_i128)),
3389 None
3390 ]
3391 );
3392 }
3393 #[test]
3394 fn test_cast_decimal128_to_decimal256() {
3395 let input_type = DataType::Decimal128(20, 3);
3396 let output_type = DataType::Decimal256(20, 4);
3397 assert!(can_cast_types(&input_type, &output_type));
3398 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3399 let array = create_decimal128_array(array, 20, 3).unwrap();
3400 generate_cast_test_case!(
3401 &array,
3402 Decimal256Array,
3403 &output_type,
3404 vec![
3405 Some(i256::from_i128(11234560_i128)),
3406 Some(i256::from_i128(21234560_i128)),
3407 Some(i256::from_i128(31234560_i128)),
3408 None
3409 ]
3410 );
3411 }
3412
3413 #[test]
3414 fn test_cast_decimal256_to_decimal128_overflow() {
3415 let input_type = DataType::Decimal256(76, 5);
3416 let output_type = DataType::Decimal128(38, 7);
3417 assert!(can_cast_types(&input_type, &output_type));
3418 let array = vec![Some(i256::from_i128(i128::MAX))];
3419 let array = create_decimal256_array(array, 76, 5).unwrap();
3420 let result = cast_with_options(
3421 &array,
3422 &output_type,
3423 &CastOptions {
3424 safe: false,
3425 format_options: FormatOptions::default(),
3426 },
3427 );
3428 assert_eq!(
3429 "Cast error: Cannot cast to Decimal128(38, 7). Overflowing on 170141183460469231731687303715884105727",
3430 result.unwrap_err().to_string()
3431 );
3432 }
3433
3434 #[test]
3435 fn test_cast_decimal256_to_decimal256_overflow() {
3436 let input_type = DataType::Decimal256(76, 5);
3437 let output_type = DataType::Decimal256(76, 55);
3438 assert!(can_cast_types(&input_type, &output_type));
3439 let array = vec![Some(i256::from_i128(i128::MAX))];
3440 let array = create_decimal256_array(array, 76, 5).unwrap();
3441 let result = cast_with_options(
3442 &array,
3443 &output_type,
3444 &CastOptions {
3445 safe: false,
3446 format_options: FormatOptions::default(),
3447 },
3448 );
3449 assert_eq!(
3450 "Cast error: Cannot cast to Decimal256(76, 55). Overflowing on 170141183460469231731687303715884105727",
3451 result.unwrap_err().to_string()
3452 );
3453 }
3454
3455 #[test]
3456 fn test_cast_decimal256_to_decimal128() {
3457 let input_type = DataType::Decimal256(20, 3);
3458 let output_type = DataType::Decimal128(20, 4);
3459 assert!(can_cast_types(&input_type, &output_type));
3460 let array = vec![
3461 Some(i256::from_i128(1123456)),
3462 Some(i256::from_i128(2123456)),
3463 Some(i256::from_i128(3123456)),
3464 None,
3465 ];
3466 let array = create_decimal256_array(array, 20, 3).unwrap();
3467 generate_cast_test_case!(
3468 &array,
3469 Decimal128Array,
3470 &output_type,
3471 vec![
3472 Some(11234560_i128),
3473 Some(21234560_i128),
3474 Some(31234560_i128),
3475 None
3476 ]
3477 );
3478 }
3479
3480 #[test]
3481 fn test_cast_decimal256_to_decimal256() {
3482 let input_type = DataType::Decimal256(20, 3);
3483 let output_type = DataType::Decimal256(20, 4);
3484 assert!(can_cast_types(&input_type, &output_type));
3485 let array = vec![
3486 Some(i256::from_i128(1123456)),
3487 Some(i256::from_i128(2123456)),
3488 Some(i256::from_i128(3123456)),
3489 None,
3490 ];
3491 let array = create_decimal256_array(array, 20, 3).unwrap();
3492 generate_cast_test_case!(
3493 &array,
3494 Decimal256Array,
3495 &output_type,
3496 vec![
3497 Some(i256::from_i128(11234560_i128)),
3498 Some(i256::from_i128(21234560_i128)),
3499 Some(i256::from_i128(31234560_i128)),
3500 None
3501 ]
3502 );
3503 }
3504
3505 fn generate_decimal_to_numeric_cast_test_case<T>(array: &PrimitiveArray<T>)
3506 where
3507 T: ArrowPrimitiveType + DecimalType,
3508 {
3509 generate_cast_test_case!(
3511 array,
3512 UInt8Array,
3513 &DataType::UInt8,
3514 vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
3515 );
3516 generate_cast_test_case!(
3518 array,
3519 UInt16Array,
3520 &DataType::UInt16,
3521 vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
3522 );
3523 generate_cast_test_case!(
3525 array,
3526 UInt32Array,
3527 &DataType::UInt32,
3528 vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
3529 );
3530 generate_cast_test_case!(
3532 array,
3533 UInt64Array,
3534 &DataType::UInt64,
3535 vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
3536 );
3537 generate_cast_test_case!(
3539 array,
3540 Int8Array,
3541 &DataType::Int8,
3542 vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
3543 );
3544 generate_cast_test_case!(
3546 array,
3547 Int16Array,
3548 &DataType::Int16,
3549 vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
3550 );
3551 generate_cast_test_case!(
3553 array,
3554 Int32Array,
3555 &DataType::Int32,
3556 vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
3557 );
3558 generate_cast_test_case!(
3560 array,
3561 Int64Array,
3562 &DataType::Int64,
3563 vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
3564 );
3565 generate_cast_test_case!(
3567 array,
3568 Float32Array,
3569 &DataType::Float32,
3570 vec![
3571 Some(1.25_f32),
3572 Some(2.25_f32),
3573 Some(3.25_f32),
3574 None,
3575 Some(5.25_f32)
3576 ]
3577 );
3578 generate_cast_test_case!(
3580 array,
3581 Float64Array,
3582 &DataType::Float64,
3583 vec![
3584 Some(1.25_f64),
3585 Some(2.25_f64),
3586 Some(3.25_f64),
3587 None,
3588 Some(5.25_f64)
3589 ]
3590 );
3591 }
3592
3593 #[test]
3594 fn test_cast_decimal32_to_numeric() {
3595 let value_array: Vec<Option<i32>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3596 let array = create_decimal32_array(value_array, 8, 2).unwrap();
3597
3598 generate_decimal_to_numeric_cast_test_case(&array);
3599 }
3600
3601 #[test]
3602 fn test_cast_decimal64_to_numeric() {
3603 let value_array: Vec<Option<i64>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3604 let array = create_decimal64_array(value_array, 8, 2).unwrap();
3605
3606 generate_decimal_to_numeric_cast_test_case(&array);
3607 }
3608
3609 #[test]
3610 fn test_cast_decimal128_to_numeric() {
3611 let value_array: Vec<Option<i128>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3612 let array = create_decimal128_array(value_array, 38, 2).unwrap();
3613
3614 generate_decimal_to_numeric_cast_test_case(&array);
3615
3616 let value_array: Vec<Option<i128>> = vec![Some(51300)];
3618 let array = create_decimal128_array(value_array, 38, 2).unwrap();
3619 let casted_array = cast_with_options(
3620 &array,
3621 &DataType::UInt8,
3622 &CastOptions {
3623 safe: false,
3624 format_options: FormatOptions::default(),
3625 },
3626 );
3627 assert_eq!(
3628 "Cast error: value of 513 is out of range UInt8".to_string(),
3629 casted_array.unwrap_err().to_string()
3630 );
3631
3632 let casted_array = cast_with_options(
3633 &array,
3634 &DataType::UInt8,
3635 &CastOptions {
3636 safe: true,
3637 format_options: FormatOptions::default(),
3638 },
3639 );
3640 assert!(casted_array.is_ok());
3641 assert!(casted_array.unwrap().is_null(0));
3642
3643 let value_array: Vec<Option<i128>> = vec![Some(24400)];
3645 let array = create_decimal128_array(value_array, 38, 2).unwrap();
3646 let casted_array = cast_with_options(
3647 &array,
3648 &DataType::Int8,
3649 &CastOptions {
3650 safe: false,
3651 format_options: FormatOptions::default(),
3652 },
3653 );
3654 assert_eq!(
3655 "Cast error: value of 244 is out of range Int8".to_string(),
3656 casted_array.unwrap_err().to_string()
3657 );
3658
3659 let casted_array = cast_with_options(
3660 &array,
3661 &DataType::Int8,
3662 &CastOptions {
3663 safe: true,
3664 format_options: FormatOptions::default(),
3665 },
3666 );
3667 assert!(casted_array.is_ok());
3668 assert!(casted_array.unwrap().is_null(0));
3669
3670 let value_array: Vec<Option<i128>> = vec![
3674 Some(125),
3675 Some(225),
3676 Some(325),
3677 None,
3678 Some(525),
3679 Some(112345678),
3680 Some(112345679),
3681 ];
3682 let array = create_decimal128_array(value_array, 38, 2).unwrap();
3683 generate_cast_test_case!(
3684 &array,
3685 Float32Array,
3686 &DataType::Float32,
3687 vec![
3688 Some(1.25_f32),
3689 Some(2.25_f32),
3690 Some(3.25_f32),
3691 None,
3692 Some(5.25_f32),
3693 Some(1_123_456.7_f32),
3694 Some(1_123_456.7_f32)
3695 ]
3696 );
3697
3698 let value_array: Vec<Option<i128>> = vec![
3701 Some(125),
3702 Some(225),
3703 Some(325),
3704 None,
3705 Some(525),
3706 Some(112345678901234568),
3707 Some(112345678901234560),
3708 ];
3709 let array = create_decimal128_array(value_array, 38, 2).unwrap();
3710 generate_cast_test_case!(
3711 &array,
3712 Float64Array,
3713 &DataType::Float64,
3714 vec![
3715 Some(1.25_f64),
3716 Some(2.25_f64),
3717 Some(3.25_f64),
3718 None,
3719 Some(5.25_f64),
3720 Some(1_123_456_789_012_345.6_f64),
3721 Some(1_123_456_789_012_345.6_f64),
3722 ]
3723 );
3724 }
3725
3726 #[test]
3727 fn test_cast_decimal256_to_numeric() {
3728 let value_array: Vec<Option<i256>> = vec![
3729 Some(i256::from_i128(125)),
3730 Some(i256::from_i128(225)),
3731 Some(i256::from_i128(325)),
3732 None,
3733 Some(i256::from_i128(525)),
3734 ];
3735 let array = create_decimal256_array(value_array, 38, 2).unwrap();
3736 generate_cast_test_case!(
3738 &array,
3739 UInt8Array,
3740 &DataType::UInt8,
3741 vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
3742 );
3743 generate_cast_test_case!(
3745 &array,
3746 UInt16Array,
3747 &DataType::UInt16,
3748 vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
3749 );
3750 generate_cast_test_case!(
3752 &array,
3753 UInt32Array,
3754 &DataType::UInt32,
3755 vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
3756 );
3757 generate_cast_test_case!(
3759 &array,
3760 UInt64Array,
3761 &DataType::UInt64,
3762 vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
3763 );
3764 generate_cast_test_case!(
3766 &array,
3767 Int8Array,
3768 &DataType::Int8,
3769 vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
3770 );
3771 generate_cast_test_case!(
3773 &array,
3774 Int16Array,
3775 &DataType::Int16,
3776 vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
3777 );
3778 generate_cast_test_case!(
3780 &array,
3781 Int32Array,
3782 &DataType::Int32,
3783 vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
3784 );
3785 generate_cast_test_case!(
3787 &array,
3788 Int64Array,
3789 &DataType::Int64,
3790 vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
3791 );
3792 generate_cast_test_case!(
3794 &array,
3795 Float32Array,
3796 &DataType::Float32,
3797 vec![
3798 Some(1.25_f32),
3799 Some(2.25_f32),
3800 Some(3.25_f32),
3801 None,
3802 Some(5.25_f32)
3803 ]
3804 );
3805 generate_cast_test_case!(
3807 &array,
3808 Float64Array,
3809 &DataType::Float64,
3810 vec![
3811 Some(1.25_f64),
3812 Some(2.25_f64),
3813 Some(3.25_f64),
3814 None,
3815 Some(5.25_f64)
3816 ]
3817 );
3818
3819 let value_array: Vec<Option<i256>> = vec![Some(i256::from_i128(24400))];
3821 let array = create_decimal256_array(value_array, 38, 2).unwrap();
3822 let casted_array = cast_with_options(
3823 &array,
3824 &DataType::Int8,
3825 &CastOptions {
3826 safe: false,
3827 format_options: FormatOptions::default(),
3828 },
3829 );
3830 assert_eq!(
3831 "Cast error: value of 244 is out of range Int8".to_string(),
3832 casted_array.unwrap_err().to_string()
3833 );
3834
3835 let casted_array = cast_with_options(
3836 &array,
3837 &DataType::Int8,
3838 &CastOptions {
3839 safe: true,
3840 format_options: FormatOptions::default(),
3841 },
3842 );
3843 assert!(casted_array.is_ok());
3844 assert!(casted_array.unwrap().is_null(0));
3845
3846 let value_array: Vec<Option<i256>> = vec![
3850 Some(i256::from_i128(125)),
3851 Some(i256::from_i128(225)),
3852 Some(i256::from_i128(325)),
3853 None,
3854 Some(i256::from_i128(525)),
3855 Some(i256::from_i128(112345678)),
3856 Some(i256::from_i128(112345679)),
3857 ];
3858 let array = create_decimal256_array(value_array, 76, 2).unwrap();
3859 generate_cast_test_case!(
3860 &array,
3861 Float32Array,
3862 &DataType::Float32,
3863 vec![
3864 Some(1.25_f32),
3865 Some(2.25_f32),
3866 Some(3.25_f32),
3867 None,
3868 Some(5.25_f32),
3869 Some(1_123_456.7_f32),
3870 Some(1_123_456.7_f32)
3871 ]
3872 );
3873
3874 let value_array: Vec<Option<i256>> = vec![
3877 Some(i256::from_i128(125)),
3878 Some(i256::from_i128(225)),
3879 Some(i256::from_i128(325)),
3880 None,
3881 Some(i256::from_i128(525)),
3882 Some(i256::from_i128(112345678901234568)),
3883 Some(i256::from_i128(112345678901234560)),
3884 ];
3885 let array = create_decimal256_array(value_array, 76, 2).unwrap();
3886 generate_cast_test_case!(
3887 &array,
3888 Float64Array,
3889 &DataType::Float64,
3890 vec![
3891 Some(1.25_f64),
3892 Some(2.25_f64),
3893 Some(3.25_f64),
3894 None,
3895 Some(5.25_f64),
3896 Some(1_123_456_789_012_345.6_f64),
3897 Some(1_123_456_789_012_345.6_f64),
3898 ]
3899 );
3900 }
3901
3902 #[test]
3903 fn test_cast_decimal_to_numeric_negative_scale() {
3904 let value_array: Vec<Option<i256>> = vec![
3905 Some(i256::from_i128(125)),
3906 Some(i256::from_i128(225)),
3907 Some(i256::from_i128(325)),
3908 None,
3909 Some(i256::from_i128(525)),
3910 ];
3911 let array = create_decimal256_array(value_array, 38, -1).unwrap();
3912
3913 generate_cast_test_case!(
3914 &array,
3915 Int64Array,
3916 &DataType::Int64,
3917 vec![Some(1_250), Some(2_250), Some(3_250), None, Some(5_250)]
3918 );
3919
3920 let value_array: Vec<Option<i32>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3921 let array = create_decimal32_array(value_array, 8, -2).unwrap();
3922 generate_cast_test_case!(
3923 &array,
3924 Int64Array,
3925 &DataType::Int64,
3926 vec![Some(12_500), Some(22_500), Some(32_500), None, Some(52_500)]
3927 );
3928
3929 let value_array: Vec<Option<i32>> = vec![Some(2), Some(1), None];
3930 let array = create_decimal32_array(value_array, 9, -9).unwrap();
3931 generate_cast_test_case!(
3932 &array,
3933 Int64Array,
3934 &DataType::Int64,
3935 vec![Some(2_000_000_000), Some(1_000_000_000), None]
3936 );
3937
3938 let value_array: Vec<Option<i64>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3939 let array = create_decimal64_array(value_array, 18, -3).unwrap();
3940 generate_cast_test_case!(
3941 &array,
3942 Int64Array,
3943 &DataType::Int64,
3944 vec![
3945 Some(125_000),
3946 Some(225_000),
3947 Some(325_000),
3948 None,
3949 Some(525_000)
3950 ]
3951 );
3952
3953 let value_array: Vec<Option<i64>> = vec![Some(12), Some(34), None];
3954 let array = create_decimal64_array(value_array, 18, -10).unwrap();
3955 generate_cast_test_case!(
3956 &array,
3957 Int64Array,
3958 &DataType::Int64,
3959 vec![Some(120_000_000_000), Some(340_000_000_000), None]
3960 );
3961
3962 let value_array: Vec<Option<i128>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3963 let array = create_decimal128_array(value_array, 38, -4).unwrap();
3964 generate_cast_test_case!(
3965 &array,
3966 Int64Array,
3967 &DataType::Int64,
3968 vec![
3969 Some(1_250_000),
3970 Some(2_250_000),
3971 Some(3_250_000),
3972 None,
3973 Some(5_250_000)
3974 ]
3975 );
3976
3977 let value_array: Vec<Option<i128>> = vec![Some(9), Some(1), None];
3978 let array = create_decimal128_array(value_array, 38, -18).unwrap();
3979 generate_cast_test_case!(
3980 &array,
3981 Int64Array,
3982 &DataType::Int64,
3983 vec![
3984 Some(9_000_000_000_000_000_000),
3985 Some(1_000_000_000_000_000_000),
3986 None
3987 ]
3988 );
3989
3990 let array = create_decimal32_array(vec![Some(999_999_999)], 9, -1).unwrap();
3991 let casted_array = cast_with_options(
3992 &array,
3993 &DataType::Int64,
3994 &CastOptions {
3995 safe: false,
3996 format_options: FormatOptions::default(),
3997 },
3998 );
3999 assert_eq!(
4000 "Arithmetic overflow: Overflow happened on: 999999999 * 10".to_string(),
4001 casted_array.unwrap_err().to_string()
4002 );
4003
4004 let casted_array = cast_with_options(
4005 &array,
4006 &DataType::Int64,
4007 &CastOptions {
4008 safe: true,
4009 format_options: FormatOptions::default(),
4010 },
4011 );
4012 assert!(casted_array.is_ok());
4013 assert!(casted_array.unwrap().is_null(0));
4014
4015 let array = create_decimal64_array(vec![Some(13)], 18, -1).unwrap();
4016 let casted_array = cast_with_options(
4017 &array,
4018 &DataType::Int8,
4019 &CastOptions {
4020 safe: false,
4021 format_options: FormatOptions::default(),
4022 },
4023 );
4024 assert_eq!(
4025 "Cast error: value of 130 is out of range Int8".to_string(),
4026 casted_array.unwrap_err().to_string()
4027 );
4028
4029 let casted_array = cast_with_options(
4030 &array,
4031 &DataType::Int8,
4032 &CastOptions {
4033 safe: true,
4034 format_options: FormatOptions::default(),
4035 },
4036 );
4037 assert!(casted_array.is_ok());
4038 assert!(casted_array.unwrap().is_null(0));
4039 }
4040
4041 #[test]
4042 fn test_cast_numeric_to_decimal128() {
4043 let decimal_type = DataType::Decimal128(38, 6);
4044 let input_datas = vec![
4046 Arc::new(UInt8Array::from(vec![
4047 Some(1),
4048 Some(2),
4049 Some(3),
4050 None,
4051 Some(5),
4052 ])) as ArrayRef, Arc::new(UInt16Array::from(vec![
4054 Some(1),
4055 Some(2),
4056 Some(3),
4057 None,
4058 Some(5),
4059 ])) as ArrayRef, Arc::new(UInt32Array::from(vec![
4061 Some(1),
4062 Some(2),
4063 Some(3),
4064 None,
4065 Some(5),
4066 ])) as ArrayRef, Arc::new(UInt64Array::from(vec![
4068 Some(1),
4069 Some(2),
4070 Some(3),
4071 None,
4072 Some(5),
4073 ])) as ArrayRef, ];
4075
4076 for array in input_datas {
4077 generate_cast_test_case!(
4078 &array,
4079 Decimal128Array,
4080 &decimal_type,
4081 vec![
4082 Some(1000000_i128),
4083 Some(2000000_i128),
4084 Some(3000000_i128),
4085 None,
4086 Some(5000000_i128)
4087 ]
4088 );
4089 }
4090
4091 let input_datas = vec![
4093 Arc::new(Int8Array::from(vec![
4094 Some(1),
4095 Some(2),
4096 Some(3),
4097 None,
4098 Some(5),
4099 ])) as ArrayRef, Arc::new(Int16Array::from(vec![
4101 Some(1),
4102 Some(2),
4103 Some(3),
4104 None,
4105 Some(5),
4106 ])) as ArrayRef, Arc::new(Int32Array::from(vec![
4108 Some(1),
4109 Some(2),
4110 Some(3),
4111 None,
4112 Some(5),
4113 ])) as ArrayRef, Arc::new(Int64Array::from(vec![
4115 Some(1),
4116 Some(2),
4117 Some(3),
4118 None,
4119 Some(5),
4120 ])) as ArrayRef, ];
4122 for array in input_datas {
4123 generate_cast_test_case!(
4124 &array,
4125 Decimal128Array,
4126 &decimal_type,
4127 vec![
4128 Some(1000000_i128),
4129 Some(2000000_i128),
4130 Some(3000000_i128),
4131 None,
4132 Some(5000000_i128)
4133 ]
4134 );
4135 }
4136
4137 let array = UInt8Array::from(vec![1, 2, 3, 4, 100]);
4140 let casted_array = cast(&array, &DataType::Decimal128(3, 1));
4141 assert!(casted_array.is_ok());
4142 let array = casted_array.unwrap();
4143 let array: &Decimal128Array = array.as_primitive();
4144 assert!(array.is_null(4));
4145
4146 let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
4149 let casted_array = cast(&array, &DataType::Decimal128(3, 1));
4150 assert!(casted_array.is_ok());
4151 let array = casted_array.unwrap();
4152 let array: &Decimal128Array = array.as_primitive();
4153 assert!(array.is_null(4));
4154
4155 let array = Float32Array::from(vec![
4157 Some(1.1),
4158 Some(2.2),
4159 Some(4.4),
4160 None,
4161 Some(1.123_456_4), Some(1.123_456_7), ]);
4164 let array = Arc::new(array) as ArrayRef;
4165 generate_cast_test_case!(
4166 &array,
4167 Decimal128Array,
4168 &decimal_type,
4169 vec![
4170 Some(1100000_i128),
4171 Some(2200000_i128),
4172 Some(4400000_i128),
4173 None,
4174 Some(1123456_i128), Some(1123457_i128), ]
4177 );
4178
4179 let array = Float64Array::from(vec![
4181 Some(1.1),
4182 Some(2.2),
4183 Some(4.4),
4184 None,
4185 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), ]);
4190 generate_cast_test_case!(
4191 &array,
4192 Decimal128Array,
4193 &decimal_type,
4194 vec![
4195 Some(1100000_i128),
4196 Some(2200000_i128),
4197 Some(4400000_i128),
4198 None,
4199 Some(1123456_i128), Some(1123457_i128), Some(1123456_i128), Some(1123457_i128), ]
4204 );
4205 }
4206
4207 #[test]
4208 fn test_cast_numeric_to_decimal256() {
4209 let decimal_type = DataType::Decimal256(76, 6);
4210 let input_datas = vec![
4212 Arc::new(UInt8Array::from(vec![
4213 Some(1),
4214 Some(2),
4215 Some(3),
4216 None,
4217 Some(5),
4218 ])) as ArrayRef, Arc::new(UInt16Array::from(vec![
4220 Some(1),
4221 Some(2),
4222 Some(3),
4223 None,
4224 Some(5),
4225 ])) as ArrayRef, Arc::new(UInt32Array::from(vec![
4227 Some(1),
4228 Some(2),
4229 Some(3),
4230 None,
4231 Some(5),
4232 ])) as ArrayRef, Arc::new(UInt64Array::from(vec![
4234 Some(1),
4235 Some(2),
4236 Some(3),
4237 None,
4238 Some(5),
4239 ])) as ArrayRef, ];
4241
4242 for array in input_datas {
4243 generate_cast_test_case!(
4244 &array,
4245 Decimal256Array,
4246 &decimal_type,
4247 vec![
4248 Some(i256::from_i128(1000000_i128)),
4249 Some(i256::from_i128(2000000_i128)),
4250 Some(i256::from_i128(3000000_i128)),
4251 None,
4252 Some(i256::from_i128(5000000_i128))
4253 ]
4254 );
4255 }
4256
4257 let input_datas = vec![
4259 Arc::new(Int8Array::from(vec![
4260 Some(1),
4261 Some(2),
4262 Some(3),
4263 None,
4264 Some(5),
4265 ])) as ArrayRef, Arc::new(Int16Array::from(vec![
4267 Some(1),
4268 Some(2),
4269 Some(3),
4270 None,
4271 Some(5),
4272 ])) as ArrayRef, Arc::new(Int32Array::from(vec![
4274 Some(1),
4275 Some(2),
4276 Some(3),
4277 None,
4278 Some(5),
4279 ])) as ArrayRef, Arc::new(Int64Array::from(vec![
4281 Some(1),
4282 Some(2),
4283 Some(3),
4284 None,
4285 Some(5),
4286 ])) as ArrayRef, ];
4288 for array in input_datas {
4289 generate_cast_test_case!(
4290 &array,
4291 Decimal256Array,
4292 &decimal_type,
4293 vec![
4294 Some(i256::from_i128(1000000_i128)),
4295 Some(i256::from_i128(2000000_i128)),
4296 Some(i256::from_i128(3000000_i128)),
4297 None,
4298 Some(i256::from_i128(5000000_i128))
4299 ]
4300 );
4301 }
4302
4303 let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
4306 let array = Arc::new(array) as ArrayRef;
4307 let casted_array = cast(&array, &DataType::Decimal256(3, 1));
4308 assert!(casted_array.is_ok());
4309 let array = casted_array.unwrap();
4310 let array: &Decimal256Array = array.as_primitive();
4311 assert!(array.is_null(4));
4312
4313 let array = Float32Array::from(vec![
4315 Some(1.1),
4316 Some(2.2),
4317 Some(4.4),
4318 None,
4319 Some(1.123_456_4), Some(1.123_456_7), ]);
4322 generate_cast_test_case!(
4323 &array,
4324 Decimal256Array,
4325 &decimal_type,
4326 vec![
4327 Some(i256::from_i128(1100000_i128)),
4328 Some(i256::from_i128(2200000_i128)),
4329 Some(i256::from_i128(4400000_i128)),
4330 None,
4331 Some(i256::from_i128(1123456_i128)), Some(i256::from_i128(1123457_i128)), ]
4334 );
4335
4336 let array = Float64Array::from(vec![
4338 Some(1.1),
4339 Some(2.2),
4340 Some(4.4),
4341 None,
4342 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), ]);
4347 generate_cast_test_case!(
4348 &array,
4349 Decimal256Array,
4350 &decimal_type,
4351 vec![
4352 Some(i256::from_i128(1100000_i128)),
4353 Some(i256::from_i128(2200000_i128)),
4354 Some(i256::from_i128(4400000_i128)),
4355 None,
4356 Some(i256::from_i128(1123456_i128)), Some(i256::from_i128(1123457_i128)), Some(i256::from_i128(1123456_i128)), Some(i256::from_i128(1123457_i128)), ]
4361 );
4362 }
4363
4364 #[test]
4365 fn test_cast_i32_to_f64() {
4366 let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4367 let b = cast(&array, &DataType::Float64).unwrap();
4368 let c = b.as_primitive::<Float64Type>();
4369 assert_eq!(5.0, c.value(0));
4370 assert_eq!(6.0, c.value(1));
4371 assert_eq!(7.0, c.value(2));
4372 assert_eq!(8.0, c.value(3));
4373 assert_eq!(9.0, c.value(4));
4374 }
4375
4376 #[test]
4377 fn test_cast_i32_to_u8() {
4378 let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4379 let b = cast(&array, &DataType::UInt8).unwrap();
4380 let c = b.as_primitive::<UInt8Type>();
4381 assert!(!c.is_valid(0));
4382 assert_eq!(6, c.value(1));
4383 assert!(!c.is_valid(2));
4384 assert_eq!(8, c.value(3));
4385 assert!(!c.is_valid(4));
4387 }
4388
4389 #[test]
4390 #[should_panic(expected = "Can't cast value -5 to type UInt8")]
4391 fn test_cast_int32_to_u8_with_error() {
4392 let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4393 let cast_option = CastOptions {
4395 safe: false,
4396 format_options: FormatOptions::default(),
4397 };
4398 let result = cast_with_options(&array, &DataType::UInt8, &cast_option);
4399 assert!(result.is_err());
4400 result.unwrap();
4401 }
4402
4403 #[test]
4404 fn test_cast_i32_to_u8_sliced() {
4405 let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4406 assert_eq!(0, array.offset());
4407 let array = array.slice(2, 3);
4408 let b = cast(&array, &DataType::UInt8).unwrap();
4409 assert_eq!(3, b.len());
4410 let c = b.as_primitive::<UInt8Type>();
4411 assert!(!c.is_valid(0));
4412 assert_eq!(8, c.value(1));
4413 assert!(!c.is_valid(2));
4415 }
4416
4417 #[test]
4418 fn test_cast_i32_to_i32() {
4419 let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4420 let b = cast(&array, &DataType::Int32).unwrap();
4421 let c = b.as_primitive::<Int32Type>();
4422 assert_eq!(5, c.value(0));
4423 assert_eq!(6, c.value(1));
4424 assert_eq!(7, c.value(2));
4425 assert_eq!(8, c.value(3));
4426 assert_eq!(9, c.value(4));
4427 }
4428
4429 #[test]
4430 fn test_cast_i32_to_list_i32() {
4431 let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4432 let b = cast(
4433 &array,
4434 &DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
4435 )
4436 .unwrap();
4437 assert_eq!(5, b.len());
4438 let arr = b.as_list::<i32>();
4439 assert_eq!(&[0, 1, 2, 3, 4, 5], arr.value_offsets());
4440 assert_eq!(1, arr.value_length(0));
4441 assert_eq!(1, arr.value_length(1));
4442 assert_eq!(1, arr.value_length(2));
4443 assert_eq!(1, arr.value_length(3));
4444 assert_eq!(1, arr.value_length(4));
4445 let c = arr.values().as_primitive::<Int32Type>();
4446 assert_eq!(5, c.value(0));
4447 assert_eq!(6, c.value(1));
4448 assert_eq!(7, c.value(2));
4449 assert_eq!(8, c.value(3));
4450 assert_eq!(9, c.value(4));
4451 }
4452
4453 #[test]
4454 fn test_cast_i32_to_list_i32_nullable() {
4455 let array = Int32Array::from(vec![Some(5), None, Some(7), Some(8), Some(9)]);
4456 let b = cast(
4457 &array,
4458 &DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
4459 )
4460 .unwrap();
4461 assert_eq!(5, b.len());
4462 assert_eq!(0, b.null_count());
4463 let arr = b.as_list::<i32>();
4464 assert_eq!(&[0, 1, 2, 3, 4, 5], arr.value_offsets());
4465 assert_eq!(1, arr.value_length(0));
4466 assert_eq!(1, arr.value_length(1));
4467 assert_eq!(1, arr.value_length(2));
4468 assert_eq!(1, arr.value_length(3));
4469 assert_eq!(1, arr.value_length(4));
4470
4471 let c = arr.values().as_primitive::<Int32Type>();
4472 assert_eq!(1, c.null_count());
4473 assert_eq!(5, c.value(0));
4474 assert!(!c.is_valid(1));
4475 assert_eq!(7, c.value(2));
4476 assert_eq!(8, c.value(3));
4477 assert_eq!(9, c.value(4));
4478 }
4479
4480 #[test]
4481 fn test_cast_i32_to_list_f64_nullable_sliced() {
4482 let array = Int32Array::from(vec![Some(5), None, Some(7), Some(8), None, Some(10)]);
4483 let array = array.slice(2, 4);
4484 let b = cast(
4485 &array,
4486 &DataType::List(Arc::new(Field::new_list_field(DataType::Float64, true))),
4487 )
4488 .unwrap();
4489 assert_eq!(4, b.len());
4490 assert_eq!(0, b.null_count());
4491 let arr = b.as_list::<i32>();
4492 assert_eq!(&[0, 1, 2, 3, 4], arr.value_offsets());
4493 assert_eq!(1, arr.value_length(0));
4494 assert_eq!(1, arr.value_length(1));
4495 assert_eq!(1, arr.value_length(2));
4496 assert_eq!(1, arr.value_length(3));
4497 let c = arr.values().as_primitive::<Float64Type>();
4498 assert_eq!(1, c.null_count());
4499 assert_eq!(7.0, c.value(0));
4500 assert_eq!(8.0, c.value(1));
4501 assert!(!c.is_valid(2));
4502 assert_eq!(10.0, c.value(3));
4503 }
4504
4505 #[test]
4506 fn test_cast_int_to_utf8view() {
4507 let inputs = vec![
4508 Arc::new(Int8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4509 Arc::new(Int16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4510 Arc::new(Int32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4511 Arc::new(Int64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4512 Arc::new(UInt8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4513 Arc::new(UInt16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4514 Arc::new(UInt32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4515 Arc::new(UInt64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4516 ];
4517 let expected: ArrayRef = Arc::new(StringViewArray::from(vec![
4518 None,
4519 Some("8"),
4520 Some("9"),
4521 Some("10"),
4522 ]));
4523
4524 for array in inputs {
4525 assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
4526 let arr = cast(&array, &DataType::Utf8View).unwrap();
4527 assert_eq!(expected.as_ref(), arr.as_ref());
4528 }
4529 }
4530
4531 #[test]
4532 fn test_cast_float_to_utf8view() {
4533 let inputs = vec![
4534 Arc::new(Float16Array::from(vec![
4535 Some(f16::from_f64(1.5)),
4536 Some(f16::from_f64(2.5)),
4537 None,
4538 ])) as ArrayRef,
4539 Arc::new(Float32Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
4540 Arc::new(Float64Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
4541 ];
4542
4543 let expected: ArrayRef =
4544 Arc::new(StringViewArray::from(vec![Some("1.5"), Some("2.5"), None]));
4545
4546 for array in inputs {
4547 assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
4548 let arr = cast(&array, &DataType::Utf8View).unwrap();
4549 assert_eq!(expected.as_ref(), arr.as_ref());
4550 }
4551 }
4552
4553 #[test]
4554 fn test_cast_utf8_to_i32() {
4555 let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4556 let b = cast(&array, &DataType::Int32).unwrap();
4557 let c = b.as_primitive::<Int32Type>();
4558 assert_eq!(5, c.value(0));
4559 assert_eq!(6, c.value(1));
4560 assert!(!c.is_valid(2));
4561 assert_eq!(8, c.value(3));
4562 assert!(!c.is_valid(4));
4563 }
4564
4565 #[test]
4566 fn test_cast_utf8view_to_i32() {
4567 let array = StringViewArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4568 let b = cast(&array, &DataType::Int32).unwrap();
4569 let c = b.as_primitive::<Int32Type>();
4570 assert_eq!(5, c.value(0));
4571 assert_eq!(6, c.value(1));
4572 assert!(!c.is_valid(2));
4573 assert_eq!(8, c.value(3));
4574 assert!(!c.is_valid(4));
4575 }
4576
4577 #[test]
4578 fn test_cast_utf8view_to_f32() {
4579 let array = StringViewArray::from(vec!["3", "4.56", "seven", "8.9"]);
4580 let b = cast(&array, &DataType::Float32).unwrap();
4581 let c = b.as_primitive::<Float32Type>();
4582 assert_eq!(3.0, c.value(0));
4583 assert_eq!(4.56, c.value(1));
4584 assert!(!c.is_valid(2));
4585 assert_eq!(8.9, c.value(3));
4586 }
4587
4588 #[test]
4589 fn test_cast_string_to_f16() {
4590 let arrays = [
4591 Arc::new(StringViewArray::from(vec!["3", "4.56", "seven", "8.9"])) as ArrayRef,
4592 Arc::new(StringArray::from(vec!["3", "4.56", "seven", "8.9"])),
4593 Arc::new(LargeStringArray::from(vec!["3", "4.56", "seven", "8.9"])),
4594 ];
4595 for array in arrays {
4596 let b = cast(&array, &DataType::Float16).unwrap();
4597 let c = b.as_primitive::<Float16Type>();
4598 assert_eq!(half::f16::from_f32(3.0), c.value(0));
4599 assert_eq!(half::f16::from_f32(4.56), c.value(1));
4600 assert!(!c.is_valid(2));
4601 assert_eq!(half::f16::from_f32(8.9), c.value(3));
4602 }
4603 }
4604
4605 #[test]
4606 fn test_cast_utf8view_to_decimal128() {
4607 let array = StringViewArray::from(vec![None, Some("4"), Some("5.6"), Some("7.89")]);
4608 let arr = Arc::new(array) as ArrayRef;
4609 generate_cast_test_case!(
4610 &arr,
4611 Decimal128Array,
4612 &DataType::Decimal128(4, 2),
4613 vec![None, Some(400_i128), Some(560_i128), Some(789_i128)]
4614 );
4615 }
4616
4617 #[test]
4618 fn test_cast_with_options_utf8_to_i32() {
4619 let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4620 let result = cast_with_options(
4621 &array,
4622 &DataType::Int32,
4623 &CastOptions {
4624 safe: false,
4625 format_options: FormatOptions::default(),
4626 },
4627 );
4628 match result {
4629 Ok(_) => panic!("expected error"),
4630 Err(e) => {
4631 assert!(
4632 e.to_string()
4633 .contains("Cast error: Cannot cast string 'seven' to value of Int32 type",),
4634 "Error: {e}"
4635 )
4636 }
4637 }
4638 }
4639
4640 #[test]
4641 fn test_cast_utf8_to_bool() {
4642 let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4643 let casted = cast(&strings, &DataType::Boolean).unwrap();
4644 let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
4645 assert_eq!(*as_boolean_array(&casted), expected);
4646 }
4647
4648 #[test]
4649 fn test_cast_utf8view_to_bool() {
4650 let strings = StringViewArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4651 let casted = cast(&strings, &DataType::Boolean).unwrap();
4652 let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
4653 assert_eq!(*as_boolean_array(&casted), expected);
4654 }
4655
4656 #[test]
4657 fn test_cast_with_options_utf8_to_bool() {
4658 let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4659 let casted = cast_with_options(
4660 &strings,
4661 &DataType::Boolean,
4662 &CastOptions {
4663 safe: false,
4664 format_options: FormatOptions::default(),
4665 },
4666 );
4667 match casted {
4668 Ok(_) => panic!("expected error"),
4669 Err(e) => {
4670 assert!(
4671 e.to_string().contains(
4672 "Cast error: Cannot cast value 'invalid' to value of Boolean type"
4673 )
4674 )
4675 }
4676 }
4677 }
4678
4679 #[test]
4680 fn test_cast_bool_to_i32() {
4681 let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4682 let b = cast(&array, &DataType::Int32).unwrap();
4683 let c = b.as_primitive::<Int32Type>();
4684 assert_eq!(1, c.value(0));
4685 assert_eq!(0, c.value(1));
4686 assert!(!c.is_valid(2));
4687 }
4688
4689 #[test]
4690 fn test_cast_bool_to_utf8view() {
4691 let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4692 let b = cast(&array, &DataType::Utf8View).unwrap();
4693 let c = b.as_any().downcast_ref::<StringViewArray>().unwrap();
4694 assert_eq!("true", c.value(0));
4695 assert_eq!("false", c.value(1));
4696 assert!(!c.is_valid(2));
4697 }
4698
4699 #[test]
4700 fn test_cast_bool_to_utf8() {
4701 let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4702 let b = cast(&array, &DataType::Utf8).unwrap();
4703 let c = b.as_any().downcast_ref::<StringArray>().unwrap();
4704 assert_eq!("true", c.value(0));
4705 assert_eq!("false", c.value(1));
4706 assert!(!c.is_valid(2));
4707 }
4708
4709 #[test]
4710 fn test_cast_bool_to_large_utf8() {
4711 let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4712 let b = cast(&array, &DataType::LargeUtf8).unwrap();
4713 let c = b.as_any().downcast_ref::<LargeStringArray>().unwrap();
4714 assert_eq!("true", c.value(0));
4715 assert_eq!("false", c.value(1));
4716 assert!(!c.is_valid(2));
4717 }
4718
4719 #[test]
4720 fn test_cast_bool_to_f64() {
4721 let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4722 let b = cast(&array, &DataType::Float64).unwrap();
4723 let c = b.as_primitive::<Float64Type>();
4724 assert_eq!(1.0, c.value(0));
4725 assert_eq!(0.0, c.value(1));
4726 assert!(!c.is_valid(2));
4727 }
4728
4729 #[test]
4730 fn test_cast_integer_to_timestamp() {
4731 let array = Int64Array::from(vec![Some(2), Some(10), None]);
4732 let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4733
4734 let array = Int8Array::from(vec![Some(2), Some(10), None]);
4735 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4736
4737 assert_eq!(&actual, &expected);
4738
4739 let array = Int16Array::from(vec![Some(2), Some(10), None]);
4740 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4741
4742 assert_eq!(&actual, &expected);
4743
4744 let array = Int32Array::from(vec![Some(2), Some(10), None]);
4745 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4746
4747 assert_eq!(&actual, &expected);
4748
4749 let array = UInt8Array::from(vec![Some(2), Some(10), None]);
4750 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4751
4752 assert_eq!(&actual, &expected);
4753
4754 let array = UInt16Array::from(vec![Some(2), Some(10), None]);
4755 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4756
4757 assert_eq!(&actual, &expected);
4758
4759 let array = UInt32Array::from(vec![Some(2), Some(10), None]);
4760 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4761
4762 assert_eq!(&actual, &expected);
4763
4764 let array = UInt64Array::from(vec![Some(2), Some(10), None]);
4765 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4766
4767 assert_eq!(&actual, &expected);
4768 }
4769
4770 #[test]
4771 fn test_cast_timestamp_to_integer() {
4772 let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
4773 .with_timezone("UTC".to_string());
4774 let expected = cast(&array, &DataType::Int64).unwrap();
4775
4776 let actual = cast(&cast(&array, &DataType::Int8).unwrap(), &DataType::Int64).unwrap();
4777 assert_eq!(&actual, &expected);
4778
4779 let actual = cast(&cast(&array, &DataType::Int16).unwrap(), &DataType::Int64).unwrap();
4780 assert_eq!(&actual, &expected);
4781
4782 let actual = cast(&cast(&array, &DataType::Int32).unwrap(), &DataType::Int64).unwrap();
4783 assert_eq!(&actual, &expected);
4784
4785 let actual = cast(&cast(&array, &DataType::UInt8).unwrap(), &DataType::Int64).unwrap();
4786 assert_eq!(&actual, &expected);
4787
4788 let actual = cast(&cast(&array, &DataType::UInt16).unwrap(), &DataType::Int64).unwrap();
4789 assert_eq!(&actual, &expected);
4790
4791 let actual = cast(&cast(&array, &DataType::UInt32).unwrap(), &DataType::Int64).unwrap();
4792 assert_eq!(&actual, &expected);
4793
4794 let actual = cast(&cast(&array, &DataType::UInt64).unwrap(), &DataType::Int64).unwrap();
4795 assert_eq!(&actual, &expected);
4796 }
4797
4798 #[test]
4799 fn test_cast_floating_to_timestamp() {
4800 let array = Int64Array::from(vec![Some(2), Some(10), None]);
4801 let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4802
4803 let array = Float16Array::from(vec![
4804 Some(f16::from_f32(2.0)),
4805 Some(f16::from_f32(10.6)),
4806 None,
4807 ]);
4808 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4809
4810 assert_eq!(&actual, &expected);
4811
4812 let array = Float32Array::from(vec![Some(2.0), Some(10.6), None]);
4813 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4814
4815 assert_eq!(&actual, &expected);
4816
4817 let array = Float64Array::from(vec![Some(2.1), Some(10.2), None]);
4818 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4819
4820 assert_eq!(&actual, &expected);
4821 }
4822
4823 #[test]
4824 fn test_cast_timestamp_to_floating() {
4825 let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
4826 .with_timezone("UTC".to_string());
4827 let expected = cast(&array, &DataType::Int64).unwrap();
4828
4829 let actual = cast(&cast(&array, &DataType::Float16).unwrap(), &DataType::Int64).unwrap();
4830 assert_eq!(&actual, &expected);
4831
4832 let actual = cast(&cast(&array, &DataType::Float32).unwrap(), &DataType::Int64).unwrap();
4833 assert_eq!(&actual, &expected);
4834
4835 let actual = cast(&cast(&array, &DataType::Float64).unwrap(), &DataType::Int64).unwrap();
4836 assert_eq!(&actual, &expected);
4837 }
4838
4839 #[test]
4840 fn test_cast_decimal_to_timestamp() {
4841 let array = Int64Array::from(vec![Some(2), Some(10), None]);
4842 let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4843
4844 let array = Decimal128Array::from(vec![Some(200), Some(1000), None])
4845 .with_precision_and_scale(4, 2)
4846 .unwrap();
4847 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4848
4849 assert_eq!(&actual, &expected);
4850
4851 let array = Decimal256Array::from(vec![
4852 Some(i256::from_i128(2000)),
4853 Some(i256::from_i128(10000)),
4854 None,
4855 ])
4856 .with_precision_and_scale(5, 3)
4857 .unwrap();
4858 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4859
4860 assert_eq!(&actual, &expected);
4861 }
4862
4863 #[test]
4864 fn test_cast_timestamp_to_decimal() {
4865 let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
4866 .with_timezone("UTC".to_string());
4867 let expected = cast(&array, &DataType::Int64).unwrap();
4868
4869 let actual = cast(
4870 &cast(&array, &DataType::Decimal128(5, 2)).unwrap(),
4871 &DataType::Int64,
4872 )
4873 .unwrap();
4874 assert_eq!(&actual, &expected);
4875
4876 let actual = cast(
4877 &cast(&array, &DataType::Decimal256(10, 5)).unwrap(),
4878 &DataType::Int64,
4879 )
4880 .unwrap();
4881 assert_eq!(&actual, &expected);
4882 }
4883
4884 #[test]
4885 fn test_cast_list_i32_to_list_u16() {
4886 let values = vec![
4887 Some(vec![Some(0), Some(0), Some(0)]),
4888 Some(vec![Some(-1), Some(-2), Some(-1)]),
4889 Some(vec![Some(2), Some(100000000)]),
4890 ];
4891 let list_array = ListArray::from_iter_primitive::<Int32Type, _, _>(values);
4892
4893 let target_type = DataType::List(Arc::new(Field::new("item", DataType::UInt16, true)));
4894 assert!(can_cast_types(list_array.data_type(), &target_type));
4895 let cast_array = cast(&list_array, &target_type).unwrap();
4896
4897 assert_eq!(0, cast_array.null_count());
4902
4903 let array = cast_array.as_list::<i32>();
4905 assert_eq!(list_array.value_offsets(), array.value_offsets());
4906
4907 assert_eq!(DataType::UInt16, array.value_type());
4908 assert_eq!(3, array.value_length(0));
4909 assert_eq!(3, array.value_length(1));
4910 assert_eq!(2, array.value_length(2));
4911
4912 let u16arr = array.values().as_primitive::<UInt16Type>();
4914 assert_eq!(4, u16arr.null_count());
4915
4916 let expected: UInt16Array =
4918 vec![Some(0), Some(0), Some(0), None, None, None, Some(2), None]
4919 .into_iter()
4920 .collect();
4921
4922 assert_eq!(u16arr, &expected);
4923 }
4924
4925 #[test]
4926 fn test_cast_list_i32_to_list_timestamp() {
4927 let value_data = Int32Array::from(vec![0, 0, 0, -1, -2, -1, 2, 8, 100000000]).into_data();
4929
4930 let value_offsets = Buffer::from_slice_ref([0, 3, 6, 9]);
4931
4932 let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
4934 let list_data = ArrayData::builder(list_data_type)
4935 .len(3)
4936 .add_buffer(value_offsets)
4937 .add_child_data(value_data)
4938 .build()
4939 .unwrap();
4940 let list_array = Arc::new(ListArray::from(list_data)) as ArrayRef;
4941
4942 let actual = cast(
4943 &list_array,
4944 &DataType::List(Arc::new(Field::new_list_field(
4945 DataType::Timestamp(TimeUnit::Microsecond, None),
4946 true,
4947 ))),
4948 )
4949 .unwrap();
4950
4951 let expected = cast(
4952 &cast(
4953 &list_array,
4954 &DataType::List(Arc::new(Field::new_list_field(DataType::Int64, true))),
4955 )
4956 .unwrap(),
4957 &DataType::List(Arc::new(Field::new_list_field(
4958 DataType::Timestamp(TimeUnit::Microsecond, None),
4959 true,
4960 ))),
4961 )
4962 .unwrap();
4963
4964 assert_eq!(&actual, &expected);
4965 }
4966
4967 #[test]
4968 fn test_cast_date32_to_date64() {
4969 let a = Date32Array::from(vec![10000, 17890]);
4970 let array = Arc::new(a) as ArrayRef;
4971 let b = cast(&array, &DataType::Date64).unwrap();
4972 let c = b.as_primitive::<Date64Type>();
4973 assert_eq!(864000000000, c.value(0));
4974 assert_eq!(1545696000000, c.value(1));
4975 }
4976
4977 #[test]
4978 fn test_cast_date64_to_date32() {
4979 let a = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
4980 let array = Arc::new(a) as ArrayRef;
4981 let b = cast(&array, &DataType::Date32).unwrap();
4982 let c = b.as_primitive::<Date32Type>();
4983 assert_eq!(10000, c.value(0));
4984 assert_eq!(17890, c.value(1));
4985 assert!(c.is_null(2));
4986 }
4987
4988 #[test]
4989 fn test_cast_string_to_integral_overflow() {
4990 let str = Arc::new(StringArray::from(vec![
4991 Some("123"),
4992 Some("-123"),
4993 Some("86374"),
4994 None,
4995 ])) as ArrayRef;
4996
4997 let options = CastOptions {
4998 safe: true,
4999 format_options: FormatOptions::default(),
5000 };
5001 let res = cast_with_options(&str, &DataType::Int16, &options).expect("should cast to i16");
5002 let expected =
5003 Arc::new(Int16Array::from(vec![Some(123), Some(-123), None, None])) as ArrayRef;
5004 assert_eq!(&res, &expected);
5005 }
5006
5007 #[test]
5008 fn test_cast_string_to_timestamp() {
5009 let a0 = Arc::new(StringViewArray::from(vec![
5010 Some("2020-09-08T12:00:00.123456789+00:00"),
5011 Some("Not a valid date"),
5012 None,
5013 ])) as ArrayRef;
5014 let a1 = Arc::new(StringArray::from(vec![
5015 Some("2020-09-08T12:00:00.123456789+00:00"),
5016 Some("Not a valid date"),
5017 None,
5018 ])) as ArrayRef;
5019 let a2 = Arc::new(LargeStringArray::from(vec![
5020 Some("2020-09-08T12:00:00.123456789+00:00"),
5021 Some("Not a valid date"),
5022 None,
5023 ])) as ArrayRef;
5024 for array in &[a0, a1, a2] {
5025 for time_unit in &[
5026 TimeUnit::Second,
5027 TimeUnit::Millisecond,
5028 TimeUnit::Microsecond,
5029 TimeUnit::Nanosecond,
5030 ] {
5031 let to_type = DataType::Timestamp(*time_unit, None);
5032 let b = cast(array, &to_type).unwrap();
5033
5034 match time_unit {
5035 TimeUnit::Second => {
5036 let c = b.as_primitive::<TimestampSecondType>();
5037 assert_eq!(1599566400, c.value(0));
5038 assert!(c.is_null(1));
5039 assert!(c.is_null(2));
5040 }
5041 TimeUnit::Millisecond => {
5042 let c = b
5043 .as_any()
5044 .downcast_ref::<TimestampMillisecondArray>()
5045 .unwrap();
5046 assert_eq!(1599566400123, c.value(0));
5047 assert!(c.is_null(1));
5048 assert!(c.is_null(2));
5049 }
5050 TimeUnit::Microsecond => {
5051 let c = b
5052 .as_any()
5053 .downcast_ref::<TimestampMicrosecondArray>()
5054 .unwrap();
5055 assert_eq!(1599566400123456, c.value(0));
5056 assert!(c.is_null(1));
5057 assert!(c.is_null(2));
5058 }
5059 TimeUnit::Nanosecond => {
5060 let c = b
5061 .as_any()
5062 .downcast_ref::<TimestampNanosecondArray>()
5063 .unwrap();
5064 assert_eq!(1599566400123456789, c.value(0));
5065 assert!(c.is_null(1));
5066 assert!(c.is_null(2));
5067 }
5068 }
5069
5070 let options = CastOptions {
5071 safe: false,
5072 format_options: FormatOptions::default(),
5073 };
5074 let err = cast_with_options(array, &to_type, &options).unwrap_err();
5075 assert_eq!(
5076 err.to_string(),
5077 "Parser error: Error parsing timestamp from 'Not a valid date': error parsing date"
5078 );
5079 }
5080 }
5081 }
5082
5083 #[test]
5084 fn test_cast_string_to_timestamp_overflow() {
5085 let array = StringArray::from(vec!["9800-09-08T12:00:00.123456789"]);
5086 let result = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
5087 let result = result.as_primitive::<TimestampSecondType>();
5088 assert_eq!(result.values(), &[247112596800]);
5089 }
5090
5091 #[test]
5092 fn test_cast_string_to_date32() {
5093 let a0 = Arc::new(StringViewArray::from(vec![
5094 Some("2018-12-25"),
5095 Some("Not a valid date"),
5096 None,
5097 ])) as ArrayRef;
5098 let a1 = Arc::new(StringArray::from(vec![
5099 Some("2018-12-25"),
5100 Some("Not a valid date"),
5101 None,
5102 ])) as ArrayRef;
5103 let a2 = Arc::new(LargeStringArray::from(vec![
5104 Some("2018-12-25"),
5105 Some("Not a valid date"),
5106 None,
5107 ])) as ArrayRef;
5108 for array in &[a0, a1, a2] {
5109 let to_type = DataType::Date32;
5110 let b = cast(array, &to_type).unwrap();
5111 let c = b.as_primitive::<Date32Type>();
5112 assert_eq!(17890, c.value(0));
5113 assert!(c.is_null(1));
5114 assert!(c.is_null(2));
5115
5116 let options = CastOptions {
5117 safe: false,
5118 format_options: FormatOptions::default(),
5119 };
5120 let err = cast_with_options(array, &to_type, &options).unwrap_err();
5121 assert_eq!(
5122 err.to_string(),
5123 "Cast error: Cannot cast string 'Not a valid date' to value of Date32 type"
5124 );
5125 }
5126 }
5127
5128 #[test]
5129 fn test_cast_string_with_large_date_to_date32() {
5130 let array = Arc::new(StringArray::from(vec![
5131 Some("+10999-12-31"),
5132 Some("-0010-02-28"),
5133 Some("0010-02-28"),
5134 Some("0000-01-01"),
5135 Some("-0000-01-01"),
5136 Some("-0001-01-01"),
5137 ])) as ArrayRef;
5138 let to_type = DataType::Date32;
5139 let options = CastOptions {
5140 safe: false,
5141 format_options: FormatOptions::default(),
5142 };
5143 let b = cast_with_options(&array, &to_type, &options).unwrap();
5144 let c = b.as_primitive::<Date32Type>();
5145 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)); }
5153
5154 #[test]
5155 fn test_cast_invalid_string_with_large_date_to_date32() {
5156 let array = Arc::new(StringArray::from(vec![Some("10999-12-31")])) as ArrayRef;
5158 let to_type = DataType::Date32;
5159 let options = CastOptions {
5160 safe: false,
5161 format_options: FormatOptions::default(),
5162 };
5163 let err = cast_with_options(&array, &to_type, &options).unwrap_err();
5164 assert_eq!(
5165 err.to_string(),
5166 "Cast error: Cannot cast string '10999-12-31' to value of Date32 type"
5167 );
5168 }
5169
5170 #[test]
5171 fn test_cast_string_format_yyyymmdd_to_date32() {
5172 let a0 = Arc::new(StringViewArray::from(vec![
5173 Some("2020-12-25"),
5174 Some("20201117"),
5175 ])) as ArrayRef;
5176 let a1 = Arc::new(StringArray::from(vec![
5177 Some("2020-12-25"),
5178 Some("20201117"),
5179 ])) as ArrayRef;
5180 let a2 = Arc::new(LargeStringArray::from(vec![
5181 Some("2020-12-25"),
5182 Some("20201117"),
5183 ])) as ArrayRef;
5184
5185 for array in &[a0, a1, a2] {
5186 let to_type = DataType::Date32;
5187 let options = CastOptions {
5188 safe: false,
5189 format_options: FormatOptions::default(),
5190 };
5191 let result = cast_with_options(&array, &to_type, &options).unwrap();
5192 let c = result.as_primitive::<Date32Type>();
5193 assert_eq!(
5194 chrono::NaiveDate::from_ymd_opt(2020, 12, 25),
5195 c.value_as_date(0)
5196 );
5197 assert_eq!(
5198 chrono::NaiveDate::from_ymd_opt(2020, 11, 17),
5199 c.value_as_date(1)
5200 );
5201 }
5202 }
5203
5204 #[test]
5205 fn test_cast_string_to_time32second() {
5206 let a0 = Arc::new(StringViewArray::from(vec![
5207 Some("08:08:35.091323414"),
5208 Some("08:08:60.091323414"), Some("08:08:61.091323414"), Some("Not a valid time"),
5211 None,
5212 ])) as ArrayRef;
5213 let a1 = Arc::new(StringArray::from(vec![
5214 Some("08:08:35.091323414"),
5215 Some("08:08:60.091323414"), Some("08:08:61.091323414"), Some("Not a valid time"),
5218 None,
5219 ])) as ArrayRef;
5220 let a2 = Arc::new(LargeStringArray::from(vec![
5221 Some("08:08:35.091323414"),
5222 Some("08:08:60.091323414"), Some("08:08:61.091323414"), Some("Not a valid time"),
5225 None,
5226 ])) as ArrayRef;
5227 for array in &[a0, a1, a2] {
5228 let to_type = DataType::Time32(TimeUnit::Second);
5229 let b = cast(array, &to_type).unwrap();
5230 let c = b.as_primitive::<Time32SecondType>();
5231 assert_eq!(29315, c.value(0));
5232 assert_eq!(29340, c.value(1));
5233 assert!(c.is_null(2));
5234 assert!(c.is_null(3));
5235 assert!(c.is_null(4));
5236
5237 let options = CastOptions {
5238 safe: false,
5239 format_options: FormatOptions::default(),
5240 };
5241 let err = cast_with_options(array, &to_type, &options).unwrap_err();
5242 assert_eq!(
5243 err.to_string(),
5244 "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(s) type"
5245 );
5246 }
5247 }
5248
5249 #[test]
5250 fn test_cast_string_to_time32millisecond() {
5251 let a0 = Arc::new(StringViewArray::from(vec![
5252 Some("08:08:35.091323414"),
5253 Some("08:08:60.091323414"), Some("08:08:61.091323414"), Some("Not a valid time"),
5256 None,
5257 ])) as ArrayRef;
5258 let a1 = Arc::new(StringArray::from(vec![
5259 Some("08:08:35.091323414"),
5260 Some("08:08:60.091323414"), Some("08:08:61.091323414"), Some("Not a valid time"),
5263 None,
5264 ])) as ArrayRef;
5265 let a2 = Arc::new(LargeStringArray::from(vec![
5266 Some("08:08:35.091323414"),
5267 Some("08:08:60.091323414"), Some("08:08:61.091323414"), Some("Not a valid time"),
5270 None,
5271 ])) as ArrayRef;
5272 for array in &[a0, a1, a2] {
5273 let to_type = DataType::Time32(TimeUnit::Millisecond);
5274 let b = cast(array, &to_type).unwrap();
5275 let c = b.as_primitive::<Time32MillisecondType>();
5276 assert_eq!(29315091, c.value(0));
5277 assert_eq!(29340091, c.value(1));
5278 assert!(c.is_null(2));
5279 assert!(c.is_null(3));
5280 assert!(c.is_null(4));
5281
5282 let options = CastOptions {
5283 safe: false,
5284 format_options: FormatOptions::default(),
5285 };
5286 let err = cast_with_options(array, &to_type, &options).unwrap_err();
5287 assert_eq!(
5288 err.to_string(),
5289 "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(ms) type"
5290 );
5291 }
5292 }
5293
5294 #[test]
5295 fn test_cast_string_to_time64microsecond() {
5296 let a0 = Arc::new(StringViewArray::from(vec![
5297 Some("08:08:35.091323414"),
5298 Some("Not a valid time"),
5299 None,
5300 ])) as ArrayRef;
5301 let a1 = Arc::new(StringArray::from(vec![
5302 Some("08:08:35.091323414"),
5303 Some("Not a valid time"),
5304 None,
5305 ])) as ArrayRef;
5306 let a2 = Arc::new(LargeStringArray::from(vec![
5307 Some("08:08:35.091323414"),
5308 Some("Not a valid time"),
5309 None,
5310 ])) as ArrayRef;
5311 for array in &[a0, a1, a2] {
5312 let to_type = DataType::Time64(TimeUnit::Microsecond);
5313 let b = cast(array, &to_type).unwrap();
5314 let c = b.as_primitive::<Time64MicrosecondType>();
5315 assert_eq!(29315091323, c.value(0));
5316 assert!(c.is_null(1));
5317 assert!(c.is_null(2));
5318
5319 let options = CastOptions {
5320 safe: false,
5321 format_options: FormatOptions::default(),
5322 };
5323 let err = cast_with_options(array, &to_type, &options).unwrap_err();
5324 assert_eq!(
5325 err.to_string(),
5326 "Cast error: Cannot cast string 'Not a valid time' to value of Time64(µs) type"
5327 );
5328 }
5329 }
5330
5331 #[test]
5332 fn test_cast_string_to_time64nanosecond() {
5333 let a0 = Arc::new(StringViewArray::from(vec![
5334 Some("08:08:35.091323414"),
5335 Some("Not a valid time"),
5336 None,
5337 ])) as ArrayRef;
5338 let a1 = Arc::new(StringArray::from(vec![
5339 Some("08:08:35.091323414"),
5340 Some("Not a valid time"),
5341 None,
5342 ])) as ArrayRef;
5343 let a2 = Arc::new(LargeStringArray::from(vec![
5344 Some("08:08:35.091323414"),
5345 Some("Not a valid time"),
5346 None,
5347 ])) as ArrayRef;
5348 for array in &[a0, a1, a2] {
5349 let to_type = DataType::Time64(TimeUnit::Nanosecond);
5350 let b = cast(array, &to_type).unwrap();
5351 let c = b.as_primitive::<Time64NanosecondType>();
5352 assert_eq!(29315091323414, c.value(0));
5353 assert!(c.is_null(1));
5354 assert!(c.is_null(2));
5355
5356 let options = CastOptions {
5357 safe: false,
5358 format_options: FormatOptions::default(),
5359 };
5360 let err = cast_with_options(array, &to_type, &options).unwrap_err();
5361 assert_eq!(
5362 err.to_string(),
5363 "Cast error: Cannot cast string 'Not a valid time' to value of Time64(ns) type"
5364 );
5365 }
5366 }
5367
5368 #[test]
5369 fn test_cast_string_to_date64() {
5370 let a0 = Arc::new(StringViewArray::from(vec![
5371 Some("2020-09-08T12:00:00"),
5372 Some("Not a valid date"),
5373 None,
5374 ])) as ArrayRef;
5375 let a1 = Arc::new(StringArray::from(vec![
5376 Some("2020-09-08T12:00:00"),
5377 Some("Not a valid date"),
5378 None,
5379 ])) as ArrayRef;
5380 let a2 = Arc::new(LargeStringArray::from(vec![
5381 Some("2020-09-08T12:00:00"),
5382 Some("Not a valid date"),
5383 None,
5384 ])) as ArrayRef;
5385 for array in &[a0, a1, a2] {
5386 let to_type = DataType::Date64;
5387 let b = cast(array, &to_type).unwrap();
5388 let c = b.as_primitive::<Date64Type>();
5389 assert_eq!(1599566400000, c.value(0));
5390 assert!(c.is_null(1));
5391 assert!(c.is_null(2));
5392
5393 let options = CastOptions {
5394 safe: false,
5395 format_options: FormatOptions::default(),
5396 };
5397 let err = cast_with_options(array, &to_type, &options).unwrap_err();
5398 assert_eq!(
5399 err.to_string(),
5400 "Cast error: Cannot cast string 'Not a valid date' to value of Date64 type"
5401 );
5402 }
5403 }
5404
5405 macro_rules! test_safe_string_to_interval {
5406 ($data_vec:expr, $interval_unit:expr, $array_ty:ty, $expect_vec:expr) => {
5407 let source_string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
5408
5409 let options = CastOptions {
5410 safe: true,
5411 format_options: FormatOptions::default(),
5412 };
5413
5414 let target_interval_array = cast_with_options(
5415 &source_string_array.clone(),
5416 &DataType::Interval($interval_unit),
5417 &options,
5418 )
5419 .unwrap()
5420 .as_any()
5421 .downcast_ref::<$array_ty>()
5422 .unwrap()
5423 .clone() as $array_ty;
5424
5425 let target_string_array =
5426 cast_with_options(&target_interval_array, &DataType::Utf8, &options)
5427 .unwrap()
5428 .as_any()
5429 .downcast_ref::<StringArray>()
5430 .unwrap()
5431 .clone();
5432
5433 let expect_string_array = StringArray::from($expect_vec);
5434
5435 assert_eq!(target_string_array, expect_string_array);
5436
5437 let target_large_string_array =
5438 cast_with_options(&target_interval_array, &DataType::LargeUtf8, &options)
5439 .unwrap()
5440 .as_any()
5441 .downcast_ref::<LargeStringArray>()
5442 .unwrap()
5443 .clone();
5444
5445 let expect_large_string_array = LargeStringArray::from($expect_vec);
5446
5447 assert_eq!(target_large_string_array, expect_large_string_array);
5448 };
5449 }
5450
5451 #[test]
5452 fn test_cast_string_to_interval_year_month() {
5453 test_safe_string_to_interval!(
5454 vec![
5455 Some("1 year 1 month"),
5456 Some("1.5 years 13 month"),
5457 Some("30 days"),
5458 Some("31 days"),
5459 Some("2 months 31 days"),
5460 Some("2 months 31 days 1 second"),
5461 Some("foobar"),
5462 ],
5463 IntervalUnit::YearMonth,
5464 IntervalYearMonthArray,
5465 vec![
5466 Some("1 years 1 mons"),
5467 Some("2 years 7 mons"),
5468 None,
5469 None,
5470 None,
5471 None,
5472 None,
5473 ]
5474 );
5475 }
5476
5477 #[test]
5478 fn test_cast_string_to_interval_day_time() {
5479 test_safe_string_to_interval!(
5480 vec![
5481 Some("1 year 1 month"),
5482 Some("1.5 years 13 month"),
5483 Some("30 days"),
5484 Some("1 day 2 second 3.5 milliseconds"),
5485 Some("foobar"),
5486 ],
5487 IntervalUnit::DayTime,
5488 IntervalDayTimeArray,
5489 vec![
5490 Some("390 days"),
5491 Some("930 days"),
5492 Some("30 days"),
5493 None,
5494 None,
5495 ]
5496 );
5497 }
5498
5499 #[test]
5500 fn test_cast_string_to_interval_month_day_nano() {
5501 test_safe_string_to_interval!(
5502 vec![
5503 Some("1 year 1 month 1 day"),
5504 None,
5505 Some("1.5 years 13 month 35 days 1.4 milliseconds"),
5506 Some("3 days"),
5507 Some("8 seconds"),
5508 None,
5509 Some("1 day 29800 milliseconds"),
5510 Some("3 months 1 second"),
5511 Some("6 minutes 120 second"),
5512 Some("2 years 39 months 9 days 19 hours 1 minute 83 seconds 399222 milliseconds"),
5513 Some("foobar"),
5514 ],
5515 IntervalUnit::MonthDayNano,
5516 IntervalMonthDayNanoArray,
5517 vec![
5518 Some("13 mons 1 days"),
5519 None,
5520 Some("31 mons 35 days 0.001400000 secs"),
5521 Some("3 days"),
5522 Some("8.000000000 secs"),
5523 None,
5524 Some("1 days 29.800000000 secs"),
5525 Some("3 mons 1.000000000 secs"),
5526 Some("8 mins"),
5527 Some("63 mons 9 days 19 hours 9 mins 2.222000000 secs"),
5528 None,
5529 ]
5530 );
5531 }
5532
5533 macro_rules! test_unsafe_string_to_interval_err {
5534 ($data_vec:expr, $interval_unit:expr, $error_msg:expr) => {
5535 let string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
5536 let options = CastOptions {
5537 safe: false,
5538 format_options: FormatOptions::default(),
5539 };
5540 let arrow_err = cast_with_options(
5541 &string_array.clone(),
5542 &DataType::Interval($interval_unit),
5543 &options,
5544 )
5545 .unwrap_err();
5546 assert_eq!($error_msg, arrow_err.to_string());
5547 };
5548 }
5549
5550 #[test]
5551 fn test_cast_string_to_interval_err() {
5552 test_unsafe_string_to_interval_err!(
5553 vec![Some("foobar")],
5554 IntervalUnit::YearMonth,
5555 r#"Parser error: Invalid input syntax for type interval: "foobar""#
5556 );
5557 test_unsafe_string_to_interval_err!(
5558 vec![Some("foobar")],
5559 IntervalUnit::DayTime,
5560 r#"Parser error: Invalid input syntax for type interval: "foobar""#
5561 );
5562 test_unsafe_string_to_interval_err!(
5563 vec![Some("foobar")],
5564 IntervalUnit::MonthDayNano,
5565 r#"Parser error: Invalid input syntax for type interval: "foobar""#
5566 );
5567 test_unsafe_string_to_interval_err!(
5568 vec![Some("2 months 31 days 1 second")],
5569 IntervalUnit::YearMonth,
5570 r#"Cast error: Cannot cast 2 months 31 days 1 second to IntervalYearMonth. Only year and month fields are allowed."#
5571 );
5572 test_unsafe_string_to_interval_err!(
5573 vec![Some("1 day 1.5 milliseconds")],
5574 IntervalUnit::DayTime,
5575 r#"Cast error: Cannot cast 1 day 1.5 milliseconds to IntervalDayTime because the nanos part isn't multiple of milliseconds"#
5576 );
5577
5578 test_unsafe_string_to_interval_err!(
5580 vec![Some(format!(
5581 "{} century {} year {} month",
5582 i64::MAX - 2,
5583 i64::MAX - 2,
5584 i64::MAX - 2
5585 ))],
5586 IntervalUnit::DayTime,
5587 format!(
5588 "Arithmetic overflow: Overflow happened on: {} * 100",
5589 i64::MAX - 2
5590 )
5591 );
5592 test_unsafe_string_to_interval_err!(
5593 vec![Some(format!(
5594 "{} year {} month {} day",
5595 i64::MAX - 2,
5596 i64::MAX - 2,
5597 i64::MAX - 2
5598 ))],
5599 IntervalUnit::MonthDayNano,
5600 format!(
5601 "Arithmetic overflow: Overflow happened on: {} * 12",
5602 i64::MAX - 2
5603 )
5604 );
5605 }
5606
5607 #[test]
5608 fn test_cast_binary_to_fixed_size_binary() {
5609 let bytes_1 = "Hiiii".as_bytes();
5610 let bytes_2 = "Hello".as_bytes();
5611
5612 let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5613 let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
5614 let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
5615
5616 let array_ref = cast(&a1, &DataType::FixedSizeBinary(5)).unwrap();
5617 let down_cast = array_ref
5618 .as_any()
5619 .downcast_ref::<FixedSizeBinaryArray>()
5620 .unwrap();
5621 assert_eq!(bytes_1, down_cast.value(0));
5622 assert_eq!(bytes_2, down_cast.value(1));
5623 assert!(down_cast.is_null(2));
5624
5625 let array_ref = cast(&a2, &DataType::FixedSizeBinary(5)).unwrap();
5626 let down_cast = array_ref
5627 .as_any()
5628 .downcast_ref::<FixedSizeBinaryArray>()
5629 .unwrap();
5630 assert_eq!(bytes_1, down_cast.value(0));
5631 assert_eq!(bytes_2, down_cast.value(1));
5632 assert!(down_cast.is_null(2));
5633
5634 let bytes_1 = "Hi".as_bytes();
5636 let bytes_2 = "Hello".as_bytes();
5637
5638 let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5639 let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
5640 let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
5641
5642 let array_ref = cast_with_options(
5643 &a1,
5644 &DataType::FixedSizeBinary(5),
5645 &CastOptions {
5646 safe: false,
5647 format_options: FormatOptions::default(),
5648 },
5649 );
5650 assert!(array_ref.is_err());
5651
5652 let array_ref = cast_with_options(
5653 &a2,
5654 &DataType::FixedSizeBinary(5),
5655 &CastOptions {
5656 safe: false,
5657 format_options: FormatOptions::default(),
5658 },
5659 );
5660 assert!(array_ref.is_err());
5661 }
5662
5663 #[test]
5664 fn test_fixed_size_binary_to_binary() {
5665 let bytes_1 = "Hiiii".as_bytes();
5666 let bytes_2 = "Hello".as_bytes();
5667
5668 let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5669 let a1 = Arc::new(FixedSizeBinaryArray::from(binary_data.clone())) as ArrayRef;
5670
5671 let array_ref = cast(&a1, &DataType::Binary).unwrap();
5672 let down_cast = array_ref.as_binary::<i32>();
5673 assert_eq!(bytes_1, down_cast.value(0));
5674 assert_eq!(bytes_2, down_cast.value(1));
5675 assert!(down_cast.is_null(2));
5676
5677 let array_ref = cast(&a1, &DataType::LargeBinary).unwrap();
5678 let down_cast = array_ref.as_binary::<i64>();
5679 assert_eq!(bytes_1, down_cast.value(0));
5680 assert_eq!(bytes_2, down_cast.value(1));
5681 assert!(down_cast.is_null(2));
5682
5683 let array_ref = cast(&a1, &DataType::BinaryView).unwrap();
5684 let down_cast = array_ref.as_binary_view();
5685 assert_eq!(bytes_1, down_cast.value(0));
5686 assert_eq!(bytes_2, down_cast.value(1));
5687 assert!(down_cast.is_null(2));
5688 }
5689
5690 #[test]
5691 fn test_fixed_size_binary_to_dictionary() {
5692 let bytes_1 = "Hiiii".as_bytes();
5693 let bytes_2 = "Hello".as_bytes();
5694
5695 let binary_data = vec![Some(bytes_1), Some(bytes_2), Some(bytes_1), None];
5696 let a1 = Arc::new(FixedSizeBinaryArray::from(binary_data.clone())) as ArrayRef;
5697
5698 let cast_type = DataType::Dictionary(
5699 Box::new(DataType::Int8),
5700 Box::new(DataType::FixedSizeBinary(5)),
5701 );
5702 let cast_array = cast(&a1, &cast_type).unwrap();
5703 assert_eq!(cast_array.data_type(), &cast_type);
5704 assert_eq!(
5705 array_to_strings(&cast_array),
5706 vec!["4869696969", "48656c6c6f", "4869696969", "null"]
5707 );
5708 let dict_array = cast_array.as_dictionary::<Int8Type>();
5710 assert_eq!(dict_array.values().len(), 2);
5711 }
5712
5713 #[test]
5714 fn test_binary_to_dictionary() {
5715 let mut builder = GenericBinaryBuilder::<i32>::new();
5716 builder.append_value(b"hello");
5717 builder.append_value(b"hiiii");
5718 builder.append_value(b"hiiii"); builder.append_null();
5720 builder.append_value(b"rustt");
5721
5722 let a1 = builder.finish();
5723
5724 let cast_type = DataType::Dictionary(
5725 Box::new(DataType::Int8),
5726 Box::new(DataType::FixedSizeBinary(5)),
5727 );
5728 let cast_array = cast(&a1, &cast_type).unwrap();
5729 assert_eq!(cast_array.data_type(), &cast_type);
5730 assert_eq!(
5731 array_to_strings(&cast_array),
5732 vec![
5733 "68656c6c6f",
5734 "6869696969",
5735 "6869696969",
5736 "null",
5737 "7275737474"
5738 ]
5739 );
5740 let dict_array = cast_array.as_dictionary::<Int8Type>();
5742 assert_eq!(dict_array.values().len(), 3);
5743 }
5744
5745 #[test]
5746 fn test_cast_string_array_to_dict_utf8_view() {
5747 let array = StringArray::from(vec![Some("one"), None, Some("three"), Some("one")]);
5748
5749 let cast_type =
5750 DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
5751 assert!(can_cast_types(array.data_type(), &cast_type));
5752 let cast_array = cast(&array, &cast_type).unwrap();
5753 assert_eq!(cast_array.data_type(), &cast_type);
5754
5755 let dict_array = cast_array.as_dictionary::<UInt16Type>();
5756 assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
5757 assert_eq!(dict_array.values().len(), 2); let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
5760 let actual: Vec<Option<&str>> = typed.into_iter().collect();
5761 assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
5762
5763 let keys = dict_array.keys();
5764 assert!(keys.is_null(1));
5765 assert_eq!(keys.value(0), keys.value(3));
5766 assert_ne!(keys.value(0), keys.value(2));
5767 }
5768
5769 #[test]
5770 fn test_cast_string_array_to_dict_utf8_view_null_vs_literal_null() {
5771 let array = StringArray::from(vec![Some("one"), None, Some("null"), Some("one")]);
5772
5773 let cast_type =
5774 DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
5775 assert!(can_cast_types(array.data_type(), &cast_type));
5776 let cast_array = cast(&array, &cast_type).unwrap();
5777 assert_eq!(cast_array.data_type(), &cast_type);
5778
5779 let dict_array = cast_array.as_dictionary::<UInt16Type>();
5780 assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
5781 assert_eq!(dict_array.values().len(), 2);
5782
5783 let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
5784 let actual: Vec<Option<&str>> = typed.into_iter().collect();
5785 assert_eq!(actual, vec![Some("one"), None, Some("null"), Some("one")]);
5786
5787 let keys = dict_array.keys();
5788 assert!(keys.is_null(1));
5789 assert_eq!(keys.value(0), keys.value(3));
5790 assert_ne!(keys.value(0), keys.value(2));
5791 }
5792
5793 #[test]
5794 fn test_cast_string_view_array_to_dict_utf8_view() {
5795 let array = StringViewArray::from(vec![Some("one"), None, Some("three"), Some("one")]);
5796
5797 let cast_type =
5798 DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
5799 assert!(can_cast_types(array.data_type(), &cast_type));
5800 let cast_array = cast(&array, &cast_type).unwrap();
5801 assert_eq!(cast_array.data_type(), &cast_type);
5802
5803 let dict_array = cast_array.as_dictionary::<UInt16Type>();
5804 assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
5805 assert_eq!(dict_array.values().len(), 2); let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
5808 let actual: Vec<Option<&str>> = typed.into_iter().collect();
5809 assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
5810
5811 let keys = dict_array.keys();
5812 assert!(keys.is_null(1));
5813 assert_eq!(keys.value(0), keys.value(3));
5814 assert_ne!(keys.value(0), keys.value(2));
5815 }
5816
5817 #[test]
5818 fn test_cast_string_view_slice_to_dict_utf8_view() {
5819 let array = StringViewArray::from(vec![
5820 Some("zero"),
5821 Some("one"),
5822 None,
5823 Some("three"),
5824 Some("one"),
5825 ]);
5826 let view = array.slice(1, 4);
5827
5828 let cast_type =
5829 DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
5830 assert!(can_cast_types(view.data_type(), &cast_type));
5831 let cast_array = cast(&view, &cast_type).unwrap();
5832 assert_eq!(cast_array.data_type(), &cast_type);
5833
5834 let dict_array = cast_array.as_dictionary::<UInt16Type>();
5835 assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
5836 assert_eq!(dict_array.values().len(), 2);
5837
5838 let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
5839 let actual: Vec<Option<&str>> = typed.into_iter().collect();
5840 assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
5841
5842 let keys = dict_array.keys();
5843 assert!(keys.is_null(1));
5844 assert_eq!(keys.value(0), keys.value(3));
5845 assert_ne!(keys.value(0), keys.value(2));
5846 }
5847
5848 #[test]
5849 fn test_cast_binary_array_to_dict_binary_view() {
5850 let mut builder = GenericBinaryBuilder::<i32>::new();
5851 builder.append_value(b"hello");
5852 builder.append_value(b"hiiii");
5853 builder.append_value(b"hiiii"); builder.append_null();
5855 builder.append_value(b"rustt");
5856
5857 let array = builder.finish();
5858
5859 let cast_type =
5860 DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
5861 assert!(can_cast_types(array.data_type(), &cast_type));
5862 let cast_array = cast(&array, &cast_type).unwrap();
5863 assert_eq!(cast_array.data_type(), &cast_type);
5864
5865 let dict_array = cast_array.as_dictionary::<UInt16Type>();
5866 assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
5867 assert_eq!(dict_array.values().len(), 3);
5868
5869 let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
5870 let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
5871 assert_eq!(
5872 actual,
5873 vec![
5874 Some(b"hello".as_slice()),
5875 Some(b"hiiii".as_slice()),
5876 Some(b"hiiii".as_slice()),
5877 None,
5878 Some(b"rustt".as_slice())
5879 ]
5880 );
5881
5882 let keys = dict_array.keys();
5883 assert!(keys.is_null(3));
5884 assert_eq!(keys.value(1), keys.value(2));
5885 assert_ne!(keys.value(0), keys.value(1));
5886 }
5887
5888 #[test]
5889 fn test_cast_binary_view_array_to_dict_binary_view() {
5890 let view = BinaryViewArray::from_iter([
5891 Some(b"hello".as_slice()),
5892 Some(b"hiiii".as_slice()),
5893 Some(b"hiiii".as_slice()), None,
5895 Some(b"rustt".as_slice()),
5896 ]);
5897
5898 let cast_type =
5899 DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
5900 assert!(can_cast_types(view.data_type(), &cast_type));
5901 let cast_array = cast(&view, &cast_type).unwrap();
5902 assert_eq!(cast_array.data_type(), &cast_type);
5903
5904 let dict_array = cast_array.as_dictionary::<UInt16Type>();
5905 assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
5906 assert_eq!(dict_array.values().len(), 3);
5907
5908 let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
5909 let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
5910 assert_eq!(
5911 actual,
5912 vec![
5913 Some(b"hello".as_slice()),
5914 Some(b"hiiii".as_slice()),
5915 Some(b"hiiii".as_slice()),
5916 None,
5917 Some(b"rustt".as_slice())
5918 ]
5919 );
5920
5921 let keys = dict_array.keys();
5922 assert!(keys.is_null(3));
5923 assert_eq!(keys.value(1), keys.value(2));
5924 assert_ne!(keys.value(0), keys.value(1));
5925 }
5926
5927 #[test]
5928 fn test_cast_binary_view_slice_to_dict_binary_view() {
5929 let view = BinaryViewArray::from_iter([
5930 Some(b"hello".as_slice()),
5931 Some(b"hiiii".as_slice()),
5932 Some(b"hiiii".as_slice()), None,
5934 Some(b"rustt".as_slice()),
5935 ]);
5936 let sliced = view.slice(1, 4);
5937
5938 let cast_type =
5939 DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
5940 assert!(can_cast_types(sliced.data_type(), &cast_type));
5941 let cast_array = cast(&sliced, &cast_type).unwrap();
5942 assert_eq!(cast_array.data_type(), &cast_type);
5943
5944 let dict_array = cast_array.as_dictionary::<UInt16Type>();
5945 assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
5946 assert_eq!(dict_array.values().len(), 2);
5947
5948 let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
5949 let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
5950 assert_eq!(
5951 actual,
5952 vec![
5953 Some(b"hiiii".as_slice()),
5954 Some(b"hiiii".as_slice()),
5955 None,
5956 Some(b"rustt".as_slice())
5957 ]
5958 );
5959
5960 let keys = dict_array.keys();
5961 assert!(keys.is_null(2));
5962 assert_eq!(keys.value(0), keys.value(1));
5963 assert_ne!(keys.value(0), keys.value(3));
5964 }
5965
5966 #[test]
5967 fn test_cast_string_array_to_dict_utf8_view_key_overflow_u8() {
5968 let array = StringArray::from_iter_values((0..257).map(|i| format!("v{i}")));
5969
5970 let cast_type =
5971 DataType::Dictionary(Box::new(DataType::UInt8), Box::new(DataType::Utf8View));
5972 assert!(can_cast_types(array.data_type(), &cast_type));
5973 let err = cast(&array, &cast_type).unwrap_err();
5974 assert!(matches!(err, ArrowError::DictionaryKeyOverflowError));
5975 }
5976
5977 #[test]
5978 fn test_cast_large_string_array_to_dict_utf8_view() {
5979 let array = LargeStringArray::from(vec![Some("one"), None, Some("three"), Some("one")]);
5980
5981 let cast_type =
5982 DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
5983 assert!(can_cast_types(array.data_type(), &cast_type));
5984 let cast_array = cast(&array, &cast_type).unwrap();
5985 assert_eq!(cast_array.data_type(), &cast_type);
5986
5987 let dict_array = cast_array.as_dictionary::<UInt16Type>();
5988 assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
5989 assert_eq!(dict_array.values().len(), 2); let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
5992 let actual: Vec<Option<&str>> = typed.into_iter().collect();
5993 assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
5994
5995 let keys = dict_array.keys();
5996 assert!(keys.is_null(1));
5997 assert_eq!(keys.value(0), keys.value(3));
5998 assert_ne!(keys.value(0), keys.value(2));
5999 }
6000
6001 #[test]
6002 fn test_cast_large_binary_array_to_dict_binary_view() {
6003 let mut builder = GenericBinaryBuilder::<i64>::new();
6004 builder.append_value(b"hello");
6005 builder.append_value(b"world");
6006 builder.append_value(b"hello"); builder.append_null();
6008
6009 let array = builder.finish();
6010
6011 let cast_type =
6012 DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6013 assert!(can_cast_types(array.data_type(), &cast_type));
6014 let cast_array = cast(&array, &cast_type).unwrap();
6015 assert_eq!(cast_array.data_type(), &cast_type);
6016
6017 let dict_array = cast_array.as_dictionary::<UInt16Type>();
6018 assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
6019 assert_eq!(dict_array.values().len(), 2); let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
6022 let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6023 assert_eq!(
6024 actual,
6025 vec![
6026 Some(b"hello".as_slice()),
6027 Some(b"world".as_slice()),
6028 Some(b"hello".as_slice()),
6029 None
6030 ]
6031 );
6032
6033 let keys = dict_array.keys();
6034 assert!(keys.is_null(3));
6035 assert_eq!(keys.value(0), keys.value(2));
6036 assert_ne!(keys.value(0), keys.value(1));
6037 }
6038
6039 #[test]
6040 fn test_cast_empty_string_array_to_dict_utf8_view() {
6041 let array = StringArray::from(Vec::<Option<&str>>::new());
6042
6043 let cast_type =
6044 DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6045 assert!(can_cast_types(array.data_type(), &cast_type));
6046 let cast_array = cast(&array, &cast_type).unwrap();
6047 assert_eq!(cast_array.data_type(), &cast_type);
6048 assert_eq!(cast_array.len(), 0);
6049 }
6050
6051 #[test]
6052 fn test_cast_empty_binary_array_to_dict_binary_view() {
6053 let array = BinaryArray::from(Vec::<Option<&[u8]>>::new());
6054
6055 let cast_type =
6056 DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6057 assert!(can_cast_types(array.data_type(), &cast_type));
6058 let cast_array = cast(&array, &cast_type).unwrap();
6059 assert_eq!(cast_array.data_type(), &cast_type);
6060 assert_eq!(cast_array.len(), 0);
6061 }
6062
6063 #[test]
6064 fn test_cast_all_null_string_array_to_dict_utf8_view() {
6065 let array = StringArray::from(vec![None::<&str>, None, None]);
6066
6067 let cast_type =
6068 DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6069 assert!(can_cast_types(array.data_type(), &cast_type));
6070 let cast_array = cast(&array, &cast_type).unwrap();
6071 assert_eq!(cast_array.data_type(), &cast_type);
6072 assert_eq!(cast_array.null_count(), 3);
6073
6074 let dict_array = cast_array.as_dictionary::<UInt16Type>();
6075 assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6076 assert_eq!(dict_array.values().len(), 0);
6077 assert_eq!(dict_array.keys().null_count(), 3);
6078
6079 let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6080 let actual: Vec<Option<&str>> = typed.into_iter().collect();
6081 assert_eq!(actual, vec![None, None, None]);
6082 }
6083
6084 #[test]
6085 fn test_cast_all_null_binary_array_to_dict_binary_view() {
6086 let array = BinaryArray::from(vec![None::<&[u8]>, None, None]);
6087
6088 let cast_type =
6089 DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6090 assert!(can_cast_types(array.data_type(), &cast_type));
6091 let cast_array = cast(&array, &cast_type).unwrap();
6092 assert_eq!(cast_array.data_type(), &cast_type);
6093 assert_eq!(cast_array.null_count(), 3);
6094
6095 let dict_array = cast_array.as_dictionary::<UInt16Type>();
6096 assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
6097 assert_eq!(dict_array.values().len(), 0);
6098 assert_eq!(dict_array.keys().null_count(), 3);
6099
6100 let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
6101 let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6102 assert_eq!(actual, vec![None, None, None]);
6103 }
6104
6105 #[test]
6106 fn test_numeric_to_binary() {
6107 let a = Int16Array::from(vec![Some(1), Some(511), None]);
6108
6109 let array_ref = cast(&a, &DataType::Binary).unwrap();
6110 let down_cast = array_ref.as_binary::<i32>();
6111 assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
6112 assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
6113 assert!(down_cast.is_null(2));
6114
6115 let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
6116
6117 let array_ref = cast(&a, &DataType::Binary).unwrap();
6118 let down_cast = array_ref.as_binary::<i32>();
6119 assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
6120 assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
6121 assert!(down_cast.is_null(2));
6122 }
6123
6124 #[test]
6125 fn test_numeric_to_large_binary() {
6126 let a = Int16Array::from(vec![Some(1), Some(511), None]);
6127
6128 let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
6129 let down_cast = array_ref.as_binary::<i64>();
6130 assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
6131 assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
6132 assert!(down_cast.is_null(2));
6133
6134 let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
6135
6136 let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
6137 let down_cast = array_ref.as_binary::<i64>();
6138 assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
6139 assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
6140 assert!(down_cast.is_null(2));
6141 }
6142
6143 #[test]
6144 fn test_cast_date32_to_int32() {
6145 let array = Date32Array::from(vec![10000, 17890]);
6146 let b = cast(&array, &DataType::Int32).unwrap();
6147 let c = b.as_primitive::<Int32Type>();
6148 assert_eq!(10000, c.value(0));
6149 assert_eq!(17890, c.value(1));
6150 }
6151
6152 #[test]
6153 fn test_cast_int32_to_date32() {
6154 let array = Int32Array::from(vec![10000, 17890]);
6155 let b = cast(&array, &DataType::Date32).unwrap();
6156 let c = b.as_primitive::<Date32Type>();
6157 assert_eq!(10000, c.value(0));
6158 assert_eq!(17890, c.value(1));
6159 }
6160
6161 #[test]
6162 fn test_cast_timestamp_to_date32() {
6163 let array =
6164 TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
6165 .with_timezone("+00:00".to_string());
6166 let b = cast(&array, &DataType::Date32).unwrap();
6167 let c = b.as_primitive::<Date32Type>();
6168 assert_eq!(10000, c.value(0));
6169 assert_eq!(17890, c.value(1));
6170 assert!(c.is_null(2));
6171 }
6172 #[test]
6173 fn test_cast_timestamp_to_date32_zone() {
6174 let strings = StringArray::from_iter([
6175 Some("1970-01-01T00:00:01"),
6176 Some("1970-01-01T23:59:59"),
6177 None,
6178 Some("2020-03-01T02:00:23+00:00"),
6179 ]);
6180 let dt = DataType::Timestamp(TimeUnit::Millisecond, Some("-07:00".into()));
6181 let timestamps = cast(&strings, &dt).unwrap();
6182 let dates = cast(timestamps.as_ref(), &DataType::Date32).unwrap();
6183
6184 let c = dates.as_primitive::<Date32Type>();
6185 let expected = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap();
6186 assert_eq!(c.value_as_date(0).unwrap(), expected);
6187 assert_eq!(c.value_as_date(1).unwrap(), expected);
6188 assert!(c.is_null(2));
6189 let expected = NaiveDate::from_ymd_opt(2020, 2, 29).unwrap();
6190 assert_eq!(c.value_as_date(3).unwrap(), expected);
6191 }
6192 #[test]
6193 fn test_cast_timestamp_to_date64() {
6194 let array =
6195 TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None]);
6196 let b = cast(&array, &DataType::Date64).unwrap();
6197 let c = b.as_primitive::<Date64Type>();
6198 assert_eq!(864000000005, c.value(0));
6199 assert_eq!(1545696000001, c.value(1));
6200 assert!(c.is_null(2));
6201
6202 let array = TimestampSecondArray::from(vec![Some(864000000005), Some(1545696000001)]);
6203 let b = cast(&array, &DataType::Date64).unwrap();
6204 let c = b.as_primitive::<Date64Type>();
6205 assert_eq!(864000000005000, c.value(0));
6206 assert_eq!(1545696000001000, c.value(1));
6207
6208 let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
6210 let b = cast(&array, &DataType::Date64).unwrap();
6211 assert!(b.is_null(0));
6212 let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
6214 let options = CastOptions {
6215 safe: false,
6216 format_options: FormatOptions::default(),
6217 };
6218 let b = cast_with_options(&array, &DataType::Date64, &options);
6219 assert!(b.is_err());
6220 }
6221
6222 #[test]
6223 fn test_cast_timestamp_to_time64() {
6224 let array = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
6226 .with_timezone("+01:00".to_string());
6227 let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6228 let c = b.as_primitive::<Time64MicrosecondType>();
6229 assert_eq!(3605000000, c.value(0));
6230 assert_eq!(3601000000, c.value(1));
6231 assert!(c.is_null(2));
6232 let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6233 let c = b.as_primitive::<Time64NanosecondType>();
6234 assert_eq!(3605000000000, c.value(0));
6235 assert_eq!(3601000000000, c.value(1));
6236 assert!(c.is_null(2));
6237
6238 let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
6240 .with_timezone("+01:00".to_string());
6241 let array = Arc::new(a) as ArrayRef;
6242 let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6243 let c = b.as_primitive::<Time64MicrosecondType>();
6244 assert_eq!(3605000000, c.value(0));
6245 assert_eq!(3601000000, c.value(1));
6246 assert!(c.is_null(2));
6247 let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6248 let c = b.as_primitive::<Time64NanosecondType>();
6249 assert_eq!(3605000000000, c.value(0));
6250 assert_eq!(3601000000000, c.value(1));
6251 assert!(c.is_null(2));
6252
6253 let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
6255 .with_timezone("+01:00".to_string());
6256 let array = Arc::new(a) as ArrayRef;
6257 let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6258 let c = b.as_primitive::<Time64MicrosecondType>();
6259 assert_eq!(3605000000, c.value(0));
6260 assert_eq!(3601000000, c.value(1));
6261 assert!(c.is_null(2));
6262 let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6263 let c = b.as_primitive::<Time64NanosecondType>();
6264 assert_eq!(3605000000000, c.value(0));
6265 assert_eq!(3601000000000, c.value(1));
6266 assert!(c.is_null(2));
6267
6268 let a = TimestampNanosecondArray::from(vec![Some(86405000000000), Some(1000000000), None])
6270 .with_timezone("+01:00".to_string());
6271 let array = Arc::new(a) as ArrayRef;
6272 let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6273 let c = b.as_primitive::<Time64MicrosecondType>();
6274 assert_eq!(3605000000, c.value(0));
6275 assert_eq!(3601000000, c.value(1));
6276 assert!(c.is_null(2));
6277 let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6278 let c = b.as_primitive::<Time64NanosecondType>();
6279 assert_eq!(3605000000000, c.value(0));
6280 assert_eq!(3601000000000, c.value(1));
6281 assert!(c.is_null(2));
6282
6283 let a =
6285 TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
6286 let array = Arc::new(a) as ArrayRef;
6287 let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond));
6288 assert!(b.is_err());
6289 let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond));
6290 assert!(b.is_err());
6291 let b = cast(&array, &DataType::Time64(TimeUnit::Millisecond));
6292 assert!(b.is_err());
6293 }
6294
6295 #[test]
6296 fn test_cast_timestamp_to_time32() {
6297 let a = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
6299 .with_timezone("+01:00".to_string());
6300 let array = Arc::new(a) as ArrayRef;
6301 let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6302 let c = b.as_primitive::<Time32SecondType>();
6303 assert_eq!(3605, c.value(0));
6304 assert_eq!(3601, c.value(1));
6305 assert!(c.is_null(2));
6306 let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6307 let c = b.as_primitive::<Time32MillisecondType>();
6308 assert_eq!(3605000, c.value(0));
6309 assert_eq!(3601000, c.value(1));
6310 assert!(c.is_null(2));
6311
6312 let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
6314 .with_timezone("+01:00".to_string());
6315 let array = Arc::new(a) as ArrayRef;
6316 let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6317 let c = b.as_primitive::<Time32SecondType>();
6318 assert_eq!(3605, c.value(0));
6319 assert_eq!(3601, c.value(1));
6320 assert!(c.is_null(2));
6321 let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6322 let c = b.as_primitive::<Time32MillisecondType>();
6323 assert_eq!(3605000, c.value(0));
6324 assert_eq!(3601000, c.value(1));
6325 assert!(c.is_null(2));
6326
6327 let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
6329 .with_timezone("+01:00".to_string());
6330 let array = Arc::new(a) as ArrayRef;
6331 let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6332 let c = b.as_primitive::<Time32SecondType>();
6333 assert_eq!(3605, c.value(0));
6334 assert_eq!(3601, c.value(1));
6335 assert!(c.is_null(2));
6336 let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6337 let c = b.as_primitive::<Time32MillisecondType>();
6338 assert_eq!(3605000, c.value(0));
6339 assert_eq!(3601000, c.value(1));
6340 assert!(c.is_null(2));
6341
6342 let a = TimestampNanosecondArray::from(vec![Some(86405000000000), Some(1000000000), None])
6344 .with_timezone("+01:00".to_string());
6345 let array = Arc::new(a) as ArrayRef;
6346 let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6347 let c = b.as_primitive::<Time32SecondType>();
6348 assert_eq!(3605, c.value(0));
6349 assert_eq!(3601, c.value(1));
6350 assert!(c.is_null(2));
6351 let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6352 let c = b.as_primitive::<Time32MillisecondType>();
6353 assert_eq!(3605000, c.value(0));
6354 assert_eq!(3601000, c.value(1));
6355 assert!(c.is_null(2));
6356
6357 let a =
6359 TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
6360 let array = Arc::new(a) as ArrayRef;
6361 let b = cast(&array, &DataType::Time32(TimeUnit::Second));
6362 assert!(b.is_err());
6363 let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond));
6364 assert!(b.is_err());
6365 }
6366
6367 #[test]
6369 fn test_cast_timestamp_with_timezone_1() {
6370 let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
6371 Some("2000-01-01T00:00:00.123456789"),
6372 Some("2010-01-01T00:00:00.123456789"),
6373 None,
6374 ]));
6375 let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
6376 let timestamp_array = cast(&string_array, &to_type).unwrap();
6377
6378 let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
6379 let timestamp_array = cast(×tamp_array, &to_type).unwrap();
6380
6381 let string_array = cast(×tamp_array, &DataType::Utf8).unwrap();
6382 let result = string_array.as_string::<i32>();
6383 assert_eq!("2000-01-01T00:00:00.123456+07:00", result.value(0));
6384 assert_eq!("2010-01-01T00:00:00.123456+07:00", result.value(1));
6385 assert!(result.is_null(2));
6386 }
6387
6388 #[test]
6390 fn test_cast_timestamp_with_timezone_2() {
6391 let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
6392 Some("2000-01-01T07:00:00.123456789"),
6393 Some("2010-01-01T07:00:00.123456789"),
6394 None,
6395 ]));
6396 let to_type = DataType::Timestamp(TimeUnit::Millisecond, Some("+0700".into()));
6397 let timestamp_array = cast(&string_array, &to_type).unwrap();
6398
6399 let string_array = cast(×tamp_array, &DataType::Utf8).unwrap();
6401 let result = string_array.as_string::<i32>();
6402 assert_eq!("2000-01-01T07:00:00.123+07:00", result.value(0));
6403 assert_eq!("2010-01-01T07:00:00.123+07:00", result.value(1));
6404 assert!(result.is_null(2));
6405
6406 let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
6407 let timestamp_array = cast(×tamp_array, &to_type).unwrap();
6408
6409 let string_array = cast(×tamp_array, &DataType::Utf8).unwrap();
6410 let result = string_array.as_string::<i32>();
6411 assert_eq!("2000-01-01T00:00:00.123", result.value(0));
6412 assert_eq!("2010-01-01T00:00:00.123", result.value(1));
6413 assert!(result.is_null(2));
6414 }
6415
6416 #[test]
6418 fn test_cast_timestamp_with_timezone_3() {
6419 let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
6420 Some("2000-01-01T07:00:00.123456789"),
6421 Some("2010-01-01T07:00:00.123456789"),
6422 None,
6423 ]));
6424 let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
6425 let timestamp_array = cast(&string_array, &to_type).unwrap();
6426
6427 let string_array = cast(×tamp_array, &DataType::Utf8).unwrap();
6429 let result = string_array.as_string::<i32>();
6430 assert_eq!("2000-01-01T07:00:00.123456+07:00", result.value(0));
6431 assert_eq!("2010-01-01T07:00:00.123456+07:00", result.value(1));
6432 assert!(result.is_null(2));
6433
6434 let to_type = DataType::Timestamp(TimeUnit::Second, Some("-08:00".into()));
6435 let timestamp_array = cast(×tamp_array, &to_type).unwrap();
6436
6437 let string_array = cast(×tamp_array, &DataType::Utf8).unwrap();
6438 let result = string_array.as_string::<i32>();
6439 assert_eq!("1999-12-31T16:00:00-08:00", result.value(0));
6440 assert_eq!("2009-12-31T16:00:00-08:00", result.value(1));
6441 assert!(result.is_null(2));
6442 }
6443
6444 #[test]
6445 fn test_cast_date64_to_timestamp() {
6446 let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6447 let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
6448 let c = b.as_primitive::<TimestampSecondType>();
6449 assert_eq!(864000000, c.value(0));
6450 assert_eq!(1545696000, c.value(1));
6451 assert!(c.is_null(2));
6452 }
6453
6454 #[test]
6455 fn test_cast_date64_to_timestamp_ms() {
6456 let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6457 let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
6458 let c = b
6459 .as_any()
6460 .downcast_ref::<TimestampMillisecondArray>()
6461 .unwrap();
6462 assert_eq!(864000000005, c.value(0));
6463 assert_eq!(1545696000001, c.value(1));
6464 assert!(c.is_null(2));
6465 }
6466
6467 #[test]
6468 fn test_cast_date64_to_timestamp_us() {
6469 let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6470 let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
6471 let c = b
6472 .as_any()
6473 .downcast_ref::<TimestampMicrosecondArray>()
6474 .unwrap();
6475 assert_eq!(864000000005000, c.value(0));
6476 assert_eq!(1545696000001000, c.value(1));
6477 assert!(c.is_null(2));
6478 }
6479
6480 #[test]
6481 fn test_cast_date64_to_timestamp_ns() {
6482 let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6483 let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
6484 let c = b
6485 .as_any()
6486 .downcast_ref::<TimestampNanosecondArray>()
6487 .unwrap();
6488 assert_eq!(864000000005000000, c.value(0));
6489 assert_eq!(1545696000001000000, c.value(1));
6490 assert!(c.is_null(2));
6491 }
6492
6493 #[test]
6494 fn test_cast_timestamp_to_i64() {
6495 let array =
6496 TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
6497 .with_timezone("UTC".to_string());
6498 let b = cast(&array, &DataType::Int64).unwrap();
6499 let c = b.as_primitive::<Int64Type>();
6500 assert_eq!(&DataType::Int64, c.data_type());
6501 assert_eq!(864000000005, c.value(0));
6502 assert_eq!(1545696000001, c.value(1));
6503 assert!(c.is_null(2));
6504 }
6505
6506 macro_rules! assert_cast {
6507 ($array:expr, $datatype:expr, $output_array_type: ty, $expected:expr) => {{
6508 assert!(can_cast_types($array.data_type(), &$datatype));
6509 let out = cast(&$array, &$datatype).unwrap();
6510 let actual = out
6511 .as_any()
6512 .downcast_ref::<$output_array_type>()
6513 .unwrap()
6514 .into_iter()
6515 .collect::<Vec<_>>();
6516 assert_eq!(actual, $expected);
6517 }};
6518 ($array:expr, $datatype:expr, $output_array_type: ty, $options:expr, $expected:expr) => {{
6519 assert!(can_cast_types($array.data_type(), &$datatype));
6520 let out = cast_with_options(&$array, &$datatype, &$options).unwrap();
6521 let actual = out
6522 .as_any()
6523 .downcast_ref::<$output_array_type>()
6524 .unwrap()
6525 .into_iter()
6526 .collect::<Vec<_>>();
6527 assert_eq!(actual, $expected);
6528 }};
6529 }
6530
6531 #[test]
6532 fn test_cast_date32_to_string() {
6533 let array = Date32Array::from(vec![Some(0), Some(10000), Some(13036), Some(17890), None]);
6534 let expected = vec![
6535 Some("1970-01-01"),
6536 Some("1997-05-19"),
6537 Some("2005-09-10"),
6538 Some("2018-12-25"),
6539 None,
6540 ];
6541
6542 assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
6543 assert_cast!(array, DataType::Utf8, StringArray, expected);
6544 assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
6545 }
6546
6547 #[test]
6548 fn test_cast_date64_to_string() {
6549 let array = Date64Array::from(vec![
6550 Some(0),
6551 Some(10000 * 86400000),
6552 Some(13036 * 86400000),
6553 Some(17890 * 86400000),
6554 None,
6555 ]);
6556 let expected = vec![
6557 Some("1970-01-01T00:00:00"),
6558 Some("1997-05-19T00:00:00"),
6559 Some("2005-09-10T00:00:00"),
6560 Some("2018-12-25T00:00:00"),
6561 None,
6562 ];
6563
6564 assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
6565 assert_cast!(array, DataType::Utf8, StringArray, expected);
6566 assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
6567 }
6568
6569 #[test]
6570 fn test_cast_date32_to_timestamp_and_timestamp_with_timezone() {
6571 let tz = "+0545"; let a = Date32Array::from(vec![Some(18628), None, None]); let array = Arc::new(a) as ArrayRef;
6574
6575 let b = cast(
6576 &array,
6577 &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
6578 )
6579 .unwrap();
6580 let c = b.as_primitive::<TimestampSecondType>();
6581 let string_array = cast(&c, &DataType::Utf8).unwrap();
6582 let result = string_array.as_string::<i32>();
6583 assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6584
6585 let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
6586 let c = b.as_primitive::<TimestampSecondType>();
6587 let string_array = cast(&c, &DataType::Utf8).unwrap();
6588 let result = string_array.as_string::<i32>();
6589 assert_eq!("2021-01-01T00:00:00", result.value(0));
6590 }
6591
6592 #[test]
6593 fn test_cast_date32_to_timestamp_with_timezone() {
6594 let tz = "+0545"; let a = Date32Array::from(vec![Some(18628), Some(18993), None]); let array = Arc::new(a) as ArrayRef;
6597 let b = cast(
6598 &array,
6599 &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
6600 )
6601 .unwrap();
6602 let c = b.as_primitive::<TimestampSecondType>();
6603 assert_eq!(1609438500, c.value(0));
6604 assert_eq!(1640974500, c.value(1));
6605 assert!(c.is_null(2));
6606
6607 let string_array = cast(&c, &DataType::Utf8).unwrap();
6608 let result = string_array.as_string::<i32>();
6609 assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6610 assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
6611 }
6612
6613 #[test]
6614 fn test_cast_date32_to_timestamp_with_timezone_ms() {
6615 let tz = "+0545"; let a = Date32Array::from(vec![Some(18628), Some(18993), None]); let array = Arc::new(a) as ArrayRef;
6618 let b = cast(
6619 &array,
6620 &DataType::Timestamp(TimeUnit::Millisecond, Some(tz.into())),
6621 )
6622 .unwrap();
6623 let c = b.as_primitive::<TimestampMillisecondType>();
6624 assert_eq!(1609438500000, c.value(0));
6625 assert_eq!(1640974500000, c.value(1));
6626 assert!(c.is_null(2));
6627
6628 let string_array = cast(&c, &DataType::Utf8).unwrap();
6629 let result = string_array.as_string::<i32>();
6630 assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6631 assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
6632 }
6633
6634 #[test]
6635 fn test_cast_date32_to_timestamp_with_timezone_us() {
6636 let tz = "+0545"; let a = Date32Array::from(vec![Some(18628), Some(18993), None]); let array = Arc::new(a) as ArrayRef;
6639 let b = cast(
6640 &array,
6641 &DataType::Timestamp(TimeUnit::Microsecond, Some(tz.into())),
6642 )
6643 .unwrap();
6644 let c = b.as_primitive::<TimestampMicrosecondType>();
6645 assert_eq!(1609438500000000, c.value(0));
6646 assert_eq!(1640974500000000, c.value(1));
6647 assert!(c.is_null(2));
6648
6649 let string_array = cast(&c, &DataType::Utf8).unwrap();
6650 let result = string_array.as_string::<i32>();
6651 assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6652 assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
6653 }
6654
6655 #[test]
6656 fn test_cast_date32_to_timestamp_with_timezone_ns() {
6657 let tz = "+0545"; let a = Date32Array::from(vec![Some(18628), Some(18993), None]); let array = Arc::new(a) as ArrayRef;
6660 let b = cast(
6661 &array,
6662 &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.into())),
6663 )
6664 .unwrap();
6665 let c = b.as_primitive::<TimestampNanosecondType>();
6666 assert_eq!(1609438500000000000, c.value(0));
6667 assert_eq!(1640974500000000000, c.value(1));
6668 assert!(c.is_null(2));
6669
6670 let string_array = cast(&c, &DataType::Utf8).unwrap();
6671 let result = string_array.as_string::<i32>();
6672 assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6673 assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
6674 }
6675
6676 #[test]
6677 fn test_cast_date64_to_timestamp_with_timezone() {
6678 let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6679 let tz = "+0545"; let b = cast(
6681 &array,
6682 &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
6683 )
6684 .unwrap();
6685
6686 let c = b.as_primitive::<TimestampSecondType>();
6687 assert_eq!(863979300, c.value(0));
6688 assert_eq!(1545675300, c.value(1));
6689 assert!(c.is_null(2));
6690
6691 let string_array = cast(&c, &DataType::Utf8).unwrap();
6692 let result = string_array.as_string::<i32>();
6693 assert_eq!("1997-05-19T00:00:00+05:45", result.value(0));
6694 assert_eq!("2018-12-25T00:00:00+05:45", result.value(1));
6695 }
6696
6697 #[test]
6698 fn test_cast_date64_to_timestamp_with_timezone_ms() {
6699 let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6700 let tz = "+0545"; let b = cast(
6702 &array,
6703 &DataType::Timestamp(TimeUnit::Millisecond, Some(tz.into())),
6704 )
6705 .unwrap();
6706
6707 let c = b.as_primitive::<TimestampMillisecondType>();
6708 assert_eq!(863979300005, c.value(0));
6709 assert_eq!(1545675300001, c.value(1));
6710 assert!(c.is_null(2));
6711
6712 let string_array = cast(&c, &DataType::Utf8).unwrap();
6713 let result = string_array.as_string::<i32>();
6714 assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
6715 assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
6716 }
6717
6718 #[test]
6719 fn test_cast_date64_to_timestamp_with_timezone_us() {
6720 let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6721 let tz = "+0545"; let b = cast(
6723 &array,
6724 &DataType::Timestamp(TimeUnit::Microsecond, Some(tz.into())),
6725 )
6726 .unwrap();
6727
6728 let c = b.as_primitive::<TimestampMicrosecondType>();
6729 assert_eq!(863979300005000, c.value(0));
6730 assert_eq!(1545675300001000, c.value(1));
6731 assert!(c.is_null(2));
6732
6733 let string_array = cast(&c, &DataType::Utf8).unwrap();
6734 let result = string_array.as_string::<i32>();
6735 assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
6736 assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
6737 }
6738
6739 #[test]
6740 fn test_cast_date64_to_timestamp_with_timezone_ns() {
6741 let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6742 let tz = "+0545"; let b = cast(
6744 &array,
6745 &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.into())),
6746 )
6747 .unwrap();
6748
6749 let c = b.as_primitive::<TimestampNanosecondType>();
6750 assert_eq!(863979300005000000, c.value(0));
6751 assert_eq!(1545675300001000000, c.value(1));
6752 assert!(c.is_null(2));
6753
6754 let string_array = cast(&c, &DataType::Utf8).unwrap();
6755 let result = string_array.as_string::<i32>();
6756 assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
6757 assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
6758 }
6759
6760 #[test]
6761 fn test_cast_timestamp_to_strings() {
6762 let array =
6764 TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
6765 let expected = vec![
6766 Some("1997-05-19T00:00:03.005"),
6767 Some("2018-12-25T00:00:02.001"),
6768 None,
6769 ];
6770
6771 assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
6772 assert_cast!(array, DataType::Utf8, StringArray, expected);
6773 assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
6774 }
6775
6776 #[test]
6777 fn test_cast_timestamp_to_strings_opt() {
6778 let ts_format = "%Y-%m-%d %H:%M:%S%.6f";
6779 let tz = "+0545"; let cast_options = CastOptions {
6781 safe: true,
6782 format_options: FormatOptions::default()
6783 .with_timestamp_format(Some(ts_format))
6784 .with_timestamp_tz_format(Some(ts_format)),
6785 };
6786
6787 let array_without_tz =
6789 TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
6790 let expected = vec![
6791 Some("1997-05-19 00:00:03.005000"),
6792 Some("2018-12-25 00:00:02.001000"),
6793 None,
6794 ];
6795 assert_cast!(
6796 array_without_tz,
6797 DataType::Utf8View,
6798 StringViewArray,
6799 cast_options,
6800 expected
6801 );
6802 assert_cast!(
6803 array_without_tz,
6804 DataType::Utf8,
6805 StringArray,
6806 cast_options,
6807 expected
6808 );
6809 assert_cast!(
6810 array_without_tz,
6811 DataType::LargeUtf8,
6812 LargeStringArray,
6813 cast_options,
6814 expected
6815 );
6816
6817 let array_with_tz =
6818 TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None])
6819 .with_timezone(tz.to_string());
6820 let expected = vec![
6821 Some("1997-05-19 05:45:03.005000"),
6822 Some("2018-12-25 05:45:02.001000"),
6823 None,
6824 ];
6825 assert_cast!(
6826 array_with_tz,
6827 DataType::Utf8View,
6828 StringViewArray,
6829 cast_options,
6830 expected
6831 );
6832 assert_cast!(
6833 array_with_tz,
6834 DataType::Utf8,
6835 StringArray,
6836 cast_options,
6837 expected
6838 );
6839 assert_cast!(
6840 array_with_tz,
6841 DataType::LargeUtf8,
6842 LargeStringArray,
6843 cast_options,
6844 expected
6845 );
6846 }
6847
6848 #[test]
6849 fn test_cast_between_timestamps() {
6850 let array =
6851 TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
6852 let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
6853 let c = b.as_primitive::<TimestampSecondType>();
6854 assert_eq!(864000003, c.value(0));
6855 assert_eq!(1545696002, c.value(1));
6856 assert!(c.is_null(2));
6857 }
6858
6859 #[test]
6860 fn test_cast_duration_to_i64() {
6861 let base = vec![5, 6, 7, 8, 100000000];
6862
6863 let duration_arrays = vec![
6864 Arc::new(DurationNanosecondArray::from(base.clone())) as ArrayRef,
6865 Arc::new(DurationMicrosecondArray::from(base.clone())) as ArrayRef,
6866 Arc::new(DurationMillisecondArray::from(base.clone())) as ArrayRef,
6867 Arc::new(DurationSecondArray::from(base.clone())) as ArrayRef,
6868 ];
6869
6870 for arr in duration_arrays {
6871 assert!(can_cast_types(arr.data_type(), &DataType::Int64));
6872 let result = cast(&arr, &DataType::Int64).unwrap();
6873 let result = result.as_primitive::<Int64Type>();
6874 assert_eq!(base.as_slice(), result.values());
6875 }
6876 }
6877
6878 #[test]
6879 fn test_cast_between_durations_and_numerics() {
6880 fn test_cast_between_durations<FromType, ToType>()
6881 where
6882 FromType: ArrowPrimitiveType<Native = i64>,
6883 ToType: ArrowPrimitiveType<Native = i64>,
6884 PrimitiveArray<FromType>: From<Vec<Option<i64>>>,
6885 {
6886 let from_unit = match FromType::DATA_TYPE {
6887 DataType::Duration(unit) => unit,
6888 _ => panic!("Expected a duration type"),
6889 };
6890 let to_unit = match ToType::DATA_TYPE {
6891 DataType::Duration(unit) => unit,
6892 _ => panic!("Expected a duration type"),
6893 };
6894 let from_size = time_unit_multiple(&from_unit);
6895 let to_size = time_unit_multiple(&to_unit);
6896
6897 let (v1_before, v2_before) = (8640003005, 1696002001);
6898 let (v1_after, v2_after) = if from_size >= to_size {
6899 (
6900 v1_before / (from_size / to_size),
6901 v2_before / (from_size / to_size),
6902 )
6903 } else {
6904 (
6905 v1_before * (to_size / from_size),
6906 v2_before * (to_size / from_size),
6907 )
6908 };
6909
6910 let array =
6911 PrimitiveArray::<FromType>::from(vec![Some(v1_before), Some(v2_before), None]);
6912 let b = cast(&array, &ToType::DATA_TYPE).unwrap();
6913 let c = b.as_primitive::<ToType>();
6914 assert_eq!(v1_after, c.value(0));
6915 assert_eq!(v2_after, c.value(1));
6916 assert!(c.is_null(2));
6917 }
6918
6919 test_cast_between_durations::<DurationSecondType, DurationMillisecondType>();
6921 test_cast_between_durations::<DurationSecondType, DurationMicrosecondType>();
6922 test_cast_between_durations::<DurationSecondType, DurationNanosecondType>();
6923 test_cast_between_durations::<DurationMillisecondType, DurationSecondType>();
6924 test_cast_between_durations::<DurationMillisecondType, DurationMicrosecondType>();
6925 test_cast_between_durations::<DurationMillisecondType, DurationNanosecondType>();
6926 test_cast_between_durations::<DurationMicrosecondType, DurationSecondType>();
6927 test_cast_between_durations::<DurationMicrosecondType, DurationMillisecondType>();
6928 test_cast_between_durations::<DurationMicrosecondType, DurationNanosecondType>();
6929 test_cast_between_durations::<DurationNanosecondType, DurationSecondType>();
6930 test_cast_between_durations::<DurationNanosecondType, DurationMillisecondType>();
6931 test_cast_between_durations::<DurationNanosecondType, DurationMicrosecondType>();
6932
6933 let array = DurationSecondArray::from(vec![
6935 Some(i64::MAX),
6936 Some(8640203410378005),
6937 Some(10241096),
6938 None,
6939 ]);
6940 let b = cast(&array, &DataType::Duration(TimeUnit::Nanosecond)).unwrap();
6941 let c = b.as_primitive::<DurationNanosecondType>();
6942 assert!(c.is_null(0));
6943 assert!(c.is_null(1));
6944 assert_eq!(10241096000000000, c.value(2));
6945 assert!(c.is_null(3));
6946
6947 let array = DurationSecondArray::from(vec![
6949 Some(i64::MAX),
6950 Some(8640203410378005),
6951 Some(10241096),
6952 None,
6953 ]);
6954 let b = cast(&array, &DataType::Int64).unwrap();
6955 let c = b.as_primitive::<Int64Type>();
6956 assert_eq!(i64::MAX, c.value(0));
6957 assert_eq!(8640203410378005, c.value(1));
6958 assert_eq!(10241096, c.value(2));
6959 assert!(c.is_null(3));
6960
6961 let b = cast(&array, &DataType::Int32).unwrap();
6962 let c = b.as_primitive::<Int32Type>();
6963 assert_eq!(0, c.value(0));
6964 assert_eq!(0, c.value(1));
6965 assert_eq!(10241096, c.value(2));
6966 assert!(c.is_null(3));
6967
6968 let array = Int32Array::from(vec![Some(i32::MAX), Some(802034103), Some(10241096), None]);
6970 let b = cast(&array, &DataType::Duration(TimeUnit::Second)).unwrap();
6971 let c = b.as_any().downcast_ref::<DurationSecondArray>().unwrap();
6972 assert_eq!(i32::MAX as i64, c.value(0));
6973 assert_eq!(802034103, c.value(1));
6974 assert_eq!(10241096, c.value(2));
6975 assert!(c.is_null(3));
6976 }
6977
6978 #[test]
6979 fn test_cast_to_strings() {
6980 let a = Int32Array::from(vec![1, 2, 3]);
6981 let out = cast(&a, &DataType::Utf8).unwrap();
6982 let out = out
6983 .as_any()
6984 .downcast_ref::<StringArray>()
6985 .unwrap()
6986 .into_iter()
6987 .collect::<Vec<_>>();
6988 assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
6989 let out = cast(&a, &DataType::LargeUtf8).unwrap();
6990 let out = out
6991 .as_any()
6992 .downcast_ref::<LargeStringArray>()
6993 .unwrap()
6994 .into_iter()
6995 .collect::<Vec<_>>();
6996 assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
6997 }
6998
6999 #[test]
7000 fn test_str_to_str_casts() {
7001 for data in [
7002 vec![Some("foo"), Some("bar"), Some("ham")],
7003 vec![Some("foo"), None, Some("bar")],
7004 ] {
7005 let a = LargeStringArray::from(data.clone());
7006 let to = cast(&a, &DataType::Utf8).unwrap();
7007 let expect = a
7008 .as_any()
7009 .downcast_ref::<LargeStringArray>()
7010 .unwrap()
7011 .into_iter()
7012 .collect::<Vec<_>>();
7013 let out = to
7014 .as_any()
7015 .downcast_ref::<StringArray>()
7016 .unwrap()
7017 .into_iter()
7018 .collect::<Vec<_>>();
7019 assert_eq!(expect, out);
7020
7021 let a = StringArray::from(data);
7022 let to = cast(&a, &DataType::LargeUtf8).unwrap();
7023 let expect = a
7024 .as_any()
7025 .downcast_ref::<StringArray>()
7026 .unwrap()
7027 .into_iter()
7028 .collect::<Vec<_>>();
7029 let out = to
7030 .as_any()
7031 .downcast_ref::<LargeStringArray>()
7032 .unwrap()
7033 .into_iter()
7034 .collect::<Vec<_>>();
7035 assert_eq!(expect, out);
7036 }
7037 }
7038
7039 const VIEW_TEST_DATA: [Option<&str>; 5] = [
7040 Some("hello"),
7041 Some("repeated"),
7042 None,
7043 Some("large payload over 12 bytes"),
7044 Some("repeated"),
7045 ];
7046
7047 #[test]
7048 fn test_string_view_to_binary_view() {
7049 let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7050
7051 assert!(can_cast_types(
7052 string_view_array.data_type(),
7053 &DataType::BinaryView
7054 ));
7055
7056 let binary_view_array = cast(&string_view_array, &DataType::BinaryView).unwrap();
7057 assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
7058
7059 let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7060 assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
7061 }
7062
7063 #[test]
7064 fn test_binary_view_to_string_view() {
7065 let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7066
7067 assert!(can_cast_types(
7068 binary_view_array.data_type(),
7069 &DataType::Utf8View
7070 ));
7071
7072 let string_view_array = cast(&binary_view_array, &DataType::Utf8View).unwrap();
7073 assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7074
7075 let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7076 assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
7077 }
7078
7079 #[test]
7080 fn test_binary_view_to_string_view_with_invalid_utf8() {
7081 let binary_view_array = BinaryViewArray::from_iter(vec![
7082 Some("valid".as_bytes()),
7083 Some(&[0xff]),
7084 Some("utf8".as_bytes()),
7085 None,
7086 ]);
7087
7088 let strict_options = CastOptions {
7089 safe: false,
7090 ..Default::default()
7091 };
7092
7093 assert!(
7094 cast_with_options(&binary_view_array, &DataType::Utf8View, &strict_options).is_err()
7095 );
7096
7097 let safe_options = CastOptions {
7098 safe: true,
7099 ..Default::default()
7100 };
7101
7102 let string_view_array =
7103 cast_with_options(&binary_view_array, &DataType::Utf8View, &safe_options).unwrap();
7104 assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7105
7106 let values: Vec<_> = string_view_array.as_string_view().iter().collect();
7107
7108 assert_eq!(values, vec![Some("valid"), None, Some("utf8"), None]);
7109 }
7110
7111 #[test]
7112 fn test_string_to_view() {
7113 _test_string_to_view::<i32>();
7114 _test_string_to_view::<i64>();
7115 }
7116
7117 fn _test_string_to_view<O>()
7118 where
7119 O: OffsetSizeTrait,
7120 {
7121 let string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
7122
7123 assert!(can_cast_types(
7124 string_array.data_type(),
7125 &DataType::Utf8View
7126 ));
7127
7128 assert!(can_cast_types(
7129 string_array.data_type(),
7130 &DataType::BinaryView
7131 ));
7132
7133 let string_view_array = cast(&string_array, &DataType::Utf8View).unwrap();
7134 assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7135
7136 let binary_view_array = cast(&string_array, &DataType::BinaryView).unwrap();
7137 assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
7138
7139 let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7140 assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
7141
7142 let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7143 assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
7144 }
7145
7146 #[test]
7147 fn test_bianry_to_view() {
7148 _test_binary_to_view::<i32>();
7149 _test_binary_to_view::<i64>();
7150 }
7151
7152 fn _test_binary_to_view<O>()
7153 where
7154 O: OffsetSizeTrait,
7155 {
7156 let binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
7157
7158 assert!(can_cast_types(
7159 binary_array.data_type(),
7160 &DataType::Utf8View
7161 ));
7162
7163 assert!(can_cast_types(
7164 binary_array.data_type(),
7165 &DataType::BinaryView
7166 ));
7167
7168 let string_view_array = cast(&binary_array, &DataType::Utf8View).unwrap();
7169 assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7170
7171 let binary_view_array = cast(&binary_array, &DataType::BinaryView).unwrap();
7172 assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
7173
7174 let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7175 assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
7176
7177 let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7178 assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
7179 }
7180
7181 #[test]
7182 fn test_dict_to_view() {
7183 let values = StringArray::from_iter(VIEW_TEST_DATA);
7184 let keys = Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
7185 let string_dict_array =
7186 DictionaryArray::<Int8Type>::try_new(keys, Arc::new(values)).unwrap();
7187 let typed_dict = string_dict_array.downcast_dict::<StringArray>().unwrap();
7188
7189 let string_view_array = {
7190 let mut builder = StringViewBuilder::new().with_fixed_block_size(8); for v in typed_dict.into_iter() {
7192 builder.append_option(v);
7193 }
7194 builder.finish()
7195 };
7196 let expected_string_array_type = string_view_array.data_type();
7197 let casted_string_array = cast(&string_dict_array, expected_string_array_type).unwrap();
7198 assert_eq!(casted_string_array.data_type(), expected_string_array_type);
7199 assert_eq!(casted_string_array.as_ref(), &string_view_array);
7200
7201 let binary_buffer = cast(&typed_dict.values(), &DataType::Binary).unwrap();
7202 let binary_dict_array =
7203 DictionaryArray::<Int8Type>::new(typed_dict.keys().clone(), binary_buffer);
7204 let typed_binary_dict = binary_dict_array.downcast_dict::<BinaryArray>().unwrap();
7205
7206 let binary_view_array = {
7207 let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); for v in typed_binary_dict.into_iter() {
7209 builder.append_option(v);
7210 }
7211 builder.finish()
7212 };
7213 let expected_binary_array_type = binary_view_array.data_type();
7214 let casted_binary_array = cast(&binary_dict_array, expected_binary_array_type).unwrap();
7215 assert_eq!(casted_binary_array.data_type(), expected_binary_array_type);
7216 assert_eq!(casted_binary_array.as_ref(), &binary_view_array);
7217 }
7218
7219 #[test]
7220 fn test_view_to_dict() {
7221 let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7222 let string_dict_array: DictionaryArray<Int8Type> = VIEW_TEST_DATA.into_iter().collect();
7223 let casted_type = string_dict_array.data_type();
7224 let casted_dict_array = cast(&string_view_array, casted_type).unwrap();
7225 assert_eq!(casted_dict_array.data_type(), casted_type);
7226 assert_eq!(casted_dict_array.as_ref(), &string_dict_array);
7227
7228 let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7229 let binary_dict_array = string_dict_array.downcast_dict::<StringArray>().unwrap();
7230 let binary_buffer = cast(&binary_dict_array.values(), &DataType::Binary).unwrap();
7231 let binary_dict_array =
7232 DictionaryArray::<Int8Type>::new(binary_dict_array.keys().clone(), binary_buffer);
7233 let casted_type = binary_dict_array.data_type();
7234 let casted_binary_array = cast(&binary_view_array, casted_type).unwrap();
7235 assert_eq!(casted_binary_array.data_type(), casted_type);
7236 assert_eq!(casted_binary_array.as_ref(), &binary_dict_array);
7237 }
7238
7239 #[test]
7240 fn test_view_to_string() {
7241 _test_view_to_string::<i32>();
7242 _test_view_to_string::<i64>();
7243 }
7244
7245 fn _test_view_to_string<O>()
7246 where
7247 O: OffsetSizeTrait,
7248 {
7249 let string_view_array = {
7250 let mut builder = StringViewBuilder::new().with_fixed_block_size(8); for s in VIEW_TEST_DATA.iter() {
7252 builder.append_option(*s);
7253 }
7254 builder.finish()
7255 };
7256
7257 let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7258
7259 let expected_string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
7260 let expected_type = expected_string_array.data_type();
7261
7262 assert!(can_cast_types(string_view_array.data_type(), expected_type));
7263 assert!(can_cast_types(binary_view_array.data_type(), expected_type));
7264
7265 let string_view_casted_array = cast(&string_view_array, expected_type).unwrap();
7266 assert_eq!(string_view_casted_array.data_type(), expected_type);
7267 assert_eq!(string_view_casted_array.as_ref(), &expected_string_array);
7268
7269 let binary_view_casted_array = cast(&binary_view_array, expected_type).unwrap();
7270 assert_eq!(binary_view_casted_array.data_type(), expected_type);
7271 assert_eq!(binary_view_casted_array.as_ref(), &expected_string_array);
7272 }
7273
7274 #[test]
7275 fn test_view_to_binary() {
7276 _test_view_to_binary::<i32>();
7277 _test_view_to_binary::<i64>();
7278 }
7279
7280 fn _test_view_to_binary<O>()
7281 where
7282 O: OffsetSizeTrait,
7283 {
7284 let view_array = {
7285 let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); for s in VIEW_TEST_DATA.iter() {
7287 builder.append_option(*s);
7288 }
7289 builder.finish()
7290 };
7291
7292 let expected_binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
7293 let expected_type = expected_binary_array.data_type();
7294
7295 assert!(can_cast_types(view_array.data_type(), expected_type));
7296
7297 let binary_array = cast(&view_array, expected_type).unwrap();
7298 assert_eq!(binary_array.data_type(), expected_type);
7299
7300 assert_eq!(binary_array.as_ref(), &expected_binary_array);
7301 }
7302
7303 #[test]
7304 fn test_cast_from_f64() {
7305 let f64_values: Vec<f64> = vec![
7306 i64::MIN as f64,
7307 i32::MIN as f64,
7308 i16::MIN as f64,
7309 i8::MIN as f64,
7310 0_f64,
7311 u8::MAX as f64,
7312 u16::MAX as f64,
7313 u32::MAX as f64,
7314 u64::MAX as f64,
7315 ];
7316 let f64_array: ArrayRef = Arc::new(Float64Array::from(f64_values));
7317
7318 let f64_expected = vec![
7319 -9223372036854776000.0,
7320 -2147483648.0,
7321 -32768.0,
7322 -128.0,
7323 0.0,
7324 255.0,
7325 65535.0,
7326 4294967295.0,
7327 18446744073709552000.0,
7328 ];
7329 assert_eq!(
7330 f64_expected,
7331 get_cast_values::<Float64Type>(&f64_array, &DataType::Float64)
7332 .iter()
7333 .map(|i| i.parse::<f64>().unwrap())
7334 .collect::<Vec<f64>>()
7335 );
7336
7337 let f32_expected = vec![
7338 -9223372000000000000.0,
7339 -2147483600.0,
7340 -32768.0,
7341 -128.0,
7342 0.0,
7343 255.0,
7344 65535.0,
7345 4294967300.0,
7346 18446744000000000000.0,
7347 ];
7348 assert_eq!(
7349 f32_expected,
7350 get_cast_values::<Float32Type>(&f64_array, &DataType::Float32)
7351 .iter()
7352 .map(|i| i.parse::<f32>().unwrap())
7353 .collect::<Vec<f32>>()
7354 );
7355
7356 let f16_expected = vec![
7357 f16::from_f64(-9223372000000000000.0),
7358 f16::from_f64(-2147483600.0),
7359 f16::from_f64(-32768.0),
7360 f16::from_f64(-128.0),
7361 f16::from_f64(0.0),
7362 f16::from_f64(255.0),
7363 f16::from_f64(65535.0),
7364 f16::from_f64(4294967300.0),
7365 f16::from_f64(18446744000000000000.0),
7366 ];
7367 assert_eq!(
7368 f16_expected,
7369 get_cast_values::<Float16Type>(&f64_array, &DataType::Float16)
7370 .iter()
7371 .map(|i| i.parse::<f16>().unwrap())
7372 .collect::<Vec<f16>>()
7373 );
7374
7375 let i64_expected = vec![
7376 "-9223372036854775808",
7377 "-2147483648",
7378 "-32768",
7379 "-128",
7380 "0",
7381 "255",
7382 "65535",
7383 "4294967295",
7384 "null",
7385 ];
7386 assert_eq!(
7387 i64_expected,
7388 get_cast_values::<Int64Type>(&f64_array, &DataType::Int64)
7389 );
7390
7391 let i32_expected = vec![
7392 "null",
7393 "-2147483648",
7394 "-32768",
7395 "-128",
7396 "0",
7397 "255",
7398 "65535",
7399 "null",
7400 "null",
7401 ];
7402 assert_eq!(
7403 i32_expected,
7404 get_cast_values::<Int32Type>(&f64_array, &DataType::Int32)
7405 );
7406
7407 let i16_expected = vec![
7408 "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
7409 ];
7410 assert_eq!(
7411 i16_expected,
7412 get_cast_values::<Int16Type>(&f64_array, &DataType::Int16)
7413 );
7414
7415 let i8_expected = vec![
7416 "null", "null", "null", "-128", "0", "null", "null", "null", "null",
7417 ];
7418 assert_eq!(
7419 i8_expected,
7420 get_cast_values::<Int8Type>(&f64_array, &DataType::Int8)
7421 );
7422
7423 let u64_expected = vec![
7424 "null",
7425 "null",
7426 "null",
7427 "null",
7428 "0",
7429 "255",
7430 "65535",
7431 "4294967295",
7432 "null",
7433 ];
7434 assert_eq!(
7435 u64_expected,
7436 get_cast_values::<UInt64Type>(&f64_array, &DataType::UInt64)
7437 );
7438
7439 let u32_expected = vec![
7440 "null",
7441 "null",
7442 "null",
7443 "null",
7444 "0",
7445 "255",
7446 "65535",
7447 "4294967295",
7448 "null",
7449 ];
7450 assert_eq!(
7451 u32_expected,
7452 get_cast_values::<UInt32Type>(&f64_array, &DataType::UInt32)
7453 );
7454
7455 let u16_expected = vec![
7456 "null", "null", "null", "null", "0", "255", "65535", "null", "null",
7457 ];
7458 assert_eq!(
7459 u16_expected,
7460 get_cast_values::<UInt16Type>(&f64_array, &DataType::UInt16)
7461 );
7462
7463 let u8_expected = vec![
7464 "null", "null", "null", "null", "0", "255", "null", "null", "null",
7465 ];
7466 assert_eq!(
7467 u8_expected,
7468 get_cast_values::<UInt8Type>(&f64_array, &DataType::UInt8)
7469 );
7470 }
7471
7472 #[test]
7473 fn test_cast_from_f32() {
7474 let f32_values: Vec<f32> = vec![
7475 i32::MIN as f32,
7476 i32::MIN as f32,
7477 i16::MIN as f32,
7478 i8::MIN as f32,
7479 0_f32,
7480 u8::MAX as f32,
7481 u16::MAX as f32,
7482 u32::MAX as f32,
7483 u32::MAX as f32,
7484 ];
7485 let f32_array: ArrayRef = Arc::new(Float32Array::from(f32_values));
7486
7487 let f64_expected = vec![
7488 "-2147483648.0",
7489 "-2147483648.0",
7490 "-32768.0",
7491 "-128.0",
7492 "0.0",
7493 "255.0",
7494 "65535.0",
7495 "4294967296.0",
7496 "4294967296.0",
7497 ];
7498 assert_eq!(
7499 f64_expected,
7500 get_cast_values::<Float64Type>(&f32_array, &DataType::Float64)
7501 );
7502
7503 let f32_expected = vec![
7504 "-2147483600.0",
7505 "-2147483600.0",
7506 "-32768.0",
7507 "-128.0",
7508 "0.0",
7509 "255.0",
7510 "65535.0",
7511 "4294967300.0",
7512 "4294967300.0",
7513 ];
7514 assert_eq!(
7515 f32_expected,
7516 get_cast_values::<Float32Type>(&f32_array, &DataType::Float32)
7517 );
7518
7519 let f16_expected = vec![
7520 "-inf", "-inf", "-32768.0", "-128.0", "0.0", "255.0", "inf", "inf", "inf",
7521 ];
7522 assert_eq!(
7523 f16_expected,
7524 get_cast_values::<Float16Type>(&f32_array, &DataType::Float16)
7525 );
7526
7527 let i64_expected = vec![
7528 "-2147483648",
7529 "-2147483648",
7530 "-32768",
7531 "-128",
7532 "0",
7533 "255",
7534 "65535",
7535 "4294967296",
7536 "4294967296",
7537 ];
7538 assert_eq!(
7539 i64_expected,
7540 get_cast_values::<Int64Type>(&f32_array, &DataType::Int64)
7541 );
7542
7543 let i32_expected = vec![
7544 "-2147483648",
7545 "-2147483648",
7546 "-32768",
7547 "-128",
7548 "0",
7549 "255",
7550 "65535",
7551 "null",
7552 "null",
7553 ];
7554 assert_eq!(
7555 i32_expected,
7556 get_cast_values::<Int32Type>(&f32_array, &DataType::Int32)
7557 );
7558
7559 let i16_expected = vec![
7560 "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
7561 ];
7562 assert_eq!(
7563 i16_expected,
7564 get_cast_values::<Int16Type>(&f32_array, &DataType::Int16)
7565 );
7566
7567 let i8_expected = vec![
7568 "null", "null", "null", "-128", "0", "null", "null", "null", "null",
7569 ];
7570 assert_eq!(
7571 i8_expected,
7572 get_cast_values::<Int8Type>(&f32_array, &DataType::Int8)
7573 );
7574
7575 let u64_expected = vec![
7576 "null",
7577 "null",
7578 "null",
7579 "null",
7580 "0",
7581 "255",
7582 "65535",
7583 "4294967296",
7584 "4294967296",
7585 ];
7586 assert_eq!(
7587 u64_expected,
7588 get_cast_values::<UInt64Type>(&f32_array, &DataType::UInt64)
7589 );
7590
7591 let u32_expected = vec![
7592 "null", "null", "null", "null", "0", "255", "65535", "null", "null",
7593 ];
7594 assert_eq!(
7595 u32_expected,
7596 get_cast_values::<UInt32Type>(&f32_array, &DataType::UInt32)
7597 );
7598
7599 let u16_expected = vec![
7600 "null", "null", "null", "null", "0", "255", "65535", "null", "null",
7601 ];
7602 assert_eq!(
7603 u16_expected,
7604 get_cast_values::<UInt16Type>(&f32_array, &DataType::UInt16)
7605 );
7606
7607 let u8_expected = vec![
7608 "null", "null", "null", "null", "0", "255", "null", "null", "null",
7609 ];
7610 assert_eq!(
7611 u8_expected,
7612 get_cast_values::<UInt8Type>(&f32_array, &DataType::UInt8)
7613 );
7614 }
7615
7616 #[test]
7617 fn test_cast_from_uint64() {
7618 let u64_values: Vec<u64> = vec![
7619 0,
7620 u8::MAX as u64,
7621 u16::MAX as u64,
7622 u32::MAX as u64,
7623 u64::MAX,
7624 ];
7625 let u64_array: ArrayRef = Arc::new(UInt64Array::from(u64_values));
7626
7627 let f64_expected = vec![0.0, 255.0, 65535.0, 4294967295.0, 18446744073709552000.0];
7628 assert_eq!(
7629 f64_expected,
7630 get_cast_values::<Float64Type>(&u64_array, &DataType::Float64)
7631 .iter()
7632 .map(|i| i.parse::<f64>().unwrap())
7633 .collect::<Vec<f64>>()
7634 );
7635
7636 let f32_expected = vec![0.0, 255.0, 65535.0, 4294967300.0, 18446744000000000000.0];
7637 assert_eq!(
7638 f32_expected,
7639 get_cast_values::<Float32Type>(&u64_array, &DataType::Float32)
7640 .iter()
7641 .map(|i| i.parse::<f32>().unwrap())
7642 .collect::<Vec<f32>>()
7643 );
7644
7645 let f16_expected = vec![
7646 f16::from_f64(0.0),
7647 f16::from_f64(255.0),
7648 f16::from_f64(65535.0),
7649 f16::from_f64(4294967300.0),
7650 f16::from_f64(18446744000000000000.0),
7651 ];
7652 assert_eq!(
7653 f16_expected,
7654 get_cast_values::<Float16Type>(&u64_array, &DataType::Float16)
7655 .iter()
7656 .map(|i| i.parse::<f16>().unwrap())
7657 .collect::<Vec<f16>>()
7658 );
7659
7660 let i64_expected = vec!["0", "255", "65535", "4294967295", "null"];
7661 assert_eq!(
7662 i64_expected,
7663 get_cast_values::<Int64Type>(&u64_array, &DataType::Int64)
7664 );
7665
7666 let i32_expected = vec!["0", "255", "65535", "null", "null"];
7667 assert_eq!(
7668 i32_expected,
7669 get_cast_values::<Int32Type>(&u64_array, &DataType::Int32)
7670 );
7671
7672 let i16_expected = vec!["0", "255", "null", "null", "null"];
7673 assert_eq!(
7674 i16_expected,
7675 get_cast_values::<Int16Type>(&u64_array, &DataType::Int16)
7676 );
7677
7678 let i8_expected = vec!["0", "null", "null", "null", "null"];
7679 assert_eq!(
7680 i8_expected,
7681 get_cast_values::<Int8Type>(&u64_array, &DataType::Int8)
7682 );
7683
7684 let u64_expected = vec!["0", "255", "65535", "4294967295", "18446744073709551615"];
7685 assert_eq!(
7686 u64_expected,
7687 get_cast_values::<UInt64Type>(&u64_array, &DataType::UInt64)
7688 );
7689
7690 let u32_expected = vec!["0", "255", "65535", "4294967295", "null"];
7691 assert_eq!(
7692 u32_expected,
7693 get_cast_values::<UInt32Type>(&u64_array, &DataType::UInt32)
7694 );
7695
7696 let u16_expected = vec!["0", "255", "65535", "null", "null"];
7697 assert_eq!(
7698 u16_expected,
7699 get_cast_values::<UInt16Type>(&u64_array, &DataType::UInt16)
7700 );
7701
7702 let u8_expected = vec!["0", "255", "null", "null", "null"];
7703 assert_eq!(
7704 u8_expected,
7705 get_cast_values::<UInt8Type>(&u64_array, &DataType::UInt8)
7706 );
7707 }
7708
7709 #[test]
7710 fn test_cast_from_uint32() {
7711 let u32_values: Vec<u32> = vec![0, u8::MAX as u32, u16::MAX as u32, u32::MAX];
7712 let u32_array: ArrayRef = Arc::new(UInt32Array::from(u32_values));
7713
7714 let f64_expected = vec!["0.0", "255.0", "65535.0", "4294967295.0"];
7715 assert_eq!(
7716 f64_expected,
7717 get_cast_values::<Float64Type>(&u32_array, &DataType::Float64)
7718 );
7719
7720 let f32_expected = vec!["0.0", "255.0", "65535.0", "4294967300.0"];
7721 assert_eq!(
7722 f32_expected,
7723 get_cast_values::<Float32Type>(&u32_array, &DataType::Float32)
7724 );
7725
7726 let f16_expected = vec!["0.0", "255.0", "inf", "inf"];
7727 assert_eq!(
7728 f16_expected,
7729 get_cast_values::<Float16Type>(&u32_array, &DataType::Float16)
7730 );
7731
7732 let i64_expected = vec!["0", "255", "65535", "4294967295"];
7733 assert_eq!(
7734 i64_expected,
7735 get_cast_values::<Int64Type>(&u32_array, &DataType::Int64)
7736 );
7737
7738 let i32_expected = vec!["0", "255", "65535", "null"];
7739 assert_eq!(
7740 i32_expected,
7741 get_cast_values::<Int32Type>(&u32_array, &DataType::Int32)
7742 );
7743
7744 let i16_expected = vec!["0", "255", "null", "null"];
7745 assert_eq!(
7746 i16_expected,
7747 get_cast_values::<Int16Type>(&u32_array, &DataType::Int16)
7748 );
7749
7750 let i8_expected = vec!["0", "null", "null", "null"];
7751 assert_eq!(
7752 i8_expected,
7753 get_cast_values::<Int8Type>(&u32_array, &DataType::Int8)
7754 );
7755
7756 let u64_expected = vec!["0", "255", "65535", "4294967295"];
7757 assert_eq!(
7758 u64_expected,
7759 get_cast_values::<UInt64Type>(&u32_array, &DataType::UInt64)
7760 );
7761
7762 let u32_expected = vec!["0", "255", "65535", "4294967295"];
7763 assert_eq!(
7764 u32_expected,
7765 get_cast_values::<UInt32Type>(&u32_array, &DataType::UInt32)
7766 );
7767
7768 let u16_expected = vec!["0", "255", "65535", "null"];
7769 assert_eq!(
7770 u16_expected,
7771 get_cast_values::<UInt16Type>(&u32_array, &DataType::UInt16)
7772 );
7773
7774 let u8_expected = vec!["0", "255", "null", "null"];
7775 assert_eq!(
7776 u8_expected,
7777 get_cast_values::<UInt8Type>(&u32_array, &DataType::UInt8)
7778 );
7779 }
7780
7781 #[test]
7782 fn test_cast_from_uint16() {
7783 let u16_values: Vec<u16> = vec![0, u8::MAX as u16, u16::MAX];
7784 let u16_array: ArrayRef = Arc::new(UInt16Array::from(u16_values));
7785
7786 let f64_expected = vec!["0.0", "255.0", "65535.0"];
7787 assert_eq!(
7788 f64_expected,
7789 get_cast_values::<Float64Type>(&u16_array, &DataType::Float64)
7790 );
7791
7792 let f32_expected = vec!["0.0", "255.0", "65535.0"];
7793 assert_eq!(
7794 f32_expected,
7795 get_cast_values::<Float32Type>(&u16_array, &DataType::Float32)
7796 );
7797
7798 let f16_expected = vec!["0.0", "255.0", "inf"];
7799 assert_eq!(
7800 f16_expected,
7801 get_cast_values::<Float16Type>(&u16_array, &DataType::Float16)
7802 );
7803
7804 let i64_expected = vec!["0", "255", "65535"];
7805 assert_eq!(
7806 i64_expected,
7807 get_cast_values::<Int64Type>(&u16_array, &DataType::Int64)
7808 );
7809
7810 let i32_expected = vec!["0", "255", "65535"];
7811 assert_eq!(
7812 i32_expected,
7813 get_cast_values::<Int32Type>(&u16_array, &DataType::Int32)
7814 );
7815
7816 let i16_expected = vec!["0", "255", "null"];
7817 assert_eq!(
7818 i16_expected,
7819 get_cast_values::<Int16Type>(&u16_array, &DataType::Int16)
7820 );
7821
7822 let i8_expected = vec!["0", "null", "null"];
7823 assert_eq!(
7824 i8_expected,
7825 get_cast_values::<Int8Type>(&u16_array, &DataType::Int8)
7826 );
7827
7828 let u64_expected = vec!["0", "255", "65535"];
7829 assert_eq!(
7830 u64_expected,
7831 get_cast_values::<UInt64Type>(&u16_array, &DataType::UInt64)
7832 );
7833
7834 let u32_expected = vec!["0", "255", "65535"];
7835 assert_eq!(
7836 u32_expected,
7837 get_cast_values::<UInt32Type>(&u16_array, &DataType::UInt32)
7838 );
7839
7840 let u16_expected = vec!["0", "255", "65535"];
7841 assert_eq!(
7842 u16_expected,
7843 get_cast_values::<UInt16Type>(&u16_array, &DataType::UInt16)
7844 );
7845
7846 let u8_expected = vec!["0", "255", "null"];
7847 assert_eq!(
7848 u8_expected,
7849 get_cast_values::<UInt8Type>(&u16_array, &DataType::UInt8)
7850 );
7851 }
7852
7853 #[test]
7854 fn test_cast_from_uint8() {
7855 let u8_values: Vec<u8> = vec![0, u8::MAX];
7856 let u8_array: ArrayRef = Arc::new(UInt8Array::from(u8_values));
7857
7858 let f64_expected = vec!["0.0", "255.0"];
7859 assert_eq!(
7860 f64_expected,
7861 get_cast_values::<Float64Type>(&u8_array, &DataType::Float64)
7862 );
7863
7864 let f32_expected = vec!["0.0", "255.0"];
7865 assert_eq!(
7866 f32_expected,
7867 get_cast_values::<Float32Type>(&u8_array, &DataType::Float32)
7868 );
7869
7870 let f16_expected = vec!["0.0", "255.0"];
7871 assert_eq!(
7872 f16_expected,
7873 get_cast_values::<Float16Type>(&u8_array, &DataType::Float16)
7874 );
7875
7876 let i64_expected = vec!["0", "255"];
7877 assert_eq!(
7878 i64_expected,
7879 get_cast_values::<Int64Type>(&u8_array, &DataType::Int64)
7880 );
7881
7882 let i32_expected = vec!["0", "255"];
7883 assert_eq!(
7884 i32_expected,
7885 get_cast_values::<Int32Type>(&u8_array, &DataType::Int32)
7886 );
7887
7888 let i16_expected = vec!["0", "255"];
7889 assert_eq!(
7890 i16_expected,
7891 get_cast_values::<Int16Type>(&u8_array, &DataType::Int16)
7892 );
7893
7894 let i8_expected = vec!["0", "null"];
7895 assert_eq!(
7896 i8_expected,
7897 get_cast_values::<Int8Type>(&u8_array, &DataType::Int8)
7898 );
7899
7900 let u64_expected = vec!["0", "255"];
7901 assert_eq!(
7902 u64_expected,
7903 get_cast_values::<UInt64Type>(&u8_array, &DataType::UInt64)
7904 );
7905
7906 let u32_expected = vec!["0", "255"];
7907 assert_eq!(
7908 u32_expected,
7909 get_cast_values::<UInt32Type>(&u8_array, &DataType::UInt32)
7910 );
7911
7912 let u16_expected = vec!["0", "255"];
7913 assert_eq!(
7914 u16_expected,
7915 get_cast_values::<UInt16Type>(&u8_array, &DataType::UInt16)
7916 );
7917
7918 let u8_expected = vec!["0", "255"];
7919 assert_eq!(
7920 u8_expected,
7921 get_cast_values::<UInt8Type>(&u8_array, &DataType::UInt8)
7922 );
7923 }
7924
7925 #[test]
7926 fn test_cast_from_int64() {
7927 let i64_values: Vec<i64> = vec![
7928 i64::MIN,
7929 i32::MIN as i64,
7930 i16::MIN as i64,
7931 i8::MIN as i64,
7932 0,
7933 i8::MAX as i64,
7934 i16::MAX as i64,
7935 i32::MAX as i64,
7936 i64::MAX,
7937 ];
7938 let i64_array: ArrayRef = Arc::new(Int64Array::from(i64_values));
7939
7940 let f64_expected = vec![
7941 -9223372036854776000.0,
7942 -2147483648.0,
7943 -32768.0,
7944 -128.0,
7945 0.0,
7946 127.0,
7947 32767.0,
7948 2147483647.0,
7949 9223372036854776000.0,
7950 ];
7951 assert_eq!(
7952 f64_expected,
7953 get_cast_values::<Float64Type>(&i64_array, &DataType::Float64)
7954 .iter()
7955 .map(|i| i.parse::<f64>().unwrap())
7956 .collect::<Vec<f64>>()
7957 );
7958
7959 let f32_expected = vec![
7960 -9223372000000000000.0,
7961 -2147483600.0,
7962 -32768.0,
7963 -128.0,
7964 0.0,
7965 127.0,
7966 32767.0,
7967 2147483600.0,
7968 9223372000000000000.0,
7969 ];
7970 assert_eq!(
7971 f32_expected,
7972 get_cast_values::<Float32Type>(&i64_array, &DataType::Float32)
7973 .iter()
7974 .map(|i| i.parse::<f32>().unwrap())
7975 .collect::<Vec<f32>>()
7976 );
7977
7978 let f16_expected = vec![
7979 f16::from_f64(-9223372000000000000.0),
7980 f16::from_f64(-2147483600.0),
7981 f16::from_f64(-32768.0),
7982 f16::from_f64(-128.0),
7983 f16::from_f64(0.0),
7984 f16::from_f64(127.0),
7985 f16::from_f64(32767.0),
7986 f16::from_f64(2147483600.0),
7987 f16::from_f64(9223372000000000000.0),
7988 ];
7989 assert_eq!(
7990 f16_expected,
7991 get_cast_values::<Float16Type>(&i64_array, &DataType::Float16)
7992 .iter()
7993 .map(|i| i.parse::<f16>().unwrap())
7994 .collect::<Vec<f16>>()
7995 );
7996
7997 let i64_expected = vec![
7998 "-9223372036854775808",
7999 "-2147483648",
8000 "-32768",
8001 "-128",
8002 "0",
8003 "127",
8004 "32767",
8005 "2147483647",
8006 "9223372036854775807",
8007 ];
8008 assert_eq!(
8009 i64_expected,
8010 get_cast_values::<Int64Type>(&i64_array, &DataType::Int64)
8011 );
8012
8013 let i32_expected = vec![
8014 "null",
8015 "-2147483648",
8016 "-32768",
8017 "-128",
8018 "0",
8019 "127",
8020 "32767",
8021 "2147483647",
8022 "null",
8023 ];
8024 assert_eq!(
8025 i32_expected,
8026 get_cast_values::<Int32Type>(&i64_array, &DataType::Int32)
8027 );
8028
8029 assert_eq!(
8030 i32_expected,
8031 get_cast_values::<Date32Type>(&i64_array, &DataType::Date32)
8032 );
8033
8034 let i16_expected = vec![
8035 "null", "null", "-32768", "-128", "0", "127", "32767", "null", "null",
8036 ];
8037 assert_eq!(
8038 i16_expected,
8039 get_cast_values::<Int16Type>(&i64_array, &DataType::Int16)
8040 );
8041
8042 let i8_expected = vec![
8043 "null", "null", "null", "-128", "0", "127", "null", "null", "null",
8044 ];
8045 assert_eq!(
8046 i8_expected,
8047 get_cast_values::<Int8Type>(&i64_array, &DataType::Int8)
8048 );
8049
8050 let u64_expected = vec![
8051 "null",
8052 "null",
8053 "null",
8054 "null",
8055 "0",
8056 "127",
8057 "32767",
8058 "2147483647",
8059 "9223372036854775807",
8060 ];
8061 assert_eq!(
8062 u64_expected,
8063 get_cast_values::<UInt64Type>(&i64_array, &DataType::UInt64)
8064 );
8065
8066 let u32_expected = vec![
8067 "null",
8068 "null",
8069 "null",
8070 "null",
8071 "0",
8072 "127",
8073 "32767",
8074 "2147483647",
8075 "null",
8076 ];
8077 assert_eq!(
8078 u32_expected,
8079 get_cast_values::<UInt32Type>(&i64_array, &DataType::UInt32)
8080 );
8081
8082 let u16_expected = vec![
8083 "null", "null", "null", "null", "0", "127", "32767", "null", "null",
8084 ];
8085 assert_eq!(
8086 u16_expected,
8087 get_cast_values::<UInt16Type>(&i64_array, &DataType::UInt16)
8088 );
8089
8090 let u8_expected = vec![
8091 "null", "null", "null", "null", "0", "127", "null", "null", "null",
8092 ];
8093 assert_eq!(
8094 u8_expected,
8095 get_cast_values::<UInt8Type>(&i64_array, &DataType::UInt8)
8096 );
8097 }
8098
8099 #[test]
8100 fn test_cast_from_int32() {
8101 let i32_values: Vec<i32> = vec![
8102 i32::MIN,
8103 i16::MIN as i32,
8104 i8::MIN as i32,
8105 0,
8106 i8::MAX as i32,
8107 i16::MAX as i32,
8108 i32::MAX,
8109 ];
8110 let i32_array: ArrayRef = Arc::new(Int32Array::from(i32_values));
8111
8112 let f64_expected = vec![
8113 "-2147483648.0",
8114 "-32768.0",
8115 "-128.0",
8116 "0.0",
8117 "127.0",
8118 "32767.0",
8119 "2147483647.0",
8120 ];
8121 assert_eq!(
8122 f64_expected,
8123 get_cast_values::<Float64Type>(&i32_array, &DataType::Float64)
8124 );
8125
8126 let f32_expected = vec![
8127 "-2147483600.0",
8128 "-32768.0",
8129 "-128.0",
8130 "0.0",
8131 "127.0",
8132 "32767.0",
8133 "2147483600.0",
8134 ];
8135 assert_eq!(
8136 f32_expected,
8137 get_cast_values::<Float32Type>(&i32_array, &DataType::Float32)
8138 );
8139
8140 let f16_expected = vec![
8141 f16::from_f64(-2147483600.0),
8142 f16::from_f64(-32768.0),
8143 f16::from_f64(-128.0),
8144 f16::from_f64(0.0),
8145 f16::from_f64(127.0),
8146 f16::from_f64(32767.0),
8147 f16::from_f64(2147483600.0),
8148 ];
8149 assert_eq!(
8150 f16_expected,
8151 get_cast_values::<Float16Type>(&i32_array, &DataType::Float16)
8152 .iter()
8153 .map(|i| i.parse::<f16>().unwrap())
8154 .collect::<Vec<f16>>()
8155 );
8156
8157 let i16_expected = vec!["null", "-32768", "-128", "0", "127", "32767", "null"];
8158 assert_eq!(
8159 i16_expected,
8160 get_cast_values::<Int16Type>(&i32_array, &DataType::Int16)
8161 );
8162
8163 let i8_expected = vec!["null", "null", "-128", "0", "127", "null", "null"];
8164 assert_eq!(
8165 i8_expected,
8166 get_cast_values::<Int8Type>(&i32_array, &DataType::Int8)
8167 );
8168
8169 let u64_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
8170 assert_eq!(
8171 u64_expected,
8172 get_cast_values::<UInt64Type>(&i32_array, &DataType::UInt64)
8173 );
8174
8175 let u32_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
8176 assert_eq!(
8177 u32_expected,
8178 get_cast_values::<UInt32Type>(&i32_array, &DataType::UInt32)
8179 );
8180
8181 let u16_expected = vec!["null", "null", "null", "0", "127", "32767", "null"];
8182 assert_eq!(
8183 u16_expected,
8184 get_cast_values::<UInt16Type>(&i32_array, &DataType::UInt16)
8185 );
8186
8187 let u8_expected = vec!["null", "null", "null", "0", "127", "null", "null"];
8188 assert_eq!(
8189 u8_expected,
8190 get_cast_values::<UInt8Type>(&i32_array, &DataType::UInt8)
8191 );
8192
8193 let i64_expected = vec![
8195 "-185542587187200000",
8196 "-2831155200000",
8197 "-11059200000",
8198 "0",
8199 "10972800000",
8200 "2831068800000",
8201 "185542587100800000",
8202 ];
8203 assert_eq!(
8204 i64_expected,
8205 get_cast_values::<Date64Type>(&i32_array, &DataType::Date64)
8206 );
8207 }
8208
8209 #[test]
8210 fn test_cast_from_int16() {
8211 let i16_values: Vec<i16> = vec![i16::MIN, i8::MIN as i16, 0, i8::MAX as i16, i16::MAX];
8212 let i16_array: ArrayRef = Arc::new(Int16Array::from(i16_values));
8213
8214 let f64_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
8215 assert_eq!(
8216 f64_expected,
8217 get_cast_values::<Float64Type>(&i16_array, &DataType::Float64)
8218 );
8219
8220 let f32_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
8221 assert_eq!(
8222 f32_expected,
8223 get_cast_values::<Float32Type>(&i16_array, &DataType::Float32)
8224 );
8225
8226 let f16_expected = vec![
8227 f16::from_f64(-32768.0),
8228 f16::from_f64(-128.0),
8229 f16::from_f64(0.0),
8230 f16::from_f64(127.0),
8231 f16::from_f64(32767.0),
8232 ];
8233 assert_eq!(
8234 f16_expected,
8235 get_cast_values::<Float16Type>(&i16_array, &DataType::Float16)
8236 .iter()
8237 .map(|i| i.parse::<f16>().unwrap())
8238 .collect::<Vec<f16>>()
8239 );
8240
8241 let i64_expected = vec!["-32768", "-128", "0", "127", "32767"];
8242 assert_eq!(
8243 i64_expected,
8244 get_cast_values::<Int64Type>(&i16_array, &DataType::Int64)
8245 );
8246
8247 let i32_expected = vec!["-32768", "-128", "0", "127", "32767"];
8248 assert_eq!(
8249 i32_expected,
8250 get_cast_values::<Int32Type>(&i16_array, &DataType::Int32)
8251 );
8252
8253 let i16_expected = vec!["-32768", "-128", "0", "127", "32767"];
8254 assert_eq!(
8255 i16_expected,
8256 get_cast_values::<Int16Type>(&i16_array, &DataType::Int16)
8257 );
8258
8259 let i8_expected = vec!["null", "-128", "0", "127", "null"];
8260 assert_eq!(
8261 i8_expected,
8262 get_cast_values::<Int8Type>(&i16_array, &DataType::Int8)
8263 );
8264
8265 let u64_expected = vec!["null", "null", "0", "127", "32767"];
8266 assert_eq!(
8267 u64_expected,
8268 get_cast_values::<UInt64Type>(&i16_array, &DataType::UInt64)
8269 );
8270
8271 let u32_expected = vec!["null", "null", "0", "127", "32767"];
8272 assert_eq!(
8273 u32_expected,
8274 get_cast_values::<UInt32Type>(&i16_array, &DataType::UInt32)
8275 );
8276
8277 let u16_expected = vec!["null", "null", "0", "127", "32767"];
8278 assert_eq!(
8279 u16_expected,
8280 get_cast_values::<UInt16Type>(&i16_array, &DataType::UInt16)
8281 );
8282
8283 let u8_expected = vec!["null", "null", "0", "127", "null"];
8284 assert_eq!(
8285 u8_expected,
8286 get_cast_values::<UInt8Type>(&i16_array, &DataType::UInt8)
8287 );
8288 }
8289
8290 #[test]
8291 fn test_cast_from_date32() {
8292 let i32_values: Vec<i32> = vec![
8293 i32::MIN,
8294 i16::MIN as i32,
8295 i8::MIN as i32,
8296 0,
8297 i8::MAX as i32,
8298 i16::MAX as i32,
8299 i32::MAX,
8300 ];
8301 let date32_array: ArrayRef = Arc::new(Date32Array::from(i32_values));
8302
8303 let i64_expected = vec![
8304 "-2147483648",
8305 "-32768",
8306 "-128",
8307 "0",
8308 "127",
8309 "32767",
8310 "2147483647",
8311 ];
8312 assert_eq!(
8313 i64_expected,
8314 get_cast_values::<Int64Type>(&date32_array, &DataType::Int64)
8315 );
8316 }
8317
8318 #[test]
8319 fn test_cast_from_int8() {
8320 let i8_values: Vec<i8> = vec![i8::MIN, 0, i8::MAX];
8321 let i8_array = Int8Array::from(i8_values);
8322
8323 let f64_expected = vec!["-128.0", "0.0", "127.0"];
8324 assert_eq!(
8325 f64_expected,
8326 get_cast_values::<Float64Type>(&i8_array, &DataType::Float64)
8327 );
8328
8329 let f32_expected = vec!["-128.0", "0.0", "127.0"];
8330 assert_eq!(
8331 f32_expected,
8332 get_cast_values::<Float32Type>(&i8_array, &DataType::Float32)
8333 );
8334
8335 let f16_expected = vec!["-128.0", "0.0", "127.0"];
8336 assert_eq!(
8337 f16_expected,
8338 get_cast_values::<Float16Type>(&i8_array, &DataType::Float16)
8339 );
8340
8341 let i64_expected = vec!["-128", "0", "127"];
8342 assert_eq!(
8343 i64_expected,
8344 get_cast_values::<Int64Type>(&i8_array, &DataType::Int64)
8345 );
8346
8347 let i32_expected = vec!["-128", "0", "127"];
8348 assert_eq!(
8349 i32_expected,
8350 get_cast_values::<Int32Type>(&i8_array, &DataType::Int32)
8351 );
8352
8353 let i16_expected = vec!["-128", "0", "127"];
8354 assert_eq!(
8355 i16_expected,
8356 get_cast_values::<Int16Type>(&i8_array, &DataType::Int16)
8357 );
8358
8359 let i8_expected = vec!["-128", "0", "127"];
8360 assert_eq!(
8361 i8_expected,
8362 get_cast_values::<Int8Type>(&i8_array, &DataType::Int8)
8363 );
8364
8365 let u64_expected = vec!["null", "0", "127"];
8366 assert_eq!(
8367 u64_expected,
8368 get_cast_values::<UInt64Type>(&i8_array, &DataType::UInt64)
8369 );
8370
8371 let u32_expected = vec!["null", "0", "127"];
8372 assert_eq!(
8373 u32_expected,
8374 get_cast_values::<UInt32Type>(&i8_array, &DataType::UInt32)
8375 );
8376
8377 let u16_expected = vec!["null", "0", "127"];
8378 assert_eq!(
8379 u16_expected,
8380 get_cast_values::<UInt16Type>(&i8_array, &DataType::UInt16)
8381 );
8382
8383 let u8_expected = vec!["null", "0", "127"];
8384 assert_eq!(
8385 u8_expected,
8386 get_cast_values::<UInt8Type>(&i8_array, &DataType::UInt8)
8387 );
8388 }
8389
8390 fn get_cast_values<T>(array: &dyn Array, dt: &DataType) -> Vec<String>
8392 where
8393 T: ArrowPrimitiveType,
8394 {
8395 let c = cast(array, dt).unwrap();
8396 let a = c.as_primitive::<T>();
8397 let mut v: Vec<String> = vec![];
8398 for i in 0..array.len() {
8399 if a.is_null(i) {
8400 v.push("null".to_string())
8401 } else {
8402 v.push(format!("{:?}", a.value(i)));
8403 }
8404 }
8405 v
8406 }
8407
8408 #[test]
8409 fn test_cast_utf8_dict() {
8410 let mut builder = StringDictionaryBuilder::<Int8Type>::new();
8412 builder.append("one").unwrap();
8413 builder.append_null();
8414 builder.append("three").unwrap();
8415 let array: ArrayRef = Arc::new(builder.finish());
8416
8417 let expected = vec!["one", "null", "three"];
8418
8419 let cast_type = Utf8;
8421 let cast_array = cast(&array, &cast_type).expect("cast to UTF-8 failed");
8422 assert_eq!(cast_array.data_type(), &cast_type);
8423 assert_eq!(array_to_strings(&cast_array), expected);
8424
8425 let cast_type = Dictionary(Box::new(Int16), Box::new(Utf8));
8428 let cast_array = cast(&array, &cast_type).expect("cast failed");
8429 assert_eq!(cast_array.data_type(), &cast_type);
8430 assert_eq!(array_to_strings(&cast_array), expected);
8431
8432 let cast_type = Dictionary(Box::new(Int32), Box::new(Utf8));
8433 let cast_array = cast(&array, &cast_type).expect("cast failed");
8434 assert_eq!(cast_array.data_type(), &cast_type);
8435 assert_eq!(array_to_strings(&cast_array), expected);
8436
8437 let cast_type = Dictionary(Box::new(Int64), Box::new(Utf8));
8438 let cast_array = cast(&array, &cast_type).expect("cast failed");
8439 assert_eq!(cast_array.data_type(), &cast_type);
8440 assert_eq!(array_to_strings(&cast_array), expected);
8441
8442 let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
8443 let cast_array = cast(&array, &cast_type).expect("cast failed");
8444 assert_eq!(cast_array.data_type(), &cast_type);
8445 assert_eq!(array_to_strings(&cast_array), expected);
8446
8447 let cast_type = Dictionary(Box::new(UInt16), Box::new(Utf8));
8448 let cast_array = cast(&array, &cast_type).expect("cast failed");
8449 assert_eq!(cast_array.data_type(), &cast_type);
8450 assert_eq!(array_to_strings(&cast_array), expected);
8451
8452 let cast_type = Dictionary(Box::new(UInt32), Box::new(Utf8));
8453 let cast_array = cast(&array, &cast_type).expect("cast failed");
8454 assert_eq!(cast_array.data_type(), &cast_type);
8455 assert_eq!(array_to_strings(&cast_array), expected);
8456
8457 let cast_type = Dictionary(Box::new(UInt64), Box::new(Utf8));
8458 let cast_array = cast(&array, &cast_type).expect("cast failed");
8459 assert_eq!(cast_array.data_type(), &cast_type);
8460 assert_eq!(array_to_strings(&cast_array), expected);
8461 }
8462
8463 #[test]
8464 fn test_cast_dict_to_dict_bad_index_value_primitive() {
8465 let mut builder = PrimitiveDictionaryBuilder::<Int32Type, Int64Type>::new();
8470
8471 for i in 0..200 {
8475 builder.append(i).unwrap();
8476 }
8477 let array: ArrayRef = Arc::new(builder.finish());
8478
8479 let cast_type = Dictionary(Box::new(Int8), Box::new(Utf8));
8480 let res = cast(&array, &cast_type);
8481 assert!(res.is_err());
8482 let actual_error = format!("{res:?}");
8483 let expected_error = "Could not convert 72 dictionary indexes from Int32 to Int8";
8484 assert!(
8485 actual_error.contains(expected_error),
8486 "did not find expected error '{actual_error}' in actual error '{expected_error}'"
8487 );
8488 }
8489
8490 #[test]
8491 fn test_cast_dict_to_dict_bad_index_value_utf8() {
8492 let mut builder = StringDictionaryBuilder::<Int32Type>::new();
8496
8497 for i in 0..200 {
8501 let val = format!("val{i}");
8502 builder.append(&val).unwrap();
8503 }
8504 let array = builder.finish();
8505
8506 let cast_type = Dictionary(Box::new(Int8), Box::new(Utf8));
8507 let res = cast(&array, &cast_type);
8508 assert!(res.is_err());
8509 let actual_error = format!("{res:?}");
8510 let expected_error = "Could not convert 72 dictionary indexes from Int32 to Int8";
8511 assert!(
8512 actual_error.contains(expected_error),
8513 "did not find expected error '{actual_error}' in actual error '{expected_error}'"
8514 );
8515 }
8516
8517 #[test]
8518 fn test_cast_primitive_dict() {
8519 let mut builder = PrimitiveDictionaryBuilder::<Int8Type, Int32Type>::new();
8521 builder.append(1).unwrap();
8522 builder.append_null();
8523 builder.append(3).unwrap();
8524 let array: ArrayRef = Arc::new(builder.finish());
8525
8526 let expected = vec!["1", "null", "3"];
8527
8528 let cast_array = cast(&array, &Utf8).expect("cast to UTF-8 failed");
8530 assert_eq!(array_to_strings(&cast_array), expected);
8531 assert_eq!(cast_array.data_type(), &Utf8);
8532
8533 let cast_array = cast(&array, &Int64).expect("cast to int64 failed");
8534 assert_eq!(array_to_strings(&cast_array), expected);
8535 assert_eq!(cast_array.data_type(), &Int64);
8536 }
8537
8538 #[test]
8539 fn test_cast_primitive_array_to_dict() {
8540 let mut builder = PrimitiveBuilder::<Int32Type>::new();
8541 builder.append_value(1);
8542 builder.append_null();
8543 builder.append_value(3);
8544 let array: ArrayRef = Arc::new(builder.finish());
8545
8546 let expected = vec!["1", "null", "3"];
8547
8548 let cast_type = Dictionary(Box::new(UInt8), Box::new(Int32));
8550 let cast_array = cast(&array, &cast_type).expect("cast failed");
8551 assert_eq!(cast_array.data_type(), &cast_type);
8552 assert_eq!(array_to_strings(&cast_array), expected);
8553
8554 let cast_type = Dictionary(Box::new(UInt8), Box::new(Int8));
8556 let cast_array = cast(&array, &cast_type).expect("cast failed");
8557 assert_eq!(cast_array.data_type(), &cast_type);
8558 assert_eq!(array_to_strings(&cast_array), expected);
8559 }
8560
8561 #[test]
8562 fn test_cast_time_array_to_dict() {
8563 use DataType::*;
8564
8565 let array = Arc::new(Date32Array::from(vec![Some(1000), None, Some(2000)])) as ArrayRef;
8566
8567 let expected = vec!["1972-09-27", "null", "1975-06-24"];
8568
8569 let cast_type = Dictionary(Box::new(UInt8), Box::new(Date32));
8570 let cast_array = cast(&array, &cast_type).expect("cast failed");
8571 assert_eq!(cast_array.data_type(), &cast_type);
8572 assert_eq!(array_to_strings(&cast_array), expected);
8573 }
8574
8575 #[test]
8576 fn test_cast_timestamp_array_to_dict() {
8577 use DataType::*;
8578
8579 let array = Arc::new(
8580 TimestampSecondArray::from(vec![Some(1000), None, Some(2000)]).with_timezone_utc(),
8581 ) as ArrayRef;
8582
8583 let expected = vec!["1970-01-01T00:16:40", "null", "1970-01-01T00:33:20"];
8584
8585 let cast_type = Dictionary(Box::new(UInt8), Box::new(Timestamp(TimeUnit::Second, None)));
8586 let cast_array = cast(&array, &cast_type).expect("cast failed");
8587 assert_eq!(cast_array.data_type(), &cast_type);
8588 assert_eq!(array_to_strings(&cast_array), expected);
8589 }
8590
8591 #[test]
8592 fn test_cast_string_array_to_dict() {
8593 use DataType::*;
8594
8595 let array = Arc::new(StringArray::from(vec![Some("one"), None, Some("three")])) as ArrayRef;
8596
8597 let expected = vec!["one", "null", "three"];
8598
8599 let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
8601 let cast_array = cast(&array, &cast_type).expect("cast failed");
8602 assert_eq!(cast_array.data_type(), &cast_type);
8603 assert_eq!(array_to_strings(&cast_array), expected);
8604 }
8605
8606 #[test]
8607 fn test_cast_null_array_to_from_decimal_array() {
8608 let data_type = DataType::Decimal128(12, 4);
8609 let array = new_null_array(&DataType::Null, 4);
8610 assert_eq!(array.data_type(), &DataType::Null);
8611 let cast_array = cast(&array, &data_type).expect("cast failed");
8612 assert_eq!(cast_array.data_type(), &data_type);
8613 for i in 0..4 {
8614 assert!(cast_array.is_null(i));
8615 }
8616
8617 let array = new_null_array(&data_type, 4);
8618 assert_eq!(array.data_type(), &data_type);
8619 let cast_array = cast(&array, &DataType::Null).expect("cast failed");
8620 assert_eq!(cast_array.data_type(), &DataType::Null);
8621 assert_eq!(cast_array.len(), 4);
8622 assert_eq!(cast_array.logical_nulls().unwrap().null_count(), 4);
8623 }
8624
8625 #[test]
8626 fn test_cast_null_array_from_and_to_primitive_array() {
8627 macro_rules! typed_test {
8628 ($ARR_TYPE:ident, $DATATYPE:ident, $TYPE:tt) => {{
8629 {
8630 let array = Arc::new(NullArray::new(6)) as ArrayRef;
8631 let expected = $ARR_TYPE::from(vec![None; 6]);
8632 let cast_type = DataType::$DATATYPE;
8633 let cast_array = cast(&array, &cast_type).expect("cast failed");
8634 let cast_array = cast_array.as_primitive::<$TYPE>();
8635 assert_eq!(cast_array.data_type(), &cast_type);
8636 assert_eq!(cast_array, &expected);
8637 }
8638 }};
8639 }
8640
8641 typed_test!(Int16Array, Int16, Int16Type);
8642 typed_test!(Int32Array, Int32, Int32Type);
8643 typed_test!(Int64Array, Int64, Int64Type);
8644
8645 typed_test!(UInt16Array, UInt16, UInt16Type);
8646 typed_test!(UInt32Array, UInt32, UInt32Type);
8647 typed_test!(UInt64Array, UInt64, UInt64Type);
8648
8649 typed_test!(Float16Array, Float16, Float16Type);
8650 typed_test!(Float32Array, Float32, Float32Type);
8651 typed_test!(Float64Array, Float64, Float64Type);
8652
8653 typed_test!(Date32Array, Date32, Date32Type);
8654 typed_test!(Date64Array, Date64, Date64Type);
8655 }
8656
8657 fn cast_from_null_to_other_base(data_type: &DataType, is_complex: bool) {
8658 let array = new_null_array(&DataType::Null, 4);
8660 assert_eq!(array.data_type(), &DataType::Null);
8661 let cast_array = cast(&array, data_type).expect("cast failed");
8662 assert_eq!(cast_array.data_type(), data_type);
8663 for i in 0..4 {
8664 if is_complex {
8665 assert!(cast_array.logical_nulls().unwrap().is_null(i));
8666 } else {
8667 assert!(cast_array.is_null(i));
8668 }
8669 }
8670 }
8671
8672 fn cast_from_null_to_other(data_type: &DataType) {
8673 cast_from_null_to_other_base(data_type, false);
8674 }
8675
8676 fn cast_from_null_to_other_complex(data_type: &DataType) {
8677 cast_from_null_to_other_base(data_type, true);
8678 }
8679
8680 #[test]
8681 fn test_cast_null_from_and_to_variable_sized() {
8682 cast_from_null_to_other(&DataType::Utf8);
8683 cast_from_null_to_other(&DataType::LargeUtf8);
8684 cast_from_null_to_other(&DataType::Binary);
8685 cast_from_null_to_other(&DataType::LargeBinary);
8686 }
8687
8688 #[test]
8689 fn test_cast_null_from_and_to_nested_type() {
8690 let data_type = DataType::Map(
8692 Arc::new(Field::new_struct(
8693 "entry",
8694 vec![
8695 Field::new("key", DataType::Utf8, false),
8696 Field::new("value", DataType::Int32, true),
8697 ],
8698 false,
8699 )),
8700 false,
8701 );
8702 cast_from_null_to_other(&data_type);
8703
8704 let data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
8706 cast_from_null_to_other(&data_type);
8707 let data_type = DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
8708 cast_from_null_to_other(&data_type);
8709 let data_type =
8710 DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 4);
8711 cast_from_null_to_other(&data_type);
8712
8713 let values = vec![None, None, None, None] as Vec<Option<&str>>;
8715 let array: DictionaryArray<Int8Type> = values.into_iter().collect();
8716 let array = Arc::new(array) as ArrayRef;
8717 let data_type = array.data_type().to_owned();
8718 cast_from_null_to_other(&data_type);
8719
8720 let data_type = DataType::Struct(vec![Field::new("data", DataType::Int64, false)].into());
8722 cast_from_null_to_other(&data_type);
8723
8724 let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Int32, true)));
8725 cast_from_null_to_other(&target_type);
8726
8727 let target_type =
8728 DataType::LargeListView(Arc::new(Field::new("item", DataType::Int32, true)));
8729 cast_from_null_to_other(&target_type);
8730
8731 let fields = UnionFields::from_fields(vec![Field::new("a", DataType::Int64, false)]);
8732 let target_type = DataType::Union(fields, UnionMode::Sparse);
8733 cast_from_null_to_other_complex(&target_type);
8734
8735 let target_type = DataType::RunEndEncoded(
8736 Arc::new(Field::new("item", DataType::Int32, true)),
8737 Arc::new(Field::new("item", DataType::Int32, true)),
8738 );
8739 cast_from_null_to_other_complex(&target_type);
8740 }
8741
8742 fn array_to_strings(array: &ArrayRef) -> Vec<String> {
8744 let options = FormatOptions::new().with_null("null");
8745 let formatter = ArrayFormatter::try_new(array.as_ref(), &options).unwrap();
8746 (0..array.len())
8747 .map(|i| formatter.value(i).to_string())
8748 .collect()
8749 }
8750
8751 #[test]
8752 fn test_cast_utf8_to_date32() {
8753 use chrono::NaiveDate;
8754 let from_ymd = chrono::NaiveDate::from_ymd_opt;
8755 let since = chrono::NaiveDate::signed_duration_since;
8756
8757 let a = StringArray::from(vec![
8758 "2000-01-01", "2000-01-01T12:00:00", "2000-2-2", "2000-00-00", "2000", ]);
8764 let array = Arc::new(a) as ArrayRef;
8765 let b = cast(&array, &DataType::Date32).unwrap();
8766 let c = b.as_primitive::<Date32Type>();
8767
8768 let date_value = since(
8770 NaiveDate::from_ymd_opt(2000, 1, 1).unwrap(),
8771 from_ymd(1970, 1, 1).unwrap(),
8772 )
8773 .num_days() as i32;
8774 assert!(c.is_valid(0)); assert_eq!(date_value, c.value(0));
8776
8777 assert!(c.is_valid(1)); assert_eq!(date_value, c.value(1));
8779
8780 let date_value = since(
8781 NaiveDate::from_ymd_opt(2000, 2, 2).unwrap(),
8782 from_ymd(1970, 1, 1).unwrap(),
8783 )
8784 .num_days() as i32;
8785 assert!(c.is_valid(2)); assert_eq!(date_value, c.value(2));
8787
8788 assert!(!c.is_valid(3)); assert!(!c.is_valid(4)); }
8792
8793 #[test]
8794 fn test_cast_utf8_to_date64() {
8795 let a = StringArray::from(vec![
8796 "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", ]);
8803 let array = Arc::new(a) as ArrayRef;
8804 let b = cast(&array, &DataType::Date64).unwrap();
8805 let c = b.as_primitive::<Date64Type>();
8806
8807 assert!(c.is_valid(0)); assert_eq!(946728000000, c.value(0));
8810 assert!(c.is_valid(1)); assert_eq!(1608035696000, c.value(1));
8812 assert!(!c.is_valid(2)); assert!(!c.is_valid(3)); assert!(c.is_valid(4)); assert_eq!(946728000000, c.value(4));
8817 assert!(c.is_valid(5)); assert_eq!(946684800000, c.value(5));
8819 }
8820
8821 #[test]
8822 fn test_can_cast_fsl_to_fsl() {
8823 let from_array = Arc::new(
8824 FixedSizeListArray::from_iter_primitive::<Float32Type, _, _>(
8825 [Some([Some(1.0), Some(2.0)]), None],
8826 2,
8827 ),
8828 ) as ArrayRef;
8829 let to_array = Arc::new(
8830 FixedSizeListArray::from_iter_primitive::<Float16Type, _, _>(
8831 [
8832 Some([Some(f16::from_f32(1.0)), Some(f16::from_f32(2.0))]),
8833 None,
8834 ],
8835 2,
8836 ),
8837 ) as ArrayRef;
8838
8839 assert!(can_cast_types(from_array.data_type(), to_array.data_type()));
8840 let actual = cast(&from_array, to_array.data_type()).unwrap();
8841 assert_eq!(actual.data_type(), to_array.data_type());
8842
8843 let invalid_target =
8844 DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Binary, true)), 2);
8845 assert!(!can_cast_types(from_array.data_type(), &invalid_target));
8846
8847 let invalid_size =
8848 DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Float16, true)), 5);
8849 assert!(!can_cast_types(from_array.data_type(), &invalid_size));
8850 }
8851
8852 #[test]
8853 fn test_can_cast_types_fixed_size_list_to_list() {
8854 let array1 = make_fixed_size_list_array();
8856 assert!(can_cast_types(
8857 array1.data_type(),
8858 &DataType::List(Arc::new(Field::new("", DataType::Int32, false)))
8859 ));
8860
8861 let array2 = make_fixed_size_list_array_for_large_list();
8863 assert!(can_cast_types(
8864 array2.data_type(),
8865 &DataType::LargeList(Arc::new(Field::new("", DataType::Int64, false)))
8866 ));
8867 }
8868
8869 #[test]
8870 fn test_cast_fixed_size_list_to_list() {
8871 let cases = [
8877 (
8879 Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8880 [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
8881 2,
8882 )) as ArrayRef,
8883 Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>([
8884 Some([Some(1), Some(1)]),
8885 Some([Some(2), Some(2)]),
8886 ])) as ArrayRef,
8887 ),
8888 (
8890 Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8891 [None, Some([Some(2), Some(2)])],
8892 2,
8893 )) as ArrayRef,
8894 Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>([
8895 None,
8896 Some([Some(2), Some(2)]),
8897 ])) as ArrayRef,
8898 ),
8899 (
8901 Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8902 [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
8903 2,
8904 )) as ArrayRef,
8905 Arc::new(LargeListArray::from_iter_primitive::<Int64Type, _, _>([
8906 Some([Some(1), Some(1)]),
8907 Some([Some(2), Some(2)]),
8908 ])) as ArrayRef,
8909 ),
8910 (
8912 Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8913 [None, Some([Some(2), Some(2)])],
8914 2,
8915 )) as ArrayRef,
8916 Arc::new(LargeListArray::from_iter_primitive::<Int64Type, _, _>([
8917 None,
8918 Some([Some(2), Some(2)]),
8919 ])) as ArrayRef,
8920 ),
8921 (
8923 Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8924 [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
8925 2,
8926 )) as ArrayRef,
8927 Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>([
8928 Some([Some(1), Some(1)]),
8929 Some([Some(2), Some(2)]),
8930 ])) as ArrayRef,
8931 ),
8932 (
8934 Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8935 [None, Some([Some(2), Some(2)])],
8936 2,
8937 )) as ArrayRef,
8938 Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>([
8939 None,
8940 Some([Some(2), Some(2)]),
8941 ])) as ArrayRef,
8942 ),
8943 (
8945 Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8946 [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
8947 2,
8948 )) as ArrayRef,
8949 Arc::new(LargeListViewArray::from_iter_primitive::<Int64Type, _, _>(
8950 [Some([Some(1), Some(1)]), Some([Some(2), Some(2)])],
8951 )) as ArrayRef,
8952 ),
8953 (
8955 Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8956 [None, Some([Some(2), Some(2)])],
8957 2,
8958 )) as ArrayRef,
8959 Arc::new(LargeListViewArray::from_iter_primitive::<Int64Type, _, _>(
8960 [None, Some([Some(2), Some(2)])],
8961 )) as ArrayRef,
8962 ),
8963 ];
8964
8965 for (array, expected) in cases {
8966 assert!(
8967 can_cast_types(array.data_type(), expected.data_type()),
8968 "can_cast_types claims we cannot cast {:?} to {:?}",
8969 array.data_type(),
8970 expected.data_type()
8971 );
8972
8973 let list_array = cast(&array, expected.data_type())
8974 .unwrap_or_else(|_| panic!("Failed to cast {array:?} to {expected:?}"));
8975 assert_eq!(
8976 list_array.as_ref(),
8977 &expected,
8978 "Incorrect result from casting {array:?} to {expected:?}",
8979 );
8980 }
8981 }
8982
8983 #[test]
8984 fn test_cast_utf8_to_list() {
8985 let array = Arc::new(StringArray::from(vec!["5"])) as ArrayRef;
8987 let field = Arc::new(Field::new("", DataType::Int32, false));
8988 let list_array = cast(&array, &DataType::List(field.clone())).unwrap();
8989 let actual = list_array.as_list_opt::<i32>().unwrap();
8990 let expect = ListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
8991 assert_eq!(&expect.value(0), &actual.value(0));
8992
8993 let list_array = cast(&array, &DataType::LargeList(field.clone())).unwrap();
8995 let actual = list_array.as_list_opt::<i64>().unwrap();
8996 let expect = LargeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
8997 assert_eq!(&expect.value(0), &actual.value(0));
8998
8999 let list_array = cast(&array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
9001 let actual = list_array.as_fixed_size_list_opt().unwrap();
9002 let expect =
9003 FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])], 1);
9004 assert_eq!(&expect.value(0), &actual.value(0));
9005 }
9006
9007 #[test]
9008 fn test_cast_single_element_fixed_size_list() {
9009 let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
9011 [(Some([Some(5)]))],
9012 1,
9013 )) as ArrayRef;
9014 let casted_array = cast(&from_array, &DataType::Int32).unwrap();
9015 let actual: &Int32Array = casted_array.as_primitive();
9016 let expected = Int32Array::from(vec![Some(5)]);
9017 assert_eq!(&expected, actual);
9018
9019 let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
9021 [(Some([Some(5)]))],
9022 1,
9023 )) as ArrayRef;
9024 let to_field = Arc::new(Field::new("dummy", DataType::Float32, false));
9025 let actual = cast(&from_array, &DataType::FixedSizeList(to_field.clone(), 1)).unwrap();
9026 let expected = Arc::new(FixedSizeListArray::new(
9027 to_field.clone(),
9028 1,
9029 Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
9030 None,
9031 )) as ArrayRef;
9032 assert_eq!(*expected, *actual);
9033
9034 let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
9036 [(Some([Some(5)]))],
9037 1,
9038 )) as ArrayRef;
9039 let to_field_inner = Arc::new(Field::new_list_field(DataType::Float32, false));
9040 let to_field = Arc::new(Field::new(
9041 "dummy",
9042 DataType::FixedSizeList(to_field_inner.clone(), 1),
9043 false,
9044 ));
9045 let actual = cast(&from_array, &DataType::FixedSizeList(to_field.clone(), 1)).unwrap();
9046 let expected = Arc::new(FixedSizeListArray::new(
9047 to_field.clone(),
9048 1,
9049 Arc::new(FixedSizeListArray::new(
9050 to_field_inner.clone(),
9051 1,
9052 Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
9053 None,
9054 )) as ArrayRef,
9055 None,
9056 )) as ArrayRef;
9057 assert_eq!(*expected, *actual);
9058
9059 let field = Arc::new(Field::new("dummy", DataType::Float32, false));
9061 let from_array = Arc::new(Int8Array::from(vec![Some(5)])) as ArrayRef;
9062 let casted_array = cast(&from_array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
9063 let actual = casted_array.as_fixed_size_list();
9064 let expected = Arc::new(FixedSizeListArray::new(
9065 field.clone(),
9066 1,
9067 Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
9068 None,
9069 )) as ArrayRef;
9070 assert_eq!(expected.as_ref(), actual);
9071
9072 let field = Arc::new(Field::new("nullable", DataType::Float32, true));
9074 let from_array = Arc::new(Int8Array::from(vec![None])) as ArrayRef;
9075 let casted_array = cast(&from_array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
9076 let actual = casted_array.as_fixed_size_list();
9077 let expected = Arc::new(FixedSizeListArray::new(
9078 field.clone(),
9079 1,
9080 Arc::new(Float32Array::from(vec![None])) as ArrayRef,
9081 None,
9082 )) as ArrayRef;
9083 assert_eq!(expected.as_ref(), actual);
9084 }
9085
9086 #[test]
9087 fn test_cast_list_containers() {
9088 let array = make_large_list_array();
9090 let list_array = cast(
9091 &array,
9092 &DataType::List(Arc::new(Field::new("", DataType::Int32, false))),
9093 )
9094 .unwrap();
9095 let actual = list_array.as_any().downcast_ref::<ListArray>().unwrap();
9096 let expected = array.as_any().downcast_ref::<LargeListArray>().unwrap();
9097
9098 assert_eq!(&expected.value(0), &actual.value(0));
9099 assert_eq!(&expected.value(1), &actual.value(1));
9100 assert_eq!(&expected.value(2), &actual.value(2));
9101
9102 let array = make_list_array();
9104 let large_list_array = cast(
9105 &array,
9106 &DataType::LargeList(Arc::new(Field::new("", DataType::Int32, false))),
9107 )
9108 .unwrap();
9109 let actual = large_list_array
9110 .as_any()
9111 .downcast_ref::<LargeListArray>()
9112 .unwrap();
9113 let expected = array.as_any().downcast_ref::<ListArray>().unwrap();
9114
9115 assert_eq!(&expected.value(0), &actual.value(0));
9116 assert_eq!(&expected.value(1), &actual.value(1));
9117 assert_eq!(&expected.value(2), &actual.value(2));
9118 }
9119
9120 #[test]
9121 fn test_cast_list_view() {
9122 let array = make_list_view_array();
9124 let to = DataType::ListView(Field::new_list_field(DataType::Float32, true).into());
9125 assert!(can_cast_types(array.data_type(), &to));
9126 let actual = cast(&array, &to).unwrap();
9127 let actual = actual.as_list_view::<i32>();
9128
9129 assert_eq!(
9130 &Float32Array::from(vec![0.0, 1.0, 2.0]) as &dyn Array,
9131 actual.value(0).as_ref()
9132 );
9133 assert_eq!(
9134 &Float32Array::from(vec![3.0, 4.0, 5.0]) as &dyn Array,
9135 actual.value(1).as_ref()
9136 );
9137 assert_eq!(
9138 &Float32Array::from(vec![6.0, 7.0]) as &dyn Array,
9139 actual.value(2).as_ref()
9140 );
9141
9142 let array = make_large_list_view_array();
9144 let to = DataType::LargeListView(Field::new_list_field(DataType::Float32, true).into());
9145 assert!(can_cast_types(array.data_type(), &to));
9146 let actual = cast(&array, &to).unwrap();
9147 let actual = actual.as_list_view::<i64>();
9148
9149 assert_eq!(
9150 &Float32Array::from(vec![0.0, 1.0, 2.0]) as &dyn Array,
9151 actual.value(0).as_ref()
9152 );
9153 assert_eq!(
9154 &Float32Array::from(vec![3.0, 4.0, 5.0]) as &dyn Array,
9155 actual.value(1).as_ref()
9156 );
9157 assert_eq!(
9158 &Float32Array::from(vec![6.0, 7.0]) as &dyn Array,
9159 actual.value(2).as_ref()
9160 );
9161 }
9162
9163 #[test]
9164 fn test_non_list_to_list_view() {
9165 let input = Arc::new(Int32Array::from(vec![Some(0), None, Some(2)])) as ArrayRef;
9166 let expected_primitive =
9167 Arc::new(Float32Array::from(vec![Some(0.0), None, Some(2.0)])) as ArrayRef;
9168
9169 let expected = ListViewArray::new(
9171 Field::new_list_field(DataType::Float32, true).into(),
9172 vec![0, 1, 2].into(),
9173 vec![1, 1, 1].into(),
9174 expected_primitive.clone(),
9175 None,
9176 );
9177 assert!(can_cast_types(input.data_type(), expected.data_type()));
9178 let actual = cast(&input, expected.data_type()).unwrap();
9179 assert_eq!(actual.as_ref(), &expected);
9180
9181 let expected = LargeListViewArray::new(
9183 Field::new_list_field(DataType::Float32, true).into(),
9184 vec![0, 1, 2].into(),
9185 vec![1, 1, 1].into(),
9186 expected_primitive.clone(),
9187 None,
9188 );
9189 assert!(can_cast_types(input.data_type(), expected.data_type()));
9190 let actual = cast(&input, expected.data_type()).unwrap();
9191 assert_eq!(actual.as_ref(), &expected);
9192 }
9193
9194 #[test]
9195 fn test_cast_list_to_fsl() {
9196 let field = Arc::new(Field::new_list_field(DataType::Int32, true));
9204 let values = vec![
9205 Some(vec![Some(1), Some(2), Some(3)]),
9206 Some(vec![Some(4), Some(5), Some(6)]),
9207 ];
9208 let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(
9209 values.clone(),
9210 )) as ArrayRef;
9211 let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9212 values, 3,
9213 )) as ArrayRef;
9214 let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
9215 assert_eq!(expected.as_ref(), actual.as_ref());
9216
9217 let cases = [
9220 (
9221 vec![1, 2, 3, 4, 5, 6],
9223 vec![3, 0, 3, 0],
9224 ),
9225 (
9226 vec![1, 2, 3, 0, 0, 4, 5, 6, 0],
9228 vec![3, 2, 3, 1],
9229 ),
9230 (
9231 vec![1, 2, 3, 0, 0, 0, 4, 5, 6, 0, 0, 0],
9233 vec![3, 3, 3, 3],
9234 ),
9235 (
9236 vec![1, 2, 3, 4, 5, 6, 0, 0, 0],
9238 vec![3, 0, 3, 3],
9239 ),
9240 ];
9241 let null_buffer = NullBuffer::from(vec![true, false, true, false]);
9242
9243 let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9244 vec![
9245 Some(vec![Some(1), Some(2), Some(3)]),
9246 None,
9247 Some(vec![Some(4), Some(5), Some(6)]),
9248 None,
9249 ],
9250 3,
9251 )) as ArrayRef;
9252
9253 for (values, lengths) in cases.iter() {
9254 let array = Arc::new(ListArray::new(
9255 field.clone(),
9256 OffsetBuffer::from_lengths(lengths.clone()),
9257 Arc::new(Int32Array::from(values.clone())),
9258 Some(null_buffer.clone()),
9259 )) as ArrayRef;
9260 let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
9261 assert_eq!(expected.as_ref(), actual.as_ref());
9262 }
9263 }
9264
9265 #[test]
9266 fn test_cast_list_view_to_fsl() {
9267 let field = Arc::new(Field::new_list_field(DataType::Int32, true));
9275 let values = vec![
9276 Some(vec![Some(1), Some(2), Some(3)]),
9277 Some(vec![Some(4), Some(5), Some(6)]),
9278 ];
9279 let array = Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>(
9280 values.clone(),
9281 )) as ArrayRef;
9282 let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9283 values, 3,
9284 )) as ArrayRef;
9285 let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
9286 assert_eq!(expected.as_ref(), actual.as_ref());
9287
9288 let cases = [
9291 (
9292 vec![1, 2, 3, 4, 5, 6],
9294 vec![0, 0, 3, 0],
9295 vec![3, 0, 3, 0],
9296 ),
9297 (
9298 vec![1, 2, 3, 0, 0, 4, 5, 6, 0],
9300 vec![0, 1, 5, 0],
9301 vec![3, 2, 3, 1],
9302 ),
9303 (
9304 vec![1, 2, 3, 0, 0, 0, 4, 5, 6, 0, 0, 0],
9306 vec![0, 3, 6, 9],
9307 vec![3, 3, 3, 3],
9308 ),
9309 (
9310 vec![1, 2, 3, 4, 5, 6, 0, 0, 0],
9312 vec![0, 0, 3, 6],
9313 vec![3, 0, 3, 3],
9314 ),
9315 ];
9316 let null_buffer = NullBuffer::from(vec![true, false, true, false]);
9317
9318 let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9319 vec![
9320 Some(vec![Some(1), Some(2), Some(3)]),
9321 None,
9322 Some(vec![Some(4), Some(5), Some(6)]),
9323 None,
9324 ],
9325 3,
9326 )) as ArrayRef;
9327
9328 for (values, offsets, lengths) in cases.iter() {
9329 let array = Arc::new(ListViewArray::new(
9330 field.clone(),
9331 offsets.clone().into(),
9332 lengths.clone().into(),
9333 Arc::new(Int32Array::from(values.clone())),
9334 Some(null_buffer.clone()),
9335 )) as ArrayRef;
9336 let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
9337 assert_eq!(expected.as_ref(), actual.as_ref());
9338 }
9339 }
9340
9341 #[test]
9342 fn test_cast_list_to_fsl_safety() {
9343 let values = vec![
9344 Some(vec![Some(1), Some(2), Some(3)]),
9345 Some(vec![Some(4), Some(5)]),
9346 Some(vec![Some(6), Some(7), Some(8), Some(9)]),
9347 Some(vec![Some(3), Some(4), Some(5)]),
9348 ];
9349 let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(
9350 values.clone(),
9351 )) as ArrayRef;
9352
9353 let res = cast_with_options(
9354 array.as_ref(),
9355 &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9356 &CastOptions {
9357 safe: false,
9358 ..Default::default()
9359 },
9360 );
9361 assert!(res.is_err());
9362 assert!(
9363 format!("{res:?}")
9364 .contains("Cannot cast to FixedSizeList(3): value at index 1 has length 2")
9365 );
9366
9367 let res = cast(
9370 array.as_ref(),
9371 &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9372 )
9373 .unwrap();
9374 let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9375 vec![
9376 Some(vec![Some(1), Some(2), Some(3)]),
9377 None, None, Some(vec![Some(3), Some(4), Some(5)]),
9380 ],
9381 3,
9382 )) as ArrayRef;
9383 assert_eq!(expected.as_ref(), res.as_ref());
9384
9385 let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
9388 Some(vec![Some(1), Some(2), Some(3)]),
9389 None,
9390 ])) as ArrayRef;
9391 let res = cast_with_options(
9392 array.as_ref(),
9393 &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9394 &CastOptions {
9395 safe: false,
9396 ..Default::default()
9397 },
9398 )
9399 .unwrap();
9400 let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9401 vec![Some(vec![Some(1), Some(2), Some(3)]), None],
9402 3,
9403 )) as ArrayRef;
9404 assert_eq!(expected.as_ref(), res.as_ref());
9405 }
9406
9407 #[test]
9408 fn test_cast_list_view_to_fsl_safety() {
9409 let values = vec![
9410 Some(vec![Some(1), Some(2), Some(3)]),
9411 Some(vec![Some(4), Some(5)]),
9412 Some(vec![Some(6), Some(7), Some(8), Some(9)]),
9413 Some(vec![Some(3), Some(4), Some(5)]),
9414 ];
9415 let array = Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>(
9416 values.clone(),
9417 )) as ArrayRef;
9418
9419 let res = cast_with_options(
9420 array.as_ref(),
9421 &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9422 &CastOptions {
9423 safe: false,
9424 ..Default::default()
9425 },
9426 );
9427 assert!(res.is_err());
9428 assert!(
9429 format!("{res:?}")
9430 .contains("Cannot cast to FixedSizeList(3): value at index 1 has length 2")
9431 );
9432
9433 let res = cast(
9436 array.as_ref(),
9437 &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9438 )
9439 .unwrap();
9440 let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9441 vec![
9442 Some(vec![Some(1), Some(2), Some(3)]),
9443 None, None, Some(vec![Some(3), Some(4), Some(5)]),
9446 ],
9447 3,
9448 )) as ArrayRef;
9449 assert_eq!(expected.as_ref(), res.as_ref());
9450
9451 let array = Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>(vec![
9454 Some(vec![Some(1), Some(2), Some(3)]),
9455 None,
9456 ])) as ArrayRef;
9457 let res = cast_with_options(
9458 array.as_ref(),
9459 &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9460 &CastOptions {
9461 safe: false,
9462 ..Default::default()
9463 },
9464 )
9465 .unwrap();
9466 let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9467 vec![Some(vec![Some(1), Some(2), Some(3)]), None],
9468 3,
9469 )) as ArrayRef;
9470 assert_eq!(expected.as_ref(), res.as_ref());
9471 }
9472
9473 #[test]
9474 fn test_cast_large_list_to_fsl() {
9475 let values = vec![Some(vec![Some(1), Some(2)]), Some(vec![Some(3), Some(4)])];
9476 let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9477 values.clone(),
9478 2,
9479 )) as ArrayRef;
9480 let target_type =
9481 DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 2);
9482
9483 let array = Arc::new(LargeListArray::from_iter_primitive::<Int32Type, _, _>(
9484 values.clone(),
9485 )) as ArrayRef;
9486 let actual = cast(array.as_ref(), &target_type).unwrap();
9487 assert_eq!(expected.as_ref(), actual.as_ref());
9488
9489 let array = Arc::new(LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(
9490 values.clone(),
9491 )) as ArrayRef;
9492 let actual = cast(array.as_ref(), &target_type).unwrap();
9493 assert_eq!(expected.as_ref(), actual.as_ref());
9494 }
9495
9496 #[test]
9497 fn test_cast_list_to_fsl_subcast() {
9498 let array = Arc::new(LargeListArray::from_iter_primitive::<Int32Type, _, _>(
9499 vec![
9500 Some(vec![Some(1), Some(2)]),
9501 Some(vec![Some(3), Some(i32::MAX)]),
9502 ],
9503 )) as ArrayRef;
9504 let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
9505 vec![
9506 Some(vec![Some(1), Some(2)]),
9507 Some(vec![Some(3), Some(i32::MAX as i64)]),
9508 ],
9509 2,
9510 )) as ArrayRef;
9511 let actual = cast(
9512 array.as_ref(),
9513 &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int64, true)), 2),
9514 )
9515 .unwrap();
9516 assert_eq!(expected.as_ref(), actual.as_ref());
9517
9518 let res = cast_with_options(
9519 array.as_ref(),
9520 &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int16, true)), 2),
9521 &CastOptions {
9522 safe: false,
9523 ..Default::default()
9524 },
9525 );
9526 assert!(res.is_err());
9527 assert!(format!("{res:?}").contains("Can't cast value 2147483647 to type Int16"));
9528 }
9529
9530 #[test]
9531 fn test_cast_list_to_fsl_empty() {
9532 let inner_field = Arc::new(Field::new_list_field(DataType::Int32, true));
9533 let target_type = DataType::FixedSizeList(inner_field.clone(), 3);
9534 let expected = new_empty_array(&target_type);
9535
9536 let array = new_empty_array(&DataType::List(inner_field.clone()));
9538 assert!(can_cast_types(array.data_type(), &target_type));
9539 let actual = cast(array.as_ref(), &target_type).unwrap();
9540 assert_eq!(expected.as_ref(), actual.as_ref());
9541
9542 let array = new_empty_array(&DataType::LargeList(inner_field.clone()));
9544 assert!(can_cast_types(array.data_type(), &target_type));
9545 let actual = cast(array.as_ref(), &target_type).unwrap();
9546 assert_eq!(expected.as_ref(), actual.as_ref());
9547
9548 let array = new_empty_array(&DataType::ListView(inner_field.clone()));
9550 assert!(can_cast_types(array.data_type(), &target_type));
9551 let actual = cast(array.as_ref(), &target_type).unwrap();
9552 assert_eq!(expected.as_ref(), actual.as_ref());
9553
9554 let array = new_empty_array(&DataType::LargeListView(inner_field.clone()));
9556 assert!(can_cast_types(array.data_type(), &target_type));
9557 let actual = cast(array.as_ref(), &target_type).unwrap();
9558 assert_eq!(expected.as_ref(), actual.as_ref());
9559 }
9560
9561 fn make_list_array() -> ArrayRef {
9562 Arc::new(ListArray::new(
9564 Field::new_list_field(DataType::Int32, true).into(),
9565 OffsetBuffer::from_lengths(vec![3, 3, 2]),
9566 Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
9567 None,
9568 ))
9569 }
9570
9571 fn make_large_list_array() -> ArrayRef {
9572 Arc::new(LargeListArray::new(
9574 Field::new_list_field(DataType::Int32, true).into(),
9575 OffsetBuffer::from_lengths(vec![3, 3, 2]),
9576 Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
9577 None,
9578 ))
9579 }
9580
9581 fn make_list_view_array() -> ArrayRef {
9582 Arc::new(ListViewArray::new(
9584 Field::new_list_field(DataType::Int32, true).into(),
9585 vec![0, 3, 6].into(),
9586 vec![3, 3, 2].into(),
9587 Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
9588 None,
9589 ))
9590 }
9591
9592 fn make_large_list_view_array() -> ArrayRef {
9593 Arc::new(LargeListViewArray::new(
9595 Field::new_list_field(DataType::Int32, true).into(),
9596 vec![0, 3, 6].into(),
9597 vec![3, 3, 2].into(),
9598 Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
9599 None,
9600 ))
9601 }
9602
9603 fn make_fixed_size_list_array() -> ArrayRef {
9604 Arc::new(FixedSizeListArray::new(
9606 Field::new_list_field(DataType::Int32, true).into(),
9607 4,
9608 Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
9609 None,
9610 ))
9611 }
9612
9613 fn make_fixed_size_list_array_for_large_list() -> ArrayRef {
9614 Arc::new(FixedSizeListArray::new(
9616 Field::new_list_field(DataType::Int64, true).into(),
9617 4,
9618 Arc::new(Int64Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
9619 None,
9620 ))
9621 }
9622
9623 #[test]
9624 fn test_cast_map_dont_allow_change_of_order() {
9625 let string_builder = StringBuilder::new();
9626 let value_builder = StringBuilder::new();
9627 let mut builder = MapBuilder::new(
9628 Some(MapFieldNames {
9629 entry: "entries".to_string(),
9630 key: "key".to_string(),
9631 value: "value".to_string(),
9632 }),
9633 string_builder,
9634 value_builder,
9635 );
9636
9637 builder.keys().append_value("0");
9638 builder.values().append_value("test_val_1");
9639 builder.append(true).unwrap();
9640 builder.keys().append_value("1");
9641 builder.values().append_value("test_val_2");
9642 builder.append(true).unwrap();
9643
9644 let array = builder.finish();
9646
9647 let new_ordered = true;
9648 let new_type = DataType::Map(
9649 Arc::new(Field::new(
9650 "entries",
9651 DataType::Struct(
9652 vec![
9653 Field::new("key", DataType::Utf8, false),
9654 Field::new("value", DataType::Utf8, false),
9655 ]
9656 .into(),
9657 ),
9658 false,
9659 )),
9660 new_ordered,
9661 );
9662
9663 let new_array_result = cast(&array, &new_type.clone());
9664 assert!(!can_cast_types(array.data_type(), &new_type));
9665 let Err(ArrowError::CastError(t)) = new_array_result else {
9666 panic!();
9667 };
9668 assert_eq!(
9669 t,
9670 r#"Casting from Map("entries": non-null Struct("key": non-null Utf8, "value": Utf8), unsorted) to Map("entries": non-null Struct("key": non-null Utf8, "value": non-null Utf8), sorted) not supported"#
9671 );
9672 }
9673
9674 #[test]
9675 fn test_cast_map_dont_allow_when_container_cant_cast() {
9676 let string_builder = StringBuilder::new();
9677 let value_builder = IntervalDayTimeArray::builder(2);
9678 let mut builder = MapBuilder::new(
9679 Some(MapFieldNames {
9680 entry: "entries".to_string(),
9681 key: "key".to_string(),
9682 value: "value".to_string(),
9683 }),
9684 string_builder,
9685 value_builder,
9686 );
9687
9688 builder.keys().append_value("0");
9689 builder.values().append_value(IntervalDayTime::new(1, 1));
9690 builder.append(true).unwrap();
9691 builder.keys().append_value("1");
9692 builder.values().append_value(IntervalDayTime::new(2, 2));
9693 builder.append(true).unwrap();
9694
9695 let array = builder.finish();
9697
9698 let new_ordered = true;
9699 let new_type = DataType::Map(
9700 Arc::new(Field::new(
9701 "entries",
9702 DataType::Struct(
9703 vec![
9704 Field::new("key", DataType::Utf8, false),
9705 Field::new("value", DataType::Duration(TimeUnit::Second), false),
9706 ]
9707 .into(),
9708 ),
9709 false,
9710 )),
9711 new_ordered,
9712 );
9713
9714 let new_array_result = cast(&array, &new_type.clone());
9715 assert!(!can_cast_types(array.data_type(), &new_type));
9716 let Err(ArrowError::CastError(t)) = new_array_result else {
9717 panic!();
9718 };
9719 assert_eq!(
9720 t,
9721 r#"Casting from Map("entries": non-null Struct("key": non-null Utf8, "value": Interval(DayTime)), unsorted) to Map("entries": non-null Struct("key": non-null Utf8, "value": non-null Duration(s)), sorted) not supported"#
9722 );
9723 }
9724
9725 #[test]
9726 fn test_cast_map_field_names() {
9727 let string_builder = StringBuilder::new();
9728 let value_builder = StringBuilder::new();
9729 let mut builder = MapBuilder::new(
9730 Some(MapFieldNames {
9731 entry: "entries".to_string(),
9732 key: "key".to_string(),
9733 value: "value".to_string(),
9734 }),
9735 string_builder,
9736 value_builder,
9737 );
9738
9739 builder.keys().append_value("0");
9740 builder.values().append_value("test_val_1");
9741 builder.append(true).unwrap();
9742 builder.keys().append_value("1");
9743 builder.values().append_value("test_val_2");
9744 builder.append(true).unwrap();
9745 builder.append(false).unwrap();
9746
9747 let array = builder.finish();
9748
9749 let new_type = DataType::Map(
9750 Arc::new(Field::new(
9751 "entries_new",
9752 DataType::Struct(
9753 vec![
9754 Field::new("key_new", DataType::Utf8, false),
9755 Field::new("value_values", DataType::Utf8, false),
9756 ]
9757 .into(),
9758 ),
9759 false,
9760 )),
9761 false,
9762 );
9763
9764 assert_ne!(new_type, array.data_type().clone());
9765
9766 let new_array = cast(&array, &new_type.clone()).unwrap();
9767 assert_eq!(new_type, new_array.data_type().clone());
9768 let map_array = new_array.as_map();
9769
9770 assert_ne!(new_type, array.data_type().clone());
9771 assert_eq!(new_type, map_array.data_type().clone());
9772
9773 let key_string = map_array
9774 .keys()
9775 .as_any()
9776 .downcast_ref::<StringArray>()
9777 .unwrap()
9778 .into_iter()
9779 .flatten()
9780 .collect::<Vec<_>>();
9781 assert_eq!(&key_string, &vec!["0", "1"]);
9782
9783 let values_string_array = cast(map_array.values(), &DataType::Utf8).unwrap();
9784 let values_string = values_string_array
9785 .as_any()
9786 .downcast_ref::<StringArray>()
9787 .unwrap()
9788 .into_iter()
9789 .flatten()
9790 .collect::<Vec<_>>();
9791 assert_eq!(&values_string, &vec!["test_val_1", "test_val_2"]);
9792
9793 assert_eq!(
9794 map_array.nulls(),
9795 Some(&NullBuffer::from(vec![true, true, false]))
9796 );
9797 }
9798
9799 #[test]
9800 fn test_cast_map_contained_values() {
9801 let string_builder = StringBuilder::new();
9802 let value_builder = Int8Builder::new();
9803 let mut builder = MapBuilder::new(
9804 Some(MapFieldNames {
9805 entry: "entries".to_string(),
9806 key: "key".to_string(),
9807 value: "value".to_string(),
9808 }),
9809 string_builder,
9810 value_builder,
9811 );
9812
9813 builder.keys().append_value("0");
9814 builder.values().append_value(44);
9815 builder.append(true).unwrap();
9816 builder.keys().append_value("1");
9817 builder.values().append_value(22);
9818 builder.append(true).unwrap();
9819
9820 let array = builder.finish();
9821
9822 let new_type = DataType::Map(
9823 Arc::new(Field::new(
9824 "entries",
9825 DataType::Struct(
9826 vec![
9827 Field::new("key", DataType::Utf8, false),
9828 Field::new("value", DataType::Utf8, false),
9829 ]
9830 .into(),
9831 ),
9832 false,
9833 )),
9834 false,
9835 );
9836
9837 let new_array = cast(&array, &new_type.clone()).unwrap();
9838 assert_eq!(new_type, new_array.data_type().clone());
9839 let map_array = new_array.as_map();
9840
9841 assert_ne!(new_type, array.data_type().clone());
9842 assert_eq!(new_type, map_array.data_type().clone());
9843
9844 let key_string = map_array
9845 .keys()
9846 .as_any()
9847 .downcast_ref::<StringArray>()
9848 .unwrap()
9849 .into_iter()
9850 .flatten()
9851 .collect::<Vec<_>>();
9852 assert_eq!(&key_string, &vec!["0", "1"]);
9853
9854 let values_string_array = cast(map_array.values(), &DataType::Utf8).unwrap();
9855 let values_string = values_string_array
9856 .as_any()
9857 .downcast_ref::<StringArray>()
9858 .unwrap()
9859 .into_iter()
9860 .flatten()
9861 .collect::<Vec<_>>();
9862 assert_eq!(&values_string, &vec!["44", "22"]);
9863 }
9864
9865 #[test]
9866 fn test_utf8_cast_offsets() {
9867 let str_array = StringArray::from(vec!["a", "b", "c"]);
9869 let str_array = str_array.slice(1, 2);
9870
9871 let out = cast(&str_array, &DataType::LargeUtf8).unwrap();
9872
9873 let large_str_array = out.as_any().downcast_ref::<LargeStringArray>().unwrap();
9874 let strs = large_str_array.into_iter().flatten().collect::<Vec<_>>();
9875 assert_eq!(strs, &["b", "c"])
9876 }
9877
9878 #[test]
9879 fn test_list_cast_offsets() {
9880 let array1 = make_list_array().slice(1, 2);
9882 let array2 = make_list_array();
9883
9884 let dt = DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
9885 let out1 = cast(&array1, &dt).unwrap();
9886 let out2 = cast(&array2, &dt).unwrap();
9887
9888 assert_eq!(&out1, &out2.slice(1, 2))
9889 }
9890
9891 #[test]
9892 fn test_list_to_string() {
9893 fn assert_cast(array: &ArrayRef, expected: &[&str]) {
9894 assert!(can_cast_types(array.data_type(), &DataType::Utf8));
9895 let out = cast(array, &DataType::Utf8).unwrap();
9896 let out = out
9897 .as_string::<i32>()
9898 .into_iter()
9899 .flatten()
9900 .collect::<Vec<_>>();
9901 assert_eq!(out, expected);
9902
9903 assert!(can_cast_types(array.data_type(), &DataType::LargeUtf8));
9904 let out = cast(array, &DataType::LargeUtf8).unwrap();
9905 let out = out
9906 .as_string::<i64>()
9907 .into_iter()
9908 .flatten()
9909 .collect::<Vec<_>>();
9910 assert_eq!(out, expected);
9911
9912 assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
9913 let out = cast(array, &DataType::Utf8View).unwrap();
9914 let out = out
9915 .as_string_view()
9916 .into_iter()
9917 .flatten()
9918 .collect::<Vec<_>>();
9919 assert_eq!(out, expected);
9920 }
9921
9922 let array = Arc::new(ListArray::new(
9923 Field::new_list_field(DataType::Utf8, true).into(),
9924 OffsetBuffer::from_lengths(vec![3, 3, 2]),
9925 Arc::new(StringArray::from(vec![
9926 "a", "b", "c", "d", "e", "f", "g", "h",
9927 ])),
9928 None,
9929 )) as ArrayRef;
9930
9931 assert_cast(&array, &["[a, b, c]", "[d, e, f]", "[g, h]"]);
9932
9933 let array = make_list_array();
9934 assert_cast(&array, &["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
9935
9936 let array = make_large_list_array();
9937 assert_cast(&array, &["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
9938
9939 let array = make_list_view_array();
9940 assert_cast(&array, &["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
9941
9942 let array = make_large_list_view_array();
9943 assert_cast(&array, &["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
9944 }
9945
9946 #[test]
9947 fn test_cast_f64_to_decimal128() {
9948 let decimal_type = DataType::Decimal128(18, 2);
9951 let array = Float64Array::from(vec![
9952 Some(0.0699999999),
9953 Some(0.0659999999),
9954 Some(0.0650000000),
9955 Some(0.0649999999),
9956 ]);
9957 let array = Arc::new(array) as ArrayRef;
9958 generate_cast_test_case!(
9959 &array,
9960 Decimal128Array,
9961 &decimal_type,
9962 vec![
9963 Some(7_i128), Some(7_i128), Some(7_i128), Some(6_i128), ]
9968 );
9969
9970 let decimal_type = DataType::Decimal128(18, 3);
9971 let array = Float64Array::from(vec![
9972 Some(0.0699999999),
9973 Some(0.0659999999),
9974 Some(0.0650000000),
9975 Some(0.0649999999),
9976 ]);
9977 let array = Arc::new(array) as ArrayRef;
9978 generate_cast_test_case!(
9979 &array,
9980 Decimal128Array,
9981 &decimal_type,
9982 vec![
9983 Some(70_i128), Some(66_i128), Some(65_i128), Some(65_i128), ]
9988 );
9989 }
9990
9991 #[test]
9992 fn test_cast_numeric_to_decimal128_overflow() {
9993 let array = Int64Array::from(vec![i64::MAX]);
9994 let array = Arc::new(array) as ArrayRef;
9995 let casted_array = cast_with_options(
9996 &array,
9997 &DataType::Decimal128(38, 30),
9998 &CastOptions {
9999 safe: true,
10000 format_options: FormatOptions::default(),
10001 },
10002 );
10003 assert!(casted_array.is_ok());
10004 assert!(casted_array.unwrap().is_null(0));
10005
10006 let casted_array = cast_with_options(
10007 &array,
10008 &DataType::Decimal128(38, 30),
10009 &CastOptions {
10010 safe: false,
10011 format_options: FormatOptions::default(),
10012 },
10013 );
10014 assert!(casted_array.is_err());
10015 }
10016
10017 #[test]
10018 fn test_cast_numeric_to_decimal256_overflow() {
10019 let array = Int64Array::from(vec![i64::MAX]);
10020 let array = Arc::new(array) as ArrayRef;
10021 let casted_array = cast_with_options(
10022 &array,
10023 &DataType::Decimal256(76, 76),
10024 &CastOptions {
10025 safe: true,
10026 format_options: FormatOptions::default(),
10027 },
10028 );
10029 assert!(casted_array.is_ok());
10030 assert!(casted_array.unwrap().is_null(0));
10031
10032 let casted_array = cast_with_options(
10033 &array,
10034 &DataType::Decimal256(76, 76),
10035 &CastOptions {
10036 safe: false,
10037 format_options: FormatOptions::default(),
10038 },
10039 );
10040 assert!(casted_array.is_err());
10041 }
10042
10043 #[test]
10044 fn test_cast_floating_point_to_decimal128_precision_overflow() {
10045 let array = Float64Array::from(vec![1.1]);
10046 let array = Arc::new(array) as ArrayRef;
10047 let casted_array = cast_with_options(
10048 &array,
10049 &DataType::Decimal128(2, 2),
10050 &CastOptions {
10051 safe: true,
10052 format_options: FormatOptions::default(),
10053 },
10054 );
10055 assert!(casted_array.is_ok());
10056 assert!(casted_array.unwrap().is_null(0));
10057
10058 let casted_array = cast_with_options(
10059 &array,
10060 &DataType::Decimal128(2, 2),
10061 &CastOptions {
10062 safe: false,
10063 format_options: FormatOptions::default(),
10064 },
10065 );
10066 let err = casted_array.unwrap_err().to_string();
10067 let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal128 of precision 2. Max is 0.99";
10068 assert!(
10069 err.contains(expected_error),
10070 "did not find expected error '{expected_error}' in actual error '{err}'"
10071 );
10072 }
10073
10074 #[test]
10075 fn test_cast_floating_point_to_decimal256_precision_overflow() {
10076 let array = Float64Array::from(vec![1.1]);
10077 let array = Arc::new(array) as ArrayRef;
10078 let casted_array = cast_with_options(
10079 &array,
10080 &DataType::Decimal256(2, 2),
10081 &CastOptions {
10082 safe: true,
10083 format_options: FormatOptions::default(),
10084 },
10085 );
10086 assert!(casted_array.is_ok());
10087 assert!(casted_array.unwrap().is_null(0));
10088
10089 let casted_array = cast_with_options(
10090 &array,
10091 &DataType::Decimal256(2, 2),
10092 &CastOptions {
10093 safe: false,
10094 format_options: FormatOptions::default(),
10095 },
10096 );
10097 let err = casted_array.unwrap_err().to_string();
10098 let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal256 of precision 2. Max is 0.99";
10099 assert_eq!(err, expected_error);
10100 }
10101
10102 #[test]
10103 fn test_cast_floating_point_to_decimal128_overflow() {
10104 let array = Float64Array::from(vec![f64::MAX]);
10105 let array = Arc::new(array) as ArrayRef;
10106 let casted_array = cast_with_options(
10107 &array,
10108 &DataType::Decimal128(38, 30),
10109 &CastOptions {
10110 safe: true,
10111 format_options: FormatOptions::default(),
10112 },
10113 );
10114 assert!(casted_array.is_ok());
10115 assert!(casted_array.unwrap().is_null(0));
10116
10117 let casted_array = cast_with_options(
10118 &array,
10119 &DataType::Decimal128(38, 30),
10120 &CastOptions {
10121 safe: false,
10122 format_options: FormatOptions::default(),
10123 },
10124 );
10125 let err = casted_array.unwrap_err().to_string();
10126 let expected_error = "Cast error: Cannot cast to Decimal128(38, 30)";
10127 assert!(
10128 err.contains(expected_error),
10129 "did not find expected error '{expected_error}' in actual error '{err}'"
10130 );
10131 }
10132
10133 #[test]
10134 fn test_cast_floating_point_to_decimal256_overflow() {
10135 let array = Float64Array::from(vec![f64::MAX]);
10136 let array = Arc::new(array) as ArrayRef;
10137 let casted_array = cast_with_options(
10138 &array,
10139 &DataType::Decimal256(76, 50),
10140 &CastOptions {
10141 safe: true,
10142 format_options: FormatOptions::default(),
10143 },
10144 );
10145 assert!(casted_array.is_ok());
10146 assert!(casted_array.unwrap().is_null(0));
10147
10148 let casted_array = cast_with_options(
10149 &array,
10150 &DataType::Decimal256(76, 50),
10151 &CastOptions {
10152 safe: false,
10153 format_options: FormatOptions::default(),
10154 },
10155 );
10156 let err = casted_array.unwrap_err().to_string();
10157 let expected_error = "Cast error: Cannot cast to Decimal256(76, 50)";
10158 assert!(
10159 err.contains(expected_error),
10160 "did not find expected error '{expected_error}' in actual error '{err}'"
10161 );
10162 }
10163 #[test]
10164 fn test_cast_decimal256_to_f64_no_overflow() {
10165 let array = vec![Some(i256::MAX)];
10167 let array = create_decimal256_array(array, 76, 2).unwrap();
10168 let array = Arc::new(array) as ArrayRef;
10169
10170 let result = cast(&array, &DataType::Float64).unwrap();
10171 let result = result.as_primitive::<Float64Type>();
10172 assert!(result.value(0).is_finite());
10173 assert!(result.value(0) > 0.0); let array = vec![Some(i256::MIN)];
10177 let array = create_decimal256_array(array, 76, 2).unwrap();
10178 let array = Arc::new(array) as ArrayRef;
10179
10180 let result = cast(&array, &DataType::Float64).unwrap();
10181 let result = result.as_primitive::<Float64Type>();
10182 assert!(result.value(0).is_finite());
10183 assert!(result.value(0) < 0.0); }
10185
10186 #[test]
10187 fn test_cast_decimal128_to_decimal128_negative_scale() {
10188 let input_type = DataType::Decimal128(20, 0);
10189 let output_type = DataType::Decimal128(20, -1);
10190 assert!(can_cast_types(&input_type, &output_type));
10191 let array = vec![Some(1123450), Some(2123455), Some(3123456), None];
10192 let input_decimal_array = create_decimal128_array(array, 20, 0).unwrap();
10193 let array = Arc::new(input_decimal_array) as ArrayRef;
10194 generate_cast_test_case!(
10195 &array,
10196 Decimal128Array,
10197 &output_type,
10198 vec![
10199 Some(112345_i128),
10200 Some(212346_i128),
10201 Some(312346_i128),
10202 None
10203 ]
10204 );
10205
10206 let casted_array = cast(&array, &output_type).unwrap();
10207 let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10208
10209 assert_eq!("1123450", decimal_arr.value_as_string(0));
10210 assert_eq!("2123460", decimal_arr.value_as_string(1));
10211 assert_eq!("3123460", decimal_arr.value_as_string(2));
10212 }
10213
10214 #[test]
10215 fn decimal128_min_max_to_f64() {
10216 let min128 = i128::MIN;
10218 let max128 = i128::MAX;
10219 assert_eq!(min128 as f64, min128 as f64);
10220 assert_eq!(max128 as f64, max128 as f64);
10221 }
10222
10223 #[test]
10224 fn test_cast_numeric_to_decimal128_negative() {
10225 let decimal_type = DataType::Decimal128(38, -1);
10226 let array = Arc::new(Int32Array::from(vec![
10227 Some(1123456),
10228 Some(2123456),
10229 Some(3123456),
10230 ])) as ArrayRef;
10231
10232 let casted_array = cast(&array, &decimal_type).unwrap();
10233 let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10234
10235 assert_eq!("1123450", decimal_arr.value_as_string(0));
10236 assert_eq!("2123450", decimal_arr.value_as_string(1));
10237 assert_eq!("3123450", decimal_arr.value_as_string(2));
10238
10239 let array = Arc::new(Float32Array::from(vec![
10240 Some(1123.456),
10241 Some(2123.456),
10242 Some(3123.456),
10243 ])) as ArrayRef;
10244
10245 let casted_array = cast(&array, &decimal_type).unwrap();
10246 let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10247
10248 assert_eq!("1120", decimal_arr.value_as_string(0));
10249 assert_eq!("2120", decimal_arr.value_as_string(1));
10250 assert_eq!("3120", decimal_arr.value_as_string(2));
10251 }
10252
10253 #[test]
10254 fn test_cast_decimal128_to_decimal128_negative() {
10255 let input_type = DataType::Decimal128(10, -1);
10256 let output_type = DataType::Decimal128(10, -2);
10257 assert!(can_cast_types(&input_type, &output_type));
10258 let array = vec![Some(123)];
10259 let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
10260 let array = Arc::new(input_decimal_array) as ArrayRef;
10261 generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(12_i128),]);
10262
10263 let casted_array = cast(&array, &output_type).unwrap();
10264 let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10265
10266 assert_eq!("1200", decimal_arr.value_as_string(0));
10267
10268 let array = vec![Some(125)];
10269 let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
10270 let array = Arc::new(input_decimal_array) as ArrayRef;
10271 generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(13_i128),]);
10272
10273 let casted_array = cast(&array, &output_type).unwrap();
10274 let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10275
10276 assert_eq!("1300", decimal_arr.value_as_string(0));
10277 }
10278
10279 #[test]
10280 fn test_cast_decimal128_to_decimal256_negative() {
10281 let input_type = DataType::Decimal128(10, 3);
10282 let output_type = DataType::Decimal256(10, 5);
10283 assert!(can_cast_types(&input_type, &output_type));
10284 let array = vec![Some(123456), Some(-123456)];
10285 let input_decimal_array = create_decimal128_array(array, 10, 3).unwrap();
10286 let array = Arc::new(input_decimal_array) as ArrayRef;
10287
10288 let hundred = i256::from_i128(100);
10289 generate_cast_test_case!(
10290 &array,
10291 Decimal256Array,
10292 &output_type,
10293 vec![
10294 Some(i256::from_i128(123456).mul_wrapping(hundred)),
10295 Some(i256::from_i128(-123456).mul_wrapping(hundred))
10296 ]
10297 );
10298 }
10299
10300 #[test]
10301 fn test_parse_string_to_decimal() {
10302 assert_eq!(
10303 Decimal128Type::format_decimal(
10304 parse_string_to_decimal_native::<Decimal128Type>("123.45", 2).unwrap(),
10305 38,
10306 2,
10307 ),
10308 "123.45"
10309 );
10310 assert_eq!(
10311 Decimal128Type::format_decimal(
10312 parse_string_to_decimal_native::<Decimal128Type>("12345", 2).unwrap(),
10313 38,
10314 2,
10315 ),
10316 "12345.00"
10317 );
10318 assert_eq!(
10319 Decimal128Type::format_decimal(
10320 parse_string_to_decimal_native::<Decimal128Type>("0.12345", 2).unwrap(),
10321 38,
10322 2,
10323 ),
10324 "0.12"
10325 );
10326 assert_eq!(
10327 Decimal128Type::format_decimal(
10328 parse_string_to_decimal_native::<Decimal128Type>(".12345", 2).unwrap(),
10329 38,
10330 2,
10331 ),
10332 "0.12"
10333 );
10334 assert_eq!(
10335 Decimal128Type::format_decimal(
10336 parse_string_to_decimal_native::<Decimal128Type>(".1265", 2).unwrap(),
10337 38,
10338 2,
10339 ),
10340 "0.13"
10341 );
10342 assert_eq!(
10343 Decimal128Type::format_decimal(
10344 parse_string_to_decimal_native::<Decimal128Type>(".1265", 2).unwrap(),
10345 38,
10346 2,
10347 ),
10348 "0.13"
10349 );
10350
10351 assert_eq!(
10352 Decimal256Type::format_decimal(
10353 parse_string_to_decimal_native::<Decimal256Type>("123.45", 3).unwrap(),
10354 38,
10355 3,
10356 ),
10357 "123.450"
10358 );
10359 assert_eq!(
10360 Decimal256Type::format_decimal(
10361 parse_string_to_decimal_native::<Decimal256Type>("12345", 3).unwrap(),
10362 38,
10363 3,
10364 ),
10365 "12345.000"
10366 );
10367 assert_eq!(
10368 Decimal256Type::format_decimal(
10369 parse_string_to_decimal_native::<Decimal256Type>("0.12345", 3).unwrap(),
10370 38,
10371 3,
10372 ),
10373 "0.123"
10374 );
10375 assert_eq!(
10376 Decimal256Type::format_decimal(
10377 parse_string_to_decimal_native::<Decimal256Type>(".12345", 3).unwrap(),
10378 38,
10379 3,
10380 ),
10381 "0.123"
10382 );
10383 assert_eq!(
10384 Decimal256Type::format_decimal(
10385 parse_string_to_decimal_native::<Decimal256Type>(".1265", 3).unwrap(),
10386 38,
10387 3,
10388 ),
10389 "0.127"
10390 );
10391 }
10392
10393 fn test_cast_string_to_decimal(array: ArrayRef) {
10394 let output_type = DataType::Decimal128(38, 2);
10396 assert!(can_cast_types(array.data_type(), &output_type));
10397
10398 let casted_array = cast(&array, &output_type).unwrap();
10399 let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10400
10401 assert_eq!("123.45", decimal_arr.value_as_string(0));
10402 assert_eq!("1.23", decimal_arr.value_as_string(1));
10403 assert_eq!("0.12", decimal_arr.value_as_string(2));
10404 assert_eq!("0.13", decimal_arr.value_as_string(3));
10405 assert_eq!("1.26", decimal_arr.value_as_string(4));
10406 assert_eq!("12345.00", decimal_arr.value_as_string(5));
10407 assert_eq!("12345.00", decimal_arr.value_as_string(6));
10408 assert_eq!("0.12", decimal_arr.value_as_string(7));
10409 assert_eq!("12.23", decimal_arr.value_as_string(8));
10410 assert!(decimal_arr.is_null(9));
10411 assert_eq!("0.00", decimal_arr.value_as_string(10));
10412 assert_eq!("0.00", decimal_arr.value_as_string(11));
10413 assert!(decimal_arr.is_null(12));
10414 assert_eq!("-1.23", decimal_arr.value_as_string(13));
10415 assert_eq!("-1.24", decimal_arr.value_as_string(14));
10416 assert_eq!("0.00", decimal_arr.value_as_string(15));
10417 assert_eq!("-123.00", decimal_arr.value_as_string(16));
10418 assert_eq!("-123.23", decimal_arr.value_as_string(17));
10419 assert_eq!("-0.12", decimal_arr.value_as_string(18));
10420 assert_eq!("1.23", decimal_arr.value_as_string(19));
10421 assert_eq!("1.24", decimal_arr.value_as_string(20));
10422 assert_eq!("0.00", decimal_arr.value_as_string(21));
10423 assert_eq!("123.00", decimal_arr.value_as_string(22));
10424 assert_eq!("123.23", decimal_arr.value_as_string(23));
10425 assert_eq!("0.12", decimal_arr.value_as_string(24));
10426 assert!(decimal_arr.is_null(25));
10427 assert!(decimal_arr.is_null(26));
10428 assert!(decimal_arr.is_null(27));
10429 assert_eq!("0.00", decimal_arr.value_as_string(28));
10430 assert_eq!("0.00", decimal_arr.value_as_string(29));
10431 assert_eq!("12345.00", decimal_arr.value_as_string(30));
10432 assert_eq!(decimal_arr.len(), 31);
10433
10434 let output_type = DataType::Decimal256(76, 3);
10436 assert!(can_cast_types(array.data_type(), &output_type));
10437
10438 let casted_array = cast(&array, &output_type).unwrap();
10439 let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
10440
10441 assert_eq!("123.450", decimal_arr.value_as_string(0));
10442 assert_eq!("1.235", decimal_arr.value_as_string(1));
10443 assert_eq!("0.123", decimal_arr.value_as_string(2));
10444 assert_eq!("0.127", decimal_arr.value_as_string(3));
10445 assert_eq!("1.263", decimal_arr.value_as_string(4));
10446 assert_eq!("12345.000", decimal_arr.value_as_string(5));
10447 assert_eq!("12345.000", decimal_arr.value_as_string(6));
10448 assert_eq!("0.123", decimal_arr.value_as_string(7));
10449 assert_eq!("12.234", decimal_arr.value_as_string(8));
10450 assert!(decimal_arr.is_null(9));
10451 assert_eq!("0.000", decimal_arr.value_as_string(10));
10452 assert_eq!("0.000", decimal_arr.value_as_string(11));
10453 assert!(decimal_arr.is_null(12));
10454 assert_eq!("-1.235", decimal_arr.value_as_string(13));
10455 assert_eq!("-1.236", decimal_arr.value_as_string(14));
10456 assert_eq!("0.000", decimal_arr.value_as_string(15));
10457 assert_eq!("-123.000", decimal_arr.value_as_string(16));
10458 assert_eq!("-123.234", decimal_arr.value_as_string(17));
10459 assert_eq!("-0.123", decimal_arr.value_as_string(18));
10460 assert_eq!("1.235", decimal_arr.value_as_string(19));
10461 assert_eq!("1.236", decimal_arr.value_as_string(20));
10462 assert_eq!("0.000", decimal_arr.value_as_string(21));
10463 assert_eq!("123.000", decimal_arr.value_as_string(22));
10464 assert_eq!("123.234", decimal_arr.value_as_string(23));
10465 assert_eq!("0.123", decimal_arr.value_as_string(24));
10466 assert!(decimal_arr.is_null(25));
10467 assert!(decimal_arr.is_null(26));
10468 assert!(decimal_arr.is_null(27));
10469 assert_eq!("0.000", decimal_arr.value_as_string(28));
10470 assert_eq!("0.000", decimal_arr.value_as_string(29));
10471 assert_eq!("12345.000", decimal_arr.value_as_string(30));
10472 assert_eq!(decimal_arr.len(), 31);
10473 }
10474
10475 #[test]
10476 fn test_cast_utf8_to_decimal() {
10477 let str_array = StringArray::from(vec![
10478 Some("123.45"),
10479 Some("1.2345"),
10480 Some("0.12345"),
10481 Some("0.1267"),
10482 Some("1.263"),
10483 Some("12345.0"),
10484 Some("12345"),
10485 Some("000.123"),
10486 Some("12.234000"),
10487 None,
10488 Some(""),
10489 Some(" "),
10490 None,
10491 Some("-1.23499999"),
10492 Some("-1.23599999"),
10493 Some("-0.00001"),
10494 Some("-123"),
10495 Some("-123.234000"),
10496 Some("-000.123"),
10497 Some("+1.23499999"),
10498 Some("+1.23599999"),
10499 Some("+0.00001"),
10500 Some("+123"),
10501 Some("+123.234000"),
10502 Some("+000.123"),
10503 Some("1.-23499999"),
10504 Some("-1.-23499999"),
10505 Some("--1.23499999"),
10506 Some("0"),
10507 Some("000.000"),
10508 Some("0000000000000000012345.000"),
10509 ]);
10510 let array = Arc::new(str_array) as ArrayRef;
10511
10512 test_cast_string_to_decimal(array);
10513
10514 let test_cases = [
10515 (None, None),
10516 (Some("0"), Some("0")),
10519 (Some("000.000"), Some("0")),
10520 (Some("12345"), Some("12345")),
10521 (Some("000000000000000000000000000012345"), Some("12345")),
10522 (Some("-123"), Some("-123")),
10523 (Some("+123"), Some("123")),
10524 ];
10525 let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
10526 let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
10527
10528 let array = Arc::new(StringArray::from(inputs)) as ArrayRef;
10529 test_cast_string_to_decimal_scale_zero(array, &expected);
10530 }
10531
10532 #[test]
10533 fn test_cast_large_utf8_to_decimal() {
10534 let str_array = LargeStringArray::from(vec![
10535 Some("123.45"),
10536 Some("1.2345"),
10537 Some("0.12345"),
10538 Some("0.1267"),
10539 Some("1.263"),
10540 Some("12345.0"),
10541 Some("12345"),
10542 Some("000.123"),
10543 Some("12.234000"),
10544 None,
10545 Some(""),
10546 Some(" "),
10547 None,
10548 Some("-1.23499999"),
10549 Some("-1.23599999"),
10550 Some("-0.00001"),
10551 Some("-123"),
10552 Some("-123.234000"),
10553 Some("-000.123"),
10554 Some("+1.23499999"),
10555 Some("+1.23599999"),
10556 Some("+0.00001"),
10557 Some("+123"),
10558 Some("+123.234000"),
10559 Some("+000.123"),
10560 Some("1.-23499999"),
10561 Some("-1.-23499999"),
10562 Some("--1.23499999"),
10563 Some("0"),
10564 Some("000.000"),
10565 Some("0000000000000000012345.000"),
10566 ]);
10567 let array = Arc::new(str_array) as ArrayRef;
10568
10569 test_cast_string_to_decimal(array);
10570
10571 let test_cases = [
10572 (None, None),
10573 (Some(""), None),
10574 (Some(" "), None),
10575 (Some("0"), Some("0")),
10576 (Some("000.000"), Some("0")),
10577 (Some("12345"), Some("12345")),
10578 (Some("000000000000000000000000000012345"), Some("12345")),
10579 (Some("-123"), Some("-123")),
10580 (Some("+123"), Some("123")),
10581 ];
10582 let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
10583 let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
10584
10585 let array = Arc::new(LargeStringArray::from(inputs)) as ArrayRef;
10586 test_cast_string_to_decimal_scale_zero(array, &expected);
10587 }
10588
10589 fn test_cast_string_to_decimal_scale_zero(
10590 array: ArrayRef,
10591 expected_as_string: &[Option<&str>],
10592 ) {
10593 let output_type = DataType::Decimal128(38, 0);
10595 assert!(can_cast_types(array.data_type(), &output_type));
10596 let casted_array = cast(&array, &output_type).unwrap();
10597 let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10598 assert_decimal_array_contents(decimal_arr, expected_as_string);
10599
10600 let output_type = DataType::Decimal256(76, 0);
10602 assert!(can_cast_types(array.data_type(), &output_type));
10603 let casted_array = cast(&array, &output_type).unwrap();
10604 let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
10605 assert_decimal_array_contents(decimal_arr, expected_as_string);
10606 }
10607
10608 fn assert_decimal_array_contents<T>(
10609 array: &PrimitiveArray<T>,
10610 expected_as_string: &[Option<&str>],
10611 ) where
10612 T: DecimalType + ArrowPrimitiveType,
10613 {
10614 assert_eq!(array.len(), expected_as_string.len());
10615 for (i, expected) in expected_as_string.iter().enumerate() {
10616 let actual = if array.is_null(i) {
10617 None
10618 } else {
10619 Some(array.value_as_string(i))
10620 };
10621 let actual = actual.as_ref().map(|s| s.as_ref());
10622 assert_eq!(*expected, actual, "Expected at position {i}");
10623 }
10624 }
10625
10626 #[test]
10627 fn test_cast_invalid_utf8_to_decimal() {
10628 let str_array = StringArray::from(vec!["4.4.5", ". 0.123"]);
10629 let array = Arc::new(str_array) as ArrayRef;
10630
10631 let output_type = DataType::Decimal128(38, 2);
10633 let casted_array = cast(&array, &output_type).unwrap();
10634 assert!(casted_array.is_null(0));
10635 assert!(casted_array.is_null(1));
10636
10637 let output_type = DataType::Decimal256(76, 2);
10638 let casted_array = cast(&array, &output_type).unwrap();
10639 assert!(casted_array.is_null(0));
10640 assert!(casted_array.is_null(1));
10641
10642 let output_type = DataType::Decimal128(38, 2);
10644 let str_array = StringArray::from(vec!["4.4.5"]);
10645 let array = Arc::new(str_array) as ArrayRef;
10646 let option = CastOptions {
10647 safe: false,
10648 format_options: FormatOptions::default(),
10649 };
10650 let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
10651 assert!(
10652 casted_err
10653 .to_string()
10654 .contains("Cannot cast string '4.4.5' to value of Decimal128(38, 10) type")
10655 );
10656
10657 let str_array = StringArray::from(vec![". 0.123"]);
10658 let array = Arc::new(str_array) as ArrayRef;
10659 let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
10660 assert!(
10661 casted_err
10662 .to_string()
10663 .contains("Cannot cast string '. 0.123' to value of Decimal128(38, 10) type")
10664 );
10665 }
10666
10667 fn test_cast_string_to_decimal128_overflow(overflow_array: ArrayRef) {
10668 let output_type = DataType::Decimal128(38, 2);
10669 let casted_array = cast(&overflow_array, &output_type).unwrap();
10670 let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10671
10672 assert!(decimal_arr.is_null(0));
10673 assert!(decimal_arr.is_null(1));
10674 assert!(decimal_arr.is_null(2));
10675 assert_eq!(
10676 "999999999999999999999999999999999999.99",
10677 decimal_arr.value_as_string(3)
10678 );
10679 assert_eq!(
10680 "100000000000000000000000000000000000.00",
10681 decimal_arr.value_as_string(4)
10682 );
10683 }
10684
10685 #[test]
10686 fn test_cast_string_to_decimal128_precision_overflow() {
10687 let array = StringArray::from(vec!["1000".to_string()]);
10688 let array = Arc::new(array) as ArrayRef;
10689 let casted_array = cast_with_options(
10690 &array,
10691 &DataType::Decimal128(10, 8),
10692 &CastOptions {
10693 safe: true,
10694 format_options: FormatOptions::default(),
10695 },
10696 );
10697 assert!(casted_array.is_ok());
10698 assert!(casted_array.unwrap().is_null(0));
10699
10700 let err = cast_with_options(
10701 &array,
10702 &DataType::Decimal128(10, 8),
10703 &CastOptions {
10704 safe: false,
10705 format_options: FormatOptions::default(),
10706 },
10707 );
10708 assert_eq!(
10709 "Invalid argument error: 1000.00000000 is too large to store in a Decimal128 of precision 10. Max is 99.99999999",
10710 err.unwrap_err().to_string()
10711 );
10712 }
10713
10714 #[test]
10715 fn test_cast_utf8_to_decimal128_overflow() {
10716 let overflow_str_array = StringArray::from(vec![
10717 i128::MAX.to_string(),
10718 i128::MIN.to_string(),
10719 "99999999999999999999999999999999999999".to_string(),
10720 "999999999999999999999999999999999999.99".to_string(),
10721 "99999999999999999999999999999999999.999".to_string(),
10722 ]);
10723 let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
10724
10725 test_cast_string_to_decimal128_overflow(overflow_array);
10726 }
10727
10728 #[test]
10729 fn test_cast_large_utf8_to_decimal128_overflow() {
10730 let overflow_str_array = LargeStringArray::from(vec![
10731 i128::MAX.to_string(),
10732 i128::MIN.to_string(),
10733 "99999999999999999999999999999999999999".to_string(),
10734 "999999999999999999999999999999999999.99".to_string(),
10735 "99999999999999999999999999999999999.999".to_string(),
10736 ]);
10737 let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
10738
10739 test_cast_string_to_decimal128_overflow(overflow_array);
10740 }
10741
10742 fn test_cast_string_to_decimal256_overflow(overflow_array: ArrayRef) {
10743 let output_type = DataType::Decimal256(76, 2);
10744 let casted_array = cast(&overflow_array, &output_type).unwrap();
10745 let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
10746
10747 assert_eq!(
10748 "170141183460469231731687303715884105727.00",
10749 decimal_arr.value_as_string(0)
10750 );
10751 assert_eq!(
10752 "-170141183460469231731687303715884105728.00",
10753 decimal_arr.value_as_string(1)
10754 );
10755 assert_eq!(
10756 "99999999999999999999999999999999999999.00",
10757 decimal_arr.value_as_string(2)
10758 );
10759 assert_eq!(
10760 "999999999999999999999999999999999999.99",
10761 decimal_arr.value_as_string(3)
10762 );
10763 assert_eq!(
10764 "100000000000000000000000000000000000.00",
10765 decimal_arr.value_as_string(4)
10766 );
10767 assert!(decimal_arr.is_null(5));
10768 assert!(decimal_arr.is_null(6));
10769 }
10770
10771 #[test]
10772 fn test_cast_string_to_decimal256_precision_overflow() {
10773 let array = StringArray::from(vec!["1000".to_string()]);
10774 let array = Arc::new(array) as ArrayRef;
10775 let casted_array = cast_with_options(
10776 &array,
10777 &DataType::Decimal256(10, 8),
10778 &CastOptions {
10779 safe: true,
10780 format_options: FormatOptions::default(),
10781 },
10782 );
10783 assert!(casted_array.is_ok());
10784 assert!(casted_array.unwrap().is_null(0));
10785
10786 let err = cast_with_options(
10787 &array,
10788 &DataType::Decimal256(10, 8),
10789 &CastOptions {
10790 safe: false,
10791 format_options: FormatOptions::default(),
10792 },
10793 );
10794 assert_eq!(
10795 "Invalid argument error: 1000.00000000 is too large to store in a Decimal256 of precision 10. Max is 99.99999999",
10796 err.unwrap_err().to_string()
10797 );
10798 }
10799
10800 #[test]
10801 fn test_cast_utf8_to_decimal256_overflow() {
10802 let overflow_str_array = StringArray::from(vec![
10803 i128::MAX.to_string(),
10804 i128::MIN.to_string(),
10805 "99999999999999999999999999999999999999".to_string(),
10806 "999999999999999999999999999999999999.99".to_string(),
10807 "99999999999999999999999999999999999.999".to_string(),
10808 i256::MAX.to_string(),
10809 i256::MIN.to_string(),
10810 ]);
10811 let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
10812
10813 test_cast_string_to_decimal256_overflow(overflow_array);
10814 }
10815
10816 #[test]
10817 fn test_cast_large_utf8_to_decimal256_overflow() {
10818 let overflow_str_array = LargeStringArray::from(vec![
10819 i128::MAX.to_string(),
10820 i128::MIN.to_string(),
10821 "99999999999999999999999999999999999999".to_string(),
10822 "999999999999999999999999999999999999.99".to_string(),
10823 "99999999999999999999999999999999999.999".to_string(),
10824 i256::MAX.to_string(),
10825 i256::MIN.to_string(),
10826 ]);
10827 let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
10828
10829 test_cast_string_to_decimal256_overflow(overflow_array);
10830 }
10831
10832 #[test]
10833 fn test_cast_outside_supported_range_for_nanoseconds() {
10834 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";
10835
10836 let array = StringArray::from(vec![Some("1650-01-01 01:01:01.000001")]);
10837
10838 let cast_options = CastOptions {
10839 safe: false,
10840 format_options: FormatOptions::default(),
10841 };
10842
10843 let result = cast_string_to_timestamp::<i32, TimestampNanosecondType>(
10844 &array,
10845 &None::<Arc<str>>,
10846 &cast_options,
10847 );
10848
10849 let err = result.unwrap_err();
10850 assert_eq!(
10851 err.to_string(),
10852 format!(
10853 "Cast error: Overflow converting {} to Nanosecond. {}",
10854 array.value(0),
10855 EXPECTED_ERROR_MESSAGE
10856 )
10857 );
10858 }
10859
10860 #[test]
10861 fn test_cast_date32_to_timestamp() {
10862 let a = Date32Array::from(vec![Some(18628), Some(18993), None]); let array = Arc::new(a) as ArrayRef;
10864 let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
10865 let c = b.as_primitive::<TimestampSecondType>();
10866 assert_eq!(1609459200, c.value(0));
10867 assert_eq!(1640995200, c.value(1));
10868 assert!(c.is_null(2));
10869 }
10870
10871 #[test]
10872 fn test_cast_date32_to_timestamp_ms() {
10873 let a = Date32Array::from(vec![Some(18628), Some(18993), None]); let array = Arc::new(a) as ArrayRef;
10875 let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
10876 let c = b
10877 .as_any()
10878 .downcast_ref::<TimestampMillisecondArray>()
10879 .unwrap();
10880 assert_eq!(1609459200000, c.value(0));
10881 assert_eq!(1640995200000, c.value(1));
10882 assert!(c.is_null(2));
10883 }
10884
10885 #[test]
10886 fn test_cast_date32_to_timestamp_us() {
10887 let a = Date32Array::from(vec![Some(18628), Some(18993), None]); let array = Arc::new(a) as ArrayRef;
10889 let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
10890 let c = b
10891 .as_any()
10892 .downcast_ref::<TimestampMicrosecondArray>()
10893 .unwrap();
10894 assert_eq!(1609459200000000, c.value(0));
10895 assert_eq!(1640995200000000, c.value(1));
10896 assert!(c.is_null(2));
10897 }
10898
10899 #[test]
10900 fn test_cast_date32_to_timestamp_ns() {
10901 let a = Date32Array::from(vec![Some(18628), Some(18993), None]); let array = Arc::new(a) as ArrayRef;
10903 let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
10904 let c = b
10905 .as_any()
10906 .downcast_ref::<TimestampNanosecondArray>()
10907 .unwrap();
10908 assert_eq!(1609459200000000000, c.value(0));
10909 assert_eq!(1640995200000000000, c.value(1));
10910 assert!(c.is_null(2));
10911 }
10912
10913 #[test]
10914 fn test_timezone_cast() {
10915 let a = StringArray::from(vec![
10916 "2000-01-01T12:00:00", "2020-12-15T12:34:56", ]);
10919 let array = Arc::new(a) as ArrayRef;
10920 let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
10921 let v = b.as_primitive::<TimestampNanosecondType>();
10922
10923 assert_eq!(v.value(0), 946728000000000000);
10924 assert_eq!(v.value(1), 1608035696000000000);
10925
10926 let b = cast(
10927 &b,
10928 &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
10929 )
10930 .unwrap();
10931 let v = b.as_primitive::<TimestampNanosecondType>();
10932
10933 assert_eq!(v.value(0), 946728000000000000);
10934 assert_eq!(v.value(1), 1608035696000000000);
10935
10936 let b = cast(
10937 &b,
10938 &DataType::Timestamp(TimeUnit::Millisecond, Some("+02:00".into())),
10939 )
10940 .unwrap();
10941 let v = b.as_primitive::<TimestampMillisecondType>();
10942
10943 assert_eq!(v.value(0), 946728000000);
10944 assert_eq!(v.value(1), 1608035696000);
10945 }
10946
10947 #[test]
10948 fn test_cast_utf8_to_timestamp() {
10949 fn test_tz(tz: Arc<str>) {
10950 let valid = StringArray::from(vec![
10951 "2023-01-01 04:05:06.789000-08:00",
10952 "2023-01-01 04:05:06.789000-07:00",
10953 "2023-01-01 04:05:06.789 -0800",
10954 "2023-01-01 04:05:06.789 -08:00",
10955 "2023-01-01 040506 +0730",
10956 "2023-01-01 040506 +07:30",
10957 "2023-01-01 04:05:06.789",
10958 "2023-01-01 04:05:06",
10959 "2023-01-01",
10960 ]);
10961
10962 let array = Arc::new(valid) as ArrayRef;
10963 let b = cast_with_options(
10964 &array,
10965 &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.clone())),
10966 &CastOptions {
10967 safe: false,
10968 format_options: FormatOptions::default(),
10969 },
10970 )
10971 .unwrap();
10972
10973 let tz = tz.as_ref().parse().unwrap();
10974
10975 let as_tz =
10976 |v: i64| as_datetime_with_timezone::<TimestampNanosecondType>(v, tz).unwrap();
10977
10978 let as_utc = |v: &i64| as_tz(*v).naive_utc().to_string();
10979 let as_local = |v: &i64| as_tz(*v).naive_local().to_string();
10980
10981 let values = b.as_primitive::<TimestampNanosecondType>().values();
10982 let utc_results: Vec<_> = values.iter().map(as_utc).collect();
10983 let local_results: Vec<_> = values.iter().map(as_local).collect();
10984
10985 assert_eq!(
10987 &utc_results[..6],
10988 &[
10989 "2023-01-01 12:05:06.789".to_string(),
10990 "2023-01-01 11:05:06.789".to_string(),
10991 "2023-01-01 12:05:06.789".to_string(),
10992 "2023-01-01 12:05:06.789".to_string(),
10993 "2022-12-31 20:35:06".to_string(),
10994 "2022-12-31 20:35:06".to_string(),
10995 ]
10996 );
10997 assert_eq!(
10999 &local_results[6..],
11000 &[
11001 "2023-01-01 04:05:06.789".to_string(),
11002 "2023-01-01 04:05:06".to_string(),
11003 "2023-01-01 00:00:00".to_string()
11004 ]
11005 )
11006 }
11007
11008 test_tz("+00:00".into());
11009 test_tz("+02:00".into());
11010 }
11011
11012 #[test]
11013 fn test_cast_invalid_utf8() {
11014 let v1: &[u8] = b"\xFF invalid";
11015 let v2: &[u8] = b"\x00 Foo";
11016 let s = BinaryArray::from(vec![v1, v2]);
11017 let options = CastOptions {
11018 safe: true,
11019 format_options: FormatOptions::default(),
11020 };
11021 let array = cast_with_options(&s, &DataType::Utf8, &options).unwrap();
11022 let a = array.as_string::<i32>();
11023 a.to_data().validate_full().unwrap();
11024
11025 assert_eq!(a.null_count(), 1);
11026 assert_eq!(a.len(), 2);
11027 assert!(a.is_null(0));
11028 assert_eq!(a.value(0), "");
11029 assert_eq!(a.value(1), "\x00 Foo");
11030 }
11031
11032 #[test]
11033 fn test_cast_utf8_to_timestamptz() {
11034 let valid = StringArray::from(vec!["2023-01-01"]);
11035
11036 let array = Arc::new(valid) as ArrayRef;
11037 let b = cast(
11038 &array,
11039 &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
11040 )
11041 .unwrap();
11042
11043 let expect = DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into()));
11044
11045 assert_eq!(b.data_type(), &expect);
11046 let c = b
11047 .as_any()
11048 .downcast_ref::<TimestampNanosecondArray>()
11049 .unwrap();
11050 assert_eq!(1672531200000000000, c.value(0));
11051 }
11052
11053 #[test]
11054 fn test_cast_decimal_to_string() {
11055 assert!(can_cast_types(
11056 &DataType::Decimal32(9, 4),
11057 &DataType::Utf8View
11058 ));
11059 assert!(can_cast_types(
11060 &DataType::Decimal64(16, 4),
11061 &DataType::Utf8View
11062 ));
11063 assert!(can_cast_types(
11064 &DataType::Decimal128(10, 4),
11065 &DataType::Utf8View
11066 ));
11067 assert!(can_cast_types(
11068 &DataType::Decimal256(38, 10),
11069 &DataType::Utf8View
11070 ));
11071
11072 macro_rules! assert_decimal_values {
11073 ($array:expr) => {
11074 let c = $array;
11075 assert_eq!("1123.454", c.value(0));
11076 assert_eq!("2123.456", c.value(1));
11077 assert_eq!("-3123.453", c.value(2));
11078 assert_eq!("-3123.456", c.value(3));
11079 assert_eq!("0.000", c.value(4));
11080 assert_eq!("0.123", c.value(5));
11081 assert_eq!("1234.567", c.value(6));
11082 assert_eq!("-1234.567", c.value(7));
11083 assert!(c.is_null(8));
11084 };
11085 }
11086
11087 fn test_decimal_to_string<IN: ArrowPrimitiveType, OffsetSize: OffsetSizeTrait>(
11088 output_type: DataType,
11089 array: PrimitiveArray<IN>,
11090 ) {
11091 let b = cast(&array, &output_type).unwrap();
11092
11093 assert_eq!(b.data_type(), &output_type);
11094 match b.data_type() {
11095 DataType::Utf8View => {
11096 let c = b.as_string_view();
11097 assert_decimal_values!(c);
11098 }
11099 DataType::Utf8 | DataType::LargeUtf8 => {
11100 let c = b.as_string::<OffsetSize>();
11101 assert_decimal_values!(c);
11102 }
11103 _ => (),
11104 }
11105 }
11106
11107 let array32: Vec<Option<i32>> = vec![
11108 Some(1123454),
11109 Some(2123456),
11110 Some(-3123453),
11111 Some(-3123456),
11112 Some(0),
11113 Some(123),
11114 Some(123456789),
11115 Some(-123456789),
11116 None,
11117 ];
11118 let array64: Vec<Option<i64>> = array32.iter().map(|num| num.map(|x| x as i64)).collect();
11119 let array128: Vec<Option<i128>> =
11120 array64.iter().map(|num| num.map(|x| x as i128)).collect();
11121 let array256: Vec<Option<i256>> = array128
11122 .iter()
11123 .map(|num| num.map(i256::from_i128))
11124 .collect();
11125
11126 test_decimal_to_string::<Decimal32Type, i32>(
11127 DataType::Utf8View,
11128 create_decimal32_array(array32.clone(), 7, 3).unwrap(),
11129 );
11130 test_decimal_to_string::<Decimal32Type, i32>(
11131 DataType::Utf8,
11132 create_decimal32_array(array32.clone(), 7, 3).unwrap(),
11133 );
11134 test_decimal_to_string::<Decimal32Type, i64>(
11135 DataType::LargeUtf8,
11136 create_decimal32_array(array32, 7, 3).unwrap(),
11137 );
11138
11139 test_decimal_to_string::<Decimal64Type, i32>(
11140 DataType::Utf8View,
11141 create_decimal64_array(array64.clone(), 7, 3).unwrap(),
11142 );
11143 test_decimal_to_string::<Decimal64Type, i32>(
11144 DataType::Utf8,
11145 create_decimal64_array(array64.clone(), 7, 3).unwrap(),
11146 );
11147 test_decimal_to_string::<Decimal64Type, i64>(
11148 DataType::LargeUtf8,
11149 create_decimal64_array(array64, 7, 3).unwrap(),
11150 );
11151
11152 test_decimal_to_string::<Decimal128Type, i32>(
11153 DataType::Utf8View,
11154 create_decimal128_array(array128.clone(), 7, 3).unwrap(),
11155 );
11156 test_decimal_to_string::<Decimal128Type, i32>(
11157 DataType::Utf8,
11158 create_decimal128_array(array128.clone(), 7, 3).unwrap(),
11159 );
11160 test_decimal_to_string::<Decimal128Type, i64>(
11161 DataType::LargeUtf8,
11162 create_decimal128_array(array128, 7, 3).unwrap(),
11163 );
11164
11165 test_decimal_to_string::<Decimal256Type, i32>(
11166 DataType::Utf8View,
11167 create_decimal256_array(array256.clone(), 7, 3).unwrap(),
11168 );
11169 test_decimal_to_string::<Decimal256Type, i32>(
11170 DataType::Utf8,
11171 create_decimal256_array(array256.clone(), 7, 3).unwrap(),
11172 );
11173 test_decimal_to_string::<Decimal256Type, i64>(
11174 DataType::LargeUtf8,
11175 create_decimal256_array(array256, 7, 3).unwrap(),
11176 );
11177 }
11178
11179 #[test]
11180 fn test_cast_numeric_to_decimal128_precision_overflow() {
11181 let array = Int64Array::from(vec![1234567]);
11182 let array = Arc::new(array) as ArrayRef;
11183 let casted_array = cast_with_options(
11184 &array,
11185 &DataType::Decimal128(7, 3),
11186 &CastOptions {
11187 safe: true,
11188 format_options: FormatOptions::default(),
11189 },
11190 );
11191 assert!(casted_array.is_ok());
11192 assert!(casted_array.unwrap().is_null(0));
11193
11194 let err = cast_with_options(
11195 &array,
11196 &DataType::Decimal128(7, 3),
11197 &CastOptions {
11198 safe: false,
11199 format_options: FormatOptions::default(),
11200 },
11201 );
11202 assert_eq!(
11203 "Invalid argument error: 1234567.000 is too large to store in a Decimal128 of precision 7. Max is 9999.999",
11204 err.unwrap_err().to_string()
11205 );
11206 }
11207
11208 #[test]
11209 fn test_cast_numeric_to_decimal256_precision_overflow() {
11210 let array = Int64Array::from(vec![1234567]);
11211 let array = Arc::new(array) as ArrayRef;
11212 let casted_array = cast_with_options(
11213 &array,
11214 &DataType::Decimal256(7, 3),
11215 &CastOptions {
11216 safe: true,
11217 format_options: FormatOptions::default(),
11218 },
11219 );
11220 assert!(casted_array.is_ok());
11221 assert!(casted_array.unwrap().is_null(0));
11222
11223 let err = cast_with_options(
11224 &array,
11225 &DataType::Decimal256(7, 3),
11226 &CastOptions {
11227 safe: false,
11228 format_options: FormatOptions::default(),
11229 },
11230 );
11231 assert_eq!(
11232 "Invalid argument error: 1234567.000 is too large to store in a Decimal256 of precision 7. Max is 9999.999",
11233 err.unwrap_err().to_string()
11234 );
11235 }
11236
11237 fn cast_from_duration_to_interval<T: ArrowTemporalType<Native = i64>>(
11239 array: Vec<i64>,
11240 cast_options: &CastOptions,
11241 ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
11242 let array = PrimitiveArray::<T>::new(array.into(), None);
11243 let array = Arc::new(array) as ArrayRef;
11244 let interval = DataType::Interval(IntervalUnit::MonthDayNano);
11245 let out = cast_with_options(&array, &interval, cast_options)?;
11246 let out = out.as_primitive::<IntervalMonthDayNanoType>().clone();
11247 Ok(out)
11248 }
11249
11250 #[test]
11251 fn test_cast_from_duration_to_interval() {
11252 let array = vec![1234567];
11254 let casted_array =
11255 cast_from_duration_to_interval::<DurationSecondType>(array, &CastOptions::default())
11256 .unwrap();
11257 assert_eq!(
11258 casted_array.data_type(),
11259 &DataType::Interval(IntervalUnit::MonthDayNano)
11260 );
11261 assert_eq!(
11262 casted_array.value(0),
11263 IntervalMonthDayNano::new(0, 0, 1234567000000000)
11264 );
11265
11266 let array = vec![i64::MAX];
11267 let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
11268 array.clone(),
11269 &CastOptions::default(),
11270 )
11271 .unwrap();
11272 assert!(!casted_array.is_valid(0));
11273
11274 let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
11275 array,
11276 &CastOptions {
11277 safe: false,
11278 format_options: FormatOptions::default(),
11279 },
11280 );
11281 assert!(casted_array.is_err());
11282
11283 let array = vec![1234567];
11285 let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
11286 array,
11287 &CastOptions::default(),
11288 )
11289 .unwrap();
11290 assert_eq!(
11291 casted_array.data_type(),
11292 &DataType::Interval(IntervalUnit::MonthDayNano)
11293 );
11294 assert_eq!(
11295 casted_array.value(0),
11296 IntervalMonthDayNano::new(0, 0, 1234567000000)
11297 );
11298
11299 let array = vec![i64::MAX];
11300 let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
11301 array.clone(),
11302 &CastOptions::default(),
11303 )
11304 .unwrap();
11305 assert!(!casted_array.is_valid(0));
11306
11307 let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
11308 array,
11309 &CastOptions {
11310 safe: false,
11311 format_options: FormatOptions::default(),
11312 },
11313 );
11314 assert!(casted_array.is_err());
11315
11316 let array = vec![1234567];
11318 let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
11319 array,
11320 &CastOptions::default(),
11321 )
11322 .unwrap();
11323 assert_eq!(
11324 casted_array.data_type(),
11325 &DataType::Interval(IntervalUnit::MonthDayNano)
11326 );
11327 assert_eq!(
11328 casted_array.value(0),
11329 IntervalMonthDayNano::new(0, 0, 1234567000)
11330 );
11331
11332 let array = vec![i64::MAX];
11333 let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
11334 array.clone(),
11335 &CastOptions::default(),
11336 )
11337 .unwrap();
11338 assert!(!casted_array.is_valid(0));
11339
11340 let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
11341 array,
11342 &CastOptions {
11343 safe: false,
11344 format_options: FormatOptions::default(),
11345 },
11346 );
11347 assert!(casted_array.is_err());
11348
11349 let array = vec![1234567];
11351 let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
11352 array,
11353 &CastOptions::default(),
11354 )
11355 .unwrap();
11356 assert_eq!(
11357 casted_array.data_type(),
11358 &DataType::Interval(IntervalUnit::MonthDayNano)
11359 );
11360 assert_eq!(
11361 casted_array.value(0),
11362 IntervalMonthDayNano::new(0, 0, 1234567)
11363 );
11364
11365 let array = vec![i64::MAX];
11366 let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
11367 array,
11368 &CastOptions {
11369 safe: false,
11370 format_options: FormatOptions::default(),
11371 },
11372 )
11373 .unwrap();
11374 assert_eq!(
11375 casted_array.value(0),
11376 IntervalMonthDayNano::new(0, 0, i64::MAX)
11377 );
11378 }
11379
11380 fn cast_from_interval_to_duration<T: ArrowTemporalType>(
11382 array: &IntervalMonthDayNanoArray,
11383 cast_options: &CastOptions,
11384 ) -> Result<PrimitiveArray<T>, ArrowError> {
11385 let casted_array = cast_with_options(&array, &T::DATA_TYPE, cast_options)?;
11386 casted_array
11387 .as_any()
11388 .downcast_ref::<PrimitiveArray<T>>()
11389 .ok_or_else(|| {
11390 ArrowError::ComputeError(format!("Failed to downcast to {}", T::DATA_TYPE))
11391 })
11392 .cloned()
11393 }
11394
11395 #[test]
11396 fn test_cast_from_interval_to_duration() {
11397 let nullable = CastOptions::default();
11398 let fallible = CastOptions {
11399 safe: false,
11400 format_options: FormatOptions::default(),
11401 };
11402 let v = IntervalMonthDayNano::new(0, 0, 1234567);
11403
11404 let array = vec![v].into();
11406 let casted_array: DurationSecondArray =
11407 cast_from_interval_to_duration(&array, &nullable).unwrap();
11408 assert_eq!(casted_array.value(0), 0);
11409
11410 let array = vec![IntervalMonthDayNano::MAX].into();
11411 let casted_array: DurationSecondArray =
11412 cast_from_interval_to_duration(&array, &nullable).unwrap();
11413 assert!(!casted_array.is_valid(0));
11414
11415 let res = cast_from_interval_to_duration::<DurationSecondType>(&array, &fallible);
11416 assert!(res.is_err());
11417
11418 let array = vec![v].into();
11420 let casted_array: DurationMillisecondArray =
11421 cast_from_interval_to_duration(&array, &nullable).unwrap();
11422 assert_eq!(casted_array.value(0), 1);
11423
11424 let array = vec![IntervalMonthDayNano::MAX].into();
11425 let casted_array: DurationMillisecondArray =
11426 cast_from_interval_to_duration(&array, &nullable).unwrap();
11427 assert!(!casted_array.is_valid(0));
11428
11429 let res = cast_from_interval_to_duration::<DurationMillisecondType>(&array, &fallible);
11430 assert!(res.is_err());
11431
11432 let array = vec![v].into();
11434 let casted_array: DurationMicrosecondArray =
11435 cast_from_interval_to_duration(&array, &nullable).unwrap();
11436 assert_eq!(casted_array.value(0), 1234);
11437
11438 let array = vec![IntervalMonthDayNano::MAX].into();
11439 let casted_array =
11440 cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &nullable).unwrap();
11441 assert!(!casted_array.is_valid(0));
11442
11443 let casted_array =
11444 cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &fallible);
11445 assert!(casted_array.is_err());
11446
11447 let array = vec![v].into();
11449 let casted_array: DurationNanosecondArray =
11450 cast_from_interval_to_duration(&array, &nullable).unwrap();
11451 assert_eq!(casted_array.value(0), 1234567);
11452
11453 let array = vec![IntervalMonthDayNano::MAX].into();
11454 let casted_array: DurationNanosecondArray =
11455 cast_from_interval_to_duration(&array, &nullable).unwrap();
11456 assert!(!casted_array.is_valid(0));
11457
11458 let casted_array =
11459 cast_from_interval_to_duration::<DurationNanosecondType>(&array, &fallible);
11460 assert!(casted_array.is_err());
11461
11462 let array = vec![
11463 IntervalMonthDayNanoType::make_value(0, 1, 0),
11464 IntervalMonthDayNanoType::make_value(-1, 0, 0),
11465 IntervalMonthDayNanoType::make_value(1, 1, 0),
11466 IntervalMonthDayNanoType::make_value(1, 0, 1),
11467 IntervalMonthDayNanoType::make_value(0, 0, -1),
11468 ]
11469 .into();
11470 let casted_array =
11471 cast_from_interval_to_duration::<DurationNanosecondType>(&array, &nullable).unwrap();
11472 assert!(!casted_array.is_valid(0));
11473 assert!(!casted_array.is_valid(1));
11474 assert!(!casted_array.is_valid(2));
11475 assert!(!casted_array.is_valid(3));
11476 assert!(casted_array.is_valid(4));
11477 assert_eq!(casted_array.value(4), -1);
11478 }
11479
11480 fn cast_from_interval_year_month_to_interval_month_day_nano(
11482 array: Vec<i32>,
11483 cast_options: &CastOptions,
11484 ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
11485 let array = PrimitiveArray::<IntervalYearMonthType>::from(array);
11486 let array = Arc::new(array) as ArrayRef;
11487 let casted_array = cast_with_options(
11488 &array,
11489 &DataType::Interval(IntervalUnit::MonthDayNano),
11490 cast_options,
11491 )?;
11492 casted_array
11493 .as_any()
11494 .downcast_ref::<IntervalMonthDayNanoArray>()
11495 .ok_or_else(|| {
11496 ArrowError::ComputeError(
11497 "Failed to downcast to IntervalMonthDayNanoArray".to_string(),
11498 )
11499 })
11500 .cloned()
11501 }
11502
11503 #[test]
11504 fn test_cast_from_interval_year_month_to_interval_month_day_nano() {
11505 let array = vec![1234567];
11507 let casted_array = cast_from_interval_year_month_to_interval_month_day_nano(
11508 array,
11509 &CastOptions::default(),
11510 )
11511 .unwrap();
11512 assert_eq!(
11513 casted_array.data_type(),
11514 &DataType::Interval(IntervalUnit::MonthDayNano)
11515 );
11516 assert_eq!(
11517 casted_array.value(0),
11518 IntervalMonthDayNano::new(1234567, 0, 0)
11519 );
11520 }
11521
11522 fn cast_from_interval_day_time_to_interval_month_day_nano(
11524 array: Vec<IntervalDayTime>,
11525 cast_options: &CastOptions,
11526 ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
11527 let array = PrimitiveArray::<IntervalDayTimeType>::from(array);
11528 let array = Arc::new(array) as ArrayRef;
11529 let casted_array = cast_with_options(
11530 &array,
11531 &DataType::Interval(IntervalUnit::MonthDayNano),
11532 cast_options,
11533 )?;
11534 Ok(casted_array
11535 .as_primitive::<IntervalMonthDayNanoType>()
11536 .clone())
11537 }
11538
11539 #[test]
11540 fn test_cast_from_interval_day_time_to_interval_month_day_nano() {
11541 let array = vec![IntervalDayTime::new(123, 0)];
11543 let casted_array =
11544 cast_from_interval_day_time_to_interval_month_day_nano(array, &CastOptions::default())
11545 .unwrap();
11546 assert_eq!(
11547 casted_array.data_type(),
11548 &DataType::Interval(IntervalUnit::MonthDayNano)
11549 );
11550 assert_eq!(casted_array.value(0), IntervalMonthDayNano::new(0, 123, 0));
11551 }
11552
11553 #[test]
11554 fn test_cast_below_unixtimestamp() {
11555 let valid = StringArray::from(vec![
11556 "1900-01-03 23:59:59",
11557 "1969-12-31 00:00:01",
11558 "1989-12-31 00:00:01",
11559 ]);
11560
11561 let array = Arc::new(valid) as ArrayRef;
11562 let casted_array = cast_with_options(
11563 &array,
11564 &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
11565 &CastOptions {
11566 safe: false,
11567 format_options: FormatOptions::default(),
11568 },
11569 )
11570 .unwrap();
11571
11572 let ts_array = casted_array
11573 .as_primitive::<TimestampNanosecondType>()
11574 .values()
11575 .iter()
11576 .map(|ts| ts / 1_000_000)
11577 .collect::<Vec<_>>();
11578
11579 let array = TimestampMillisecondArray::from(ts_array).with_timezone("+00:00".to_string());
11580 let casted_array = cast(&array, &DataType::Date32).unwrap();
11581 let date_array = casted_array.as_primitive::<Date32Type>();
11582 let casted_array = cast(&date_array, &DataType::Utf8).unwrap();
11583 let string_array = casted_array.as_string::<i32>();
11584 assert_eq!("1900-01-03", string_array.value(0));
11585 assert_eq!("1969-12-31", string_array.value(1));
11586 assert_eq!("1989-12-31", string_array.value(2));
11587 }
11588
11589 #[test]
11590 fn test_nested_list() {
11591 let mut list = ListBuilder::new(Int32Builder::new());
11592 list.append_value([Some(1), Some(2), Some(3)]);
11593 list.append_value([Some(4), None, Some(6)]);
11594 let list = list.finish();
11595
11596 let to_field = Field::new("nested", list.data_type().clone(), false);
11597 let to = DataType::List(Arc::new(to_field));
11598 let out = cast(&list, &to).unwrap();
11599 let opts = FormatOptions::default().with_null("null");
11600 let formatted = ArrayFormatter::try_new(out.as_ref(), &opts).unwrap();
11601
11602 assert_eq!(formatted.value(0).to_string(), "[[1], [2], [3]]");
11603 assert_eq!(formatted.value(1).to_string(), "[[4], [null], [6]]");
11604 }
11605
11606 #[test]
11607 fn test_nested_list_cast() {
11608 let mut builder = ListBuilder::new(ListBuilder::new(Int32Builder::new()));
11609 builder.append_value([Some([Some(1), Some(2), None]), None]);
11610 builder.append_value([None, Some([]), None]);
11611 builder.append_null();
11612 builder.append_value([Some([Some(2), Some(3)])]);
11613 let start = builder.finish();
11614
11615 let mut builder = LargeListBuilder::new(LargeListBuilder::new(Int8Builder::new()));
11616 builder.append_value([Some([Some(1), Some(2), None]), None]);
11617 builder.append_value([None, Some([]), None]);
11618 builder.append_null();
11619 builder.append_value([Some([Some(2), Some(3)])]);
11620 let expected = builder.finish();
11621
11622 let actual = cast(&start, expected.data_type()).unwrap();
11623 assert_eq!(actual.as_ref(), &expected);
11624 }
11625
11626 const CAST_OPTIONS: CastOptions<'static> = CastOptions {
11627 safe: true,
11628 format_options: FormatOptions::new(),
11629 };
11630
11631 #[test]
11632 #[allow(clippy::assertions_on_constants)]
11633 fn test_const_options() {
11634 assert!(CAST_OPTIONS.safe)
11635 }
11636
11637 #[test]
11638 fn test_list_format_options() {
11639 let options = CastOptions {
11640 safe: false,
11641 format_options: FormatOptions::default().with_null("null"),
11642 };
11643 let array = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
11644 Some(vec![Some(0), Some(1), Some(2)]),
11645 Some(vec![Some(0), None, Some(2)]),
11646 ]);
11647 let a = cast_with_options(&array, &DataType::Utf8, &options).unwrap();
11648 let r: Vec<_> = a.as_string::<i32>().iter().flatten().collect();
11649 assert_eq!(r, &["[0, 1, 2]", "[0, null, 2]"]);
11650 }
11651 #[test]
11652 fn test_cast_string_to_timestamp_invalid_tz() {
11653 let bad_timestamp = "2023-12-05T21:58:10.45ZZTOP";
11655 let array = StringArray::from(vec![Some(bad_timestamp)]);
11656
11657 let data_types = [
11658 DataType::Timestamp(TimeUnit::Second, None),
11659 DataType::Timestamp(TimeUnit::Millisecond, None),
11660 DataType::Timestamp(TimeUnit::Microsecond, None),
11661 DataType::Timestamp(TimeUnit::Nanosecond, None),
11662 ];
11663
11664 let cast_options = CastOptions {
11665 safe: false,
11666 ..Default::default()
11667 };
11668
11669 for dt in data_types {
11670 assert_eq!(
11671 cast_with_options(&array, &dt, &cast_options)
11672 .unwrap_err()
11673 .to_string(),
11674 "Parser error: Invalid timezone \"ZZTOP\": only offset based timezones supported without chrono-tz feature"
11675 );
11676 }
11677 }
11678 #[test]
11679 fn test_cast_struct_to_struct() {
11680 let struct_type = DataType::Struct(
11681 vec![
11682 Field::new("a", DataType::Boolean, false),
11683 Field::new("b", DataType::Int32, false),
11684 ]
11685 .into(),
11686 );
11687 let to_type = DataType::Struct(
11688 vec![
11689 Field::new("a", DataType::Utf8, false),
11690 Field::new("b", DataType::Utf8, false),
11691 ]
11692 .into(),
11693 );
11694 let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
11695 let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
11696 let struct_array = StructArray::from(vec![
11697 (
11698 Arc::new(Field::new("b", DataType::Boolean, false)),
11699 boolean.clone() as ArrayRef,
11700 ),
11701 (
11702 Arc::new(Field::new("c", DataType::Int32, false)),
11703 int.clone() as ArrayRef,
11704 ),
11705 ]);
11706 let casted_array = cast(&struct_array, &to_type).unwrap();
11707 let casted_array = casted_array.as_struct();
11708 assert_eq!(casted_array.data_type(), &to_type);
11709 let casted_boolean_array = casted_array
11710 .column(0)
11711 .as_string::<i32>()
11712 .into_iter()
11713 .flatten()
11714 .collect::<Vec<_>>();
11715 let casted_int_array = casted_array
11716 .column(1)
11717 .as_string::<i32>()
11718 .into_iter()
11719 .flatten()
11720 .collect::<Vec<_>>();
11721 assert_eq!(casted_boolean_array, vec!["false", "false", "true", "true"]);
11722 assert_eq!(casted_int_array, vec!["42", "28", "19", "31"]);
11723
11724 let to_type = DataType::Struct(
11726 vec![
11727 Field::new("a", DataType::Date32, false),
11728 Field::new("b", DataType::Utf8, false),
11729 ]
11730 .into(),
11731 );
11732 assert!(!can_cast_types(&struct_type, &to_type));
11733 let result = cast(&struct_array, &to_type);
11734 assert_eq!(
11735 "Cast error: Casting from Boolean to Date32 not supported",
11736 result.unwrap_err().to_string()
11737 );
11738 }
11739
11740 #[test]
11741 fn test_cast_struct_to_struct_nullability() {
11742 let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
11743 let int = Arc::new(Int32Array::from(vec![Some(42), None, Some(19), None]));
11744 let struct_array = StructArray::from(vec![
11745 (
11746 Arc::new(Field::new("b", DataType::Boolean, false)),
11747 boolean.clone() as ArrayRef,
11748 ),
11749 (
11750 Arc::new(Field::new("c", DataType::Int32, true)),
11751 int.clone() as ArrayRef,
11752 ),
11753 ]);
11754
11755 let to_type = DataType::Struct(
11757 vec![
11758 Field::new("a", DataType::Utf8, false),
11759 Field::new("b", DataType::Utf8, true),
11760 ]
11761 .into(),
11762 );
11763 cast(&struct_array, &to_type).expect("Cast nullable to nullable struct field should work");
11764
11765 let to_type = DataType::Struct(
11767 vec![
11768 Field::new("a", DataType::Utf8, false),
11769 Field::new("b", DataType::Utf8, false),
11770 ]
11771 .into(),
11772 );
11773 cast(&struct_array, &to_type)
11774 .expect_err("Cast nullable to non-nullable struct field should fail");
11775
11776 let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
11777 let int = Arc::new(Int32Array::from(vec![i32::MAX, 25, 1, 100]));
11778 let struct_array = StructArray::from(vec![
11779 (
11780 Arc::new(Field::new("b", DataType::Boolean, false)),
11781 boolean.clone() as ArrayRef,
11782 ),
11783 (
11784 Arc::new(Field::new("c", DataType::Int32, false)),
11785 int.clone() as ArrayRef,
11786 ),
11787 ]);
11788
11789 let to_type = DataType::Struct(
11791 vec![
11792 Field::new("a", DataType::Utf8, false),
11793 Field::new("b", DataType::Utf8, false),
11794 ]
11795 .into(),
11796 );
11797 cast(&struct_array, &to_type)
11798 .expect("Cast non-nullable to non-nullable struct field should work");
11799
11800 let to_type = DataType::Struct(
11802 vec![
11803 Field::new("a", DataType::Utf8, false),
11804 Field::new("b", DataType::Int8, false),
11805 ]
11806 .into(),
11807 );
11808 cast(&struct_array, &to_type).expect_err(
11809 "Cast non-nullable to non-nullable struct field returning null should fail",
11810 );
11811 }
11812
11813 #[test]
11814 fn test_cast_struct_to_non_struct() {
11815 let boolean = Arc::new(BooleanArray::from(vec![true, false]));
11816 let struct_array = StructArray::from(vec![(
11817 Arc::new(Field::new("a", DataType::Boolean, false)),
11818 boolean.clone() as ArrayRef,
11819 )]);
11820 let to_type = DataType::Utf8;
11821 let result = cast(&struct_array, &to_type);
11822 assert_eq!(
11823 r#"Cast error: Casting from Struct("a": non-null Boolean) to Utf8 not supported"#,
11824 result.unwrap_err().to_string()
11825 );
11826 }
11827
11828 #[test]
11829 fn test_cast_non_struct_to_struct() {
11830 let array = StringArray::from(vec!["a", "b"]);
11831 let to_type = DataType::Struct(vec![Field::new("a", DataType::Boolean, false)].into());
11832 let result = cast(&array, &to_type);
11833 assert_eq!(
11834 r#"Cast error: Casting from Utf8 to Struct("a": non-null Boolean) not supported"#,
11835 result.unwrap_err().to_string()
11836 );
11837 }
11838
11839 #[test]
11840 fn test_cast_struct_with_different_field_order() {
11841 let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
11843 let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
11844 let string = Arc::new(StringArray::from(vec!["foo", "bar", "baz", "qux"]));
11845
11846 let struct_array = StructArray::from(vec![
11847 (
11848 Arc::new(Field::new("a", DataType::Boolean, false)),
11849 boolean.clone() as ArrayRef,
11850 ),
11851 (
11852 Arc::new(Field::new("b", DataType::Int32, false)),
11853 int.clone() as ArrayRef,
11854 ),
11855 (
11856 Arc::new(Field::new("c", DataType::Utf8, false)),
11857 string.clone() as ArrayRef,
11858 ),
11859 ]);
11860
11861 let to_type = DataType::Struct(
11863 vec![
11864 Field::new("c", DataType::Utf8, false),
11865 Field::new("a", DataType::Utf8, false), Field::new("b", DataType::Utf8, false), ]
11868 .into(),
11869 );
11870
11871 let result = cast(&struct_array, &to_type).unwrap();
11872 let result_struct = result.as_struct();
11873
11874 assert_eq!(result_struct.data_type(), &to_type);
11875 assert_eq!(result_struct.num_columns(), 3);
11876
11877 let c_column = result_struct.column(0).as_string::<i32>();
11879 assert_eq!(
11880 c_column.into_iter().flatten().collect::<Vec<_>>(),
11881 vec!["foo", "bar", "baz", "qux"]
11882 );
11883
11884 let a_column = result_struct.column(1).as_string::<i32>();
11886 assert_eq!(
11887 a_column.into_iter().flatten().collect::<Vec<_>>(),
11888 vec!["false", "false", "true", "true"]
11889 );
11890
11891 let b_column = result_struct.column(2).as_string::<i32>();
11893 assert_eq!(
11894 b_column.into_iter().flatten().collect::<Vec<_>>(),
11895 vec!["42", "28", "19", "31"]
11896 );
11897 }
11898
11899 #[test]
11900 fn test_cast_struct_with_missing_field() {
11901 let boolean = Arc::new(BooleanArray::from(vec![false, true]));
11903 let struct_array = StructArray::from(vec![(
11904 Arc::new(Field::new("a", DataType::Boolean, false)),
11905 boolean.clone() as ArrayRef,
11906 )]);
11907
11908 let to_type = DataType::Struct(
11909 vec![
11910 Field::new("a", DataType::Utf8, false),
11911 Field::new("b", DataType::Int32, false), ]
11913 .into(),
11914 );
11915
11916 let result = cast(&struct_array, &to_type);
11917 assert!(result.is_err());
11918 assert_eq!(
11919 result.unwrap_err().to_string(),
11920 "Invalid argument error: Incorrect number of arrays for StructArray fields, expected 2 got 1"
11921 );
11922 }
11923
11924 #[test]
11925 fn test_cast_struct_with_subset_of_fields() {
11926 let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
11928 let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
11929 let string = Arc::new(StringArray::from(vec!["foo", "bar", "baz", "qux"]));
11930
11931 let struct_array = StructArray::from(vec![
11932 (
11933 Arc::new(Field::new("a", DataType::Boolean, false)),
11934 boolean.clone() as ArrayRef,
11935 ),
11936 (
11937 Arc::new(Field::new("b", DataType::Int32, false)),
11938 int.clone() as ArrayRef,
11939 ),
11940 (
11941 Arc::new(Field::new("c", DataType::Utf8, false)),
11942 string.clone() as ArrayRef,
11943 ),
11944 ]);
11945
11946 let to_type = DataType::Struct(
11948 vec![
11949 Field::new("c", DataType::Utf8, false),
11950 Field::new("a", DataType::Utf8, false),
11951 ]
11952 .into(),
11953 );
11954
11955 let result = cast(&struct_array, &to_type).unwrap();
11956 let result_struct = result.as_struct();
11957
11958 assert_eq!(result_struct.data_type(), &to_type);
11959 assert_eq!(result_struct.num_columns(), 2);
11960
11961 let c_column = result_struct.column(0).as_string::<i32>();
11963 assert_eq!(
11964 c_column.into_iter().flatten().collect::<Vec<_>>(),
11965 vec!["foo", "bar", "baz", "qux"]
11966 );
11967
11968 let a_column = result_struct.column(1).as_string::<i32>();
11970 assert_eq!(
11971 a_column.into_iter().flatten().collect::<Vec<_>>(),
11972 vec!["false", "false", "true", "true"]
11973 );
11974 }
11975
11976 #[test]
11977 fn test_can_cast_struct_rename_field() {
11978 let from_type = DataType::Struct(
11980 vec![
11981 Field::new("a", DataType::Int32, false),
11982 Field::new("b", DataType::Utf8, false),
11983 ]
11984 .into(),
11985 );
11986
11987 let to_type = DataType::Struct(
11988 vec![
11989 Field::new("a", DataType::Int64, false),
11990 Field::new("c", DataType::Boolean, false), ]
11992 .into(),
11993 );
11994
11995 assert!(can_cast_types(&from_type, &to_type));
11996 }
11997
11998 fn run_decimal_cast_test_case_between_multiple_types(t: DecimalCastTestConfig) {
11999 run_decimal_cast_test_case::<Decimal128Type, Decimal128Type>(t.clone());
12000 run_decimal_cast_test_case::<Decimal128Type, Decimal256Type>(t.clone());
12001 run_decimal_cast_test_case::<Decimal256Type, Decimal128Type>(t.clone());
12002 run_decimal_cast_test_case::<Decimal256Type, Decimal256Type>(t.clone());
12003 }
12004
12005 #[test]
12006 fn test_decimal_to_decimal_coverage() {
12007 let test_cases = [
12008 DecimalCastTestConfig {
12010 input_prec: 5,
12011 input_scale: 1,
12012 input_repr: 99999, output_prec: 10,
12014 output_scale: 6,
12015 expected_output_repr: Ok(9999900000), },
12017 DecimalCastTestConfig {
12019 input_prec: 5,
12020 input_scale: 1,
12021 input_repr: 99, output_prec: 7,
12023 output_scale: 6,
12024 expected_output_repr: Ok(9900000), },
12026 DecimalCastTestConfig {
12028 input_prec: 5,
12029 input_scale: 1,
12030 input_repr: 99999, output_prec: 7,
12032 output_scale: 6,
12033 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()) },
12035 DecimalCastTestConfig {
12037 input_prec: 5,
12038 input_scale: 3,
12039 input_repr: 99999, output_prec: 10,
12041 output_scale: 2,
12042 expected_output_repr: Ok(10000), },
12044 DecimalCastTestConfig {
12046 input_prec: 5,
12047 input_scale: 3,
12048 input_repr: 99994, output_prec: 10,
12050 output_scale: 2,
12051 expected_output_repr: Ok(9999), },
12053 DecimalCastTestConfig {
12055 input_prec: 5,
12056 input_scale: 3,
12057 input_repr: 99999, output_prec: 10,
12059 output_scale: 3,
12060 expected_output_repr: Ok(99999), },
12062 DecimalCastTestConfig {
12064 input_prec: 10,
12065 input_scale: 5,
12066 input_repr: 999999, output_prec: 8,
12068 output_scale: 7,
12069 expected_output_repr: Ok(99999900), },
12071 DecimalCastTestConfig {
12073 input_prec: 10,
12074 input_scale: 5,
12075 input_repr: 9999999, output_prec: 8,
12077 output_scale: 7,
12078 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()) },
12080 DecimalCastTestConfig {
12082 input_prec: 7,
12083 input_scale: 4,
12084 input_repr: 9999999, output_prec: 6,
12086 output_scale: 2,
12087 expected_output_repr: Ok(100000),
12088 },
12089 DecimalCastTestConfig {
12091 input_prec: 10,
12092 input_scale: 5,
12093 input_repr: 12345678, output_prec: 8,
12095 output_scale: 3,
12096 expected_output_repr: Ok(123457), },
12098 DecimalCastTestConfig {
12100 input_prec: 10,
12101 input_scale: 5,
12102 input_repr: 9999999, output_prec: 4,
12104 output_scale: 3,
12105 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()) },
12107 DecimalCastTestConfig {
12109 input_prec: 10,
12110 input_scale: 5,
12111 input_repr: 999999, output_prec: 6,
12113 output_scale: 5,
12114 expected_output_repr: Ok(999999), },
12116 DecimalCastTestConfig {
12118 input_prec: 10,
12119 input_scale: 5,
12120 input_repr: 9999999, output_prec: 6,
12122 output_scale: 5,
12123 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()) },
12125 DecimalCastTestConfig {
12127 input_prec: 7,
12128 input_scale: 4,
12129 input_repr: 12345, output_prec: 7,
12131 output_scale: 6,
12132 expected_output_repr: Ok(1234500), },
12134 DecimalCastTestConfig {
12136 input_prec: 7,
12137 input_scale: 4,
12138 input_repr: 123456, output_prec: 7,
12140 output_scale: 6,
12141 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()) },
12143 DecimalCastTestConfig {
12145 input_prec: 7,
12146 input_scale: 5,
12147 input_repr: 1234567, output_prec: 7,
12149 output_scale: 4,
12150 expected_output_repr: Ok(123457), },
12152 DecimalCastTestConfig {
12154 input_prec: 7,
12155 input_scale: 5,
12156 input_repr: 9999999, output_prec: 7,
12158 output_scale: 5,
12159 expected_output_repr: Ok(9999999), },
12161 DecimalCastTestConfig {
12163 input_prec: 7,
12164 input_scale: 0,
12165 input_repr: 1234567, output_prec: 8,
12167 output_scale: 0,
12168 expected_output_repr: Ok(1234567), },
12170 DecimalCastTestConfig {
12172 input_prec: 7,
12173 input_scale: 0,
12174 input_repr: 1234567, output_prec: 6,
12176 output_scale: 0,
12177 expected_output_repr: Err("Invalid argument error: 1234567 is too large to store in a {} of precision 6. Max is 999999".to_string())
12178 },
12179 DecimalCastTestConfig {
12181 input_prec: 7,
12182 input_scale: 0,
12183 input_repr: 123456, output_prec: 6,
12185 output_scale: 0,
12186 expected_output_repr: Ok(123456), },
12188 ];
12189
12190 for t in test_cases {
12191 run_decimal_cast_test_case_between_multiple_types(t);
12192 }
12193 }
12194
12195 #[test]
12196 fn test_decimal_to_decimal_increase_scale_and_precision_unchecked() {
12197 let test_cases = [
12198 DecimalCastTestConfig {
12199 input_prec: 5,
12200 input_scale: 0,
12201 input_repr: 99999,
12202 output_prec: 10,
12203 output_scale: 5,
12204 expected_output_repr: Ok(9999900000),
12205 },
12206 DecimalCastTestConfig {
12207 input_prec: 5,
12208 input_scale: 0,
12209 input_repr: -99999,
12210 output_prec: 10,
12211 output_scale: 5,
12212 expected_output_repr: Ok(-9999900000),
12213 },
12214 DecimalCastTestConfig {
12215 input_prec: 5,
12216 input_scale: 2,
12217 input_repr: 99999,
12218 output_prec: 10,
12219 output_scale: 5,
12220 expected_output_repr: Ok(99999000),
12221 },
12222 DecimalCastTestConfig {
12223 input_prec: 5,
12224 input_scale: -2,
12225 input_repr: -99999,
12226 output_prec: 10,
12227 output_scale: 3,
12228 expected_output_repr: Ok(-9999900000),
12229 },
12230 DecimalCastTestConfig {
12231 input_prec: 5,
12232 input_scale: 3,
12233 input_repr: -12345,
12234 output_prec: 6,
12235 output_scale: 5,
12236 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())
12237 },
12238 ];
12239
12240 for t in test_cases {
12241 run_decimal_cast_test_case_between_multiple_types(t);
12242 }
12243 }
12244
12245 #[test]
12246 fn test_decimal_to_decimal_decrease_scale_and_precision_unchecked() {
12247 let test_cases = [
12248 DecimalCastTestConfig {
12249 input_prec: 5,
12250 input_scale: 0,
12251 input_repr: 99999,
12252 output_scale: -3,
12253 output_prec: 3,
12254 expected_output_repr: Ok(100),
12255 },
12256 DecimalCastTestConfig {
12257 input_prec: 5,
12258 input_scale: 0,
12259 input_repr: -99999,
12260 output_prec: 1,
12261 output_scale: -5,
12262 expected_output_repr: Ok(-1),
12263 },
12264 DecimalCastTestConfig {
12265 input_prec: 10,
12266 input_scale: 2,
12267 input_repr: 123456789,
12268 output_prec: 5,
12269 output_scale: -2,
12270 expected_output_repr: Ok(12346),
12271 },
12272 DecimalCastTestConfig {
12273 input_prec: 10,
12274 input_scale: 4,
12275 input_repr: -9876543210,
12276 output_prec: 7,
12277 output_scale: 0,
12278 expected_output_repr: Ok(-987654),
12279 },
12280 DecimalCastTestConfig {
12281 input_prec: 7,
12282 input_scale: 4,
12283 input_repr: 9999999,
12284 output_prec: 6,
12285 output_scale: 3,
12286 expected_output_repr:
12287 Err("Invalid argument error: 1000.000 is too large to store in a {} of precision 6. Max is 999.999".to_string()),
12288 },
12289 ];
12290 for t in test_cases {
12291 run_decimal_cast_test_case_between_multiple_types(t);
12292 }
12293 }
12294
12295 #[test]
12296 fn test_decimal_to_decimal_throw_error_on_precision_overflow_same_scale() {
12297 let array = vec![Some(123456789)];
12298 let array = create_decimal128_array(array, 24, 2).unwrap();
12299 let input_type = DataType::Decimal128(24, 2);
12300 let output_type = DataType::Decimal128(6, 2);
12301 assert!(can_cast_types(&input_type, &output_type));
12302
12303 let options = CastOptions {
12304 safe: false,
12305 ..Default::default()
12306 };
12307 let result = cast_with_options(&array, &output_type, &options);
12308 assert_eq!(
12309 result.unwrap_err().to_string(),
12310 "Invalid argument error: 1234567.89 is too large to store in a Decimal128 of precision 6. Max is 9999.99"
12311 );
12312 }
12313
12314 #[test]
12315 fn test_decimal_to_decimal_same_scale() {
12316 let array = vec![Some(520)];
12317 let array = create_decimal128_array(array, 4, 2).unwrap();
12318 let input_type = DataType::Decimal128(4, 2);
12319 let output_type = DataType::Decimal128(3, 2);
12320 assert!(can_cast_types(&input_type, &output_type));
12321
12322 let options = CastOptions {
12323 safe: false,
12324 ..Default::default()
12325 };
12326 let result = cast_with_options(&array, &output_type, &options);
12327 assert_eq!(
12328 result.unwrap().as_primitive::<Decimal128Type>().value(0),
12329 520
12330 );
12331
12332 assert_eq!(
12334 &cast(
12335 &create_decimal128_array(vec![Some(0)], 3, 0).unwrap(),
12336 &DataType::Decimal128(2, 0)
12337 )
12338 .unwrap(),
12339 &(Arc::new(create_decimal128_array(vec![Some(0)], 2, 0).unwrap()) as ArrayRef)
12340 );
12341 }
12342
12343 #[test]
12344 fn test_decimal_to_decimal_throw_error_on_precision_overflow_lower_scale() {
12345 let array = vec![Some(123456789)];
12346 let array = create_decimal128_array(array, 24, 4).unwrap();
12347 let input_type = DataType::Decimal128(24, 4);
12348 let output_type = DataType::Decimal128(6, 2);
12349 assert!(can_cast_types(&input_type, &output_type));
12350
12351 let options = CastOptions {
12352 safe: false,
12353 ..Default::default()
12354 };
12355 let result = cast_with_options(&array, &output_type, &options);
12356 assert_eq!(
12357 result.unwrap_err().to_string(),
12358 "Invalid argument error: 12345.68 is too large to store in a Decimal128 of precision 6. Max is 9999.99"
12359 );
12360 }
12361
12362 #[test]
12363 fn test_decimal_to_decimal_throw_error_on_precision_overflow_greater_scale() {
12364 let array = vec![Some(123456789)];
12365 let array = create_decimal128_array(array, 24, 2).unwrap();
12366 let input_type = DataType::Decimal128(24, 2);
12367 let output_type = DataType::Decimal128(6, 3);
12368 assert!(can_cast_types(&input_type, &output_type));
12369
12370 let options = CastOptions {
12371 safe: false,
12372 ..Default::default()
12373 };
12374 let result = cast_with_options(&array, &output_type, &options);
12375 assert_eq!(
12376 result.unwrap_err().to_string(),
12377 "Invalid argument error: 1234567.890 is too large to store in a Decimal128 of precision 6. Max is 999.999"
12378 );
12379 }
12380
12381 #[test]
12382 fn test_decimal_to_decimal_throw_error_on_precision_overflow_diff_type() {
12383 let array = vec![Some(123456789)];
12384 let array = create_decimal128_array(array, 24, 2).unwrap();
12385 let input_type = DataType::Decimal128(24, 2);
12386 let output_type = DataType::Decimal256(6, 2);
12387 assert!(can_cast_types(&input_type, &output_type));
12388
12389 let options = CastOptions {
12390 safe: false,
12391 ..Default::default()
12392 };
12393 let result = cast_with_options(&array, &output_type, &options).unwrap_err();
12394 assert_eq!(
12395 result.to_string(),
12396 "Invalid argument error: 1234567.89 is too large to store in a Decimal256 of precision 6. Max is 9999.99"
12397 );
12398 }
12399
12400 #[test]
12401 fn test_first_none() {
12402 let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
12403 None,
12404 Some(vec![Some(1), Some(2)]),
12405 ])) as ArrayRef;
12406 let data_type =
12407 DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
12408 let opt = CastOptions::default();
12409 let r = cast_with_options(&array, &data_type, &opt).unwrap();
12410
12411 let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
12412 vec![None, Some(vec![Some(1), Some(2)])],
12413 2,
12414 )) as ArrayRef;
12415 assert_eq!(*fixed_array, *r);
12416 }
12417
12418 #[test]
12419 fn test_first_last_none() {
12420 let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
12421 None,
12422 Some(vec![Some(1), Some(2)]),
12423 None,
12424 ])) as ArrayRef;
12425 let data_type =
12426 DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
12427 let opt = CastOptions::default();
12428 let r = cast_with_options(&array, &data_type, &opt).unwrap();
12429
12430 let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
12431 vec![None, Some(vec![Some(1), Some(2)]), None],
12432 2,
12433 )) as ArrayRef;
12434 assert_eq!(*fixed_array, *r);
12435 }
12436
12437 #[test]
12438 fn test_cast_decimal_error_output() {
12439 let array = Int64Array::from(vec![1]);
12440 let error = cast_with_options(
12441 &array,
12442 &DataType::Decimal32(1, 1),
12443 &CastOptions {
12444 safe: false,
12445 format_options: FormatOptions::default(),
12446 },
12447 )
12448 .unwrap_err();
12449 assert_eq!(
12450 error.to_string(),
12451 "Invalid argument error: 1.0 is too large to store in a Decimal32 of precision 1. Max is 0.9"
12452 );
12453
12454 let array = Int64Array::from(vec![-1]);
12455 let error = cast_with_options(
12456 &array,
12457 &DataType::Decimal32(1, 1),
12458 &CastOptions {
12459 safe: false,
12460 format_options: FormatOptions::default(),
12461 },
12462 )
12463 .unwrap_err();
12464 assert_eq!(
12465 error.to_string(),
12466 "Invalid argument error: -1.0 is too small to store in a Decimal32 of precision 1. Min is -0.9"
12467 );
12468 }
12469
12470 #[test]
12471 fn test_run_end_encoded_to_primitive() {
12472 let run_ends = Int32Array::from(vec![2, 5, 6]);
12474 let values = Int32Array::from(vec![1, 2, 3]);
12475 let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
12476 let array_ref = Arc::new(run_array) as ArrayRef;
12477 let cast_result = cast(&array_ref, &DataType::Int64).unwrap();
12479 let result_run_array = cast_result.as_any().downcast_ref::<Int64Array>().unwrap();
12481 assert_eq!(
12482 result_run_array.values(),
12483 &[1i64, 1i64, 2i64, 2i64, 2i64, 3i64]
12484 );
12485 }
12486
12487 #[test]
12488 fn test_sliced_run_end_encoded_to_primitive() {
12489 let run_ends = Int32Array::from(vec![2, 5, 6]);
12490 let values = Int32Array::from(vec![1, 2, 3]);
12491 let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
12493 let run_array = run_array.slice(3, 3); let array_ref = Arc::new(run_array) as ArrayRef;
12495
12496 let cast_result = cast(&array_ref, &DataType::Int64).unwrap();
12497 let result_run_array = cast_result.as_primitive::<Int64Type>();
12498 assert_eq!(result_run_array.values(), &[2, 2, 3]);
12499 }
12500
12501 #[test]
12502 fn test_run_end_encoded_to_string() {
12503 let run_ends = Int32Array::from(vec![2, 3, 5]);
12504 let values = Int32Array::from(vec![10, 20, 30]);
12505 let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
12506 let array_ref = Arc::new(run_array) as ArrayRef;
12507
12508 let cast_result = cast(&array_ref, &DataType::Utf8).unwrap();
12510
12511 let result_array = cast_result.as_any().downcast_ref::<StringArray>().unwrap();
12513 assert_eq!(result_array.value(0), "10");
12515 assert_eq!(result_array.value(1), "10");
12516 assert_eq!(result_array.value(2), "20");
12517 }
12518
12519 #[test]
12520 fn test_primitive_to_run_end_encoded() {
12521 let source_array = Int32Array::from(vec![1, 1, 2, 2, 2, 3]);
12523 let array_ref = Arc::new(source_array) as ArrayRef;
12524
12525 let target_type = DataType::RunEndEncoded(
12527 Arc::new(Field::new("run_ends", DataType::Int32, false)),
12528 Arc::new(Field::new("values", DataType::Int32, true)),
12529 );
12530 let cast_result = cast(&array_ref, &target_type).unwrap();
12531
12532 let result_run_array = cast_result
12534 .as_any()
12535 .downcast_ref::<RunArray<Int32Type>>()
12536 .unwrap();
12537
12538 assert_eq!(result_run_array.run_ends().values(), &[2, 5, 6]);
12540
12541 let values_array = result_run_array.values().as_primitive::<Int32Type>();
12543 assert_eq!(values_array.values(), &[1, 2, 3]);
12544 }
12545
12546 #[test]
12547 fn test_primitive_to_run_end_encoded_with_nulls() {
12548 let source_array = Int32Array::from(vec![
12549 Some(1),
12550 Some(1),
12551 None,
12552 None,
12553 Some(2),
12554 Some(2),
12555 Some(3),
12556 Some(3),
12557 None,
12558 None,
12559 Some(4),
12560 Some(4),
12561 Some(5),
12562 Some(5),
12563 None,
12564 None,
12565 ]);
12566 let array_ref = Arc::new(source_array) as ArrayRef;
12567 let target_type = DataType::RunEndEncoded(
12568 Arc::new(Field::new("run_ends", DataType::Int32, false)),
12569 Arc::new(Field::new("values", DataType::Int32, true)),
12570 );
12571 let cast_result = cast(&array_ref, &target_type).unwrap();
12572 let result_run_array = cast_result
12573 .as_any()
12574 .downcast_ref::<RunArray<Int32Type>>()
12575 .unwrap();
12576 assert_eq!(
12577 result_run_array.run_ends().values(),
12578 &[2, 4, 6, 8, 10, 12, 14, 16]
12579 );
12580 assert_eq!(
12581 result_run_array
12582 .values()
12583 .as_primitive::<Int32Type>()
12584 .values(),
12585 &[1, 0, 2, 3, 0, 4, 5, 0]
12586 );
12587 assert_eq!(result_run_array.values().null_count(), 3);
12588 }
12589
12590 #[test]
12591 fn test_primitive_to_run_end_encoded_with_nulls_consecutive() {
12592 let source_array = Int64Array::from(vec![
12593 Some(1),
12594 Some(1),
12595 None,
12596 None,
12597 None,
12598 None,
12599 None,
12600 None,
12601 None,
12602 None,
12603 Some(4),
12604 Some(20),
12605 Some(500),
12606 Some(500),
12607 None,
12608 None,
12609 ]);
12610 let array_ref = Arc::new(source_array) as ArrayRef;
12611 let target_type = DataType::RunEndEncoded(
12612 Arc::new(Field::new("run_ends", DataType::Int16, false)),
12613 Arc::new(Field::new("values", DataType::Int64, true)),
12614 );
12615 let cast_result = cast(&array_ref, &target_type).unwrap();
12616 let result_run_array = cast_result
12617 .as_any()
12618 .downcast_ref::<RunArray<Int16Type>>()
12619 .unwrap();
12620 assert_eq!(
12621 result_run_array.run_ends().values(),
12622 &[2, 10, 11, 12, 14, 16]
12623 );
12624 assert_eq!(
12625 result_run_array
12626 .values()
12627 .as_primitive::<Int64Type>()
12628 .values(),
12629 &[1, 0, 4, 20, 500, 0]
12630 );
12631 assert_eq!(result_run_array.values().null_count(), 2);
12632 }
12633
12634 #[test]
12635 fn test_string_to_run_end_encoded() {
12636 let source_array = StringArray::from(vec!["a", "a", "b", "c", "c"]);
12638 let array_ref = Arc::new(source_array) as ArrayRef;
12639
12640 let target_type = DataType::RunEndEncoded(
12642 Arc::new(Field::new("run_ends", DataType::Int32, false)),
12643 Arc::new(Field::new("values", DataType::Utf8, true)),
12644 );
12645 let cast_result = cast(&array_ref, &target_type).unwrap();
12646
12647 let result_run_array = cast_result
12649 .as_any()
12650 .downcast_ref::<RunArray<Int32Type>>()
12651 .unwrap();
12652
12653 assert_eq!(result_run_array.run_ends().values(), &[2, 3, 5]);
12655
12656 let values_array = result_run_array.values().as_string::<i32>();
12658 assert_eq!(values_array.value(0), "a");
12659 assert_eq!(values_array.value(1), "b");
12660 assert_eq!(values_array.value(2), "c");
12661 }
12662
12663 #[test]
12664 fn test_empty_array_to_run_end_encoded() {
12665 let source_array = Int32Array::from(Vec::<i32>::new());
12667 let array_ref = Arc::new(source_array) as ArrayRef;
12668
12669 let target_type = DataType::RunEndEncoded(
12671 Arc::new(Field::new("run_ends", DataType::Int32, false)),
12672 Arc::new(Field::new("values", DataType::Int32, true)),
12673 );
12674 let cast_result = cast(&array_ref, &target_type).unwrap();
12675
12676 let result_run_array = cast_result
12678 .as_any()
12679 .downcast_ref::<RunArray<Int32Type>>()
12680 .unwrap();
12681
12682 assert_eq!(result_run_array.run_ends().len(), 0);
12684 assert_eq!(result_run_array.values().len(), 0);
12685 }
12686
12687 #[test]
12688 fn test_run_end_encoded_with_nulls() {
12689 let run_ends = Int32Array::from(vec![2, 3, 5]);
12691 let values = Int32Array::from(vec![Some(1), None, Some(2)]);
12692 let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
12693 let array_ref = Arc::new(run_array) as ArrayRef;
12694
12695 let cast_result = cast(&array_ref, &DataType::Utf8).unwrap();
12697
12698 let result_run_array = cast_result.as_any().downcast_ref::<StringArray>().unwrap();
12700 assert_eq!(result_run_array.value(0), "1");
12701 assert!(result_run_array.is_null(2));
12702 assert_eq!(result_run_array.value(4), "2");
12703 }
12704
12705 #[test]
12706 fn test_different_index_types() {
12707 let source_array = Int32Array::from(vec![1, 1, 2, 3, 3]);
12709 let array_ref = Arc::new(source_array) as ArrayRef;
12710
12711 let target_type = DataType::RunEndEncoded(
12712 Arc::new(Field::new("run_ends", DataType::Int16, false)),
12713 Arc::new(Field::new("values", DataType::Int32, true)),
12714 );
12715 let cast_result = cast(&array_ref, &target_type).unwrap();
12716 assert_eq!(cast_result.data_type(), &target_type);
12717
12718 let run_array = cast_result
12721 .as_any()
12722 .downcast_ref::<RunArray<Int16Type>>()
12723 .unwrap();
12724 assert_eq!(run_array.values().as_primitive::<Int32Type>().value(0), 1);
12725 assert_eq!(run_array.values().as_primitive::<Int32Type>().value(1), 2);
12726 assert_eq!(run_array.values().as_primitive::<Int32Type>().value(2), 3);
12727 assert_eq!(run_array.run_ends().values(), &[2i16, 3i16, 5i16]);
12728
12729 let target_type = DataType::RunEndEncoded(
12731 Arc::new(Field::new("run_ends", DataType::Int64, false)),
12732 Arc::new(Field::new("values", DataType::Int32, true)),
12733 );
12734 let cast_result = cast(&array_ref, &target_type).unwrap();
12735 assert_eq!(cast_result.data_type(), &target_type);
12736
12737 let run_array = cast_result
12740 .as_any()
12741 .downcast_ref::<RunArray<Int64Type>>()
12742 .unwrap();
12743 assert_eq!(run_array.values().as_primitive::<Int32Type>().value(0), 1);
12744 assert_eq!(run_array.values().as_primitive::<Int32Type>().value(1), 2);
12745 assert_eq!(run_array.values().as_primitive::<Int32Type>().value(2), 3);
12746 assert_eq!(run_array.run_ends().values(), &[2i64, 3i64, 5i64]);
12747 }
12748
12749 #[test]
12750 fn test_unsupported_cast_to_run_end_encoded() {
12751 let field = Field::new("item", DataType::Int32, false);
12753 let struct_array = StructArray::from(vec![(
12754 Arc::new(field),
12755 Arc::new(Int32Array::from(vec![1, 2, 3])) as ArrayRef,
12756 )]);
12757 let array_ref = Arc::new(struct_array) as ArrayRef;
12758
12759 let cast_result = cast(&array_ref, &DataType::FixedSizeBinary(10));
12763
12764 assert!(cast_result.is_err());
12766 }
12767
12768 #[test]
12770 fn test_cast_run_end_encoded_int64_to_int16_should_fail() {
12771 let run_ends = Int64Array::from(vec![100_000, 400_000, 700_000]); let values = StringArray::from(vec!["a", "b", "c"]);
12774
12775 let ree_array = RunArray::<Int64Type>::try_new(&run_ends, &values).unwrap();
12776 let array_ref = Arc::new(ree_array) as ArrayRef;
12777
12778 let target_type = DataType::RunEndEncoded(
12780 Arc::new(Field::new("run_ends", DataType::Int16, false)),
12781 Arc::new(Field::new("values", DataType::Utf8, true)),
12782 );
12783 let cast_options = CastOptions {
12784 safe: false, format_options: FormatOptions::default(),
12786 };
12787
12788 let result: Result<Arc<dyn Array + 'static>, ArrowError> =
12790 cast_with_options(&array_ref, &target_type, &cast_options);
12791
12792 let e = result.expect_err("Cast should have failed but succeeded");
12793 assert!(
12794 e.to_string()
12795 .contains("Cast error: Can't cast value 100000 to type Int16")
12796 );
12797 }
12798
12799 #[test]
12800 fn test_cast_run_end_encoded_int64_to_int16_with_safe_should_fail_with_null_invalid_error() {
12801 let run_ends = Int64Array::from(vec![100_000, 400_000, 700_000]); let values = StringArray::from(vec!["a", "b", "c"]);
12804
12805 let ree_array = RunArray::<Int64Type>::try_new(&run_ends, &values).unwrap();
12806 let array_ref = Arc::new(ree_array) as ArrayRef;
12807
12808 let target_type = DataType::RunEndEncoded(
12810 Arc::new(Field::new("run_ends", DataType::Int16, false)),
12811 Arc::new(Field::new("values", DataType::Utf8, true)),
12812 );
12813 let cast_options = CastOptions {
12814 safe: true,
12815 format_options: FormatOptions::default(),
12816 };
12817
12818 let result: Result<Arc<dyn Array + 'static>, ArrowError> =
12820 cast_with_options(&array_ref, &target_type, &cast_options);
12821 let e = result.expect_err("Cast should have failed but succeeded");
12822 assert!(
12823 e.to_string()
12824 .contains("Invalid argument error: Found null values in run_ends array. The run_ends array should not have null values.")
12825 );
12826 }
12827
12828 #[test]
12830 fn test_cast_run_end_encoded_int16_to_int64_should_succeed() {
12831 let run_ends = Int16Array::from(vec![2, 5, 8]); let values = StringArray::from(vec!["a", "b", "c"]);
12834
12835 let ree_array = RunArray::<Int16Type>::try_new(&run_ends, &values).unwrap();
12836 let array_ref = Arc::new(ree_array) as ArrayRef;
12837
12838 let target_type = DataType::RunEndEncoded(
12840 Arc::new(Field::new("run_ends", DataType::Int64, false)),
12841 Arc::new(Field::new("values", DataType::Utf8, true)),
12842 );
12843 let cast_options = CastOptions {
12844 safe: false,
12845 format_options: FormatOptions::default(),
12846 };
12847
12848 let result: Result<Arc<dyn Array + 'static>, ArrowError> =
12850 cast_with_options(&array_ref, &target_type, &cast_options);
12851
12852 let array_ref = result.expect("Cast should have succeeded but failed");
12853 let run_array = array_ref
12855 .as_any()
12856 .downcast_ref::<RunArray<Int64Type>>()
12857 .unwrap();
12858
12859 assert_eq!(run_array.run_ends().values(), &[2i64, 5i64, 8i64]);
12862 assert_eq!(run_array.values().as_string::<i32>().value(0), "a");
12863 assert_eq!(run_array.values().as_string::<i32>().value(1), "b");
12864 assert_eq!(run_array.values().as_string::<i32>().value(2), "c");
12865 }
12866
12867 #[test]
12868 fn test_cast_run_end_encoded_dictionary_to_run_end_encoded() {
12869 let values = StringArray::from_iter([Some("a"), Some("b"), Some("c")]);
12871 let keys = UInt64Array::from_iter(vec![1, 1, 1, 0, 0, 0, 2, 2, 2]);
12872 let array_ref = Arc::new(DictionaryArray::new(keys, Arc::new(values))) as ArrayRef;
12873
12874 let target_type = DataType::RunEndEncoded(
12876 Arc::new(Field::new("run_ends", DataType::Int64, false)),
12877 Arc::new(Field::new("values", DataType::Utf8, true)),
12878 );
12879 let cast_options = CastOptions {
12880 safe: false,
12881 format_options: FormatOptions::default(),
12882 };
12883
12884 let result = cast_with_options(&array_ref, &target_type, &cast_options)
12886 .expect("Cast should have succeeded but failed");
12887
12888 let run_array = result
12891 .as_any()
12892 .downcast_ref::<RunArray<Int64Type>>()
12893 .unwrap();
12894 assert_eq!(run_array.values().as_string::<i32>().value(0), "b");
12895 assert_eq!(run_array.values().as_string::<i32>().value(1), "a");
12896 assert_eq!(run_array.values().as_string::<i32>().value(2), "c");
12897
12898 assert_eq!(run_array.run_ends().values(), &[3i64, 6i64, 9i64]);
12900 }
12901
12902 fn int32_list_values() -> Vec<Option<Vec<Option<i32>>>> {
12903 vec![
12904 Some(vec![Some(1), Some(2), Some(3)]),
12905 Some(vec![Some(4), Some(5), Some(6)]),
12906 None,
12907 Some(vec![Some(7), Some(8), Some(9)]),
12908 Some(vec![None, Some(10)]),
12909 ]
12910 }
12911
12912 #[test]
12913 fn test_cast_list_view_to_list() {
12914 let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12915 let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
12916 assert!(can_cast_types(list_view.data_type(), &target_type));
12917 let cast_result = cast(&list_view, &target_type).unwrap();
12918 let got_list = cast_result.as_list::<i32>();
12919 let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12920 assert_eq!(got_list, &expected_list);
12921 }
12922
12923 #[test]
12924 fn test_cast_list_view_to_large_list() {
12925 let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12926 let target_type = DataType::LargeList(Arc::new(Field::new("item", DataType::Int32, true)));
12927 assert!(can_cast_types(list_view.data_type(), &target_type));
12928 let cast_result = cast(&list_view, &target_type).unwrap();
12929 let got_list = cast_result.as_list::<i64>();
12930 let expected_list =
12931 LargeListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12932 assert_eq!(got_list, &expected_list);
12933 }
12934
12935 #[test]
12936 fn test_cast_list_to_list_view() {
12937 let list = ListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12938 let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Int32, true)));
12939 assert!(can_cast_types(list.data_type(), &target_type));
12940 let cast_result = cast(&list, &target_type).unwrap();
12941
12942 let got_list_view = cast_result.as_list_view::<i32>();
12943 let expected_list_view =
12944 ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12945 assert_eq!(got_list_view, &expected_list_view);
12946
12947 let list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
12949 Some(vec![Some(1), Some(2)]),
12950 None,
12951 Some(vec![None, Some(3)]),
12952 ]);
12953 let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Float32, true)));
12954 assert!(can_cast_types(list.data_type(), &target_type));
12955 let cast_result = cast(&list, &target_type).unwrap();
12956
12957 let got_list_view = cast_result.as_list_view::<i32>();
12958 let expected_list_view = ListViewArray::from_iter_primitive::<Float32Type, _, _>(vec![
12959 Some(vec![Some(1.0), Some(2.0)]),
12960 None,
12961 Some(vec![None, Some(3.0)]),
12962 ]);
12963 assert_eq!(got_list_view, &expected_list_view);
12964 }
12965
12966 #[test]
12967 fn test_cast_list_to_large_list_view() {
12968 let list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
12969 Some(vec![Some(1), Some(2)]),
12970 None,
12971 Some(vec![None, Some(3)]),
12972 ]);
12973 let target_type =
12974 DataType::LargeListView(Arc::new(Field::new("item", DataType::Float32, true)));
12975 assert!(can_cast_types(list.data_type(), &target_type));
12976 let cast_result = cast(&list, &target_type).unwrap();
12977
12978 let got_list_view = cast_result.as_list_view::<i64>();
12979 let expected_list_view =
12980 LargeListViewArray::from_iter_primitive::<Float32Type, _, _>(vec![
12981 Some(vec![Some(1.0), Some(2.0)]),
12982 None,
12983 Some(vec![None, Some(3.0)]),
12984 ]);
12985 assert_eq!(got_list_view, &expected_list_view);
12986 }
12987
12988 #[test]
12989 fn test_cast_large_list_view_to_large_list() {
12990 let list_view =
12991 LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12992 let target_type = DataType::LargeList(Arc::new(Field::new("item", DataType::Int32, true)));
12993 assert!(can_cast_types(list_view.data_type(), &target_type));
12994 let cast_result = cast(&list_view, &target_type).unwrap();
12995 let got_list = cast_result.as_list::<i64>();
12996
12997 let expected_list =
12998 LargeListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12999 assert_eq!(got_list, &expected_list);
13000 }
13001
13002 #[test]
13003 fn test_cast_large_list_view_to_list() {
13004 let list_view =
13005 LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13006 let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13007 assert!(can_cast_types(list_view.data_type(), &target_type));
13008 let cast_result = cast(&list_view, &target_type).unwrap();
13009 let got_list = cast_result.as_list::<i32>();
13010
13011 let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13012 assert_eq!(got_list, &expected_list);
13013 }
13014
13015 #[test]
13016 fn test_cast_large_list_to_large_list_view() {
13017 let list = LargeListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13018 let target_type =
13019 DataType::LargeListView(Arc::new(Field::new("item", DataType::Int32, true)));
13020 assert!(can_cast_types(list.data_type(), &target_type));
13021 let cast_result = cast(&list, &target_type).unwrap();
13022
13023 let got_list_view = cast_result.as_list_view::<i64>();
13024 let expected_list_view =
13025 LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13026 assert_eq!(got_list_view, &expected_list_view);
13027
13028 let list = LargeListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13030 Some(vec![Some(1), Some(2)]),
13031 None,
13032 Some(vec![None, Some(3)]),
13033 ]);
13034 let target_type =
13035 DataType::LargeListView(Arc::new(Field::new("item", DataType::Float32, true)));
13036 assert!(can_cast_types(list.data_type(), &target_type));
13037 let cast_result = cast(&list, &target_type).unwrap();
13038
13039 let got_list_view = cast_result.as_list_view::<i64>();
13040 let expected_list_view =
13041 LargeListViewArray::from_iter_primitive::<Float32Type, _, _>(vec![
13042 Some(vec![Some(1.0), Some(2.0)]),
13043 None,
13044 Some(vec![None, Some(3.0)]),
13045 ]);
13046 assert_eq!(got_list_view, &expected_list_view);
13047 }
13048
13049 #[test]
13050 fn test_cast_large_list_to_list_view() {
13051 let list = LargeListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13052 Some(vec![Some(1), Some(2)]),
13053 None,
13054 Some(vec![None, Some(3)]),
13055 ]);
13056 let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Float32, true)));
13057 assert!(can_cast_types(list.data_type(), &target_type));
13058 let cast_result = cast(&list, &target_type).unwrap();
13059
13060 let got_list_view = cast_result.as_list_view::<i32>();
13061 let expected_list_view = ListViewArray::from_iter_primitive::<Float32Type, _, _>(vec![
13062 Some(vec![Some(1.0), Some(2.0)]),
13063 None,
13064 Some(vec![None, Some(3.0)]),
13065 ]);
13066 assert_eq!(got_list_view, &expected_list_view);
13067 }
13068
13069 #[test]
13070 fn test_cast_list_view_to_list_out_of_order() {
13071 let list_view = ListViewArray::new(
13072 Arc::new(Field::new("item", DataType::Int32, true)),
13073 ScalarBuffer::from(vec![0, 6, 3]),
13074 ScalarBuffer::from(vec![3, 3, 3]),
13075 Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5, 6, 7, 8, 9])),
13076 None,
13077 );
13078 let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13079 assert!(can_cast_types(list_view.data_type(), &target_type));
13080 let cast_result = cast(&list_view, &target_type).unwrap();
13081 let got_list = cast_result.as_list::<i32>();
13082 let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13083 Some(vec![Some(1), Some(2), Some(3)]),
13084 Some(vec![Some(7), Some(8), Some(9)]),
13085 Some(vec![Some(4), Some(5), Some(6)]),
13086 ]);
13087 assert_eq!(got_list, &expected_list);
13088 }
13089
13090 #[test]
13091 fn test_cast_list_view_to_list_overlapping() {
13092 let list_view = ListViewArray::new(
13093 Arc::new(Field::new("item", DataType::Int32, true)),
13094 ScalarBuffer::from(vec![0, 0]),
13095 ScalarBuffer::from(vec![1, 2]),
13096 Arc::new(Int32Array::from(vec![1, 2])),
13097 None,
13098 );
13099 let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13100 assert!(can_cast_types(list_view.data_type(), &target_type));
13101 let cast_result = cast(&list_view, &target_type).unwrap();
13102 let got_list = cast_result.as_list::<i32>();
13103 let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13104 Some(vec![Some(1)]),
13105 Some(vec![Some(1), Some(2)]),
13106 ]);
13107 assert_eq!(got_list, &expected_list);
13108 }
13109
13110 #[test]
13111 fn test_cast_list_view_to_list_empty() {
13112 let values: Vec<Option<Vec<Option<i32>>>> = vec![];
13113 let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(values.clone());
13114 let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13115 assert!(can_cast_types(list_view.data_type(), &target_type));
13116 let cast_result = cast(&list_view, &target_type).unwrap();
13117 let got_list = cast_result.as_list::<i32>();
13118 let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(values);
13119 assert_eq!(got_list, &expected_list);
13120 }
13121
13122 #[test]
13123 fn test_cast_list_view_to_list_different_inner_type() {
13124 let values = int32_list_values();
13125 let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(values.clone());
13126 let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int64, true)));
13127 assert!(can_cast_types(list_view.data_type(), &target_type));
13128 let cast_result = cast(&list_view, &target_type).unwrap();
13129 let got_list = cast_result.as_list::<i32>();
13130
13131 let expected_list =
13132 ListArray::from_iter_primitive::<Int64Type, _, _>(values.into_iter().map(|list| {
13133 list.map(|list| {
13134 list.into_iter()
13135 .map(|v| v.map(|v| v as i64))
13136 .collect::<Vec<_>>()
13137 })
13138 }));
13139 assert_eq!(got_list, &expected_list);
13140 }
13141
13142 #[test]
13143 fn test_cast_list_view_to_list_out_of_order_with_nulls() {
13144 let list_view = ListViewArray::new(
13145 Arc::new(Field::new("item", DataType::Int32, true)),
13146 ScalarBuffer::from(vec![0, 6, 3]),
13147 ScalarBuffer::from(vec![3, 3, 3]),
13148 Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5, 6, 7, 8, 9])),
13149 Some(NullBuffer::from(vec![false, true, false])),
13150 );
13151 let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13152 assert!(can_cast_types(list_view.data_type(), &target_type));
13153 let cast_result = cast(&list_view, &target_type).unwrap();
13154 let got_list = cast_result.as_list::<i32>();
13155 let expected_list = ListArray::new(
13156 Arc::new(Field::new("item", DataType::Int32, true)),
13157 OffsetBuffer::from_lengths([3, 3, 3]),
13158 Arc::new(Int32Array::from(vec![1, 2, 3, 7, 8, 9, 4, 5, 6])),
13159 Some(NullBuffer::from(vec![false, true, false])),
13160 );
13161 assert_eq!(got_list, &expected_list);
13162 }
13163
13164 #[test]
13165 fn test_cast_list_view_to_large_list_view() {
13166 let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13167 let target_type =
13168 DataType::LargeListView(Arc::new(Field::new("item", DataType::Int32, true)));
13169 assert!(can_cast_types(list_view.data_type(), &target_type));
13170 let cast_result = cast(&list_view, &target_type).unwrap();
13171 let got = cast_result.as_list_view::<i64>();
13172
13173 let expected =
13174 LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13175 assert_eq!(got, &expected);
13176 }
13177
13178 #[test]
13179 fn test_cast_large_list_view_to_list_view() {
13180 let list_view =
13181 LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13182 let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Int32, true)));
13183 assert!(can_cast_types(list_view.data_type(), &target_type));
13184 let cast_result = cast(&list_view, &target_type).unwrap();
13185 let got = cast_result.as_list_view::<i32>();
13186
13187 let expected = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13188 assert_eq!(got, &expected);
13189 }
13190
13191 #[test]
13192 fn test_cast_time32_second_to_int64() {
13193 let array = Time32SecondArray::from(vec![1000, 2000, 3000]);
13194 let array = Arc::new(array) as Arc<dyn Array>;
13195 let to_type = DataType::Int64;
13196 let cast_options = CastOptions::default();
13197
13198 assert!(can_cast_types(array.data_type(), &to_type));
13199
13200 let result = cast_with_options(&array, &to_type, &cast_options);
13201 assert!(
13202 result.is_ok(),
13203 "Failed to cast Time32(Second) to Int64: {:?}",
13204 result.err()
13205 );
13206
13207 let cast_array = result.unwrap();
13208 let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
13209
13210 assert_eq!(cast_array.value(0), 1000);
13211 assert_eq!(cast_array.value(1), 2000);
13212 assert_eq!(cast_array.value(2), 3000);
13213 }
13214
13215 #[test]
13216 fn test_cast_time32_millisecond_to_int64() {
13217 let array = Time32MillisecondArray::from(vec![1000, 2000, 3000]);
13218 let array = Arc::new(array) as Arc<dyn Array>;
13219 let to_type = DataType::Int64;
13220 let cast_options = CastOptions::default();
13221
13222 assert!(can_cast_types(array.data_type(), &to_type));
13223
13224 let result = cast_with_options(&array, &to_type, &cast_options);
13225 assert!(
13226 result.is_ok(),
13227 "Failed to cast Time32(Millisecond) to Int64: {:?}",
13228 result.err()
13229 );
13230
13231 let cast_array = result.unwrap();
13232 let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
13233
13234 assert_eq!(cast_array.value(0), 1000);
13235 assert_eq!(cast_array.value(1), 2000);
13236 assert_eq!(cast_array.value(2), 3000);
13237 }
13238
13239 #[test]
13240 fn test_cast_string_to_time32_second_to_int64() {
13241 let array = StringArray::from(vec!["03:12:44"]);
13244 let array = Arc::new(array) as Arc<dyn Array>;
13245 let cast_options = CastOptions::default();
13246
13247 let time32_type = DataType::Time32(TimeUnit::Second);
13249 let time32_array = cast_with_options(&array, &time32_type, &cast_options).unwrap();
13250
13251 let int64_type = DataType::Int64;
13253 assert!(can_cast_types(time32_array.data_type(), &int64_type));
13254
13255 let result = cast_with_options(&time32_array, &int64_type, &cast_options);
13256
13257 assert!(
13258 result.is_ok(),
13259 "Failed to cast Time32(Second) to Int64: {:?}",
13260 result.err()
13261 );
13262
13263 let cast_array = result.unwrap();
13264 let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
13265
13266 assert_eq!(cast_array.value(0), 11564);
13268 }
13269 #[test]
13270 fn test_string_dicts_to_binary_view() {
13271 let expected = BinaryViewArray::from_iter(vec![
13272 VIEW_TEST_DATA[1],
13273 VIEW_TEST_DATA[0],
13274 None,
13275 VIEW_TEST_DATA[3],
13276 None,
13277 VIEW_TEST_DATA[1],
13278 VIEW_TEST_DATA[4],
13279 ]);
13280
13281 let values_arrays: [ArrayRef; _] = [
13282 Arc::new(StringArray::from_iter(VIEW_TEST_DATA)),
13283 Arc::new(StringViewArray::from_iter(VIEW_TEST_DATA)),
13284 Arc::new(LargeStringArray::from_iter(VIEW_TEST_DATA)),
13285 ];
13286 for values in values_arrays {
13287 let keys =
13288 Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
13289 let string_dict_array = DictionaryArray::<Int8Type>::try_new(keys, values).unwrap();
13290
13291 let casted = cast(&string_dict_array, &DataType::BinaryView).unwrap();
13292 assert_eq!(casted.as_ref(), &expected);
13293 }
13294 }
13295
13296 #[test]
13297 fn test_binary_dicts_to_string_view() {
13298 let expected = StringViewArray::from_iter(vec![
13299 VIEW_TEST_DATA[1],
13300 VIEW_TEST_DATA[0],
13301 None,
13302 VIEW_TEST_DATA[3],
13303 None,
13304 VIEW_TEST_DATA[1],
13305 VIEW_TEST_DATA[4],
13306 ]);
13307
13308 let values_arrays: [ArrayRef; _] = [
13309 Arc::new(BinaryArray::from_iter(VIEW_TEST_DATA)),
13310 Arc::new(BinaryViewArray::from_iter(VIEW_TEST_DATA)),
13311 Arc::new(LargeBinaryArray::from_iter(VIEW_TEST_DATA)),
13312 ];
13313 for values in values_arrays {
13314 let keys =
13315 Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
13316 let string_dict_array = DictionaryArray::<Int8Type>::try_new(keys, values).unwrap();
13317
13318 let casted = cast(&string_dict_array, &DataType::Utf8View).unwrap();
13319 assert_eq!(casted.as_ref(), &expected);
13320 }
13321 }
13322
13323 #[test]
13324 fn test_cast_between_sliced_run_end_encoded() {
13325 let run_ends = Int16Array::from(vec![2, 5, 8]);
13326 let values = StringArray::from(vec!["a", "b", "c"]);
13327
13328 let ree_array = RunArray::<Int16Type>::try_new(&run_ends, &values).unwrap();
13329 let ree_array = ree_array.slice(1, 2);
13330 let array_ref = Arc::new(ree_array) as ArrayRef;
13331
13332 let target_type = DataType::RunEndEncoded(
13333 Arc::new(Field::new("run_ends", DataType::Int64, false)),
13334 Arc::new(Field::new("values", DataType::Utf8, true)),
13335 );
13336 let cast_options = CastOptions {
13337 safe: false,
13338 format_options: FormatOptions::default(),
13339 };
13340
13341 let result = cast_with_options(&array_ref, &target_type, &cast_options).unwrap();
13342 let run_array = result.as_run::<Int64Type>();
13343 let run_array = run_array.downcast::<StringArray>().unwrap();
13344
13345 let expected = vec!["a", "b"];
13346 let actual = run_array.into_iter().flatten().collect::<Vec<_>>();
13347
13348 assert_eq!(expected, actual);
13349 }
13350}