1mod decimal;
41mod dictionary;
42mod list;
43mod map;
44mod run_array;
45mod string;
46mod union;
47
48use crate::cast::decimal::*;
49use crate::cast::dictionary::*;
50use crate::cast::list::*;
51use crate::cast::map::*;
52use crate::cast::run_array::*;
53use crate::cast::string::*;
54pub use crate::cast::union::*;
55
56use arrow_buffer::IntervalMonthDayNano;
57use arrow_data::ByteView;
58use chrono::{NaiveTime, Offset, TimeZone, Utc};
59use std::cmp::Ordering;
60use std::sync::Arc;
61
62use crate::display::{ArrayFormatter, FormatOptions};
63use crate::parse::{
64 Parser, parse_interval_day_time, parse_interval_month_day_nano, parse_interval_year_month,
65 string_to_datetime,
66};
67use arrow_array::{builder::*, cast::*, temporal_conversions::*, timezone::Tz, types::*, *};
68use arrow_buffer::{ArrowNativeType, OffsetBuffer, i256};
69use arrow_data::ArrayData;
70use arrow_data::transform::MutableArrayData;
71use arrow_schema::*;
72use arrow_select::take::take;
73use num_traits::{NumCast, ToPrimitive, cast::AsPrimitive};
74
75pub use decimal::{
76 DecimalCast, parse_string_to_decimal_native, rescale_decimal, single_float_to_decimal,
77};
78pub use string::cast_single_string_to_boolean_default;
79
80#[inline(always)]
86pub fn single_decimal_to_float_lossy<D, F>(f: &F, x: D::Native, scale: i32) -> f64
87where
88 D: DecimalType,
89 F: Fn(D::Native) -> f64,
90{
91 f(x) / 10_f64.powi(scale)
92}
93
94#[derive(Debug, Clone, PartialEq, Eq, Hash)]
96pub struct CastOptions<'a> {
97 pub safe: bool,
99 pub format_options: FormatOptions<'a>,
101}
102
103impl Default for CastOptions<'_> {
104 fn default() -> Self {
105 Self {
106 safe: true,
107 format_options: FormatOptions::default(),
108 }
109 }
110}
111
112pub fn can_cast_types(from_type: &DataType, to_type: &DataType) -> bool {
116 use self::DataType::*;
117 use self::IntervalUnit::*;
118 use self::TimeUnit::*;
119 if from_type == to_type {
120 return true;
121 }
122
123 match (from_type, to_type) {
124 (Null, _) => true,
125 (Dictionary(_, from_value_type), Dictionary(_, to_value_type)) => {
127 can_cast_types(from_value_type, to_value_type)
128 }
129 (Dictionary(_, value_type), _) => can_cast_types(value_type, to_type),
130 (Union(fields, _), _) => union::resolve_child_array(fields, to_type).is_some(),
131 (_, Union(_, _)) => false,
132 (RunEndEncoded(_, value_type), _) => can_cast_types(value_type.data_type(), to_type),
133 (_, RunEndEncoded(_, value_type)) => can_cast_types(from_type, value_type.data_type()),
134 (_, Dictionary(_, value_type)) => can_cast_types(from_type, value_type),
135 (
136 List(list_from) | LargeList(list_from) | ListView(list_from) | LargeListView(list_from),
137 List(list_to) | LargeList(list_to) | ListView(list_to) | LargeListView(list_to),
138 ) => can_cast_types(list_from.data_type(), list_to.data_type()),
139 (
140 List(list_from) | LargeList(list_from) | ListView(list_from) | LargeListView(list_from),
141 Utf8 | LargeUtf8 | Utf8View,
142 ) => can_cast_types(list_from.data_type(), to_type),
143 (
144 FixedSizeList(list_from, _),
145 List(list_to) | LargeList(list_to) | ListView(list_to) | LargeListView(list_to),
146 ) => can_cast_types(list_from.data_type(), list_to.data_type()),
147 (
148 List(list_from) | LargeList(list_from) | ListView(list_from) | LargeListView(list_from),
149 FixedSizeList(list_to, _),
150 ) => can_cast_types(list_from.data_type(), list_to.data_type()),
151 (FixedSizeList(inner, size), FixedSizeList(inner_to, size_to)) if size == size_to => {
152 can_cast_types(inner.data_type(), inner_to.data_type())
153 }
154 (_, List(list_to) | LargeList(list_to) | ListView(list_to) | LargeListView(list_to)) => {
155 can_cast_types(from_type, list_to.data_type())
156 }
157 (_, FixedSizeList(list_to, size)) if *size == 1 => {
158 can_cast_types(from_type, list_to.data_type())
159 }
160 (FixedSizeList(list_from, size), _) if *size == 1 => {
161 can_cast_types(list_from.data_type(), to_type)
162 }
163 (Map(from_entries, ordered_from), Map(to_entries, ordered_to))
164 if ordered_from == ordered_to =>
165 {
166 match (
167 key_field(from_entries),
168 key_field(to_entries),
169 value_field(from_entries),
170 value_field(to_entries),
171 ) {
172 (Some(from_key), Some(to_key), Some(from_value), Some(to_value)) => {
173 can_cast_types(from_key.data_type(), to_key.data_type())
174 && can_cast_types(from_value.data_type(), to_value.data_type())
175 }
176 _ => false,
177 }
178 }
179 (
181 Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
182 Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
183 ) => true,
184 (
186 UInt8 | UInt16 | UInt32 | UInt64,
187 Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
188 ) => true,
189 (
191 Int8 | Int16 | Int32 | Int64 | Float16 | Float32 | Float64,
192 Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
193 ) => true,
194 (
196 Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
197 UInt8 | UInt16 | UInt32 | UInt64,
198 ) => true,
199 (
201 Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
202 Null | Int8 | Int16 | Int32 | Int64 | Float16 | Float32 | Float64,
203 ) => true,
204 (
206 Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
207 Utf8View | Utf8 | LargeUtf8,
208 ) => true,
209 (
211 Utf8View | Utf8 | LargeUtf8,
212 Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
213 ) => true,
214 (Struct(from_fields), Struct(to_fields)) => {
215 if from_fields.len() != to_fields.len() {
216 return false;
217 }
218
219 if from_fields
221 .iter()
222 .zip(to_fields.iter())
223 .all(|(f1, f2)| f1.name() == f2.name())
224 {
225 return from_fields.iter().zip(to_fields.iter()).all(|(f1, f2)| {
226 can_cast_types(f1.data_type(), f2.data_type())
229 });
230 }
231
232 if to_fields.iter().all(|to_field| {
234 from_fields
235 .iter()
236 .find(|from_field| from_field.name() == to_field.name())
237 .is_some_and(|from_field| {
238 can_cast_types(from_field.data_type(), to_field.data_type())
241 })
242 }) {
243 return true;
244 }
245
246 from_fields
248 .iter()
249 .zip(to_fields.iter())
250 .all(|(f1, f2)| can_cast_types(f1.data_type(), f2.data_type()))
251 }
252 (Struct(_), _) => false,
253 (_, Struct(_)) => false,
254 (_, Boolean) => from_type.is_integer() || from_type.is_floating() || from_type.is_string(),
255 (Boolean, _) => to_type.is_integer() || to_type.is_floating() || to_type.is_string(),
256
257 (Binary, LargeBinary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView | Utf8View) => {
258 true
259 }
260 (LargeBinary, Binary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView | Utf8View) => {
261 true
262 }
263 (FixedSizeBinary(_), Binary | LargeBinary | BinaryView) => true,
264 (
265 Utf8 | LargeUtf8 | Utf8View,
266 Binary
267 | LargeBinary
268 | Utf8
269 | LargeUtf8
270 | Date32
271 | Date64
272 | Time32(Second)
273 | Time32(Millisecond)
274 | Time64(Microsecond)
275 | Time64(Nanosecond)
276 | Timestamp(Second, _)
277 | Timestamp(Millisecond, _)
278 | Timestamp(Microsecond, _)
279 | Timestamp(Nanosecond, _)
280 | Interval(_)
281 | BinaryView,
282 ) => true,
283 (Utf8 | LargeUtf8, Utf8View) => true,
284 (BinaryView, Binary | LargeBinary | Utf8 | LargeUtf8 | Utf8View) => true,
285 (Utf8View | Utf8 | LargeUtf8, _) => to_type.is_numeric(),
286 (_, Utf8 | Utf8View | LargeUtf8) => from_type.is_primitive(),
287
288 (_, Binary | LargeBinary) => from_type.is_integer(),
289
290 (
292 UInt8 | UInt16 | UInt32 | UInt64 | Int8 | Int16 | Int32 | Int64 | Float16 | Float32
293 | Float64,
294 UInt8 | UInt16 | UInt32 | UInt64 | Int8 | Int16 | Int32 | Int64 | Float16 | Float32
295 | Float64,
296 ) => true,
297 (Int32, Date32 | Date64 | Time32(_)) => true,
301 (Date32, Int32 | Int64) => true,
302 (Time32(_), Int32 | Int64) => true,
303 (Int64, Date64 | Date32 | Time64(_)) => true,
304 (Date64, Int64 | Int32) => true,
305 (Time64(_), Int64) => true,
306 (Date32 | Date64, Date32 | Date64) => true,
307 (Time32(_), Time32(_)) => true,
309 (Time32(_), Time64(_)) => true,
310 (Time64(_), Time64(_)) => true,
311 (Time64(_), Time32(to_unit)) => {
312 matches!(to_unit, Second | Millisecond)
313 }
314 (Timestamp(_, _), _) if to_type.is_numeric() => true,
315 (_, Timestamp(_, _)) if from_type.is_numeric() => true,
316 (Date64, Timestamp(_, _)) => true,
317 (Date32, Timestamp(_, _)) => true,
318 (
319 Timestamp(_, _),
320 Timestamp(_, _)
321 | Date32
322 | Date64
323 | Time32(Second)
324 | Time32(Millisecond)
325 | Time64(Microsecond)
326 | Time64(Nanosecond),
327 ) => true,
328 (_, Duration(_)) if from_type.is_numeric() => true,
329 (Duration(_), _) if to_type.is_numeric() => true,
330 (Duration(_), Duration(_)) => true,
331 (Interval(from_type), Int64) => {
332 match from_type {
333 YearMonth => true,
334 DayTime => true,
335 MonthDayNano => false, }
337 }
338 (Int32, Interval(to_type)) => match to_type {
339 YearMonth => true,
340 DayTime => false,
341 MonthDayNano => false,
342 },
343 (Duration(_), Interval(MonthDayNano)) => true,
344 (Interval(MonthDayNano), Duration(_)) => true,
345 (Interval(YearMonth), Interval(MonthDayNano)) => true,
346 (Interval(DayTime), Interval(MonthDayNano)) => true,
347 (_, _) => false,
348 }
349}
350
351pub fn cast(array: &dyn Array, to_type: &DataType) -> Result<ArrayRef, ArrowError> {
355 cast_with_options(array, to_type, &CastOptions::default())
356}
357
358fn cast_integer_to_decimal<
359 T: ArrowPrimitiveType,
360 D: DecimalType + ArrowPrimitiveType<Native = M>,
361 M,
362>(
363 array: &PrimitiveArray<T>,
364 precision: u8,
365 scale: i8,
366 base: M,
367 cast_options: &CastOptions,
368) -> Result<ArrayRef, ArrowError>
369where
370 <T as ArrowPrimitiveType>::Native: AsPrimitive<M>,
371 M: ArrowNativeTypeOp,
372{
373 let scale_factor = base.pow_checked(scale.unsigned_abs() as u32).map_err(|_| {
374 ArrowError::CastError(format!(
375 "Cannot cast to {:?}({}, {}). The scale causes overflow.",
376 D::PREFIX,
377 precision,
378 scale,
379 ))
380 })?;
381
382 let array = if scale < 0 {
383 match cast_options.safe {
384 true => array.unary_opt::<_, D>(|v| {
385 v.as_()
386 .div_checked(scale_factor)
387 .ok()
388 .and_then(|v| (D::is_valid_decimal_precision(v, precision)).then_some(v))
389 }),
390 false => array.try_unary::<_, D, _>(|v| {
391 v.as_()
392 .div_checked(scale_factor)
393 .and_then(|v| D::validate_decimal_precision(v, precision, scale).map(|_| v))
394 })?,
395 }
396 } else {
397 match cast_options.safe {
398 true => array.unary_opt::<_, D>(|v| {
399 v.as_()
400 .mul_checked(scale_factor)
401 .ok()
402 .and_then(|v| (D::is_valid_decimal_precision(v, precision)).then_some(v))
403 }),
404 false => array.try_unary::<_, D, _>(|v| {
405 v.as_()
406 .mul_checked(scale_factor)
407 .and_then(|v| D::validate_decimal_precision(v, precision, scale).map(|_| v))
408 })?,
409 }
410 };
411
412 Ok(Arc::new(array.with_precision_and_scale(precision, scale)?))
413}
414
415fn cast_interval_year_month_to_interval_month_day_nano(
417 array: &dyn Array,
418 _cast_options: &CastOptions,
419) -> Result<ArrayRef, ArrowError> {
420 let array = array.as_primitive::<IntervalYearMonthType>();
421
422 Ok(Arc::new(array.unary::<_, IntervalMonthDayNanoType>(|v| {
423 let months = IntervalYearMonthType::to_months(v);
424 IntervalMonthDayNanoType::make_value(months, 0, 0)
425 })))
426}
427
428fn cast_interval_day_time_to_interval_month_day_nano(
430 array: &dyn Array,
431 _cast_options: &CastOptions,
432) -> Result<ArrayRef, ArrowError> {
433 let array = array.as_primitive::<IntervalDayTimeType>();
434 let mul = 1_000_000;
435
436 Ok(Arc::new(array.unary::<_, IntervalMonthDayNanoType>(|v| {
437 let (days, ms) = IntervalDayTimeType::to_parts(v);
438 IntervalMonthDayNanoType::make_value(0, days, ms as i64 * mul)
439 })))
440}
441
442fn cast_month_day_nano_to_duration<D: ArrowTemporalType<Native = i64>>(
444 array: &dyn Array,
445 cast_options: &CastOptions,
446) -> Result<ArrayRef, ArrowError> {
447 let array = array.as_primitive::<IntervalMonthDayNanoType>();
448 let scale = match D::DATA_TYPE {
449 DataType::Duration(TimeUnit::Second) => 1_000_000_000,
450 DataType::Duration(TimeUnit::Millisecond) => 1_000_000,
451 DataType::Duration(TimeUnit::Microsecond) => 1_000,
452 DataType::Duration(TimeUnit::Nanosecond) => 1,
453 _ => unreachable!(),
454 };
455
456 if cast_options.safe {
457 let iter = array.iter().map(|v| {
458 v.and_then(|v| (v.days == 0 && v.months == 0).then_some(v.nanoseconds / scale))
459 });
460 Ok(Arc::new(unsafe {
461 PrimitiveArray::<D>::from_trusted_len_iter(iter)
462 }))
463 } else {
464 let vec = array
465 .iter()
466 .map(|v| {
467 v.map(|v| match v.days == 0 && v.months == 0 {
468 true => Ok((v.nanoseconds) / scale),
469 _ => Err(ArrowError::ComputeError(
470 "Cannot convert interval containing non-zero months or days to duration"
471 .to_string(),
472 )),
473 })
474 .transpose()
475 })
476 .collect::<Result<Vec<_>, _>>()?;
477 Ok(Arc::new(unsafe {
478 PrimitiveArray::<D>::from_trusted_len_iter(vec.iter())
479 }))
480 }
481}
482
483fn cast_duration_to_interval<D: ArrowTemporalType<Native = i64>>(
485 array: &dyn Array,
486 cast_options: &CastOptions,
487) -> Result<ArrayRef, ArrowError> {
488 let array = array
489 .as_any()
490 .downcast_ref::<PrimitiveArray<D>>()
491 .ok_or_else(|| {
492 ArrowError::ComputeError(
493 "Internal Error: Cannot cast duration to DurationArray of expected type"
494 .to_string(),
495 )
496 })?;
497
498 let scale = match array.data_type() {
499 DataType::Duration(TimeUnit::Second) => 1_000_000_000,
500 DataType::Duration(TimeUnit::Millisecond) => 1_000_000,
501 DataType::Duration(TimeUnit::Microsecond) => 1_000,
502 DataType::Duration(TimeUnit::Nanosecond) => 1,
503 _ => unreachable!(),
504 };
505
506 if cast_options.safe {
507 let iter = array.iter().map(|v| {
508 v.and_then(|v| {
509 v.checked_mul(scale)
510 .map(|v| IntervalMonthDayNano::new(0, 0, v))
511 })
512 });
513 Ok(Arc::new(unsafe {
514 PrimitiveArray::<IntervalMonthDayNanoType>::from_trusted_len_iter(iter)
515 }))
516 } else {
517 let vec = array
518 .iter()
519 .map(|v| {
520 v.map(|v| {
521 if let Ok(v) = v.mul_checked(scale) {
522 Ok(IntervalMonthDayNano::new(0, 0, v))
523 } else {
524 Err(ArrowError::ComputeError(format!(
525 "Cannot cast to {:?}. Overflowing on {:?}",
526 IntervalMonthDayNanoType::DATA_TYPE,
527 v
528 )))
529 }
530 })
531 .transpose()
532 })
533 .collect::<Result<Vec<_>, _>>()?;
534 Ok(Arc::new(unsafe {
535 PrimitiveArray::<IntervalMonthDayNanoType>::from_trusted_len_iter(vec.iter())
536 }))
537 }
538}
539
540fn cast_reinterpret_arrays<I: ArrowPrimitiveType, O: ArrowPrimitiveType<Native = I::Native>>(
542 array: &dyn Array,
543) -> Result<ArrayRef, ArrowError> {
544 Ok(Arc::new(array.as_primitive::<I>().reinterpret_cast::<O>()))
545}
546
547fn make_timestamp_array(
548 array: &PrimitiveArray<Int64Type>,
549 unit: TimeUnit,
550 tz: Option<Arc<str>>,
551) -> ArrayRef {
552 match unit {
553 TimeUnit::Second => Arc::new(
554 array
555 .reinterpret_cast::<TimestampSecondType>()
556 .with_timezone_opt(tz),
557 ),
558 TimeUnit::Millisecond => Arc::new(
559 array
560 .reinterpret_cast::<TimestampMillisecondType>()
561 .with_timezone_opt(tz),
562 ),
563 TimeUnit::Microsecond => Arc::new(
564 array
565 .reinterpret_cast::<TimestampMicrosecondType>()
566 .with_timezone_opt(tz),
567 ),
568 TimeUnit::Nanosecond => Arc::new(
569 array
570 .reinterpret_cast::<TimestampNanosecondType>()
571 .with_timezone_opt(tz),
572 ),
573 }
574}
575
576fn make_duration_array(array: &PrimitiveArray<Int64Type>, unit: TimeUnit) -> ArrayRef {
577 match unit {
578 TimeUnit::Second => Arc::new(array.reinterpret_cast::<DurationSecondType>()),
579 TimeUnit::Millisecond => Arc::new(array.reinterpret_cast::<DurationMillisecondType>()),
580 TimeUnit::Microsecond => Arc::new(array.reinterpret_cast::<DurationMicrosecondType>()),
581 TimeUnit::Nanosecond => Arc::new(array.reinterpret_cast::<DurationNanosecondType>()),
582 }
583}
584
585fn as_time_res_with_timezone<T: ArrowPrimitiveType>(
586 v: i64,
587 tz: Option<Tz>,
588) -> Result<NaiveTime, ArrowError> {
589 let time = match tz {
590 Some(tz) => as_datetime_with_timezone::<T>(v, tz).map(|d| d.time()),
591 None => as_datetime::<T>(v).map(|d| d.time()),
592 };
593
594 time.ok_or_else(|| {
595 ArrowError::CastError(format!(
596 "Failed to create naive time with {} {}",
597 std::any::type_name::<T>(),
598 v
599 ))
600 })
601}
602
603fn timestamp_to_date32<T: ArrowTimestampType>(
604 array: &PrimitiveArray<T>,
605) -> Result<ArrayRef, ArrowError> {
606 let err = |x: i64| {
607 ArrowError::CastError(format!(
608 "Cannot convert {} {x} to datetime",
609 std::any::type_name::<T>()
610 ))
611 };
612
613 let array: Date32Array = match array.timezone() {
614 Some(tz) => {
615 let tz: Tz = tz.parse()?;
616 array.try_unary(|x| {
617 as_datetime_with_timezone::<T>(x, tz)
618 .ok_or_else(|| err(x))
619 .map(|d| Date32Type::from_naive_date(d.date_naive()))
620 })?
621 }
622 None => array.try_unary(|x| {
623 as_datetime::<T>(x)
624 .ok_or_else(|| err(x))
625 .map(|d| Date32Type::from_naive_date(d.date()))
626 })?,
627 };
628 Ok(Arc::new(array))
629}
630
631pub fn cast_with_options(
761 array: &dyn Array,
762 to_type: &DataType,
763 cast_options: &CastOptions,
764) -> Result<ArrayRef, ArrowError> {
765 use DataType::*;
766 let from_type = array.data_type();
767 if from_type == to_type {
769 return Ok(make_array(array.to_data()));
770 }
771 match (from_type, to_type) {
772 (Null, _) => Ok(new_null_array(to_type, array.len())),
773 (RunEndEncoded(index_type, _), _) => match index_type.data_type() {
774 Int16 => run_end_encoded_cast::<Int16Type>(array, to_type, cast_options),
775 Int32 => run_end_encoded_cast::<Int32Type>(array, to_type, cast_options),
776 Int64 => run_end_encoded_cast::<Int64Type>(array, to_type, cast_options),
777 _ => Err(ArrowError::CastError(format!(
778 "Casting from run end encoded type {from_type:?} to {to_type:?} not supported",
779 ))),
780 },
781 (_, RunEndEncoded(index_type, value_type)) => {
782 let array_ref = make_array(array.to_data());
783 match index_type.data_type() {
784 Int16 => cast_to_run_end_encoded::<Int16Type>(
785 &array_ref,
786 value_type.data_type(),
787 cast_options,
788 ),
789 Int32 => cast_to_run_end_encoded::<Int32Type>(
790 &array_ref,
791 value_type.data_type(),
792 cast_options,
793 ),
794 Int64 => cast_to_run_end_encoded::<Int64Type>(
795 &array_ref,
796 value_type.data_type(),
797 cast_options,
798 ),
799 _ => Err(ArrowError::CastError(format!(
800 "Casting from type {from_type:?} to run end encoded type {to_type:?} not supported",
801 ))),
802 }
803 }
804 (Union(_, _), _) => union_extract_by_type(
805 array.as_any().downcast_ref::<UnionArray>().unwrap(),
806 to_type,
807 cast_options,
808 ),
809 (_, Union(_, _)) => Err(ArrowError::CastError(format!(
810 "Casting from {from_type} to {to_type} not supported"
811 ))),
812 (Dictionary(index_type, _), _) => match **index_type {
813 Int8 => dictionary_cast::<Int8Type>(array, to_type, cast_options),
814 Int16 => dictionary_cast::<Int16Type>(array, to_type, cast_options),
815 Int32 => dictionary_cast::<Int32Type>(array, to_type, cast_options),
816 Int64 => dictionary_cast::<Int64Type>(array, to_type, cast_options),
817 UInt8 => dictionary_cast::<UInt8Type>(array, to_type, cast_options),
818 UInt16 => dictionary_cast::<UInt16Type>(array, to_type, cast_options),
819 UInt32 => dictionary_cast::<UInt32Type>(array, to_type, cast_options),
820 UInt64 => dictionary_cast::<UInt64Type>(array, to_type, cast_options),
821 _ => Err(ArrowError::CastError(format!(
822 "Casting from dictionary type {from_type} to {to_type} not supported",
823 ))),
824 },
825 (_, Dictionary(index_type, value_type)) => match **index_type {
826 Int8 => cast_to_dictionary::<Int8Type>(array, value_type, cast_options),
827 Int16 => cast_to_dictionary::<Int16Type>(array, value_type, cast_options),
828 Int32 => cast_to_dictionary::<Int32Type>(array, value_type, cast_options),
829 Int64 => cast_to_dictionary::<Int64Type>(array, value_type, cast_options),
830 UInt8 => cast_to_dictionary::<UInt8Type>(array, value_type, cast_options),
831 UInt16 => cast_to_dictionary::<UInt16Type>(array, value_type, cast_options),
832 UInt32 => cast_to_dictionary::<UInt32Type>(array, value_type, cast_options),
833 UInt64 => cast_to_dictionary::<UInt64Type>(array, value_type, cast_options),
834 _ => Err(ArrowError::CastError(format!(
835 "Casting from type {from_type} to dictionary type {to_type} not supported",
836 ))),
837 },
838 (List(_), List(to)) => cast_list_values::<i32>(array, to, cast_options),
840 (LargeList(_), LargeList(to)) => cast_list_values::<i64>(array, to, cast_options),
841 (FixedSizeList(_, size_from), FixedSizeList(list_to, size_to)) => {
842 if size_from != size_to {
843 return Err(ArrowError::CastError(
844 "cannot cast fixed-size-list to fixed-size-list with different size".into(),
845 ));
846 }
847 let array = array.as_fixed_size_list();
848 let values = cast_with_options(array.values(), list_to.data_type(), cast_options)?;
849 Ok(Arc::new(FixedSizeListArray::try_new(
850 list_to.clone(),
851 *size_from,
852 values,
853 array.nulls().cloned(),
854 )?))
855 }
856 (ListView(_), ListView(to)) => cast_list_view_values::<i32>(array, to, cast_options),
857 (LargeListView(_), LargeListView(to)) => {
858 cast_list_view_values::<i64>(array, to, cast_options)
859 }
860 (List(_), LargeList(list_to)) => cast_list::<i32, i64>(array, list_to, cast_options),
863 (List(_), FixedSizeList(field, size)) => {
864 cast_list_to_fixed_size_list::<i32>(array, field, *size, cast_options)
865 }
866 (List(_), ListView(list_to)) => {
867 cast_list_to_list_view::<i32, i32>(array, list_to, cast_options)
868 }
869 (List(_), LargeListView(list_to)) => {
870 cast_list_to_list_view::<i32, i64>(array, list_to, cast_options)
871 }
872 (LargeList(_), List(list_to)) => cast_list::<i64, i32>(array, list_to, cast_options),
874 (LargeList(_), FixedSizeList(field, size)) => {
875 cast_list_to_fixed_size_list::<i64>(array, field, *size, cast_options)
876 }
877 (LargeList(_), ListView(list_to)) => {
878 cast_list_to_list_view::<i64, i32>(array, list_to, cast_options)
879 }
880 (LargeList(_), LargeListView(list_to)) => {
881 cast_list_to_list_view::<i64, i64>(array, list_to, cast_options)
882 }
883 (ListView(_), List(list_to)) => {
885 cast_list_view_to_list::<i32, Int32Type>(array, list_to, cast_options)
886 }
887 (ListView(_), LargeList(list_to)) => {
888 cast_list_view_to_list::<i32, Int64Type>(array, list_to, cast_options)
889 }
890 (ListView(_), LargeListView(list_to)) => {
891 cast_list_view::<i32, i64>(array, list_to, cast_options)
892 }
893 (ListView(_), FixedSizeList(field, size)) => {
894 cast_list_view_to_fixed_size_list::<i32>(array, field, *size, cast_options)
895 }
896 (LargeListView(_), LargeList(list_to)) => {
898 cast_list_view_to_list::<i64, Int64Type>(array, list_to, cast_options)
899 }
900 (LargeListView(_), List(list_to)) => {
901 cast_list_view_to_list::<i64, Int32Type>(array, list_to, cast_options)
902 }
903 (LargeListView(_), ListView(list_to)) => {
904 cast_list_view::<i64, i32>(array, list_to, cast_options)
905 }
906 (LargeListView(_), FixedSizeList(field, size)) => {
907 cast_list_view_to_fixed_size_list::<i64>(array, field, *size, cast_options)
908 }
909 (FixedSizeList(_, _), List(list_to)) => {
911 cast_fixed_size_list_to_list::<i32>(array, list_to, cast_options)
912 }
913 (FixedSizeList(_, _), LargeList(list_to)) => {
914 cast_fixed_size_list_to_list::<i64>(array, list_to, cast_options)
915 }
916 (FixedSizeList(_, _), ListView(list_to)) => {
917 cast_fixed_size_list_to_list_view::<i32>(array, list_to, cast_options)
918 }
919 (FixedSizeList(_, _), LargeListView(list_to)) => {
920 cast_fixed_size_list_to_list_view::<i64>(array, list_to, cast_options)
921 }
922 (FixedSizeList(_, size), _) if *size == 1 => {
924 cast_single_element_fixed_size_list_to_values(array, to_type, cast_options)
925 }
926 (List(_) | LargeList(_) | ListView(_) | LargeListView(_), _) => match to_type {
929 Utf8 => value_to_string::<i32>(array, cast_options),
930 LargeUtf8 => value_to_string::<i64>(array, cast_options),
931 Utf8View => value_to_string_view(array, cast_options),
932 dt => Err(ArrowError::CastError(format!(
933 "Cannot cast LIST to non-list data type {dt}"
934 ))),
935 },
936 (_, List(to)) => cast_values_to_list::<i32>(array, to, cast_options),
937 (_, LargeList(to)) => cast_values_to_list::<i64>(array, to, cast_options),
938 (_, ListView(to)) => cast_values_to_list_view::<i32>(array, to, cast_options),
939 (_, LargeListView(to)) => cast_values_to_list_view::<i64>(array, to, cast_options),
940 (_, FixedSizeList(to, size)) if *size == 1 => {
941 cast_values_to_fixed_size_list(array, to, *size, cast_options)
942 }
943 (Map(_, ordered1), Map(_, ordered2)) if ordered1 == ordered2 => {
945 cast_map_values(array.as_map(), to_type, cast_options, ordered1.to_owned())
946 }
947 (Decimal32(p1, s1), Decimal32(p2, s2)) => {
949 cast_decimal_to_decimal_same_type::<Decimal32Type>(
950 array.as_primitive(),
951 *p1,
952 *s1,
953 *p2,
954 *s2,
955 cast_options,
956 )
957 }
958 (Decimal64(p1, s1), Decimal64(p2, s2)) => {
959 cast_decimal_to_decimal_same_type::<Decimal64Type>(
960 array.as_primitive(),
961 *p1,
962 *s1,
963 *p2,
964 *s2,
965 cast_options,
966 )
967 }
968 (Decimal128(p1, s1), Decimal128(p2, s2)) => {
969 cast_decimal_to_decimal_same_type::<Decimal128Type>(
970 array.as_primitive(),
971 *p1,
972 *s1,
973 *p2,
974 *s2,
975 cast_options,
976 )
977 }
978 (Decimal256(p1, s1), Decimal256(p2, s2)) => {
979 cast_decimal_to_decimal_same_type::<Decimal256Type>(
980 array.as_primitive(),
981 *p1,
982 *s1,
983 *p2,
984 *s2,
985 cast_options,
986 )
987 }
988 (Decimal32(p1, s1), Decimal64(p2, s2)) => {
990 cast_decimal_to_decimal::<Decimal32Type, Decimal64Type>(
991 array.as_primitive(),
992 *p1,
993 *s1,
994 *p2,
995 *s2,
996 cast_options,
997 )
998 }
999 (Decimal32(p1, s1), Decimal128(p2, s2)) => {
1000 cast_decimal_to_decimal::<Decimal32Type, Decimal128Type>(
1001 array.as_primitive(),
1002 *p1,
1003 *s1,
1004 *p2,
1005 *s2,
1006 cast_options,
1007 )
1008 }
1009 (Decimal32(p1, s1), Decimal256(p2, s2)) => {
1010 cast_decimal_to_decimal::<Decimal32Type, Decimal256Type>(
1011 array.as_primitive(),
1012 *p1,
1013 *s1,
1014 *p2,
1015 *s2,
1016 cast_options,
1017 )
1018 }
1019 (Decimal64(p1, s1), Decimal32(p2, s2)) => {
1020 cast_decimal_to_decimal::<Decimal64Type, Decimal32Type>(
1021 array.as_primitive(),
1022 *p1,
1023 *s1,
1024 *p2,
1025 *s2,
1026 cast_options,
1027 )
1028 }
1029 (Decimal64(p1, s1), Decimal128(p2, s2)) => {
1030 cast_decimal_to_decimal::<Decimal64Type, Decimal128Type>(
1031 array.as_primitive(),
1032 *p1,
1033 *s1,
1034 *p2,
1035 *s2,
1036 cast_options,
1037 )
1038 }
1039 (Decimal64(p1, s1), Decimal256(p2, s2)) => {
1040 cast_decimal_to_decimal::<Decimal64Type, Decimal256Type>(
1041 array.as_primitive(),
1042 *p1,
1043 *s1,
1044 *p2,
1045 *s2,
1046 cast_options,
1047 )
1048 }
1049 (Decimal128(p1, s1), Decimal32(p2, s2)) => {
1050 cast_decimal_to_decimal::<Decimal128Type, Decimal32Type>(
1051 array.as_primitive(),
1052 *p1,
1053 *s1,
1054 *p2,
1055 *s2,
1056 cast_options,
1057 )
1058 }
1059 (Decimal128(p1, s1), Decimal64(p2, s2)) => {
1060 cast_decimal_to_decimal::<Decimal128Type, Decimal64Type>(
1061 array.as_primitive(),
1062 *p1,
1063 *s1,
1064 *p2,
1065 *s2,
1066 cast_options,
1067 )
1068 }
1069 (Decimal128(p1, s1), Decimal256(p2, s2)) => {
1070 cast_decimal_to_decimal::<Decimal128Type, Decimal256Type>(
1071 array.as_primitive(),
1072 *p1,
1073 *s1,
1074 *p2,
1075 *s2,
1076 cast_options,
1077 )
1078 }
1079 (Decimal256(p1, s1), Decimal32(p2, s2)) => {
1080 cast_decimal_to_decimal::<Decimal256Type, Decimal32Type>(
1081 array.as_primitive(),
1082 *p1,
1083 *s1,
1084 *p2,
1085 *s2,
1086 cast_options,
1087 )
1088 }
1089 (Decimal256(p1, s1), Decimal64(p2, s2)) => {
1090 cast_decimal_to_decimal::<Decimal256Type, Decimal64Type>(
1091 array.as_primitive(),
1092 *p1,
1093 *s1,
1094 *p2,
1095 *s2,
1096 cast_options,
1097 )
1098 }
1099 (Decimal256(p1, s1), Decimal128(p2, s2)) => {
1100 cast_decimal_to_decimal::<Decimal256Type, Decimal128Type>(
1101 array.as_primitive(),
1102 *p1,
1103 *s1,
1104 *p2,
1105 *s2,
1106 cast_options,
1107 )
1108 }
1109 (Decimal32(_, scale), _) if !to_type.is_temporal() => {
1111 cast_from_decimal::<Decimal32Type, _>(
1112 array,
1113 10_i32,
1114 scale,
1115 from_type,
1116 to_type,
1117 |x: i32| x as f64,
1118 cast_options,
1119 )
1120 }
1121 (Decimal64(_, scale), _) if !to_type.is_temporal() => {
1122 cast_from_decimal::<Decimal64Type, _>(
1123 array,
1124 10_i64,
1125 scale,
1126 from_type,
1127 to_type,
1128 |x: i64| x as f64,
1129 cast_options,
1130 )
1131 }
1132 (Decimal128(_, scale), _) if !to_type.is_temporal() => {
1133 cast_from_decimal::<Decimal128Type, _>(
1134 array,
1135 10_i128,
1136 scale,
1137 from_type,
1138 to_type,
1139 |x: i128| x as f64,
1140 cast_options,
1141 )
1142 }
1143 (Decimal256(_, scale), _) if !to_type.is_temporal() => {
1144 cast_from_decimal::<Decimal256Type, _>(
1145 array,
1146 i256::from_i128(10_i128),
1147 scale,
1148 from_type,
1149 to_type,
1150 |x: i256| x.to_f64().expect("All i256 values fit in f64"),
1151 cast_options,
1152 )
1153 }
1154 (_, Decimal32(precision, scale)) if !from_type.is_temporal() => {
1156 cast_to_decimal::<Decimal32Type, _>(
1157 array,
1158 10_i32,
1159 precision,
1160 scale,
1161 from_type,
1162 to_type,
1163 cast_options,
1164 )
1165 }
1166 (_, Decimal64(precision, scale)) if !from_type.is_temporal() => {
1167 cast_to_decimal::<Decimal64Type, _>(
1168 array,
1169 10_i64,
1170 precision,
1171 scale,
1172 from_type,
1173 to_type,
1174 cast_options,
1175 )
1176 }
1177 (_, Decimal128(precision, scale)) if !from_type.is_temporal() => {
1178 cast_to_decimal::<Decimal128Type, _>(
1179 array,
1180 10_i128,
1181 precision,
1182 scale,
1183 from_type,
1184 to_type,
1185 cast_options,
1186 )
1187 }
1188 (_, Decimal256(precision, scale)) if !from_type.is_temporal() => {
1189 cast_to_decimal::<Decimal256Type, _>(
1190 array,
1191 i256::from_i128(10_i128),
1192 precision,
1193 scale,
1194 from_type,
1195 to_type,
1196 cast_options,
1197 )
1198 }
1199 (Struct(from_fields), Struct(to_fields)) => cast_struct_to_struct(
1200 array.as_struct(),
1201 from_fields.clone(),
1202 to_fields.clone(),
1203 cast_options,
1204 ),
1205 (Struct(_), _) => Err(ArrowError::CastError(format!(
1206 "Casting from {from_type} to {to_type} not supported"
1207 ))),
1208 (_, Struct(_)) => Err(ArrowError::CastError(format!(
1209 "Casting from {from_type} to {to_type} not supported"
1210 ))),
1211 (_, Boolean) => match from_type {
1212 UInt8 => cast_numeric_to_bool::<UInt8Type>(array),
1213 UInt16 => cast_numeric_to_bool::<UInt16Type>(array),
1214 UInt32 => cast_numeric_to_bool::<UInt32Type>(array),
1215 UInt64 => cast_numeric_to_bool::<UInt64Type>(array),
1216 Int8 => cast_numeric_to_bool::<Int8Type>(array),
1217 Int16 => cast_numeric_to_bool::<Int16Type>(array),
1218 Int32 => cast_numeric_to_bool::<Int32Type>(array),
1219 Int64 => cast_numeric_to_bool::<Int64Type>(array),
1220 Float16 => cast_numeric_to_bool::<Float16Type>(array),
1221 Float32 => cast_numeric_to_bool::<Float32Type>(array),
1222 Float64 => cast_numeric_to_bool::<Float64Type>(array),
1223 Utf8View => cast_utf8view_to_boolean(array, cast_options),
1224 Utf8 => cast_utf8_to_boolean::<i32>(array, cast_options),
1225 LargeUtf8 => cast_utf8_to_boolean::<i64>(array, cast_options),
1226 _ => Err(ArrowError::CastError(format!(
1227 "Casting from {from_type} to {to_type} not supported",
1228 ))),
1229 },
1230 (Boolean, _) => match to_type {
1231 UInt8 => cast_bool_to_numeric::<UInt8Type>(array, cast_options),
1232 UInt16 => cast_bool_to_numeric::<UInt16Type>(array, cast_options),
1233 UInt32 => cast_bool_to_numeric::<UInt32Type>(array, cast_options),
1234 UInt64 => cast_bool_to_numeric::<UInt64Type>(array, cast_options),
1235 Int8 => cast_bool_to_numeric::<Int8Type>(array, cast_options),
1236 Int16 => cast_bool_to_numeric::<Int16Type>(array, cast_options),
1237 Int32 => cast_bool_to_numeric::<Int32Type>(array, cast_options),
1238 Int64 => cast_bool_to_numeric::<Int64Type>(array, cast_options),
1239 Float16 => cast_bool_to_numeric::<Float16Type>(array, cast_options),
1240 Float32 => cast_bool_to_numeric::<Float32Type>(array, cast_options),
1241 Float64 => cast_bool_to_numeric::<Float64Type>(array, cast_options),
1242 Utf8View => value_to_string_view(array, cast_options),
1243 Utf8 => value_to_string::<i32>(array, cast_options),
1244 LargeUtf8 => value_to_string::<i64>(array, cast_options),
1245 _ => Err(ArrowError::CastError(format!(
1246 "Casting from {from_type} to {to_type} not supported",
1247 ))),
1248 },
1249 (Utf8, _) => match to_type {
1250 UInt8 => parse_string::<UInt8Type, i32>(array, cast_options),
1251 UInt16 => parse_string::<UInt16Type, i32>(array, cast_options),
1252 UInt32 => parse_string::<UInt32Type, i32>(array, cast_options),
1253 UInt64 => parse_string::<UInt64Type, i32>(array, cast_options),
1254 Int8 => parse_string::<Int8Type, i32>(array, cast_options),
1255 Int16 => parse_string::<Int16Type, i32>(array, cast_options),
1256 Int32 => parse_string::<Int32Type, i32>(array, cast_options),
1257 Int64 => parse_string::<Int64Type, i32>(array, cast_options),
1258 Float16 => parse_string::<Float16Type, i32>(array, cast_options),
1259 Float32 => parse_string::<Float32Type, i32>(array, cast_options),
1260 Float64 => parse_string::<Float64Type, i32>(array, cast_options),
1261 Date32 => parse_string::<Date32Type, i32>(array, cast_options),
1262 Date64 => parse_string::<Date64Type, i32>(array, cast_options),
1263 Binary => Ok(Arc::new(BinaryArray::from(
1264 array.as_string::<i32>().clone(),
1265 ))),
1266 LargeBinary => {
1267 let binary = BinaryArray::from(array.as_string::<i32>().clone());
1268 cast_byte_container::<BinaryType, LargeBinaryType>(&binary)
1269 }
1270 Utf8View => Ok(Arc::new(StringViewArray::from(array.as_string::<i32>()))),
1271 BinaryView => Ok(Arc::new(
1272 StringViewArray::from(array.as_string::<i32>()).to_binary_view(),
1273 )),
1274 LargeUtf8 => cast_byte_container::<Utf8Type, LargeUtf8Type>(array),
1275 Time32(TimeUnit::Second) => parse_string::<Time32SecondType, i32>(array, cast_options),
1276 Time32(TimeUnit::Millisecond) => {
1277 parse_string::<Time32MillisecondType, i32>(array, cast_options)
1278 }
1279 Time64(TimeUnit::Microsecond) => {
1280 parse_string::<Time64MicrosecondType, i32>(array, cast_options)
1281 }
1282 Time64(TimeUnit::Nanosecond) => {
1283 parse_string::<Time64NanosecondType, i32>(array, cast_options)
1284 }
1285 Timestamp(TimeUnit::Second, to_tz) => {
1286 cast_string_to_timestamp::<i32, TimestampSecondType>(array, to_tz, cast_options)
1287 }
1288 Timestamp(TimeUnit::Millisecond, to_tz) => cast_string_to_timestamp::<
1289 i32,
1290 TimestampMillisecondType,
1291 >(array, to_tz, cast_options),
1292 Timestamp(TimeUnit::Microsecond, to_tz) => cast_string_to_timestamp::<
1293 i32,
1294 TimestampMicrosecondType,
1295 >(array, to_tz, cast_options),
1296 Timestamp(TimeUnit::Nanosecond, to_tz) => {
1297 cast_string_to_timestamp::<i32, TimestampNanosecondType>(array, to_tz, cast_options)
1298 }
1299 Interval(IntervalUnit::YearMonth) => {
1300 cast_string_to_year_month_interval::<i32>(array, cast_options)
1301 }
1302 Interval(IntervalUnit::DayTime) => {
1303 cast_string_to_day_time_interval::<i32>(array, cast_options)
1304 }
1305 Interval(IntervalUnit::MonthDayNano) => {
1306 cast_string_to_month_day_nano_interval::<i32>(array, cast_options)
1307 }
1308 _ => Err(ArrowError::CastError(format!(
1309 "Casting from {from_type} to {to_type} not supported",
1310 ))),
1311 },
1312 (Utf8View, _) => match to_type {
1313 UInt8 => parse_string_view::<UInt8Type>(array, cast_options),
1314 UInt16 => parse_string_view::<UInt16Type>(array, cast_options),
1315 UInt32 => parse_string_view::<UInt32Type>(array, cast_options),
1316 UInt64 => parse_string_view::<UInt64Type>(array, cast_options),
1317 Int8 => parse_string_view::<Int8Type>(array, cast_options),
1318 Int16 => parse_string_view::<Int16Type>(array, cast_options),
1319 Int32 => parse_string_view::<Int32Type>(array, cast_options),
1320 Int64 => parse_string_view::<Int64Type>(array, cast_options),
1321 Float16 => parse_string_view::<Float16Type>(array, cast_options),
1322 Float32 => parse_string_view::<Float32Type>(array, cast_options),
1323 Float64 => parse_string_view::<Float64Type>(array, cast_options),
1324 Date32 => parse_string_view::<Date32Type>(array, cast_options),
1325 Date64 => parse_string_view::<Date64Type>(array, cast_options),
1326 Binary => cast_view_to_byte::<StringViewType, GenericBinaryType<i32>>(array),
1327 LargeBinary => cast_view_to_byte::<StringViewType, GenericBinaryType<i64>>(array),
1328 BinaryView => Ok(Arc::new(array.as_string_view().clone().to_binary_view())),
1329 Utf8 => cast_view_to_byte::<StringViewType, GenericStringType<i32>>(array),
1330 LargeUtf8 => cast_view_to_byte::<StringViewType, GenericStringType<i64>>(array),
1331 Time32(TimeUnit::Second) => parse_string_view::<Time32SecondType>(array, cast_options),
1332 Time32(TimeUnit::Millisecond) => {
1333 parse_string_view::<Time32MillisecondType>(array, cast_options)
1334 }
1335 Time64(TimeUnit::Microsecond) => {
1336 parse_string_view::<Time64MicrosecondType>(array, cast_options)
1337 }
1338 Time64(TimeUnit::Nanosecond) => {
1339 parse_string_view::<Time64NanosecondType>(array, cast_options)
1340 }
1341 Timestamp(TimeUnit::Second, to_tz) => {
1342 cast_view_to_timestamp::<TimestampSecondType>(array, to_tz, cast_options)
1343 }
1344 Timestamp(TimeUnit::Millisecond, to_tz) => {
1345 cast_view_to_timestamp::<TimestampMillisecondType>(array, to_tz, cast_options)
1346 }
1347 Timestamp(TimeUnit::Microsecond, to_tz) => {
1348 cast_view_to_timestamp::<TimestampMicrosecondType>(array, to_tz, cast_options)
1349 }
1350 Timestamp(TimeUnit::Nanosecond, to_tz) => {
1351 cast_view_to_timestamp::<TimestampNanosecondType>(array, to_tz, cast_options)
1352 }
1353 Interval(IntervalUnit::YearMonth) => {
1354 cast_view_to_year_month_interval(array, cast_options)
1355 }
1356 Interval(IntervalUnit::DayTime) => cast_view_to_day_time_interval(array, cast_options),
1357 Interval(IntervalUnit::MonthDayNano) => {
1358 cast_view_to_month_day_nano_interval(array, cast_options)
1359 }
1360 _ => Err(ArrowError::CastError(format!(
1361 "Casting from {from_type} to {to_type} not supported",
1362 ))),
1363 },
1364 (LargeUtf8, _) => match to_type {
1365 UInt8 => parse_string::<UInt8Type, i64>(array, cast_options),
1366 UInt16 => parse_string::<UInt16Type, i64>(array, cast_options),
1367 UInt32 => parse_string::<UInt32Type, i64>(array, cast_options),
1368 UInt64 => parse_string::<UInt64Type, i64>(array, cast_options),
1369 Int8 => parse_string::<Int8Type, i64>(array, cast_options),
1370 Int16 => parse_string::<Int16Type, i64>(array, cast_options),
1371 Int32 => parse_string::<Int32Type, i64>(array, cast_options),
1372 Int64 => parse_string::<Int64Type, i64>(array, cast_options),
1373 Float16 => parse_string::<Float16Type, i64>(array, cast_options),
1374 Float32 => parse_string::<Float32Type, i64>(array, cast_options),
1375 Float64 => parse_string::<Float64Type, i64>(array, cast_options),
1376 Date32 => parse_string::<Date32Type, i64>(array, cast_options),
1377 Date64 => parse_string::<Date64Type, i64>(array, cast_options),
1378 Utf8 => cast_byte_container::<LargeUtf8Type, Utf8Type>(array),
1379 Binary => {
1380 let large_binary = LargeBinaryArray::from(array.as_string::<i64>().clone());
1381 cast_byte_container::<LargeBinaryType, BinaryType>(&large_binary)
1382 }
1383 LargeBinary => Ok(Arc::new(LargeBinaryArray::from(
1384 array.as_string::<i64>().clone(),
1385 ))),
1386 Utf8View => Ok(Arc::new(StringViewArray::from(array.as_string::<i64>()))),
1387 BinaryView => Ok(Arc::new(BinaryViewArray::from(
1388 array
1389 .as_string::<i64>()
1390 .into_iter()
1391 .map(|x| x.map(|x| x.as_bytes()))
1392 .collect::<Vec<_>>(),
1393 ))),
1394 Time32(TimeUnit::Second) => parse_string::<Time32SecondType, i64>(array, cast_options),
1395 Time32(TimeUnit::Millisecond) => {
1396 parse_string::<Time32MillisecondType, i64>(array, cast_options)
1397 }
1398 Time64(TimeUnit::Microsecond) => {
1399 parse_string::<Time64MicrosecondType, i64>(array, cast_options)
1400 }
1401 Time64(TimeUnit::Nanosecond) => {
1402 parse_string::<Time64NanosecondType, i64>(array, cast_options)
1403 }
1404 Timestamp(TimeUnit::Second, to_tz) => {
1405 cast_string_to_timestamp::<i64, TimestampSecondType>(array, to_tz, cast_options)
1406 }
1407 Timestamp(TimeUnit::Millisecond, to_tz) => cast_string_to_timestamp::<
1408 i64,
1409 TimestampMillisecondType,
1410 >(array, to_tz, cast_options),
1411 Timestamp(TimeUnit::Microsecond, to_tz) => cast_string_to_timestamp::<
1412 i64,
1413 TimestampMicrosecondType,
1414 >(array, to_tz, cast_options),
1415 Timestamp(TimeUnit::Nanosecond, to_tz) => {
1416 cast_string_to_timestamp::<i64, TimestampNanosecondType>(array, to_tz, cast_options)
1417 }
1418 Interval(IntervalUnit::YearMonth) => {
1419 cast_string_to_year_month_interval::<i64>(array, cast_options)
1420 }
1421 Interval(IntervalUnit::DayTime) => {
1422 cast_string_to_day_time_interval::<i64>(array, cast_options)
1423 }
1424 Interval(IntervalUnit::MonthDayNano) => {
1425 cast_string_to_month_day_nano_interval::<i64>(array, cast_options)
1426 }
1427 _ => Err(ArrowError::CastError(format!(
1428 "Casting from {from_type} to {to_type} not supported",
1429 ))),
1430 },
1431 (Binary, _) => match to_type {
1432 Utf8 => cast_binary_to_string::<i32>(array, cast_options),
1433 LargeUtf8 => {
1434 let array = cast_binary_to_string::<i32>(array, cast_options)?;
1435 cast_byte_container::<Utf8Type, LargeUtf8Type>(array.as_ref())
1436 }
1437 LargeBinary => cast_byte_container::<BinaryType, LargeBinaryType>(array),
1438 FixedSizeBinary(size) => {
1439 cast_binary_to_fixed_size_binary::<i32>(array, *size, cast_options)
1440 }
1441 BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i32>()))),
1442 Utf8View => Ok(Arc::new(StringViewArray::from(
1443 cast_binary_to_string::<i32>(array, cast_options)?.as_string::<i32>(),
1444 ))),
1445 _ => Err(ArrowError::CastError(format!(
1446 "Casting from {from_type} to {to_type} not supported",
1447 ))),
1448 },
1449 (LargeBinary, _) => match to_type {
1450 Utf8 => {
1451 let array = cast_binary_to_string::<i64>(array, cast_options)?;
1452 cast_byte_container::<LargeUtf8Type, Utf8Type>(array.as_ref())
1453 }
1454 LargeUtf8 => cast_binary_to_string::<i64>(array, cast_options),
1455 Binary => cast_byte_container::<LargeBinaryType, BinaryType>(array),
1456 FixedSizeBinary(size) => {
1457 cast_binary_to_fixed_size_binary::<i64>(array, *size, cast_options)
1458 }
1459 BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i64>()))),
1460 Utf8View => {
1461 let array = cast_binary_to_string::<i64>(array, cast_options)?;
1462 Ok(Arc::new(StringViewArray::from(array.as_string::<i64>())))
1463 }
1464 _ => Err(ArrowError::CastError(format!(
1465 "Casting from {from_type} to {to_type} not supported",
1466 ))),
1467 },
1468 (FixedSizeBinary(size), _) => match to_type {
1469 Binary => cast_fixed_size_binary_to_binary::<i32>(array, *size),
1470 LargeBinary => cast_fixed_size_binary_to_binary::<i64>(array, *size),
1471 BinaryView => cast_fixed_size_binary_to_binary_view(array, *size),
1472 _ => Err(ArrowError::CastError(format!(
1473 "Casting from {from_type} to {to_type} not supported",
1474 ))),
1475 },
1476 (BinaryView, Binary) => cast_view_to_byte::<BinaryViewType, GenericBinaryType<i32>>(array),
1477 (BinaryView, LargeBinary) => {
1478 cast_view_to_byte::<BinaryViewType, GenericBinaryType<i64>>(array)
1479 }
1480 (BinaryView, Utf8) => {
1481 let binary_arr = cast_view_to_byte::<BinaryViewType, GenericBinaryType<i32>>(array)?;
1482 cast_binary_to_string::<i32>(&binary_arr, cast_options)
1483 }
1484 (BinaryView, LargeUtf8) => {
1485 let binary_arr = cast_view_to_byte::<BinaryViewType, GenericBinaryType<i64>>(array)?;
1486 cast_binary_to_string::<i64>(&binary_arr, cast_options)
1487 }
1488 (BinaryView, Utf8View) => cast_binary_view_to_string_view(array, cast_options),
1489 (BinaryView, _) => Err(ArrowError::CastError(format!(
1490 "Casting from {from_type} to {to_type} not supported",
1491 ))),
1492 (from_type, Utf8View) if from_type.is_primitive() => {
1493 value_to_string_view(array, cast_options)
1494 }
1495 (from_type, LargeUtf8) if from_type.is_primitive() => {
1496 value_to_string::<i64>(array, cast_options)
1497 }
1498 (from_type, Utf8) if from_type.is_primitive() => {
1499 value_to_string::<i32>(array, cast_options)
1500 }
1501 (from_type, Binary) if from_type.is_integer() => match from_type {
1502 UInt8 => cast_numeric_to_binary::<UInt8Type, i32>(array),
1503 UInt16 => cast_numeric_to_binary::<UInt16Type, i32>(array),
1504 UInt32 => cast_numeric_to_binary::<UInt32Type, i32>(array),
1505 UInt64 => cast_numeric_to_binary::<UInt64Type, i32>(array),
1506 Int8 => cast_numeric_to_binary::<Int8Type, i32>(array),
1507 Int16 => cast_numeric_to_binary::<Int16Type, i32>(array),
1508 Int32 => cast_numeric_to_binary::<Int32Type, i32>(array),
1509 Int64 => cast_numeric_to_binary::<Int64Type, i32>(array),
1510 _ => unreachable!(),
1511 },
1512 (from_type, LargeBinary) if from_type.is_integer() => match from_type {
1513 UInt8 => cast_numeric_to_binary::<UInt8Type, i64>(array),
1514 UInt16 => cast_numeric_to_binary::<UInt16Type, i64>(array),
1515 UInt32 => cast_numeric_to_binary::<UInt32Type, i64>(array),
1516 UInt64 => cast_numeric_to_binary::<UInt64Type, i64>(array),
1517 Int8 => cast_numeric_to_binary::<Int8Type, i64>(array),
1518 Int16 => cast_numeric_to_binary::<Int16Type, i64>(array),
1519 Int32 => cast_numeric_to_binary::<Int32Type, i64>(array),
1520 Int64 => cast_numeric_to_binary::<Int64Type, i64>(array),
1521 _ => unreachable!(),
1522 },
1523 (UInt8, UInt16) => cast_numeric_arrays::<UInt8Type, UInt16Type>(array, cast_options),
1525 (UInt8, UInt32) => cast_numeric_arrays::<UInt8Type, UInt32Type>(array, cast_options),
1526 (UInt8, UInt64) => cast_numeric_arrays::<UInt8Type, UInt64Type>(array, cast_options),
1527 (UInt8, Int8) => cast_numeric_arrays::<UInt8Type, Int8Type>(array, cast_options),
1528 (UInt8, Int16) => cast_numeric_arrays::<UInt8Type, Int16Type>(array, cast_options),
1529 (UInt8, Int32) => cast_numeric_arrays::<UInt8Type, Int32Type>(array, cast_options),
1530 (UInt8, Int64) => cast_numeric_arrays::<UInt8Type, Int64Type>(array, cast_options),
1531 (UInt8, Float16) => cast_numeric_arrays::<UInt8Type, Float16Type>(array, cast_options),
1532 (UInt8, Float32) => cast_numeric_arrays::<UInt8Type, Float32Type>(array, cast_options),
1533 (UInt8, Float64) => cast_numeric_arrays::<UInt8Type, Float64Type>(array, cast_options),
1534
1535 (UInt16, UInt8) => cast_numeric_arrays::<UInt16Type, UInt8Type>(array, cast_options),
1536 (UInt16, UInt32) => cast_numeric_arrays::<UInt16Type, UInt32Type>(array, cast_options),
1537 (UInt16, UInt64) => cast_numeric_arrays::<UInt16Type, UInt64Type>(array, cast_options),
1538 (UInt16, Int8) => cast_numeric_arrays::<UInt16Type, Int8Type>(array, cast_options),
1539 (UInt16, Int16) => cast_numeric_arrays::<UInt16Type, Int16Type>(array, cast_options),
1540 (UInt16, Int32) => cast_numeric_arrays::<UInt16Type, Int32Type>(array, cast_options),
1541 (UInt16, Int64) => cast_numeric_arrays::<UInt16Type, Int64Type>(array, cast_options),
1542 (UInt16, Float16) => cast_numeric_arrays::<UInt16Type, Float16Type>(array, cast_options),
1543 (UInt16, Float32) => cast_numeric_arrays::<UInt16Type, Float32Type>(array, cast_options),
1544 (UInt16, Float64) => cast_numeric_arrays::<UInt16Type, Float64Type>(array, cast_options),
1545
1546 (UInt32, UInt8) => cast_numeric_arrays::<UInt32Type, UInt8Type>(array, cast_options),
1547 (UInt32, UInt16) => cast_numeric_arrays::<UInt32Type, UInt16Type>(array, cast_options),
1548 (UInt32, UInt64) => cast_numeric_arrays::<UInt32Type, UInt64Type>(array, cast_options),
1549 (UInt32, Int8) => cast_numeric_arrays::<UInt32Type, Int8Type>(array, cast_options),
1550 (UInt32, Int16) => cast_numeric_arrays::<UInt32Type, Int16Type>(array, cast_options),
1551 (UInt32, Int32) => cast_numeric_arrays::<UInt32Type, Int32Type>(array, cast_options),
1552 (UInt32, Int64) => cast_numeric_arrays::<UInt32Type, Int64Type>(array, cast_options),
1553 (UInt32, Float16) => cast_numeric_arrays::<UInt32Type, Float16Type>(array, cast_options),
1554 (UInt32, Float32) => cast_numeric_arrays::<UInt32Type, Float32Type>(array, cast_options),
1555 (UInt32, Float64) => cast_numeric_arrays::<UInt32Type, Float64Type>(array, cast_options),
1556
1557 (UInt64, UInt8) => cast_numeric_arrays::<UInt64Type, UInt8Type>(array, cast_options),
1558 (UInt64, UInt16) => cast_numeric_arrays::<UInt64Type, UInt16Type>(array, cast_options),
1559 (UInt64, UInt32) => cast_numeric_arrays::<UInt64Type, UInt32Type>(array, cast_options),
1560 (UInt64, Int8) => cast_numeric_arrays::<UInt64Type, Int8Type>(array, cast_options),
1561 (UInt64, Int16) => cast_numeric_arrays::<UInt64Type, Int16Type>(array, cast_options),
1562 (UInt64, Int32) => cast_numeric_arrays::<UInt64Type, Int32Type>(array, cast_options),
1563 (UInt64, Int64) => cast_numeric_arrays::<UInt64Type, Int64Type>(array, cast_options),
1564 (UInt64, Float16) => cast_numeric_arrays::<UInt64Type, Float16Type>(array, cast_options),
1565 (UInt64, Float32) => cast_numeric_arrays::<UInt64Type, Float32Type>(array, cast_options),
1566 (UInt64, Float64) => cast_numeric_arrays::<UInt64Type, Float64Type>(array, cast_options),
1567
1568 (Int8, UInt8) => cast_numeric_arrays::<Int8Type, UInt8Type>(array, cast_options),
1569 (Int8, UInt16) => cast_numeric_arrays::<Int8Type, UInt16Type>(array, cast_options),
1570 (Int8, UInt32) => cast_numeric_arrays::<Int8Type, UInt32Type>(array, cast_options),
1571 (Int8, UInt64) => cast_numeric_arrays::<Int8Type, UInt64Type>(array, cast_options),
1572 (Int8, Int16) => cast_numeric_arrays::<Int8Type, Int16Type>(array, cast_options),
1573 (Int8, Int32) => cast_numeric_arrays::<Int8Type, Int32Type>(array, cast_options),
1574 (Int8, Int64) => cast_numeric_arrays::<Int8Type, Int64Type>(array, cast_options),
1575 (Int8, Float16) => cast_numeric_arrays::<Int8Type, Float16Type>(array, cast_options),
1576 (Int8, Float32) => cast_numeric_arrays::<Int8Type, Float32Type>(array, cast_options),
1577 (Int8, Float64) => cast_numeric_arrays::<Int8Type, Float64Type>(array, cast_options),
1578
1579 (Int16, UInt8) => cast_numeric_arrays::<Int16Type, UInt8Type>(array, cast_options),
1580 (Int16, UInt16) => cast_numeric_arrays::<Int16Type, UInt16Type>(array, cast_options),
1581 (Int16, UInt32) => cast_numeric_arrays::<Int16Type, UInt32Type>(array, cast_options),
1582 (Int16, UInt64) => cast_numeric_arrays::<Int16Type, UInt64Type>(array, cast_options),
1583 (Int16, Int8) => cast_numeric_arrays::<Int16Type, Int8Type>(array, cast_options),
1584 (Int16, Int32) => cast_numeric_arrays::<Int16Type, Int32Type>(array, cast_options),
1585 (Int16, Int64) => cast_numeric_arrays::<Int16Type, Int64Type>(array, cast_options),
1586 (Int16, Float16) => cast_numeric_arrays::<Int16Type, Float16Type>(array, cast_options),
1587 (Int16, Float32) => cast_numeric_arrays::<Int16Type, Float32Type>(array, cast_options),
1588 (Int16, Float64) => cast_numeric_arrays::<Int16Type, Float64Type>(array, cast_options),
1589
1590 (Int32, UInt8) => cast_numeric_arrays::<Int32Type, UInt8Type>(array, cast_options),
1591 (Int32, UInt16) => cast_numeric_arrays::<Int32Type, UInt16Type>(array, cast_options),
1592 (Int32, UInt32) => cast_numeric_arrays::<Int32Type, UInt32Type>(array, cast_options),
1593 (Int32, UInt64) => cast_numeric_arrays::<Int32Type, UInt64Type>(array, cast_options),
1594 (Int32, Int8) => cast_numeric_arrays::<Int32Type, Int8Type>(array, cast_options),
1595 (Int32, Int16) => cast_numeric_arrays::<Int32Type, Int16Type>(array, cast_options),
1596 (Int32, Int64) => cast_numeric_arrays::<Int32Type, Int64Type>(array, cast_options),
1597 (Int32, Float16) => cast_numeric_arrays::<Int32Type, Float16Type>(array, cast_options),
1598 (Int32, Float32) => cast_numeric_arrays::<Int32Type, Float32Type>(array, cast_options),
1599 (Int32, Float64) => cast_numeric_arrays::<Int32Type, Float64Type>(array, cast_options),
1600
1601 (Int64, UInt8) => cast_numeric_arrays::<Int64Type, UInt8Type>(array, cast_options),
1602 (Int64, UInt16) => cast_numeric_arrays::<Int64Type, UInt16Type>(array, cast_options),
1603 (Int64, UInt32) => cast_numeric_arrays::<Int64Type, UInt32Type>(array, cast_options),
1604 (Int64, UInt64) => cast_numeric_arrays::<Int64Type, UInt64Type>(array, cast_options),
1605 (Int64, Int8) => cast_numeric_arrays::<Int64Type, Int8Type>(array, cast_options),
1606 (Int64, Int16) => cast_numeric_arrays::<Int64Type, Int16Type>(array, cast_options),
1607 (Int64, Int32) => cast_numeric_arrays::<Int64Type, Int32Type>(array, cast_options),
1608 (Int64, Float16) => cast_numeric_arrays::<Int64Type, Float16Type>(array, cast_options),
1609 (Int64, Float32) => cast_numeric_arrays::<Int64Type, Float32Type>(array, cast_options),
1610 (Int64, Float64) => cast_numeric_arrays::<Int64Type, Float64Type>(array, cast_options),
1611
1612 (Float16, UInt8) => cast_numeric_arrays::<Float16Type, UInt8Type>(array, cast_options),
1613 (Float16, UInt16) => cast_numeric_arrays::<Float16Type, UInt16Type>(array, cast_options),
1614 (Float16, UInt32) => cast_numeric_arrays::<Float16Type, UInt32Type>(array, cast_options),
1615 (Float16, UInt64) => cast_numeric_arrays::<Float16Type, UInt64Type>(array, cast_options),
1616 (Float16, Int8) => cast_numeric_arrays::<Float16Type, Int8Type>(array, cast_options),
1617 (Float16, Int16) => cast_numeric_arrays::<Float16Type, Int16Type>(array, cast_options),
1618 (Float16, Int32) => cast_numeric_arrays::<Float16Type, Int32Type>(array, cast_options),
1619 (Float16, Int64) => cast_numeric_arrays::<Float16Type, Int64Type>(array, cast_options),
1620 (Float16, Float32) => cast_numeric_arrays::<Float16Type, Float32Type>(array, cast_options),
1621 (Float16, Float64) => cast_numeric_arrays::<Float16Type, Float64Type>(array, cast_options),
1622
1623 (Float32, UInt8) => cast_numeric_arrays::<Float32Type, UInt8Type>(array, cast_options),
1624 (Float32, UInt16) => cast_numeric_arrays::<Float32Type, UInt16Type>(array, cast_options),
1625 (Float32, UInt32) => cast_numeric_arrays::<Float32Type, UInt32Type>(array, cast_options),
1626 (Float32, UInt64) => cast_numeric_arrays::<Float32Type, UInt64Type>(array, cast_options),
1627 (Float32, Int8) => cast_numeric_arrays::<Float32Type, Int8Type>(array, cast_options),
1628 (Float32, Int16) => cast_numeric_arrays::<Float32Type, Int16Type>(array, cast_options),
1629 (Float32, Int32) => cast_numeric_arrays::<Float32Type, Int32Type>(array, cast_options),
1630 (Float32, Int64) => cast_numeric_arrays::<Float32Type, Int64Type>(array, cast_options),
1631 (Float32, Float16) => cast_numeric_arrays::<Float32Type, Float16Type>(array, cast_options),
1632 (Float32, Float64) => cast_numeric_arrays::<Float32Type, Float64Type>(array, cast_options),
1633
1634 (Float64, UInt8) => cast_numeric_arrays::<Float64Type, UInt8Type>(array, cast_options),
1635 (Float64, UInt16) => cast_numeric_arrays::<Float64Type, UInt16Type>(array, cast_options),
1636 (Float64, UInt32) => cast_numeric_arrays::<Float64Type, UInt32Type>(array, cast_options),
1637 (Float64, UInt64) => cast_numeric_arrays::<Float64Type, UInt64Type>(array, cast_options),
1638 (Float64, Int8) => cast_numeric_arrays::<Float64Type, Int8Type>(array, cast_options),
1639 (Float64, Int16) => cast_numeric_arrays::<Float64Type, Int16Type>(array, cast_options),
1640 (Float64, Int32) => cast_numeric_arrays::<Float64Type, Int32Type>(array, cast_options),
1641 (Float64, Int64) => cast_numeric_arrays::<Float64Type, Int64Type>(array, cast_options),
1642 (Float64, Float16) => cast_numeric_arrays::<Float64Type, Float16Type>(array, cast_options),
1643 (Float64, Float32) => cast_numeric_arrays::<Float64Type, Float32Type>(array, cast_options),
1644 (Int32, Date32) => cast_reinterpret_arrays::<Int32Type, Date32Type>(array),
1648 (Int32, Date64) => cast_with_options(
1649 &cast_with_options(array, &Date32, cast_options)?,
1650 &Date64,
1651 cast_options,
1652 ),
1653 (Int32, Time32(TimeUnit::Second)) => {
1654 cast_reinterpret_arrays::<Int32Type, Time32SecondType>(array)
1655 }
1656 (Int32, Time32(TimeUnit::Millisecond)) => {
1657 cast_reinterpret_arrays::<Int32Type, Time32MillisecondType>(array)
1658 }
1659 (Date32, Int32) => cast_reinterpret_arrays::<Date32Type, Int32Type>(array),
1661 (Date32, Int64) => cast_with_options(
1662 &cast_with_options(array, &Int32, cast_options)?,
1663 &Int64,
1664 cast_options,
1665 ),
1666 (Time32(TimeUnit::Second), Int32) => {
1667 cast_reinterpret_arrays::<Time32SecondType, Int32Type>(array)
1668 }
1669 (Time32(TimeUnit::Millisecond), Int32) => {
1670 cast_reinterpret_arrays::<Time32MillisecondType, Int32Type>(array)
1671 }
1672 (Time32(TimeUnit::Second), Int64) => cast_with_options(
1673 &cast_with_options(array, &Int32, cast_options)?,
1674 &Int64,
1675 cast_options,
1676 ),
1677 (Time32(TimeUnit::Millisecond), Int64) => cast_with_options(
1678 &cast_with_options(array, &Int32, cast_options)?,
1679 &Int64,
1680 cast_options,
1681 ),
1682 (Int64, Date64) => cast_reinterpret_arrays::<Int64Type, Date64Type>(array),
1683 (Int64, Date32) => cast_with_options(
1684 &cast_with_options(array, &Int32, cast_options)?,
1685 &Date32,
1686 cast_options,
1687 ),
1688 (Int64, Time64(TimeUnit::Microsecond)) => {
1690 cast_reinterpret_arrays::<Int64Type, Time64MicrosecondType>(array)
1691 }
1692 (Int64, Time64(TimeUnit::Nanosecond)) => {
1693 cast_reinterpret_arrays::<Int64Type, Time64NanosecondType>(array)
1694 }
1695
1696 (Date64, Int64) => cast_reinterpret_arrays::<Date64Type, Int64Type>(array),
1697 (Date64, Int32) => cast_with_options(
1698 &cast_with_options(array, &Int64, cast_options)?,
1699 &Int32,
1700 cast_options,
1701 ),
1702 (Time64(TimeUnit::Microsecond), Int64) => {
1703 cast_reinterpret_arrays::<Time64MicrosecondType, Int64Type>(array)
1704 }
1705 (Time64(TimeUnit::Nanosecond), Int64) => {
1706 cast_reinterpret_arrays::<Time64NanosecondType, Int64Type>(array)
1707 }
1708 (Date32, Date64) => Ok(Arc::new(
1709 array
1710 .as_primitive::<Date32Type>()
1711 .unary::<_, Date64Type>(|x| x as i64 * MILLISECONDS_IN_DAY),
1712 )),
1713 (Date64, Date32) => Ok(Arc::new(
1714 array
1715 .as_primitive::<Date64Type>()
1716 .unary::<_, Date32Type>(|x| (x / MILLISECONDS_IN_DAY) as i32),
1717 )),
1718
1719 (Time32(TimeUnit::Second), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1720 array
1721 .as_primitive::<Time32SecondType>()
1722 .unary::<_, Time32MillisecondType>(|x| x * MILLISECONDS as i32),
1723 )),
1724 (Time32(TimeUnit::Second), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1725 array
1726 .as_primitive::<Time32SecondType>()
1727 .unary::<_, Time64MicrosecondType>(|x| x as i64 * MICROSECONDS),
1728 )),
1729 (Time32(TimeUnit::Second), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1730 array
1731 .as_primitive::<Time32SecondType>()
1732 .unary::<_, Time64NanosecondType>(|x| x as i64 * NANOSECONDS),
1733 )),
1734
1735 (Time32(TimeUnit::Millisecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1736 array
1737 .as_primitive::<Time32MillisecondType>()
1738 .unary::<_, Time32SecondType>(|x| x / MILLISECONDS as i32),
1739 )),
1740 (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1741 array
1742 .as_primitive::<Time32MillisecondType>()
1743 .unary::<_, Time64MicrosecondType>(|x| x as i64 * (MICROSECONDS / MILLISECONDS)),
1744 )),
1745 (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1746 array
1747 .as_primitive::<Time32MillisecondType>()
1748 .unary::<_, Time64NanosecondType>(|x| x as i64 * (NANOSECONDS / MILLISECONDS)),
1749 )),
1750
1751 (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1752 array
1753 .as_primitive::<Time64MicrosecondType>()
1754 .unary::<_, Time32SecondType>(|x| (x / MICROSECONDS) as i32),
1755 )),
1756 (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1757 array
1758 .as_primitive::<Time64MicrosecondType>()
1759 .unary::<_, Time32MillisecondType>(|x| (x / (MICROSECONDS / MILLISECONDS)) as i32),
1760 )),
1761 (Time64(TimeUnit::Microsecond), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1762 array
1763 .as_primitive::<Time64MicrosecondType>()
1764 .unary::<_, Time64NanosecondType>(|x| x * (NANOSECONDS / MICROSECONDS)),
1765 )),
1766
1767 (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1768 array
1769 .as_primitive::<Time64NanosecondType>()
1770 .unary::<_, Time32SecondType>(|x| (x / NANOSECONDS) as i32),
1771 )),
1772 (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1773 array
1774 .as_primitive::<Time64NanosecondType>()
1775 .unary::<_, Time32MillisecondType>(|x| (x / (NANOSECONDS / MILLISECONDS)) as i32),
1776 )),
1777 (Time64(TimeUnit::Nanosecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1778 array
1779 .as_primitive::<Time64NanosecondType>()
1780 .unary::<_, Time64MicrosecondType>(|x| x / (NANOSECONDS / MICROSECONDS)),
1781 )),
1782
1783 (Timestamp(TimeUnit::Second, _), _) if to_type.is_numeric() => {
1785 let array = cast_reinterpret_arrays::<TimestampSecondType, Int64Type>(array)?;
1786 cast_with_options(&array, to_type, cast_options)
1787 }
1788 (Timestamp(TimeUnit::Millisecond, _), _) if to_type.is_numeric() => {
1789 let array = cast_reinterpret_arrays::<TimestampMillisecondType, Int64Type>(array)?;
1790 cast_with_options(&array, to_type, cast_options)
1791 }
1792 (Timestamp(TimeUnit::Microsecond, _), _) if to_type.is_numeric() => {
1793 let array = cast_reinterpret_arrays::<TimestampMicrosecondType, Int64Type>(array)?;
1794 cast_with_options(&array, to_type, cast_options)
1795 }
1796 (Timestamp(TimeUnit::Nanosecond, _), _) if to_type.is_numeric() => {
1797 let array = cast_reinterpret_arrays::<TimestampNanosecondType, Int64Type>(array)?;
1798 cast_with_options(&array, to_type, cast_options)
1799 }
1800
1801 (_, Timestamp(unit, tz)) if from_type.is_numeric() => {
1802 let array = cast_with_options(array, &Int64, cast_options)?;
1803 Ok(make_timestamp_array(
1804 array.as_primitive(),
1805 *unit,
1806 tz.clone(),
1807 ))
1808 }
1809
1810 (Timestamp(from_unit, from_tz), Timestamp(to_unit, to_tz)) => {
1811 let array = cast_with_options(array, &Int64, cast_options)?;
1812 let time_array = array.as_primitive::<Int64Type>();
1813 let from_size = time_unit_multiple(from_unit);
1814 let to_size = time_unit_multiple(to_unit);
1815 let converted = match from_size.cmp(&to_size) {
1818 Ordering::Greater => {
1819 let divisor = from_size / to_size;
1820 time_array.unary::<_, Int64Type>(|o| o / divisor)
1821 }
1822 Ordering::Equal => time_array.clone(),
1823 Ordering::Less => {
1824 let mul = to_size / from_size;
1825 if cast_options.safe {
1826 time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
1827 } else {
1828 time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
1829 }
1830 }
1831 };
1832 let adjusted = match (from_tz, to_tz) {
1834 (None, Some(to_tz)) => {
1840 let to_tz: Tz = to_tz.parse()?;
1841 match to_unit {
1842 TimeUnit::Second => adjust_timestamp_to_timezone::<TimestampSecondType>(
1843 converted,
1844 &to_tz,
1845 cast_options,
1846 )?,
1847 TimeUnit::Millisecond => adjust_timestamp_to_timezone::<
1848 TimestampMillisecondType,
1849 >(
1850 converted, &to_tz, cast_options
1851 )?,
1852 TimeUnit::Microsecond => adjust_timestamp_to_timezone::<
1853 TimestampMicrosecondType,
1854 >(
1855 converted, &to_tz, cast_options
1856 )?,
1857 TimeUnit::Nanosecond => adjust_timestamp_to_timezone::<
1858 TimestampNanosecondType,
1859 >(
1860 converted, &to_tz, cast_options
1861 )?,
1862 }
1863 }
1864 _ => converted,
1865 };
1866 Ok(make_timestamp_array(&adjusted, *to_unit, to_tz.clone()))
1867 }
1868 (Timestamp(TimeUnit::Microsecond, _), Date32) => {
1869 timestamp_to_date32(array.as_primitive::<TimestampMicrosecondType>())
1870 }
1871 (Timestamp(TimeUnit::Millisecond, _), Date32) => {
1872 timestamp_to_date32(array.as_primitive::<TimestampMillisecondType>())
1873 }
1874 (Timestamp(TimeUnit::Second, _), Date32) => {
1875 timestamp_to_date32(array.as_primitive::<TimestampSecondType>())
1876 }
1877 (Timestamp(TimeUnit::Nanosecond, _), Date32) => {
1878 timestamp_to_date32(array.as_primitive::<TimestampNanosecondType>())
1879 }
1880 (Timestamp(TimeUnit::Second, _), Date64) => Ok(Arc::new(match cast_options.safe {
1881 true => {
1882 array
1884 .as_primitive::<TimestampSecondType>()
1885 .unary_opt::<_, Date64Type>(|x| x.checked_mul(MILLISECONDS))
1886 }
1887 false => array
1888 .as_primitive::<TimestampSecondType>()
1889 .try_unary::<_, Date64Type, _>(|x| x.mul_checked(MILLISECONDS))?,
1890 })),
1891 (Timestamp(TimeUnit::Millisecond, _), Date64) => {
1892 cast_reinterpret_arrays::<TimestampMillisecondType, Date64Type>(array)
1893 }
1894 (Timestamp(TimeUnit::Microsecond, _), Date64) => Ok(Arc::new(
1895 array
1896 .as_primitive::<TimestampMicrosecondType>()
1897 .unary::<_, Date64Type>(|x| x / (MICROSECONDS / MILLISECONDS)),
1898 )),
1899 (Timestamp(TimeUnit::Nanosecond, _), Date64) => Ok(Arc::new(
1900 array
1901 .as_primitive::<TimestampNanosecondType>()
1902 .unary::<_, Date64Type>(|x| x / (NANOSECONDS / MILLISECONDS)),
1903 )),
1904 (Timestamp(TimeUnit::Second, tz), Time64(TimeUnit::Microsecond)) => {
1905 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1906 Ok(Arc::new(
1907 array
1908 .as_primitive::<TimestampSecondType>()
1909 .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1910 Ok(time_to_time64us(as_time_res_with_timezone::<
1911 TimestampSecondType,
1912 >(x, tz)?))
1913 })?,
1914 ))
1915 }
1916 (Timestamp(TimeUnit::Second, tz), Time64(TimeUnit::Nanosecond)) => {
1917 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1918 Ok(Arc::new(
1919 array
1920 .as_primitive::<TimestampSecondType>()
1921 .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1922 Ok(time_to_time64ns(as_time_res_with_timezone::<
1923 TimestampSecondType,
1924 >(x, tz)?))
1925 })?,
1926 ))
1927 }
1928 (Timestamp(TimeUnit::Millisecond, tz), Time64(TimeUnit::Microsecond)) => {
1929 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1930 Ok(Arc::new(
1931 array
1932 .as_primitive::<TimestampMillisecondType>()
1933 .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1934 Ok(time_to_time64us(as_time_res_with_timezone::<
1935 TimestampMillisecondType,
1936 >(x, tz)?))
1937 })?,
1938 ))
1939 }
1940 (Timestamp(TimeUnit::Millisecond, tz), Time64(TimeUnit::Nanosecond)) => {
1941 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1942 Ok(Arc::new(
1943 array
1944 .as_primitive::<TimestampMillisecondType>()
1945 .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1946 Ok(time_to_time64ns(as_time_res_with_timezone::<
1947 TimestampMillisecondType,
1948 >(x, tz)?))
1949 })?,
1950 ))
1951 }
1952 (Timestamp(TimeUnit::Microsecond, tz), Time64(TimeUnit::Microsecond)) => {
1953 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1954 Ok(Arc::new(
1955 array
1956 .as_primitive::<TimestampMicrosecondType>()
1957 .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1958 Ok(time_to_time64us(as_time_res_with_timezone::<
1959 TimestampMicrosecondType,
1960 >(x, tz)?))
1961 })?,
1962 ))
1963 }
1964 (Timestamp(TimeUnit::Microsecond, tz), Time64(TimeUnit::Nanosecond)) => {
1965 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1966 Ok(Arc::new(
1967 array
1968 .as_primitive::<TimestampMicrosecondType>()
1969 .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1970 Ok(time_to_time64ns(as_time_res_with_timezone::<
1971 TimestampMicrosecondType,
1972 >(x, tz)?))
1973 })?,
1974 ))
1975 }
1976 (Timestamp(TimeUnit::Nanosecond, tz), Time64(TimeUnit::Microsecond)) => {
1977 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1978 Ok(Arc::new(
1979 array
1980 .as_primitive::<TimestampNanosecondType>()
1981 .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1982 Ok(time_to_time64us(as_time_res_with_timezone::<
1983 TimestampNanosecondType,
1984 >(x, tz)?))
1985 })?,
1986 ))
1987 }
1988 (Timestamp(TimeUnit::Nanosecond, tz), Time64(TimeUnit::Nanosecond)) => {
1989 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1990 Ok(Arc::new(
1991 array
1992 .as_primitive::<TimestampNanosecondType>()
1993 .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1994 Ok(time_to_time64ns(as_time_res_with_timezone::<
1995 TimestampNanosecondType,
1996 >(x, tz)?))
1997 })?,
1998 ))
1999 }
2000 (Timestamp(TimeUnit::Second, tz), Time32(TimeUnit::Second)) => {
2001 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2002 Ok(Arc::new(
2003 array
2004 .as_primitive::<TimestampSecondType>()
2005 .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2006 Ok(time_to_time32s(as_time_res_with_timezone::<
2007 TimestampSecondType,
2008 >(x, tz)?))
2009 })?,
2010 ))
2011 }
2012 (Timestamp(TimeUnit::Second, tz), Time32(TimeUnit::Millisecond)) => {
2013 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2014 Ok(Arc::new(
2015 array
2016 .as_primitive::<TimestampSecondType>()
2017 .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2018 Ok(time_to_time32ms(as_time_res_with_timezone::<
2019 TimestampSecondType,
2020 >(x, tz)?))
2021 })?,
2022 ))
2023 }
2024 (Timestamp(TimeUnit::Millisecond, tz), Time32(TimeUnit::Second)) => {
2025 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2026 Ok(Arc::new(
2027 array
2028 .as_primitive::<TimestampMillisecondType>()
2029 .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2030 Ok(time_to_time32s(as_time_res_with_timezone::<
2031 TimestampMillisecondType,
2032 >(x, tz)?))
2033 })?,
2034 ))
2035 }
2036 (Timestamp(TimeUnit::Millisecond, tz), Time32(TimeUnit::Millisecond)) => {
2037 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2038 Ok(Arc::new(
2039 array
2040 .as_primitive::<TimestampMillisecondType>()
2041 .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2042 Ok(time_to_time32ms(as_time_res_with_timezone::<
2043 TimestampMillisecondType,
2044 >(x, tz)?))
2045 })?,
2046 ))
2047 }
2048 (Timestamp(TimeUnit::Microsecond, tz), Time32(TimeUnit::Second)) => {
2049 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2050 Ok(Arc::new(
2051 array
2052 .as_primitive::<TimestampMicrosecondType>()
2053 .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2054 Ok(time_to_time32s(as_time_res_with_timezone::<
2055 TimestampMicrosecondType,
2056 >(x, tz)?))
2057 })?,
2058 ))
2059 }
2060 (Timestamp(TimeUnit::Microsecond, tz), Time32(TimeUnit::Millisecond)) => {
2061 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2062 Ok(Arc::new(
2063 array
2064 .as_primitive::<TimestampMicrosecondType>()
2065 .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2066 Ok(time_to_time32ms(as_time_res_with_timezone::<
2067 TimestampMicrosecondType,
2068 >(x, tz)?))
2069 })?,
2070 ))
2071 }
2072 (Timestamp(TimeUnit::Nanosecond, tz), Time32(TimeUnit::Second)) => {
2073 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2074 Ok(Arc::new(
2075 array
2076 .as_primitive::<TimestampNanosecondType>()
2077 .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2078 Ok(time_to_time32s(as_time_res_with_timezone::<
2079 TimestampNanosecondType,
2080 >(x, tz)?))
2081 })?,
2082 ))
2083 }
2084 (Timestamp(TimeUnit::Nanosecond, tz), Time32(TimeUnit::Millisecond)) => {
2085 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2086 Ok(Arc::new(
2087 array
2088 .as_primitive::<TimestampNanosecondType>()
2089 .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2090 Ok(time_to_time32ms(as_time_res_with_timezone::<
2091 TimestampNanosecondType,
2092 >(x, tz)?))
2093 })?,
2094 ))
2095 }
2096 (Date64, Timestamp(TimeUnit::Second, _)) => {
2097 let array = array
2098 .as_primitive::<Date64Type>()
2099 .unary::<_, TimestampSecondType>(|x| x / MILLISECONDS);
2100
2101 cast_with_options(&array, to_type, cast_options)
2102 }
2103 (Date64, Timestamp(TimeUnit::Millisecond, _)) => {
2104 let array = array
2105 .as_primitive::<Date64Type>()
2106 .reinterpret_cast::<TimestampMillisecondType>();
2107
2108 cast_with_options(&array, to_type, cast_options)
2109 }
2110
2111 (Date64, Timestamp(TimeUnit::Microsecond, _)) => {
2112 let array = array
2113 .as_primitive::<Date64Type>()
2114 .unary::<_, TimestampMicrosecondType>(|x| x * (MICROSECONDS / MILLISECONDS));
2115
2116 cast_with_options(&array, to_type, cast_options)
2117 }
2118 (Date64, Timestamp(TimeUnit::Nanosecond, _)) => {
2119 let array = array
2120 .as_primitive::<Date64Type>()
2121 .unary::<_, TimestampNanosecondType>(|x| x * (NANOSECONDS / MILLISECONDS));
2122
2123 cast_with_options(&array, to_type, cast_options)
2124 }
2125 (Date32, Timestamp(TimeUnit::Second, _)) => {
2126 let array = array
2127 .as_primitive::<Date32Type>()
2128 .unary::<_, TimestampSecondType>(|x| (x as i64) * SECONDS_IN_DAY);
2129
2130 cast_with_options(&array, to_type, cast_options)
2131 }
2132 (Date32, Timestamp(TimeUnit::Millisecond, _)) => {
2133 let array = array
2134 .as_primitive::<Date32Type>()
2135 .unary::<_, TimestampMillisecondType>(|x| (x as i64) * MILLISECONDS_IN_DAY);
2136
2137 cast_with_options(&array, to_type, cast_options)
2138 }
2139 (Date32, Timestamp(TimeUnit::Microsecond, _)) => {
2140 let array = array
2141 .as_primitive::<Date32Type>()
2142 .unary::<_, TimestampMicrosecondType>(|x| (x as i64) * MICROSECONDS_IN_DAY);
2143
2144 cast_with_options(&array, to_type, cast_options)
2145 }
2146 (Date32, Timestamp(TimeUnit::Nanosecond, _)) => {
2147 let array = array
2148 .as_primitive::<Date32Type>()
2149 .unary::<_, TimestampNanosecondType>(|x| (x as i64) * NANOSECONDS_IN_DAY);
2150
2151 cast_with_options(&array, to_type, cast_options)
2152 }
2153
2154 (_, Duration(unit)) if from_type.is_numeric() => {
2155 let array = cast_with_options(array, &Int64, cast_options)?;
2156 Ok(make_duration_array(array.as_primitive(), *unit))
2157 }
2158 (Duration(TimeUnit::Second), _) if to_type.is_numeric() => {
2159 let array = cast_reinterpret_arrays::<DurationSecondType, Int64Type>(array)?;
2160 cast_with_options(&array, to_type, cast_options)
2161 }
2162 (Duration(TimeUnit::Millisecond), _) if to_type.is_numeric() => {
2163 let array = cast_reinterpret_arrays::<DurationMillisecondType, Int64Type>(array)?;
2164 cast_with_options(&array, to_type, cast_options)
2165 }
2166 (Duration(TimeUnit::Microsecond), _) if to_type.is_numeric() => {
2167 let array = cast_reinterpret_arrays::<DurationMicrosecondType, Int64Type>(array)?;
2168 cast_with_options(&array, to_type, cast_options)
2169 }
2170 (Duration(TimeUnit::Nanosecond), _) if to_type.is_numeric() => {
2171 let array = cast_reinterpret_arrays::<DurationNanosecondType, Int64Type>(array)?;
2172 cast_with_options(&array, to_type, cast_options)
2173 }
2174
2175 (Duration(from_unit), Duration(to_unit)) => {
2176 let array = cast_with_options(array, &Int64, cast_options)?;
2177 let time_array = array.as_primitive::<Int64Type>();
2178 let from_size = time_unit_multiple(from_unit);
2179 let to_size = time_unit_multiple(to_unit);
2180 let converted = match from_size.cmp(&to_size) {
2183 Ordering::Greater => {
2184 let divisor = from_size / to_size;
2185 time_array.unary::<_, Int64Type>(|o| o / divisor)
2186 }
2187 Ordering::Equal => time_array.clone(),
2188 Ordering::Less => {
2189 let mul = to_size / from_size;
2190 if cast_options.safe {
2191 time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
2192 } else {
2193 time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
2194 }
2195 }
2196 };
2197 Ok(make_duration_array(&converted, *to_unit))
2198 }
2199
2200 (Duration(TimeUnit::Second), Interval(IntervalUnit::MonthDayNano)) => {
2201 cast_duration_to_interval::<DurationSecondType>(array, cast_options)
2202 }
2203 (Duration(TimeUnit::Millisecond), Interval(IntervalUnit::MonthDayNano)) => {
2204 cast_duration_to_interval::<DurationMillisecondType>(array, cast_options)
2205 }
2206 (Duration(TimeUnit::Microsecond), Interval(IntervalUnit::MonthDayNano)) => {
2207 cast_duration_to_interval::<DurationMicrosecondType>(array, cast_options)
2208 }
2209 (Duration(TimeUnit::Nanosecond), Interval(IntervalUnit::MonthDayNano)) => {
2210 cast_duration_to_interval::<DurationNanosecondType>(array, cast_options)
2211 }
2212 (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Second)) => {
2213 cast_month_day_nano_to_duration::<DurationSecondType>(array, cast_options)
2214 }
2215 (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Millisecond)) => {
2216 cast_month_day_nano_to_duration::<DurationMillisecondType>(array, cast_options)
2217 }
2218 (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Microsecond)) => {
2219 cast_month_day_nano_to_duration::<DurationMicrosecondType>(array, cast_options)
2220 }
2221 (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Nanosecond)) => {
2222 cast_month_day_nano_to_duration::<DurationNanosecondType>(array, cast_options)
2223 }
2224 (Interval(IntervalUnit::YearMonth), Interval(IntervalUnit::MonthDayNano)) => {
2225 cast_interval_year_month_to_interval_month_day_nano(array, cast_options)
2226 }
2227 (Interval(IntervalUnit::DayTime), Interval(IntervalUnit::MonthDayNano)) => {
2228 cast_interval_day_time_to_interval_month_day_nano(array, cast_options)
2229 }
2230 (Int32, Interval(IntervalUnit::YearMonth)) => {
2231 cast_reinterpret_arrays::<Int32Type, IntervalYearMonthType>(array)
2232 }
2233 (_, _) => Err(ArrowError::CastError(format!(
2234 "Casting from {from_type} to {to_type} not supported",
2235 ))),
2236 }
2237}
2238
2239fn cast_struct_to_struct(
2240 array: &StructArray,
2241 from_fields: Fields,
2242 to_fields: Fields,
2243 cast_options: &CastOptions,
2244) -> Result<ArrayRef, ArrowError> {
2245 let fields_match_order = from_fields.len() == to_fields.len()
2247 && from_fields
2248 .iter()
2249 .zip(to_fields.iter())
2250 .all(|(f1, f2)| f1.name() == f2.name());
2251
2252 let fields = if fields_match_order {
2253 cast_struct_fields_in_order(array, to_fields.clone(), cast_options)?
2255 } else {
2256 let all_fields_match_by_name = to_fields.iter().all(|to_field| {
2257 from_fields
2258 .iter()
2259 .any(|from_field| from_field.name() == to_field.name())
2260 });
2261
2262 if all_fields_match_by_name {
2263 cast_struct_fields_by_name(array, from_fields.clone(), to_fields.clone(), cast_options)?
2265 } else {
2266 cast_struct_fields_in_order(array, to_fields.clone(), cast_options)?
2268 }
2269 };
2270
2271 let array = StructArray::try_new(to_fields.clone(), fields, array.nulls().cloned())?;
2272 Ok(Arc::new(array) as ArrayRef)
2273}
2274
2275fn cast_struct_fields_by_name(
2276 array: &StructArray,
2277 from_fields: Fields,
2278 to_fields: Fields,
2279 cast_options: &CastOptions,
2280) -> Result<Vec<ArrayRef>, ArrowError> {
2281 to_fields
2282 .iter()
2283 .map(|to_field| {
2284 let from_field_idx = from_fields
2285 .iter()
2286 .position(|from_field| from_field.name() == to_field.name())
2287 .unwrap(); let column = array.column(from_field_idx);
2289 cast_with_options(column, to_field.data_type(), cast_options)
2290 })
2291 .collect::<Result<Vec<ArrayRef>, ArrowError>>()
2292}
2293
2294fn cast_struct_fields_in_order(
2295 array: &StructArray,
2296 to_fields: Fields,
2297 cast_options: &CastOptions,
2298) -> Result<Vec<ArrayRef>, ArrowError> {
2299 array
2300 .columns()
2301 .iter()
2302 .zip(to_fields.iter())
2303 .map(|(l, field)| cast_with_options(l, field.data_type(), cast_options))
2304 .collect::<Result<Vec<ArrayRef>, ArrowError>>()
2305}
2306
2307fn cast_from_decimal<D, F>(
2308 array: &dyn Array,
2309 base: D::Native,
2310 scale: &i8,
2311 from_type: &DataType,
2312 to_type: &DataType,
2313 as_float: F,
2314 cast_options: &CastOptions,
2315) -> Result<ArrayRef, ArrowError>
2316where
2317 D: DecimalType + ArrowPrimitiveType,
2318 <D as ArrowPrimitiveType>::Native: ToPrimitive,
2319 F: Fn(D::Native) -> f64,
2320{
2321 use DataType::*;
2322 match to_type {
2324 UInt8 => cast_decimal_to_integer::<D, UInt8Type>(array, base, *scale, cast_options),
2325 UInt16 => cast_decimal_to_integer::<D, UInt16Type>(array, base, *scale, cast_options),
2326 UInt32 => cast_decimal_to_integer::<D, UInt32Type>(array, base, *scale, cast_options),
2327 UInt64 => cast_decimal_to_integer::<D, UInt64Type>(array, base, *scale, cast_options),
2328 Int8 => cast_decimal_to_integer::<D, Int8Type>(array, base, *scale, cast_options),
2329 Int16 => cast_decimal_to_integer::<D, Int16Type>(array, base, *scale, cast_options),
2330 Int32 => cast_decimal_to_integer::<D, Int32Type>(array, base, *scale, cast_options),
2331 Int64 => cast_decimal_to_integer::<D, Int64Type>(array, base, *scale, cast_options),
2332 Float16 => cast_decimal_to_float::<D, Float16Type, _>(array, |x| {
2333 half::f16::from_f64(single_decimal_to_float_lossy::<D, F>(
2334 &as_float,
2335 x,
2336 <i32 as From<i8>>::from(*scale),
2337 ))
2338 }),
2339 Float32 => cast_decimal_to_float::<D, Float32Type, _>(array, |x| {
2340 single_decimal_to_float_lossy::<D, F>(&as_float, x, <i32 as From<i8>>::from(*scale))
2341 as f32
2342 }),
2343 Float64 => cast_decimal_to_float::<D, Float64Type, _>(array, |x| {
2344 single_decimal_to_float_lossy::<D, F>(&as_float, x, <i32 as From<i8>>::from(*scale))
2345 }),
2346 Utf8View => value_to_string_view(array, cast_options),
2347 Utf8 => value_to_string::<i32>(array, cast_options),
2348 LargeUtf8 => value_to_string::<i64>(array, cast_options),
2349 Null => Ok(new_null_array(to_type, array.len())),
2350 _ => Err(ArrowError::CastError(format!(
2351 "Casting from {from_type} to {to_type} not supported"
2352 ))),
2353 }
2354}
2355
2356fn cast_to_decimal<D, M>(
2357 array: &dyn Array,
2358 base: M,
2359 precision: &u8,
2360 scale: &i8,
2361 from_type: &DataType,
2362 to_type: &DataType,
2363 cast_options: &CastOptions,
2364) -> Result<ArrayRef, ArrowError>
2365where
2366 D: DecimalType + ArrowPrimitiveType<Native = M>,
2367 M: ArrowNativeTypeOp + DecimalCast,
2368 u8: num_traits::AsPrimitive<M>,
2369 u16: num_traits::AsPrimitive<M>,
2370 u32: num_traits::AsPrimitive<M>,
2371 u64: num_traits::AsPrimitive<M>,
2372 i8: num_traits::AsPrimitive<M>,
2373 i16: num_traits::AsPrimitive<M>,
2374 i32: num_traits::AsPrimitive<M>,
2375 i64: num_traits::AsPrimitive<M>,
2376{
2377 use DataType::*;
2378 match from_type {
2380 UInt8 => cast_integer_to_decimal::<_, D, M>(
2381 array.as_primitive::<UInt8Type>(),
2382 *precision,
2383 *scale,
2384 base,
2385 cast_options,
2386 ),
2387 UInt16 => cast_integer_to_decimal::<_, D, _>(
2388 array.as_primitive::<UInt16Type>(),
2389 *precision,
2390 *scale,
2391 base,
2392 cast_options,
2393 ),
2394 UInt32 => cast_integer_to_decimal::<_, D, _>(
2395 array.as_primitive::<UInt32Type>(),
2396 *precision,
2397 *scale,
2398 base,
2399 cast_options,
2400 ),
2401 UInt64 => cast_integer_to_decimal::<_, D, _>(
2402 array.as_primitive::<UInt64Type>(),
2403 *precision,
2404 *scale,
2405 base,
2406 cast_options,
2407 ),
2408 Int8 => cast_integer_to_decimal::<_, D, _>(
2409 array.as_primitive::<Int8Type>(),
2410 *precision,
2411 *scale,
2412 base,
2413 cast_options,
2414 ),
2415 Int16 => cast_integer_to_decimal::<_, D, _>(
2416 array.as_primitive::<Int16Type>(),
2417 *precision,
2418 *scale,
2419 base,
2420 cast_options,
2421 ),
2422 Int32 => cast_integer_to_decimal::<_, D, _>(
2423 array.as_primitive::<Int32Type>(),
2424 *precision,
2425 *scale,
2426 base,
2427 cast_options,
2428 ),
2429 Int64 => cast_integer_to_decimal::<_, D, _>(
2430 array.as_primitive::<Int64Type>(),
2431 *precision,
2432 *scale,
2433 base,
2434 cast_options,
2435 ),
2436 Float16 => cast_floating_point_to_decimal::<_, D>(
2437 array.as_primitive::<Float16Type>(),
2438 *precision,
2439 *scale,
2440 cast_options,
2441 ),
2442 Float32 => cast_floating_point_to_decimal::<_, D>(
2443 array.as_primitive::<Float32Type>(),
2444 *precision,
2445 *scale,
2446 cast_options,
2447 ),
2448 Float64 => cast_floating_point_to_decimal::<_, D>(
2449 array.as_primitive::<Float64Type>(),
2450 *precision,
2451 *scale,
2452 cast_options,
2453 ),
2454 Utf8View | Utf8 => {
2455 cast_string_to_decimal::<D, i32>(array, *precision, *scale, cast_options)
2456 }
2457 LargeUtf8 => cast_string_to_decimal::<D, i64>(array, *precision, *scale, cast_options),
2458 Null => Ok(new_null_array(to_type, array.len())),
2459 _ => Err(ArrowError::CastError(format!(
2460 "Casting from {from_type} to {to_type} not supported"
2461 ))),
2462 }
2463}
2464
2465const fn time_unit_multiple(unit: &TimeUnit) -> i64 {
2467 match unit {
2468 TimeUnit::Second => 1,
2469 TimeUnit::Millisecond => MILLISECONDS,
2470 TimeUnit::Microsecond => MICROSECONDS,
2471 TimeUnit::Nanosecond => NANOSECONDS,
2472 }
2473}
2474
2475fn cast_numeric_arrays<FROM, TO>(
2477 from: &dyn Array,
2478 cast_options: &CastOptions,
2479) -> Result<ArrayRef, ArrowError>
2480where
2481 FROM: ArrowPrimitiveType,
2482 TO: ArrowPrimitiveType,
2483 FROM::Native: NumCast,
2484 TO::Native: NumCast,
2485{
2486 if cast_options.safe {
2487 Ok(Arc::new(numeric_cast::<FROM, TO>(
2489 from.as_primitive::<FROM>(),
2490 )))
2491 } else {
2492 Ok(Arc::new(try_numeric_cast::<FROM, TO>(
2494 from.as_primitive::<FROM>(),
2495 )?))
2496 }
2497}
2498
2499fn try_numeric_cast<T, R>(from: &PrimitiveArray<T>) -> Result<PrimitiveArray<R>, ArrowError>
2502where
2503 T: ArrowPrimitiveType,
2504 R: ArrowPrimitiveType,
2505 T::Native: NumCast,
2506 R::Native: NumCast,
2507{
2508 from.try_unary(|value| {
2509 num_cast::<T::Native, R::Native>(value).ok_or_else(|| {
2510 ArrowError::CastError(format!(
2511 "Can't cast value {:?} to type {}",
2512 value,
2513 R::DATA_TYPE
2514 ))
2515 })
2516 })
2517}
2518
2519#[inline]
2522pub fn num_cast<I, O>(value: I) -> Option<O>
2523where
2524 I: NumCast,
2525 O: NumCast,
2526{
2527 num_traits::cast::cast::<I, O>(value)
2528}
2529
2530fn numeric_cast<T, R>(from: &PrimitiveArray<T>) -> PrimitiveArray<R>
2533where
2534 T: ArrowPrimitiveType,
2535 R: ArrowPrimitiveType,
2536 T::Native: NumCast,
2537 R::Native: NumCast,
2538{
2539 from.unary_opt::<_, R>(num_cast::<T::Native, R::Native>)
2540}
2541
2542fn cast_numeric_to_binary<FROM: ArrowPrimitiveType, O: OffsetSizeTrait>(
2543 array: &dyn Array,
2544) -> Result<ArrayRef, ArrowError> {
2545 let array = array.as_primitive::<FROM>();
2546 let size = std::mem::size_of::<FROM::Native>();
2547 let offsets = OffsetBuffer::from_repeated_length(size, array.len());
2548 Ok(Arc::new(GenericBinaryArray::<O>::try_new(
2549 offsets,
2550 array.values().inner().clone(),
2551 array.nulls().cloned(),
2552 )?))
2553}
2554
2555fn adjust_timestamp_to_timezone<T: ArrowTimestampType>(
2556 array: PrimitiveArray<Int64Type>,
2557 to_tz: &Tz,
2558 cast_options: &CastOptions,
2559) -> Result<PrimitiveArray<Int64Type>, ArrowError> {
2560 let adjust = |o| {
2561 let local = as_datetime::<T>(o)?;
2562 let offset = to_tz.offset_from_local_datetime(&local).single()?;
2563 T::from_naive_datetime(local - offset.fix(), None)
2564 };
2565 let adjusted = if cast_options.safe {
2566 array.unary_opt::<_, Int64Type>(adjust)
2567 } else {
2568 array.try_unary::<_, Int64Type, _>(|o| {
2569 adjust(o).ok_or_else(|| {
2570 ArrowError::CastError("Cannot cast timezone to different timezone".to_string())
2571 })
2572 })?
2573 };
2574 Ok(adjusted)
2575}
2576
2577fn cast_numeric_to_bool<FROM>(from: &dyn Array) -> Result<ArrayRef, ArrowError>
2581where
2582 FROM: ArrowPrimitiveType,
2583{
2584 numeric_to_bool_cast::<FROM>(from.as_primitive::<FROM>()).map(|to| Arc::new(to) as ArrayRef)
2585}
2586
2587fn numeric_to_bool_cast<T>(from: &PrimitiveArray<T>) -> Result<BooleanArray, ArrowError>
2588where
2589 T: ArrowPrimitiveType + ArrowPrimitiveType,
2590{
2591 let mut b = BooleanBuilder::with_capacity(from.len());
2592
2593 for i in 0..from.len() {
2594 if from.is_null(i) {
2595 b.append_null();
2596 } else {
2597 b.append_value(cast_num_to_bool::<T::Native>(from.value(i)));
2598 }
2599 }
2600
2601 Ok(b.finish())
2602}
2603
2604#[inline]
2606pub fn cast_num_to_bool<I>(value: I) -> bool
2607where
2608 I: Default + PartialEq,
2609{
2610 value != I::default()
2611}
2612
2613fn cast_bool_to_numeric<TO>(
2617 from: &dyn Array,
2618 cast_options: &CastOptions,
2619) -> Result<ArrayRef, ArrowError>
2620where
2621 TO: ArrowPrimitiveType,
2622 TO::Native: num_traits::cast::NumCast,
2623{
2624 Ok(Arc::new(bool_to_numeric_cast::<TO>(
2625 from.as_any().downcast_ref::<BooleanArray>().unwrap(),
2626 cast_options,
2627 )))
2628}
2629
2630fn bool_to_numeric_cast<T>(from: &BooleanArray, _cast_options: &CastOptions) -> PrimitiveArray<T>
2631where
2632 T: ArrowPrimitiveType,
2633 T::Native: num_traits::NumCast,
2634{
2635 let iter = (0..from.len()).map(|i| {
2636 if from.is_null(i) {
2637 None
2638 } else {
2639 single_bool_to_numeric::<T::Native>(from.value(i))
2640 }
2641 });
2642 unsafe { PrimitiveArray::<T>::from_trusted_len_iter(iter) }
2647}
2648
2649#[inline]
2651pub fn single_bool_to_numeric<O>(value: bool) -> Option<O>
2652where
2653 O: num_traits::NumCast + Default,
2654{
2655 if value {
2656 num_traits::cast::cast(1)
2658 } else {
2659 Some(O::default())
2660 }
2661}
2662
2663fn cast_binary_to_fixed_size_binary<O: OffsetSizeTrait>(
2665 array: &dyn Array,
2666 byte_width: i32,
2667 cast_options: &CastOptions,
2668) -> Result<ArrayRef, ArrowError> {
2669 let array = array.as_binary::<O>();
2670 let mut builder = FixedSizeBinaryBuilder::with_capacity(array.len(), byte_width);
2671
2672 for i in 0..array.len() {
2673 if array.is_null(i) {
2674 builder.append_null();
2675 } else {
2676 match builder.append_value(array.value(i)) {
2677 Ok(_) => {}
2678 Err(e) => match cast_options.safe {
2679 true => builder.append_null(),
2680 false => return Err(e),
2681 },
2682 }
2683 }
2684 }
2685
2686 Ok(Arc::new(builder.finish()))
2687}
2688
2689fn cast_fixed_size_binary_to_binary<O: OffsetSizeTrait>(
2692 array: &dyn Array,
2693 byte_width: i32,
2694) -> Result<ArrayRef, ArrowError> {
2695 let array = array
2696 .as_any()
2697 .downcast_ref::<FixedSizeBinaryArray>()
2698 .unwrap();
2699
2700 let offsets: i128 = byte_width as i128 * array.len() as i128;
2701
2702 let is_binary = matches!(GenericBinaryType::<O>::DATA_TYPE, DataType::Binary);
2703 if is_binary && offsets > i32::MAX as i128 {
2704 return Err(ArrowError::ComputeError(
2705 "FixedSizeBinary array too large to cast to Binary array".to_string(),
2706 ));
2707 } else if !is_binary && offsets > i64::MAX as i128 {
2708 return Err(ArrowError::ComputeError(
2709 "FixedSizeBinary array too large to cast to LargeBinary array".to_string(),
2710 ));
2711 }
2712
2713 let mut builder = GenericBinaryBuilder::<O>::with_capacity(array.len(), array.len());
2714
2715 for i in 0..array.len() {
2716 if array.is_null(i) {
2717 builder.append_null();
2718 } else {
2719 builder.append_value(array.value(i));
2720 }
2721 }
2722
2723 Ok(Arc::new(builder.finish()))
2724}
2725
2726fn cast_fixed_size_binary_to_binary_view(
2727 array: &dyn Array,
2728 _byte_width: i32,
2729) -> Result<ArrayRef, ArrowError> {
2730 let array = array
2731 .as_any()
2732 .downcast_ref::<FixedSizeBinaryArray>()
2733 .unwrap();
2734
2735 let mut builder = BinaryViewBuilder::with_capacity(array.len());
2736 for i in 0..array.len() {
2737 if array.is_null(i) {
2738 builder.append_null();
2739 } else {
2740 builder.append_value(array.value(i));
2741 }
2742 }
2743
2744 Ok(Arc::new(builder.finish()))
2745}
2746
2747fn cast_byte_container<FROM, TO>(array: &dyn Array) -> Result<ArrayRef, ArrowError>
2750where
2751 FROM: ByteArrayType,
2752 TO: ByteArrayType<Native = FROM::Native>,
2753 FROM::Offset: OffsetSizeTrait + ToPrimitive,
2754 TO::Offset: OffsetSizeTrait + NumCast,
2755{
2756 let data = array.to_data();
2757 assert_eq!(data.data_type(), &FROM::DATA_TYPE);
2758 let str_values_buf = data.buffers()[1].clone();
2759 let offsets = data.buffers()[0].typed_data::<FROM::Offset>();
2760
2761 let mut offset_builder = BufferBuilder::<TO::Offset>::new(offsets.len());
2762 offsets
2763 .iter()
2764 .try_for_each::<_, Result<_, ArrowError>>(|offset| {
2765 let offset =
2766 <<TO as ByteArrayType>::Offset as NumCast>::from(*offset).ok_or_else(|| {
2767 ArrowError::ComputeError(format!(
2768 "{}{} array too large to cast to {}{} array",
2769 FROM::Offset::PREFIX,
2770 FROM::PREFIX,
2771 TO::Offset::PREFIX,
2772 TO::PREFIX
2773 ))
2774 })?;
2775 offset_builder.append(offset);
2776 Ok(())
2777 })?;
2778
2779 let offset_buffer = offset_builder.finish();
2780
2781 let dtype = TO::DATA_TYPE;
2782
2783 let builder = ArrayData::builder(dtype)
2784 .offset(array.offset())
2785 .len(array.len())
2786 .add_buffer(offset_buffer)
2787 .add_buffer(str_values_buf)
2788 .nulls(data.nulls().cloned());
2789
2790 let array_data = unsafe { builder.build_unchecked() };
2791
2792 Ok(Arc::new(GenericByteArray::<TO>::from(array_data)))
2793}
2794
2795fn cast_view_to_byte<FROM, TO>(array: &dyn Array) -> Result<ArrayRef, ArrowError>
2797where
2798 FROM: ByteViewType,
2799 TO: ByteArrayType,
2800 FROM::Native: AsRef<TO::Native>,
2801{
2802 let data = array.to_data();
2803 let view_array = GenericByteViewArray::<FROM>::from(data);
2804
2805 let len = view_array.len();
2806 let bytes = view_array
2807 .views()
2808 .iter()
2809 .map(|v| ByteView::from(*v).length as usize)
2810 .sum::<usize>();
2811
2812 let mut byte_array_builder = GenericByteBuilder::<TO>::with_capacity(len, bytes);
2813
2814 for val in view_array.iter() {
2815 byte_array_builder.append_option(val);
2816 }
2817
2818 Ok(Arc::new(byte_array_builder.finish()))
2819}
2820
2821#[cfg(test)]
2822mod tests {
2823 use super::*;
2824 use DataType::*;
2825 use arrow_array::{Int64Array, RunArray, StringArray};
2826 use arrow_buffer::{Buffer, IntervalDayTime, NullBuffer};
2827 use arrow_buffer::{ScalarBuffer, i256};
2828 use arrow_schema::{DataType, Field};
2829 use chrono::NaiveDate;
2830 use half::f16;
2831 use std::sync::Arc;
2832
2833 #[derive(Clone)]
2834 struct DecimalCastTestConfig {
2835 input_prec: u8,
2836 input_scale: i8,
2837 input_repr: i128,
2838 output_prec: u8,
2839 output_scale: i8,
2840 expected_output_repr: Result<i128, String>, }
2845
2846 macro_rules! generate_cast_test_case {
2847 ($INPUT_ARRAY: expr, $OUTPUT_TYPE_ARRAY: ident, $OUTPUT_TYPE: expr, $OUTPUT_VALUES: expr) => {
2848 let output =
2849 $OUTPUT_TYPE_ARRAY::from($OUTPUT_VALUES).with_data_type($OUTPUT_TYPE.clone());
2850
2851 let input_array_type = $INPUT_ARRAY.data_type();
2853 assert!(can_cast_types(input_array_type, $OUTPUT_TYPE));
2854 let result = cast($INPUT_ARRAY, $OUTPUT_TYPE).unwrap();
2855 assert_eq!($OUTPUT_TYPE, result.data_type());
2856 assert_eq!(result.as_ref(), &output);
2857
2858 let cast_option = CastOptions {
2859 safe: false,
2860 format_options: FormatOptions::default(),
2861 };
2862 let result = cast_with_options($INPUT_ARRAY, $OUTPUT_TYPE, &cast_option).unwrap();
2863 assert_eq!($OUTPUT_TYPE, result.data_type());
2864 assert_eq!(result.as_ref(), &output);
2865 };
2866 }
2867
2868 fn run_decimal_cast_test_case<I, O>(t: DecimalCastTestConfig)
2869 where
2870 I: DecimalType,
2871 O: DecimalType,
2872 I::Native: DecimalCast,
2873 O::Native: DecimalCast,
2874 {
2875 let array = vec![I::Native::from_decimal(t.input_repr)];
2876 let array = array
2877 .into_iter()
2878 .collect::<PrimitiveArray<I>>()
2879 .with_precision_and_scale(t.input_prec, t.input_scale)
2880 .unwrap();
2881 let input_type = array.data_type();
2882 let output_type = O::TYPE_CONSTRUCTOR(t.output_prec, t.output_scale);
2883 assert!(can_cast_types(input_type, &output_type));
2884
2885 let options = CastOptions {
2886 safe: false,
2887 ..Default::default()
2888 };
2889 let result = cast_with_options(&array, &output_type, &options);
2890
2891 match t.expected_output_repr {
2892 Ok(v) => {
2893 let expected_array = vec![O::Native::from_decimal(v)];
2894 let expected_array = expected_array
2895 .into_iter()
2896 .collect::<PrimitiveArray<O>>()
2897 .with_precision_and_scale(t.output_prec, t.output_scale)
2898 .unwrap();
2899 assert_eq!(*result.unwrap(), expected_array);
2900 }
2901 Err(expected_output_message_template) => {
2902 assert!(result.is_err());
2903 let expected_error_message =
2904 expected_output_message_template.replace("{}", O::PREFIX);
2905 assert_eq!(result.unwrap_err().to_string(), expected_error_message);
2906 }
2907 }
2908 }
2909
2910 fn create_decimal32_array(
2911 array: Vec<Option<i32>>,
2912 precision: u8,
2913 scale: i8,
2914 ) -> Result<Decimal32Array, ArrowError> {
2915 array
2916 .into_iter()
2917 .collect::<Decimal32Array>()
2918 .with_precision_and_scale(precision, scale)
2919 }
2920
2921 fn create_decimal64_array(
2922 array: Vec<Option<i64>>,
2923 precision: u8,
2924 scale: i8,
2925 ) -> Result<Decimal64Array, ArrowError> {
2926 array
2927 .into_iter()
2928 .collect::<Decimal64Array>()
2929 .with_precision_and_scale(precision, scale)
2930 }
2931
2932 fn create_decimal128_array(
2933 array: Vec<Option<i128>>,
2934 precision: u8,
2935 scale: i8,
2936 ) -> Result<Decimal128Array, ArrowError> {
2937 array
2938 .into_iter()
2939 .collect::<Decimal128Array>()
2940 .with_precision_and_scale(precision, scale)
2941 }
2942
2943 fn create_decimal256_array(
2944 array: Vec<Option<i256>>,
2945 precision: u8,
2946 scale: i8,
2947 ) -> Result<Decimal256Array, ArrowError> {
2948 array
2949 .into_iter()
2950 .collect::<Decimal256Array>()
2951 .with_precision_and_scale(precision, scale)
2952 }
2953
2954 #[test]
2955 #[cfg(not(feature = "force_validate"))]
2956 #[should_panic(
2957 expected = "Cannot cast to Decimal128(20, 3). Overflowing on 57896044618658097711785492504343953926634992332820282019728792003956564819967"
2958 )]
2959 fn test_cast_decimal_to_decimal_round_with_error() {
2960 let array = vec![
2962 Some(i256::from_i128(1123454)),
2963 Some(i256::from_i128(2123456)),
2964 Some(i256::from_i128(-3123453)),
2965 Some(i256::from_i128(-3123456)),
2966 None,
2967 Some(i256::MAX),
2968 Some(i256::MIN),
2969 ];
2970 let input_decimal_array = create_decimal256_array(array, 76, 4).unwrap();
2971 let array = Arc::new(input_decimal_array) as ArrayRef;
2972 let input_type = DataType::Decimal256(76, 4);
2973 let output_type = DataType::Decimal128(20, 3);
2974 assert!(can_cast_types(&input_type, &output_type));
2975 generate_cast_test_case!(
2976 &array,
2977 Decimal128Array,
2978 &output_type,
2979 vec![
2980 Some(112345_i128),
2981 Some(212346_i128),
2982 Some(-312345_i128),
2983 Some(-312346_i128),
2984 None,
2985 None,
2986 None,
2987 ]
2988 );
2989 }
2990
2991 #[test]
2992 #[cfg(not(feature = "force_validate"))]
2993 fn test_cast_decimal_to_decimal_round() {
2994 let array = vec![
2995 Some(1123454),
2996 Some(2123456),
2997 Some(-3123453),
2998 Some(-3123456),
2999 None,
3000 ];
3001 let array = create_decimal128_array(array, 20, 4).unwrap();
3002 let input_type = DataType::Decimal128(20, 4);
3004 let output_type = DataType::Decimal128(20, 3);
3005 assert!(can_cast_types(&input_type, &output_type));
3006 generate_cast_test_case!(
3007 &array,
3008 Decimal128Array,
3009 &output_type,
3010 vec![
3011 Some(112345_i128),
3012 Some(212346_i128),
3013 Some(-312345_i128),
3014 Some(-312346_i128),
3015 None
3016 ]
3017 );
3018
3019 let input_type = DataType::Decimal128(20, 4);
3021 let output_type = DataType::Decimal256(20, 3);
3022 assert!(can_cast_types(&input_type, &output_type));
3023 generate_cast_test_case!(
3024 &array,
3025 Decimal256Array,
3026 &output_type,
3027 vec![
3028 Some(i256::from_i128(112345_i128)),
3029 Some(i256::from_i128(212346_i128)),
3030 Some(i256::from_i128(-312345_i128)),
3031 Some(i256::from_i128(-312346_i128)),
3032 None
3033 ]
3034 );
3035
3036 let array = vec![
3038 Some(i256::from_i128(1123454)),
3039 Some(i256::from_i128(2123456)),
3040 Some(i256::from_i128(-3123453)),
3041 Some(i256::from_i128(-3123456)),
3042 None,
3043 ];
3044 let array = create_decimal256_array(array, 20, 4).unwrap();
3045
3046 let input_type = DataType::Decimal256(20, 4);
3048 let output_type = DataType::Decimal256(20, 3);
3049 assert!(can_cast_types(&input_type, &output_type));
3050 generate_cast_test_case!(
3051 &array,
3052 Decimal256Array,
3053 &output_type,
3054 vec![
3055 Some(i256::from_i128(112345_i128)),
3056 Some(i256::from_i128(212346_i128)),
3057 Some(i256::from_i128(-312345_i128)),
3058 Some(i256::from_i128(-312346_i128)),
3059 None
3060 ]
3061 );
3062 let input_type = DataType::Decimal256(20, 4);
3064 let output_type = DataType::Decimal128(20, 3);
3065 assert!(can_cast_types(&input_type, &output_type));
3066 generate_cast_test_case!(
3067 &array,
3068 Decimal128Array,
3069 &output_type,
3070 vec![
3071 Some(112345_i128),
3072 Some(212346_i128),
3073 Some(-312345_i128),
3074 Some(-312346_i128),
3075 None
3076 ]
3077 );
3078 }
3079
3080 #[test]
3081 fn test_cast_decimal32_to_decimal32() {
3082 let input_type = DataType::Decimal32(9, 3);
3084 let output_type = DataType::Decimal32(9, 4);
3085 assert!(can_cast_types(&input_type, &output_type));
3086 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3087 let array = create_decimal32_array(array, 9, 3).unwrap();
3088 generate_cast_test_case!(
3089 &array,
3090 Decimal32Array,
3091 &output_type,
3092 vec![
3093 Some(11234560_i32),
3094 Some(21234560_i32),
3095 Some(31234560_i32),
3096 None
3097 ]
3098 );
3099 let array = vec![Some(123456), None];
3101 let array = create_decimal32_array(array, 9, 0).unwrap();
3102 let result_safe = cast(&array, &DataType::Decimal32(2, 2));
3103 assert!(result_safe.is_ok());
3104 let options = CastOptions {
3105 safe: false,
3106 ..Default::default()
3107 };
3108
3109 let result_unsafe = cast_with_options(&array, &DataType::Decimal32(2, 2), &options);
3110 assert_eq!(
3111 "Invalid argument error: 123456.00 is too large to store in a Decimal32 of precision 2. Max is 0.99",
3112 result_unsafe.unwrap_err().to_string()
3113 );
3114 }
3115
3116 #[test]
3117 fn test_cast_decimal64_to_decimal64() {
3118 let input_type = DataType::Decimal64(17, 3);
3120 let output_type = DataType::Decimal64(17, 4);
3121 assert!(can_cast_types(&input_type, &output_type));
3122 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3123 let array = create_decimal64_array(array, 17, 3).unwrap();
3124 generate_cast_test_case!(
3125 &array,
3126 Decimal64Array,
3127 &output_type,
3128 vec![
3129 Some(11234560_i64),
3130 Some(21234560_i64),
3131 Some(31234560_i64),
3132 None
3133 ]
3134 );
3135 let array = vec![Some(123456), None];
3137 let array = create_decimal64_array(array, 9, 0).unwrap();
3138 let result_safe = cast(&array, &DataType::Decimal64(2, 2));
3139 assert!(result_safe.is_ok());
3140 let options = CastOptions {
3141 safe: false,
3142 ..Default::default()
3143 };
3144
3145 let result_unsafe = cast_with_options(&array, &DataType::Decimal64(2, 2), &options);
3146 assert_eq!(
3147 "Invalid argument error: 123456.00 is too large to store in a Decimal64 of precision 2. Max is 0.99",
3148 result_unsafe.unwrap_err().to_string()
3149 );
3150 }
3151
3152 #[test]
3153 fn test_cast_decimal128_to_decimal128() {
3154 let input_type = DataType::Decimal128(20, 3);
3156 let output_type = DataType::Decimal128(20, 4);
3157 assert!(can_cast_types(&input_type, &output_type));
3158 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3159 let array = create_decimal128_array(array, 20, 3).unwrap();
3160 generate_cast_test_case!(
3161 &array,
3162 Decimal128Array,
3163 &output_type,
3164 vec![
3165 Some(11234560_i128),
3166 Some(21234560_i128),
3167 Some(31234560_i128),
3168 None
3169 ]
3170 );
3171 let array = vec![Some(123456), None];
3173 let array = create_decimal128_array(array, 10, 0).unwrap();
3174 let result_safe = cast(&array, &DataType::Decimal128(2, 2));
3175 assert!(result_safe.is_ok());
3176 let options = CastOptions {
3177 safe: false,
3178 ..Default::default()
3179 };
3180
3181 let result_unsafe = cast_with_options(&array, &DataType::Decimal128(2, 2), &options);
3182 assert_eq!(
3183 "Invalid argument error: 123456.00 is too large to store in a Decimal128 of precision 2. Max is 0.99",
3184 result_unsafe.unwrap_err().to_string()
3185 );
3186 }
3187
3188 #[test]
3189 fn test_cast_decimal32_to_decimal32_dict() {
3190 let p = 9;
3191 let s = 3;
3192 let input_type = DataType::Decimal32(p, s);
3193 let output_type = DataType::Dictionary(
3194 Box::new(DataType::Int32),
3195 Box::new(DataType::Decimal32(p, s)),
3196 );
3197 assert!(can_cast_types(&input_type, &output_type));
3198 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3199 let array = create_decimal32_array(array, p, s).unwrap();
3200 let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3201 assert_eq!(cast_array.data_type(), &output_type);
3202 }
3203
3204 #[test]
3205 fn test_cast_decimal64_to_decimal64_dict() {
3206 let p = 15;
3207 let s = 3;
3208 let input_type = DataType::Decimal64(p, s);
3209 let output_type = DataType::Dictionary(
3210 Box::new(DataType::Int32),
3211 Box::new(DataType::Decimal64(p, s)),
3212 );
3213 assert!(can_cast_types(&input_type, &output_type));
3214 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3215 let array = create_decimal64_array(array, p, s).unwrap();
3216 let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3217 assert_eq!(cast_array.data_type(), &output_type);
3218 }
3219
3220 #[test]
3221 fn test_cast_decimal128_to_decimal128_dict() {
3222 let p = 20;
3223 let s = 3;
3224 let input_type = DataType::Decimal128(p, s);
3225 let output_type = DataType::Dictionary(
3226 Box::new(DataType::Int32),
3227 Box::new(DataType::Decimal128(p, s)),
3228 );
3229 assert!(can_cast_types(&input_type, &output_type));
3230 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3231 let array = create_decimal128_array(array, p, s).unwrap();
3232 let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3233 assert_eq!(cast_array.data_type(), &output_type);
3234 }
3235
3236 #[test]
3237 fn test_cast_decimal256_to_decimal256_dict() {
3238 let p = 20;
3239 let s = 3;
3240 let input_type = DataType::Decimal256(p, s);
3241 let output_type = DataType::Dictionary(
3242 Box::new(DataType::Int32),
3243 Box::new(DataType::Decimal256(p, s)),
3244 );
3245 assert!(can_cast_types(&input_type, &output_type));
3246 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3247 let array = create_decimal128_array(array, p, s).unwrap();
3248 let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3249 assert_eq!(cast_array.data_type(), &output_type);
3250 }
3251
3252 #[test]
3253 fn test_cast_decimal32_to_decimal32_overflow() {
3254 let input_type = DataType::Decimal32(9, 3);
3255 let output_type = DataType::Decimal32(9, 9);
3256 assert!(can_cast_types(&input_type, &output_type));
3257
3258 let array = vec![Some(i32::MAX)];
3259 let array = create_decimal32_array(array, 9, 3).unwrap();
3260 let result = cast_with_options(
3261 &array,
3262 &output_type,
3263 &CastOptions {
3264 safe: false,
3265 format_options: FormatOptions::default(),
3266 },
3267 );
3268 assert_eq!(
3269 "Cast error: Cannot cast to Decimal32(9, 9). Overflowing on 2147483647",
3270 result.unwrap_err().to_string()
3271 );
3272 }
3273
3274 #[test]
3275 fn test_cast_decimal32_to_decimal32_large_scale_reduction() {
3276 let array = vec![Some(-999999999), Some(0), Some(999999999), None];
3277 let array = create_decimal32_array(array, 9, 3).unwrap();
3278
3279 let output_type = DataType::Decimal32(9, -6);
3281 assert!(can_cast_types(array.data_type(), &output_type));
3282 generate_cast_test_case!(
3283 &array,
3284 Decimal32Array,
3285 &output_type,
3286 vec![Some(-1), Some(0), Some(1), None]
3287 );
3288
3289 let output_type = DataType::Decimal32(9, -7);
3291 assert!(can_cast_types(array.data_type(), &output_type));
3292 generate_cast_test_case!(
3293 &array,
3294 Decimal32Array,
3295 &output_type,
3296 vec![Some(0), Some(0), Some(0), None]
3297 );
3298 }
3299
3300 #[test]
3301 fn test_cast_decimal64_to_decimal64_overflow() {
3302 let input_type = DataType::Decimal64(18, 3);
3303 let output_type = DataType::Decimal64(18, 18);
3304 assert!(can_cast_types(&input_type, &output_type));
3305
3306 let array = vec![Some(i64::MAX)];
3307 let array = create_decimal64_array(array, 18, 3).unwrap();
3308 let result = cast_with_options(
3309 &array,
3310 &output_type,
3311 &CastOptions {
3312 safe: false,
3313 format_options: FormatOptions::default(),
3314 },
3315 );
3316 assert_eq!(
3317 "Cast error: Cannot cast to Decimal64(18, 18). Overflowing on 9223372036854775807",
3318 result.unwrap_err().to_string()
3319 );
3320 }
3321
3322 #[test]
3323 fn test_cast_decimal64_to_decimal64_large_scale_reduction() {
3324 let array = vec![
3325 Some(-999999999999999999),
3326 Some(0),
3327 Some(999999999999999999),
3328 None,
3329 ];
3330 let array = create_decimal64_array(array, 18, 3).unwrap();
3331
3332 let output_type = DataType::Decimal64(18, -15);
3334 assert!(can_cast_types(array.data_type(), &output_type));
3335 generate_cast_test_case!(
3336 &array,
3337 Decimal64Array,
3338 &output_type,
3339 vec![Some(-1), Some(0), Some(1), None]
3340 );
3341
3342 let output_type = DataType::Decimal64(18, -16);
3344 assert!(can_cast_types(array.data_type(), &output_type));
3345 generate_cast_test_case!(
3346 &array,
3347 Decimal64Array,
3348 &output_type,
3349 vec![Some(0), Some(0), Some(0), None]
3350 );
3351 }
3352
3353 #[test]
3354 fn test_cast_floating_to_decimals() {
3355 for output_type in [
3356 DataType::Decimal32(9, 3),
3357 DataType::Decimal64(9, 3),
3358 DataType::Decimal128(9, 3),
3359 DataType::Decimal256(9, 3),
3360 ] {
3361 let input_type = DataType::Float64;
3362 assert!(can_cast_types(&input_type, &output_type));
3363
3364 let array = vec![Some(1.1_f64)];
3365 let array = PrimitiveArray::<Float64Type>::from_iter(array);
3366 let result = cast_with_options(
3367 &array,
3368 &output_type,
3369 &CastOptions {
3370 safe: false,
3371 format_options: FormatOptions::default(),
3372 },
3373 );
3374 assert!(
3375 result.is_ok(),
3376 "Failed to cast to {output_type} with: {}",
3377 result.unwrap_err()
3378 );
3379 }
3380 }
3381
3382 #[test]
3383 fn test_cast_float16_to_decimals() {
3384 let array = Float16Array::from(vec![
3385 Some(f16::from_f32(1.25)),
3386 Some(f16::from_f32(-2.5)),
3387 Some(f16::from_f32(1.125)),
3388 Some(f16::from_f32(-1.125)),
3389 Some(f16::from_f32(0.0)),
3390 None,
3391 ]);
3392
3393 generate_cast_test_case!(
3394 &array,
3395 Decimal32Array,
3396 &DataType::Decimal32(9, 2),
3397 vec![
3398 Some(125_i32),
3399 Some(-250_i32),
3400 Some(113_i32),
3401 Some(-113_i32),
3402 Some(0_i32),
3403 None
3404 ]
3405 );
3406 generate_cast_test_case!(
3407 &array,
3408 Decimal64Array,
3409 &DataType::Decimal64(18, 2),
3410 vec![
3411 Some(125_i64),
3412 Some(-250_i64),
3413 Some(113_i64),
3414 Some(-113_i64),
3415 Some(0_i64),
3416 None
3417 ]
3418 );
3419 generate_cast_test_case!(
3420 &array,
3421 Decimal128Array,
3422 &DataType::Decimal128(38, 2),
3423 vec![
3424 Some(125_i128),
3425 Some(-250_i128),
3426 Some(113_i128),
3427 Some(-113_i128),
3428 Some(0_i128),
3429 None
3430 ]
3431 );
3432 generate_cast_test_case!(
3433 &array,
3434 Decimal256Array,
3435 &DataType::Decimal256(76, 2),
3436 vec![
3437 Some(i256::from_i128(125_i128)),
3438 Some(i256::from_i128(-250_i128)),
3439 Some(i256::from_i128(113_i128)),
3440 Some(i256::from_i128(-113_i128)),
3441 Some(i256::from_i128(0_i128)),
3442 None
3443 ]
3444 );
3445
3446 let array = Float16Array::from(vec![
3447 Some(f16::from_f32(1250.0)),
3448 Some(f16::from_f32(-1250.0)),
3449 Some(f16::from_f32(1249.0)),
3450 None,
3451 ]);
3452 generate_cast_test_case!(
3453 &array,
3454 Decimal128Array,
3455 &DataType::Decimal128(5, -2),
3456 vec![Some(13_i128), Some(-13_i128), Some(12_i128), None]
3457 );
3458 }
3459
3460 #[test]
3461 fn test_cast_decimal128_to_decimal128_overflow() {
3462 let input_type = DataType::Decimal128(38, 3);
3463 let output_type = DataType::Decimal128(38, 38);
3464 assert!(can_cast_types(&input_type, &output_type));
3465
3466 let array = vec![Some(i128::MAX)];
3467 let array = create_decimal128_array(array, 38, 3).unwrap();
3468 let result = cast_with_options(
3469 &array,
3470 &output_type,
3471 &CastOptions {
3472 safe: false,
3473 format_options: FormatOptions::default(),
3474 },
3475 );
3476 assert_eq!(
3477 "Cast error: Cannot cast to Decimal128(38, 38). Overflowing on 170141183460469231731687303715884105727",
3478 result.unwrap_err().to_string()
3479 );
3480 }
3481
3482 #[test]
3483 fn test_cast_decimal128_to_decimal256_overflow() {
3484 let input_type = DataType::Decimal128(38, 3);
3485 let output_type = DataType::Decimal256(76, 76);
3486 assert!(can_cast_types(&input_type, &output_type));
3487
3488 let array = vec![Some(i128::MAX)];
3489 let array = create_decimal128_array(array, 38, 3).unwrap();
3490 let result = cast_with_options(
3491 &array,
3492 &output_type,
3493 &CastOptions {
3494 safe: false,
3495 format_options: FormatOptions::default(),
3496 },
3497 );
3498 assert_eq!(
3499 "Cast error: Cannot cast to Decimal256(76, 76). Overflowing on 170141183460469231731687303715884105727",
3500 result.unwrap_err().to_string()
3501 );
3502 }
3503
3504 #[test]
3505 fn test_cast_decimal32_to_decimal256() {
3506 let input_type = DataType::Decimal32(8, 3);
3507 let output_type = DataType::Decimal256(20, 4);
3508 assert!(can_cast_types(&input_type, &output_type));
3509 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3510 let array = create_decimal32_array(array, 8, 3).unwrap();
3511 generate_cast_test_case!(
3512 &array,
3513 Decimal256Array,
3514 &output_type,
3515 vec![
3516 Some(i256::from_i128(11234560_i128)),
3517 Some(i256::from_i128(21234560_i128)),
3518 Some(i256::from_i128(31234560_i128)),
3519 None
3520 ]
3521 );
3522 }
3523 #[test]
3524 fn test_cast_decimal64_to_decimal256() {
3525 let input_type = DataType::Decimal64(12, 3);
3526 let output_type = DataType::Decimal256(20, 4);
3527 assert!(can_cast_types(&input_type, &output_type));
3528 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3529 let array = create_decimal64_array(array, 12, 3).unwrap();
3530 generate_cast_test_case!(
3531 &array,
3532 Decimal256Array,
3533 &output_type,
3534 vec![
3535 Some(i256::from_i128(11234560_i128)),
3536 Some(i256::from_i128(21234560_i128)),
3537 Some(i256::from_i128(31234560_i128)),
3538 None
3539 ]
3540 );
3541 }
3542 #[test]
3543 fn test_cast_decimal128_to_decimal256() {
3544 let input_type = DataType::Decimal128(20, 3);
3545 let output_type = DataType::Decimal256(20, 4);
3546 assert!(can_cast_types(&input_type, &output_type));
3547 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3548 let array = create_decimal128_array(array, 20, 3).unwrap();
3549 generate_cast_test_case!(
3550 &array,
3551 Decimal256Array,
3552 &output_type,
3553 vec![
3554 Some(i256::from_i128(11234560_i128)),
3555 Some(i256::from_i128(21234560_i128)),
3556 Some(i256::from_i128(31234560_i128)),
3557 None
3558 ]
3559 );
3560 }
3561
3562 #[test]
3563 fn test_cast_decimal256_to_decimal128_overflow() {
3564 let input_type = DataType::Decimal256(76, 5);
3565 let output_type = DataType::Decimal128(38, 7);
3566 assert!(can_cast_types(&input_type, &output_type));
3567 let array = vec![Some(i256::from_i128(i128::MAX))];
3568 let array = create_decimal256_array(array, 76, 5).unwrap();
3569 let result = cast_with_options(
3570 &array,
3571 &output_type,
3572 &CastOptions {
3573 safe: false,
3574 format_options: FormatOptions::default(),
3575 },
3576 );
3577 assert_eq!(
3578 "Cast error: Cannot cast to Decimal128(38, 7). Overflowing on 170141183460469231731687303715884105727",
3579 result.unwrap_err().to_string()
3580 );
3581 }
3582
3583 #[test]
3584 fn test_cast_decimal256_to_decimal256_overflow() {
3585 let input_type = DataType::Decimal256(76, 5);
3586 let output_type = DataType::Decimal256(76, 55);
3587 assert!(can_cast_types(&input_type, &output_type));
3588 let array = vec![Some(i256::from_i128(i128::MAX))];
3589 let array = create_decimal256_array(array, 76, 5).unwrap();
3590 let result = cast_with_options(
3591 &array,
3592 &output_type,
3593 &CastOptions {
3594 safe: false,
3595 format_options: FormatOptions::default(),
3596 },
3597 );
3598 assert_eq!(
3599 "Cast error: Cannot cast to Decimal256(76, 55). Overflowing on 170141183460469231731687303715884105727",
3600 result.unwrap_err().to_string()
3601 );
3602 }
3603
3604 #[test]
3605 fn test_cast_decimal256_to_decimal128() {
3606 let input_type = DataType::Decimal256(20, 3);
3607 let output_type = DataType::Decimal128(20, 4);
3608 assert!(can_cast_types(&input_type, &output_type));
3609 let array = vec![
3610 Some(i256::from_i128(1123456)),
3611 Some(i256::from_i128(2123456)),
3612 Some(i256::from_i128(3123456)),
3613 None,
3614 ];
3615 let array = create_decimal256_array(array, 20, 3).unwrap();
3616 generate_cast_test_case!(
3617 &array,
3618 Decimal128Array,
3619 &output_type,
3620 vec![
3621 Some(11234560_i128),
3622 Some(21234560_i128),
3623 Some(31234560_i128),
3624 None
3625 ]
3626 );
3627 }
3628
3629 #[test]
3630 fn test_cast_decimal256_to_decimal256() {
3631 let input_type = DataType::Decimal256(20, 3);
3632 let output_type = DataType::Decimal256(20, 4);
3633 assert!(can_cast_types(&input_type, &output_type));
3634 let array = vec![
3635 Some(i256::from_i128(1123456)),
3636 Some(i256::from_i128(2123456)),
3637 Some(i256::from_i128(3123456)),
3638 None,
3639 ];
3640 let array = create_decimal256_array(array, 20, 3).unwrap();
3641 generate_cast_test_case!(
3642 &array,
3643 Decimal256Array,
3644 &output_type,
3645 vec![
3646 Some(i256::from_i128(11234560_i128)),
3647 Some(i256::from_i128(21234560_i128)),
3648 Some(i256::from_i128(31234560_i128)),
3649 None
3650 ]
3651 );
3652 }
3653
3654 fn generate_decimal_to_numeric_cast_test_case<T>(array: &PrimitiveArray<T>)
3655 where
3656 T: ArrowPrimitiveType + DecimalType,
3657 {
3658 generate_cast_test_case!(
3660 array,
3661 UInt8Array,
3662 &DataType::UInt8,
3663 vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
3664 );
3665 generate_cast_test_case!(
3667 array,
3668 UInt16Array,
3669 &DataType::UInt16,
3670 vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
3671 );
3672 generate_cast_test_case!(
3674 array,
3675 UInt32Array,
3676 &DataType::UInt32,
3677 vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
3678 );
3679 generate_cast_test_case!(
3681 array,
3682 UInt64Array,
3683 &DataType::UInt64,
3684 vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
3685 );
3686 generate_cast_test_case!(
3688 array,
3689 Int8Array,
3690 &DataType::Int8,
3691 vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
3692 );
3693 generate_cast_test_case!(
3695 array,
3696 Int16Array,
3697 &DataType::Int16,
3698 vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
3699 );
3700 generate_cast_test_case!(
3702 array,
3703 Int32Array,
3704 &DataType::Int32,
3705 vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
3706 );
3707 generate_cast_test_case!(
3709 array,
3710 Int64Array,
3711 &DataType::Int64,
3712 vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
3713 );
3714 generate_cast_test_case!(
3716 array,
3717 Float16Array,
3718 &DataType::Float16,
3719 vec![
3720 Some(f16::from_f32(1.25)),
3721 Some(f16::from_f32(2.25)),
3722 Some(f16::from_f32(3.25)),
3723 None,
3724 Some(f16::from_f32(5.25))
3725 ]
3726 );
3727 generate_cast_test_case!(
3729 array,
3730 Float32Array,
3731 &DataType::Float32,
3732 vec![
3733 Some(1.25_f32),
3734 Some(2.25_f32),
3735 Some(3.25_f32),
3736 None,
3737 Some(5.25_f32)
3738 ]
3739 );
3740 generate_cast_test_case!(
3742 array,
3743 Float64Array,
3744 &DataType::Float64,
3745 vec![
3746 Some(1.25_f64),
3747 Some(2.25_f64),
3748 Some(3.25_f64),
3749 None,
3750 Some(5.25_f64)
3751 ]
3752 );
3753 }
3754
3755 #[test]
3756 fn test_cast_decimal32_to_numeric() {
3757 let value_array: Vec<Option<i32>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3758 let array = create_decimal32_array(value_array, 8, 2).unwrap();
3759
3760 generate_decimal_to_numeric_cast_test_case(&array);
3761 }
3762
3763 #[test]
3764 fn test_cast_decimal64_to_numeric() {
3765 let value_array: Vec<Option<i64>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3766 let array = create_decimal64_array(value_array, 8, 2).unwrap();
3767
3768 generate_decimal_to_numeric_cast_test_case(&array);
3769 }
3770
3771 #[test]
3772 fn test_cast_decimal128_to_numeric() {
3773 let value_array: Vec<Option<i128>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3774 let array = create_decimal128_array(value_array, 38, 2).unwrap();
3775
3776 generate_decimal_to_numeric_cast_test_case(&array);
3777
3778 let value_array: Vec<Option<i128>> = vec![Some(51300)];
3780 let array = create_decimal128_array(value_array, 38, 2).unwrap();
3781 let casted_array = cast_with_options(
3782 &array,
3783 &DataType::UInt8,
3784 &CastOptions {
3785 safe: false,
3786 format_options: FormatOptions::default(),
3787 },
3788 );
3789 assert_eq!(
3790 "Cast error: value of 513 is out of range UInt8".to_string(),
3791 casted_array.unwrap_err().to_string()
3792 );
3793
3794 let casted_array = cast_with_options(
3795 &array,
3796 &DataType::UInt8,
3797 &CastOptions {
3798 safe: true,
3799 format_options: FormatOptions::default(),
3800 },
3801 );
3802 assert!(casted_array.is_ok());
3803 assert!(casted_array.unwrap().is_null(0));
3804
3805 let value_array: Vec<Option<i128>> = vec![Some(24400)];
3807 let array = create_decimal128_array(value_array, 38, 2).unwrap();
3808 let casted_array = cast_with_options(
3809 &array,
3810 &DataType::Int8,
3811 &CastOptions {
3812 safe: false,
3813 format_options: FormatOptions::default(),
3814 },
3815 );
3816 assert_eq!(
3817 "Cast error: value of 244 is out of range Int8".to_string(),
3818 casted_array.unwrap_err().to_string()
3819 );
3820
3821 let casted_array = cast_with_options(
3822 &array,
3823 &DataType::Int8,
3824 &CastOptions {
3825 safe: true,
3826 format_options: FormatOptions::default(),
3827 },
3828 );
3829 assert!(casted_array.is_ok());
3830 assert!(casted_array.unwrap().is_null(0));
3831
3832 let value_array: Vec<Option<i128>> = vec![
3836 Some(125),
3837 Some(225),
3838 Some(325),
3839 None,
3840 Some(525),
3841 Some(112345678),
3842 Some(112345679),
3843 ];
3844 let array = create_decimal128_array(value_array, 38, 2).unwrap();
3845 generate_cast_test_case!(
3846 &array,
3847 Float32Array,
3848 &DataType::Float32,
3849 vec![
3850 Some(1.25_f32),
3851 Some(2.25_f32),
3852 Some(3.25_f32),
3853 None,
3854 Some(5.25_f32),
3855 Some(1_123_456.7_f32),
3856 Some(1_123_456.7_f32)
3857 ]
3858 );
3859
3860 let value_array: Vec<Option<i128>> = vec![
3863 Some(125),
3864 Some(225),
3865 Some(325),
3866 None,
3867 Some(525),
3868 Some(112345678901234568),
3869 Some(112345678901234560),
3870 ];
3871 let array = create_decimal128_array(value_array, 38, 2).unwrap();
3872 generate_cast_test_case!(
3873 &array,
3874 Float64Array,
3875 &DataType::Float64,
3876 vec![
3877 Some(1.25_f64),
3878 Some(2.25_f64),
3879 Some(3.25_f64),
3880 None,
3881 Some(5.25_f64),
3882 Some(1_123_456_789_012_345.6_f64),
3883 Some(1_123_456_789_012_345.6_f64),
3884 ]
3885 );
3886 }
3887
3888 #[test]
3889 fn test_cast_decimal256_to_numeric() {
3890 let value_array: Vec<Option<i256>> = vec![
3891 Some(i256::from_i128(125)),
3892 Some(i256::from_i128(225)),
3893 Some(i256::from_i128(325)),
3894 None,
3895 Some(i256::from_i128(525)),
3896 ];
3897 let array = create_decimal256_array(value_array, 38, 2).unwrap();
3898 generate_cast_test_case!(
3900 &array,
3901 UInt8Array,
3902 &DataType::UInt8,
3903 vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
3904 );
3905 generate_cast_test_case!(
3907 &array,
3908 UInt16Array,
3909 &DataType::UInt16,
3910 vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
3911 );
3912 generate_cast_test_case!(
3914 &array,
3915 UInt32Array,
3916 &DataType::UInt32,
3917 vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
3918 );
3919 generate_cast_test_case!(
3921 &array,
3922 UInt64Array,
3923 &DataType::UInt64,
3924 vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
3925 );
3926 generate_cast_test_case!(
3928 &array,
3929 Int8Array,
3930 &DataType::Int8,
3931 vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
3932 );
3933 generate_cast_test_case!(
3935 &array,
3936 Int16Array,
3937 &DataType::Int16,
3938 vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
3939 );
3940 generate_cast_test_case!(
3942 &array,
3943 Int32Array,
3944 &DataType::Int32,
3945 vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
3946 );
3947 generate_cast_test_case!(
3949 &array,
3950 Int64Array,
3951 &DataType::Int64,
3952 vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
3953 );
3954 generate_cast_test_case!(
3956 &array,
3957 Float16Array,
3958 &DataType::Float16,
3959 vec![
3960 Some(f16::from_f32(1.25)),
3961 Some(f16::from_f32(2.25)),
3962 Some(f16::from_f32(3.25)),
3963 None,
3964 Some(f16::from_f32(5.25))
3965 ]
3966 );
3967 generate_cast_test_case!(
3969 &array,
3970 Float32Array,
3971 &DataType::Float32,
3972 vec![
3973 Some(1.25_f32),
3974 Some(2.25_f32),
3975 Some(3.25_f32),
3976 None,
3977 Some(5.25_f32)
3978 ]
3979 );
3980 generate_cast_test_case!(
3982 &array,
3983 Float64Array,
3984 &DataType::Float64,
3985 vec![
3986 Some(1.25_f64),
3987 Some(2.25_f64),
3988 Some(3.25_f64),
3989 None,
3990 Some(5.25_f64)
3991 ]
3992 );
3993
3994 let value_array: Vec<Option<i256>> = vec![Some(i256::from_i128(24400))];
3996 let array = create_decimal256_array(value_array, 38, 2).unwrap();
3997 let casted_array = cast_with_options(
3998 &array,
3999 &DataType::Int8,
4000 &CastOptions {
4001 safe: false,
4002 format_options: FormatOptions::default(),
4003 },
4004 );
4005 assert_eq!(
4006 "Cast error: value of 244 is out of range Int8".to_string(),
4007 casted_array.unwrap_err().to_string()
4008 );
4009
4010 let casted_array = cast_with_options(
4011 &array,
4012 &DataType::Int8,
4013 &CastOptions {
4014 safe: true,
4015 format_options: FormatOptions::default(),
4016 },
4017 );
4018 assert!(casted_array.is_ok());
4019 assert!(casted_array.unwrap().is_null(0));
4020
4021 let value_array: Vec<Option<i256>> = vec![
4025 Some(i256::from_i128(125)),
4026 Some(i256::from_i128(225)),
4027 Some(i256::from_i128(325)),
4028 None,
4029 Some(i256::from_i128(525)),
4030 Some(i256::from_i128(112345678)),
4031 Some(i256::from_i128(112345679)),
4032 ];
4033 let array = create_decimal256_array(value_array, 76, 2).unwrap();
4034 generate_cast_test_case!(
4035 &array,
4036 Float32Array,
4037 &DataType::Float32,
4038 vec![
4039 Some(1.25_f32),
4040 Some(2.25_f32),
4041 Some(3.25_f32),
4042 None,
4043 Some(5.25_f32),
4044 Some(1_123_456.7_f32),
4045 Some(1_123_456.7_f32)
4046 ]
4047 );
4048
4049 let value_array: Vec<Option<i256>> = vec![
4052 Some(i256::from_i128(125)),
4053 Some(i256::from_i128(225)),
4054 Some(i256::from_i128(325)),
4055 None,
4056 Some(i256::from_i128(525)),
4057 Some(i256::from_i128(112345678901234568)),
4058 Some(i256::from_i128(112345678901234560)),
4059 ];
4060 let array = create_decimal256_array(value_array, 76, 2).unwrap();
4061 generate_cast_test_case!(
4062 &array,
4063 Float64Array,
4064 &DataType::Float64,
4065 vec![
4066 Some(1.25_f64),
4067 Some(2.25_f64),
4068 Some(3.25_f64),
4069 None,
4070 Some(5.25_f64),
4071 Some(1_123_456_789_012_345.6_f64),
4072 Some(1_123_456_789_012_345.6_f64),
4073 ]
4074 );
4075 }
4076
4077 #[test]
4078 fn test_cast_decimal128_to_float16_overflow() {
4079 let array = create_decimal128_array(
4080 vec![
4081 Some(6_550_400_i128),
4082 Some(100_000_000_i128),
4083 Some(-100_000_000_i128),
4084 None,
4085 ],
4086 10,
4087 2,
4088 )
4089 .unwrap();
4090
4091 generate_cast_test_case!(
4092 &array,
4093 Float16Array,
4094 &DataType::Float16,
4095 vec![
4096 Some(f16::from_f64(65504.0)),
4097 Some(f16::INFINITY),
4098 Some(f16::NEG_INFINITY),
4099 None
4100 ]
4101 );
4102 }
4103
4104 #[test]
4105 fn test_cast_decimal256_to_float16_overflow() {
4106 let array = create_decimal256_array(
4107 vec![
4108 Some(i256::from_i128(6_550_400_i128)),
4109 Some(i256::from_i128(100_000_000_i128)),
4110 Some(i256::from_i128(-100_000_000_i128)),
4111 None,
4112 ],
4113 10,
4114 2,
4115 )
4116 .unwrap();
4117
4118 generate_cast_test_case!(
4119 &array,
4120 Float16Array,
4121 &DataType::Float16,
4122 vec![
4123 Some(f16::from_f64(65504.0)),
4124 Some(f16::INFINITY),
4125 Some(f16::NEG_INFINITY),
4126 None
4127 ]
4128 );
4129 }
4130
4131 #[test]
4132 fn test_cast_decimal_to_numeric_negative_scale() {
4133 let value_array: Vec<Option<i256>> = vec![
4134 Some(i256::from_i128(125)),
4135 Some(i256::from_i128(225)),
4136 Some(i256::from_i128(325)),
4137 None,
4138 Some(i256::from_i128(525)),
4139 ];
4140 let array = create_decimal256_array(value_array, 38, -1).unwrap();
4141
4142 generate_cast_test_case!(
4143 &array,
4144 Int64Array,
4145 &DataType::Int64,
4146 vec![Some(1_250), Some(2_250), Some(3_250), None, Some(5_250)]
4147 );
4148
4149 let value_array: Vec<Option<i128>> = vec![Some(12), Some(-12), None];
4150 let array = create_decimal128_array(value_array, 10, -2).unwrap();
4151 generate_cast_test_case!(
4152 &array,
4153 Float16Array,
4154 &DataType::Float16,
4155 vec![
4156 Some(f16::from_f32(1200.0)),
4157 Some(f16::from_f32(-1200.0)),
4158 None
4159 ]
4160 );
4161
4162 let value_array: Vec<Option<i32>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
4163 let array = create_decimal32_array(value_array, 8, -2).unwrap();
4164 generate_cast_test_case!(
4165 &array,
4166 Int64Array,
4167 &DataType::Int64,
4168 vec![Some(12_500), Some(22_500), Some(32_500), None, Some(52_500)]
4169 );
4170
4171 let value_array: Vec<Option<i32>> = vec![Some(2), Some(1), None];
4172 let array = create_decimal32_array(value_array, 9, -9).unwrap();
4173 generate_cast_test_case!(
4174 &array,
4175 Int64Array,
4176 &DataType::Int64,
4177 vec![Some(2_000_000_000), Some(1_000_000_000), None]
4178 );
4179
4180 let value_array: Vec<Option<i64>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
4181 let array = create_decimal64_array(value_array, 18, -3).unwrap();
4182 generate_cast_test_case!(
4183 &array,
4184 Int64Array,
4185 &DataType::Int64,
4186 vec![
4187 Some(125_000),
4188 Some(225_000),
4189 Some(325_000),
4190 None,
4191 Some(525_000)
4192 ]
4193 );
4194
4195 let value_array: Vec<Option<i64>> = vec![Some(12), Some(34), None];
4196 let array = create_decimal64_array(value_array, 18, -10).unwrap();
4197 generate_cast_test_case!(
4198 &array,
4199 Int64Array,
4200 &DataType::Int64,
4201 vec![Some(120_000_000_000), Some(340_000_000_000), None]
4202 );
4203
4204 let value_array: Vec<Option<i128>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
4205 let array = create_decimal128_array(value_array, 38, -4).unwrap();
4206 generate_cast_test_case!(
4207 &array,
4208 Int64Array,
4209 &DataType::Int64,
4210 vec![
4211 Some(1_250_000),
4212 Some(2_250_000),
4213 Some(3_250_000),
4214 None,
4215 Some(5_250_000)
4216 ]
4217 );
4218
4219 let value_array: Vec<Option<i128>> = vec![Some(9), Some(1), None];
4220 let array = create_decimal128_array(value_array, 38, -18).unwrap();
4221 generate_cast_test_case!(
4222 &array,
4223 Int64Array,
4224 &DataType::Int64,
4225 vec![
4226 Some(9_000_000_000_000_000_000),
4227 Some(1_000_000_000_000_000_000),
4228 None
4229 ]
4230 );
4231
4232 let array = create_decimal32_array(vec![Some(999_999_999)], 9, -1).unwrap();
4233 let casted_array = cast_with_options(
4234 &array,
4235 &DataType::Int64,
4236 &CastOptions {
4237 safe: false,
4238 format_options: FormatOptions::default(),
4239 },
4240 );
4241 assert_eq!(
4242 "Arithmetic overflow: Overflow happened on: 999999999 * 10".to_string(),
4243 casted_array.unwrap_err().to_string()
4244 );
4245
4246 let casted_array = cast_with_options(
4247 &array,
4248 &DataType::Int64,
4249 &CastOptions {
4250 safe: true,
4251 format_options: FormatOptions::default(),
4252 },
4253 );
4254 assert!(casted_array.is_ok());
4255 assert!(casted_array.unwrap().is_null(0));
4256
4257 let array = create_decimal64_array(vec![Some(13)], 18, -1).unwrap();
4258 let casted_array = cast_with_options(
4259 &array,
4260 &DataType::Int8,
4261 &CastOptions {
4262 safe: false,
4263 format_options: FormatOptions::default(),
4264 },
4265 );
4266 assert_eq!(
4267 "Cast error: value of 130 is out of range Int8".to_string(),
4268 casted_array.unwrap_err().to_string()
4269 );
4270
4271 let casted_array = cast_with_options(
4272 &array,
4273 &DataType::Int8,
4274 &CastOptions {
4275 safe: true,
4276 format_options: FormatOptions::default(),
4277 },
4278 );
4279 assert!(casted_array.is_ok());
4280 assert!(casted_array.unwrap().is_null(0));
4281 }
4282
4283 #[test]
4284 fn test_cast_numeric_to_decimal128() {
4285 let decimal_type = DataType::Decimal128(38, 6);
4286 let input_datas = vec![
4288 Arc::new(UInt8Array::from(vec![
4289 Some(1),
4290 Some(2),
4291 Some(3),
4292 None,
4293 Some(5),
4294 ])) as ArrayRef, Arc::new(UInt16Array::from(vec![
4296 Some(1),
4297 Some(2),
4298 Some(3),
4299 None,
4300 Some(5),
4301 ])) as ArrayRef, Arc::new(UInt32Array::from(vec![
4303 Some(1),
4304 Some(2),
4305 Some(3),
4306 None,
4307 Some(5),
4308 ])) as ArrayRef, Arc::new(UInt64Array::from(vec![
4310 Some(1),
4311 Some(2),
4312 Some(3),
4313 None,
4314 Some(5),
4315 ])) as ArrayRef, ];
4317
4318 for array in input_datas {
4319 generate_cast_test_case!(
4320 &array,
4321 Decimal128Array,
4322 &decimal_type,
4323 vec![
4324 Some(1000000_i128),
4325 Some(2000000_i128),
4326 Some(3000000_i128),
4327 None,
4328 Some(5000000_i128)
4329 ]
4330 );
4331 }
4332
4333 let input_datas = vec![
4335 Arc::new(Int8Array::from(vec![
4336 Some(1),
4337 Some(2),
4338 Some(3),
4339 None,
4340 Some(5),
4341 ])) as ArrayRef, Arc::new(Int16Array::from(vec![
4343 Some(1),
4344 Some(2),
4345 Some(3),
4346 None,
4347 Some(5),
4348 ])) as ArrayRef, Arc::new(Int32Array::from(vec![
4350 Some(1),
4351 Some(2),
4352 Some(3),
4353 None,
4354 Some(5),
4355 ])) as ArrayRef, Arc::new(Int64Array::from(vec![
4357 Some(1),
4358 Some(2),
4359 Some(3),
4360 None,
4361 Some(5),
4362 ])) as ArrayRef, ];
4364 for array in input_datas {
4365 generate_cast_test_case!(
4366 &array,
4367 Decimal128Array,
4368 &decimal_type,
4369 vec![
4370 Some(1000000_i128),
4371 Some(2000000_i128),
4372 Some(3000000_i128),
4373 None,
4374 Some(5000000_i128)
4375 ]
4376 );
4377 }
4378
4379 let array = UInt8Array::from(vec![1, 2, 3, 4, 100]);
4382 let casted_array = cast(&array, &DataType::Decimal128(3, 1));
4383 assert!(casted_array.is_ok());
4384 let array = casted_array.unwrap();
4385 let array: &Decimal128Array = array.as_primitive();
4386 assert!(array.is_null(4));
4387
4388 let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
4391 let casted_array = cast(&array, &DataType::Decimal128(3, 1));
4392 assert!(casted_array.is_ok());
4393 let array = casted_array.unwrap();
4394 let array: &Decimal128Array = array.as_primitive();
4395 assert!(array.is_null(4));
4396
4397 let array = Float32Array::from(vec![
4399 Some(1.1),
4400 Some(2.2),
4401 Some(4.4),
4402 None,
4403 Some(1.123_456_4), Some(1.123_456_7), ]);
4406 let array = Arc::new(array) as ArrayRef;
4407 generate_cast_test_case!(
4408 &array,
4409 Decimal128Array,
4410 &decimal_type,
4411 vec![
4412 Some(1100000_i128),
4413 Some(2200000_i128),
4414 Some(4400000_i128),
4415 None,
4416 Some(1123456_i128), Some(1123457_i128), ]
4419 );
4420
4421 let array = Float64Array::from(vec![
4423 Some(1.1),
4424 Some(2.2),
4425 Some(4.4),
4426 None,
4427 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), ]);
4432 generate_cast_test_case!(
4433 &array,
4434 Decimal128Array,
4435 &decimal_type,
4436 vec![
4437 Some(1100000_i128),
4438 Some(2200000_i128),
4439 Some(4400000_i128),
4440 None,
4441 Some(1123456_i128), Some(1123457_i128), Some(1123456_i128), Some(1123457_i128), ]
4446 );
4447 }
4448
4449 #[test]
4450 fn test_cast_numeric_to_decimal256() {
4451 let decimal_type = DataType::Decimal256(76, 6);
4452 let input_datas = vec![
4454 Arc::new(UInt8Array::from(vec![
4455 Some(1),
4456 Some(2),
4457 Some(3),
4458 None,
4459 Some(5),
4460 ])) as ArrayRef, Arc::new(UInt16Array::from(vec![
4462 Some(1),
4463 Some(2),
4464 Some(3),
4465 None,
4466 Some(5),
4467 ])) as ArrayRef, Arc::new(UInt32Array::from(vec![
4469 Some(1),
4470 Some(2),
4471 Some(3),
4472 None,
4473 Some(5),
4474 ])) as ArrayRef, Arc::new(UInt64Array::from(vec![
4476 Some(1),
4477 Some(2),
4478 Some(3),
4479 None,
4480 Some(5),
4481 ])) as ArrayRef, ];
4483
4484 for array in input_datas {
4485 generate_cast_test_case!(
4486 &array,
4487 Decimal256Array,
4488 &decimal_type,
4489 vec![
4490 Some(i256::from_i128(1000000_i128)),
4491 Some(i256::from_i128(2000000_i128)),
4492 Some(i256::from_i128(3000000_i128)),
4493 None,
4494 Some(i256::from_i128(5000000_i128))
4495 ]
4496 );
4497 }
4498
4499 let input_datas = vec![
4501 Arc::new(Int8Array::from(vec![
4502 Some(1),
4503 Some(2),
4504 Some(3),
4505 None,
4506 Some(5),
4507 ])) as ArrayRef, Arc::new(Int16Array::from(vec![
4509 Some(1),
4510 Some(2),
4511 Some(3),
4512 None,
4513 Some(5),
4514 ])) as ArrayRef, Arc::new(Int32Array::from(vec![
4516 Some(1),
4517 Some(2),
4518 Some(3),
4519 None,
4520 Some(5),
4521 ])) as ArrayRef, Arc::new(Int64Array::from(vec![
4523 Some(1),
4524 Some(2),
4525 Some(3),
4526 None,
4527 Some(5),
4528 ])) as ArrayRef, ];
4530 for array in input_datas {
4531 generate_cast_test_case!(
4532 &array,
4533 Decimal256Array,
4534 &decimal_type,
4535 vec![
4536 Some(i256::from_i128(1000000_i128)),
4537 Some(i256::from_i128(2000000_i128)),
4538 Some(i256::from_i128(3000000_i128)),
4539 None,
4540 Some(i256::from_i128(5000000_i128))
4541 ]
4542 );
4543 }
4544
4545 let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
4548 let array = Arc::new(array) as ArrayRef;
4549 let casted_array = cast(&array, &DataType::Decimal256(3, 1));
4550 assert!(casted_array.is_ok());
4551 let array = casted_array.unwrap();
4552 let array: &Decimal256Array = array.as_primitive();
4553 assert!(array.is_null(4));
4554
4555 let array = Float32Array::from(vec![
4557 Some(1.1),
4558 Some(2.2),
4559 Some(4.4),
4560 None,
4561 Some(1.123_456_4), Some(1.123_456_7), ]);
4564 generate_cast_test_case!(
4565 &array,
4566 Decimal256Array,
4567 &decimal_type,
4568 vec![
4569 Some(i256::from_i128(1100000_i128)),
4570 Some(i256::from_i128(2200000_i128)),
4571 Some(i256::from_i128(4400000_i128)),
4572 None,
4573 Some(i256::from_i128(1123456_i128)), Some(i256::from_i128(1123457_i128)), ]
4576 );
4577
4578 let array = Float64Array::from(vec![
4580 Some(1.1),
4581 Some(2.2),
4582 Some(4.4),
4583 None,
4584 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), ]);
4589 generate_cast_test_case!(
4590 &array,
4591 Decimal256Array,
4592 &decimal_type,
4593 vec![
4594 Some(i256::from_i128(1100000_i128)),
4595 Some(i256::from_i128(2200000_i128)),
4596 Some(i256::from_i128(4400000_i128)),
4597 None,
4598 Some(i256::from_i128(1123456_i128)), Some(i256::from_i128(1123457_i128)), Some(i256::from_i128(1123456_i128)), Some(i256::from_i128(1123457_i128)), ]
4603 );
4604 }
4605
4606 #[test]
4607 fn test_cast_i32_to_f64() {
4608 let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4609 let b = cast(&array, &DataType::Float64).unwrap();
4610 let c = b.as_primitive::<Float64Type>();
4611 assert_eq!(5.0, c.value(0));
4612 assert_eq!(6.0, c.value(1));
4613 assert_eq!(7.0, c.value(2));
4614 assert_eq!(8.0, c.value(3));
4615 assert_eq!(9.0, c.value(4));
4616 }
4617
4618 #[test]
4619 fn test_cast_i32_to_u8() {
4620 let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4621 let b = cast(&array, &DataType::UInt8).unwrap();
4622 let c = b.as_primitive::<UInt8Type>();
4623 assert!(!c.is_valid(0));
4624 assert_eq!(6, c.value(1));
4625 assert!(!c.is_valid(2));
4626 assert_eq!(8, c.value(3));
4627 assert!(!c.is_valid(4));
4629 }
4630
4631 #[test]
4632 #[should_panic(expected = "Can't cast value -5 to type UInt8")]
4633 fn test_cast_int32_to_u8_with_error() {
4634 let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4635 let cast_option = CastOptions {
4637 safe: false,
4638 format_options: FormatOptions::default(),
4639 };
4640 let result = cast_with_options(&array, &DataType::UInt8, &cast_option);
4641 assert!(result.is_err());
4642 result.unwrap();
4643 }
4644
4645 #[test]
4646 fn test_cast_i32_to_u8_sliced() {
4647 let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4648 assert_eq!(0, array.offset());
4649 let array = array.slice(2, 3);
4650 let b = cast(&array, &DataType::UInt8).unwrap();
4651 assert_eq!(3, b.len());
4652 let c = b.as_primitive::<UInt8Type>();
4653 assert!(!c.is_valid(0));
4654 assert_eq!(8, c.value(1));
4655 assert!(!c.is_valid(2));
4657 }
4658
4659 #[test]
4660 fn test_cast_i32_to_i32() {
4661 let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4662 let b = cast(&array, &DataType::Int32).unwrap();
4663 let c = b.as_primitive::<Int32Type>();
4664 assert_eq!(5, c.value(0));
4665 assert_eq!(6, c.value(1));
4666 assert_eq!(7, c.value(2));
4667 assert_eq!(8, c.value(3));
4668 assert_eq!(9, c.value(4));
4669 }
4670
4671 #[test]
4672 fn test_cast_i32_to_list_i32() {
4673 let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4674 let b = cast(
4675 &array,
4676 &DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
4677 )
4678 .unwrap();
4679 assert_eq!(5, b.len());
4680 let arr = b.as_list::<i32>();
4681 assert_eq!(&[0, 1, 2, 3, 4, 5], arr.value_offsets());
4682 assert_eq!(1, arr.value_length(0));
4683 assert_eq!(1, arr.value_length(1));
4684 assert_eq!(1, arr.value_length(2));
4685 assert_eq!(1, arr.value_length(3));
4686 assert_eq!(1, arr.value_length(4));
4687 let c = arr.values().as_primitive::<Int32Type>();
4688 assert_eq!(5, c.value(0));
4689 assert_eq!(6, c.value(1));
4690 assert_eq!(7, c.value(2));
4691 assert_eq!(8, c.value(3));
4692 assert_eq!(9, c.value(4));
4693 }
4694
4695 #[test]
4696 fn test_cast_i32_to_list_i32_nullable() {
4697 let array = Int32Array::from(vec![Some(5), None, Some(7), Some(8), Some(9)]);
4698 let b = cast(
4699 &array,
4700 &DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
4701 )
4702 .unwrap();
4703 assert_eq!(5, b.len());
4704 assert_eq!(0, b.null_count());
4705 let arr = b.as_list::<i32>();
4706 assert_eq!(&[0, 1, 2, 3, 4, 5], arr.value_offsets());
4707 assert_eq!(1, arr.value_length(0));
4708 assert_eq!(1, arr.value_length(1));
4709 assert_eq!(1, arr.value_length(2));
4710 assert_eq!(1, arr.value_length(3));
4711 assert_eq!(1, arr.value_length(4));
4712
4713 let c = arr.values().as_primitive::<Int32Type>();
4714 assert_eq!(1, c.null_count());
4715 assert_eq!(5, c.value(0));
4716 assert!(!c.is_valid(1));
4717 assert_eq!(7, c.value(2));
4718 assert_eq!(8, c.value(3));
4719 assert_eq!(9, c.value(4));
4720 }
4721
4722 #[test]
4723 fn test_cast_i32_to_list_f64_nullable_sliced() {
4724 let array = Int32Array::from(vec![Some(5), None, Some(7), Some(8), None, Some(10)]);
4725 let array = array.slice(2, 4);
4726 let b = cast(
4727 &array,
4728 &DataType::List(Arc::new(Field::new_list_field(DataType::Float64, true))),
4729 )
4730 .unwrap();
4731 assert_eq!(4, b.len());
4732 assert_eq!(0, b.null_count());
4733 let arr = b.as_list::<i32>();
4734 assert_eq!(&[0, 1, 2, 3, 4], arr.value_offsets());
4735 assert_eq!(1, arr.value_length(0));
4736 assert_eq!(1, arr.value_length(1));
4737 assert_eq!(1, arr.value_length(2));
4738 assert_eq!(1, arr.value_length(3));
4739 let c = arr.values().as_primitive::<Float64Type>();
4740 assert_eq!(1, c.null_count());
4741 assert_eq!(7.0, c.value(0));
4742 assert_eq!(8.0, c.value(1));
4743 assert!(!c.is_valid(2));
4744 assert_eq!(10.0, c.value(3));
4745 }
4746
4747 #[test]
4748 fn test_cast_int_to_utf8view() {
4749 let inputs = vec![
4750 Arc::new(Int8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4751 Arc::new(Int16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4752 Arc::new(Int32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4753 Arc::new(Int64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4754 Arc::new(UInt8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4755 Arc::new(UInt16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4756 Arc::new(UInt32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4757 Arc::new(UInt64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4758 ];
4759 let expected: ArrayRef = Arc::new(StringViewArray::from(vec![
4760 None,
4761 Some("8"),
4762 Some("9"),
4763 Some("10"),
4764 ]));
4765
4766 for array in inputs {
4767 assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
4768 let arr = cast(&array, &DataType::Utf8View).unwrap();
4769 assert_eq!(expected.as_ref(), arr.as_ref());
4770 }
4771 }
4772
4773 #[test]
4774 fn test_cast_float_to_utf8view() {
4775 let inputs = vec![
4776 Arc::new(Float16Array::from(vec![
4777 Some(f16::from_f64(1.5)),
4778 Some(f16::from_f64(2.5)),
4779 None,
4780 ])) as ArrayRef,
4781 Arc::new(Float32Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
4782 Arc::new(Float64Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
4783 ];
4784
4785 let expected: ArrayRef =
4786 Arc::new(StringViewArray::from(vec![Some("1.5"), Some("2.5"), None]));
4787
4788 for array in inputs {
4789 assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
4790 let arr = cast(&array, &DataType::Utf8View).unwrap();
4791 assert_eq!(expected.as_ref(), arr.as_ref());
4792 }
4793 }
4794
4795 #[test]
4796 fn test_cast_utf8_to_i32() {
4797 let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4798 let b = cast(&array, &DataType::Int32).unwrap();
4799 let c = b.as_primitive::<Int32Type>();
4800 assert_eq!(5, c.value(0));
4801 assert_eq!(6, c.value(1));
4802 assert!(!c.is_valid(2));
4803 assert_eq!(8, c.value(3));
4804 assert!(!c.is_valid(4));
4805 }
4806
4807 #[test]
4808 fn test_cast_utf8view_to_i32() {
4809 let array = StringViewArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4810 let b = cast(&array, &DataType::Int32).unwrap();
4811 let c = b.as_primitive::<Int32Type>();
4812 assert_eq!(5, c.value(0));
4813 assert_eq!(6, c.value(1));
4814 assert!(!c.is_valid(2));
4815 assert_eq!(8, c.value(3));
4816 assert!(!c.is_valid(4));
4817 }
4818
4819 #[test]
4820 fn test_cast_utf8view_to_f32() {
4821 let array = StringViewArray::from(vec!["3", "4.56", "seven", "8.9"]);
4822 let b = cast(&array, &DataType::Float32).unwrap();
4823 let c = b.as_primitive::<Float32Type>();
4824 assert_eq!(3.0, c.value(0));
4825 assert_eq!(4.56, c.value(1));
4826 assert!(!c.is_valid(2));
4827 assert_eq!(8.9, c.value(3));
4828 }
4829
4830 #[test]
4831 fn test_cast_string_to_f16() {
4832 let arrays = [
4833 Arc::new(StringViewArray::from(vec!["3", "4.56", "seven", "8.9"])) as ArrayRef,
4834 Arc::new(StringArray::from(vec!["3", "4.56", "seven", "8.9"])),
4835 Arc::new(LargeStringArray::from(vec!["3", "4.56", "seven", "8.9"])),
4836 ];
4837 for array in arrays {
4838 let b = cast(&array, &DataType::Float16).unwrap();
4839 let c = b.as_primitive::<Float16Type>();
4840 assert_eq!(half::f16::from_f32(3.0), c.value(0));
4841 assert_eq!(half::f16::from_f32(4.56), c.value(1));
4842 assert!(!c.is_valid(2));
4843 assert_eq!(half::f16::from_f32(8.9), c.value(3));
4844 }
4845 }
4846
4847 #[test]
4848 fn test_cast_utf8view_to_decimal128() {
4849 let array = StringViewArray::from(vec![None, Some("4"), Some("5.6"), Some("7.89")]);
4850 let arr = Arc::new(array) as ArrayRef;
4851 generate_cast_test_case!(
4852 &arr,
4853 Decimal128Array,
4854 &DataType::Decimal128(4, 2),
4855 vec![None, Some(400_i128), Some(560_i128), Some(789_i128)]
4856 );
4857 }
4858
4859 #[test]
4860 fn test_cast_with_options_utf8_to_i32() {
4861 let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4862 let result = cast_with_options(
4863 &array,
4864 &DataType::Int32,
4865 &CastOptions {
4866 safe: false,
4867 format_options: FormatOptions::default(),
4868 },
4869 );
4870 match result {
4871 Ok(_) => panic!("expected error"),
4872 Err(e) => {
4873 assert!(
4874 e.to_string()
4875 .contains("Cast error: Cannot cast string 'seven' to value of Int32 type",),
4876 "Error: {e}"
4877 )
4878 }
4879 }
4880 }
4881
4882 #[test]
4883 fn test_cast_utf8_to_bool() {
4884 let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4885 let casted = cast(&strings, &DataType::Boolean).unwrap();
4886 let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
4887 assert_eq!(*as_boolean_array(&casted), expected);
4888 }
4889
4890 #[test]
4891 fn test_cast_utf8view_to_bool() {
4892 let strings = StringViewArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4893 let casted = cast(&strings, &DataType::Boolean).unwrap();
4894 let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
4895 assert_eq!(*as_boolean_array(&casted), expected);
4896 }
4897
4898 #[test]
4899 fn test_cast_with_options_utf8_to_bool() {
4900 let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4901 let casted = cast_with_options(
4902 &strings,
4903 &DataType::Boolean,
4904 &CastOptions {
4905 safe: false,
4906 format_options: FormatOptions::default(),
4907 },
4908 );
4909 match casted {
4910 Ok(_) => panic!("expected error"),
4911 Err(e) => {
4912 assert!(
4913 e.to_string().contains(
4914 "Cast error: Cannot cast value 'invalid' to value of Boolean type"
4915 )
4916 )
4917 }
4918 }
4919 }
4920
4921 #[test]
4922 fn test_cast_bool_to_i32() {
4923 let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4924 let b = cast(&array, &DataType::Int32).unwrap();
4925 let c = b.as_primitive::<Int32Type>();
4926 assert_eq!(1, c.value(0));
4927 assert_eq!(0, c.value(1));
4928 assert!(!c.is_valid(2));
4929 }
4930
4931 #[test]
4932 fn test_cast_bool_to_utf8view() {
4933 let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4934 let b = cast(&array, &DataType::Utf8View).unwrap();
4935 let c = b.as_any().downcast_ref::<StringViewArray>().unwrap();
4936 assert_eq!("true", c.value(0));
4937 assert_eq!("false", c.value(1));
4938 assert!(!c.is_valid(2));
4939 }
4940
4941 #[test]
4942 fn test_cast_bool_to_utf8() {
4943 let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4944 let b = cast(&array, &DataType::Utf8).unwrap();
4945 let c = b.as_any().downcast_ref::<StringArray>().unwrap();
4946 assert_eq!("true", c.value(0));
4947 assert_eq!("false", c.value(1));
4948 assert!(!c.is_valid(2));
4949 }
4950
4951 #[test]
4952 fn test_cast_bool_to_large_utf8() {
4953 let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4954 let b = cast(&array, &DataType::LargeUtf8).unwrap();
4955 let c = b.as_any().downcast_ref::<LargeStringArray>().unwrap();
4956 assert_eq!("true", c.value(0));
4957 assert_eq!("false", c.value(1));
4958 assert!(!c.is_valid(2));
4959 }
4960
4961 #[test]
4962 fn test_cast_bool_to_f64() {
4963 let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4964 let b = cast(&array, &DataType::Float64).unwrap();
4965 let c = b.as_primitive::<Float64Type>();
4966 assert_eq!(1.0, c.value(0));
4967 assert_eq!(0.0, c.value(1));
4968 assert!(!c.is_valid(2));
4969 }
4970
4971 #[test]
4972 fn test_cast_integer_to_timestamp() {
4973 let array = Int64Array::from(vec![Some(2), Some(10), None]);
4974 let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4975
4976 let array = Int8Array::from(vec![Some(2), Some(10), None]);
4977 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4978
4979 assert_eq!(&actual, &expected);
4980
4981 let array = Int16Array::from(vec![Some(2), Some(10), None]);
4982 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4983
4984 assert_eq!(&actual, &expected);
4985
4986 let array = Int32Array::from(vec![Some(2), Some(10), None]);
4987 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4988
4989 assert_eq!(&actual, &expected);
4990
4991 let array = UInt8Array::from(vec![Some(2), Some(10), None]);
4992 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4993
4994 assert_eq!(&actual, &expected);
4995
4996 let array = UInt16Array::from(vec![Some(2), Some(10), None]);
4997 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4998
4999 assert_eq!(&actual, &expected);
5000
5001 let array = UInt32Array::from(vec![Some(2), Some(10), None]);
5002 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5003
5004 assert_eq!(&actual, &expected);
5005
5006 let array = UInt64Array::from(vec![Some(2), Some(10), None]);
5007 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5008
5009 assert_eq!(&actual, &expected);
5010 }
5011
5012 #[test]
5013 fn test_cast_timestamp_to_integer() {
5014 let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
5015 .with_timezone("UTC".to_string());
5016 let expected = cast(&array, &DataType::Int64).unwrap();
5017
5018 let actual = cast(&cast(&array, &DataType::Int8).unwrap(), &DataType::Int64).unwrap();
5019 assert_eq!(&actual, &expected);
5020
5021 let actual = cast(&cast(&array, &DataType::Int16).unwrap(), &DataType::Int64).unwrap();
5022 assert_eq!(&actual, &expected);
5023
5024 let actual = cast(&cast(&array, &DataType::Int32).unwrap(), &DataType::Int64).unwrap();
5025 assert_eq!(&actual, &expected);
5026
5027 let actual = cast(&cast(&array, &DataType::UInt8).unwrap(), &DataType::Int64).unwrap();
5028 assert_eq!(&actual, &expected);
5029
5030 let actual = cast(&cast(&array, &DataType::UInt16).unwrap(), &DataType::Int64).unwrap();
5031 assert_eq!(&actual, &expected);
5032
5033 let actual = cast(&cast(&array, &DataType::UInt32).unwrap(), &DataType::Int64).unwrap();
5034 assert_eq!(&actual, &expected);
5035
5036 let actual = cast(&cast(&array, &DataType::UInt64).unwrap(), &DataType::Int64).unwrap();
5037 assert_eq!(&actual, &expected);
5038 }
5039
5040 #[test]
5041 fn test_cast_floating_to_timestamp() {
5042 let array = Int64Array::from(vec![Some(2), Some(10), None]);
5043 let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5044
5045 let array = Float16Array::from(vec![
5046 Some(f16::from_f32(2.0)),
5047 Some(f16::from_f32(10.6)),
5048 None,
5049 ]);
5050 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5051
5052 assert_eq!(&actual, &expected);
5053
5054 let array = Float32Array::from(vec![Some(2.0), Some(10.6), None]);
5055 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5056
5057 assert_eq!(&actual, &expected);
5058
5059 let array = Float64Array::from(vec![Some(2.1), Some(10.2), None]);
5060 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5061
5062 assert_eq!(&actual, &expected);
5063 }
5064
5065 #[test]
5066 fn test_cast_timestamp_to_floating() {
5067 let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
5068 .with_timezone("UTC".to_string());
5069 let expected = cast(&array, &DataType::Int64).unwrap();
5070
5071 let actual = cast(&cast(&array, &DataType::Float16).unwrap(), &DataType::Int64).unwrap();
5072 assert_eq!(&actual, &expected);
5073
5074 let actual = cast(&cast(&array, &DataType::Float32).unwrap(), &DataType::Int64).unwrap();
5075 assert_eq!(&actual, &expected);
5076
5077 let actual = cast(&cast(&array, &DataType::Float64).unwrap(), &DataType::Int64).unwrap();
5078 assert_eq!(&actual, &expected);
5079 }
5080
5081 #[test]
5082 fn test_cast_decimal_to_timestamp() {
5083 let array = Int64Array::from(vec![Some(2), Some(10), None]);
5084 let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5085
5086 let array = Decimal128Array::from(vec![Some(200), Some(1000), None])
5087 .with_precision_and_scale(4, 2)
5088 .unwrap();
5089 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5090
5091 assert_eq!(&actual, &expected);
5092
5093 let array = Decimal256Array::from(vec![
5094 Some(i256::from_i128(2000)),
5095 Some(i256::from_i128(10000)),
5096 None,
5097 ])
5098 .with_precision_and_scale(5, 3)
5099 .unwrap();
5100 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5101
5102 assert_eq!(&actual, &expected);
5103 }
5104
5105 #[test]
5106 fn test_cast_timestamp_to_decimal() {
5107 let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
5108 .with_timezone("UTC".to_string());
5109 let expected = cast(&array, &DataType::Int64).unwrap();
5110
5111 let actual = cast(
5112 &cast(&array, &DataType::Decimal128(5, 2)).unwrap(),
5113 &DataType::Int64,
5114 )
5115 .unwrap();
5116 assert_eq!(&actual, &expected);
5117
5118 let actual = cast(
5119 &cast(&array, &DataType::Decimal256(10, 5)).unwrap(),
5120 &DataType::Int64,
5121 )
5122 .unwrap();
5123 assert_eq!(&actual, &expected);
5124 }
5125
5126 #[test]
5127 fn test_cast_list_i32_to_list_u16() {
5128 let values = vec![
5129 Some(vec![Some(0), Some(0), Some(0)]),
5130 Some(vec![Some(-1), Some(-2), Some(-1)]),
5131 Some(vec![Some(2), Some(100000000)]),
5132 ];
5133 let list_array = ListArray::from_iter_primitive::<Int32Type, _, _>(values);
5134
5135 let target_type = DataType::List(Arc::new(Field::new("item", DataType::UInt16, true)));
5136 assert!(can_cast_types(list_array.data_type(), &target_type));
5137 let cast_array = cast(&list_array, &target_type).unwrap();
5138
5139 assert_eq!(0, cast_array.null_count());
5144
5145 let array = cast_array.as_list::<i32>();
5147 assert_eq!(list_array.value_offsets(), array.value_offsets());
5148
5149 assert_eq!(DataType::UInt16, array.value_type());
5150 assert_eq!(3, array.value_length(0));
5151 assert_eq!(3, array.value_length(1));
5152 assert_eq!(2, array.value_length(2));
5153
5154 let u16arr = array.values().as_primitive::<UInt16Type>();
5156 assert_eq!(4, u16arr.null_count());
5157
5158 let expected: UInt16Array =
5160 vec![Some(0), Some(0), Some(0), None, None, None, Some(2), None]
5161 .into_iter()
5162 .collect();
5163
5164 assert_eq!(u16arr, &expected);
5165 }
5166
5167 #[test]
5168 fn test_cast_list_i32_to_list_timestamp() {
5169 let value_data = Int32Array::from(vec![0, 0, 0, -1, -2, -1, 2, 8, 100000000]).into_data();
5171
5172 let value_offsets = Buffer::from_slice_ref([0, 3, 6, 9]);
5173
5174 let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
5176 let list_data = ArrayData::builder(list_data_type)
5177 .len(3)
5178 .add_buffer(value_offsets)
5179 .add_child_data(value_data)
5180 .build()
5181 .unwrap();
5182 let list_array = Arc::new(ListArray::from(list_data)) as ArrayRef;
5183
5184 let actual = cast(
5185 &list_array,
5186 &DataType::List(Arc::new(Field::new_list_field(
5187 DataType::Timestamp(TimeUnit::Microsecond, None),
5188 true,
5189 ))),
5190 )
5191 .unwrap();
5192
5193 let expected = cast(
5194 &cast(
5195 &list_array,
5196 &DataType::List(Arc::new(Field::new_list_field(DataType::Int64, true))),
5197 )
5198 .unwrap(),
5199 &DataType::List(Arc::new(Field::new_list_field(
5200 DataType::Timestamp(TimeUnit::Microsecond, None),
5201 true,
5202 ))),
5203 )
5204 .unwrap();
5205
5206 assert_eq!(&actual, &expected);
5207 }
5208
5209 #[test]
5210 fn test_cast_date32_to_date64() {
5211 let a = Date32Array::from(vec![10000, 17890]);
5212 let array = Arc::new(a) as ArrayRef;
5213 let b = cast(&array, &DataType::Date64).unwrap();
5214 let c = b.as_primitive::<Date64Type>();
5215 assert_eq!(864000000000, c.value(0));
5216 assert_eq!(1545696000000, c.value(1));
5217 }
5218
5219 #[test]
5220 fn test_cast_date64_to_date32() {
5221 let a = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5222 let array = Arc::new(a) as ArrayRef;
5223 let b = cast(&array, &DataType::Date32).unwrap();
5224 let c = b.as_primitive::<Date32Type>();
5225 assert_eq!(10000, c.value(0));
5226 assert_eq!(17890, c.value(1));
5227 assert!(c.is_null(2));
5228 }
5229
5230 #[test]
5231 fn test_cast_string_to_integral_overflow() {
5232 let str = Arc::new(StringArray::from(vec![
5233 Some("123"),
5234 Some("-123"),
5235 Some("86374"),
5236 None,
5237 ])) as ArrayRef;
5238
5239 let options = CastOptions {
5240 safe: true,
5241 format_options: FormatOptions::default(),
5242 };
5243 let res = cast_with_options(&str, &DataType::Int16, &options).expect("should cast to i16");
5244 let expected =
5245 Arc::new(Int16Array::from(vec![Some(123), Some(-123), None, None])) as ArrayRef;
5246 assert_eq!(&res, &expected);
5247 }
5248
5249 #[test]
5250 fn test_cast_string_to_timestamp() {
5251 let a0 = Arc::new(StringViewArray::from(vec![
5252 Some("2020-09-08T12:00:00.123456789+00:00"),
5253 Some("Not a valid date"),
5254 None,
5255 ])) as ArrayRef;
5256 let a1 = Arc::new(StringArray::from(vec![
5257 Some("2020-09-08T12:00:00.123456789+00:00"),
5258 Some("Not a valid date"),
5259 None,
5260 ])) as ArrayRef;
5261 let a2 = Arc::new(LargeStringArray::from(vec![
5262 Some("2020-09-08T12:00:00.123456789+00:00"),
5263 Some("Not a valid date"),
5264 None,
5265 ])) as ArrayRef;
5266 for array in &[a0, a1, a2] {
5267 for time_unit in &[
5268 TimeUnit::Second,
5269 TimeUnit::Millisecond,
5270 TimeUnit::Microsecond,
5271 TimeUnit::Nanosecond,
5272 ] {
5273 let to_type = DataType::Timestamp(*time_unit, None);
5274 let b = cast(array, &to_type).unwrap();
5275
5276 match time_unit {
5277 TimeUnit::Second => {
5278 let c = b.as_primitive::<TimestampSecondType>();
5279 assert_eq!(1599566400, c.value(0));
5280 assert!(c.is_null(1));
5281 assert!(c.is_null(2));
5282 }
5283 TimeUnit::Millisecond => {
5284 let c = b
5285 .as_any()
5286 .downcast_ref::<TimestampMillisecondArray>()
5287 .unwrap();
5288 assert_eq!(1599566400123, c.value(0));
5289 assert!(c.is_null(1));
5290 assert!(c.is_null(2));
5291 }
5292 TimeUnit::Microsecond => {
5293 let c = b
5294 .as_any()
5295 .downcast_ref::<TimestampMicrosecondArray>()
5296 .unwrap();
5297 assert_eq!(1599566400123456, c.value(0));
5298 assert!(c.is_null(1));
5299 assert!(c.is_null(2));
5300 }
5301 TimeUnit::Nanosecond => {
5302 let c = b
5303 .as_any()
5304 .downcast_ref::<TimestampNanosecondArray>()
5305 .unwrap();
5306 assert_eq!(1599566400123456789, c.value(0));
5307 assert!(c.is_null(1));
5308 assert!(c.is_null(2));
5309 }
5310 }
5311
5312 let options = CastOptions {
5313 safe: false,
5314 format_options: FormatOptions::default(),
5315 };
5316 let err = cast_with_options(array, &to_type, &options).unwrap_err();
5317 assert_eq!(
5318 err.to_string(),
5319 "Parser error: Error parsing timestamp from 'Not a valid date': error parsing date"
5320 );
5321 }
5322 }
5323 }
5324
5325 #[test]
5326 fn test_cast_string_to_timestamp_overflow() {
5327 let array = StringArray::from(vec!["9800-09-08T12:00:00.123456789"]);
5328 let result = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
5329 let result = result.as_primitive::<TimestampSecondType>();
5330 assert_eq!(result.values(), &[247112596800]);
5331 }
5332
5333 #[test]
5334 fn test_cast_string_to_date32() {
5335 let a0 = Arc::new(StringViewArray::from(vec![
5336 Some("2018-12-25"),
5337 Some("Not a valid date"),
5338 None,
5339 ])) as ArrayRef;
5340 let a1 = Arc::new(StringArray::from(vec![
5341 Some("2018-12-25"),
5342 Some("Not a valid date"),
5343 None,
5344 ])) as ArrayRef;
5345 let a2 = Arc::new(LargeStringArray::from(vec![
5346 Some("2018-12-25"),
5347 Some("Not a valid date"),
5348 None,
5349 ])) as ArrayRef;
5350 for array in &[a0, a1, a2] {
5351 let to_type = DataType::Date32;
5352 let b = cast(array, &to_type).unwrap();
5353 let c = b.as_primitive::<Date32Type>();
5354 assert_eq!(17890, c.value(0));
5355 assert!(c.is_null(1));
5356 assert!(c.is_null(2));
5357
5358 let options = CastOptions {
5359 safe: false,
5360 format_options: FormatOptions::default(),
5361 };
5362 let err = cast_with_options(array, &to_type, &options).unwrap_err();
5363 assert_eq!(
5364 err.to_string(),
5365 "Cast error: Cannot cast string 'Not a valid date' to value of Date32 type"
5366 );
5367 }
5368 }
5369
5370 #[test]
5371 fn test_cast_string_with_large_date_to_date32() {
5372 let array = Arc::new(StringArray::from(vec![
5373 Some("+10999-12-31"),
5374 Some("-0010-02-28"),
5375 Some("0010-02-28"),
5376 Some("0000-01-01"),
5377 Some("-0000-01-01"),
5378 Some("-0001-01-01"),
5379 ])) as ArrayRef;
5380 let to_type = DataType::Date32;
5381 let options = CastOptions {
5382 safe: false,
5383 format_options: FormatOptions::default(),
5384 };
5385 let b = cast_with_options(&array, &to_type, &options).unwrap();
5386 let c = b.as_primitive::<Date32Type>();
5387 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)); }
5395
5396 #[test]
5397 fn test_cast_invalid_string_with_large_date_to_date32() {
5398 let array = Arc::new(StringArray::from(vec![Some("10999-12-31")])) as ArrayRef;
5400 let to_type = DataType::Date32;
5401 let options = CastOptions {
5402 safe: false,
5403 format_options: FormatOptions::default(),
5404 };
5405 let err = cast_with_options(&array, &to_type, &options).unwrap_err();
5406 assert_eq!(
5407 err.to_string(),
5408 "Cast error: Cannot cast string '10999-12-31' to value of Date32 type"
5409 );
5410 }
5411
5412 #[test]
5413 fn test_cast_string_format_yyyymmdd_to_date32() {
5414 let a0 = Arc::new(StringViewArray::from(vec![
5415 Some("2020-12-25"),
5416 Some("20201117"),
5417 ])) as ArrayRef;
5418 let a1 = Arc::new(StringArray::from(vec![
5419 Some("2020-12-25"),
5420 Some("20201117"),
5421 ])) as ArrayRef;
5422 let a2 = Arc::new(LargeStringArray::from(vec![
5423 Some("2020-12-25"),
5424 Some("20201117"),
5425 ])) as ArrayRef;
5426
5427 for array in &[a0, a1, a2] {
5428 let to_type = DataType::Date32;
5429 let options = CastOptions {
5430 safe: false,
5431 format_options: FormatOptions::default(),
5432 };
5433 let result = cast_with_options(&array, &to_type, &options).unwrap();
5434 let c = result.as_primitive::<Date32Type>();
5435 assert_eq!(
5436 chrono::NaiveDate::from_ymd_opt(2020, 12, 25),
5437 c.value_as_date(0)
5438 );
5439 assert_eq!(
5440 chrono::NaiveDate::from_ymd_opt(2020, 11, 17),
5441 c.value_as_date(1)
5442 );
5443 }
5444 }
5445
5446 #[test]
5447 fn test_cast_string_to_time32second() {
5448 let a0 = Arc::new(StringViewArray::from(vec![
5449 Some("08:08:35.091323414"),
5450 Some("08:08:60.091323414"), Some("08:08:61.091323414"), Some("Not a valid time"),
5453 None,
5454 ])) as ArrayRef;
5455 let a1 = Arc::new(StringArray::from(vec![
5456 Some("08:08:35.091323414"),
5457 Some("08:08:60.091323414"), Some("08:08:61.091323414"), Some("Not a valid time"),
5460 None,
5461 ])) as ArrayRef;
5462 let a2 = Arc::new(LargeStringArray::from(vec![
5463 Some("08:08:35.091323414"),
5464 Some("08:08:60.091323414"), Some("08:08:61.091323414"), Some("Not a valid time"),
5467 None,
5468 ])) as ArrayRef;
5469 for array in &[a0, a1, a2] {
5470 let to_type = DataType::Time32(TimeUnit::Second);
5471 let b = cast(array, &to_type).unwrap();
5472 let c = b.as_primitive::<Time32SecondType>();
5473 assert_eq!(29315, c.value(0));
5474 assert_eq!(29340, c.value(1));
5475 assert!(c.is_null(2));
5476 assert!(c.is_null(3));
5477 assert!(c.is_null(4));
5478
5479 let options = CastOptions {
5480 safe: false,
5481 format_options: FormatOptions::default(),
5482 };
5483 let err = cast_with_options(array, &to_type, &options).unwrap_err();
5484 assert_eq!(
5485 err.to_string(),
5486 "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(s) type"
5487 );
5488 }
5489 }
5490
5491 #[test]
5492 fn test_cast_string_to_time32millisecond() {
5493 let a0 = Arc::new(StringViewArray::from(vec![
5494 Some("08:08:35.091323414"),
5495 Some("08:08:60.091323414"), Some("08:08:61.091323414"), Some("Not a valid time"),
5498 None,
5499 ])) as ArrayRef;
5500 let a1 = Arc::new(StringArray::from(vec![
5501 Some("08:08:35.091323414"),
5502 Some("08:08:60.091323414"), Some("08:08:61.091323414"), Some("Not a valid time"),
5505 None,
5506 ])) as ArrayRef;
5507 let a2 = Arc::new(LargeStringArray::from(vec![
5508 Some("08:08:35.091323414"),
5509 Some("08:08:60.091323414"), Some("08:08:61.091323414"), Some("Not a valid time"),
5512 None,
5513 ])) as ArrayRef;
5514 for array in &[a0, a1, a2] {
5515 let to_type = DataType::Time32(TimeUnit::Millisecond);
5516 let b = cast(array, &to_type).unwrap();
5517 let c = b.as_primitive::<Time32MillisecondType>();
5518 assert_eq!(29315091, c.value(0));
5519 assert_eq!(29340091, c.value(1));
5520 assert!(c.is_null(2));
5521 assert!(c.is_null(3));
5522 assert!(c.is_null(4));
5523
5524 let options = CastOptions {
5525 safe: false,
5526 format_options: FormatOptions::default(),
5527 };
5528 let err = cast_with_options(array, &to_type, &options).unwrap_err();
5529 assert_eq!(
5530 err.to_string(),
5531 "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(ms) type"
5532 );
5533 }
5534 }
5535
5536 #[test]
5537 fn test_cast_string_to_time64microsecond() {
5538 let a0 = Arc::new(StringViewArray::from(vec![
5539 Some("08:08:35.091323414"),
5540 Some("Not a valid time"),
5541 None,
5542 ])) as ArrayRef;
5543 let a1 = Arc::new(StringArray::from(vec![
5544 Some("08:08:35.091323414"),
5545 Some("Not a valid time"),
5546 None,
5547 ])) as ArrayRef;
5548 let a2 = Arc::new(LargeStringArray::from(vec![
5549 Some("08:08:35.091323414"),
5550 Some("Not a valid time"),
5551 None,
5552 ])) as ArrayRef;
5553 for array in &[a0, a1, a2] {
5554 let to_type = DataType::Time64(TimeUnit::Microsecond);
5555 let b = cast(array, &to_type).unwrap();
5556 let c = b.as_primitive::<Time64MicrosecondType>();
5557 assert_eq!(29315091323, c.value(0));
5558 assert!(c.is_null(1));
5559 assert!(c.is_null(2));
5560
5561 let options = CastOptions {
5562 safe: false,
5563 format_options: FormatOptions::default(),
5564 };
5565 let err = cast_with_options(array, &to_type, &options).unwrap_err();
5566 assert_eq!(
5567 err.to_string(),
5568 "Cast error: Cannot cast string 'Not a valid time' to value of Time64(µs) type"
5569 );
5570 }
5571 }
5572
5573 #[test]
5574 fn test_cast_string_to_time64nanosecond() {
5575 let a0 = Arc::new(StringViewArray::from(vec![
5576 Some("08:08:35.091323414"),
5577 Some("Not a valid time"),
5578 None,
5579 ])) as ArrayRef;
5580 let a1 = Arc::new(StringArray::from(vec![
5581 Some("08:08:35.091323414"),
5582 Some("Not a valid time"),
5583 None,
5584 ])) as ArrayRef;
5585 let a2 = Arc::new(LargeStringArray::from(vec![
5586 Some("08:08:35.091323414"),
5587 Some("Not a valid time"),
5588 None,
5589 ])) as ArrayRef;
5590 for array in &[a0, a1, a2] {
5591 let to_type = DataType::Time64(TimeUnit::Nanosecond);
5592 let b = cast(array, &to_type).unwrap();
5593 let c = b.as_primitive::<Time64NanosecondType>();
5594 assert_eq!(29315091323414, c.value(0));
5595 assert!(c.is_null(1));
5596 assert!(c.is_null(2));
5597
5598 let options = CastOptions {
5599 safe: false,
5600 format_options: FormatOptions::default(),
5601 };
5602 let err = cast_with_options(array, &to_type, &options).unwrap_err();
5603 assert_eq!(
5604 err.to_string(),
5605 "Cast error: Cannot cast string 'Not a valid time' to value of Time64(ns) type"
5606 );
5607 }
5608 }
5609
5610 #[test]
5611 fn test_cast_string_to_date64() {
5612 let a0 = Arc::new(StringViewArray::from(vec![
5613 Some("2020-09-08T12:00:00"),
5614 Some("Not a valid date"),
5615 None,
5616 ])) as ArrayRef;
5617 let a1 = Arc::new(StringArray::from(vec![
5618 Some("2020-09-08T12:00:00"),
5619 Some("Not a valid date"),
5620 None,
5621 ])) as ArrayRef;
5622 let a2 = Arc::new(LargeStringArray::from(vec![
5623 Some("2020-09-08T12:00:00"),
5624 Some("Not a valid date"),
5625 None,
5626 ])) as ArrayRef;
5627 for array in &[a0, a1, a2] {
5628 let to_type = DataType::Date64;
5629 let b = cast(array, &to_type).unwrap();
5630 let c = b.as_primitive::<Date64Type>();
5631 assert_eq!(1599566400000, c.value(0));
5632 assert!(c.is_null(1));
5633 assert!(c.is_null(2));
5634
5635 let options = CastOptions {
5636 safe: false,
5637 format_options: FormatOptions::default(),
5638 };
5639 let err = cast_with_options(array, &to_type, &options).unwrap_err();
5640 assert_eq!(
5641 err.to_string(),
5642 "Cast error: Cannot cast string 'Not a valid date' to value of Date64 type"
5643 );
5644 }
5645 }
5646
5647 macro_rules! test_safe_string_to_interval {
5648 ($data_vec:expr, $interval_unit:expr, $array_ty:ty, $expect_vec:expr) => {
5649 let source_string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
5650
5651 let options = CastOptions {
5652 safe: true,
5653 format_options: FormatOptions::default(),
5654 };
5655
5656 let target_interval_array = cast_with_options(
5657 &source_string_array.clone(),
5658 &DataType::Interval($interval_unit),
5659 &options,
5660 )
5661 .unwrap()
5662 .as_any()
5663 .downcast_ref::<$array_ty>()
5664 .unwrap()
5665 .clone() as $array_ty;
5666
5667 let target_string_array =
5668 cast_with_options(&target_interval_array, &DataType::Utf8, &options)
5669 .unwrap()
5670 .as_any()
5671 .downcast_ref::<StringArray>()
5672 .unwrap()
5673 .clone();
5674
5675 let expect_string_array = StringArray::from($expect_vec);
5676
5677 assert_eq!(target_string_array, expect_string_array);
5678
5679 let target_large_string_array =
5680 cast_with_options(&target_interval_array, &DataType::LargeUtf8, &options)
5681 .unwrap()
5682 .as_any()
5683 .downcast_ref::<LargeStringArray>()
5684 .unwrap()
5685 .clone();
5686
5687 let expect_large_string_array = LargeStringArray::from($expect_vec);
5688
5689 assert_eq!(target_large_string_array, expect_large_string_array);
5690 };
5691 }
5692
5693 #[test]
5694 fn test_cast_string_to_interval_year_month() {
5695 test_safe_string_to_interval!(
5696 vec![
5697 Some("1 year 1 month"),
5698 Some("1.5 years 13 month"),
5699 Some("30 days"),
5700 Some("31 days"),
5701 Some("2 months 31 days"),
5702 Some("2 months 31 days 1 second"),
5703 Some("foobar"),
5704 ],
5705 IntervalUnit::YearMonth,
5706 IntervalYearMonthArray,
5707 vec![
5708 Some("1 years 1 mons"),
5709 Some("2 years 7 mons"),
5710 None,
5711 None,
5712 None,
5713 None,
5714 None,
5715 ]
5716 );
5717 }
5718
5719 #[test]
5720 fn test_cast_string_to_interval_day_time() {
5721 test_safe_string_to_interval!(
5722 vec![
5723 Some("1 year 1 month"),
5724 Some("1.5 years 13 month"),
5725 Some("30 days"),
5726 Some("1 day 2 second 3.5 milliseconds"),
5727 Some("foobar"),
5728 ],
5729 IntervalUnit::DayTime,
5730 IntervalDayTimeArray,
5731 vec![
5732 Some("390 days"),
5733 Some("930 days"),
5734 Some("30 days"),
5735 None,
5736 None,
5737 ]
5738 );
5739 }
5740
5741 #[test]
5742 fn test_cast_string_to_interval_month_day_nano() {
5743 test_safe_string_to_interval!(
5744 vec![
5745 Some("1 year 1 month 1 day"),
5746 None,
5747 Some("1.5 years 13 month 35 days 1.4 milliseconds"),
5748 Some("3 days"),
5749 Some("8 seconds"),
5750 None,
5751 Some("1 day 29800 milliseconds"),
5752 Some("3 months 1 second"),
5753 Some("6 minutes 120 second"),
5754 Some("2 years 39 months 9 days 19 hours 1 minute 83 seconds 399222 milliseconds"),
5755 Some("foobar"),
5756 ],
5757 IntervalUnit::MonthDayNano,
5758 IntervalMonthDayNanoArray,
5759 vec![
5760 Some("13 mons 1 days"),
5761 None,
5762 Some("31 mons 35 days 0.001400000 secs"),
5763 Some("3 days"),
5764 Some("8.000000000 secs"),
5765 None,
5766 Some("1 days 29.800000000 secs"),
5767 Some("3 mons 1.000000000 secs"),
5768 Some("8 mins"),
5769 Some("63 mons 9 days 19 hours 9 mins 2.222000000 secs"),
5770 None,
5771 ]
5772 );
5773 }
5774
5775 macro_rules! test_unsafe_string_to_interval_err {
5776 ($data_vec:expr, $interval_unit:expr, $error_msg:expr) => {
5777 let string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
5778 let options = CastOptions {
5779 safe: false,
5780 format_options: FormatOptions::default(),
5781 };
5782 let arrow_err = cast_with_options(
5783 &string_array.clone(),
5784 &DataType::Interval($interval_unit),
5785 &options,
5786 )
5787 .unwrap_err();
5788 assert_eq!($error_msg, arrow_err.to_string());
5789 };
5790 }
5791
5792 #[test]
5793 fn test_cast_string_to_interval_err() {
5794 test_unsafe_string_to_interval_err!(
5795 vec![Some("foobar")],
5796 IntervalUnit::YearMonth,
5797 r#"Parser error: Invalid input syntax for type interval: "foobar""#
5798 );
5799 test_unsafe_string_to_interval_err!(
5800 vec![Some("foobar")],
5801 IntervalUnit::DayTime,
5802 r#"Parser error: Invalid input syntax for type interval: "foobar""#
5803 );
5804 test_unsafe_string_to_interval_err!(
5805 vec![Some("foobar")],
5806 IntervalUnit::MonthDayNano,
5807 r#"Parser error: Invalid input syntax for type interval: "foobar""#
5808 );
5809 test_unsafe_string_to_interval_err!(
5810 vec![Some("2 months 31 days 1 second")],
5811 IntervalUnit::YearMonth,
5812 r#"Cast error: Cannot cast 2 months 31 days 1 second to IntervalYearMonth. Only year and month fields are allowed."#
5813 );
5814 test_unsafe_string_to_interval_err!(
5815 vec![Some("1 day 1.5 milliseconds")],
5816 IntervalUnit::DayTime,
5817 r#"Cast error: Cannot cast 1 day 1.5 milliseconds to IntervalDayTime because the nanos part isn't multiple of milliseconds"#
5818 );
5819
5820 test_unsafe_string_to_interval_err!(
5822 vec![Some(format!(
5823 "{} century {} year {} month",
5824 i64::MAX - 2,
5825 i64::MAX - 2,
5826 i64::MAX - 2
5827 ))],
5828 IntervalUnit::DayTime,
5829 format!(
5830 "Arithmetic overflow: Overflow happened on: {} * 100",
5831 i64::MAX - 2
5832 )
5833 );
5834 test_unsafe_string_to_interval_err!(
5835 vec![Some(format!(
5836 "{} year {} month {} day",
5837 i64::MAX - 2,
5838 i64::MAX - 2,
5839 i64::MAX - 2
5840 ))],
5841 IntervalUnit::MonthDayNano,
5842 format!(
5843 "Arithmetic overflow: Overflow happened on: {} * 12",
5844 i64::MAX - 2
5845 )
5846 );
5847 }
5848
5849 #[test]
5850 fn test_cast_binary_to_fixed_size_binary() {
5851 let bytes_1 = "Hiiii".as_bytes();
5852 let bytes_2 = "Hello".as_bytes();
5853
5854 let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5855 let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
5856 let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
5857
5858 let array_ref = cast(&a1, &DataType::FixedSizeBinary(5)).unwrap();
5859 let down_cast = array_ref
5860 .as_any()
5861 .downcast_ref::<FixedSizeBinaryArray>()
5862 .unwrap();
5863 assert_eq!(bytes_1, down_cast.value(0));
5864 assert_eq!(bytes_2, down_cast.value(1));
5865 assert!(down_cast.is_null(2));
5866
5867 let array_ref = cast(&a2, &DataType::FixedSizeBinary(5)).unwrap();
5868 let down_cast = array_ref
5869 .as_any()
5870 .downcast_ref::<FixedSizeBinaryArray>()
5871 .unwrap();
5872 assert_eq!(bytes_1, down_cast.value(0));
5873 assert_eq!(bytes_2, down_cast.value(1));
5874 assert!(down_cast.is_null(2));
5875
5876 let bytes_1 = "Hi".as_bytes();
5878 let bytes_2 = "Hello".as_bytes();
5879
5880 let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5881 let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
5882 let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
5883
5884 let array_ref = cast_with_options(
5885 &a1,
5886 &DataType::FixedSizeBinary(5),
5887 &CastOptions {
5888 safe: false,
5889 format_options: FormatOptions::default(),
5890 },
5891 );
5892 assert!(array_ref.is_err());
5893
5894 let array_ref = cast_with_options(
5895 &a2,
5896 &DataType::FixedSizeBinary(5),
5897 &CastOptions {
5898 safe: false,
5899 format_options: FormatOptions::default(),
5900 },
5901 );
5902 assert!(array_ref.is_err());
5903 }
5904
5905 #[test]
5906 fn test_fixed_size_binary_to_binary() {
5907 let bytes_1 = "Hiiii".as_bytes();
5908 let bytes_2 = "Hello".as_bytes();
5909
5910 let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5911 let a1 = Arc::new(FixedSizeBinaryArray::from(binary_data.clone())) as ArrayRef;
5912
5913 let array_ref = cast(&a1, &DataType::Binary).unwrap();
5914 let down_cast = array_ref.as_binary::<i32>();
5915 assert_eq!(bytes_1, down_cast.value(0));
5916 assert_eq!(bytes_2, down_cast.value(1));
5917 assert!(down_cast.is_null(2));
5918
5919 let array_ref = cast(&a1, &DataType::LargeBinary).unwrap();
5920 let down_cast = array_ref.as_binary::<i64>();
5921 assert_eq!(bytes_1, down_cast.value(0));
5922 assert_eq!(bytes_2, down_cast.value(1));
5923 assert!(down_cast.is_null(2));
5924
5925 let array_ref = cast(&a1, &DataType::BinaryView).unwrap();
5926 let down_cast = array_ref.as_binary_view();
5927 assert_eq!(bytes_1, down_cast.value(0));
5928 assert_eq!(bytes_2, down_cast.value(1));
5929 assert!(down_cast.is_null(2));
5930 }
5931
5932 #[test]
5933 fn test_fixed_size_binary_to_dictionary() {
5934 let bytes_1 = "Hiiii".as_bytes();
5935 let bytes_2 = "Hello".as_bytes();
5936
5937 let binary_data = vec![Some(bytes_1), Some(bytes_2), Some(bytes_1), None];
5938 let a1 = Arc::new(FixedSizeBinaryArray::from(binary_data.clone())) as ArrayRef;
5939
5940 let cast_type = DataType::Dictionary(
5941 Box::new(DataType::Int8),
5942 Box::new(DataType::FixedSizeBinary(5)),
5943 );
5944 let cast_array = cast(&a1, &cast_type).unwrap();
5945 assert_eq!(cast_array.data_type(), &cast_type);
5946 assert_eq!(
5947 array_to_strings(&cast_array),
5948 vec!["4869696969", "48656c6c6f", "4869696969", "null"]
5949 );
5950 let dict_array = cast_array.as_dictionary::<Int8Type>();
5952 assert_eq!(dict_array.values().len(), 2);
5953 }
5954
5955 #[test]
5956 fn test_binary_to_dictionary() {
5957 let mut builder = GenericBinaryBuilder::<i32>::new();
5958 builder.append_value(b"hello");
5959 builder.append_value(b"hiiii");
5960 builder.append_value(b"hiiii"); builder.append_null();
5962 builder.append_value(b"rustt");
5963
5964 let a1 = builder.finish();
5965
5966 let cast_type = DataType::Dictionary(
5967 Box::new(DataType::Int8),
5968 Box::new(DataType::FixedSizeBinary(5)),
5969 );
5970 let cast_array = cast(&a1, &cast_type).unwrap();
5971 assert_eq!(cast_array.data_type(), &cast_type);
5972 assert_eq!(
5973 array_to_strings(&cast_array),
5974 vec![
5975 "68656c6c6f",
5976 "6869696969",
5977 "6869696969",
5978 "null",
5979 "7275737474"
5980 ]
5981 );
5982 let dict_array = cast_array.as_dictionary::<Int8Type>();
5984 assert_eq!(dict_array.values().len(), 3);
5985 }
5986
5987 #[test]
5988 fn test_cast_string_array_to_dict_utf8_view() {
5989 let array = StringArray::from(vec![Some("one"), None, Some("three"), Some("one")]);
5990
5991 let cast_type =
5992 DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
5993 assert!(can_cast_types(array.data_type(), &cast_type));
5994 let cast_array = cast(&array, &cast_type).unwrap();
5995 assert_eq!(cast_array.data_type(), &cast_type);
5996
5997 let dict_array = cast_array.as_dictionary::<UInt16Type>();
5998 assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
5999 assert_eq!(dict_array.values().len(), 2); let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6002 let actual: Vec<Option<&str>> = typed.into_iter().collect();
6003 assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
6004
6005 let keys = dict_array.keys();
6006 assert!(keys.is_null(1));
6007 assert_eq!(keys.value(0), keys.value(3));
6008 assert_ne!(keys.value(0), keys.value(2));
6009 }
6010
6011 #[test]
6012 fn test_cast_string_array_to_dict_utf8_view_null_vs_literal_null() {
6013 let array = StringArray::from(vec![Some("one"), None, Some("null"), Some("one")]);
6014
6015 let cast_type =
6016 DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6017 assert!(can_cast_types(array.data_type(), &cast_type));
6018 let cast_array = cast(&array, &cast_type).unwrap();
6019 assert_eq!(cast_array.data_type(), &cast_type);
6020
6021 let dict_array = cast_array.as_dictionary::<UInt16Type>();
6022 assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6023 assert_eq!(dict_array.values().len(), 2);
6024
6025 let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6026 let actual: Vec<Option<&str>> = typed.into_iter().collect();
6027 assert_eq!(actual, vec![Some("one"), None, Some("null"), Some("one")]);
6028
6029 let keys = dict_array.keys();
6030 assert!(keys.is_null(1));
6031 assert_eq!(keys.value(0), keys.value(3));
6032 assert_ne!(keys.value(0), keys.value(2));
6033 }
6034
6035 #[test]
6036 fn test_cast_string_view_array_to_dict_utf8_view() {
6037 let array = StringViewArray::from(vec![Some("one"), None, Some("three"), Some("one")]);
6038
6039 let cast_type =
6040 DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6041 assert!(can_cast_types(array.data_type(), &cast_type));
6042 let cast_array = cast(&array, &cast_type).unwrap();
6043 assert_eq!(cast_array.data_type(), &cast_type);
6044
6045 let dict_array = cast_array.as_dictionary::<UInt16Type>();
6046 assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6047 assert_eq!(dict_array.values().len(), 2); let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6050 let actual: Vec<Option<&str>> = typed.into_iter().collect();
6051 assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
6052
6053 let keys = dict_array.keys();
6054 assert!(keys.is_null(1));
6055 assert_eq!(keys.value(0), keys.value(3));
6056 assert_ne!(keys.value(0), keys.value(2));
6057 }
6058
6059 #[test]
6060 fn test_cast_string_view_slice_to_dict_utf8_view() {
6061 let array = StringViewArray::from(vec![
6062 Some("zero"),
6063 Some("one"),
6064 None,
6065 Some("three"),
6066 Some("one"),
6067 ]);
6068 let view = array.slice(1, 4);
6069
6070 let cast_type =
6071 DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6072 assert!(can_cast_types(view.data_type(), &cast_type));
6073 let cast_array = cast(&view, &cast_type).unwrap();
6074 assert_eq!(cast_array.data_type(), &cast_type);
6075
6076 let dict_array = cast_array.as_dictionary::<UInt16Type>();
6077 assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6078 assert_eq!(dict_array.values().len(), 2);
6079
6080 let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6081 let actual: Vec<Option<&str>> = typed.into_iter().collect();
6082 assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
6083
6084 let keys = dict_array.keys();
6085 assert!(keys.is_null(1));
6086 assert_eq!(keys.value(0), keys.value(3));
6087 assert_ne!(keys.value(0), keys.value(2));
6088 }
6089
6090 #[test]
6091 fn test_cast_binary_array_to_dict_binary_view() {
6092 let mut builder = GenericBinaryBuilder::<i32>::new();
6093 builder.append_value(b"hello");
6094 builder.append_value(b"hiiii");
6095 builder.append_value(b"hiiii"); builder.append_null();
6097 builder.append_value(b"rustt");
6098
6099 let array = builder.finish();
6100
6101 let cast_type =
6102 DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6103 assert!(can_cast_types(array.data_type(), &cast_type));
6104 let cast_array = cast(&array, &cast_type).unwrap();
6105 assert_eq!(cast_array.data_type(), &cast_type);
6106
6107 let dict_array = cast_array.as_dictionary::<UInt16Type>();
6108 assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
6109 assert_eq!(dict_array.values().len(), 3);
6110
6111 let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
6112 let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6113 assert_eq!(
6114 actual,
6115 vec![
6116 Some(b"hello".as_slice()),
6117 Some(b"hiiii".as_slice()),
6118 Some(b"hiiii".as_slice()),
6119 None,
6120 Some(b"rustt".as_slice())
6121 ]
6122 );
6123
6124 let keys = dict_array.keys();
6125 assert!(keys.is_null(3));
6126 assert_eq!(keys.value(1), keys.value(2));
6127 assert_ne!(keys.value(0), keys.value(1));
6128 }
6129
6130 #[test]
6131 fn test_cast_binary_view_array_to_dict_binary_view() {
6132 let view = BinaryViewArray::from_iter([
6133 Some(b"hello".as_slice()),
6134 Some(b"hiiii".as_slice()),
6135 Some(b"hiiii".as_slice()), None,
6137 Some(b"rustt".as_slice()),
6138 ]);
6139
6140 let cast_type =
6141 DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6142 assert!(can_cast_types(view.data_type(), &cast_type));
6143 let cast_array = cast(&view, &cast_type).unwrap();
6144 assert_eq!(cast_array.data_type(), &cast_type);
6145
6146 let dict_array = cast_array.as_dictionary::<UInt16Type>();
6147 assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
6148 assert_eq!(dict_array.values().len(), 3);
6149
6150 let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
6151 let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6152 assert_eq!(
6153 actual,
6154 vec![
6155 Some(b"hello".as_slice()),
6156 Some(b"hiiii".as_slice()),
6157 Some(b"hiiii".as_slice()),
6158 None,
6159 Some(b"rustt".as_slice())
6160 ]
6161 );
6162
6163 let keys = dict_array.keys();
6164 assert!(keys.is_null(3));
6165 assert_eq!(keys.value(1), keys.value(2));
6166 assert_ne!(keys.value(0), keys.value(1));
6167 }
6168
6169 #[test]
6170 fn test_cast_binary_view_slice_to_dict_binary_view() {
6171 let view = BinaryViewArray::from_iter([
6172 Some(b"hello".as_slice()),
6173 Some(b"hiiii".as_slice()),
6174 Some(b"hiiii".as_slice()), None,
6176 Some(b"rustt".as_slice()),
6177 ]);
6178 let sliced = view.slice(1, 4);
6179
6180 let cast_type =
6181 DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6182 assert!(can_cast_types(sliced.data_type(), &cast_type));
6183 let cast_array = cast(&sliced, &cast_type).unwrap();
6184 assert_eq!(cast_array.data_type(), &cast_type);
6185
6186 let dict_array = cast_array.as_dictionary::<UInt16Type>();
6187 assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
6188 assert_eq!(dict_array.values().len(), 2);
6189
6190 let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
6191 let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6192 assert_eq!(
6193 actual,
6194 vec![
6195 Some(b"hiiii".as_slice()),
6196 Some(b"hiiii".as_slice()),
6197 None,
6198 Some(b"rustt".as_slice())
6199 ]
6200 );
6201
6202 let keys = dict_array.keys();
6203 assert!(keys.is_null(2));
6204 assert_eq!(keys.value(0), keys.value(1));
6205 assert_ne!(keys.value(0), keys.value(3));
6206 }
6207
6208 #[test]
6209 fn test_cast_string_array_to_dict_utf8_view_key_overflow_u8() {
6210 let array = StringArray::from_iter_values((0..257).map(|i| format!("v{i}")));
6211
6212 let cast_type =
6213 DataType::Dictionary(Box::new(DataType::UInt8), Box::new(DataType::Utf8View));
6214 assert!(can_cast_types(array.data_type(), &cast_type));
6215 let err = cast(&array, &cast_type).unwrap_err();
6216 assert!(matches!(err, ArrowError::DictionaryKeyOverflowError));
6217 }
6218
6219 #[test]
6220 fn test_cast_large_string_array_to_dict_utf8_view() {
6221 let array = LargeStringArray::from(vec![Some("one"), None, Some("three"), Some("one")]);
6222
6223 let cast_type =
6224 DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6225 assert!(can_cast_types(array.data_type(), &cast_type));
6226 let cast_array = cast(&array, &cast_type).unwrap();
6227 assert_eq!(cast_array.data_type(), &cast_type);
6228
6229 let dict_array = cast_array.as_dictionary::<UInt16Type>();
6230 assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6231 assert_eq!(dict_array.values().len(), 2); let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6234 let actual: Vec<Option<&str>> = typed.into_iter().collect();
6235 assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
6236
6237 let keys = dict_array.keys();
6238 assert!(keys.is_null(1));
6239 assert_eq!(keys.value(0), keys.value(3));
6240 assert_ne!(keys.value(0), keys.value(2));
6241 }
6242
6243 #[test]
6244 fn test_cast_large_binary_array_to_dict_binary_view() {
6245 let mut builder = GenericBinaryBuilder::<i64>::new();
6246 builder.append_value(b"hello");
6247 builder.append_value(b"world");
6248 builder.append_value(b"hello"); builder.append_null();
6250
6251 let array = builder.finish();
6252
6253 let cast_type =
6254 DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6255 assert!(can_cast_types(array.data_type(), &cast_type));
6256 let cast_array = cast(&array, &cast_type).unwrap();
6257 assert_eq!(cast_array.data_type(), &cast_type);
6258
6259 let dict_array = cast_array.as_dictionary::<UInt16Type>();
6260 assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
6261 assert_eq!(dict_array.values().len(), 2); let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
6264 let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6265 assert_eq!(
6266 actual,
6267 vec![
6268 Some(b"hello".as_slice()),
6269 Some(b"world".as_slice()),
6270 Some(b"hello".as_slice()),
6271 None
6272 ]
6273 );
6274
6275 let keys = dict_array.keys();
6276 assert!(keys.is_null(3));
6277 assert_eq!(keys.value(0), keys.value(2));
6278 assert_ne!(keys.value(0), keys.value(1));
6279 }
6280
6281 #[test]
6282 fn test_cast_empty_string_array_to_dict_utf8_view() {
6283 let array = StringArray::from(Vec::<Option<&str>>::new());
6284
6285 let cast_type =
6286 DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6287 assert!(can_cast_types(array.data_type(), &cast_type));
6288 let cast_array = cast(&array, &cast_type).unwrap();
6289 assert_eq!(cast_array.data_type(), &cast_type);
6290 assert_eq!(cast_array.len(), 0);
6291 }
6292
6293 #[test]
6294 fn test_cast_empty_binary_array_to_dict_binary_view() {
6295 let array = BinaryArray::from(Vec::<Option<&[u8]>>::new());
6296
6297 let cast_type =
6298 DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6299 assert!(can_cast_types(array.data_type(), &cast_type));
6300 let cast_array = cast(&array, &cast_type).unwrap();
6301 assert_eq!(cast_array.data_type(), &cast_type);
6302 assert_eq!(cast_array.len(), 0);
6303 }
6304
6305 #[test]
6306 fn test_cast_all_null_string_array_to_dict_utf8_view() {
6307 let array = StringArray::from(vec![None::<&str>, None, None]);
6308
6309 let cast_type =
6310 DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6311 assert!(can_cast_types(array.data_type(), &cast_type));
6312 let cast_array = cast(&array, &cast_type).unwrap();
6313 assert_eq!(cast_array.data_type(), &cast_type);
6314 assert_eq!(cast_array.null_count(), 3);
6315
6316 let dict_array = cast_array.as_dictionary::<UInt16Type>();
6317 assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6318 assert_eq!(dict_array.values().len(), 0);
6319 assert_eq!(dict_array.keys().null_count(), 3);
6320
6321 let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6322 let actual: Vec<Option<&str>> = typed.into_iter().collect();
6323 assert_eq!(actual, vec![None, None, None]);
6324 }
6325
6326 #[test]
6327 fn test_cast_all_null_binary_array_to_dict_binary_view() {
6328 let array = BinaryArray::from(vec![None::<&[u8]>, None, None]);
6329
6330 let cast_type =
6331 DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6332 assert!(can_cast_types(array.data_type(), &cast_type));
6333 let cast_array = cast(&array, &cast_type).unwrap();
6334 assert_eq!(cast_array.data_type(), &cast_type);
6335 assert_eq!(cast_array.null_count(), 3);
6336
6337 let dict_array = cast_array.as_dictionary::<UInt16Type>();
6338 assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
6339 assert_eq!(dict_array.values().len(), 0);
6340 assert_eq!(dict_array.keys().null_count(), 3);
6341
6342 let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
6343 let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6344 assert_eq!(actual, vec![None, None, None]);
6345 }
6346
6347 #[test]
6348 fn test_numeric_to_binary() {
6349 let a = Int16Array::from(vec![Some(1), Some(511), None]);
6350
6351 let array_ref = cast(&a, &DataType::Binary).unwrap();
6352 let down_cast = array_ref.as_binary::<i32>();
6353 assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
6354 assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
6355 assert!(down_cast.is_null(2));
6356
6357 let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
6358
6359 let array_ref = cast(&a, &DataType::Binary).unwrap();
6360 let down_cast = array_ref.as_binary::<i32>();
6361 assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
6362 assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
6363 assert!(down_cast.is_null(2));
6364 }
6365
6366 #[test]
6367 fn test_numeric_to_large_binary() {
6368 let a = Int16Array::from(vec![Some(1), Some(511), None]);
6369
6370 let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
6371 let down_cast = array_ref.as_binary::<i64>();
6372 assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
6373 assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
6374 assert!(down_cast.is_null(2));
6375
6376 let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
6377
6378 let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
6379 let down_cast = array_ref.as_binary::<i64>();
6380 assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
6381 assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
6382 assert!(down_cast.is_null(2));
6383 }
6384
6385 #[test]
6386 fn test_cast_date32_to_int32() {
6387 let array = Date32Array::from(vec![10000, 17890]);
6388 let b = cast(&array, &DataType::Int32).unwrap();
6389 let c = b.as_primitive::<Int32Type>();
6390 assert_eq!(10000, c.value(0));
6391 assert_eq!(17890, c.value(1));
6392 }
6393
6394 #[test]
6395 fn test_cast_int32_to_date32() {
6396 let array = Int32Array::from(vec![10000, 17890]);
6397 let b = cast(&array, &DataType::Date32).unwrap();
6398 let c = b.as_primitive::<Date32Type>();
6399 assert_eq!(10000, c.value(0));
6400 assert_eq!(17890, c.value(1));
6401 }
6402
6403 #[test]
6404 fn test_cast_timestamp_to_date32() {
6405 let array =
6406 TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
6407 .with_timezone("+00:00".to_string());
6408 let b = cast(&array, &DataType::Date32).unwrap();
6409 let c = b.as_primitive::<Date32Type>();
6410 assert_eq!(10000, c.value(0));
6411 assert_eq!(17890, c.value(1));
6412 assert!(c.is_null(2));
6413 }
6414 #[test]
6415 fn test_cast_timestamp_to_date32_zone() {
6416 let strings = StringArray::from_iter([
6417 Some("1970-01-01T00:00:01"),
6418 Some("1970-01-01T23:59:59"),
6419 None,
6420 Some("2020-03-01T02:00:23+00:00"),
6421 ]);
6422 let dt = DataType::Timestamp(TimeUnit::Millisecond, Some("-07:00".into()));
6423 let timestamps = cast(&strings, &dt).unwrap();
6424 let dates = cast(timestamps.as_ref(), &DataType::Date32).unwrap();
6425
6426 let c = dates.as_primitive::<Date32Type>();
6427 let expected = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap();
6428 assert_eq!(c.value_as_date(0).unwrap(), expected);
6429 assert_eq!(c.value_as_date(1).unwrap(), expected);
6430 assert!(c.is_null(2));
6431 let expected = NaiveDate::from_ymd_opt(2020, 2, 29).unwrap();
6432 assert_eq!(c.value_as_date(3).unwrap(), expected);
6433 }
6434 #[test]
6435 fn test_cast_timestamp_to_date64() {
6436 let array =
6437 TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None]);
6438 let b = cast(&array, &DataType::Date64).unwrap();
6439 let c = b.as_primitive::<Date64Type>();
6440 assert_eq!(864000000005, c.value(0));
6441 assert_eq!(1545696000001, c.value(1));
6442 assert!(c.is_null(2));
6443
6444 let array = TimestampSecondArray::from(vec![Some(864000000005), Some(1545696000001)]);
6445 let b = cast(&array, &DataType::Date64).unwrap();
6446 let c = b.as_primitive::<Date64Type>();
6447 assert_eq!(864000000005000, c.value(0));
6448 assert_eq!(1545696000001000, c.value(1));
6449
6450 let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
6452 let b = cast(&array, &DataType::Date64).unwrap();
6453 assert!(b.is_null(0));
6454 let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
6456 let options = CastOptions {
6457 safe: false,
6458 format_options: FormatOptions::default(),
6459 };
6460 let b = cast_with_options(&array, &DataType::Date64, &options);
6461 assert!(b.is_err());
6462 }
6463
6464 #[test]
6465 fn test_cast_timestamp_to_time64() {
6466 let array = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
6468 .with_timezone("+01:00".to_string());
6469 let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6470 let c = b.as_primitive::<Time64MicrosecondType>();
6471 assert_eq!(3605000000, c.value(0));
6472 assert_eq!(3601000000, c.value(1));
6473 assert!(c.is_null(2));
6474 let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6475 let c = b.as_primitive::<Time64NanosecondType>();
6476 assert_eq!(3605000000000, c.value(0));
6477 assert_eq!(3601000000000, c.value(1));
6478 assert!(c.is_null(2));
6479
6480 let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
6482 .with_timezone("+01:00".to_string());
6483 let array = Arc::new(a) as ArrayRef;
6484 let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6485 let c = b.as_primitive::<Time64MicrosecondType>();
6486 assert_eq!(3605000000, c.value(0));
6487 assert_eq!(3601000000, c.value(1));
6488 assert!(c.is_null(2));
6489 let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6490 let c = b.as_primitive::<Time64NanosecondType>();
6491 assert_eq!(3605000000000, c.value(0));
6492 assert_eq!(3601000000000, c.value(1));
6493 assert!(c.is_null(2));
6494
6495 let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
6497 .with_timezone("+01:00".to_string());
6498 let array = Arc::new(a) as ArrayRef;
6499 let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6500 let c = b.as_primitive::<Time64MicrosecondType>();
6501 assert_eq!(3605000000, c.value(0));
6502 assert_eq!(3601000000, c.value(1));
6503 assert!(c.is_null(2));
6504 let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6505 let c = b.as_primitive::<Time64NanosecondType>();
6506 assert_eq!(3605000000000, c.value(0));
6507 assert_eq!(3601000000000, c.value(1));
6508 assert!(c.is_null(2));
6509
6510 let a = TimestampNanosecondArray::from(vec![Some(86405000000000), Some(1000000000), None])
6512 .with_timezone("+01:00".to_string());
6513 let array = Arc::new(a) as ArrayRef;
6514 let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6515 let c = b.as_primitive::<Time64MicrosecondType>();
6516 assert_eq!(3605000000, c.value(0));
6517 assert_eq!(3601000000, c.value(1));
6518 assert!(c.is_null(2));
6519 let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6520 let c = b.as_primitive::<Time64NanosecondType>();
6521 assert_eq!(3605000000000, c.value(0));
6522 assert_eq!(3601000000000, c.value(1));
6523 assert!(c.is_null(2));
6524
6525 let a =
6527 TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
6528 let array = Arc::new(a) as ArrayRef;
6529 let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond));
6530 assert!(b.is_err());
6531 let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond));
6532 assert!(b.is_err());
6533 let b = cast(&array, &DataType::Time64(TimeUnit::Millisecond));
6534 assert!(b.is_err());
6535 }
6536
6537 #[test]
6538 fn test_cast_timestamp_to_time32() {
6539 let a = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
6541 .with_timezone("+01:00".to_string());
6542 let array = Arc::new(a) as ArrayRef;
6543 let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6544 let c = b.as_primitive::<Time32SecondType>();
6545 assert_eq!(3605, c.value(0));
6546 assert_eq!(3601, c.value(1));
6547 assert!(c.is_null(2));
6548 let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6549 let c = b.as_primitive::<Time32MillisecondType>();
6550 assert_eq!(3605000, c.value(0));
6551 assert_eq!(3601000, c.value(1));
6552 assert!(c.is_null(2));
6553
6554 let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
6556 .with_timezone("+01:00".to_string());
6557 let array = Arc::new(a) as ArrayRef;
6558 let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6559 let c = b.as_primitive::<Time32SecondType>();
6560 assert_eq!(3605, c.value(0));
6561 assert_eq!(3601, c.value(1));
6562 assert!(c.is_null(2));
6563 let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6564 let c = b.as_primitive::<Time32MillisecondType>();
6565 assert_eq!(3605000, c.value(0));
6566 assert_eq!(3601000, c.value(1));
6567 assert!(c.is_null(2));
6568
6569 let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
6571 .with_timezone("+01:00".to_string());
6572 let array = Arc::new(a) as ArrayRef;
6573 let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6574 let c = b.as_primitive::<Time32SecondType>();
6575 assert_eq!(3605, c.value(0));
6576 assert_eq!(3601, c.value(1));
6577 assert!(c.is_null(2));
6578 let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6579 let c = b.as_primitive::<Time32MillisecondType>();
6580 assert_eq!(3605000, c.value(0));
6581 assert_eq!(3601000, c.value(1));
6582 assert!(c.is_null(2));
6583
6584 let a = TimestampNanosecondArray::from(vec![Some(86405000000000), Some(1000000000), None])
6586 .with_timezone("+01:00".to_string());
6587 let array = Arc::new(a) as ArrayRef;
6588 let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6589 let c = b.as_primitive::<Time32SecondType>();
6590 assert_eq!(3605, c.value(0));
6591 assert_eq!(3601, c.value(1));
6592 assert!(c.is_null(2));
6593 let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6594 let c = b.as_primitive::<Time32MillisecondType>();
6595 assert_eq!(3605000, c.value(0));
6596 assert_eq!(3601000, c.value(1));
6597 assert!(c.is_null(2));
6598
6599 let a =
6601 TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
6602 let array = Arc::new(a) as ArrayRef;
6603 let b = cast(&array, &DataType::Time32(TimeUnit::Second));
6604 assert!(b.is_err());
6605 let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond));
6606 assert!(b.is_err());
6607 }
6608
6609 #[test]
6611 fn test_cast_timestamp_with_timezone_1() {
6612 let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
6613 Some("2000-01-01T00:00:00.123456789"),
6614 Some("2010-01-01T00:00:00.123456789"),
6615 None,
6616 ]));
6617 let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
6618 let timestamp_array = cast(&string_array, &to_type).unwrap();
6619
6620 let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
6621 let timestamp_array = cast(×tamp_array, &to_type).unwrap();
6622
6623 let string_array = cast(×tamp_array, &DataType::Utf8).unwrap();
6624 let result = string_array.as_string::<i32>();
6625 assert_eq!("2000-01-01T00:00:00.123456+07:00", result.value(0));
6626 assert_eq!("2010-01-01T00:00:00.123456+07:00", result.value(1));
6627 assert!(result.is_null(2));
6628 }
6629
6630 #[test]
6632 fn test_cast_timestamp_with_timezone_2() {
6633 let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
6634 Some("2000-01-01T07:00:00.123456789"),
6635 Some("2010-01-01T07:00:00.123456789"),
6636 None,
6637 ]));
6638 let to_type = DataType::Timestamp(TimeUnit::Millisecond, Some("+0700".into()));
6639 let timestamp_array = cast(&string_array, &to_type).unwrap();
6640
6641 let string_array = cast(×tamp_array, &DataType::Utf8).unwrap();
6643 let result = string_array.as_string::<i32>();
6644 assert_eq!("2000-01-01T07:00:00.123+07:00", result.value(0));
6645 assert_eq!("2010-01-01T07:00:00.123+07:00", result.value(1));
6646 assert!(result.is_null(2));
6647
6648 let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
6649 let timestamp_array = cast(×tamp_array, &to_type).unwrap();
6650
6651 let string_array = cast(×tamp_array, &DataType::Utf8).unwrap();
6652 let result = string_array.as_string::<i32>();
6653 assert_eq!("2000-01-01T00:00:00.123", result.value(0));
6654 assert_eq!("2010-01-01T00:00:00.123", result.value(1));
6655 assert!(result.is_null(2));
6656 }
6657
6658 #[test]
6660 fn test_cast_timestamp_with_timezone_3() {
6661 let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
6662 Some("2000-01-01T07:00:00.123456789"),
6663 Some("2010-01-01T07:00:00.123456789"),
6664 None,
6665 ]));
6666 let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
6667 let timestamp_array = cast(&string_array, &to_type).unwrap();
6668
6669 let string_array = cast(×tamp_array, &DataType::Utf8).unwrap();
6671 let result = string_array.as_string::<i32>();
6672 assert_eq!("2000-01-01T07:00:00.123456+07:00", result.value(0));
6673 assert_eq!("2010-01-01T07:00:00.123456+07:00", result.value(1));
6674 assert!(result.is_null(2));
6675
6676 let to_type = DataType::Timestamp(TimeUnit::Second, Some("-08:00".into()));
6677 let timestamp_array = cast(×tamp_array, &to_type).unwrap();
6678
6679 let string_array = cast(×tamp_array, &DataType::Utf8).unwrap();
6680 let result = string_array.as_string::<i32>();
6681 assert_eq!("1999-12-31T16:00:00-08:00", result.value(0));
6682 assert_eq!("2009-12-31T16:00:00-08:00", result.value(1));
6683 assert!(result.is_null(2));
6684 }
6685
6686 #[test]
6687 fn test_cast_date64_to_timestamp() {
6688 let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6689 let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
6690 let c = b.as_primitive::<TimestampSecondType>();
6691 assert_eq!(864000000, c.value(0));
6692 assert_eq!(1545696000, c.value(1));
6693 assert!(c.is_null(2));
6694 }
6695
6696 #[test]
6697 fn test_cast_date64_to_timestamp_ms() {
6698 let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6699 let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
6700 let c = b
6701 .as_any()
6702 .downcast_ref::<TimestampMillisecondArray>()
6703 .unwrap();
6704 assert_eq!(864000000005, c.value(0));
6705 assert_eq!(1545696000001, c.value(1));
6706 assert!(c.is_null(2));
6707 }
6708
6709 #[test]
6710 fn test_cast_date64_to_timestamp_us() {
6711 let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6712 let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
6713 let c = b
6714 .as_any()
6715 .downcast_ref::<TimestampMicrosecondArray>()
6716 .unwrap();
6717 assert_eq!(864000000005000, c.value(0));
6718 assert_eq!(1545696000001000, c.value(1));
6719 assert!(c.is_null(2));
6720 }
6721
6722 #[test]
6723 fn test_cast_date64_to_timestamp_ns() {
6724 let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6725 let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
6726 let c = b
6727 .as_any()
6728 .downcast_ref::<TimestampNanosecondArray>()
6729 .unwrap();
6730 assert_eq!(864000000005000000, c.value(0));
6731 assert_eq!(1545696000001000000, c.value(1));
6732 assert!(c.is_null(2));
6733 }
6734
6735 #[test]
6736 fn test_cast_timestamp_to_i64() {
6737 let array =
6738 TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
6739 .with_timezone("UTC".to_string());
6740 let b = cast(&array, &DataType::Int64).unwrap();
6741 let c = b.as_primitive::<Int64Type>();
6742 assert_eq!(&DataType::Int64, c.data_type());
6743 assert_eq!(864000000005, c.value(0));
6744 assert_eq!(1545696000001, c.value(1));
6745 assert!(c.is_null(2));
6746 }
6747
6748 macro_rules! assert_cast {
6749 ($array:expr, $datatype:expr, $output_array_type: ty, $expected:expr) => {{
6750 assert!(can_cast_types($array.data_type(), &$datatype));
6751 let out = cast(&$array, &$datatype).unwrap();
6752 let actual = out
6753 .as_any()
6754 .downcast_ref::<$output_array_type>()
6755 .unwrap()
6756 .into_iter()
6757 .collect::<Vec<_>>();
6758 assert_eq!(actual, $expected);
6759 }};
6760 ($array:expr, $datatype:expr, $output_array_type: ty, $options:expr, $expected:expr) => {{
6761 assert!(can_cast_types($array.data_type(), &$datatype));
6762 let out = cast_with_options(&$array, &$datatype, &$options).unwrap();
6763 let actual = out
6764 .as_any()
6765 .downcast_ref::<$output_array_type>()
6766 .unwrap()
6767 .into_iter()
6768 .collect::<Vec<_>>();
6769 assert_eq!(actual, $expected);
6770 }};
6771 }
6772
6773 #[test]
6774 fn test_cast_date32_to_string() {
6775 let array = Date32Array::from(vec![Some(0), Some(10000), Some(13036), Some(17890), None]);
6776 let expected = vec![
6777 Some("1970-01-01"),
6778 Some("1997-05-19"),
6779 Some("2005-09-10"),
6780 Some("2018-12-25"),
6781 None,
6782 ];
6783
6784 assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
6785 assert_cast!(array, DataType::Utf8, StringArray, expected);
6786 assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
6787 }
6788
6789 #[test]
6790 fn test_cast_date64_to_string() {
6791 let array = Date64Array::from(vec![
6792 Some(0),
6793 Some(10000 * 86400000),
6794 Some(13036 * 86400000),
6795 Some(17890 * 86400000),
6796 None,
6797 ]);
6798 let expected = vec![
6799 Some("1970-01-01T00:00:00"),
6800 Some("1997-05-19T00:00:00"),
6801 Some("2005-09-10T00:00:00"),
6802 Some("2018-12-25T00:00:00"),
6803 None,
6804 ];
6805
6806 assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
6807 assert_cast!(array, DataType::Utf8, StringArray, expected);
6808 assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
6809 }
6810
6811 #[test]
6812 fn test_cast_date32_to_timestamp_and_timestamp_with_timezone() {
6813 let tz = "+0545"; let a = Date32Array::from(vec![Some(18628), None, None]); let array = Arc::new(a) as ArrayRef;
6816
6817 let b = cast(
6818 &array,
6819 &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
6820 )
6821 .unwrap();
6822 let c = b.as_primitive::<TimestampSecondType>();
6823 let string_array = cast(&c, &DataType::Utf8).unwrap();
6824 let result = string_array.as_string::<i32>();
6825 assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6826
6827 let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
6828 let c = b.as_primitive::<TimestampSecondType>();
6829 let string_array = cast(&c, &DataType::Utf8).unwrap();
6830 let result = string_array.as_string::<i32>();
6831 assert_eq!("2021-01-01T00:00:00", result.value(0));
6832 }
6833
6834 #[test]
6835 fn test_cast_date32_to_timestamp_with_timezone() {
6836 let tz = "+0545"; let a = Date32Array::from(vec![Some(18628), Some(18993), None]); let array = Arc::new(a) as ArrayRef;
6839 let b = cast(
6840 &array,
6841 &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
6842 )
6843 .unwrap();
6844 let c = b.as_primitive::<TimestampSecondType>();
6845 assert_eq!(1609438500, c.value(0));
6846 assert_eq!(1640974500, c.value(1));
6847 assert!(c.is_null(2));
6848
6849 let string_array = cast(&c, &DataType::Utf8).unwrap();
6850 let result = string_array.as_string::<i32>();
6851 assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6852 assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
6853 }
6854
6855 #[test]
6856 fn test_cast_date32_to_timestamp_with_timezone_ms() {
6857 let tz = "+0545"; let a = Date32Array::from(vec![Some(18628), Some(18993), None]); let array = Arc::new(a) as ArrayRef;
6860 let b = cast(
6861 &array,
6862 &DataType::Timestamp(TimeUnit::Millisecond, Some(tz.into())),
6863 )
6864 .unwrap();
6865 let c = b.as_primitive::<TimestampMillisecondType>();
6866 assert_eq!(1609438500000, c.value(0));
6867 assert_eq!(1640974500000, c.value(1));
6868 assert!(c.is_null(2));
6869
6870 let string_array = cast(&c, &DataType::Utf8).unwrap();
6871 let result = string_array.as_string::<i32>();
6872 assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6873 assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
6874 }
6875
6876 #[test]
6877 fn test_cast_date32_to_timestamp_with_timezone_us() {
6878 let tz = "+0545"; let a = Date32Array::from(vec![Some(18628), Some(18993), None]); let array = Arc::new(a) as ArrayRef;
6881 let b = cast(
6882 &array,
6883 &DataType::Timestamp(TimeUnit::Microsecond, Some(tz.into())),
6884 )
6885 .unwrap();
6886 let c = b.as_primitive::<TimestampMicrosecondType>();
6887 assert_eq!(1609438500000000, c.value(0));
6888 assert_eq!(1640974500000000, c.value(1));
6889 assert!(c.is_null(2));
6890
6891 let string_array = cast(&c, &DataType::Utf8).unwrap();
6892 let result = string_array.as_string::<i32>();
6893 assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6894 assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
6895 }
6896
6897 #[test]
6898 fn test_cast_date32_to_timestamp_with_timezone_ns() {
6899 let tz = "+0545"; let a = Date32Array::from(vec![Some(18628), Some(18993), None]); let array = Arc::new(a) as ArrayRef;
6902 let b = cast(
6903 &array,
6904 &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.into())),
6905 )
6906 .unwrap();
6907 let c = b.as_primitive::<TimestampNanosecondType>();
6908 assert_eq!(1609438500000000000, c.value(0));
6909 assert_eq!(1640974500000000000, c.value(1));
6910 assert!(c.is_null(2));
6911
6912 let string_array = cast(&c, &DataType::Utf8).unwrap();
6913 let result = string_array.as_string::<i32>();
6914 assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6915 assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
6916 }
6917
6918 #[test]
6919 fn test_cast_date64_to_timestamp_with_timezone() {
6920 let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6921 let tz = "+0545"; let b = cast(
6923 &array,
6924 &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
6925 )
6926 .unwrap();
6927
6928 let c = b.as_primitive::<TimestampSecondType>();
6929 assert_eq!(863979300, c.value(0));
6930 assert_eq!(1545675300, c.value(1));
6931 assert!(c.is_null(2));
6932
6933 let string_array = cast(&c, &DataType::Utf8).unwrap();
6934 let result = string_array.as_string::<i32>();
6935 assert_eq!("1997-05-19T00:00:00+05:45", result.value(0));
6936 assert_eq!("2018-12-25T00:00:00+05:45", result.value(1));
6937 }
6938
6939 #[test]
6940 fn test_cast_date64_to_timestamp_with_timezone_ms() {
6941 let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6942 let tz = "+0545"; let b = cast(
6944 &array,
6945 &DataType::Timestamp(TimeUnit::Millisecond, Some(tz.into())),
6946 )
6947 .unwrap();
6948
6949 let c = b.as_primitive::<TimestampMillisecondType>();
6950 assert_eq!(863979300005, c.value(0));
6951 assert_eq!(1545675300001, c.value(1));
6952 assert!(c.is_null(2));
6953
6954 let string_array = cast(&c, &DataType::Utf8).unwrap();
6955 let result = string_array.as_string::<i32>();
6956 assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
6957 assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
6958 }
6959
6960 #[test]
6961 fn test_cast_date64_to_timestamp_with_timezone_us() {
6962 let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6963 let tz = "+0545"; let b = cast(
6965 &array,
6966 &DataType::Timestamp(TimeUnit::Microsecond, Some(tz.into())),
6967 )
6968 .unwrap();
6969
6970 let c = b.as_primitive::<TimestampMicrosecondType>();
6971 assert_eq!(863979300005000, c.value(0));
6972 assert_eq!(1545675300001000, c.value(1));
6973 assert!(c.is_null(2));
6974
6975 let string_array = cast(&c, &DataType::Utf8).unwrap();
6976 let result = string_array.as_string::<i32>();
6977 assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
6978 assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
6979 }
6980
6981 #[test]
6982 fn test_cast_date64_to_timestamp_with_timezone_ns() {
6983 let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6984 let tz = "+0545"; let b = cast(
6986 &array,
6987 &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.into())),
6988 )
6989 .unwrap();
6990
6991 let c = b.as_primitive::<TimestampNanosecondType>();
6992 assert_eq!(863979300005000000, c.value(0));
6993 assert_eq!(1545675300001000000, c.value(1));
6994 assert!(c.is_null(2));
6995
6996 let string_array = cast(&c, &DataType::Utf8).unwrap();
6997 let result = string_array.as_string::<i32>();
6998 assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
6999 assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
7000 }
7001
7002 #[test]
7003 fn test_cast_timestamp_to_strings() {
7004 let array =
7006 TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
7007 let expected = vec![
7008 Some("1997-05-19T00:00:03.005"),
7009 Some("2018-12-25T00:00:02.001"),
7010 None,
7011 ];
7012
7013 assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
7014 assert_cast!(array, DataType::Utf8, StringArray, expected);
7015 assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
7016 }
7017
7018 #[test]
7019 fn test_cast_timestamp_to_strings_opt() {
7020 let ts_format = "%Y-%m-%d %H:%M:%S%.6f";
7021 let tz = "+0545"; let cast_options = CastOptions {
7023 safe: true,
7024 format_options: FormatOptions::default()
7025 .with_timestamp_format(Some(ts_format))
7026 .with_timestamp_tz_format(Some(ts_format)),
7027 };
7028
7029 let array_without_tz =
7031 TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
7032 let expected = vec![
7033 Some("1997-05-19 00:00:03.005000"),
7034 Some("2018-12-25 00:00:02.001000"),
7035 None,
7036 ];
7037 assert_cast!(
7038 array_without_tz,
7039 DataType::Utf8View,
7040 StringViewArray,
7041 cast_options,
7042 expected
7043 );
7044 assert_cast!(
7045 array_without_tz,
7046 DataType::Utf8,
7047 StringArray,
7048 cast_options,
7049 expected
7050 );
7051 assert_cast!(
7052 array_without_tz,
7053 DataType::LargeUtf8,
7054 LargeStringArray,
7055 cast_options,
7056 expected
7057 );
7058
7059 let array_with_tz =
7060 TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None])
7061 .with_timezone(tz.to_string());
7062 let expected = vec![
7063 Some("1997-05-19 05:45:03.005000"),
7064 Some("2018-12-25 05:45:02.001000"),
7065 None,
7066 ];
7067 assert_cast!(
7068 array_with_tz,
7069 DataType::Utf8View,
7070 StringViewArray,
7071 cast_options,
7072 expected
7073 );
7074 assert_cast!(
7075 array_with_tz,
7076 DataType::Utf8,
7077 StringArray,
7078 cast_options,
7079 expected
7080 );
7081 assert_cast!(
7082 array_with_tz,
7083 DataType::LargeUtf8,
7084 LargeStringArray,
7085 cast_options,
7086 expected
7087 );
7088 }
7089
7090 #[test]
7091 fn test_cast_between_timestamps() {
7092 let array =
7093 TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
7094 let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
7095 let c = b.as_primitive::<TimestampSecondType>();
7096 assert_eq!(864000003, c.value(0));
7097 assert_eq!(1545696002, c.value(1));
7098 assert!(c.is_null(2));
7099 }
7100
7101 #[test]
7102 fn test_cast_duration_to_i64() {
7103 let base = vec![5, 6, 7, 8, 100000000];
7104
7105 let duration_arrays = vec![
7106 Arc::new(DurationNanosecondArray::from(base.clone())) as ArrayRef,
7107 Arc::new(DurationMicrosecondArray::from(base.clone())) as ArrayRef,
7108 Arc::new(DurationMillisecondArray::from(base.clone())) as ArrayRef,
7109 Arc::new(DurationSecondArray::from(base.clone())) as ArrayRef,
7110 ];
7111
7112 for arr in duration_arrays {
7113 assert!(can_cast_types(arr.data_type(), &DataType::Int64));
7114 let result = cast(&arr, &DataType::Int64).unwrap();
7115 let result = result.as_primitive::<Int64Type>();
7116 assert_eq!(base.as_slice(), result.values());
7117 }
7118 }
7119
7120 #[test]
7121 fn test_cast_between_durations_and_numerics() {
7122 fn test_cast_between_durations<FromType, ToType>()
7123 where
7124 FromType: ArrowPrimitiveType<Native = i64>,
7125 ToType: ArrowPrimitiveType<Native = i64>,
7126 PrimitiveArray<FromType>: From<Vec<Option<i64>>>,
7127 {
7128 let from_unit = match FromType::DATA_TYPE {
7129 DataType::Duration(unit) => unit,
7130 _ => panic!("Expected a duration type"),
7131 };
7132 let to_unit = match ToType::DATA_TYPE {
7133 DataType::Duration(unit) => unit,
7134 _ => panic!("Expected a duration type"),
7135 };
7136 let from_size = time_unit_multiple(&from_unit);
7137 let to_size = time_unit_multiple(&to_unit);
7138
7139 let (v1_before, v2_before) = (8640003005, 1696002001);
7140 let (v1_after, v2_after) = if from_size >= to_size {
7141 (
7142 v1_before / (from_size / to_size),
7143 v2_before / (from_size / to_size),
7144 )
7145 } else {
7146 (
7147 v1_before * (to_size / from_size),
7148 v2_before * (to_size / from_size),
7149 )
7150 };
7151
7152 let array =
7153 PrimitiveArray::<FromType>::from(vec![Some(v1_before), Some(v2_before), None]);
7154 let b = cast(&array, &ToType::DATA_TYPE).unwrap();
7155 let c = b.as_primitive::<ToType>();
7156 assert_eq!(v1_after, c.value(0));
7157 assert_eq!(v2_after, c.value(1));
7158 assert!(c.is_null(2));
7159 }
7160
7161 test_cast_between_durations::<DurationSecondType, DurationMillisecondType>();
7163 test_cast_between_durations::<DurationSecondType, DurationMicrosecondType>();
7164 test_cast_between_durations::<DurationSecondType, DurationNanosecondType>();
7165 test_cast_between_durations::<DurationMillisecondType, DurationSecondType>();
7166 test_cast_between_durations::<DurationMillisecondType, DurationMicrosecondType>();
7167 test_cast_between_durations::<DurationMillisecondType, DurationNanosecondType>();
7168 test_cast_between_durations::<DurationMicrosecondType, DurationSecondType>();
7169 test_cast_between_durations::<DurationMicrosecondType, DurationMillisecondType>();
7170 test_cast_between_durations::<DurationMicrosecondType, DurationNanosecondType>();
7171 test_cast_between_durations::<DurationNanosecondType, DurationSecondType>();
7172 test_cast_between_durations::<DurationNanosecondType, DurationMillisecondType>();
7173 test_cast_between_durations::<DurationNanosecondType, DurationMicrosecondType>();
7174
7175 let array = DurationSecondArray::from(vec![
7177 Some(i64::MAX),
7178 Some(8640203410378005),
7179 Some(10241096),
7180 None,
7181 ]);
7182 let b = cast(&array, &DataType::Duration(TimeUnit::Nanosecond)).unwrap();
7183 let c = b.as_primitive::<DurationNanosecondType>();
7184 assert!(c.is_null(0));
7185 assert!(c.is_null(1));
7186 assert_eq!(10241096000000000, c.value(2));
7187 assert!(c.is_null(3));
7188
7189 let array = DurationSecondArray::from(vec![
7191 Some(i64::MAX),
7192 Some(8640203410378005),
7193 Some(10241096),
7194 None,
7195 ]);
7196 let b = cast(&array, &DataType::Int64).unwrap();
7197 let c = b.as_primitive::<Int64Type>();
7198 assert_eq!(i64::MAX, c.value(0));
7199 assert_eq!(8640203410378005, c.value(1));
7200 assert_eq!(10241096, c.value(2));
7201 assert!(c.is_null(3));
7202
7203 let b = cast(&array, &DataType::Int32).unwrap();
7204 let c = b.as_primitive::<Int32Type>();
7205 assert_eq!(0, c.value(0));
7206 assert_eq!(0, c.value(1));
7207 assert_eq!(10241096, c.value(2));
7208 assert!(c.is_null(3));
7209
7210 let array = Int32Array::from(vec![Some(i32::MAX), Some(802034103), Some(10241096), None]);
7212 let b = cast(&array, &DataType::Duration(TimeUnit::Second)).unwrap();
7213 let c = b.as_any().downcast_ref::<DurationSecondArray>().unwrap();
7214 assert_eq!(i32::MAX as i64, c.value(0));
7215 assert_eq!(802034103, c.value(1));
7216 assert_eq!(10241096, c.value(2));
7217 assert!(c.is_null(3));
7218 }
7219
7220 #[test]
7221 fn test_cast_to_strings() {
7222 let a = Int32Array::from(vec![1, 2, 3]);
7223 let out = cast(&a, &DataType::Utf8).unwrap();
7224 let out = out
7225 .as_any()
7226 .downcast_ref::<StringArray>()
7227 .unwrap()
7228 .into_iter()
7229 .collect::<Vec<_>>();
7230 assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
7231 let out = cast(&a, &DataType::LargeUtf8).unwrap();
7232 let out = out
7233 .as_any()
7234 .downcast_ref::<LargeStringArray>()
7235 .unwrap()
7236 .into_iter()
7237 .collect::<Vec<_>>();
7238 assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
7239 }
7240
7241 #[test]
7242 fn test_str_to_str_casts() {
7243 for data in [
7244 vec![Some("foo"), Some("bar"), Some("ham")],
7245 vec![Some("foo"), None, Some("bar")],
7246 ] {
7247 let a = LargeStringArray::from(data.clone());
7248 let to = cast(&a, &DataType::Utf8).unwrap();
7249 let expect = a
7250 .as_any()
7251 .downcast_ref::<LargeStringArray>()
7252 .unwrap()
7253 .into_iter()
7254 .collect::<Vec<_>>();
7255 let out = to
7256 .as_any()
7257 .downcast_ref::<StringArray>()
7258 .unwrap()
7259 .into_iter()
7260 .collect::<Vec<_>>();
7261 assert_eq!(expect, out);
7262
7263 let a = StringArray::from(data);
7264 let to = cast(&a, &DataType::LargeUtf8).unwrap();
7265 let expect = a
7266 .as_any()
7267 .downcast_ref::<StringArray>()
7268 .unwrap()
7269 .into_iter()
7270 .collect::<Vec<_>>();
7271 let out = to
7272 .as_any()
7273 .downcast_ref::<LargeStringArray>()
7274 .unwrap()
7275 .into_iter()
7276 .collect::<Vec<_>>();
7277 assert_eq!(expect, out);
7278 }
7279 }
7280
7281 const VIEW_TEST_DATA: [Option<&str>; 5] = [
7282 Some("hello"),
7283 Some("repeated"),
7284 None,
7285 Some("large payload over 12 bytes"),
7286 Some("repeated"),
7287 ];
7288
7289 #[test]
7290 fn test_string_view_to_binary_view() {
7291 let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7292
7293 assert!(can_cast_types(
7294 string_view_array.data_type(),
7295 &DataType::BinaryView
7296 ));
7297
7298 let binary_view_array = cast(&string_view_array, &DataType::BinaryView).unwrap();
7299 assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
7300
7301 let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7302 assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
7303 }
7304
7305 #[test]
7306 fn test_binary_view_to_string_view() {
7307 let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7308
7309 assert!(can_cast_types(
7310 binary_view_array.data_type(),
7311 &DataType::Utf8View
7312 ));
7313
7314 let string_view_array = cast(&binary_view_array, &DataType::Utf8View).unwrap();
7315 assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7316
7317 let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7318 assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
7319 }
7320
7321 #[test]
7322 fn test_binary_view_to_string_view_with_invalid_utf8() {
7323 let binary_view_array = BinaryViewArray::from_iter(vec![
7324 Some("valid".as_bytes()),
7325 Some(&[0xff]),
7326 Some("utf8".as_bytes()),
7327 None,
7328 ]);
7329
7330 let strict_options = CastOptions {
7331 safe: false,
7332 ..Default::default()
7333 };
7334
7335 assert!(
7336 cast_with_options(&binary_view_array, &DataType::Utf8View, &strict_options).is_err()
7337 );
7338
7339 let safe_options = CastOptions {
7340 safe: true,
7341 ..Default::default()
7342 };
7343
7344 let string_view_array =
7345 cast_with_options(&binary_view_array, &DataType::Utf8View, &safe_options).unwrap();
7346 assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7347
7348 let values: Vec<_> = string_view_array.as_string_view().iter().collect();
7349
7350 assert_eq!(values, vec![Some("valid"), None, Some("utf8"), None]);
7351 }
7352
7353 #[test]
7354 fn test_string_to_view() {
7355 _test_string_to_view::<i32>();
7356 _test_string_to_view::<i64>();
7357 }
7358
7359 fn _test_string_to_view<O>()
7360 where
7361 O: OffsetSizeTrait,
7362 {
7363 let string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
7364
7365 assert!(can_cast_types(
7366 string_array.data_type(),
7367 &DataType::Utf8View
7368 ));
7369
7370 assert!(can_cast_types(
7371 string_array.data_type(),
7372 &DataType::BinaryView
7373 ));
7374
7375 let string_view_array = cast(&string_array, &DataType::Utf8View).unwrap();
7376 assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7377
7378 let binary_view_array = cast(&string_array, &DataType::BinaryView).unwrap();
7379 assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
7380
7381 let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7382 assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
7383
7384 let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7385 assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
7386 }
7387
7388 #[test]
7389 fn test_bianry_to_view() {
7390 _test_binary_to_view::<i32>();
7391 _test_binary_to_view::<i64>();
7392 }
7393
7394 fn _test_binary_to_view<O>()
7395 where
7396 O: OffsetSizeTrait,
7397 {
7398 let binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
7399
7400 assert!(can_cast_types(
7401 binary_array.data_type(),
7402 &DataType::Utf8View
7403 ));
7404
7405 assert!(can_cast_types(
7406 binary_array.data_type(),
7407 &DataType::BinaryView
7408 ));
7409
7410 let string_view_array = cast(&binary_array, &DataType::Utf8View).unwrap();
7411 assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7412
7413 let binary_view_array = cast(&binary_array, &DataType::BinaryView).unwrap();
7414 assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
7415
7416 let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7417 assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
7418
7419 let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7420 assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
7421 }
7422
7423 #[test]
7424 fn test_dict_to_view() {
7425 let values = StringArray::from_iter(VIEW_TEST_DATA);
7426 let keys = Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
7427 let string_dict_array =
7428 DictionaryArray::<Int8Type>::try_new(keys, Arc::new(values)).unwrap();
7429 let typed_dict = string_dict_array.downcast_dict::<StringArray>().unwrap();
7430
7431 let string_view_array = {
7432 let mut builder = StringViewBuilder::new().with_fixed_block_size(8); for v in typed_dict.into_iter() {
7434 builder.append_option(v);
7435 }
7436 builder.finish()
7437 };
7438 let expected_string_array_type = string_view_array.data_type();
7439 let casted_string_array = cast(&string_dict_array, expected_string_array_type).unwrap();
7440 assert_eq!(casted_string_array.data_type(), expected_string_array_type);
7441 assert_eq!(casted_string_array.as_ref(), &string_view_array);
7442
7443 let binary_buffer = cast(&typed_dict.values(), &DataType::Binary).unwrap();
7444 let binary_dict_array =
7445 DictionaryArray::<Int8Type>::new(typed_dict.keys().clone(), binary_buffer);
7446 let typed_binary_dict = binary_dict_array.downcast_dict::<BinaryArray>().unwrap();
7447
7448 let binary_view_array = {
7449 let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); for v in typed_binary_dict.into_iter() {
7451 builder.append_option(v);
7452 }
7453 builder.finish()
7454 };
7455 let expected_binary_array_type = binary_view_array.data_type();
7456 let casted_binary_array = cast(&binary_dict_array, expected_binary_array_type).unwrap();
7457 assert_eq!(casted_binary_array.data_type(), expected_binary_array_type);
7458 assert_eq!(casted_binary_array.as_ref(), &binary_view_array);
7459 }
7460
7461 #[test]
7462 fn test_view_to_dict() {
7463 let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7464 let string_dict_array: DictionaryArray<Int8Type> = VIEW_TEST_DATA.into_iter().collect();
7465 let casted_type = string_dict_array.data_type();
7466 let casted_dict_array = cast(&string_view_array, casted_type).unwrap();
7467 assert_eq!(casted_dict_array.data_type(), casted_type);
7468 assert_eq!(casted_dict_array.as_ref(), &string_dict_array);
7469
7470 let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7471 let binary_dict_array = string_dict_array.downcast_dict::<StringArray>().unwrap();
7472 let binary_buffer = cast(&binary_dict_array.values(), &DataType::Binary).unwrap();
7473 let binary_dict_array =
7474 DictionaryArray::<Int8Type>::new(binary_dict_array.keys().clone(), binary_buffer);
7475 let casted_type = binary_dict_array.data_type();
7476 let casted_binary_array = cast(&binary_view_array, casted_type).unwrap();
7477 assert_eq!(casted_binary_array.data_type(), casted_type);
7478 assert_eq!(casted_binary_array.as_ref(), &binary_dict_array);
7479 }
7480
7481 #[test]
7482 fn test_view_to_string() {
7483 _test_view_to_string::<i32>();
7484 _test_view_to_string::<i64>();
7485 }
7486
7487 fn _test_view_to_string<O>()
7488 where
7489 O: OffsetSizeTrait,
7490 {
7491 let string_view_array = {
7492 let mut builder = StringViewBuilder::new().with_fixed_block_size(8); for s in VIEW_TEST_DATA.iter() {
7494 builder.append_option(*s);
7495 }
7496 builder.finish()
7497 };
7498
7499 let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7500
7501 let expected_string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
7502 let expected_type = expected_string_array.data_type();
7503
7504 assert!(can_cast_types(string_view_array.data_type(), expected_type));
7505 assert!(can_cast_types(binary_view_array.data_type(), expected_type));
7506
7507 let string_view_casted_array = cast(&string_view_array, expected_type).unwrap();
7508 assert_eq!(string_view_casted_array.data_type(), expected_type);
7509 assert_eq!(string_view_casted_array.as_ref(), &expected_string_array);
7510
7511 let binary_view_casted_array = cast(&binary_view_array, expected_type).unwrap();
7512 assert_eq!(binary_view_casted_array.data_type(), expected_type);
7513 assert_eq!(binary_view_casted_array.as_ref(), &expected_string_array);
7514 }
7515
7516 #[test]
7517 fn test_view_to_binary() {
7518 _test_view_to_binary::<i32>();
7519 _test_view_to_binary::<i64>();
7520 }
7521
7522 fn _test_view_to_binary<O>()
7523 where
7524 O: OffsetSizeTrait,
7525 {
7526 let view_array = {
7527 let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); for s in VIEW_TEST_DATA.iter() {
7529 builder.append_option(*s);
7530 }
7531 builder.finish()
7532 };
7533
7534 let expected_binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
7535 let expected_type = expected_binary_array.data_type();
7536
7537 assert!(can_cast_types(view_array.data_type(), expected_type));
7538
7539 let binary_array = cast(&view_array, expected_type).unwrap();
7540 assert_eq!(binary_array.data_type(), expected_type);
7541
7542 assert_eq!(binary_array.as_ref(), &expected_binary_array);
7543 }
7544
7545 #[test]
7546 fn test_cast_from_f64() {
7547 let f64_values: Vec<f64> = vec![
7548 i64::MIN as f64,
7549 i32::MIN as f64,
7550 i16::MIN as f64,
7551 i8::MIN as f64,
7552 0_f64,
7553 u8::MAX as f64,
7554 u16::MAX as f64,
7555 u32::MAX as f64,
7556 u64::MAX as f64,
7557 ];
7558 let f64_array: ArrayRef = Arc::new(Float64Array::from(f64_values));
7559
7560 let f64_expected = vec![
7561 -9223372036854776000.0,
7562 -2147483648.0,
7563 -32768.0,
7564 -128.0,
7565 0.0,
7566 255.0,
7567 65535.0,
7568 4294967295.0,
7569 18446744073709552000.0,
7570 ];
7571 assert_eq!(
7572 f64_expected,
7573 get_cast_values::<Float64Type>(&f64_array, &DataType::Float64)
7574 .iter()
7575 .map(|i| i.parse::<f64>().unwrap())
7576 .collect::<Vec<f64>>()
7577 );
7578
7579 let f32_expected = vec![
7580 -9223372000000000000.0,
7581 -2147483600.0,
7582 -32768.0,
7583 -128.0,
7584 0.0,
7585 255.0,
7586 65535.0,
7587 4294967300.0,
7588 18446744000000000000.0,
7589 ];
7590 assert_eq!(
7591 f32_expected,
7592 get_cast_values::<Float32Type>(&f64_array, &DataType::Float32)
7593 .iter()
7594 .map(|i| i.parse::<f32>().unwrap())
7595 .collect::<Vec<f32>>()
7596 );
7597
7598 let f16_expected = vec![
7599 f16::from_f64(-9223372000000000000.0),
7600 f16::from_f64(-2147483600.0),
7601 f16::from_f64(-32768.0),
7602 f16::from_f64(-128.0),
7603 f16::from_f64(0.0),
7604 f16::from_f64(255.0),
7605 f16::from_f64(65535.0),
7606 f16::from_f64(4294967300.0),
7607 f16::from_f64(18446744000000000000.0),
7608 ];
7609 assert_eq!(
7610 f16_expected,
7611 get_cast_values::<Float16Type>(&f64_array, &DataType::Float16)
7612 .iter()
7613 .map(|i| i.parse::<f16>().unwrap())
7614 .collect::<Vec<f16>>()
7615 );
7616
7617 let i64_expected = vec![
7618 "-9223372036854775808",
7619 "-2147483648",
7620 "-32768",
7621 "-128",
7622 "0",
7623 "255",
7624 "65535",
7625 "4294967295",
7626 "null",
7627 ];
7628 assert_eq!(
7629 i64_expected,
7630 get_cast_values::<Int64Type>(&f64_array, &DataType::Int64)
7631 );
7632
7633 let i32_expected = vec![
7634 "null",
7635 "-2147483648",
7636 "-32768",
7637 "-128",
7638 "0",
7639 "255",
7640 "65535",
7641 "null",
7642 "null",
7643 ];
7644 assert_eq!(
7645 i32_expected,
7646 get_cast_values::<Int32Type>(&f64_array, &DataType::Int32)
7647 );
7648
7649 let i16_expected = vec![
7650 "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
7651 ];
7652 assert_eq!(
7653 i16_expected,
7654 get_cast_values::<Int16Type>(&f64_array, &DataType::Int16)
7655 );
7656
7657 let i8_expected = vec![
7658 "null", "null", "null", "-128", "0", "null", "null", "null", "null",
7659 ];
7660 assert_eq!(
7661 i8_expected,
7662 get_cast_values::<Int8Type>(&f64_array, &DataType::Int8)
7663 );
7664
7665 let u64_expected = vec![
7666 "null",
7667 "null",
7668 "null",
7669 "null",
7670 "0",
7671 "255",
7672 "65535",
7673 "4294967295",
7674 "null",
7675 ];
7676 assert_eq!(
7677 u64_expected,
7678 get_cast_values::<UInt64Type>(&f64_array, &DataType::UInt64)
7679 );
7680
7681 let u32_expected = vec![
7682 "null",
7683 "null",
7684 "null",
7685 "null",
7686 "0",
7687 "255",
7688 "65535",
7689 "4294967295",
7690 "null",
7691 ];
7692 assert_eq!(
7693 u32_expected,
7694 get_cast_values::<UInt32Type>(&f64_array, &DataType::UInt32)
7695 );
7696
7697 let u16_expected = vec![
7698 "null", "null", "null", "null", "0", "255", "65535", "null", "null",
7699 ];
7700 assert_eq!(
7701 u16_expected,
7702 get_cast_values::<UInt16Type>(&f64_array, &DataType::UInt16)
7703 );
7704
7705 let u8_expected = vec![
7706 "null", "null", "null", "null", "0", "255", "null", "null", "null",
7707 ];
7708 assert_eq!(
7709 u8_expected,
7710 get_cast_values::<UInt8Type>(&f64_array, &DataType::UInt8)
7711 );
7712 }
7713
7714 #[test]
7715 fn test_cast_from_f32() {
7716 let f32_values: Vec<f32> = vec![
7717 i32::MIN as f32,
7718 i32::MIN as f32,
7719 i16::MIN as f32,
7720 i8::MIN as f32,
7721 0_f32,
7722 u8::MAX as f32,
7723 u16::MAX as f32,
7724 u32::MAX as f32,
7725 u32::MAX as f32,
7726 ];
7727 let f32_array: ArrayRef = Arc::new(Float32Array::from(f32_values));
7728
7729 let f64_expected = vec![
7730 "-2147483648.0",
7731 "-2147483648.0",
7732 "-32768.0",
7733 "-128.0",
7734 "0.0",
7735 "255.0",
7736 "65535.0",
7737 "4294967296.0",
7738 "4294967296.0",
7739 ];
7740 assert_eq!(
7741 f64_expected,
7742 get_cast_values::<Float64Type>(&f32_array, &DataType::Float64)
7743 );
7744
7745 let f32_expected = vec![
7746 "-2147483600.0",
7747 "-2147483600.0",
7748 "-32768.0",
7749 "-128.0",
7750 "0.0",
7751 "255.0",
7752 "65535.0",
7753 "4294967300.0",
7754 "4294967300.0",
7755 ];
7756 assert_eq!(
7757 f32_expected,
7758 get_cast_values::<Float32Type>(&f32_array, &DataType::Float32)
7759 );
7760
7761 let f16_expected = vec![
7762 "-inf", "-inf", "-32768.0", "-128.0", "0.0", "255.0", "inf", "inf", "inf",
7763 ];
7764 assert_eq!(
7765 f16_expected,
7766 get_cast_values::<Float16Type>(&f32_array, &DataType::Float16)
7767 );
7768
7769 let i64_expected = vec![
7770 "-2147483648",
7771 "-2147483648",
7772 "-32768",
7773 "-128",
7774 "0",
7775 "255",
7776 "65535",
7777 "4294967296",
7778 "4294967296",
7779 ];
7780 assert_eq!(
7781 i64_expected,
7782 get_cast_values::<Int64Type>(&f32_array, &DataType::Int64)
7783 );
7784
7785 let i32_expected = vec![
7786 "-2147483648",
7787 "-2147483648",
7788 "-32768",
7789 "-128",
7790 "0",
7791 "255",
7792 "65535",
7793 "null",
7794 "null",
7795 ];
7796 assert_eq!(
7797 i32_expected,
7798 get_cast_values::<Int32Type>(&f32_array, &DataType::Int32)
7799 );
7800
7801 let i16_expected = vec![
7802 "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
7803 ];
7804 assert_eq!(
7805 i16_expected,
7806 get_cast_values::<Int16Type>(&f32_array, &DataType::Int16)
7807 );
7808
7809 let i8_expected = vec![
7810 "null", "null", "null", "-128", "0", "null", "null", "null", "null",
7811 ];
7812 assert_eq!(
7813 i8_expected,
7814 get_cast_values::<Int8Type>(&f32_array, &DataType::Int8)
7815 );
7816
7817 let u64_expected = vec![
7818 "null",
7819 "null",
7820 "null",
7821 "null",
7822 "0",
7823 "255",
7824 "65535",
7825 "4294967296",
7826 "4294967296",
7827 ];
7828 assert_eq!(
7829 u64_expected,
7830 get_cast_values::<UInt64Type>(&f32_array, &DataType::UInt64)
7831 );
7832
7833 let u32_expected = vec![
7834 "null", "null", "null", "null", "0", "255", "65535", "null", "null",
7835 ];
7836 assert_eq!(
7837 u32_expected,
7838 get_cast_values::<UInt32Type>(&f32_array, &DataType::UInt32)
7839 );
7840
7841 let u16_expected = vec![
7842 "null", "null", "null", "null", "0", "255", "65535", "null", "null",
7843 ];
7844 assert_eq!(
7845 u16_expected,
7846 get_cast_values::<UInt16Type>(&f32_array, &DataType::UInt16)
7847 );
7848
7849 let u8_expected = vec![
7850 "null", "null", "null", "null", "0", "255", "null", "null", "null",
7851 ];
7852 assert_eq!(
7853 u8_expected,
7854 get_cast_values::<UInt8Type>(&f32_array, &DataType::UInt8)
7855 );
7856 }
7857
7858 #[test]
7859 fn test_cast_from_uint64() {
7860 let u64_values: Vec<u64> = vec![
7861 0,
7862 u8::MAX as u64,
7863 u16::MAX as u64,
7864 u32::MAX as u64,
7865 u64::MAX,
7866 ];
7867 let u64_array: ArrayRef = Arc::new(UInt64Array::from(u64_values));
7868
7869 let f64_expected = vec![0.0, 255.0, 65535.0, 4294967295.0, 18446744073709552000.0];
7870 assert_eq!(
7871 f64_expected,
7872 get_cast_values::<Float64Type>(&u64_array, &DataType::Float64)
7873 .iter()
7874 .map(|i| i.parse::<f64>().unwrap())
7875 .collect::<Vec<f64>>()
7876 );
7877
7878 let f32_expected = vec![0.0, 255.0, 65535.0, 4294967300.0, 18446744000000000000.0];
7879 assert_eq!(
7880 f32_expected,
7881 get_cast_values::<Float32Type>(&u64_array, &DataType::Float32)
7882 .iter()
7883 .map(|i| i.parse::<f32>().unwrap())
7884 .collect::<Vec<f32>>()
7885 );
7886
7887 let f16_expected = vec![
7888 f16::from_f64(0.0),
7889 f16::from_f64(255.0),
7890 f16::from_f64(65535.0),
7891 f16::from_f64(4294967300.0),
7892 f16::from_f64(18446744000000000000.0),
7893 ];
7894 assert_eq!(
7895 f16_expected,
7896 get_cast_values::<Float16Type>(&u64_array, &DataType::Float16)
7897 .iter()
7898 .map(|i| i.parse::<f16>().unwrap())
7899 .collect::<Vec<f16>>()
7900 );
7901
7902 let i64_expected = vec!["0", "255", "65535", "4294967295", "null"];
7903 assert_eq!(
7904 i64_expected,
7905 get_cast_values::<Int64Type>(&u64_array, &DataType::Int64)
7906 );
7907
7908 let i32_expected = vec!["0", "255", "65535", "null", "null"];
7909 assert_eq!(
7910 i32_expected,
7911 get_cast_values::<Int32Type>(&u64_array, &DataType::Int32)
7912 );
7913
7914 let i16_expected = vec!["0", "255", "null", "null", "null"];
7915 assert_eq!(
7916 i16_expected,
7917 get_cast_values::<Int16Type>(&u64_array, &DataType::Int16)
7918 );
7919
7920 let i8_expected = vec!["0", "null", "null", "null", "null"];
7921 assert_eq!(
7922 i8_expected,
7923 get_cast_values::<Int8Type>(&u64_array, &DataType::Int8)
7924 );
7925
7926 let u64_expected = vec!["0", "255", "65535", "4294967295", "18446744073709551615"];
7927 assert_eq!(
7928 u64_expected,
7929 get_cast_values::<UInt64Type>(&u64_array, &DataType::UInt64)
7930 );
7931
7932 let u32_expected = vec!["0", "255", "65535", "4294967295", "null"];
7933 assert_eq!(
7934 u32_expected,
7935 get_cast_values::<UInt32Type>(&u64_array, &DataType::UInt32)
7936 );
7937
7938 let u16_expected = vec!["0", "255", "65535", "null", "null"];
7939 assert_eq!(
7940 u16_expected,
7941 get_cast_values::<UInt16Type>(&u64_array, &DataType::UInt16)
7942 );
7943
7944 let u8_expected = vec!["0", "255", "null", "null", "null"];
7945 assert_eq!(
7946 u8_expected,
7947 get_cast_values::<UInt8Type>(&u64_array, &DataType::UInt8)
7948 );
7949 }
7950
7951 #[test]
7952 fn test_cast_from_uint32() {
7953 let u32_values: Vec<u32> = vec![0, u8::MAX as u32, u16::MAX as u32, u32::MAX];
7954 let u32_array: ArrayRef = Arc::new(UInt32Array::from(u32_values));
7955
7956 let f64_expected = vec!["0.0", "255.0", "65535.0", "4294967295.0"];
7957 assert_eq!(
7958 f64_expected,
7959 get_cast_values::<Float64Type>(&u32_array, &DataType::Float64)
7960 );
7961
7962 let f32_expected = vec!["0.0", "255.0", "65535.0", "4294967300.0"];
7963 assert_eq!(
7964 f32_expected,
7965 get_cast_values::<Float32Type>(&u32_array, &DataType::Float32)
7966 );
7967
7968 let f16_expected = vec!["0.0", "255.0", "inf", "inf"];
7969 assert_eq!(
7970 f16_expected,
7971 get_cast_values::<Float16Type>(&u32_array, &DataType::Float16)
7972 );
7973
7974 let i64_expected = vec!["0", "255", "65535", "4294967295"];
7975 assert_eq!(
7976 i64_expected,
7977 get_cast_values::<Int64Type>(&u32_array, &DataType::Int64)
7978 );
7979
7980 let i32_expected = vec!["0", "255", "65535", "null"];
7981 assert_eq!(
7982 i32_expected,
7983 get_cast_values::<Int32Type>(&u32_array, &DataType::Int32)
7984 );
7985
7986 let i16_expected = vec!["0", "255", "null", "null"];
7987 assert_eq!(
7988 i16_expected,
7989 get_cast_values::<Int16Type>(&u32_array, &DataType::Int16)
7990 );
7991
7992 let i8_expected = vec!["0", "null", "null", "null"];
7993 assert_eq!(
7994 i8_expected,
7995 get_cast_values::<Int8Type>(&u32_array, &DataType::Int8)
7996 );
7997
7998 let u64_expected = vec!["0", "255", "65535", "4294967295"];
7999 assert_eq!(
8000 u64_expected,
8001 get_cast_values::<UInt64Type>(&u32_array, &DataType::UInt64)
8002 );
8003
8004 let u32_expected = vec!["0", "255", "65535", "4294967295"];
8005 assert_eq!(
8006 u32_expected,
8007 get_cast_values::<UInt32Type>(&u32_array, &DataType::UInt32)
8008 );
8009
8010 let u16_expected = vec!["0", "255", "65535", "null"];
8011 assert_eq!(
8012 u16_expected,
8013 get_cast_values::<UInt16Type>(&u32_array, &DataType::UInt16)
8014 );
8015
8016 let u8_expected = vec!["0", "255", "null", "null"];
8017 assert_eq!(
8018 u8_expected,
8019 get_cast_values::<UInt8Type>(&u32_array, &DataType::UInt8)
8020 );
8021 }
8022
8023 #[test]
8024 fn test_cast_from_uint16() {
8025 let u16_values: Vec<u16> = vec![0, u8::MAX as u16, u16::MAX];
8026 let u16_array: ArrayRef = Arc::new(UInt16Array::from(u16_values));
8027
8028 let f64_expected = vec!["0.0", "255.0", "65535.0"];
8029 assert_eq!(
8030 f64_expected,
8031 get_cast_values::<Float64Type>(&u16_array, &DataType::Float64)
8032 );
8033
8034 let f32_expected = vec!["0.0", "255.0", "65535.0"];
8035 assert_eq!(
8036 f32_expected,
8037 get_cast_values::<Float32Type>(&u16_array, &DataType::Float32)
8038 );
8039
8040 let f16_expected = vec!["0.0", "255.0", "inf"];
8041 assert_eq!(
8042 f16_expected,
8043 get_cast_values::<Float16Type>(&u16_array, &DataType::Float16)
8044 );
8045
8046 let i64_expected = vec!["0", "255", "65535"];
8047 assert_eq!(
8048 i64_expected,
8049 get_cast_values::<Int64Type>(&u16_array, &DataType::Int64)
8050 );
8051
8052 let i32_expected = vec!["0", "255", "65535"];
8053 assert_eq!(
8054 i32_expected,
8055 get_cast_values::<Int32Type>(&u16_array, &DataType::Int32)
8056 );
8057
8058 let i16_expected = vec!["0", "255", "null"];
8059 assert_eq!(
8060 i16_expected,
8061 get_cast_values::<Int16Type>(&u16_array, &DataType::Int16)
8062 );
8063
8064 let i8_expected = vec!["0", "null", "null"];
8065 assert_eq!(
8066 i8_expected,
8067 get_cast_values::<Int8Type>(&u16_array, &DataType::Int8)
8068 );
8069
8070 let u64_expected = vec!["0", "255", "65535"];
8071 assert_eq!(
8072 u64_expected,
8073 get_cast_values::<UInt64Type>(&u16_array, &DataType::UInt64)
8074 );
8075
8076 let u32_expected = vec!["0", "255", "65535"];
8077 assert_eq!(
8078 u32_expected,
8079 get_cast_values::<UInt32Type>(&u16_array, &DataType::UInt32)
8080 );
8081
8082 let u16_expected = vec!["0", "255", "65535"];
8083 assert_eq!(
8084 u16_expected,
8085 get_cast_values::<UInt16Type>(&u16_array, &DataType::UInt16)
8086 );
8087
8088 let u8_expected = vec!["0", "255", "null"];
8089 assert_eq!(
8090 u8_expected,
8091 get_cast_values::<UInt8Type>(&u16_array, &DataType::UInt8)
8092 );
8093 }
8094
8095 #[test]
8096 fn test_cast_from_uint8() {
8097 let u8_values: Vec<u8> = vec![0, u8::MAX];
8098 let u8_array: ArrayRef = Arc::new(UInt8Array::from(u8_values));
8099
8100 let f64_expected = vec!["0.0", "255.0"];
8101 assert_eq!(
8102 f64_expected,
8103 get_cast_values::<Float64Type>(&u8_array, &DataType::Float64)
8104 );
8105
8106 let f32_expected = vec!["0.0", "255.0"];
8107 assert_eq!(
8108 f32_expected,
8109 get_cast_values::<Float32Type>(&u8_array, &DataType::Float32)
8110 );
8111
8112 let f16_expected = vec!["0.0", "255.0"];
8113 assert_eq!(
8114 f16_expected,
8115 get_cast_values::<Float16Type>(&u8_array, &DataType::Float16)
8116 );
8117
8118 let i64_expected = vec!["0", "255"];
8119 assert_eq!(
8120 i64_expected,
8121 get_cast_values::<Int64Type>(&u8_array, &DataType::Int64)
8122 );
8123
8124 let i32_expected = vec!["0", "255"];
8125 assert_eq!(
8126 i32_expected,
8127 get_cast_values::<Int32Type>(&u8_array, &DataType::Int32)
8128 );
8129
8130 let i16_expected = vec!["0", "255"];
8131 assert_eq!(
8132 i16_expected,
8133 get_cast_values::<Int16Type>(&u8_array, &DataType::Int16)
8134 );
8135
8136 let i8_expected = vec!["0", "null"];
8137 assert_eq!(
8138 i8_expected,
8139 get_cast_values::<Int8Type>(&u8_array, &DataType::Int8)
8140 );
8141
8142 let u64_expected = vec!["0", "255"];
8143 assert_eq!(
8144 u64_expected,
8145 get_cast_values::<UInt64Type>(&u8_array, &DataType::UInt64)
8146 );
8147
8148 let u32_expected = vec!["0", "255"];
8149 assert_eq!(
8150 u32_expected,
8151 get_cast_values::<UInt32Type>(&u8_array, &DataType::UInt32)
8152 );
8153
8154 let u16_expected = vec!["0", "255"];
8155 assert_eq!(
8156 u16_expected,
8157 get_cast_values::<UInt16Type>(&u8_array, &DataType::UInt16)
8158 );
8159
8160 let u8_expected = vec!["0", "255"];
8161 assert_eq!(
8162 u8_expected,
8163 get_cast_values::<UInt8Type>(&u8_array, &DataType::UInt8)
8164 );
8165 }
8166
8167 #[test]
8168 fn test_cast_from_int64() {
8169 let i64_values: Vec<i64> = vec![
8170 i64::MIN,
8171 i32::MIN as i64,
8172 i16::MIN as i64,
8173 i8::MIN as i64,
8174 0,
8175 i8::MAX as i64,
8176 i16::MAX as i64,
8177 i32::MAX as i64,
8178 i64::MAX,
8179 ];
8180 let i64_array: ArrayRef = Arc::new(Int64Array::from(i64_values));
8181
8182 let f64_expected = vec![
8183 -9223372036854776000.0,
8184 -2147483648.0,
8185 -32768.0,
8186 -128.0,
8187 0.0,
8188 127.0,
8189 32767.0,
8190 2147483647.0,
8191 9223372036854776000.0,
8192 ];
8193 assert_eq!(
8194 f64_expected,
8195 get_cast_values::<Float64Type>(&i64_array, &DataType::Float64)
8196 .iter()
8197 .map(|i| i.parse::<f64>().unwrap())
8198 .collect::<Vec<f64>>()
8199 );
8200
8201 let f32_expected = vec![
8202 -9223372000000000000.0,
8203 -2147483600.0,
8204 -32768.0,
8205 -128.0,
8206 0.0,
8207 127.0,
8208 32767.0,
8209 2147483600.0,
8210 9223372000000000000.0,
8211 ];
8212 assert_eq!(
8213 f32_expected,
8214 get_cast_values::<Float32Type>(&i64_array, &DataType::Float32)
8215 .iter()
8216 .map(|i| i.parse::<f32>().unwrap())
8217 .collect::<Vec<f32>>()
8218 );
8219
8220 let f16_expected = vec![
8221 f16::from_f64(-9223372000000000000.0),
8222 f16::from_f64(-2147483600.0),
8223 f16::from_f64(-32768.0),
8224 f16::from_f64(-128.0),
8225 f16::from_f64(0.0),
8226 f16::from_f64(127.0),
8227 f16::from_f64(32767.0),
8228 f16::from_f64(2147483600.0),
8229 f16::from_f64(9223372000000000000.0),
8230 ];
8231 assert_eq!(
8232 f16_expected,
8233 get_cast_values::<Float16Type>(&i64_array, &DataType::Float16)
8234 .iter()
8235 .map(|i| i.parse::<f16>().unwrap())
8236 .collect::<Vec<f16>>()
8237 );
8238
8239 let i64_expected = vec![
8240 "-9223372036854775808",
8241 "-2147483648",
8242 "-32768",
8243 "-128",
8244 "0",
8245 "127",
8246 "32767",
8247 "2147483647",
8248 "9223372036854775807",
8249 ];
8250 assert_eq!(
8251 i64_expected,
8252 get_cast_values::<Int64Type>(&i64_array, &DataType::Int64)
8253 );
8254
8255 let i32_expected = vec![
8256 "null",
8257 "-2147483648",
8258 "-32768",
8259 "-128",
8260 "0",
8261 "127",
8262 "32767",
8263 "2147483647",
8264 "null",
8265 ];
8266 assert_eq!(
8267 i32_expected,
8268 get_cast_values::<Int32Type>(&i64_array, &DataType::Int32)
8269 );
8270
8271 assert_eq!(
8272 i32_expected,
8273 get_cast_values::<Date32Type>(&i64_array, &DataType::Date32)
8274 );
8275
8276 let i16_expected = vec![
8277 "null", "null", "-32768", "-128", "0", "127", "32767", "null", "null",
8278 ];
8279 assert_eq!(
8280 i16_expected,
8281 get_cast_values::<Int16Type>(&i64_array, &DataType::Int16)
8282 );
8283
8284 let i8_expected = vec![
8285 "null", "null", "null", "-128", "0", "127", "null", "null", "null",
8286 ];
8287 assert_eq!(
8288 i8_expected,
8289 get_cast_values::<Int8Type>(&i64_array, &DataType::Int8)
8290 );
8291
8292 let u64_expected = vec![
8293 "null",
8294 "null",
8295 "null",
8296 "null",
8297 "0",
8298 "127",
8299 "32767",
8300 "2147483647",
8301 "9223372036854775807",
8302 ];
8303 assert_eq!(
8304 u64_expected,
8305 get_cast_values::<UInt64Type>(&i64_array, &DataType::UInt64)
8306 );
8307
8308 let u32_expected = vec![
8309 "null",
8310 "null",
8311 "null",
8312 "null",
8313 "0",
8314 "127",
8315 "32767",
8316 "2147483647",
8317 "null",
8318 ];
8319 assert_eq!(
8320 u32_expected,
8321 get_cast_values::<UInt32Type>(&i64_array, &DataType::UInt32)
8322 );
8323
8324 let u16_expected = vec![
8325 "null", "null", "null", "null", "0", "127", "32767", "null", "null",
8326 ];
8327 assert_eq!(
8328 u16_expected,
8329 get_cast_values::<UInt16Type>(&i64_array, &DataType::UInt16)
8330 );
8331
8332 let u8_expected = vec![
8333 "null", "null", "null", "null", "0", "127", "null", "null", "null",
8334 ];
8335 assert_eq!(
8336 u8_expected,
8337 get_cast_values::<UInt8Type>(&i64_array, &DataType::UInt8)
8338 );
8339 }
8340
8341 #[test]
8342 fn test_cast_from_int32() {
8343 let i32_values: Vec<i32> = vec![
8344 i32::MIN,
8345 i16::MIN as i32,
8346 i8::MIN as i32,
8347 0,
8348 i8::MAX as i32,
8349 i16::MAX as i32,
8350 i32::MAX,
8351 ];
8352 let i32_array: ArrayRef = Arc::new(Int32Array::from(i32_values));
8353
8354 let f64_expected = vec![
8355 "-2147483648.0",
8356 "-32768.0",
8357 "-128.0",
8358 "0.0",
8359 "127.0",
8360 "32767.0",
8361 "2147483647.0",
8362 ];
8363 assert_eq!(
8364 f64_expected,
8365 get_cast_values::<Float64Type>(&i32_array, &DataType::Float64)
8366 );
8367
8368 let f32_expected = vec![
8369 "-2147483600.0",
8370 "-32768.0",
8371 "-128.0",
8372 "0.0",
8373 "127.0",
8374 "32767.0",
8375 "2147483600.0",
8376 ];
8377 assert_eq!(
8378 f32_expected,
8379 get_cast_values::<Float32Type>(&i32_array, &DataType::Float32)
8380 );
8381
8382 let f16_expected = vec![
8383 f16::from_f64(-2147483600.0),
8384 f16::from_f64(-32768.0),
8385 f16::from_f64(-128.0),
8386 f16::from_f64(0.0),
8387 f16::from_f64(127.0),
8388 f16::from_f64(32767.0),
8389 f16::from_f64(2147483600.0),
8390 ];
8391 assert_eq!(
8392 f16_expected,
8393 get_cast_values::<Float16Type>(&i32_array, &DataType::Float16)
8394 .iter()
8395 .map(|i| i.parse::<f16>().unwrap())
8396 .collect::<Vec<f16>>()
8397 );
8398
8399 let i16_expected = vec!["null", "-32768", "-128", "0", "127", "32767", "null"];
8400 assert_eq!(
8401 i16_expected,
8402 get_cast_values::<Int16Type>(&i32_array, &DataType::Int16)
8403 );
8404
8405 let i8_expected = vec!["null", "null", "-128", "0", "127", "null", "null"];
8406 assert_eq!(
8407 i8_expected,
8408 get_cast_values::<Int8Type>(&i32_array, &DataType::Int8)
8409 );
8410
8411 let u64_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
8412 assert_eq!(
8413 u64_expected,
8414 get_cast_values::<UInt64Type>(&i32_array, &DataType::UInt64)
8415 );
8416
8417 let u32_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
8418 assert_eq!(
8419 u32_expected,
8420 get_cast_values::<UInt32Type>(&i32_array, &DataType::UInt32)
8421 );
8422
8423 let u16_expected = vec!["null", "null", "null", "0", "127", "32767", "null"];
8424 assert_eq!(
8425 u16_expected,
8426 get_cast_values::<UInt16Type>(&i32_array, &DataType::UInt16)
8427 );
8428
8429 let u8_expected = vec!["null", "null", "null", "0", "127", "null", "null"];
8430 assert_eq!(
8431 u8_expected,
8432 get_cast_values::<UInt8Type>(&i32_array, &DataType::UInt8)
8433 );
8434
8435 let i64_expected = vec![
8437 "-185542587187200000",
8438 "-2831155200000",
8439 "-11059200000",
8440 "0",
8441 "10972800000",
8442 "2831068800000",
8443 "185542587100800000",
8444 ];
8445 assert_eq!(
8446 i64_expected,
8447 get_cast_values::<Date64Type>(&i32_array, &DataType::Date64)
8448 );
8449 }
8450
8451 #[test]
8452 fn test_cast_from_int16() {
8453 let i16_values: Vec<i16> = vec![i16::MIN, i8::MIN as i16, 0, i8::MAX as i16, i16::MAX];
8454 let i16_array: ArrayRef = Arc::new(Int16Array::from(i16_values));
8455
8456 let f64_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
8457 assert_eq!(
8458 f64_expected,
8459 get_cast_values::<Float64Type>(&i16_array, &DataType::Float64)
8460 );
8461
8462 let f32_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
8463 assert_eq!(
8464 f32_expected,
8465 get_cast_values::<Float32Type>(&i16_array, &DataType::Float32)
8466 );
8467
8468 let f16_expected = vec![
8469 f16::from_f64(-32768.0),
8470 f16::from_f64(-128.0),
8471 f16::from_f64(0.0),
8472 f16::from_f64(127.0),
8473 f16::from_f64(32767.0),
8474 ];
8475 assert_eq!(
8476 f16_expected,
8477 get_cast_values::<Float16Type>(&i16_array, &DataType::Float16)
8478 .iter()
8479 .map(|i| i.parse::<f16>().unwrap())
8480 .collect::<Vec<f16>>()
8481 );
8482
8483 let i64_expected = vec!["-32768", "-128", "0", "127", "32767"];
8484 assert_eq!(
8485 i64_expected,
8486 get_cast_values::<Int64Type>(&i16_array, &DataType::Int64)
8487 );
8488
8489 let i32_expected = vec!["-32768", "-128", "0", "127", "32767"];
8490 assert_eq!(
8491 i32_expected,
8492 get_cast_values::<Int32Type>(&i16_array, &DataType::Int32)
8493 );
8494
8495 let i16_expected = vec!["-32768", "-128", "0", "127", "32767"];
8496 assert_eq!(
8497 i16_expected,
8498 get_cast_values::<Int16Type>(&i16_array, &DataType::Int16)
8499 );
8500
8501 let i8_expected = vec!["null", "-128", "0", "127", "null"];
8502 assert_eq!(
8503 i8_expected,
8504 get_cast_values::<Int8Type>(&i16_array, &DataType::Int8)
8505 );
8506
8507 let u64_expected = vec!["null", "null", "0", "127", "32767"];
8508 assert_eq!(
8509 u64_expected,
8510 get_cast_values::<UInt64Type>(&i16_array, &DataType::UInt64)
8511 );
8512
8513 let u32_expected = vec!["null", "null", "0", "127", "32767"];
8514 assert_eq!(
8515 u32_expected,
8516 get_cast_values::<UInt32Type>(&i16_array, &DataType::UInt32)
8517 );
8518
8519 let u16_expected = vec!["null", "null", "0", "127", "32767"];
8520 assert_eq!(
8521 u16_expected,
8522 get_cast_values::<UInt16Type>(&i16_array, &DataType::UInt16)
8523 );
8524
8525 let u8_expected = vec!["null", "null", "0", "127", "null"];
8526 assert_eq!(
8527 u8_expected,
8528 get_cast_values::<UInt8Type>(&i16_array, &DataType::UInt8)
8529 );
8530 }
8531
8532 #[test]
8533 fn test_cast_from_date32() {
8534 let i32_values: Vec<i32> = vec![
8535 i32::MIN,
8536 i16::MIN as i32,
8537 i8::MIN as i32,
8538 0,
8539 i8::MAX as i32,
8540 i16::MAX as i32,
8541 i32::MAX,
8542 ];
8543 let date32_array: ArrayRef = Arc::new(Date32Array::from(i32_values));
8544
8545 let i64_expected = vec![
8546 "-2147483648",
8547 "-32768",
8548 "-128",
8549 "0",
8550 "127",
8551 "32767",
8552 "2147483647",
8553 ];
8554 assert_eq!(
8555 i64_expected,
8556 get_cast_values::<Int64Type>(&date32_array, &DataType::Int64)
8557 );
8558 }
8559
8560 #[test]
8561 fn test_cast_from_int8() {
8562 let i8_values: Vec<i8> = vec![i8::MIN, 0, i8::MAX];
8563 let i8_array = Int8Array::from(i8_values);
8564
8565 let f64_expected = vec!["-128.0", "0.0", "127.0"];
8566 assert_eq!(
8567 f64_expected,
8568 get_cast_values::<Float64Type>(&i8_array, &DataType::Float64)
8569 );
8570
8571 let f32_expected = vec!["-128.0", "0.0", "127.0"];
8572 assert_eq!(
8573 f32_expected,
8574 get_cast_values::<Float32Type>(&i8_array, &DataType::Float32)
8575 );
8576
8577 let f16_expected = vec!["-128.0", "0.0", "127.0"];
8578 assert_eq!(
8579 f16_expected,
8580 get_cast_values::<Float16Type>(&i8_array, &DataType::Float16)
8581 );
8582
8583 let i64_expected = vec!["-128", "0", "127"];
8584 assert_eq!(
8585 i64_expected,
8586 get_cast_values::<Int64Type>(&i8_array, &DataType::Int64)
8587 );
8588
8589 let i32_expected = vec!["-128", "0", "127"];
8590 assert_eq!(
8591 i32_expected,
8592 get_cast_values::<Int32Type>(&i8_array, &DataType::Int32)
8593 );
8594
8595 let i16_expected = vec!["-128", "0", "127"];
8596 assert_eq!(
8597 i16_expected,
8598 get_cast_values::<Int16Type>(&i8_array, &DataType::Int16)
8599 );
8600
8601 let i8_expected = vec!["-128", "0", "127"];
8602 assert_eq!(
8603 i8_expected,
8604 get_cast_values::<Int8Type>(&i8_array, &DataType::Int8)
8605 );
8606
8607 let u64_expected = vec!["null", "0", "127"];
8608 assert_eq!(
8609 u64_expected,
8610 get_cast_values::<UInt64Type>(&i8_array, &DataType::UInt64)
8611 );
8612
8613 let u32_expected = vec!["null", "0", "127"];
8614 assert_eq!(
8615 u32_expected,
8616 get_cast_values::<UInt32Type>(&i8_array, &DataType::UInt32)
8617 );
8618
8619 let u16_expected = vec!["null", "0", "127"];
8620 assert_eq!(
8621 u16_expected,
8622 get_cast_values::<UInt16Type>(&i8_array, &DataType::UInt16)
8623 );
8624
8625 let u8_expected = vec!["null", "0", "127"];
8626 assert_eq!(
8627 u8_expected,
8628 get_cast_values::<UInt8Type>(&i8_array, &DataType::UInt8)
8629 );
8630 }
8631
8632 fn get_cast_values<T>(array: &dyn Array, dt: &DataType) -> Vec<String>
8634 where
8635 T: ArrowPrimitiveType,
8636 {
8637 let c = cast(array, dt).unwrap();
8638 let a = c.as_primitive::<T>();
8639 let mut v: Vec<String> = vec![];
8640 for i in 0..array.len() {
8641 if a.is_null(i) {
8642 v.push("null".to_string())
8643 } else {
8644 v.push(format!("{:?}", a.value(i)));
8645 }
8646 }
8647 v
8648 }
8649
8650 #[test]
8651 fn test_cast_utf8_dict() {
8652 let mut builder = StringDictionaryBuilder::<Int8Type>::new();
8654 builder.append("one").unwrap();
8655 builder.append_null();
8656 builder.append("three").unwrap();
8657 let array: ArrayRef = Arc::new(builder.finish());
8658
8659 let expected = vec!["one", "null", "three"];
8660
8661 let cast_type = Utf8;
8663 let cast_array = cast(&array, &cast_type).expect("cast to UTF-8 failed");
8664 assert_eq!(cast_array.data_type(), &cast_type);
8665 assert_eq!(array_to_strings(&cast_array), expected);
8666
8667 let cast_type = Dictionary(Box::new(Int16), Box::new(Utf8));
8670 let cast_array = cast(&array, &cast_type).expect("cast failed");
8671 assert_eq!(cast_array.data_type(), &cast_type);
8672 assert_eq!(array_to_strings(&cast_array), expected);
8673
8674 let cast_type = Dictionary(Box::new(Int32), Box::new(Utf8));
8675 let cast_array = cast(&array, &cast_type).expect("cast failed");
8676 assert_eq!(cast_array.data_type(), &cast_type);
8677 assert_eq!(array_to_strings(&cast_array), expected);
8678
8679 let cast_type = Dictionary(Box::new(Int64), Box::new(Utf8));
8680 let cast_array = cast(&array, &cast_type).expect("cast failed");
8681 assert_eq!(cast_array.data_type(), &cast_type);
8682 assert_eq!(array_to_strings(&cast_array), expected);
8683
8684 let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
8685 let cast_array = cast(&array, &cast_type).expect("cast failed");
8686 assert_eq!(cast_array.data_type(), &cast_type);
8687 assert_eq!(array_to_strings(&cast_array), expected);
8688
8689 let cast_type = Dictionary(Box::new(UInt16), Box::new(Utf8));
8690 let cast_array = cast(&array, &cast_type).expect("cast failed");
8691 assert_eq!(cast_array.data_type(), &cast_type);
8692 assert_eq!(array_to_strings(&cast_array), expected);
8693
8694 let cast_type = Dictionary(Box::new(UInt32), Box::new(Utf8));
8695 let cast_array = cast(&array, &cast_type).expect("cast failed");
8696 assert_eq!(cast_array.data_type(), &cast_type);
8697 assert_eq!(array_to_strings(&cast_array), expected);
8698
8699 let cast_type = Dictionary(Box::new(UInt64), Box::new(Utf8));
8700 let cast_array = cast(&array, &cast_type).expect("cast failed");
8701 assert_eq!(cast_array.data_type(), &cast_type);
8702 assert_eq!(array_to_strings(&cast_array), expected);
8703 }
8704
8705 #[test]
8706 fn test_cast_dict_to_dict_bad_index_value_primitive() {
8707 let mut builder = PrimitiveDictionaryBuilder::<Int32Type, Int64Type>::new();
8712
8713 for i in 0..200 {
8717 builder.append(i).unwrap();
8718 }
8719 let array: ArrayRef = Arc::new(builder.finish());
8720
8721 let cast_type = Dictionary(Box::new(Int8), Box::new(Utf8));
8722 let res = cast(&array, &cast_type);
8723 assert!(res.is_err());
8724 let actual_error = format!("{res:?}");
8725 let expected_error = "Could not convert 72 dictionary indexes from Int32 to Int8";
8726 assert!(
8727 actual_error.contains(expected_error),
8728 "did not find expected error '{actual_error}' in actual error '{expected_error}'"
8729 );
8730 }
8731
8732 #[test]
8733 fn test_cast_dict_to_dict_bad_index_value_utf8() {
8734 let mut builder = StringDictionaryBuilder::<Int32Type>::new();
8738
8739 for i in 0..200 {
8743 let val = format!("val{i}");
8744 builder.append(&val).unwrap();
8745 }
8746 let array = builder.finish();
8747
8748 let cast_type = Dictionary(Box::new(Int8), Box::new(Utf8));
8749 let res = cast(&array, &cast_type);
8750 assert!(res.is_err());
8751 let actual_error = format!("{res:?}");
8752 let expected_error = "Could not convert 72 dictionary indexes from Int32 to Int8";
8753 assert!(
8754 actual_error.contains(expected_error),
8755 "did not find expected error '{actual_error}' in actual error '{expected_error}'"
8756 );
8757 }
8758
8759 #[test]
8760 fn test_cast_primitive_dict() {
8761 let mut builder = PrimitiveDictionaryBuilder::<Int8Type, Int32Type>::new();
8763 builder.append(1).unwrap();
8764 builder.append_null();
8765 builder.append(3).unwrap();
8766 let array: ArrayRef = Arc::new(builder.finish());
8767
8768 let expected = vec!["1", "null", "3"];
8769
8770 let cast_array = cast(&array, &Utf8).expect("cast to UTF-8 failed");
8772 assert_eq!(array_to_strings(&cast_array), expected);
8773 assert_eq!(cast_array.data_type(), &Utf8);
8774
8775 let cast_array = cast(&array, &Int64).expect("cast to int64 failed");
8776 assert_eq!(array_to_strings(&cast_array), expected);
8777 assert_eq!(cast_array.data_type(), &Int64);
8778 }
8779
8780 #[test]
8781 fn test_cast_primitive_array_to_dict() {
8782 let mut builder = PrimitiveBuilder::<Int32Type>::new();
8783 builder.append_value(1);
8784 builder.append_null();
8785 builder.append_value(3);
8786 let array: ArrayRef = Arc::new(builder.finish());
8787
8788 let expected = vec!["1", "null", "3"];
8789
8790 let cast_type = Dictionary(Box::new(UInt8), Box::new(Int32));
8792 let cast_array = cast(&array, &cast_type).expect("cast failed");
8793 assert_eq!(cast_array.data_type(), &cast_type);
8794 assert_eq!(array_to_strings(&cast_array), expected);
8795
8796 let cast_type = Dictionary(Box::new(UInt8), Box::new(Int8));
8798 let cast_array = cast(&array, &cast_type).expect("cast failed");
8799 assert_eq!(cast_array.data_type(), &cast_type);
8800 assert_eq!(array_to_strings(&cast_array), expected);
8801 }
8802
8803 #[test]
8804 fn test_cast_time_array_to_dict() {
8805 use DataType::*;
8806
8807 let array = Arc::new(Date32Array::from(vec![Some(1000), None, Some(2000)])) as ArrayRef;
8808
8809 let expected = vec!["1972-09-27", "null", "1975-06-24"];
8810
8811 let cast_type = Dictionary(Box::new(UInt8), Box::new(Date32));
8812 let cast_array = cast(&array, &cast_type).expect("cast failed");
8813 assert_eq!(cast_array.data_type(), &cast_type);
8814 assert_eq!(array_to_strings(&cast_array), expected);
8815 }
8816
8817 #[test]
8818 fn test_cast_timestamp_array_to_dict() {
8819 use DataType::*;
8820
8821 let array = Arc::new(
8822 TimestampSecondArray::from(vec![Some(1000), None, Some(2000)]).with_timezone_utc(),
8823 ) as ArrayRef;
8824
8825 let expected = vec!["1970-01-01T00:16:40", "null", "1970-01-01T00:33:20"];
8826
8827 let cast_type = Dictionary(Box::new(UInt8), Box::new(Timestamp(TimeUnit::Second, None)));
8828 let cast_array = cast(&array, &cast_type).expect("cast failed");
8829 assert_eq!(cast_array.data_type(), &cast_type);
8830 assert_eq!(array_to_strings(&cast_array), expected);
8831 }
8832
8833 #[test]
8834 fn test_cast_string_array_to_dict() {
8835 use DataType::*;
8836
8837 let array = Arc::new(StringArray::from(vec![Some("one"), None, Some("three")])) as ArrayRef;
8838
8839 let expected = vec!["one", "null", "three"];
8840
8841 let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
8843 let cast_array = cast(&array, &cast_type).expect("cast failed");
8844 assert_eq!(cast_array.data_type(), &cast_type);
8845 assert_eq!(array_to_strings(&cast_array), expected);
8846 }
8847
8848 #[test]
8849 fn test_cast_null_array_to_from_decimal_array() {
8850 let data_type = DataType::Decimal128(12, 4);
8851 let array = new_null_array(&DataType::Null, 4);
8852 assert_eq!(array.data_type(), &DataType::Null);
8853 let cast_array = cast(&array, &data_type).expect("cast failed");
8854 assert_eq!(cast_array.data_type(), &data_type);
8855 for i in 0..4 {
8856 assert!(cast_array.is_null(i));
8857 }
8858
8859 let array = new_null_array(&data_type, 4);
8860 assert_eq!(array.data_type(), &data_type);
8861 let cast_array = cast(&array, &DataType::Null).expect("cast failed");
8862 assert_eq!(cast_array.data_type(), &DataType::Null);
8863 assert_eq!(cast_array.len(), 4);
8864 assert_eq!(cast_array.logical_nulls().unwrap().null_count(), 4);
8865 }
8866
8867 #[test]
8868 fn test_cast_null_array_from_and_to_primitive_array() {
8869 macro_rules! typed_test {
8870 ($ARR_TYPE:ident, $DATATYPE:ident, $TYPE:tt) => {{
8871 {
8872 let array = Arc::new(NullArray::new(6)) as ArrayRef;
8873 let expected = $ARR_TYPE::from(vec![None; 6]);
8874 let cast_type = DataType::$DATATYPE;
8875 let cast_array = cast(&array, &cast_type).expect("cast failed");
8876 let cast_array = cast_array.as_primitive::<$TYPE>();
8877 assert_eq!(cast_array.data_type(), &cast_type);
8878 assert_eq!(cast_array, &expected);
8879 }
8880 }};
8881 }
8882
8883 typed_test!(Int16Array, Int16, Int16Type);
8884 typed_test!(Int32Array, Int32, Int32Type);
8885 typed_test!(Int64Array, Int64, Int64Type);
8886
8887 typed_test!(UInt16Array, UInt16, UInt16Type);
8888 typed_test!(UInt32Array, UInt32, UInt32Type);
8889 typed_test!(UInt64Array, UInt64, UInt64Type);
8890
8891 typed_test!(Float16Array, Float16, Float16Type);
8892 typed_test!(Float32Array, Float32, Float32Type);
8893 typed_test!(Float64Array, Float64, Float64Type);
8894
8895 typed_test!(Date32Array, Date32, Date32Type);
8896 typed_test!(Date64Array, Date64, Date64Type);
8897 }
8898
8899 fn cast_from_null_to_other_base(data_type: &DataType, is_complex: bool) {
8900 let array = new_null_array(&DataType::Null, 4);
8902 assert_eq!(array.data_type(), &DataType::Null);
8903 let cast_array = cast(&array, data_type).expect("cast failed");
8904 assert_eq!(cast_array.data_type(), data_type);
8905 for i in 0..4 {
8906 if is_complex {
8907 assert!(cast_array.logical_nulls().unwrap().is_null(i));
8908 } else {
8909 assert!(cast_array.is_null(i));
8910 }
8911 }
8912 }
8913
8914 fn cast_from_null_to_other(data_type: &DataType) {
8915 cast_from_null_to_other_base(data_type, false);
8916 }
8917
8918 fn cast_from_null_to_other_complex(data_type: &DataType) {
8919 cast_from_null_to_other_base(data_type, true);
8920 }
8921
8922 #[test]
8923 fn test_cast_null_from_and_to_variable_sized() {
8924 cast_from_null_to_other(&DataType::Utf8);
8925 cast_from_null_to_other(&DataType::LargeUtf8);
8926 cast_from_null_to_other(&DataType::Binary);
8927 cast_from_null_to_other(&DataType::LargeBinary);
8928 }
8929
8930 #[test]
8931 fn test_cast_null_from_and_to_nested_type() {
8932 let data_type = DataType::Map(
8934 Arc::new(Field::new_struct(
8935 "entry",
8936 vec![
8937 Field::new("key", DataType::Utf8, false),
8938 Field::new("value", DataType::Int32, true),
8939 ],
8940 false,
8941 )),
8942 false,
8943 );
8944 cast_from_null_to_other(&data_type);
8945
8946 let data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
8948 cast_from_null_to_other(&data_type);
8949 let data_type = DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
8950 cast_from_null_to_other(&data_type);
8951 let data_type =
8952 DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 4);
8953 cast_from_null_to_other(&data_type);
8954
8955 let values = vec![None, None, None, None] as Vec<Option<&str>>;
8957 let array: DictionaryArray<Int8Type> = values.into_iter().collect();
8958 let array = Arc::new(array) as ArrayRef;
8959 let data_type = array.data_type().to_owned();
8960 cast_from_null_to_other(&data_type);
8961
8962 let data_type = DataType::Struct(vec![Field::new("data", DataType::Int64, false)].into());
8964 cast_from_null_to_other(&data_type);
8965
8966 let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Int32, true)));
8967 cast_from_null_to_other(&target_type);
8968
8969 let target_type =
8970 DataType::LargeListView(Arc::new(Field::new("item", DataType::Int32, true)));
8971 cast_from_null_to_other(&target_type);
8972
8973 let fields = UnionFields::from_fields(vec![Field::new("a", DataType::Int64, false)]);
8974 let target_type = DataType::Union(fields, UnionMode::Sparse);
8975 cast_from_null_to_other_complex(&target_type);
8976
8977 let target_type = DataType::RunEndEncoded(
8978 Arc::new(Field::new("item", DataType::Int32, true)),
8979 Arc::new(Field::new("item", DataType::Int32, true)),
8980 );
8981 cast_from_null_to_other_complex(&target_type);
8982 }
8983
8984 fn array_to_strings(array: &ArrayRef) -> Vec<String> {
8986 let options = FormatOptions::new().with_null("null");
8987 let formatter = ArrayFormatter::try_new(array.as_ref(), &options).unwrap();
8988 (0..array.len())
8989 .map(|i| formatter.value(i).to_string())
8990 .collect()
8991 }
8992
8993 #[test]
8994 fn test_cast_utf8_to_date32() {
8995 use chrono::NaiveDate;
8996 let from_ymd = chrono::NaiveDate::from_ymd_opt;
8997 let since = chrono::NaiveDate::signed_duration_since;
8998
8999 let a = StringArray::from(vec![
9000 "2000-01-01", "2000-01-01T12:00:00", "2000-2-2", "2000-00-00", "2000", ]);
9006 let array = Arc::new(a) as ArrayRef;
9007 let b = cast(&array, &DataType::Date32).unwrap();
9008 let c = b.as_primitive::<Date32Type>();
9009
9010 let date_value = since(
9012 NaiveDate::from_ymd_opt(2000, 1, 1).unwrap(),
9013 from_ymd(1970, 1, 1).unwrap(),
9014 )
9015 .num_days() as i32;
9016 assert!(c.is_valid(0)); assert_eq!(date_value, c.value(0));
9018
9019 assert!(c.is_valid(1)); assert_eq!(date_value, c.value(1));
9021
9022 let date_value = since(
9023 NaiveDate::from_ymd_opt(2000, 2, 2).unwrap(),
9024 from_ymd(1970, 1, 1).unwrap(),
9025 )
9026 .num_days() as i32;
9027 assert!(c.is_valid(2)); assert_eq!(date_value, c.value(2));
9029
9030 assert!(!c.is_valid(3)); assert!(!c.is_valid(4)); }
9034
9035 #[test]
9036 fn test_cast_utf8_to_date64() {
9037 let a = StringArray::from(vec![
9038 "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", ]);
9045 let array = Arc::new(a) as ArrayRef;
9046 let b = cast(&array, &DataType::Date64).unwrap();
9047 let c = b.as_primitive::<Date64Type>();
9048
9049 assert!(c.is_valid(0)); assert_eq!(946728000000, c.value(0));
9052 assert!(c.is_valid(1)); assert_eq!(1608035696000, c.value(1));
9054 assert!(!c.is_valid(2)); assert!(!c.is_valid(3)); assert!(c.is_valid(4)); assert_eq!(946728000000, c.value(4));
9059 assert!(c.is_valid(5)); assert_eq!(946684800000, c.value(5));
9061 }
9062
9063 #[test]
9064 fn test_can_cast_fsl_to_fsl() {
9065 let from_array = Arc::new(
9066 FixedSizeListArray::from_iter_primitive::<Float32Type, _, _>(
9067 [Some([Some(1.0), Some(2.0)]), None],
9068 2,
9069 ),
9070 ) as ArrayRef;
9071 let to_array = Arc::new(
9072 FixedSizeListArray::from_iter_primitive::<Float16Type, _, _>(
9073 [
9074 Some([Some(f16::from_f32(1.0)), Some(f16::from_f32(2.0))]),
9075 None,
9076 ],
9077 2,
9078 ),
9079 ) as ArrayRef;
9080
9081 assert!(can_cast_types(from_array.data_type(), to_array.data_type()));
9082 let actual = cast(&from_array, to_array.data_type()).unwrap();
9083 assert_eq!(actual.data_type(), to_array.data_type());
9084
9085 let invalid_target =
9086 DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Binary, true)), 2);
9087 assert!(!can_cast_types(from_array.data_type(), &invalid_target));
9088
9089 let invalid_size =
9090 DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Float16, true)), 5);
9091 assert!(!can_cast_types(from_array.data_type(), &invalid_size));
9092 }
9093
9094 #[test]
9095 fn test_can_cast_types_fixed_size_list_to_list() {
9096 let array1 = make_fixed_size_list_array();
9098 assert!(can_cast_types(
9099 array1.data_type(),
9100 &DataType::List(Arc::new(Field::new("", DataType::Int32, false)))
9101 ));
9102
9103 let array2 = make_fixed_size_list_array_for_large_list();
9105 assert!(can_cast_types(
9106 array2.data_type(),
9107 &DataType::LargeList(Arc::new(Field::new("", DataType::Int64, false)))
9108 ));
9109 }
9110
9111 #[test]
9112 fn test_cast_fixed_size_list_to_list() {
9113 let cases = [
9119 (
9121 Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9122 [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
9123 2,
9124 )) as ArrayRef,
9125 Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>([
9126 Some([Some(1), Some(1)]),
9127 Some([Some(2), Some(2)]),
9128 ])) as ArrayRef,
9129 ),
9130 (
9132 Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9133 [None, Some([Some(2), Some(2)])],
9134 2,
9135 )) as ArrayRef,
9136 Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>([
9137 None,
9138 Some([Some(2), Some(2)]),
9139 ])) as ArrayRef,
9140 ),
9141 (
9143 Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9144 [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
9145 2,
9146 )) as ArrayRef,
9147 Arc::new(LargeListArray::from_iter_primitive::<Int64Type, _, _>([
9148 Some([Some(1), Some(1)]),
9149 Some([Some(2), Some(2)]),
9150 ])) as ArrayRef,
9151 ),
9152 (
9154 Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9155 [None, Some([Some(2), Some(2)])],
9156 2,
9157 )) as ArrayRef,
9158 Arc::new(LargeListArray::from_iter_primitive::<Int64Type, _, _>([
9159 None,
9160 Some([Some(2), Some(2)]),
9161 ])) as ArrayRef,
9162 ),
9163 (
9165 Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9166 [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
9167 2,
9168 )) as ArrayRef,
9169 Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>([
9170 Some([Some(1), Some(1)]),
9171 Some([Some(2), Some(2)]),
9172 ])) as ArrayRef,
9173 ),
9174 (
9176 Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9177 [None, Some([Some(2), Some(2)])],
9178 2,
9179 )) as ArrayRef,
9180 Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>([
9181 None,
9182 Some([Some(2), Some(2)]),
9183 ])) as ArrayRef,
9184 ),
9185 (
9187 Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9188 [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
9189 2,
9190 )) as ArrayRef,
9191 Arc::new(LargeListViewArray::from_iter_primitive::<Int64Type, _, _>(
9192 [Some([Some(1), Some(1)]), Some([Some(2), Some(2)])],
9193 )) as ArrayRef,
9194 ),
9195 (
9197 Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9198 [None, Some([Some(2), Some(2)])],
9199 2,
9200 )) as ArrayRef,
9201 Arc::new(LargeListViewArray::from_iter_primitive::<Int64Type, _, _>(
9202 [None, Some([Some(2), Some(2)])],
9203 )) as ArrayRef,
9204 ),
9205 ];
9206
9207 for (array, expected) in cases {
9208 assert!(
9209 can_cast_types(array.data_type(), expected.data_type()),
9210 "can_cast_types claims we cannot cast {:?} to {:?}",
9211 array.data_type(),
9212 expected.data_type()
9213 );
9214
9215 let list_array = cast(&array, expected.data_type())
9216 .unwrap_or_else(|_| panic!("Failed to cast {array:?} to {expected:?}"));
9217 assert_eq!(
9218 list_array.as_ref(),
9219 &expected,
9220 "Incorrect result from casting {array:?} to {expected:?}",
9221 );
9222 }
9223 }
9224
9225 #[test]
9226 fn test_cast_fixed_size_list_to_list_preserves_field_metadata() {
9227 use std::collections::HashMap;
9228
9229 let metadata: HashMap<String, String> =
9230 HashMap::from([("PARQUET:field_id".to_string(), "89".to_string())]);
9231
9232 let src = Arc::new(
9233 FixedSizeListArray::from_iter_primitive::<Float32Type, _, _>(
9234 [[1.0_f32, 2.0].map(Some), [3.0, 4.0].map(Some)].map(Some),
9235 2,
9236 ),
9237 ) as ArrayRef;
9238
9239 let target_field = Arc::new(
9240 Field::new("element", DataType::Float32, true).with_metadata(metadata.clone()),
9241 );
9242
9243 let target_types = [
9244 DataType::List(target_field.clone()),
9245 DataType::LargeList(target_field.clone()),
9246 DataType::ListView(target_field.clone()),
9247 DataType::LargeListView(target_field.clone()),
9248 ];
9249
9250 for target_type in &target_types {
9251 let result = cast(&src, target_type).unwrap();
9252 assert_eq!(
9253 result.data_type(),
9254 target_type,
9255 "Cast to {target_type:?} should preserve field metadata"
9256 );
9257 }
9258 }
9259
9260 #[test]
9261 fn test_cast_utf8_to_list() {
9262 let array = Arc::new(StringArray::from(vec!["5"])) as ArrayRef;
9264 let field = Arc::new(Field::new("", DataType::Int32, false));
9265 let list_array = cast(&array, &DataType::List(field.clone())).unwrap();
9266 let actual = list_array.as_list_opt::<i32>().unwrap();
9267 let expect = ListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
9268 assert_eq!(&expect.value(0), &actual.value(0));
9269
9270 let list_array = cast(&array, &DataType::LargeList(field.clone())).unwrap();
9272 let actual = list_array.as_list_opt::<i64>().unwrap();
9273 let expect = LargeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
9274 assert_eq!(&expect.value(0), &actual.value(0));
9275
9276 let list_array = cast(&array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
9278 let actual = list_array.as_fixed_size_list_opt().unwrap();
9279 let expect =
9280 FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])], 1);
9281 assert_eq!(&expect.value(0), &actual.value(0));
9282 }
9283
9284 #[test]
9285 fn test_cast_single_element_fixed_size_list() {
9286 let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
9288 [(Some([Some(5)]))],
9289 1,
9290 )) as ArrayRef;
9291 let casted_array = cast(&from_array, &DataType::Int32).unwrap();
9292 let actual: &Int32Array = casted_array.as_primitive();
9293 let expected = Int32Array::from(vec![Some(5)]);
9294 assert_eq!(&expected, actual);
9295
9296 let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
9298 [(Some([Some(5)]))],
9299 1,
9300 )) as ArrayRef;
9301 let to_field = Arc::new(Field::new("dummy", DataType::Float32, false));
9302 let actual = cast(&from_array, &DataType::FixedSizeList(to_field.clone(), 1)).unwrap();
9303 let expected = Arc::new(FixedSizeListArray::new(
9304 to_field.clone(),
9305 1,
9306 Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
9307 None,
9308 )) as ArrayRef;
9309 assert_eq!(*expected, *actual);
9310
9311 let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
9313 [(Some([Some(5)]))],
9314 1,
9315 )) as ArrayRef;
9316 let to_field_inner = Arc::new(Field::new_list_field(DataType::Float32, false));
9317 let to_field = Arc::new(Field::new(
9318 "dummy",
9319 DataType::FixedSizeList(to_field_inner.clone(), 1),
9320 false,
9321 ));
9322 let actual = cast(&from_array, &DataType::FixedSizeList(to_field.clone(), 1)).unwrap();
9323 let expected = Arc::new(FixedSizeListArray::new(
9324 to_field.clone(),
9325 1,
9326 Arc::new(FixedSizeListArray::new(
9327 to_field_inner.clone(),
9328 1,
9329 Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
9330 None,
9331 )) as ArrayRef,
9332 None,
9333 )) as ArrayRef;
9334 assert_eq!(*expected, *actual);
9335
9336 let field = Arc::new(Field::new("dummy", DataType::Float32, false));
9338 let from_array = Arc::new(Int8Array::from(vec![Some(5)])) as ArrayRef;
9339 let casted_array = cast(&from_array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
9340 let actual = casted_array.as_fixed_size_list();
9341 let expected = Arc::new(FixedSizeListArray::new(
9342 field.clone(),
9343 1,
9344 Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
9345 None,
9346 )) as ArrayRef;
9347 assert_eq!(expected.as_ref(), actual);
9348
9349 let field = Arc::new(Field::new("nullable", DataType::Float32, true));
9351 let from_array = Arc::new(Int8Array::from(vec![None])) as ArrayRef;
9352 let casted_array = cast(&from_array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
9353 let actual = casted_array.as_fixed_size_list();
9354 let expected = Arc::new(FixedSizeListArray::new(
9355 field.clone(),
9356 1,
9357 Arc::new(Float32Array::from(vec![None])) as ArrayRef,
9358 None,
9359 )) as ArrayRef;
9360 assert_eq!(expected.as_ref(), actual);
9361 }
9362
9363 #[test]
9364 fn test_cast_list_containers() {
9365 let array = make_large_list_array();
9367 let list_array = cast(
9368 &array,
9369 &DataType::List(Arc::new(Field::new("", DataType::Int32, false))),
9370 )
9371 .unwrap();
9372 let actual = list_array.as_any().downcast_ref::<ListArray>().unwrap();
9373 let expected = array.as_any().downcast_ref::<LargeListArray>().unwrap();
9374
9375 assert_eq!(&expected.value(0), &actual.value(0));
9376 assert_eq!(&expected.value(1), &actual.value(1));
9377 assert_eq!(&expected.value(2), &actual.value(2));
9378
9379 let array = make_list_array();
9381 let large_list_array = cast(
9382 &array,
9383 &DataType::LargeList(Arc::new(Field::new("", DataType::Int32, false))),
9384 )
9385 .unwrap();
9386 let actual = large_list_array
9387 .as_any()
9388 .downcast_ref::<LargeListArray>()
9389 .unwrap();
9390 let expected = array.as_any().downcast_ref::<ListArray>().unwrap();
9391
9392 assert_eq!(&expected.value(0), &actual.value(0));
9393 assert_eq!(&expected.value(1), &actual.value(1));
9394 assert_eq!(&expected.value(2), &actual.value(2));
9395 }
9396
9397 #[test]
9398 fn test_cast_list_view() {
9399 let array = make_list_view_array();
9401 let to = DataType::ListView(Field::new_list_field(DataType::Float32, true).into());
9402 assert!(can_cast_types(array.data_type(), &to));
9403 let actual = cast(&array, &to).unwrap();
9404 let actual = actual.as_list_view::<i32>();
9405
9406 assert_eq!(
9407 &Float32Array::from(vec![0.0, 1.0, 2.0]) as &dyn Array,
9408 actual.value(0).as_ref()
9409 );
9410 assert_eq!(
9411 &Float32Array::from(vec![3.0, 4.0, 5.0]) as &dyn Array,
9412 actual.value(1).as_ref()
9413 );
9414 assert_eq!(
9415 &Float32Array::from(vec![6.0, 7.0]) as &dyn Array,
9416 actual.value(2).as_ref()
9417 );
9418
9419 let array = make_large_list_view_array();
9421 let to = DataType::LargeListView(Field::new_list_field(DataType::Float32, true).into());
9422 assert!(can_cast_types(array.data_type(), &to));
9423 let actual = cast(&array, &to).unwrap();
9424 let actual = actual.as_list_view::<i64>();
9425
9426 assert_eq!(
9427 &Float32Array::from(vec![0.0, 1.0, 2.0]) as &dyn Array,
9428 actual.value(0).as_ref()
9429 );
9430 assert_eq!(
9431 &Float32Array::from(vec![3.0, 4.0, 5.0]) as &dyn Array,
9432 actual.value(1).as_ref()
9433 );
9434 assert_eq!(
9435 &Float32Array::from(vec![6.0, 7.0]) as &dyn Array,
9436 actual.value(2).as_ref()
9437 );
9438 }
9439
9440 #[test]
9441 fn test_non_list_to_list_view() {
9442 let input = Arc::new(Int32Array::from(vec![Some(0), None, Some(2)])) as ArrayRef;
9443 let expected_primitive =
9444 Arc::new(Float32Array::from(vec![Some(0.0), None, Some(2.0)])) as ArrayRef;
9445
9446 let expected = ListViewArray::new(
9448 Field::new_list_field(DataType::Float32, true).into(),
9449 vec![0, 1, 2].into(),
9450 vec![1, 1, 1].into(),
9451 expected_primitive.clone(),
9452 None,
9453 );
9454 assert!(can_cast_types(input.data_type(), expected.data_type()));
9455 let actual = cast(&input, expected.data_type()).unwrap();
9456 assert_eq!(actual.as_ref(), &expected);
9457
9458 let expected = LargeListViewArray::new(
9460 Field::new_list_field(DataType::Float32, true).into(),
9461 vec![0, 1, 2].into(),
9462 vec![1, 1, 1].into(),
9463 expected_primitive.clone(),
9464 None,
9465 );
9466 assert!(can_cast_types(input.data_type(), expected.data_type()));
9467 let actual = cast(&input, expected.data_type()).unwrap();
9468 assert_eq!(actual.as_ref(), &expected);
9469 }
9470
9471 #[test]
9472 fn test_cast_list_to_fsl() {
9473 let field = Arc::new(Field::new_list_field(DataType::Int32, true));
9481 let values = vec![
9482 Some(vec![Some(1), Some(2), Some(3)]),
9483 Some(vec![Some(4), Some(5), Some(6)]),
9484 ];
9485 let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(
9486 values.clone(),
9487 )) as ArrayRef;
9488 let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9489 values, 3,
9490 )) as ArrayRef;
9491 let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
9492 assert_eq!(expected.as_ref(), actual.as_ref());
9493
9494 let cases = [
9497 (
9498 vec![1, 2, 3, 4, 5, 6],
9500 vec![3, 0, 3, 0],
9501 ),
9502 (
9503 vec![1, 2, 3, 0, 0, 4, 5, 6, 0],
9505 vec![3, 2, 3, 1],
9506 ),
9507 (
9508 vec![1, 2, 3, 0, 0, 0, 4, 5, 6, 0, 0, 0],
9510 vec![3, 3, 3, 3],
9511 ),
9512 (
9513 vec![1, 2, 3, 4, 5, 6, 0, 0, 0],
9515 vec![3, 0, 3, 3],
9516 ),
9517 ];
9518 let null_buffer = NullBuffer::from(vec![true, false, true, false]);
9519
9520 let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9521 vec![
9522 Some(vec![Some(1), Some(2), Some(3)]),
9523 None,
9524 Some(vec![Some(4), Some(5), Some(6)]),
9525 None,
9526 ],
9527 3,
9528 )) as ArrayRef;
9529
9530 for (values, lengths) in cases.iter() {
9531 let array = Arc::new(ListArray::new(
9532 field.clone(),
9533 OffsetBuffer::from_lengths(lengths.clone()),
9534 Arc::new(Int32Array::from(values.clone())),
9535 Some(null_buffer.clone()),
9536 )) as ArrayRef;
9537 let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
9538 assert_eq!(expected.as_ref(), actual.as_ref());
9539 }
9540 }
9541
9542 #[test]
9543 fn test_cast_list_view_to_fsl() {
9544 let field = Arc::new(Field::new_list_field(DataType::Int32, true));
9552 let values = vec![
9553 Some(vec![Some(1), Some(2), Some(3)]),
9554 Some(vec![Some(4), Some(5), Some(6)]),
9555 ];
9556 let array = Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>(
9557 values.clone(),
9558 )) as ArrayRef;
9559 let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9560 values, 3,
9561 )) as ArrayRef;
9562 let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
9563 assert_eq!(expected.as_ref(), actual.as_ref());
9564
9565 let cases = [
9568 (
9569 vec![1, 2, 3, 4, 5, 6],
9571 vec![0, 0, 3, 0],
9572 vec![3, 0, 3, 0],
9573 ),
9574 (
9575 vec![1, 2, 3, 0, 0, 4, 5, 6, 0],
9577 vec![0, 1, 5, 0],
9578 vec![3, 2, 3, 1],
9579 ),
9580 (
9581 vec![1, 2, 3, 0, 0, 0, 4, 5, 6, 0, 0, 0],
9583 vec![0, 3, 6, 9],
9584 vec![3, 3, 3, 3],
9585 ),
9586 (
9587 vec![1, 2, 3, 4, 5, 6, 0, 0, 0],
9589 vec![0, 0, 3, 6],
9590 vec![3, 0, 3, 3],
9591 ),
9592 ];
9593 let null_buffer = NullBuffer::from(vec![true, false, true, false]);
9594
9595 let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9596 vec![
9597 Some(vec![Some(1), Some(2), Some(3)]),
9598 None,
9599 Some(vec![Some(4), Some(5), Some(6)]),
9600 None,
9601 ],
9602 3,
9603 )) as ArrayRef;
9604
9605 for (values, offsets, lengths) in cases.iter() {
9606 let array = Arc::new(ListViewArray::new(
9607 field.clone(),
9608 offsets.clone().into(),
9609 lengths.clone().into(),
9610 Arc::new(Int32Array::from(values.clone())),
9611 Some(null_buffer.clone()),
9612 )) as ArrayRef;
9613 let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
9614 assert_eq!(expected.as_ref(), actual.as_ref());
9615 }
9616 }
9617
9618 #[test]
9619 fn test_cast_list_to_fsl_safety() {
9620 let values = vec![
9621 Some(vec![Some(1), Some(2), Some(3)]),
9622 Some(vec![Some(4), Some(5)]),
9623 Some(vec![Some(6), Some(7), Some(8), Some(9)]),
9624 Some(vec![Some(3), Some(4), Some(5)]),
9625 ];
9626 let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(
9627 values.clone(),
9628 )) as ArrayRef;
9629
9630 let res = cast_with_options(
9631 array.as_ref(),
9632 &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9633 &CastOptions {
9634 safe: false,
9635 ..Default::default()
9636 },
9637 );
9638 assert!(res.is_err());
9639 assert!(
9640 format!("{res:?}")
9641 .contains("Cannot cast to FixedSizeList(3): value at index 1 has length 2")
9642 );
9643
9644 let res = cast(
9647 array.as_ref(),
9648 &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9649 )
9650 .unwrap();
9651 let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9652 vec![
9653 Some(vec![Some(1), Some(2), Some(3)]),
9654 None, None, Some(vec![Some(3), Some(4), Some(5)]),
9657 ],
9658 3,
9659 )) as ArrayRef;
9660 assert_eq!(expected.as_ref(), res.as_ref());
9661
9662 let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
9665 Some(vec![Some(1), Some(2), Some(3)]),
9666 None,
9667 ])) as ArrayRef;
9668 let res = cast_with_options(
9669 array.as_ref(),
9670 &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9671 &CastOptions {
9672 safe: false,
9673 ..Default::default()
9674 },
9675 )
9676 .unwrap();
9677 let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9678 vec![Some(vec![Some(1), Some(2), Some(3)]), None],
9679 3,
9680 )) as ArrayRef;
9681 assert_eq!(expected.as_ref(), res.as_ref());
9682 }
9683
9684 #[test]
9685 fn test_cast_list_view_to_fsl_safety() {
9686 let values = vec![
9687 Some(vec![Some(1), Some(2), Some(3)]),
9688 Some(vec![Some(4), Some(5)]),
9689 Some(vec![Some(6), Some(7), Some(8), Some(9)]),
9690 Some(vec![Some(3), Some(4), Some(5)]),
9691 ];
9692 let array = Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>(
9693 values.clone(),
9694 )) as ArrayRef;
9695
9696 let res = cast_with_options(
9697 array.as_ref(),
9698 &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9699 &CastOptions {
9700 safe: false,
9701 ..Default::default()
9702 },
9703 );
9704 assert!(res.is_err());
9705 assert!(
9706 format!("{res:?}")
9707 .contains("Cannot cast to FixedSizeList(3): value at index 1 has length 2")
9708 );
9709
9710 let res = cast(
9713 array.as_ref(),
9714 &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9715 )
9716 .unwrap();
9717 let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9718 vec![
9719 Some(vec![Some(1), Some(2), Some(3)]),
9720 None, None, Some(vec![Some(3), Some(4), Some(5)]),
9723 ],
9724 3,
9725 )) as ArrayRef;
9726 assert_eq!(expected.as_ref(), res.as_ref());
9727
9728 let array = Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>(vec![
9731 Some(vec![Some(1), Some(2), Some(3)]),
9732 None,
9733 ])) as ArrayRef;
9734 let res = cast_with_options(
9735 array.as_ref(),
9736 &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9737 &CastOptions {
9738 safe: false,
9739 ..Default::default()
9740 },
9741 )
9742 .unwrap();
9743 let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9744 vec![Some(vec![Some(1), Some(2), Some(3)]), None],
9745 3,
9746 )) as ArrayRef;
9747 assert_eq!(expected.as_ref(), res.as_ref());
9748 }
9749
9750 #[test]
9751 fn test_cast_large_list_to_fsl() {
9752 let values = vec![Some(vec![Some(1), Some(2)]), Some(vec![Some(3), Some(4)])];
9753 let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9754 values.clone(),
9755 2,
9756 )) as ArrayRef;
9757 let target_type =
9758 DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 2);
9759
9760 let array = Arc::new(LargeListArray::from_iter_primitive::<Int32Type, _, _>(
9761 values.clone(),
9762 )) as ArrayRef;
9763 let actual = cast(array.as_ref(), &target_type).unwrap();
9764 assert_eq!(expected.as_ref(), actual.as_ref());
9765
9766 let array = Arc::new(LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(
9767 values.clone(),
9768 )) as ArrayRef;
9769 let actual = cast(array.as_ref(), &target_type).unwrap();
9770 assert_eq!(expected.as_ref(), actual.as_ref());
9771 }
9772
9773 #[test]
9774 fn test_cast_list_to_fsl_subcast() {
9775 let array = Arc::new(LargeListArray::from_iter_primitive::<Int32Type, _, _>(
9776 vec![
9777 Some(vec![Some(1), Some(2)]),
9778 Some(vec![Some(3), Some(i32::MAX)]),
9779 ],
9780 )) as ArrayRef;
9781 let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
9782 vec![
9783 Some(vec![Some(1), Some(2)]),
9784 Some(vec![Some(3), Some(i32::MAX as i64)]),
9785 ],
9786 2,
9787 )) as ArrayRef;
9788 let actual = cast(
9789 array.as_ref(),
9790 &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int64, true)), 2),
9791 )
9792 .unwrap();
9793 assert_eq!(expected.as_ref(), actual.as_ref());
9794
9795 let res = cast_with_options(
9796 array.as_ref(),
9797 &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int16, true)), 2),
9798 &CastOptions {
9799 safe: false,
9800 ..Default::default()
9801 },
9802 );
9803 assert!(res.is_err());
9804 assert!(format!("{res:?}").contains("Can't cast value 2147483647 to type Int16"));
9805 }
9806
9807 #[test]
9808 fn test_cast_list_to_fsl_empty() {
9809 let inner_field = Arc::new(Field::new_list_field(DataType::Int32, true));
9810 let target_type = DataType::FixedSizeList(inner_field.clone(), 3);
9811 let expected = new_empty_array(&target_type);
9812
9813 let array = new_empty_array(&DataType::List(inner_field.clone()));
9815 assert!(can_cast_types(array.data_type(), &target_type));
9816 let actual = cast(array.as_ref(), &target_type).unwrap();
9817 assert_eq!(expected.as_ref(), actual.as_ref());
9818
9819 let array = new_empty_array(&DataType::LargeList(inner_field.clone()));
9821 assert!(can_cast_types(array.data_type(), &target_type));
9822 let actual = cast(array.as_ref(), &target_type).unwrap();
9823 assert_eq!(expected.as_ref(), actual.as_ref());
9824
9825 let array = new_empty_array(&DataType::ListView(inner_field.clone()));
9827 assert!(can_cast_types(array.data_type(), &target_type));
9828 let actual = cast(array.as_ref(), &target_type).unwrap();
9829 assert_eq!(expected.as_ref(), actual.as_ref());
9830
9831 let array = new_empty_array(&DataType::LargeListView(inner_field.clone()));
9833 assert!(can_cast_types(array.data_type(), &target_type));
9834 let actual = cast(array.as_ref(), &target_type).unwrap();
9835 assert_eq!(expected.as_ref(), actual.as_ref());
9836 }
9837
9838 fn make_list_array() -> ArrayRef {
9839 Arc::new(ListArray::new(
9841 Field::new_list_field(DataType::Int32, true).into(),
9842 OffsetBuffer::from_lengths(vec![3, 3, 2]),
9843 Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
9844 None,
9845 ))
9846 }
9847
9848 fn make_large_list_array() -> ArrayRef {
9849 Arc::new(LargeListArray::new(
9851 Field::new_list_field(DataType::Int32, true).into(),
9852 OffsetBuffer::from_lengths(vec![3, 3, 2]),
9853 Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
9854 None,
9855 ))
9856 }
9857
9858 fn make_list_view_array() -> ArrayRef {
9859 Arc::new(ListViewArray::new(
9861 Field::new_list_field(DataType::Int32, true).into(),
9862 vec![0, 3, 6].into(),
9863 vec![3, 3, 2].into(),
9864 Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
9865 None,
9866 ))
9867 }
9868
9869 fn make_large_list_view_array() -> ArrayRef {
9870 Arc::new(LargeListViewArray::new(
9872 Field::new_list_field(DataType::Int32, true).into(),
9873 vec![0, 3, 6].into(),
9874 vec![3, 3, 2].into(),
9875 Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
9876 None,
9877 ))
9878 }
9879
9880 fn make_fixed_size_list_array() -> ArrayRef {
9881 Arc::new(FixedSizeListArray::new(
9883 Field::new_list_field(DataType::Int32, true).into(),
9884 4,
9885 Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
9886 None,
9887 ))
9888 }
9889
9890 fn make_fixed_size_list_array_for_large_list() -> ArrayRef {
9891 Arc::new(FixedSizeListArray::new(
9893 Field::new_list_field(DataType::Int64, true).into(),
9894 4,
9895 Arc::new(Int64Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
9896 None,
9897 ))
9898 }
9899
9900 #[test]
9901 fn test_cast_map_dont_allow_change_of_order() {
9902 let string_builder = StringBuilder::new();
9903 let value_builder = StringBuilder::new();
9904 let mut builder = MapBuilder::new(
9905 Some(MapFieldNames {
9906 entry: "entries".to_string(),
9907 key: "key".to_string(),
9908 value: "value".to_string(),
9909 }),
9910 string_builder,
9911 value_builder,
9912 );
9913
9914 builder.keys().append_value("0");
9915 builder.values().append_value("test_val_1");
9916 builder.append(true).unwrap();
9917 builder.keys().append_value("1");
9918 builder.values().append_value("test_val_2");
9919 builder.append(true).unwrap();
9920
9921 let array = builder.finish();
9923
9924 let new_ordered = true;
9925 let new_type = DataType::Map(
9926 Arc::new(Field::new(
9927 "entries",
9928 DataType::Struct(
9929 vec![
9930 Field::new("key", DataType::Utf8, false),
9931 Field::new("value", DataType::Utf8, false),
9932 ]
9933 .into(),
9934 ),
9935 false,
9936 )),
9937 new_ordered,
9938 );
9939
9940 let new_array_result = cast(&array, &new_type.clone());
9941 assert!(!can_cast_types(array.data_type(), &new_type));
9942 let Err(ArrowError::CastError(t)) = new_array_result else {
9943 panic!();
9944 };
9945 assert_eq!(
9946 t,
9947 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"#
9948 );
9949 }
9950
9951 #[test]
9952 fn test_cast_map_dont_allow_when_container_cant_cast() {
9953 let string_builder = StringBuilder::new();
9954 let value_builder = IntervalDayTimeArray::builder(2);
9955 let mut builder = MapBuilder::new(
9956 Some(MapFieldNames {
9957 entry: "entries".to_string(),
9958 key: "key".to_string(),
9959 value: "value".to_string(),
9960 }),
9961 string_builder,
9962 value_builder,
9963 );
9964
9965 builder.keys().append_value("0");
9966 builder.values().append_value(IntervalDayTime::new(1, 1));
9967 builder.append(true).unwrap();
9968 builder.keys().append_value("1");
9969 builder.values().append_value(IntervalDayTime::new(2, 2));
9970 builder.append(true).unwrap();
9971
9972 let array = builder.finish();
9974
9975 let new_ordered = true;
9976 let new_type = DataType::Map(
9977 Arc::new(Field::new(
9978 "entries",
9979 DataType::Struct(
9980 vec![
9981 Field::new("key", DataType::Utf8, false),
9982 Field::new("value", DataType::Duration(TimeUnit::Second), false),
9983 ]
9984 .into(),
9985 ),
9986 false,
9987 )),
9988 new_ordered,
9989 );
9990
9991 let new_array_result = cast(&array, &new_type.clone());
9992 assert!(!can_cast_types(array.data_type(), &new_type));
9993 let Err(ArrowError::CastError(t)) = new_array_result else {
9994 panic!();
9995 };
9996 assert_eq!(
9997 t,
9998 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"#
9999 );
10000 }
10001
10002 #[test]
10003 fn test_cast_map_field_names() {
10004 let string_builder = StringBuilder::new();
10005 let value_builder = StringBuilder::new();
10006 let mut builder = MapBuilder::new(
10007 Some(MapFieldNames {
10008 entry: "entries".to_string(),
10009 key: "key".to_string(),
10010 value: "value".to_string(),
10011 }),
10012 string_builder,
10013 value_builder,
10014 );
10015
10016 builder.keys().append_value("0");
10017 builder.values().append_value("test_val_1");
10018 builder.append(true).unwrap();
10019 builder.keys().append_value("1");
10020 builder.values().append_value("test_val_2");
10021 builder.append(true).unwrap();
10022 builder.append(false).unwrap();
10023
10024 let array = builder.finish();
10025
10026 let new_type = DataType::Map(
10027 Arc::new(Field::new(
10028 "entries_new",
10029 DataType::Struct(
10030 vec![
10031 Field::new("key_new", DataType::Utf8, false),
10032 Field::new("value_values", DataType::Utf8, false),
10033 ]
10034 .into(),
10035 ),
10036 false,
10037 )),
10038 false,
10039 );
10040
10041 assert_ne!(new_type, array.data_type().clone());
10042
10043 let new_array = cast(&array, &new_type.clone()).unwrap();
10044 assert_eq!(new_type, new_array.data_type().clone());
10045 let map_array = new_array.as_map();
10046
10047 assert_ne!(new_type, array.data_type().clone());
10048 assert_eq!(new_type, map_array.data_type().clone());
10049
10050 let key_string = map_array
10051 .keys()
10052 .as_any()
10053 .downcast_ref::<StringArray>()
10054 .unwrap()
10055 .into_iter()
10056 .flatten()
10057 .collect::<Vec<_>>();
10058 assert_eq!(&key_string, &vec!["0", "1"]);
10059
10060 let values_string_array = cast(map_array.values(), &DataType::Utf8).unwrap();
10061 let values_string = values_string_array
10062 .as_any()
10063 .downcast_ref::<StringArray>()
10064 .unwrap()
10065 .into_iter()
10066 .flatten()
10067 .collect::<Vec<_>>();
10068 assert_eq!(&values_string, &vec!["test_val_1", "test_val_2"]);
10069
10070 assert_eq!(
10071 map_array.nulls(),
10072 Some(&NullBuffer::from(vec![true, true, false]))
10073 );
10074 }
10075
10076 #[test]
10077 fn test_cast_map_contained_values() {
10078 let string_builder = StringBuilder::new();
10079 let value_builder = Int8Builder::new();
10080 let mut builder = MapBuilder::new(
10081 Some(MapFieldNames {
10082 entry: "entries".to_string(),
10083 key: "key".to_string(),
10084 value: "value".to_string(),
10085 }),
10086 string_builder,
10087 value_builder,
10088 );
10089
10090 builder.keys().append_value("0");
10091 builder.values().append_value(44);
10092 builder.append(true).unwrap();
10093 builder.keys().append_value("1");
10094 builder.values().append_value(22);
10095 builder.append(true).unwrap();
10096
10097 let array = builder.finish();
10098
10099 let new_type = DataType::Map(
10100 Arc::new(Field::new(
10101 "entries",
10102 DataType::Struct(
10103 vec![
10104 Field::new("key", DataType::Utf8, false),
10105 Field::new("value", DataType::Utf8, false),
10106 ]
10107 .into(),
10108 ),
10109 false,
10110 )),
10111 false,
10112 );
10113
10114 let new_array = cast(&array, &new_type.clone()).unwrap();
10115 assert_eq!(new_type, new_array.data_type().clone());
10116 let map_array = new_array.as_map();
10117
10118 assert_ne!(new_type, array.data_type().clone());
10119 assert_eq!(new_type, map_array.data_type().clone());
10120
10121 let key_string = map_array
10122 .keys()
10123 .as_any()
10124 .downcast_ref::<StringArray>()
10125 .unwrap()
10126 .into_iter()
10127 .flatten()
10128 .collect::<Vec<_>>();
10129 assert_eq!(&key_string, &vec!["0", "1"]);
10130
10131 let values_string_array = cast(map_array.values(), &DataType::Utf8).unwrap();
10132 let values_string = values_string_array
10133 .as_any()
10134 .downcast_ref::<StringArray>()
10135 .unwrap()
10136 .into_iter()
10137 .flatten()
10138 .collect::<Vec<_>>();
10139 assert_eq!(&values_string, &vec!["44", "22"]);
10140 }
10141
10142 #[test]
10143 fn test_utf8_cast_offsets() {
10144 let str_array = StringArray::from(vec!["a", "b", "c"]);
10146 let str_array = str_array.slice(1, 2);
10147
10148 let out = cast(&str_array, &DataType::LargeUtf8).unwrap();
10149
10150 let large_str_array = out.as_any().downcast_ref::<LargeStringArray>().unwrap();
10151 let strs = large_str_array.into_iter().flatten().collect::<Vec<_>>();
10152 assert_eq!(strs, &["b", "c"])
10153 }
10154
10155 #[test]
10156 fn test_list_cast_offsets() {
10157 let array1 = make_list_array().slice(1, 2);
10159 let array2 = make_list_array();
10160
10161 let dt = DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
10162 let out1 = cast(&array1, &dt).unwrap();
10163 let out2 = cast(&array2, &dt).unwrap();
10164
10165 assert_eq!(&out1, &out2.slice(1, 2))
10166 }
10167
10168 #[test]
10169 fn test_list_to_string() {
10170 fn assert_cast(array: &ArrayRef, expected: &[&str]) {
10171 assert!(can_cast_types(array.data_type(), &DataType::Utf8));
10172 let out = cast(array, &DataType::Utf8).unwrap();
10173 let out = out
10174 .as_string::<i32>()
10175 .into_iter()
10176 .flatten()
10177 .collect::<Vec<_>>();
10178 assert_eq!(out, expected);
10179
10180 assert!(can_cast_types(array.data_type(), &DataType::LargeUtf8));
10181 let out = cast(array, &DataType::LargeUtf8).unwrap();
10182 let out = out
10183 .as_string::<i64>()
10184 .into_iter()
10185 .flatten()
10186 .collect::<Vec<_>>();
10187 assert_eq!(out, expected);
10188
10189 assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
10190 let out = cast(array, &DataType::Utf8View).unwrap();
10191 let out = out
10192 .as_string_view()
10193 .into_iter()
10194 .flatten()
10195 .collect::<Vec<_>>();
10196 assert_eq!(out, expected);
10197 }
10198
10199 let array = Arc::new(ListArray::new(
10200 Field::new_list_field(DataType::Utf8, true).into(),
10201 OffsetBuffer::from_lengths(vec![3, 3, 2]),
10202 Arc::new(StringArray::from(vec![
10203 "a", "b", "c", "d", "e", "f", "g", "h",
10204 ])),
10205 None,
10206 )) as ArrayRef;
10207
10208 assert_cast(&array, &["[a, b, c]", "[d, e, f]", "[g, h]"]);
10209
10210 let array = make_list_array();
10211 assert_cast(&array, &["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
10212
10213 let array = make_large_list_array();
10214 assert_cast(&array, &["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
10215
10216 let array = make_list_view_array();
10217 assert_cast(&array, &["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
10218
10219 let array = make_large_list_view_array();
10220 assert_cast(&array, &["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
10221 }
10222
10223 #[test]
10224 fn test_cast_f64_to_decimal128() {
10225 let decimal_type = DataType::Decimal128(18, 2);
10228 let array = Float64Array::from(vec![
10229 Some(0.0699999999),
10230 Some(0.0659999999),
10231 Some(0.0650000000),
10232 Some(0.0649999999),
10233 ]);
10234 let array = Arc::new(array) as ArrayRef;
10235 generate_cast_test_case!(
10236 &array,
10237 Decimal128Array,
10238 &decimal_type,
10239 vec![
10240 Some(7_i128), Some(7_i128), Some(7_i128), Some(6_i128), ]
10245 );
10246
10247 let decimal_type = DataType::Decimal128(18, 3);
10248 let array = Float64Array::from(vec![
10249 Some(0.0699999999),
10250 Some(0.0659999999),
10251 Some(0.0650000000),
10252 Some(0.0649999999),
10253 ]);
10254 let array = Arc::new(array) as ArrayRef;
10255 generate_cast_test_case!(
10256 &array,
10257 Decimal128Array,
10258 &decimal_type,
10259 vec![
10260 Some(70_i128), Some(66_i128), Some(65_i128), Some(65_i128), ]
10265 );
10266 }
10267
10268 #[test]
10269 fn test_cast_numeric_to_decimal128_overflow() {
10270 let array = Int64Array::from(vec![i64::MAX]);
10271 let array = Arc::new(array) as ArrayRef;
10272 let casted_array = cast_with_options(
10273 &array,
10274 &DataType::Decimal128(38, 30),
10275 &CastOptions {
10276 safe: true,
10277 format_options: FormatOptions::default(),
10278 },
10279 );
10280 assert!(casted_array.is_ok());
10281 assert!(casted_array.unwrap().is_null(0));
10282
10283 let casted_array = cast_with_options(
10284 &array,
10285 &DataType::Decimal128(38, 30),
10286 &CastOptions {
10287 safe: false,
10288 format_options: FormatOptions::default(),
10289 },
10290 );
10291 assert!(casted_array.is_err());
10292 }
10293
10294 #[test]
10295 fn test_cast_numeric_to_decimal256_overflow() {
10296 let array = Int64Array::from(vec![i64::MAX]);
10297 let array = Arc::new(array) as ArrayRef;
10298 let casted_array = cast_with_options(
10299 &array,
10300 &DataType::Decimal256(76, 76),
10301 &CastOptions {
10302 safe: true,
10303 format_options: FormatOptions::default(),
10304 },
10305 );
10306 assert!(casted_array.is_ok());
10307 assert!(casted_array.unwrap().is_null(0));
10308
10309 let casted_array = cast_with_options(
10310 &array,
10311 &DataType::Decimal256(76, 76),
10312 &CastOptions {
10313 safe: false,
10314 format_options: FormatOptions::default(),
10315 },
10316 );
10317 assert!(casted_array.is_err());
10318 }
10319
10320 #[test]
10321 fn test_cast_floating_point_to_decimal128_precision_overflow() {
10322 let array = Float64Array::from(vec![1.1]);
10323 let array = Arc::new(array) as ArrayRef;
10324 let casted_array = cast_with_options(
10325 &array,
10326 &DataType::Decimal128(2, 2),
10327 &CastOptions {
10328 safe: true,
10329 format_options: FormatOptions::default(),
10330 },
10331 );
10332 assert!(casted_array.is_ok());
10333 assert!(casted_array.unwrap().is_null(0));
10334
10335 let casted_array = cast_with_options(
10336 &array,
10337 &DataType::Decimal128(2, 2),
10338 &CastOptions {
10339 safe: false,
10340 format_options: FormatOptions::default(),
10341 },
10342 );
10343 let err = casted_array.unwrap_err().to_string();
10344 let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal128 of precision 2. Max is 0.99";
10345 assert!(
10346 err.contains(expected_error),
10347 "did not find expected error '{expected_error}' in actual error '{err}'"
10348 );
10349 }
10350
10351 #[test]
10352 fn test_cast_float16_to_decimal128_precision_overflow() {
10353 let array = Float16Array::from(vec![f16::from_f32(1.1)]);
10354 let array = Arc::new(array) as ArrayRef;
10355 let casted_array = cast_with_options(
10356 &array,
10357 &DataType::Decimal128(2, 2),
10358 &CastOptions {
10359 safe: true,
10360 format_options: FormatOptions::default(),
10361 },
10362 );
10363 assert!(casted_array.is_ok());
10364 assert!(casted_array.unwrap().is_null(0));
10365
10366 let casted_array = cast_with_options(
10367 &array,
10368 &DataType::Decimal128(2, 2),
10369 &CastOptions {
10370 safe: false,
10371 format_options: FormatOptions::default(),
10372 },
10373 );
10374 let err = casted_array.unwrap_err().to_string();
10375 let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal128 of precision 2. Max is 0.99";
10376 assert_eq!(err, expected_error);
10377 }
10378
10379 #[test]
10380 fn test_cast_float16_to_decimal256_precision_overflow() {
10381 let array = Float16Array::from(vec![f16::from_f32(1.1)]);
10382 let array = Arc::new(array) as ArrayRef;
10383 let casted_array = cast_with_options(
10384 &array,
10385 &DataType::Decimal256(2, 2),
10386 &CastOptions {
10387 safe: true,
10388 format_options: FormatOptions::default(),
10389 },
10390 );
10391 assert!(casted_array.is_ok());
10392 assert!(casted_array.unwrap().is_null(0));
10393
10394 let casted_array = cast_with_options(
10395 &array,
10396 &DataType::Decimal256(2, 2),
10397 &CastOptions {
10398 safe: false,
10399 format_options: FormatOptions::default(),
10400 },
10401 );
10402 let err = casted_array.unwrap_err().to_string();
10403 let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal256 of precision 2. Max is 0.99";
10404 assert_eq!(err, expected_error);
10405 }
10406
10407 #[test]
10408 fn test_cast_float16_to_decimal128_non_finite() {
10409 let array = Float16Array::from(vec![f16::NAN, f16::INFINITY, f16::NEG_INFINITY]);
10410 let array = Arc::new(array) as ArrayRef;
10411 let casted_array = cast_with_options(
10412 &array,
10413 &DataType::Decimal128(38, 2),
10414 &CastOptions {
10415 safe: true,
10416 format_options: FormatOptions::default(),
10417 },
10418 )
10419 .unwrap();
10420
10421 assert!(casted_array.is_null(0));
10422 assert!(casted_array.is_null(1));
10423 assert!(casted_array.is_null(2));
10424
10425 let casted_array = cast_with_options(
10426 &array,
10427 &DataType::Decimal128(38, 2),
10428 &CastOptions {
10429 safe: false,
10430 format_options: FormatOptions::default(),
10431 },
10432 );
10433 let err = casted_array.unwrap_err().to_string();
10434 let expected_error = "Cannot cast to Decimal128(38, 2)";
10435 assert!(
10436 err.contains(expected_error),
10437 "did not find expected error '{expected_error}' in actual error '{err}'"
10438 );
10439 }
10440
10441 #[test]
10442 fn test_cast_floating_point_to_decimal256_precision_overflow() {
10443 let array = Float64Array::from(vec![1.1]);
10444 let array = Arc::new(array) as ArrayRef;
10445 let casted_array = cast_with_options(
10446 &array,
10447 &DataType::Decimal256(2, 2),
10448 &CastOptions {
10449 safe: true,
10450 format_options: FormatOptions::default(),
10451 },
10452 );
10453 assert!(casted_array.is_ok());
10454 assert!(casted_array.unwrap().is_null(0));
10455
10456 let casted_array = cast_with_options(
10457 &array,
10458 &DataType::Decimal256(2, 2),
10459 &CastOptions {
10460 safe: false,
10461 format_options: FormatOptions::default(),
10462 },
10463 );
10464 let err = casted_array.unwrap_err().to_string();
10465 let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal256 of precision 2. Max is 0.99";
10466 assert_eq!(err, expected_error);
10467 }
10468
10469 #[test]
10470 fn test_cast_floating_point_to_decimal128_overflow() {
10471 let array = Float64Array::from(vec![f64::MAX]);
10472 let array = Arc::new(array) as ArrayRef;
10473 let casted_array = cast_with_options(
10474 &array,
10475 &DataType::Decimal128(38, 30),
10476 &CastOptions {
10477 safe: true,
10478 format_options: FormatOptions::default(),
10479 },
10480 );
10481 assert!(casted_array.is_ok());
10482 assert!(casted_array.unwrap().is_null(0));
10483
10484 let casted_array = cast_with_options(
10485 &array,
10486 &DataType::Decimal128(38, 30),
10487 &CastOptions {
10488 safe: false,
10489 format_options: FormatOptions::default(),
10490 },
10491 );
10492 let err = casted_array.unwrap_err().to_string();
10493 let expected_error = "Cast error: Cannot cast to Decimal128(38, 30)";
10494 assert!(
10495 err.contains(expected_error),
10496 "did not find expected error '{expected_error}' in actual error '{err}'"
10497 );
10498 }
10499
10500 #[test]
10501 fn test_cast_floating_point_to_decimal256_overflow() {
10502 let array = Float64Array::from(vec![f64::MAX]);
10503 let array = Arc::new(array) as ArrayRef;
10504 let casted_array = cast_with_options(
10505 &array,
10506 &DataType::Decimal256(76, 50),
10507 &CastOptions {
10508 safe: true,
10509 format_options: FormatOptions::default(),
10510 },
10511 );
10512 assert!(casted_array.is_ok());
10513 assert!(casted_array.unwrap().is_null(0));
10514
10515 let casted_array = cast_with_options(
10516 &array,
10517 &DataType::Decimal256(76, 50),
10518 &CastOptions {
10519 safe: false,
10520 format_options: FormatOptions::default(),
10521 },
10522 );
10523 let err = casted_array.unwrap_err().to_string();
10524 let expected_error = "Cast error: Cannot cast to Decimal256(76, 50)";
10525 assert!(
10526 err.contains(expected_error),
10527 "did not find expected error '{expected_error}' in actual error '{err}'"
10528 );
10529 }
10530 #[test]
10531 fn test_cast_decimal256_to_f64_no_overflow() {
10532 let array = vec![Some(i256::MAX)];
10534 let array = create_decimal256_array(array, 76, 2).unwrap();
10535 let array = Arc::new(array) as ArrayRef;
10536
10537 let result = cast(&array, &DataType::Float64).unwrap();
10538 let result = result.as_primitive::<Float64Type>();
10539 assert!(result.value(0).is_finite());
10540 assert!(result.value(0) > 0.0); let array = vec![Some(i256::MIN)];
10544 let array = create_decimal256_array(array, 76, 2).unwrap();
10545 let array = Arc::new(array) as ArrayRef;
10546
10547 let result = cast(&array, &DataType::Float64).unwrap();
10548 let result = result.as_primitive::<Float64Type>();
10549 assert!(result.value(0).is_finite());
10550 assert!(result.value(0) < 0.0); }
10552
10553 #[test]
10554 fn test_cast_decimal128_to_decimal128_negative_scale() {
10555 let input_type = DataType::Decimal128(20, 0);
10556 let output_type = DataType::Decimal128(20, -1);
10557 assert!(can_cast_types(&input_type, &output_type));
10558 let array = vec![Some(1123450), Some(2123455), Some(3123456), None];
10559 let input_decimal_array = create_decimal128_array(array, 20, 0).unwrap();
10560 let array = Arc::new(input_decimal_array) as ArrayRef;
10561 generate_cast_test_case!(
10562 &array,
10563 Decimal128Array,
10564 &output_type,
10565 vec![
10566 Some(112345_i128),
10567 Some(212346_i128),
10568 Some(312346_i128),
10569 None
10570 ]
10571 );
10572
10573 let casted_array = cast(&array, &output_type).unwrap();
10574 let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10575
10576 assert_eq!("1123450", decimal_arr.value_as_string(0));
10577 assert_eq!("2123460", decimal_arr.value_as_string(1));
10578 assert_eq!("3123460", decimal_arr.value_as_string(2));
10579 }
10580
10581 #[test]
10582 fn decimal128_min_max_to_f64() {
10583 let min128 = i128::MIN;
10585 let max128 = i128::MAX;
10586 assert_eq!(min128 as f64, min128 as f64);
10587 assert_eq!(max128 as f64, max128 as f64);
10588 }
10589
10590 #[test]
10591 fn test_cast_numeric_to_decimal128_negative() {
10592 let decimal_type = DataType::Decimal128(38, -1);
10593 let array = Arc::new(Int32Array::from(vec![
10594 Some(1123456),
10595 Some(2123456),
10596 Some(3123456),
10597 ])) as ArrayRef;
10598
10599 let casted_array = cast(&array, &decimal_type).unwrap();
10600 let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10601
10602 assert_eq!("1123450", decimal_arr.value_as_string(0));
10603 assert_eq!("2123450", decimal_arr.value_as_string(1));
10604 assert_eq!("3123450", decimal_arr.value_as_string(2));
10605
10606 let array = Arc::new(Float32Array::from(vec![
10607 Some(1123.456),
10608 Some(2123.456),
10609 Some(3123.456),
10610 ])) as ArrayRef;
10611
10612 let casted_array = cast(&array, &decimal_type).unwrap();
10613 let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10614
10615 assert_eq!("1120", decimal_arr.value_as_string(0));
10616 assert_eq!("2120", decimal_arr.value_as_string(1));
10617 assert_eq!("3120", decimal_arr.value_as_string(2));
10618 }
10619
10620 #[test]
10621 fn test_cast_decimal128_to_decimal128_negative() {
10622 let input_type = DataType::Decimal128(10, -1);
10623 let output_type = DataType::Decimal128(10, -2);
10624 assert!(can_cast_types(&input_type, &output_type));
10625 let array = vec![Some(123)];
10626 let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
10627 let array = Arc::new(input_decimal_array) as ArrayRef;
10628 generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(12_i128),]);
10629
10630 let casted_array = cast(&array, &output_type).unwrap();
10631 let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10632
10633 assert_eq!("1200", decimal_arr.value_as_string(0));
10634
10635 let array = vec![Some(125)];
10636 let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
10637 let array = Arc::new(input_decimal_array) as ArrayRef;
10638 generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(13_i128),]);
10639
10640 let casted_array = cast(&array, &output_type).unwrap();
10641 let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10642
10643 assert_eq!("1300", decimal_arr.value_as_string(0));
10644 }
10645
10646 #[test]
10647 fn test_cast_decimal128_to_decimal256_negative() {
10648 let input_type = DataType::Decimal128(10, 3);
10649 let output_type = DataType::Decimal256(10, 5);
10650 assert!(can_cast_types(&input_type, &output_type));
10651 let array = vec![Some(123456), Some(-123456)];
10652 let input_decimal_array = create_decimal128_array(array, 10, 3).unwrap();
10653 let array = Arc::new(input_decimal_array) as ArrayRef;
10654
10655 let hundred = i256::from_i128(100);
10656 generate_cast_test_case!(
10657 &array,
10658 Decimal256Array,
10659 &output_type,
10660 vec![
10661 Some(i256::from_i128(123456).mul_wrapping(hundred)),
10662 Some(i256::from_i128(-123456).mul_wrapping(hundred))
10663 ]
10664 );
10665 }
10666
10667 #[test]
10668 fn test_parse_string_to_decimal() {
10669 assert_eq!(
10670 Decimal128Type::format_decimal(
10671 parse_string_to_decimal_native::<Decimal128Type>("123.45", 2).unwrap(),
10672 38,
10673 2,
10674 ),
10675 "123.45"
10676 );
10677 assert_eq!(
10678 Decimal128Type::format_decimal(
10679 parse_string_to_decimal_native::<Decimal128Type>("12345", 2).unwrap(),
10680 38,
10681 2,
10682 ),
10683 "12345.00"
10684 );
10685 assert_eq!(
10686 Decimal128Type::format_decimal(
10687 parse_string_to_decimal_native::<Decimal128Type>("0.12345", 2).unwrap(),
10688 38,
10689 2,
10690 ),
10691 "0.12"
10692 );
10693 assert_eq!(
10694 Decimal128Type::format_decimal(
10695 parse_string_to_decimal_native::<Decimal128Type>(".12345", 2).unwrap(),
10696 38,
10697 2,
10698 ),
10699 "0.12"
10700 );
10701 assert_eq!(
10702 Decimal128Type::format_decimal(
10703 parse_string_to_decimal_native::<Decimal128Type>(".1265", 2).unwrap(),
10704 38,
10705 2,
10706 ),
10707 "0.13"
10708 );
10709 assert_eq!(
10710 Decimal128Type::format_decimal(
10711 parse_string_to_decimal_native::<Decimal128Type>(".1265", 2).unwrap(),
10712 38,
10713 2,
10714 ),
10715 "0.13"
10716 );
10717
10718 assert_eq!(
10719 Decimal256Type::format_decimal(
10720 parse_string_to_decimal_native::<Decimal256Type>("123.45", 3).unwrap(),
10721 38,
10722 3,
10723 ),
10724 "123.450"
10725 );
10726 assert_eq!(
10727 Decimal256Type::format_decimal(
10728 parse_string_to_decimal_native::<Decimal256Type>("12345", 3).unwrap(),
10729 38,
10730 3,
10731 ),
10732 "12345.000"
10733 );
10734 assert_eq!(
10735 Decimal256Type::format_decimal(
10736 parse_string_to_decimal_native::<Decimal256Type>("0.12345", 3).unwrap(),
10737 38,
10738 3,
10739 ),
10740 "0.123"
10741 );
10742 assert_eq!(
10743 Decimal256Type::format_decimal(
10744 parse_string_to_decimal_native::<Decimal256Type>(".12345", 3).unwrap(),
10745 38,
10746 3,
10747 ),
10748 "0.123"
10749 );
10750 assert_eq!(
10751 Decimal256Type::format_decimal(
10752 parse_string_to_decimal_native::<Decimal256Type>(".1265", 3).unwrap(),
10753 38,
10754 3,
10755 ),
10756 "0.127"
10757 );
10758 }
10759
10760 fn test_cast_string_to_decimal(array: ArrayRef) {
10761 let output_type = DataType::Decimal128(38, 2);
10763 assert!(can_cast_types(array.data_type(), &output_type));
10764
10765 let casted_array = cast(&array, &output_type).unwrap();
10766 let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10767
10768 assert_eq!("123.45", decimal_arr.value_as_string(0));
10769 assert_eq!("1.23", decimal_arr.value_as_string(1));
10770 assert_eq!("0.12", decimal_arr.value_as_string(2));
10771 assert_eq!("0.13", decimal_arr.value_as_string(3));
10772 assert_eq!("1.26", decimal_arr.value_as_string(4));
10773 assert_eq!("12345.00", decimal_arr.value_as_string(5));
10774 assert_eq!("12345.00", decimal_arr.value_as_string(6));
10775 assert_eq!("0.12", decimal_arr.value_as_string(7));
10776 assert_eq!("12.23", decimal_arr.value_as_string(8));
10777 assert!(decimal_arr.is_null(9));
10778 assert!(decimal_arr.is_null(10));
10779 assert!(decimal_arr.is_null(11));
10780 assert!(decimal_arr.is_null(12));
10781 assert_eq!("-1.23", decimal_arr.value_as_string(13));
10782 assert_eq!("-1.24", decimal_arr.value_as_string(14));
10783 assert_eq!("0.00", decimal_arr.value_as_string(15));
10784 assert_eq!("-123.00", decimal_arr.value_as_string(16));
10785 assert_eq!("-123.23", decimal_arr.value_as_string(17));
10786 assert_eq!("-0.12", decimal_arr.value_as_string(18));
10787 assert_eq!("1.23", decimal_arr.value_as_string(19));
10788 assert_eq!("1.24", decimal_arr.value_as_string(20));
10789 assert_eq!("0.00", decimal_arr.value_as_string(21));
10790 assert_eq!("123.00", decimal_arr.value_as_string(22));
10791 assert_eq!("123.23", decimal_arr.value_as_string(23));
10792 assert_eq!("0.12", decimal_arr.value_as_string(24));
10793 assert!(decimal_arr.is_null(25));
10794 assert!(decimal_arr.is_null(26));
10795 assert!(decimal_arr.is_null(27));
10796 assert_eq!("0.00", decimal_arr.value_as_string(28));
10797 assert_eq!("0.00", decimal_arr.value_as_string(29));
10798 assert_eq!("12345.00", decimal_arr.value_as_string(30));
10799 assert_eq!(decimal_arr.len(), 31);
10800
10801 let output_type = DataType::Decimal256(76, 3);
10803 assert!(can_cast_types(array.data_type(), &output_type));
10804
10805 let casted_array = cast(&array, &output_type).unwrap();
10806 let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
10807
10808 assert_eq!("123.450", decimal_arr.value_as_string(0));
10809 assert_eq!("1.235", decimal_arr.value_as_string(1));
10810 assert_eq!("0.123", decimal_arr.value_as_string(2));
10811 assert_eq!("0.127", decimal_arr.value_as_string(3));
10812 assert_eq!("1.263", decimal_arr.value_as_string(4));
10813 assert_eq!("12345.000", decimal_arr.value_as_string(5));
10814 assert_eq!("12345.000", decimal_arr.value_as_string(6));
10815 assert_eq!("0.123", decimal_arr.value_as_string(7));
10816 assert_eq!("12.234", decimal_arr.value_as_string(8));
10817 assert!(decimal_arr.is_null(9));
10818 assert!(decimal_arr.is_null(10));
10819 assert!(decimal_arr.is_null(11));
10820 assert!(decimal_arr.is_null(12));
10821 assert_eq!("-1.235", decimal_arr.value_as_string(13));
10822 assert_eq!("-1.236", decimal_arr.value_as_string(14));
10823 assert_eq!("0.000", decimal_arr.value_as_string(15));
10824 assert_eq!("-123.000", decimal_arr.value_as_string(16));
10825 assert_eq!("-123.234", decimal_arr.value_as_string(17));
10826 assert_eq!("-0.123", decimal_arr.value_as_string(18));
10827 assert_eq!("1.235", decimal_arr.value_as_string(19));
10828 assert_eq!("1.236", decimal_arr.value_as_string(20));
10829 assert_eq!("0.000", decimal_arr.value_as_string(21));
10830 assert_eq!("123.000", decimal_arr.value_as_string(22));
10831 assert_eq!("123.234", decimal_arr.value_as_string(23));
10832 assert_eq!("0.123", decimal_arr.value_as_string(24));
10833 assert!(decimal_arr.is_null(25));
10834 assert!(decimal_arr.is_null(26));
10835 assert!(decimal_arr.is_null(27));
10836 assert_eq!("0.000", decimal_arr.value_as_string(28));
10837 assert_eq!("0.000", decimal_arr.value_as_string(29));
10838 assert_eq!("12345.000", decimal_arr.value_as_string(30));
10839 assert_eq!(decimal_arr.len(), 31);
10840 }
10841
10842 #[test]
10843 fn test_cast_utf8_to_decimal() {
10844 let str_array = StringArray::from(vec![
10845 Some("123.45"),
10846 Some("1.2345"),
10847 Some("0.12345"),
10848 Some("0.1267"),
10849 Some("1.263"),
10850 Some("12345.0"),
10851 Some("12345"),
10852 Some("000.123"),
10853 Some("12.234000"),
10854 None,
10855 Some(""),
10856 Some(" "),
10857 None,
10858 Some("-1.23499999"),
10859 Some("-1.23599999"),
10860 Some("-0.00001"),
10861 Some("-123"),
10862 Some("-123.234000"),
10863 Some("-000.123"),
10864 Some("+1.23499999"),
10865 Some("+1.23599999"),
10866 Some("+0.00001"),
10867 Some("+123"),
10868 Some("+123.234000"),
10869 Some("+000.123"),
10870 Some("1.-23499999"),
10871 Some("-1.-23499999"),
10872 Some("--1.23499999"),
10873 Some("0"),
10874 Some("000.000"),
10875 Some("0000000000000000012345.000"),
10876 ]);
10877 let array = Arc::new(str_array) as ArrayRef;
10878
10879 test_cast_string_to_decimal(array);
10880
10881 let test_cases = [
10882 (None, None),
10883 (Some(""), None),
10884 (Some(" "), None),
10885 (Some("0"), Some("0")),
10886 (Some("000.000"), Some("0")),
10887 (Some("12345"), Some("12345")),
10888 (Some("000000000000000000000000000012345"), Some("12345")),
10889 (Some("-123"), Some("-123")),
10890 (Some("+123"), Some("123")),
10891 ];
10892 let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
10893 let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
10894
10895 let array = Arc::new(StringArray::from(inputs)) as ArrayRef;
10896 test_cast_string_to_decimal_scale_zero(array, &expected);
10897 }
10898
10899 #[test]
10900 fn test_cast_large_utf8_to_decimal() {
10901 let str_array = LargeStringArray::from(vec![
10902 Some("123.45"),
10903 Some("1.2345"),
10904 Some("0.12345"),
10905 Some("0.1267"),
10906 Some("1.263"),
10907 Some("12345.0"),
10908 Some("12345"),
10909 Some("000.123"),
10910 Some("12.234000"),
10911 None,
10912 Some(""),
10913 Some(" "),
10914 None,
10915 Some("-1.23499999"),
10916 Some("-1.23599999"),
10917 Some("-0.00001"),
10918 Some("-123"),
10919 Some("-123.234000"),
10920 Some("-000.123"),
10921 Some("+1.23499999"),
10922 Some("+1.23599999"),
10923 Some("+0.00001"),
10924 Some("+123"),
10925 Some("+123.234000"),
10926 Some("+000.123"),
10927 Some("1.-23499999"),
10928 Some("-1.-23499999"),
10929 Some("--1.23499999"),
10930 Some("0"),
10931 Some("000.000"),
10932 Some("0000000000000000012345.000"),
10933 ]);
10934 let array = Arc::new(str_array) as ArrayRef;
10935
10936 test_cast_string_to_decimal(array);
10937
10938 let test_cases = [
10939 (None, None),
10940 (Some(""), None),
10941 (Some(" "), None),
10942 (Some("0"), Some("0")),
10943 (Some("000.000"), Some("0")),
10944 (Some("12345"), Some("12345")),
10945 (Some("000000000000000000000000000012345"), Some("12345")),
10946 (Some("-123"), Some("-123")),
10947 (Some("+123"), Some("123")),
10948 ];
10949 let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
10950 let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
10951
10952 let array = Arc::new(LargeStringArray::from(inputs)) as ArrayRef;
10953 test_cast_string_to_decimal_scale_zero(array, &expected);
10954 }
10955
10956 fn test_cast_string_to_decimal_scale_zero(
10957 array: ArrayRef,
10958 expected_as_string: &[Option<&str>],
10959 ) {
10960 let output_type = DataType::Decimal128(38, 0);
10962 assert!(can_cast_types(array.data_type(), &output_type));
10963 let casted_array = cast(&array, &output_type).unwrap();
10964 let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10965 assert_decimal_array_contents(decimal_arr, expected_as_string);
10966
10967 let output_type = DataType::Decimal256(76, 0);
10969 assert!(can_cast_types(array.data_type(), &output_type));
10970 let casted_array = cast(&array, &output_type).unwrap();
10971 let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
10972 assert_decimal_array_contents(decimal_arr, expected_as_string);
10973 }
10974
10975 fn assert_decimal_array_contents<T>(
10976 array: &PrimitiveArray<T>,
10977 expected_as_string: &[Option<&str>],
10978 ) where
10979 T: DecimalType + ArrowPrimitiveType,
10980 {
10981 assert_eq!(array.len(), expected_as_string.len());
10982 for (i, expected) in expected_as_string.iter().enumerate() {
10983 let actual = if array.is_null(i) {
10984 None
10985 } else {
10986 Some(array.value_as_string(i))
10987 };
10988 let actual = actual.as_ref().map(|s| s.as_ref());
10989 assert_eq!(*expected, actual, "Expected at position {i}");
10990 }
10991 }
10992
10993 #[test]
10994 fn test_cast_invalid_utf8_to_decimal() {
10995 let str_array = StringArray::from(vec!["4.4.5", ". 0.123"]);
10996 let array = Arc::new(str_array) as ArrayRef;
10997
10998 let output_type = DataType::Decimal128(38, 2);
11000 let casted_array = cast(&array, &output_type).unwrap();
11001 assert!(casted_array.is_null(0));
11002 assert!(casted_array.is_null(1));
11003
11004 let output_type = DataType::Decimal256(76, 2);
11005 let casted_array = cast(&array, &output_type).unwrap();
11006 assert!(casted_array.is_null(0));
11007 assert!(casted_array.is_null(1));
11008
11009 let output_type = DataType::Decimal128(38, 2);
11011 let str_array = StringArray::from(vec!["4.4.5"]);
11012 let array = Arc::new(str_array) as ArrayRef;
11013 let option = CastOptions {
11014 safe: false,
11015 format_options: FormatOptions::default(),
11016 };
11017 let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
11018 assert!(
11019 casted_err
11020 .to_string()
11021 .contains("Cannot cast string '4.4.5' to value of Decimal128(38, 10) type")
11022 );
11023
11024 let str_array = StringArray::from(vec![". 0.123"]);
11025 let array = Arc::new(str_array) as ArrayRef;
11026 let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
11027 assert!(
11028 casted_err
11029 .to_string()
11030 .contains("Cannot cast string '. 0.123' to value of Decimal128(38, 10) type")
11031 );
11032
11033 let str_array = StringArray::from(vec![""]);
11034 let array = Arc::new(str_array) as ArrayRef;
11035 let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
11036 assert!(
11037 casted_err
11038 .to_string()
11039 .contains("Cannot cast string '' to value of Decimal128(38, 10) type")
11040 );
11041 }
11042
11043 fn test_cast_string_to_decimal128_overflow(overflow_array: ArrayRef) {
11044 let output_type = DataType::Decimal128(38, 2);
11045 let casted_array = cast(&overflow_array, &output_type).unwrap();
11046 let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
11047
11048 assert!(decimal_arr.is_null(0));
11049 assert!(decimal_arr.is_null(1));
11050 assert!(decimal_arr.is_null(2));
11051 assert_eq!(
11052 "999999999999999999999999999999999999.99",
11053 decimal_arr.value_as_string(3)
11054 );
11055 assert_eq!(
11056 "100000000000000000000000000000000000.00",
11057 decimal_arr.value_as_string(4)
11058 );
11059 }
11060
11061 #[test]
11062 fn test_cast_string_to_decimal128_precision_overflow() {
11063 let array = StringArray::from(vec!["1000".to_string()]);
11064 let array = Arc::new(array) as ArrayRef;
11065 let casted_array = cast_with_options(
11066 &array,
11067 &DataType::Decimal128(10, 8),
11068 &CastOptions {
11069 safe: true,
11070 format_options: FormatOptions::default(),
11071 },
11072 );
11073 assert!(casted_array.is_ok());
11074 assert!(casted_array.unwrap().is_null(0));
11075
11076 let err = cast_with_options(
11077 &array,
11078 &DataType::Decimal128(10, 8),
11079 &CastOptions {
11080 safe: false,
11081 format_options: FormatOptions::default(),
11082 },
11083 );
11084 assert_eq!(
11085 "Invalid argument error: 1000.00000000 is too large to store in a Decimal128 of precision 10. Max is 99.99999999",
11086 err.unwrap_err().to_string()
11087 );
11088 }
11089
11090 #[test]
11091 fn test_cast_utf8_to_decimal128_overflow() {
11092 let overflow_str_array = StringArray::from(vec![
11093 i128::MAX.to_string(),
11094 i128::MIN.to_string(),
11095 "99999999999999999999999999999999999999".to_string(),
11096 "999999999999999999999999999999999999.99".to_string(),
11097 "99999999999999999999999999999999999.999".to_string(),
11098 ]);
11099 let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
11100
11101 test_cast_string_to_decimal128_overflow(overflow_array);
11102 }
11103
11104 #[test]
11105 fn test_cast_large_utf8_to_decimal128_overflow() {
11106 let overflow_str_array = LargeStringArray::from(vec![
11107 i128::MAX.to_string(),
11108 i128::MIN.to_string(),
11109 "99999999999999999999999999999999999999".to_string(),
11110 "999999999999999999999999999999999999.99".to_string(),
11111 "99999999999999999999999999999999999.999".to_string(),
11112 ]);
11113 let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
11114
11115 test_cast_string_to_decimal128_overflow(overflow_array);
11116 }
11117
11118 fn test_cast_string_to_decimal256_overflow(overflow_array: ArrayRef) {
11119 let output_type = DataType::Decimal256(76, 2);
11120 let casted_array = cast(&overflow_array, &output_type).unwrap();
11121 let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
11122
11123 assert_eq!(
11124 "170141183460469231731687303715884105727.00",
11125 decimal_arr.value_as_string(0)
11126 );
11127 assert_eq!(
11128 "-170141183460469231731687303715884105728.00",
11129 decimal_arr.value_as_string(1)
11130 );
11131 assert_eq!(
11132 "99999999999999999999999999999999999999.00",
11133 decimal_arr.value_as_string(2)
11134 );
11135 assert_eq!(
11136 "999999999999999999999999999999999999.99",
11137 decimal_arr.value_as_string(3)
11138 );
11139 assert_eq!(
11140 "100000000000000000000000000000000000.00",
11141 decimal_arr.value_as_string(4)
11142 );
11143 assert!(decimal_arr.is_null(5));
11144 assert!(decimal_arr.is_null(6));
11145 }
11146
11147 #[test]
11148 fn test_cast_string_to_decimal256_precision_overflow() {
11149 let array = StringArray::from(vec!["1000".to_string()]);
11150 let array = Arc::new(array) as ArrayRef;
11151 let casted_array = cast_with_options(
11152 &array,
11153 &DataType::Decimal256(10, 8),
11154 &CastOptions {
11155 safe: true,
11156 format_options: FormatOptions::default(),
11157 },
11158 );
11159 assert!(casted_array.is_ok());
11160 assert!(casted_array.unwrap().is_null(0));
11161
11162 let err = cast_with_options(
11163 &array,
11164 &DataType::Decimal256(10, 8),
11165 &CastOptions {
11166 safe: false,
11167 format_options: FormatOptions::default(),
11168 },
11169 );
11170 assert_eq!(
11171 "Invalid argument error: 1000.00000000 is too large to store in a Decimal256 of precision 10. Max is 99.99999999",
11172 err.unwrap_err().to_string()
11173 );
11174 }
11175
11176 #[test]
11177 fn test_cast_utf8_to_decimal256_overflow() {
11178 let overflow_str_array = StringArray::from(vec![
11179 i128::MAX.to_string(),
11180 i128::MIN.to_string(),
11181 "99999999999999999999999999999999999999".to_string(),
11182 "999999999999999999999999999999999999.99".to_string(),
11183 "99999999999999999999999999999999999.999".to_string(),
11184 i256::MAX.to_string(),
11185 i256::MIN.to_string(),
11186 ]);
11187 let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
11188
11189 test_cast_string_to_decimal256_overflow(overflow_array);
11190 }
11191
11192 #[test]
11193 fn test_cast_large_utf8_to_decimal256_overflow() {
11194 let overflow_str_array = LargeStringArray::from(vec![
11195 i128::MAX.to_string(),
11196 i128::MIN.to_string(),
11197 "99999999999999999999999999999999999999".to_string(),
11198 "999999999999999999999999999999999999.99".to_string(),
11199 "99999999999999999999999999999999999.999".to_string(),
11200 i256::MAX.to_string(),
11201 i256::MIN.to_string(),
11202 ]);
11203 let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
11204
11205 test_cast_string_to_decimal256_overflow(overflow_array);
11206 }
11207
11208 #[test]
11209 fn test_cast_outside_supported_range_for_nanoseconds() {
11210 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";
11211
11212 let array = StringArray::from(vec![Some("1650-01-01 01:01:01.000001")]);
11213
11214 let cast_options = CastOptions {
11215 safe: false,
11216 format_options: FormatOptions::default(),
11217 };
11218
11219 let result = cast_string_to_timestamp::<i32, TimestampNanosecondType>(
11220 &array,
11221 &None::<Arc<str>>,
11222 &cast_options,
11223 );
11224
11225 let err = result.unwrap_err();
11226 assert_eq!(
11227 err.to_string(),
11228 format!(
11229 "Cast error: Overflow converting {} to Nanosecond. {}",
11230 array.value(0),
11231 EXPECTED_ERROR_MESSAGE
11232 )
11233 );
11234 }
11235
11236 #[test]
11237 fn test_cast_date32_to_timestamp() {
11238 let a = Date32Array::from(vec![Some(18628), Some(18993), None]); let array = Arc::new(a) as ArrayRef;
11240 let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
11241 let c = b.as_primitive::<TimestampSecondType>();
11242 assert_eq!(1609459200, c.value(0));
11243 assert_eq!(1640995200, c.value(1));
11244 assert!(c.is_null(2));
11245 }
11246
11247 #[test]
11248 fn test_cast_date32_to_timestamp_ms() {
11249 let a = Date32Array::from(vec![Some(18628), Some(18993), None]); let array = Arc::new(a) as ArrayRef;
11251 let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
11252 let c = b
11253 .as_any()
11254 .downcast_ref::<TimestampMillisecondArray>()
11255 .unwrap();
11256 assert_eq!(1609459200000, c.value(0));
11257 assert_eq!(1640995200000, c.value(1));
11258 assert!(c.is_null(2));
11259 }
11260
11261 #[test]
11262 fn test_cast_date32_to_timestamp_us() {
11263 let a = Date32Array::from(vec![Some(18628), Some(18993), None]); let array = Arc::new(a) as ArrayRef;
11265 let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
11266 let c = b
11267 .as_any()
11268 .downcast_ref::<TimestampMicrosecondArray>()
11269 .unwrap();
11270 assert_eq!(1609459200000000, c.value(0));
11271 assert_eq!(1640995200000000, c.value(1));
11272 assert!(c.is_null(2));
11273 }
11274
11275 #[test]
11276 fn test_cast_date32_to_timestamp_ns() {
11277 let a = Date32Array::from(vec![Some(18628), Some(18993), None]); let array = Arc::new(a) as ArrayRef;
11279 let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
11280 let c = b
11281 .as_any()
11282 .downcast_ref::<TimestampNanosecondArray>()
11283 .unwrap();
11284 assert_eq!(1609459200000000000, c.value(0));
11285 assert_eq!(1640995200000000000, c.value(1));
11286 assert!(c.is_null(2));
11287 }
11288
11289 #[test]
11290 fn test_timezone_cast() {
11291 let a = StringArray::from(vec![
11292 "2000-01-01T12:00:00", "2020-12-15T12:34:56", ]);
11295 let array = Arc::new(a) as ArrayRef;
11296 let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
11297 let v = b.as_primitive::<TimestampNanosecondType>();
11298
11299 assert_eq!(v.value(0), 946728000000000000);
11300 assert_eq!(v.value(1), 1608035696000000000);
11301
11302 let b = cast(
11303 &b,
11304 &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
11305 )
11306 .unwrap();
11307 let v = b.as_primitive::<TimestampNanosecondType>();
11308
11309 assert_eq!(v.value(0), 946728000000000000);
11310 assert_eq!(v.value(1), 1608035696000000000);
11311
11312 let b = cast(
11313 &b,
11314 &DataType::Timestamp(TimeUnit::Millisecond, Some("+02:00".into())),
11315 )
11316 .unwrap();
11317 let v = b.as_primitive::<TimestampMillisecondType>();
11318
11319 assert_eq!(v.value(0), 946728000000);
11320 assert_eq!(v.value(1), 1608035696000);
11321 }
11322
11323 #[test]
11324 fn test_cast_utf8_to_timestamp() {
11325 fn test_tz(tz: Arc<str>) {
11326 let valid = StringArray::from(vec![
11327 "2023-01-01 04:05:06.789000-08:00",
11328 "2023-01-01 04:05:06.789000-07:00",
11329 "2023-01-01 04:05:06.789 -0800",
11330 "2023-01-01 04:05:06.789 -08:00",
11331 "2023-01-01 040506 +0730",
11332 "2023-01-01 040506 +07:30",
11333 "2023-01-01 04:05:06.789",
11334 "2023-01-01 04:05:06",
11335 "2023-01-01",
11336 ]);
11337
11338 let array = Arc::new(valid) as ArrayRef;
11339 let b = cast_with_options(
11340 &array,
11341 &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.clone())),
11342 &CastOptions {
11343 safe: false,
11344 format_options: FormatOptions::default(),
11345 },
11346 )
11347 .unwrap();
11348
11349 let tz = tz.as_ref().parse().unwrap();
11350
11351 let as_tz =
11352 |v: i64| as_datetime_with_timezone::<TimestampNanosecondType>(v, tz).unwrap();
11353
11354 let as_utc = |v: &i64| as_tz(*v).naive_utc().to_string();
11355 let as_local = |v: &i64| as_tz(*v).naive_local().to_string();
11356
11357 let values = b.as_primitive::<TimestampNanosecondType>().values();
11358 let utc_results: Vec<_> = values.iter().map(as_utc).collect();
11359 let local_results: Vec<_> = values.iter().map(as_local).collect();
11360
11361 assert_eq!(
11363 &utc_results[..6],
11364 &[
11365 "2023-01-01 12:05:06.789".to_string(),
11366 "2023-01-01 11:05:06.789".to_string(),
11367 "2023-01-01 12:05:06.789".to_string(),
11368 "2023-01-01 12:05:06.789".to_string(),
11369 "2022-12-31 20:35:06".to_string(),
11370 "2022-12-31 20:35:06".to_string(),
11371 ]
11372 );
11373 assert_eq!(
11375 &local_results[6..],
11376 &[
11377 "2023-01-01 04:05:06.789".to_string(),
11378 "2023-01-01 04:05:06".to_string(),
11379 "2023-01-01 00:00:00".to_string()
11380 ]
11381 )
11382 }
11383
11384 test_tz("+00:00".into());
11385 test_tz("+02:00".into());
11386 }
11387
11388 #[test]
11389 fn test_cast_invalid_utf8() {
11390 let v1: &[u8] = b"\xFF invalid";
11391 let v2: &[u8] = b"\x00 Foo";
11392 let s = BinaryArray::from(vec![v1, v2]);
11393 let options = CastOptions {
11394 safe: true,
11395 format_options: FormatOptions::default(),
11396 };
11397 let array = cast_with_options(&s, &DataType::Utf8, &options).unwrap();
11398 let a = array.as_string::<i32>();
11399 a.to_data().validate_full().unwrap();
11400
11401 assert_eq!(a.null_count(), 1);
11402 assert_eq!(a.len(), 2);
11403 assert!(a.is_null(0));
11404 assert_eq!(a.value(0), "");
11405 assert_eq!(a.value(1), "\x00 Foo");
11406 }
11407
11408 #[test]
11409 fn test_cast_utf8_to_timestamptz() {
11410 let valid = StringArray::from(vec!["2023-01-01"]);
11411
11412 let array = Arc::new(valid) as ArrayRef;
11413 let b = cast(
11414 &array,
11415 &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
11416 )
11417 .unwrap();
11418
11419 let expect = DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into()));
11420
11421 assert_eq!(b.data_type(), &expect);
11422 let c = b
11423 .as_any()
11424 .downcast_ref::<TimestampNanosecondArray>()
11425 .unwrap();
11426 assert_eq!(1672531200000000000, c.value(0));
11427 }
11428
11429 #[test]
11430 fn test_cast_decimal_to_string() {
11431 assert!(can_cast_types(
11432 &DataType::Decimal32(9, 4),
11433 &DataType::Utf8View
11434 ));
11435 assert!(can_cast_types(
11436 &DataType::Decimal64(16, 4),
11437 &DataType::Utf8View
11438 ));
11439 assert!(can_cast_types(
11440 &DataType::Decimal128(10, 4),
11441 &DataType::Utf8View
11442 ));
11443 assert!(can_cast_types(
11444 &DataType::Decimal256(38, 10),
11445 &DataType::Utf8View
11446 ));
11447
11448 macro_rules! assert_decimal_values {
11449 ($array:expr) => {
11450 let c = $array;
11451 assert_eq!("1123.454", c.value(0));
11452 assert_eq!("2123.456", c.value(1));
11453 assert_eq!("-3123.453", c.value(2));
11454 assert_eq!("-3123.456", c.value(3));
11455 assert_eq!("0.000", c.value(4));
11456 assert_eq!("0.123", c.value(5));
11457 assert_eq!("1234.567", c.value(6));
11458 assert_eq!("-1234.567", c.value(7));
11459 assert!(c.is_null(8));
11460 };
11461 }
11462
11463 fn test_decimal_to_string<IN: ArrowPrimitiveType, OffsetSize: OffsetSizeTrait>(
11464 output_type: DataType,
11465 array: PrimitiveArray<IN>,
11466 ) {
11467 let b = cast(&array, &output_type).unwrap();
11468
11469 assert_eq!(b.data_type(), &output_type);
11470 match b.data_type() {
11471 DataType::Utf8View => {
11472 let c = b.as_string_view();
11473 assert_decimal_values!(c);
11474 }
11475 DataType::Utf8 | DataType::LargeUtf8 => {
11476 let c = b.as_string::<OffsetSize>();
11477 assert_decimal_values!(c);
11478 }
11479 _ => (),
11480 }
11481 }
11482
11483 let array32: Vec<Option<i32>> = vec![
11484 Some(1123454),
11485 Some(2123456),
11486 Some(-3123453),
11487 Some(-3123456),
11488 Some(0),
11489 Some(123),
11490 Some(123456789),
11491 Some(-123456789),
11492 None,
11493 ];
11494 let array64: Vec<Option<i64>> = array32.iter().map(|num| num.map(|x| x as i64)).collect();
11495 let array128: Vec<Option<i128>> =
11496 array64.iter().map(|num| num.map(|x| x as i128)).collect();
11497 let array256: Vec<Option<i256>> = array128
11498 .iter()
11499 .map(|num| num.map(i256::from_i128))
11500 .collect();
11501
11502 test_decimal_to_string::<Decimal32Type, i32>(
11503 DataType::Utf8View,
11504 create_decimal32_array(array32.clone(), 7, 3).unwrap(),
11505 );
11506 test_decimal_to_string::<Decimal32Type, i32>(
11507 DataType::Utf8,
11508 create_decimal32_array(array32.clone(), 7, 3).unwrap(),
11509 );
11510 test_decimal_to_string::<Decimal32Type, i64>(
11511 DataType::LargeUtf8,
11512 create_decimal32_array(array32, 7, 3).unwrap(),
11513 );
11514
11515 test_decimal_to_string::<Decimal64Type, i32>(
11516 DataType::Utf8View,
11517 create_decimal64_array(array64.clone(), 7, 3).unwrap(),
11518 );
11519 test_decimal_to_string::<Decimal64Type, i32>(
11520 DataType::Utf8,
11521 create_decimal64_array(array64.clone(), 7, 3).unwrap(),
11522 );
11523 test_decimal_to_string::<Decimal64Type, i64>(
11524 DataType::LargeUtf8,
11525 create_decimal64_array(array64, 7, 3).unwrap(),
11526 );
11527
11528 test_decimal_to_string::<Decimal128Type, i32>(
11529 DataType::Utf8View,
11530 create_decimal128_array(array128.clone(), 7, 3).unwrap(),
11531 );
11532 test_decimal_to_string::<Decimal128Type, i32>(
11533 DataType::Utf8,
11534 create_decimal128_array(array128.clone(), 7, 3).unwrap(),
11535 );
11536 test_decimal_to_string::<Decimal128Type, i64>(
11537 DataType::LargeUtf8,
11538 create_decimal128_array(array128, 7, 3).unwrap(),
11539 );
11540
11541 test_decimal_to_string::<Decimal256Type, i32>(
11542 DataType::Utf8View,
11543 create_decimal256_array(array256.clone(), 7, 3).unwrap(),
11544 );
11545 test_decimal_to_string::<Decimal256Type, i32>(
11546 DataType::Utf8,
11547 create_decimal256_array(array256.clone(), 7, 3).unwrap(),
11548 );
11549 test_decimal_to_string::<Decimal256Type, i64>(
11550 DataType::LargeUtf8,
11551 create_decimal256_array(array256, 7, 3).unwrap(),
11552 );
11553 }
11554
11555 #[test]
11556 fn test_cast_numeric_to_decimal128_precision_overflow() {
11557 let array = Int64Array::from(vec![1234567]);
11558 let array = Arc::new(array) as ArrayRef;
11559 let casted_array = cast_with_options(
11560 &array,
11561 &DataType::Decimal128(7, 3),
11562 &CastOptions {
11563 safe: true,
11564 format_options: FormatOptions::default(),
11565 },
11566 );
11567 assert!(casted_array.is_ok());
11568 assert!(casted_array.unwrap().is_null(0));
11569
11570 let err = cast_with_options(
11571 &array,
11572 &DataType::Decimal128(7, 3),
11573 &CastOptions {
11574 safe: false,
11575 format_options: FormatOptions::default(),
11576 },
11577 );
11578 assert_eq!(
11579 "Invalid argument error: 1234567.000 is too large to store in a Decimal128 of precision 7. Max is 9999.999",
11580 err.unwrap_err().to_string()
11581 );
11582 }
11583
11584 #[test]
11585 fn test_cast_numeric_to_decimal256_precision_overflow() {
11586 let array = Int64Array::from(vec![1234567]);
11587 let array = Arc::new(array) as ArrayRef;
11588 let casted_array = cast_with_options(
11589 &array,
11590 &DataType::Decimal256(7, 3),
11591 &CastOptions {
11592 safe: true,
11593 format_options: FormatOptions::default(),
11594 },
11595 );
11596 assert!(casted_array.is_ok());
11597 assert!(casted_array.unwrap().is_null(0));
11598
11599 let err = cast_with_options(
11600 &array,
11601 &DataType::Decimal256(7, 3),
11602 &CastOptions {
11603 safe: false,
11604 format_options: FormatOptions::default(),
11605 },
11606 );
11607 assert_eq!(
11608 "Invalid argument error: 1234567.000 is too large to store in a Decimal256 of precision 7. Max is 9999.999",
11609 err.unwrap_err().to_string()
11610 );
11611 }
11612
11613 fn cast_from_duration_to_interval<T: ArrowTemporalType<Native = i64>>(
11615 array: Vec<i64>,
11616 cast_options: &CastOptions,
11617 ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
11618 let array = PrimitiveArray::<T>::new(array.into(), None);
11619 let array = Arc::new(array) as ArrayRef;
11620 let interval = DataType::Interval(IntervalUnit::MonthDayNano);
11621 let out = cast_with_options(&array, &interval, cast_options)?;
11622 let out = out.as_primitive::<IntervalMonthDayNanoType>().clone();
11623 Ok(out)
11624 }
11625
11626 #[test]
11627 fn test_cast_from_duration_to_interval() {
11628 let array = vec![1234567];
11630 let casted_array =
11631 cast_from_duration_to_interval::<DurationSecondType>(array, &CastOptions::default())
11632 .unwrap();
11633 assert_eq!(
11634 casted_array.data_type(),
11635 &DataType::Interval(IntervalUnit::MonthDayNano)
11636 );
11637 assert_eq!(
11638 casted_array.value(0),
11639 IntervalMonthDayNano::new(0, 0, 1234567000000000)
11640 );
11641
11642 let array = vec![i64::MAX];
11643 let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
11644 array.clone(),
11645 &CastOptions::default(),
11646 )
11647 .unwrap();
11648 assert!(!casted_array.is_valid(0));
11649
11650 let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
11651 array,
11652 &CastOptions {
11653 safe: false,
11654 format_options: FormatOptions::default(),
11655 },
11656 );
11657 assert!(casted_array.is_err());
11658
11659 let array = vec![1234567];
11661 let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
11662 array,
11663 &CastOptions::default(),
11664 )
11665 .unwrap();
11666 assert_eq!(
11667 casted_array.data_type(),
11668 &DataType::Interval(IntervalUnit::MonthDayNano)
11669 );
11670 assert_eq!(
11671 casted_array.value(0),
11672 IntervalMonthDayNano::new(0, 0, 1234567000000)
11673 );
11674
11675 let array = vec![i64::MAX];
11676 let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
11677 array.clone(),
11678 &CastOptions::default(),
11679 )
11680 .unwrap();
11681 assert!(!casted_array.is_valid(0));
11682
11683 let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
11684 array,
11685 &CastOptions {
11686 safe: false,
11687 format_options: FormatOptions::default(),
11688 },
11689 );
11690 assert!(casted_array.is_err());
11691
11692 let array = vec![1234567];
11694 let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
11695 array,
11696 &CastOptions::default(),
11697 )
11698 .unwrap();
11699 assert_eq!(
11700 casted_array.data_type(),
11701 &DataType::Interval(IntervalUnit::MonthDayNano)
11702 );
11703 assert_eq!(
11704 casted_array.value(0),
11705 IntervalMonthDayNano::new(0, 0, 1234567000)
11706 );
11707
11708 let array = vec![i64::MAX];
11709 let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
11710 array.clone(),
11711 &CastOptions::default(),
11712 )
11713 .unwrap();
11714 assert!(!casted_array.is_valid(0));
11715
11716 let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
11717 array,
11718 &CastOptions {
11719 safe: false,
11720 format_options: FormatOptions::default(),
11721 },
11722 );
11723 assert!(casted_array.is_err());
11724
11725 let array = vec![1234567];
11727 let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
11728 array,
11729 &CastOptions::default(),
11730 )
11731 .unwrap();
11732 assert_eq!(
11733 casted_array.data_type(),
11734 &DataType::Interval(IntervalUnit::MonthDayNano)
11735 );
11736 assert_eq!(
11737 casted_array.value(0),
11738 IntervalMonthDayNano::new(0, 0, 1234567)
11739 );
11740
11741 let array = vec![i64::MAX];
11742 let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
11743 array,
11744 &CastOptions {
11745 safe: false,
11746 format_options: FormatOptions::default(),
11747 },
11748 )
11749 .unwrap();
11750 assert_eq!(
11751 casted_array.value(0),
11752 IntervalMonthDayNano::new(0, 0, i64::MAX)
11753 );
11754 }
11755
11756 fn cast_from_interval_to_duration<T: ArrowTemporalType>(
11758 array: &IntervalMonthDayNanoArray,
11759 cast_options: &CastOptions,
11760 ) -> Result<PrimitiveArray<T>, ArrowError> {
11761 let casted_array = cast_with_options(&array, &T::DATA_TYPE, cast_options)?;
11762 casted_array
11763 .as_any()
11764 .downcast_ref::<PrimitiveArray<T>>()
11765 .ok_or_else(|| {
11766 ArrowError::ComputeError(format!("Failed to downcast to {}", T::DATA_TYPE))
11767 })
11768 .cloned()
11769 }
11770
11771 #[test]
11772 fn test_cast_from_interval_to_duration() {
11773 let nullable = CastOptions::default();
11774 let fallible = CastOptions {
11775 safe: false,
11776 format_options: FormatOptions::default(),
11777 };
11778 let v = IntervalMonthDayNano::new(0, 0, 1234567);
11779
11780 let array = vec![v].into();
11782 let casted_array: DurationSecondArray =
11783 cast_from_interval_to_duration(&array, &nullable).unwrap();
11784 assert_eq!(casted_array.value(0), 0);
11785
11786 let array = vec![IntervalMonthDayNano::MAX].into();
11787 let casted_array: DurationSecondArray =
11788 cast_from_interval_to_duration(&array, &nullable).unwrap();
11789 assert!(!casted_array.is_valid(0));
11790
11791 let res = cast_from_interval_to_duration::<DurationSecondType>(&array, &fallible);
11792 assert!(res.is_err());
11793
11794 let array = vec![v].into();
11796 let casted_array: DurationMillisecondArray =
11797 cast_from_interval_to_duration(&array, &nullable).unwrap();
11798 assert_eq!(casted_array.value(0), 1);
11799
11800 let array = vec![IntervalMonthDayNano::MAX].into();
11801 let casted_array: DurationMillisecondArray =
11802 cast_from_interval_to_duration(&array, &nullable).unwrap();
11803 assert!(!casted_array.is_valid(0));
11804
11805 let res = cast_from_interval_to_duration::<DurationMillisecondType>(&array, &fallible);
11806 assert!(res.is_err());
11807
11808 let array = vec![v].into();
11810 let casted_array: DurationMicrosecondArray =
11811 cast_from_interval_to_duration(&array, &nullable).unwrap();
11812 assert_eq!(casted_array.value(0), 1234);
11813
11814 let array = vec![IntervalMonthDayNano::MAX].into();
11815 let casted_array =
11816 cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &nullable).unwrap();
11817 assert!(!casted_array.is_valid(0));
11818
11819 let casted_array =
11820 cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &fallible);
11821 assert!(casted_array.is_err());
11822
11823 let array = vec![v].into();
11825 let casted_array: DurationNanosecondArray =
11826 cast_from_interval_to_duration(&array, &nullable).unwrap();
11827 assert_eq!(casted_array.value(0), 1234567);
11828
11829 let array = vec![IntervalMonthDayNano::MAX].into();
11830 let casted_array: DurationNanosecondArray =
11831 cast_from_interval_to_duration(&array, &nullable).unwrap();
11832 assert!(!casted_array.is_valid(0));
11833
11834 let casted_array =
11835 cast_from_interval_to_duration::<DurationNanosecondType>(&array, &fallible);
11836 assert!(casted_array.is_err());
11837
11838 let array = vec![
11839 IntervalMonthDayNanoType::make_value(0, 1, 0),
11840 IntervalMonthDayNanoType::make_value(-1, 0, 0),
11841 IntervalMonthDayNanoType::make_value(1, 1, 0),
11842 IntervalMonthDayNanoType::make_value(1, 0, 1),
11843 IntervalMonthDayNanoType::make_value(0, 0, -1),
11844 ]
11845 .into();
11846 let casted_array =
11847 cast_from_interval_to_duration::<DurationNanosecondType>(&array, &nullable).unwrap();
11848 assert!(!casted_array.is_valid(0));
11849 assert!(!casted_array.is_valid(1));
11850 assert!(!casted_array.is_valid(2));
11851 assert!(!casted_array.is_valid(3));
11852 assert!(casted_array.is_valid(4));
11853 assert_eq!(casted_array.value(4), -1);
11854 }
11855
11856 fn cast_from_interval_year_month_to_interval_month_day_nano(
11858 array: Vec<i32>,
11859 cast_options: &CastOptions,
11860 ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
11861 let array = PrimitiveArray::<IntervalYearMonthType>::from(array);
11862 let array = Arc::new(array) as ArrayRef;
11863 let casted_array = cast_with_options(
11864 &array,
11865 &DataType::Interval(IntervalUnit::MonthDayNano),
11866 cast_options,
11867 )?;
11868 casted_array
11869 .as_any()
11870 .downcast_ref::<IntervalMonthDayNanoArray>()
11871 .ok_or_else(|| {
11872 ArrowError::ComputeError(
11873 "Failed to downcast to IntervalMonthDayNanoArray".to_string(),
11874 )
11875 })
11876 .cloned()
11877 }
11878
11879 #[test]
11880 fn test_cast_from_interval_year_month_to_interval_month_day_nano() {
11881 let array = vec![1234567];
11883 let casted_array = cast_from_interval_year_month_to_interval_month_day_nano(
11884 array,
11885 &CastOptions::default(),
11886 )
11887 .unwrap();
11888 assert_eq!(
11889 casted_array.data_type(),
11890 &DataType::Interval(IntervalUnit::MonthDayNano)
11891 );
11892 assert_eq!(
11893 casted_array.value(0),
11894 IntervalMonthDayNano::new(1234567, 0, 0)
11895 );
11896 }
11897
11898 fn cast_from_interval_day_time_to_interval_month_day_nano(
11900 array: Vec<IntervalDayTime>,
11901 cast_options: &CastOptions,
11902 ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
11903 let array = PrimitiveArray::<IntervalDayTimeType>::from(array);
11904 let array = Arc::new(array) as ArrayRef;
11905 let casted_array = cast_with_options(
11906 &array,
11907 &DataType::Interval(IntervalUnit::MonthDayNano),
11908 cast_options,
11909 )?;
11910 Ok(casted_array
11911 .as_primitive::<IntervalMonthDayNanoType>()
11912 .clone())
11913 }
11914
11915 #[test]
11916 fn test_cast_from_interval_day_time_to_interval_month_day_nano() {
11917 let array = vec![IntervalDayTime::new(123, 0)];
11919 let casted_array =
11920 cast_from_interval_day_time_to_interval_month_day_nano(array, &CastOptions::default())
11921 .unwrap();
11922 assert_eq!(
11923 casted_array.data_type(),
11924 &DataType::Interval(IntervalUnit::MonthDayNano)
11925 );
11926 assert_eq!(casted_array.value(0), IntervalMonthDayNano::new(0, 123, 0));
11927 }
11928
11929 #[test]
11930 fn test_cast_below_unixtimestamp() {
11931 let valid = StringArray::from(vec![
11932 "1900-01-03 23:59:59",
11933 "1969-12-31 00:00:01",
11934 "1989-12-31 00:00:01",
11935 ]);
11936
11937 let array = Arc::new(valid) as ArrayRef;
11938 let casted_array = cast_with_options(
11939 &array,
11940 &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
11941 &CastOptions {
11942 safe: false,
11943 format_options: FormatOptions::default(),
11944 },
11945 )
11946 .unwrap();
11947
11948 let ts_array = casted_array
11949 .as_primitive::<TimestampNanosecondType>()
11950 .values()
11951 .iter()
11952 .map(|ts| ts / 1_000_000)
11953 .collect::<Vec<_>>();
11954
11955 let array = TimestampMillisecondArray::from(ts_array).with_timezone("+00:00".to_string());
11956 let casted_array = cast(&array, &DataType::Date32).unwrap();
11957 let date_array = casted_array.as_primitive::<Date32Type>();
11958 let casted_array = cast(&date_array, &DataType::Utf8).unwrap();
11959 let string_array = casted_array.as_string::<i32>();
11960 assert_eq!("1900-01-03", string_array.value(0));
11961 assert_eq!("1969-12-31", string_array.value(1));
11962 assert_eq!("1989-12-31", string_array.value(2));
11963 }
11964
11965 #[test]
11966 fn test_nested_list() {
11967 let mut list = ListBuilder::new(Int32Builder::new());
11968 list.append_value([Some(1), Some(2), Some(3)]);
11969 list.append_value([Some(4), None, Some(6)]);
11970 let list = list.finish();
11971
11972 let to_field = Field::new("nested", list.data_type().clone(), false);
11973 let to = DataType::List(Arc::new(to_field));
11974 let out = cast(&list, &to).unwrap();
11975 let opts = FormatOptions::default().with_null("null");
11976 let formatted = ArrayFormatter::try_new(out.as_ref(), &opts).unwrap();
11977
11978 assert_eq!(formatted.value(0).to_string(), "[[1], [2], [3]]");
11979 assert_eq!(formatted.value(1).to_string(), "[[4], [null], [6]]");
11980 }
11981
11982 #[test]
11983 fn test_nested_list_cast() {
11984 let mut builder = ListBuilder::new(ListBuilder::new(Int32Builder::new()));
11985 builder.append_value([Some([Some(1), Some(2), None]), None]);
11986 builder.append_value([None, Some([]), None]);
11987 builder.append_null();
11988 builder.append_value([Some([Some(2), Some(3)])]);
11989 let start = builder.finish();
11990
11991 let mut builder = LargeListBuilder::new(LargeListBuilder::new(Int8Builder::new()));
11992 builder.append_value([Some([Some(1), Some(2), None]), None]);
11993 builder.append_value([None, Some([]), None]);
11994 builder.append_null();
11995 builder.append_value([Some([Some(2), Some(3)])]);
11996 let expected = builder.finish();
11997
11998 let actual = cast(&start, expected.data_type()).unwrap();
11999 assert_eq!(actual.as_ref(), &expected);
12000 }
12001
12002 const CAST_OPTIONS: CastOptions<'static> = CastOptions {
12003 safe: true,
12004 format_options: FormatOptions::new(),
12005 };
12006
12007 #[test]
12008 #[allow(clippy::assertions_on_constants)]
12009 fn test_const_options() {
12010 assert!(CAST_OPTIONS.safe)
12011 }
12012
12013 #[test]
12014 fn test_list_format_options() {
12015 let options = CastOptions {
12016 safe: false,
12017 format_options: FormatOptions::default().with_null("null"),
12018 };
12019 let array = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
12020 Some(vec![Some(0), Some(1), Some(2)]),
12021 Some(vec![Some(0), None, Some(2)]),
12022 ]);
12023 let a = cast_with_options(&array, &DataType::Utf8, &options).unwrap();
12024 let r: Vec<_> = a.as_string::<i32>().iter().flatten().collect();
12025 assert_eq!(r, &["[0, 1, 2]", "[0, null, 2]"]);
12026 }
12027 #[test]
12028 fn test_cast_string_to_timestamp_invalid_tz() {
12029 let bad_timestamp = "2023-12-05T21:58:10.45ZZTOP";
12031 let array = StringArray::from(vec![Some(bad_timestamp)]);
12032
12033 let data_types = [
12034 DataType::Timestamp(TimeUnit::Second, None),
12035 DataType::Timestamp(TimeUnit::Millisecond, None),
12036 DataType::Timestamp(TimeUnit::Microsecond, None),
12037 DataType::Timestamp(TimeUnit::Nanosecond, None),
12038 ];
12039
12040 let cast_options = CastOptions {
12041 safe: false,
12042 ..Default::default()
12043 };
12044
12045 for dt in data_types {
12046 assert_eq!(
12047 cast_with_options(&array, &dt, &cast_options)
12048 .unwrap_err()
12049 .to_string(),
12050 "Parser error: Invalid timezone \"ZZTOP\": only offset based timezones supported without chrono-tz feature"
12051 );
12052 }
12053 }
12054 #[test]
12055 fn test_cast_struct_to_struct() {
12056 let struct_type = DataType::Struct(
12057 vec![
12058 Field::new("a", DataType::Boolean, false),
12059 Field::new("b", DataType::Int32, false),
12060 ]
12061 .into(),
12062 );
12063 let to_type = DataType::Struct(
12064 vec![
12065 Field::new("a", DataType::Utf8, false),
12066 Field::new("b", DataType::Utf8, false),
12067 ]
12068 .into(),
12069 );
12070 let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
12071 let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
12072 let struct_array = StructArray::from(vec![
12073 (
12074 Arc::new(Field::new("b", DataType::Boolean, false)),
12075 boolean.clone() as ArrayRef,
12076 ),
12077 (
12078 Arc::new(Field::new("c", DataType::Int32, false)),
12079 int.clone() as ArrayRef,
12080 ),
12081 ]);
12082 let casted_array = cast(&struct_array, &to_type).unwrap();
12083 let casted_array = casted_array.as_struct();
12084 assert_eq!(casted_array.data_type(), &to_type);
12085 let casted_boolean_array = casted_array
12086 .column(0)
12087 .as_string::<i32>()
12088 .into_iter()
12089 .flatten()
12090 .collect::<Vec<_>>();
12091 let casted_int_array = casted_array
12092 .column(1)
12093 .as_string::<i32>()
12094 .into_iter()
12095 .flatten()
12096 .collect::<Vec<_>>();
12097 assert_eq!(casted_boolean_array, vec!["false", "false", "true", "true"]);
12098 assert_eq!(casted_int_array, vec!["42", "28", "19", "31"]);
12099
12100 let to_type = DataType::Struct(
12102 vec![
12103 Field::new("a", DataType::Date32, false),
12104 Field::new("b", DataType::Utf8, false),
12105 ]
12106 .into(),
12107 );
12108 assert!(!can_cast_types(&struct_type, &to_type));
12109 let result = cast(&struct_array, &to_type);
12110 assert_eq!(
12111 "Cast error: Casting from Boolean to Date32 not supported",
12112 result.unwrap_err().to_string()
12113 );
12114 }
12115
12116 #[test]
12117 fn test_cast_struct_to_struct_nullability() {
12118 let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
12119 let int = Arc::new(Int32Array::from(vec![Some(42), None, Some(19), None]));
12120 let struct_array = StructArray::from(vec![
12121 (
12122 Arc::new(Field::new("b", DataType::Boolean, false)),
12123 boolean.clone() as ArrayRef,
12124 ),
12125 (
12126 Arc::new(Field::new("c", DataType::Int32, true)),
12127 int.clone() as ArrayRef,
12128 ),
12129 ]);
12130
12131 let to_type = DataType::Struct(
12133 vec![
12134 Field::new("a", DataType::Utf8, false),
12135 Field::new("b", DataType::Utf8, true),
12136 ]
12137 .into(),
12138 );
12139 cast(&struct_array, &to_type).expect("Cast nullable to nullable struct field should work");
12140
12141 let to_type = DataType::Struct(
12143 vec![
12144 Field::new("a", DataType::Utf8, false),
12145 Field::new("b", DataType::Utf8, false),
12146 ]
12147 .into(),
12148 );
12149 cast(&struct_array, &to_type)
12150 .expect_err("Cast nullable to non-nullable struct field should fail");
12151
12152 let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
12153 let int = Arc::new(Int32Array::from(vec![i32::MAX, 25, 1, 100]));
12154 let struct_array = StructArray::from(vec![
12155 (
12156 Arc::new(Field::new("b", DataType::Boolean, false)),
12157 boolean.clone() as ArrayRef,
12158 ),
12159 (
12160 Arc::new(Field::new("c", DataType::Int32, false)),
12161 int.clone() as ArrayRef,
12162 ),
12163 ]);
12164
12165 let to_type = DataType::Struct(
12167 vec![
12168 Field::new("a", DataType::Utf8, false),
12169 Field::new("b", DataType::Utf8, false),
12170 ]
12171 .into(),
12172 );
12173 cast(&struct_array, &to_type)
12174 .expect("Cast non-nullable to non-nullable struct field should work");
12175
12176 let to_type = DataType::Struct(
12178 vec![
12179 Field::new("a", DataType::Utf8, false),
12180 Field::new("b", DataType::Int8, false),
12181 ]
12182 .into(),
12183 );
12184 cast(&struct_array, &to_type).expect_err(
12185 "Cast non-nullable to non-nullable struct field returning null should fail",
12186 );
12187 }
12188
12189 #[test]
12190 fn test_cast_struct_to_non_struct() {
12191 let boolean = Arc::new(BooleanArray::from(vec![true, false]));
12192 let struct_array = StructArray::from(vec![(
12193 Arc::new(Field::new("a", DataType::Boolean, false)),
12194 boolean.clone() as ArrayRef,
12195 )]);
12196 let to_type = DataType::Utf8;
12197 let result = cast(&struct_array, &to_type);
12198 assert_eq!(
12199 r#"Cast error: Casting from Struct("a": non-null Boolean) to Utf8 not supported"#,
12200 result.unwrap_err().to_string()
12201 );
12202 }
12203
12204 #[test]
12205 fn test_cast_non_struct_to_struct() {
12206 let array = StringArray::from(vec!["a", "b"]);
12207 let to_type = DataType::Struct(vec![Field::new("a", DataType::Boolean, false)].into());
12208 let result = cast(&array, &to_type);
12209 assert_eq!(
12210 r#"Cast error: Casting from Utf8 to Struct("a": non-null Boolean) not supported"#,
12211 result.unwrap_err().to_string()
12212 );
12213 }
12214
12215 #[test]
12216 fn test_cast_struct_with_different_field_order() {
12217 let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
12219 let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
12220 let string = Arc::new(StringArray::from(vec!["foo", "bar", "baz", "qux"]));
12221
12222 let struct_array = StructArray::from(vec![
12223 (
12224 Arc::new(Field::new("a", DataType::Boolean, false)),
12225 boolean.clone() as ArrayRef,
12226 ),
12227 (
12228 Arc::new(Field::new("b", DataType::Int32, false)),
12229 int.clone() as ArrayRef,
12230 ),
12231 (
12232 Arc::new(Field::new("c", DataType::Utf8, false)),
12233 string.clone() as ArrayRef,
12234 ),
12235 ]);
12236
12237 let to_type = DataType::Struct(
12239 vec![
12240 Field::new("c", DataType::Utf8, false),
12241 Field::new("a", DataType::Utf8, false), Field::new("b", DataType::Utf8, false), ]
12244 .into(),
12245 );
12246
12247 let result = cast(&struct_array, &to_type).unwrap();
12248 let result_struct = result.as_struct();
12249
12250 assert_eq!(result_struct.data_type(), &to_type);
12251 assert_eq!(result_struct.num_columns(), 3);
12252
12253 let c_column = result_struct.column(0).as_string::<i32>();
12255 assert_eq!(
12256 c_column.into_iter().flatten().collect::<Vec<_>>(),
12257 vec!["foo", "bar", "baz", "qux"]
12258 );
12259
12260 let a_column = result_struct.column(1).as_string::<i32>();
12262 assert_eq!(
12263 a_column.into_iter().flatten().collect::<Vec<_>>(),
12264 vec!["false", "false", "true", "true"]
12265 );
12266
12267 let b_column = result_struct.column(2).as_string::<i32>();
12269 assert_eq!(
12270 b_column.into_iter().flatten().collect::<Vec<_>>(),
12271 vec!["42", "28", "19", "31"]
12272 );
12273 }
12274
12275 #[test]
12276 fn test_cast_struct_with_missing_field() {
12277 let boolean = Arc::new(BooleanArray::from(vec![false, true]));
12279 let struct_array = StructArray::from(vec![(
12280 Arc::new(Field::new("a", DataType::Boolean, false)),
12281 boolean.clone() as ArrayRef,
12282 )]);
12283
12284 let to_type = DataType::Struct(
12285 vec![
12286 Field::new("a", DataType::Utf8, false),
12287 Field::new("b", DataType::Int32, false), ]
12289 .into(),
12290 );
12291
12292 let result = cast(&struct_array, &to_type);
12293 assert!(result.is_err());
12294 assert_eq!(
12295 result.unwrap_err().to_string(),
12296 "Invalid argument error: Incorrect number of arrays for StructArray fields, expected 2 got 1"
12297 );
12298 }
12299
12300 #[test]
12301 fn test_cast_struct_with_subset_of_fields() {
12302 let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
12304 let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
12305 let string = Arc::new(StringArray::from(vec!["foo", "bar", "baz", "qux"]));
12306
12307 let struct_array = StructArray::from(vec![
12308 (
12309 Arc::new(Field::new("a", DataType::Boolean, false)),
12310 boolean.clone() as ArrayRef,
12311 ),
12312 (
12313 Arc::new(Field::new("b", DataType::Int32, false)),
12314 int.clone() as ArrayRef,
12315 ),
12316 (
12317 Arc::new(Field::new("c", DataType::Utf8, false)),
12318 string.clone() as ArrayRef,
12319 ),
12320 ]);
12321
12322 let to_type = DataType::Struct(
12324 vec![
12325 Field::new("c", DataType::Utf8, false),
12326 Field::new("a", DataType::Utf8, false),
12327 ]
12328 .into(),
12329 );
12330
12331 let result = cast(&struct_array, &to_type).unwrap();
12332 let result_struct = result.as_struct();
12333
12334 assert_eq!(result_struct.data_type(), &to_type);
12335 assert_eq!(result_struct.num_columns(), 2);
12336
12337 let c_column = result_struct.column(0).as_string::<i32>();
12339 assert_eq!(
12340 c_column.into_iter().flatten().collect::<Vec<_>>(),
12341 vec!["foo", "bar", "baz", "qux"]
12342 );
12343
12344 let a_column = result_struct.column(1).as_string::<i32>();
12346 assert_eq!(
12347 a_column.into_iter().flatten().collect::<Vec<_>>(),
12348 vec!["false", "false", "true", "true"]
12349 );
12350 }
12351
12352 #[test]
12353 fn test_can_cast_struct_rename_field() {
12354 let from_type = DataType::Struct(
12356 vec![
12357 Field::new("a", DataType::Int32, false),
12358 Field::new("b", DataType::Utf8, false),
12359 ]
12360 .into(),
12361 );
12362
12363 let to_type = DataType::Struct(
12364 vec![
12365 Field::new("a", DataType::Int64, false),
12366 Field::new("c", DataType::Boolean, false), ]
12368 .into(),
12369 );
12370
12371 assert!(can_cast_types(&from_type, &to_type));
12372 }
12373
12374 fn run_decimal_cast_test_case_between_multiple_types(t: DecimalCastTestConfig) {
12375 run_decimal_cast_test_case::<Decimal128Type, Decimal128Type>(t.clone());
12376 run_decimal_cast_test_case::<Decimal128Type, Decimal256Type>(t.clone());
12377 run_decimal_cast_test_case::<Decimal256Type, Decimal128Type>(t.clone());
12378 run_decimal_cast_test_case::<Decimal256Type, Decimal256Type>(t.clone());
12379 }
12380
12381 #[test]
12382 fn test_decimal_to_decimal_coverage() {
12383 let test_cases = [
12384 DecimalCastTestConfig {
12386 input_prec: 5,
12387 input_scale: 1,
12388 input_repr: 99999, output_prec: 10,
12390 output_scale: 6,
12391 expected_output_repr: Ok(9999900000), },
12393 DecimalCastTestConfig {
12395 input_prec: 5,
12396 input_scale: 1,
12397 input_repr: 99, output_prec: 7,
12399 output_scale: 6,
12400 expected_output_repr: Ok(9900000), },
12402 DecimalCastTestConfig {
12404 input_prec: 5,
12405 input_scale: 1,
12406 input_repr: 99999, output_prec: 7,
12408 output_scale: 6,
12409 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()) },
12411 DecimalCastTestConfig {
12413 input_prec: 5,
12414 input_scale: 3,
12415 input_repr: 99999, output_prec: 10,
12417 output_scale: 2,
12418 expected_output_repr: Ok(10000), },
12420 DecimalCastTestConfig {
12422 input_prec: 5,
12423 input_scale: 3,
12424 input_repr: 99994, output_prec: 10,
12426 output_scale: 2,
12427 expected_output_repr: Ok(9999), },
12429 DecimalCastTestConfig {
12431 input_prec: 5,
12432 input_scale: 3,
12433 input_repr: 99999, output_prec: 10,
12435 output_scale: 3,
12436 expected_output_repr: Ok(99999), },
12438 DecimalCastTestConfig {
12440 input_prec: 10,
12441 input_scale: 5,
12442 input_repr: 999999, output_prec: 8,
12444 output_scale: 7,
12445 expected_output_repr: Ok(99999900), },
12447 DecimalCastTestConfig {
12449 input_prec: 10,
12450 input_scale: 5,
12451 input_repr: 9999999, output_prec: 8,
12453 output_scale: 7,
12454 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()) },
12456 DecimalCastTestConfig {
12458 input_prec: 7,
12459 input_scale: 4,
12460 input_repr: 9999999, output_prec: 6,
12462 output_scale: 2,
12463 expected_output_repr: Ok(100000),
12464 },
12465 DecimalCastTestConfig {
12467 input_prec: 10,
12468 input_scale: 5,
12469 input_repr: 12345678, output_prec: 8,
12471 output_scale: 3,
12472 expected_output_repr: Ok(123457), },
12474 DecimalCastTestConfig {
12476 input_prec: 10,
12477 input_scale: 5,
12478 input_repr: 9999999, output_prec: 4,
12480 output_scale: 3,
12481 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()) },
12483 DecimalCastTestConfig {
12485 input_prec: 10,
12486 input_scale: 5,
12487 input_repr: 999999, output_prec: 6,
12489 output_scale: 5,
12490 expected_output_repr: Ok(999999), },
12492 DecimalCastTestConfig {
12494 input_prec: 10,
12495 input_scale: 5,
12496 input_repr: 9999999, output_prec: 6,
12498 output_scale: 5,
12499 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()) },
12501 DecimalCastTestConfig {
12503 input_prec: 7,
12504 input_scale: 4,
12505 input_repr: 12345, output_prec: 7,
12507 output_scale: 6,
12508 expected_output_repr: Ok(1234500), },
12510 DecimalCastTestConfig {
12512 input_prec: 7,
12513 input_scale: 4,
12514 input_repr: 123456, output_prec: 7,
12516 output_scale: 6,
12517 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()) },
12519 DecimalCastTestConfig {
12521 input_prec: 7,
12522 input_scale: 5,
12523 input_repr: 1234567, output_prec: 7,
12525 output_scale: 4,
12526 expected_output_repr: Ok(123457), },
12528 DecimalCastTestConfig {
12530 input_prec: 7,
12531 input_scale: 5,
12532 input_repr: 9999999, output_prec: 7,
12534 output_scale: 5,
12535 expected_output_repr: Ok(9999999), },
12537 DecimalCastTestConfig {
12539 input_prec: 7,
12540 input_scale: 0,
12541 input_repr: 1234567, output_prec: 8,
12543 output_scale: 0,
12544 expected_output_repr: Ok(1234567), },
12546 DecimalCastTestConfig {
12548 input_prec: 7,
12549 input_scale: 0,
12550 input_repr: 1234567, output_prec: 6,
12552 output_scale: 0,
12553 expected_output_repr: Err("Invalid argument error: 1234567 is too large to store in a {} of precision 6. Max is 999999".to_string())
12554 },
12555 DecimalCastTestConfig {
12557 input_prec: 7,
12558 input_scale: 0,
12559 input_repr: 123456, output_prec: 6,
12561 output_scale: 0,
12562 expected_output_repr: Ok(123456), },
12564 ];
12565
12566 for t in test_cases {
12567 run_decimal_cast_test_case_between_multiple_types(t);
12568 }
12569 }
12570
12571 #[test]
12572 fn test_decimal_to_decimal_increase_scale_and_precision_unchecked() {
12573 let test_cases = [
12574 DecimalCastTestConfig {
12575 input_prec: 5,
12576 input_scale: 0,
12577 input_repr: 99999,
12578 output_prec: 10,
12579 output_scale: 5,
12580 expected_output_repr: Ok(9999900000),
12581 },
12582 DecimalCastTestConfig {
12583 input_prec: 5,
12584 input_scale: 0,
12585 input_repr: -99999,
12586 output_prec: 10,
12587 output_scale: 5,
12588 expected_output_repr: Ok(-9999900000),
12589 },
12590 DecimalCastTestConfig {
12591 input_prec: 5,
12592 input_scale: 2,
12593 input_repr: 99999,
12594 output_prec: 10,
12595 output_scale: 5,
12596 expected_output_repr: Ok(99999000),
12597 },
12598 DecimalCastTestConfig {
12599 input_prec: 5,
12600 input_scale: -2,
12601 input_repr: -99999,
12602 output_prec: 10,
12603 output_scale: 3,
12604 expected_output_repr: Ok(-9999900000),
12605 },
12606 DecimalCastTestConfig {
12607 input_prec: 5,
12608 input_scale: 3,
12609 input_repr: -12345,
12610 output_prec: 6,
12611 output_scale: 5,
12612 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())
12613 },
12614 ];
12615
12616 for t in test_cases {
12617 run_decimal_cast_test_case_between_multiple_types(t);
12618 }
12619 }
12620
12621 #[test]
12622 fn test_decimal_to_decimal_decrease_scale_and_precision_unchecked() {
12623 let test_cases = [
12624 DecimalCastTestConfig {
12625 input_prec: 5,
12626 input_scale: 0,
12627 input_repr: 99999,
12628 output_scale: -3,
12629 output_prec: 3,
12630 expected_output_repr: Ok(100),
12631 },
12632 DecimalCastTestConfig {
12633 input_prec: 5,
12634 input_scale: 0,
12635 input_repr: -99999,
12636 output_prec: 1,
12637 output_scale: -5,
12638 expected_output_repr: Ok(-1),
12639 },
12640 DecimalCastTestConfig {
12641 input_prec: 10,
12642 input_scale: 2,
12643 input_repr: 123456789,
12644 output_prec: 5,
12645 output_scale: -2,
12646 expected_output_repr: Ok(12346),
12647 },
12648 DecimalCastTestConfig {
12649 input_prec: 10,
12650 input_scale: 4,
12651 input_repr: -9876543210,
12652 output_prec: 7,
12653 output_scale: 0,
12654 expected_output_repr: Ok(-987654),
12655 },
12656 DecimalCastTestConfig {
12657 input_prec: 7,
12658 input_scale: 4,
12659 input_repr: 9999999,
12660 output_prec: 6,
12661 output_scale: 3,
12662 expected_output_repr:
12663 Err("Invalid argument error: 1000.000 is too large to store in a {} of precision 6. Max is 999.999".to_string()),
12664 },
12665 ];
12666 for t in test_cases {
12667 run_decimal_cast_test_case_between_multiple_types(t);
12668 }
12669 }
12670
12671 #[test]
12672 fn test_decimal_to_decimal_throw_error_on_precision_overflow_same_scale() {
12673 let array = vec![Some(123456789)];
12674 let array = create_decimal128_array(array, 24, 2).unwrap();
12675 let input_type = DataType::Decimal128(24, 2);
12676 let output_type = DataType::Decimal128(6, 2);
12677 assert!(can_cast_types(&input_type, &output_type));
12678
12679 let options = CastOptions {
12680 safe: false,
12681 ..Default::default()
12682 };
12683 let result = cast_with_options(&array, &output_type, &options);
12684 assert_eq!(
12685 result.unwrap_err().to_string(),
12686 "Invalid argument error: 1234567.89 is too large to store in a Decimal128 of precision 6. Max is 9999.99"
12687 );
12688 }
12689
12690 #[test]
12691 fn test_decimal_to_decimal_same_scale() {
12692 let array = vec![Some(520)];
12693 let array = create_decimal128_array(array, 4, 2).unwrap();
12694 let input_type = DataType::Decimal128(4, 2);
12695 let output_type = DataType::Decimal128(3, 2);
12696 assert!(can_cast_types(&input_type, &output_type));
12697
12698 let options = CastOptions {
12699 safe: false,
12700 ..Default::default()
12701 };
12702 let result = cast_with_options(&array, &output_type, &options);
12703 assert_eq!(
12704 result.unwrap().as_primitive::<Decimal128Type>().value(0),
12705 520
12706 );
12707
12708 assert_eq!(
12710 &cast(
12711 &create_decimal128_array(vec![Some(0)], 3, 0).unwrap(),
12712 &DataType::Decimal128(2, 0)
12713 )
12714 .unwrap(),
12715 &(Arc::new(create_decimal128_array(vec![Some(0)], 2, 0).unwrap()) as ArrayRef)
12716 );
12717 }
12718
12719 #[test]
12720 fn test_decimal_to_decimal_throw_error_on_precision_overflow_lower_scale() {
12721 let array = vec![Some(123456789)];
12722 let array = create_decimal128_array(array, 24, 4).unwrap();
12723 let input_type = DataType::Decimal128(24, 4);
12724 let output_type = DataType::Decimal128(6, 2);
12725 assert!(can_cast_types(&input_type, &output_type));
12726
12727 let options = CastOptions {
12728 safe: false,
12729 ..Default::default()
12730 };
12731 let result = cast_with_options(&array, &output_type, &options);
12732 assert_eq!(
12733 result.unwrap_err().to_string(),
12734 "Invalid argument error: 12345.68 is too large to store in a Decimal128 of precision 6. Max is 9999.99"
12735 );
12736 }
12737
12738 #[test]
12739 fn test_decimal_to_decimal_throw_error_on_precision_overflow_greater_scale() {
12740 let array = vec![Some(123456789)];
12741 let array = create_decimal128_array(array, 24, 2).unwrap();
12742 let input_type = DataType::Decimal128(24, 2);
12743 let output_type = DataType::Decimal128(6, 3);
12744 assert!(can_cast_types(&input_type, &output_type));
12745
12746 let options = CastOptions {
12747 safe: false,
12748 ..Default::default()
12749 };
12750 let result = cast_with_options(&array, &output_type, &options);
12751 assert_eq!(
12752 result.unwrap_err().to_string(),
12753 "Invalid argument error: 1234567.890 is too large to store in a Decimal128 of precision 6. Max is 999.999"
12754 );
12755 }
12756
12757 #[test]
12758 fn test_decimal_to_decimal_throw_error_on_precision_overflow_diff_type() {
12759 let array = vec![Some(123456789)];
12760 let array = create_decimal128_array(array, 24, 2).unwrap();
12761 let input_type = DataType::Decimal128(24, 2);
12762 let output_type = DataType::Decimal256(6, 2);
12763 assert!(can_cast_types(&input_type, &output_type));
12764
12765 let options = CastOptions {
12766 safe: false,
12767 ..Default::default()
12768 };
12769 let result = cast_with_options(&array, &output_type, &options).unwrap_err();
12770 assert_eq!(
12771 result.to_string(),
12772 "Invalid argument error: 1234567.89 is too large to store in a Decimal256 of precision 6. Max is 9999.99"
12773 );
12774 }
12775
12776 #[test]
12777 fn test_first_none() {
12778 let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
12779 None,
12780 Some(vec![Some(1), Some(2)]),
12781 ])) as ArrayRef;
12782 let data_type =
12783 DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
12784 let opt = CastOptions::default();
12785 let r = cast_with_options(&array, &data_type, &opt).unwrap();
12786
12787 let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
12788 vec![None, Some(vec![Some(1), Some(2)])],
12789 2,
12790 )) as ArrayRef;
12791 assert_eq!(*fixed_array, *r);
12792 }
12793
12794 #[test]
12795 fn test_first_last_none() {
12796 let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
12797 None,
12798 Some(vec![Some(1), Some(2)]),
12799 None,
12800 ])) as ArrayRef;
12801 let data_type =
12802 DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
12803 let opt = CastOptions::default();
12804 let r = cast_with_options(&array, &data_type, &opt).unwrap();
12805
12806 let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
12807 vec![None, Some(vec![Some(1), Some(2)]), None],
12808 2,
12809 )) as ArrayRef;
12810 assert_eq!(*fixed_array, *r);
12811 }
12812
12813 #[test]
12814 fn test_cast_decimal_error_output() {
12815 let array = Int64Array::from(vec![1]);
12816 let error = cast_with_options(
12817 &array,
12818 &DataType::Decimal32(1, 1),
12819 &CastOptions {
12820 safe: false,
12821 format_options: FormatOptions::default(),
12822 },
12823 )
12824 .unwrap_err();
12825 assert_eq!(
12826 error.to_string(),
12827 "Invalid argument error: 1.0 is too large to store in a Decimal32 of precision 1. Max is 0.9"
12828 );
12829
12830 let array = Int64Array::from(vec![-1]);
12831 let error = cast_with_options(
12832 &array,
12833 &DataType::Decimal32(1, 1),
12834 &CastOptions {
12835 safe: false,
12836 format_options: FormatOptions::default(),
12837 },
12838 )
12839 .unwrap_err();
12840 assert_eq!(
12841 error.to_string(),
12842 "Invalid argument error: -1.0 is too small to store in a Decimal32 of precision 1. Min is -0.9"
12843 );
12844 }
12845
12846 #[test]
12847 fn test_run_end_encoded_to_primitive() {
12848 let run_ends = Int32Array::from(vec![2, 5, 6]);
12850 let values = Int32Array::from(vec![1, 2, 3]);
12851 let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
12852 let array_ref = Arc::new(run_array) as ArrayRef;
12853 let cast_result = cast(&array_ref, &DataType::Int64).unwrap();
12855 let result_run_array = cast_result.as_any().downcast_ref::<Int64Array>().unwrap();
12857 assert_eq!(
12858 result_run_array.values(),
12859 &[1i64, 1i64, 2i64, 2i64, 2i64, 3i64]
12860 );
12861 }
12862
12863 #[test]
12864 fn test_sliced_run_end_encoded_to_primitive() {
12865 let run_ends = Int32Array::from(vec![2, 5, 6]);
12866 let values = Int32Array::from(vec![1, 2, 3]);
12867 let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
12869 let run_array = run_array.slice(3, 3); let array_ref = Arc::new(run_array) as ArrayRef;
12871
12872 let cast_result = cast(&array_ref, &DataType::Int64).unwrap();
12873 let result_run_array = cast_result.as_primitive::<Int64Type>();
12874 assert_eq!(result_run_array.values(), &[2, 2, 3]);
12875 }
12876
12877 #[test]
12878 fn test_run_end_encoded_to_string() {
12879 let run_ends = Int32Array::from(vec![2, 3, 5]);
12880 let values = Int32Array::from(vec![10, 20, 30]);
12881 let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
12882 let array_ref = Arc::new(run_array) as ArrayRef;
12883
12884 let cast_result = cast(&array_ref, &DataType::Utf8).unwrap();
12886
12887 let result_array = cast_result.as_any().downcast_ref::<StringArray>().unwrap();
12889 assert_eq!(result_array.value(0), "10");
12891 assert_eq!(result_array.value(1), "10");
12892 assert_eq!(result_array.value(2), "20");
12893 }
12894
12895 #[test]
12896 fn test_primitive_to_run_end_encoded() {
12897 let source_array = Int32Array::from(vec![1, 1, 2, 2, 2, 3]);
12899 let array_ref = Arc::new(source_array) as ArrayRef;
12900
12901 let target_type = DataType::RunEndEncoded(
12903 Arc::new(Field::new("run_ends", DataType::Int32, false)),
12904 Arc::new(Field::new("values", DataType::Int32, true)),
12905 );
12906 let cast_result = cast(&array_ref, &target_type).unwrap();
12907
12908 let result_run_array = cast_result
12910 .as_any()
12911 .downcast_ref::<RunArray<Int32Type>>()
12912 .unwrap();
12913
12914 assert_eq!(result_run_array.run_ends().values(), &[2, 5, 6]);
12916
12917 let values_array = result_run_array.values().as_primitive::<Int32Type>();
12919 assert_eq!(values_array.values(), &[1, 2, 3]);
12920 }
12921
12922 #[test]
12923 fn test_primitive_to_run_end_encoded_with_nulls() {
12924 let source_array = Int32Array::from(vec![
12925 Some(1),
12926 Some(1),
12927 None,
12928 None,
12929 Some(2),
12930 Some(2),
12931 Some(3),
12932 Some(3),
12933 None,
12934 None,
12935 Some(4),
12936 Some(4),
12937 Some(5),
12938 Some(5),
12939 None,
12940 None,
12941 ]);
12942 let array_ref = Arc::new(source_array) as ArrayRef;
12943 let target_type = DataType::RunEndEncoded(
12944 Arc::new(Field::new("run_ends", DataType::Int32, false)),
12945 Arc::new(Field::new("values", DataType::Int32, true)),
12946 );
12947 let cast_result = cast(&array_ref, &target_type).unwrap();
12948 let result_run_array = cast_result
12949 .as_any()
12950 .downcast_ref::<RunArray<Int32Type>>()
12951 .unwrap();
12952 assert_eq!(
12953 result_run_array.run_ends().values(),
12954 &[2, 4, 6, 8, 10, 12, 14, 16]
12955 );
12956 assert_eq!(
12957 result_run_array
12958 .values()
12959 .as_primitive::<Int32Type>()
12960 .values(),
12961 &[1, 0, 2, 3, 0, 4, 5, 0]
12962 );
12963 assert_eq!(result_run_array.values().null_count(), 3);
12964 }
12965
12966 #[test]
12967 fn test_primitive_to_run_end_encoded_with_nulls_consecutive() {
12968 let source_array = Int64Array::from(vec![
12969 Some(1),
12970 Some(1),
12971 None,
12972 None,
12973 None,
12974 None,
12975 None,
12976 None,
12977 None,
12978 None,
12979 Some(4),
12980 Some(20),
12981 Some(500),
12982 Some(500),
12983 None,
12984 None,
12985 ]);
12986 let array_ref = Arc::new(source_array) as ArrayRef;
12987 let target_type = DataType::RunEndEncoded(
12988 Arc::new(Field::new("run_ends", DataType::Int16, false)),
12989 Arc::new(Field::new("values", DataType::Int64, true)),
12990 );
12991 let cast_result = cast(&array_ref, &target_type).unwrap();
12992 let result_run_array = cast_result
12993 .as_any()
12994 .downcast_ref::<RunArray<Int16Type>>()
12995 .unwrap();
12996 assert_eq!(
12997 result_run_array.run_ends().values(),
12998 &[2, 10, 11, 12, 14, 16]
12999 );
13000 assert_eq!(
13001 result_run_array
13002 .values()
13003 .as_primitive::<Int64Type>()
13004 .values(),
13005 &[1, 0, 4, 20, 500, 0]
13006 );
13007 assert_eq!(result_run_array.values().null_count(), 2);
13008 }
13009
13010 #[test]
13011 fn test_string_to_run_end_encoded() {
13012 let source_array = StringArray::from(vec!["a", "a", "b", "c", "c"]);
13014 let array_ref = Arc::new(source_array) as ArrayRef;
13015
13016 let target_type = DataType::RunEndEncoded(
13018 Arc::new(Field::new("run_ends", DataType::Int32, false)),
13019 Arc::new(Field::new("values", DataType::Utf8, true)),
13020 );
13021 let cast_result = cast(&array_ref, &target_type).unwrap();
13022
13023 let result_run_array = cast_result
13025 .as_any()
13026 .downcast_ref::<RunArray<Int32Type>>()
13027 .unwrap();
13028
13029 assert_eq!(result_run_array.run_ends().values(), &[2, 3, 5]);
13031
13032 let values_array = result_run_array.values().as_string::<i32>();
13034 assert_eq!(values_array.value(0), "a");
13035 assert_eq!(values_array.value(1), "b");
13036 assert_eq!(values_array.value(2), "c");
13037 }
13038
13039 #[test]
13040 fn test_empty_array_to_run_end_encoded() {
13041 let source_array = Int32Array::from(Vec::<i32>::new());
13043 let array_ref = Arc::new(source_array) as ArrayRef;
13044
13045 let target_type = DataType::RunEndEncoded(
13047 Arc::new(Field::new("run_ends", DataType::Int32, false)),
13048 Arc::new(Field::new("values", DataType::Int32, true)),
13049 );
13050 let cast_result = cast(&array_ref, &target_type).unwrap();
13051
13052 let result_run_array = cast_result
13054 .as_any()
13055 .downcast_ref::<RunArray<Int32Type>>()
13056 .unwrap();
13057
13058 assert_eq!(result_run_array.run_ends().len(), 0);
13060 assert_eq!(result_run_array.values().len(), 0);
13061 }
13062
13063 #[test]
13064 fn test_run_end_encoded_with_nulls() {
13065 let run_ends = Int32Array::from(vec![2, 3, 5]);
13067 let values = Int32Array::from(vec![Some(1), None, Some(2)]);
13068 let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
13069 let array_ref = Arc::new(run_array) as ArrayRef;
13070
13071 let cast_result = cast(&array_ref, &DataType::Utf8).unwrap();
13073
13074 let result_run_array = cast_result.as_any().downcast_ref::<StringArray>().unwrap();
13076 assert_eq!(result_run_array.value(0), "1");
13077 assert!(result_run_array.is_null(2));
13078 assert_eq!(result_run_array.value(4), "2");
13079 }
13080
13081 #[test]
13082 fn test_different_index_types() {
13083 let source_array = Int32Array::from(vec![1, 1, 2, 3, 3]);
13085 let array_ref = Arc::new(source_array) as ArrayRef;
13086
13087 let target_type = DataType::RunEndEncoded(
13088 Arc::new(Field::new("run_ends", DataType::Int16, false)),
13089 Arc::new(Field::new("values", DataType::Int32, true)),
13090 );
13091 let cast_result = cast(&array_ref, &target_type).unwrap();
13092 assert_eq!(cast_result.data_type(), &target_type);
13093
13094 let run_array = cast_result
13097 .as_any()
13098 .downcast_ref::<RunArray<Int16Type>>()
13099 .unwrap();
13100 assert_eq!(run_array.values().as_primitive::<Int32Type>().value(0), 1);
13101 assert_eq!(run_array.values().as_primitive::<Int32Type>().value(1), 2);
13102 assert_eq!(run_array.values().as_primitive::<Int32Type>().value(2), 3);
13103 assert_eq!(run_array.run_ends().values(), &[2i16, 3i16, 5i16]);
13104
13105 let target_type = DataType::RunEndEncoded(
13107 Arc::new(Field::new("run_ends", DataType::Int64, false)),
13108 Arc::new(Field::new("values", DataType::Int32, true)),
13109 );
13110 let cast_result = cast(&array_ref, &target_type).unwrap();
13111 assert_eq!(cast_result.data_type(), &target_type);
13112
13113 let run_array = cast_result
13116 .as_any()
13117 .downcast_ref::<RunArray<Int64Type>>()
13118 .unwrap();
13119 assert_eq!(run_array.values().as_primitive::<Int32Type>().value(0), 1);
13120 assert_eq!(run_array.values().as_primitive::<Int32Type>().value(1), 2);
13121 assert_eq!(run_array.values().as_primitive::<Int32Type>().value(2), 3);
13122 assert_eq!(run_array.run_ends().values(), &[2i64, 3i64, 5i64]);
13123 }
13124
13125 #[test]
13126 fn test_unsupported_cast_to_run_end_encoded() {
13127 let field = Field::new("item", DataType::Int32, false);
13129 let struct_array = StructArray::from(vec![(
13130 Arc::new(field),
13131 Arc::new(Int32Array::from(vec![1, 2, 3])) as ArrayRef,
13132 )]);
13133 let array_ref = Arc::new(struct_array) as ArrayRef;
13134
13135 let cast_result = cast(&array_ref, &DataType::FixedSizeBinary(10));
13139
13140 assert!(cast_result.is_err());
13142 }
13143
13144 #[test]
13146 fn test_cast_run_end_encoded_int64_to_int16_should_fail() {
13147 let run_ends = Int64Array::from(vec![100_000, 400_000, 700_000]); let values = StringArray::from(vec!["a", "b", "c"]);
13150
13151 let ree_array = RunArray::<Int64Type>::try_new(&run_ends, &values).unwrap();
13152 let array_ref = Arc::new(ree_array) as ArrayRef;
13153
13154 let target_type = DataType::RunEndEncoded(
13156 Arc::new(Field::new("run_ends", DataType::Int16, false)),
13157 Arc::new(Field::new("values", DataType::Utf8, true)),
13158 );
13159 let cast_options = CastOptions {
13160 safe: false, format_options: FormatOptions::default(),
13162 };
13163
13164 let result: Result<Arc<dyn Array + 'static>, ArrowError> =
13166 cast_with_options(&array_ref, &target_type, &cast_options);
13167
13168 let e = result.expect_err("Cast should have failed but succeeded");
13169 assert!(
13170 e.to_string()
13171 .contains("Cast error: Can't cast value 100000 to type Int16")
13172 );
13173 }
13174
13175 #[test]
13176 fn test_cast_run_end_encoded_int64_to_int16_with_safe_should_fail_with_null_invalid_error() {
13177 let run_ends = Int64Array::from(vec![100_000, 400_000, 700_000]); let values = StringArray::from(vec!["a", "b", "c"]);
13180
13181 let ree_array = RunArray::<Int64Type>::try_new(&run_ends, &values).unwrap();
13182 let array_ref = Arc::new(ree_array) as ArrayRef;
13183
13184 let target_type = DataType::RunEndEncoded(
13186 Arc::new(Field::new("run_ends", DataType::Int16, false)),
13187 Arc::new(Field::new("values", DataType::Utf8, true)),
13188 );
13189 let cast_options = CastOptions {
13190 safe: true,
13191 format_options: FormatOptions::default(),
13192 };
13193
13194 let result: Result<Arc<dyn Array + 'static>, ArrowError> =
13196 cast_with_options(&array_ref, &target_type, &cast_options);
13197 let e = result.expect_err("Cast should have failed but succeeded");
13198 assert!(
13199 e.to_string()
13200 .contains("Invalid argument error: Found null values in run_ends array. The run_ends array should not have null values.")
13201 );
13202 }
13203
13204 #[test]
13206 fn test_cast_run_end_encoded_int16_to_int64_should_succeed() {
13207 let run_ends = Int16Array::from(vec![2, 5, 8]); let values = StringArray::from(vec!["a", "b", "c"]);
13210
13211 let ree_array = RunArray::<Int16Type>::try_new(&run_ends, &values).unwrap();
13212 let array_ref = Arc::new(ree_array) as ArrayRef;
13213
13214 let target_type = DataType::RunEndEncoded(
13216 Arc::new(Field::new("run_ends", DataType::Int64, false)),
13217 Arc::new(Field::new("values", DataType::Utf8, true)),
13218 );
13219 let cast_options = CastOptions {
13220 safe: false,
13221 format_options: FormatOptions::default(),
13222 };
13223
13224 let result: Result<Arc<dyn Array + 'static>, ArrowError> =
13226 cast_with_options(&array_ref, &target_type, &cast_options);
13227
13228 let array_ref = result.expect("Cast should have succeeded but failed");
13229 let run_array = array_ref
13231 .as_any()
13232 .downcast_ref::<RunArray<Int64Type>>()
13233 .unwrap();
13234
13235 assert_eq!(run_array.run_ends().values(), &[2i64, 5i64, 8i64]);
13238 assert_eq!(run_array.values().as_string::<i32>().value(0), "a");
13239 assert_eq!(run_array.values().as_string::<i32>().value(1), "b");
13240 assert_eq!(run_array.values().as_string::<i32>().value(2), "c");
13241 }
13242
13243 #[test]
13244 fn test_cast_run_end_encoded_dictionary_to_run_end_encoded() {
13245 let values = StringArray::from_iter([Some("a"), Some("b"), Some("c")]);
13247 let keys = UInt64Array::from_iter(vec![1, 1, 1, 0, 0, 0, 2, 2, 2]);
13248 let array_ref = Arc::new(DictionaryArray::new(keys, Arc::new(values))) as ArrayRef;
13249
13250 let target_type = DataType::RunEndEncoded(
13252 Arc::new(Field::new("run_ends", DataType::Int64, false)),
13253 Arc::new(Field::new("values", DataType::Utf8, true)),
13254 );
13255 let cast_options = CastOptions {
13256 safe: false,
13257 format_options: FormatOptions::default(),
13258 };
13259
13260 let result = cast_with_options(&array_ref, &target_type, &cast_options)
13262 .expect("Cast should have succeeded but failed");
13263
13264 let run_array = result
13267 .as_any()
13268 .downcast_ref::<RunArray<Int64Type>>()
13269 .unwrap();
13270 assert_eq!(run_array.values().as_string::<i32>().value(0), "b");
13271 assert_eq!(run_array.values().as_string::<i32>().value(1), "a");
13272 assert_eq!(run_array.values().as_string::<i32>().value(2), "c");
13273
13274 assert_eq!(run_array.run_ends().values(), &[3i64, 6i64, 9i64]);
13276 }
13277
13278 fn int32_list_values() -> Vec<Option<Vec<Option<i32>>>> {
13279 vec![
13280 Some(vec![Some(1), Some(2), Some(3)]),
13281 Some(vec![Some(4), Some(5), Some(6)]),
13282 None,
13283 Some(vec![Some(7), Some(8), Some(9)]),
13284 Some(vec![None, Some(10)]),
13285 ]
13286 }
13287
13288 #[test]
13289 fn test_cast_list_view_to_list() {
13290 let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13291 let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13292 assert!(can_cast_types(list_view.data_type(), &target_type));
13293 let cast_result = cast(&list_view, &target_type).unwrap();
13294 let got_list = cast_result.as_list::<i32>();
13295 let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13296 assert_eq!(got_list, &expected_list);
13297 }
13298
13299 #[test]
13300 fn test_cast_list_view_to_large_list() {
13301 let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13302 let target_type = DataType::LargeList(Arc::new(Field::new("item", DataType::Int32, true)));
13303 assert!(can_cast_types(list_view.data_type(), &target_type));
13304 let cast_result = cast(&list_view, &target_type).unwrap();
13305 let got_list = cast_result.as_list::<i64>();
13306 let expected_list =
13307 LargeListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13308 assert_eq!(got_list, &expected_list);
13309 }
13310
13311 #[test]
13312 fn test_cast_list_to_list_view() {
13313 let list = ListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13314 let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Int32, true)));
13315 assert!(can_cast_types(list.data_type(), &target_type));
13316 let cast_result = cast(&list, &target_type).unwrap();
13317
13318 let got_list_view = cast_result.as_list_view::<i32>();
13319 let expected_list_view =
13320 ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13321 assert_eq!(got_list_view, &expected_list_view);
13322
13323 let list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13325 Some(vec![Some(1), Some(2)]),
13326 None,
13327 Some(vec![None, Some(3)]),
13328 ]);
13329 let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Float32, true)));
13330 assert!(can_cast_types(list.data_type(), &target_type));
13331 let cast_result = cast(&list, &target_type).unwrap();
13332
13333 let got_list_view = cast_result.as_list_view::<i32>();
13334 let expected_list_view = ListViewArray::from_iter_primitive::<Float32Type, _, _>(vec![
13335 Some(vec![Some(1.0), Some(2.0)]),
13336 None,
13337 Some(vec![None, Some(3.0)]),
13338 ]);
13339 assert_eq!(got_list_view, &expected_list_view);
13340 }
13341
13342 #[test]
13343 fn test_cast_list_to_large_list_view() {
13344 let list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13345 Some(vec![Some(1), Some(2)]),
13346 None,
13347 Some(vec![None, Some(3)]),
13348 ]);
13349 let target_type =
13350 DataType::LargeListView(Arc::new(Field::new("item", DataType::Float32, true)));
13351 assert!(can_cast_types(list.data_type(), &target_type));
13352 let cast_result = cast(&list, &target_type).unwrap();
13353
13354 let got_list_view = cast_result.as_list_view::<i64>();
13355 let expected_list_view =
13356 LargeListViewArray::from_iter_primitive::<Float32Type, _, _>(vec![
13357 Some(vec![Some(1.0), Some(2.0)]),
13358 None,
13359 Some(vec![None, Some(3.0)]),
13360 ]);
13361 assert_eq!(got_list_view, &expected_list_view);
13362 }
13363
13364 #[test]
13365 fn test_cast_large_list_view_to_large_list() {
13366 let list_view =
13367 LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13368 let target_type = DataType::LargeList(Arc::new(Field::new("item", DataType::Int32, true)));
13369 assert!(can_cast_types(list_view.data_type(), &target_type));
13370 let cast_result = cast(&list_view, &target_type).unwrap();
13371 let got_list = cast_result.as_list::<i64>();
13372
13373 let expected_list =
13374 LargeListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13375 assert_eq!(got_list, &expected_list);
13376 }
13377
13378 #[test]
13379 fn test_cast_large_list_view_to_list() {
13380 let list_view =
13381 LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13382 let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13383 assert!(can_cast_types(list_view.data_type(), &target_type));
13384 let cast_result = cast(&list_view, &target_type).unwrap();
13385 let got_list = cast_result.as_list::<i32>();
13386
13387 let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13388 assert_eq!(got_list, &expected_list);
13389 }
13390
13391 #[test]
13392 fn test_cast_large_list_to_large_list_view() {
13393 let list = LargeListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13394 let target_type =
13395 DataType::LargeListView(Arc::new(Field::new("item", DataType::Int32, true)));
13396 assert!(can_cast_types(list.data_type(), &target_type));
13397 let cast_result = cast(&list, &target_type).unwrap();
13398
13399 let got_list_view = cast_result.as_list_view::<i64>();
13400 let expected_list_view =
13401 LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13402 assert_eq!(got_list_view, &expected_list_view);
13403
13404 let list = LargeListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13406 Some(vec![Some(1), Some(2)]),
13407 None,
13408 Some(vec![None, Some(3)]),
13409 ]);
13410 let target_type =
13411 DataType::LargeListView(Arc::new(Field::new("item", DataType::Float32, true)));
13412 assert!(can_cast_types(list.data_type(), &target_type));
13413 let cast_result = cast(&list, &target_type).unwrap();
13414
13415 let got_list_view = cast_result.as_list_view::<i64>();
13416 let expected_list_view =
13417 LargeListViewArray::from_iter_primitive::<Float32Type, _, _>(vec![
13418 Some(vec![Some(1.0), Some(2.0)]),
13419 None,
13420 Some(vec![None, Some(3.0)]),
13421 ]);
13422 assert_eq!(got_list_view, &expected_list_view);
13423 }
13424
13425 #[test]
13426 fn test_cast_large_list_to_list_view() {
13427 let list = LargeListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13428 Some(vec![Some(1), Some(2)]),
13429 None,
13430 Some(vec![None, Some(3)]),
13431 ]);
13432 let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Float32, true)));
13433 assert!(can_cast_types(list.data_type(), &target_type));
13434 let cast_result = cast(&list, &target_type).unwrap();
13435
13436 let got_list_view = cast_result.as_list_view::<i32>();
13437 let expected_list_view = ListViewArray::from_iter_primitive::<Float32Type, _, _>(vec![
13438 Some(vec![Some(1.0), Some(2.0)]),
13439 None,
13440 Some(vec![None, Some(3.0)]),
13441 ]);
13442 assert_eq!(got_list_view, &expected_list_view);
13443 }
13444
13445 #[test]
13446 fn test_cast_list_view_to_list_out_of_order() {
13447 let list_view = ListViewArray::new(
13448 Arc::new(Field::new("item", DataType::Int32, true)),
13449 ScalarBuffer::from(vec![0, 6, 3]),
13450 ScalarBuffer::from(vec![3, 3, 3]),
13451 Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5, 6, 7, 8, 9])),
13452 None,
13453 );
13454 let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13455 assert!(can_cast_types(list_view.data_type(), &target_type));
13456 let cast_result = cast(&list_view, &target_type).unwrap();
13457 let got_list = cast_result.as_list::<i32>();
13458 let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13459 Some(vec![Some(1), Some(2), Some(3)]),
13460 Some(vec![Some(7), Some(8), Some(9)]),
13461 Some(vec![Some(4), Some(5), Some(6)]),
13462 ]);
13463 assert_eq!(got_list, &expected_list);
13464 }
13465
13466 #[test]
13467 fn test_cast_list_view_to_list_overlapping() {
13468 let list_view = ListViewArray::new(
13469 Arc::new(Field::new("item", DataType::Int32, true)),
13470 ScalarBuffer::from(vec![0, 0]),
13471 ScalarBuffer::from(vec![1, 2]),
13472 Arc::new(Int32Array::from(vec![1, 2])),
13473 None,
13474 );
13475 let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13476 assert!(can_cast_types(list_view.data_type(), &target_type));
13477 let cast_result = cast(&list_view, &target_type).unwrap();
13478 let got_list = cast_result.as_list::<i32>();
13479 let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13480 Some(vec![Some(1)]),
13481 Some(vec![Some(1), Some(2)]),
13482 ]);
13483 assert_eq!(got_list, &expected_list);
13484 }
13485
13486 #[test]
13487 fn test_cast_list_view_to_list_empty() {
13488 let values: Vec<Option<Vec<Option<i32>>>> = vec![];
13489 let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(values.clone());
13490 let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13491 assert!(can_cast_types(list_view.data_type(), &target_type));
13492 let cast_result = cast(&list_view, &target_type).unwrap();
13493 let got_list = cast_result.as_list::<i32>();
13494 let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(values);
13495 assert_eq!(got_list, &expected_list);
13496 }
13497
13498 #[test]
13499 fn test_cast_list_view_to_list_different_inner_type() {
13500 let values = int32_list_values();
13501 let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(values.clone());
13502 let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int64, true)));
13503 assert!(can_cast_types(list_view.data_type(), &target_type));
13504 let cast_result = cast(&list_view, &target_type).unwrap();
13505 let got_list = cast_result.as_list::<i32>();
13506
13507 let expected_list =
13508 ListArray::from_iter_primitive::<Int64Type, _, _>(values.into_iter().map(|list| {
13509 list.map(|list| {
13510 list.into_iter()
13511 .map(|v| v.map(|v| v as i64))
13512 .collect::<Vec<_>>()
13513 })
13514 }));
13515 assert_eq!(got_list, &expected_list);
13516 }
13517
13518 #[test]
13519 fn test_cast_list_view_to_list_out_of_order_with_nulls() {
13520 let list_view = ListViewArray::new(
13521 Arc::new(Field::new("item", DataType::Int32, true)),
13522 ScalarBuffer::from(vec![0, 6, 3]),
13523 ScalarBuffer::from(vec![3, 3, 3]),
13524 Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5, 6, 7, 8, 9])),
13525 Some(NullBuffer::from(vec![false, true, false])),
13526 );
13527 let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13528 assert!(can_cast_types(list_view.data_type(), &target_type));
13529 let cast_result = cast(&list_view, &target_type).unwrap();
13530 let got_list = cast_result.as_list::<i32>();
13531 let expected_list = ListArray::new(
13532 Arc::new(Field::new("item", DataType::Int32, true)),
13533 OffsetBuffer::from_lengths([3, 3, 3]),
13534 Arc::new(Int32Array::from(vec![1, 2, 3, 7, 8, 9, 4, 5, 6])),
13535 Some(NullBuffer::from(vec![false, true, false])),
13536 );
13537 assert_eq!(got_list, &expected_list);
13538 }
13539
13540 #[test]
13541 fn test_cast_list_view_to_large_list_view() {
13542 let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13543 let target_type =
13544 DataType::LargeListView(Arc::new(Field::new("item", DataType::Int32, true)));
13545 assert!(can_cast_types(list_view.data_type(), &target_type));
13546 let cast_result = cast(&list_view, &target_type).unwrap();
13547 let got = cast_result.as_list_view::<i64>();
13548
13549 let expected =
13550 LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13551 assert_eq!(got, &expected);
13552 }
13553
13554 #[test]
13555 fn test_cast_large_list_view_to_list_view() {
13556 let list_view =
13557 LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13558 let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Int32, true)));
13559 assert!(can_cast_types(list_view.data_type(), &target_type));
13560 let cast_result = cast(&list_view, &target_type).unwrap();
13561 let got = cast_result.as_list_view::<i32>();
13562
13563 let expected = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13564 assert_eq!(got, &expected);
13565 }
13566
13567 #[test]
13568 fn test_cast_time32_second_to_int64() {
13569 let array = Time32SecondArray::from(vec![1000, 2000, 3000]);
13570 let array = Arc::new(array) as Arc<dyn Array>;
13571 let to_type = DataType::Int64;
13572 let cast_options = CastOptions::default();
13573
13574 assert!(can_cast_types(array.data_type(), &to_type));
13575
13576 let result = cast_with_options(&array, &to_type, &cast_options);
13577 assert!(
13578 result.is_ok(),
13579 "Failed to cast Time32(Second) to Int64: {:?}",
13580 result.err()
13581 );
13582
13583 let cast_array = result.unwrap();
13584 let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
13585
13586 assert_eq!(cast_array.value(0), 1000);
13587 assert_eq!(cast_array.value(1), 2000);
13588 assert_eq!(cast_array.value(2), 3000);
13589 }
13590
13591 #[test]
13592 fn test_cast_time32_millisecond_to_int64() {
13593 let array = Time32MillisecondArray::from(vec![1000, 2000, 3000]);
13594 let array = Arc::new(array) as Arc<dyn Array>;
13595 let to_type = DataType::Int64;
13596 let cast_options = CastOptions::default();
13597
13598 assert!(can_cast_types(array.data_type(), &to_type));
13599
13600 let result = cast_with_options(&array, &to_type, &cast_options);
13601 assert!(
13602 result.is_ok(),
13603 "Failed to cast Time32(Millisecond) to Int64: {:?}",
13604 result.err()
13605 );
13606
13607 let cast_array = result.unwrap();
13608 let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
13609
13610 assert_eq!(cast_array.value(0), 1000);
13611 assert_eq!(cast_array.value(1), 2000);
13612 assert_eq!(cast_array.value(2), 3000);
13613 }
13614
13615 #[test]
13616 fn test_cast_time32_millisecond_to_time64_nanosecond() {
13617 let array =
13618 Time32MillisecondArray::from(vec![Some(1_000), Some(2_000), None, Some(43_200_000)]);
13619 let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
13620 let c = b.as_primitive::<Time64NanosecondType>();
13621 assert_eq!(c.value(0), 1_000_000_000);
13622 assert_eq!(c.value(1), 2_000_000_000);
13623 assert!(c.is_null(2));
13624 assert_eq!(c.value(3), 43_200_000_000_000);
13625 }
13626
13627 #[test]
13628 fn test_cast_time32_millisecond_to_time64_microsecond() {
13629 let array =
13630 Time32MillisecondArray::from(vec![Some(1_000), Some(2_000), None, Some(43_200_000)]);
13631 let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
13632 let c = b.as_primitive::<Time64MicrosecondType>();
13633 assert_eq!(c.value(0), 1_000_000);
13634 assert_eq!(c.value(1), 2_000_000);
13635 assert!(c.is_null(2));
13636 assert_eq!(c.value(3), 43_200_000_000);
13637 }
13638
13639 #[test]
13640 fn test_cast_time32_second_to_time64_nanosecond() {
13641 let array = Time32SecondArray::from(vec![Some(1), Some(60), None, Some(43_200)]);
13642 let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
13643 let c = b.as_primitive::<Time64NanosecondType>();
13644 assert_eq!(c.value(0), 1_000_000_000);
13645 assert_eq!(c.value(1), 60_000_000_000);
13646 assert!(c.is_null(2));
13647 assert_eq!(c.value(3), 43_200_000_000_000);
13648 }
13649
13650 #[test]
13651 fn test_cast_time32_second_to_time64_microsecond() {
13652 let array = Time32SecondArray::from(vec![Some(1), Some(60), None, Some(43_200)]);
13653 let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
13654 let c = b.as_primitive::<Time64MicrosecondType>();
13655 assert_eq!(c.value(0), 1_000_000);
13656 assert_eq!(c.value(1), 60_000_000);
13657 assert!(c.is_null(2));
13658 assert_eq!(c.value(3), 43_200_000_000);
13659 }
13660
13661 #[test]
13662 fn test_cast_string_to_time32_second_to_int64() {
13663 let array = StringArray::from(vec!["03:12:44"]);
13666 let array = Arc::new(array) as Arc<dyn Array>;
13667 let cast_options = CastOptions::default();
13668
13669 let time32_type = DataType::Time32(TimeUnit::Second);
13671 let time32_array = cast_with_options(&array, &time32_type, &cast_options).unwrap();
13672
13673 let int64_type = DataType::Int64;
13675 assert!(can_cast_types(time32_array.data_type(), &int64_type));
13676
13677 let result = cast_with_options(&time32_array, &int64_type, &cast_options);
13678
13679 assert!(
13680 result.is_ok(),
13681 "Failed to cast Time32(Second) to Int64: {:?}",
13682 result.err()
13683 );
13684
13685 let cast_array = result.unwrap();
13686 let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
13687
13688 assert_eq!(cast_array.value(0), 11564);
13690 }
13691 #[test]
13692 fn test_string_dicts_to_binary_view() {
13693 let expected = BinaryViewArray::from_iter(vec![
13694 VIEW_TEST_DATA[1],
13695 VIEW_TEST_DATA[0],
13696 None,
13697 VIEW_TEST_DATA[3],
13698 None,
13699 VIEW_TEST_DATA[1],
13700 VIEW_TEST_DATA[4],
13701 ]);
13702
13703 let values_arrays: [ArrayRef; _] = [
13704 Arc::new(StringArray::from_iter(VIEW_TEST_DATA)),
13705 Arc::new(StringViewArray::from_iter(VIEW_TEST_DATA)),
13706 Arc::new(LargeStringArray::from_iter(VIEW_TEST_DATA)),
13707 ];
13708 for values in values_arrays {
13709 let keys =
13710 Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
13711 let string_dict_array = DictionaryArray::<Int8Type>::try_new(keys, values).unwrap();
13712
13713 let casted = cast(&string_dict_array, &DataType::BinaryView).unwrap();
13714 assert_eq!(casted.as_ref(), &expected);
13715 }
13716 }
13717
13718 #[test]
13719 fn test_binary_dicts_to_string_view() {
13720 let expected = StringViewArray::from_iter(vec![
13721 VIEW_TEST_DATA[1],
13722 VIEW_TEST_DATA[0],
13723 None,
13724 VIEW_TEST_DATA[3],
13725 None,
13726 VIEW_TEST_DATA[1],
13727 VIEW_TEST_DATA[4],
13728 ]);
13729
13730 let values_arrays: [ArrayRef; _] = [
13731 Arc::new(BinaryArray::from_iter(VIEW_TEST_DATA)),
13732 Arc::new(BinaryViewArray::from_iter(VIEW_TEST_DATA)),
13733 Arc::new(LargeBinaryArray::from_iter(VIEW_TEST_DATA)),
13734 ];
13735 for values in values_arrays {
13736 let keys =
13737 Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
13738 let string_dict_array = DictionaryArray::<Int8Type>::try_new(keys, values).unwrap();
13739
13740 let casted = cast(&string_dict_array, &DataType::Utf8View).unwrap();
13741 assert_eq!(casted.as_ref(), &expected);
13742 }
13743 }
13744
13745 #[test]
13746 fn test_cast_between_sliced_run_end_encoded() {
13747 let run_ends = Int16Array::from(vec![2, 5, 8]);
13748 let values = StringArray::from(vec!["a", "b", "c"]);
13749
13750 let ree_array = RunArray::<Int16Type>::try_new(&run_ends, &values).unwrap();
13751 let ree_array = ree_array.slice(1, 2);
13752 let array_ref = Arc::new(ree_array) as ArrayRef;
13753
13754 let target_type = DataType::RunEndEncoded(
13755 Arc::new(Field::new("run_ends", DataType::Int64, false)),
13756 Arc::new(Field::new("values", DataType::Utf8, true)),
13757 );
13758 let cast_options = CastOptions {
13759 safe: false,
13760 format_options: FormatOptions::default(),
13761 };
13762
13763 let result = cast_with_options(&array_ref, &target_type, &cast_options).unwrap();
13764 let run_array = result.as_run::<Int64Type>();
13765 let run_array = run_array.downcast::<StringArray>().unwrap();
13766
13767 let expected = vec!["a", "b"];
13768 let actual = run_array.into_iter().flatten().collect::<Vec<_>>();
13769
13770 assert_eq!(expected, actual);
13771 }
13772}