Runs try_cast_literal_to_type with the specified inputs and ensure it computes the expected output, and ensures the casting is consistent with the Arrow kernels
(
literal: ScalarValue,
target_type: DataType,
expected_result: ExpectedCast,
)
| 430 | /// ensure it computes the expected output, and ensures the |
| 431 | /// casting is consistent with the Arrow kernels |
| 432 | fn expect_cast( |
| 433 | literal: ScalarValue, |
| 434 | target_type: DataType, |
| 435 | expected_result: ExpectedCast, |
| 436 | ) { |
| 437 | let actual_value = try_cast_literal_to_type(&literal, &target_type); |
| 438 | |
| 439 | println!("expect_cast: "); |
| 440 | println!(" {literal:?} --> {target_type}"); |
| 441 | println!(" expected_result: {expected_result:?}"); |
| 442 | println!(" actual_result: {actual_value:?}"); |
| 443 | |
| 444 | match expected_result { |
| 445 | ExpectedCast::Value(expected_value) => { |
| 446 | let actual_value = |
| 447 | actual_value.expect("Expected cast value but got None"); |
| 448 | |
| 449 | assert_eq!(actual_value, expected_value); |
| 450 | |
| 451 | // Verify that calling the arrow |
| 452 | // cast kernel yields the same results |
| 453 | // input array |
| 454 | let literal_array = literal |
| 455 | .to_array_of_size(1) |
| 456 | .expect("Failed to convert to array of size"); |
| 457 | let expected_array = expected_value |
| 458 | .to_array_of_size(1) |
| 459 | .expect("Failed to convert to array of size"); |
| 460 | let cast_array = cast_with_options( |
| 461 | &literal_array, |
| 462 | &target_type, |
| 463 | &CastOptions::default(), |
| 464 | ) |
| 465 | .expect("Expected to be cast array with arrow cast kernel"); |
| 466 | |
| 467 | assert_eq!( |
| 468 | &expected_array, &cast_array, |
| 469 | "Result of casting {literal:?} with arrow was\n {cast_array:#?}\nbut expected\n{expected_array:#?}" |
| 470 | ); |
| 471 | |
| 472 | // Verify that for timestamp types the timezones are the same |
| 473 | // (ScalarValue::cmp doesn't account for timezones); |
| 474 | if let ( |
| 475 | DataType::Timestamp(left_unit, left_tz), |
| 476 | DataType::Timestamp(right_unit, right_tz), |
| 477 | ) = (actual_value.data_type(), expected_value.data_type()) |
| 478 | { |
| 479 | assert_eq!(left_unit, right_unit); |
| 480 | assert_eq!(left_tz, right_tz); |
| 481 | } |
| 482 | } |
| 483 | ExpectedCast::NoValue => { |
| 484 | assert!( |
| 485 | actual_value.is_none(), |
| 486 | "Expected no cast value, but got {actual_value:?}" |
| 487 | ); |
| 488 | } |
| 489 | } |
searching dependent graphs…