liukun4515 commented on code in PR #4147:
URL: https://github.com/apache/arrow-datafusion/pull/4147#discussion_r1017840904
##########
datafusion/optimizer/src/unwrap_cast_in_comparison.rs:
##########
@@ -653,4 +655,196 @@ mod tests {
fn null_decimal(precision: u8, scale: u8) -> Expr {
lit(ScalarValue::Decimal128(None, precision, scale))
}
+
+ #[test]
+ fn test_try_cast_to_type_nulls() {
+ // test values that can be cast to/from all integer types
+ let scalars = vec![
+ ScalarValue::Int8(None),
+ ScalarValue::Int16(None),
+ ScalarValue::Int32(None),
+ ScalarValue::Int64(None),
+ ScalarValue::Decimal128(None, 3, 0),
+ ScalarValue::Decimal128(None, 8, 2),
+ ];
+
+ for s1 in &scalars {
+ for s2 in &scalars {
+ expect_cast(
+ s1.clone(),
+ s2.get_datatype(),
+ ExpectedCast::Value(s2.clone()),
+ );
+ }
+ }
+ }
+
+ #[test]
+ fn test_try_cast_to_type_int_in_range() {
+ // test values that can be cast to/from all integer types
+ let scalars = vec![
+ ScalarValue::Int8(Some(123)),
+ ScalarValue::Int16(Some(123)),
+ ScalarValue::Int32(Some(123)),
+ ScalarValue::Int64(Some(123)),
+ ScalarValue::Decimal128(Some(123), 3, 0),
+ ScalarValue::Decimal128(Some(12300), 8, 2),
+ ];
+
+ for s1 in &scalars {
+ for s2 in &scalars {
+ expect_cast(
+ s1.clone(),
+ s2.get_datatype(),
+ ExpectedCast::Value(s2.clone()),
+ );
+ }
+ }
+ }
+
+ #[test]
+ fn test_try_cast_to_type_int_out_of_range() {
+ let max_i64 = ScalarValue::Int64(Some(i64::MAX));
+ let max_u64 = ScalarValue::UInt64(Some(u64::MAX));
+ expect_cast(max_i64.clone(), DataType::Int8, ExpectedCast::NoValue);
+
+ expect_cast(max_i64.clone(), DataType::Int16, ExpectedCast::NoValue);
+
+ expect_cast(max_i64, DataType::Int32, ExpectedCast::NoValue);
+
+ expect_cast(max_u64, DataType::Int64, ExpectedCast::NoValue);
+
+ // decimal out of range
+ expect_cast(
+
ScalarValue::Decimal128(Some(99999999999999999999999999999999999900), 38, 0),
+ DataType::Int64,
+ ExpectedCast::NoValue,
+ );
+
+ expect_cast(
+ ScalarValue::Decimal128(Some(-9999999999999999999999999999999999),
37, 1),
+ DataType::Int64,
+ ExpectedCast::NoValue,
+ );
+ }
+
+ #[test]
+ fn test_try_decimal_cast_in_range() {
+ expect_cast(
+ ScalarValue::Decimal128(Some(12300), 5, 2),
+ DataType::Decimal128(3, 0),
+ ExpectedCast::Value(ScalarValue::Decimal128(Some(123), 3, 0)),
+ );
+
+ expect_cast(
+ ScalarValue::Decimal128(Some(12300), 5, 2),
+ DataType::Decimal128(8, 0),
+ ExpectedCast::Value(ScalarValue::Decimal128(Some(123), 8, 0)),
+ );
+
+ expect_cast(
+ ScalarValue::Decimal128(Some(12300), 5, 2),
+ DataType::Decimal128(8, 5),
+ ExpectedCast::Value(ScalarValue::Decimal128(Some(12300000), 8, 5)),
+ );
+ }
+
+ #[test]
+ fn test_try_decimal_cast_out_of_range() {
Review Comment:
maybe I have tested unsupported cases in
`test_not_unwrap_cast_with_decimal_comparison`, but it's good to simplify the
test and add more test.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]