Skip to content

Commit

Permalink
support unint <-> decimal (#4634)
Browse files Browse the repository at this point in the history
  • Loading branch information
liukun4515 committed Dec 15, 2022
1 parent c132eca commit 5727ef3
Showing 1 changed file with 2 additions and 34 deletions.
36 changes: 2 additions & 34 deletions datafusion/optimizer/src/unwrap_cast_in_comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,25 +299,6 @@ fn is_support_data_type(data_type: &DataType) -> bool {
)
}

fn is_decimal_type(dt: &DataType) -> bool {
matches!(dt, DataType::Decimal128(_, _))
}

fn is_unsigned_type(dt: &DataType) -> bool {
matches!(
dt,
DataType::UInt8 | DataType::UInt16 | DataType::UInt32 | DataType::UInt64
)
}

/// Until https://github.com/apache/arrow-rs/issues/1043 is done
/// (support for unsigned <--> decimal casts) we also don't do that
/// kind of cast in this optimizer
fn is_unsupported_cast(dt1: &DataType, dt2: &DataType) -> bool {
(is_decimal_type(dt1) && is_unsigned_type(dt2))
|| (is_decimal_type(dt2) && is_unsigned_type(dt1))
}

fn try_cast_literal_to_type(
lit_value: &ScalarValue,
target_type: &DataType,
Expand All @@ -327,9 +308,6 @@ fn try_cast_literal_to_type(
if !is_support_data_type(&lit_data_type) || !is_support_data_type(target_type) {
return Ok(None);
}
if is_unsupported_cast(&lit_data_type, target_type) {
return Ok(None);
}
if lit_value.is_null() {
// null value can be cast to any type of null value
return Ok(Some(ScalarValue::try_from(target_type)?));
Expand Down Expand Up @@ -800,12 +778,7 @@ mod tests {

for s1 in &scalars {
for s2 in &scalars {
let expected_value =
if is_unsupported_cast(&s1.get_datatype(), &s2.get_datatype()) {
ExpectedCast::NoValue
} else {
ExpectedCast::Value(s2.clone())
};
let expected_value = ExpectedCast::Value(s2.clone());

expect_cast(s1.clone(), s2.get_datatype(), expected_value);
}
Expand All @@ -830,12 +803,7 @@ mod tests {

for s1 in &scalars {
for s2 in &scalars {
let expected_value =
if is_unsupported_cast(&s1.get_datatype(), &s2.get_datatype()) {
ExpectedCast::NoValue
} else {
ExpectedCast::Value(s2.clone())
};
let expected_value = ExpectedCast::Value(s2.clone());

expect_cast(s1.clone(), s2.get_datatype(), expected_value);
}
Expand Down

0 comments on commit 5727ef3

Please sign in to comment.