Skip to content

Commit bc58903

Browse files
committed
Allow comparison between boolean and int values
1 parent c4b9995 commit bc58903

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

  • datafusion/expr-common/src/type_coercion

datafusion/expr-common/src/type_coercion/binary.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,7 @@ pub fn comparison_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option<D
777777
.or_else(|| binary_coercion(lhs_type, rhs_type))
778778
.or_else(|| struct_coercion(lhs_type, rhs_type))
779779
.or_else(|| map_coercion(lhs_type, rhs_type))
780+
.or_else(|| boolean_coercion(lhs_type, rhs_type))
780781
}
781782

782783
/// Similar to [`comparison_coercion`] but prefers numeric if compares with
@@ -1052,6 +1053,20 @@ fn map_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option<DataType> {
10521053
}
10531054
}
10541055

1056+
/// Coercion rules for boolean types: If at least one argument is
1057+
/// a boolean type and both arguments can be coerced into a boolean type, coerce
1058+
/// to boolean type.
1059+
fn boolean_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option<DataType> {
1060+
use arrow::datatypes::DataType::*;
1061+
match (lhs_type, rhs_type) {
1062+
(Boolean, Int8 | Int16 | Int32 | Int64 | UInt8 | UInt16 | UInt32 | UInt64)
1063+
| (Int8 | Int16 | Int32 | Int64 | UInt8 | UInt16 | UInt32 | UInt64, Boolean) => {
1064+
Some(Boolean)
1065+
}
1066+
_ => None,
1067+
}
1068+
}
1069+
10551070
/// Returns the output type of applying mathematics operations such as
10561071
/// `+` to arguments of `lhs_type` and `rhs_type`.
10571072
fn mathematics_numerical_coercion(
@@ -2510,6 +2525,32 @@ mod tests {
25102525
DataType::List(Arc::clone(&inner_field))
25112526
);
25122527

2528+
// boolean
2529+
let int_types = vec![
2530+
DataType::Int8,
2531+
DataType::Int16,
2532+
DataType::Int32,
2533+
DataType::Int64,
2534+
DataType::UInt8,
2535+
DataType::UInt16,
2536+
DataType::UInt32,
2537+
DataType::UInt64,
2538+
];
2539+
for int_type in int_types {
2540+
test_coercion_binary_rule!(
2541+
DataType::Boolean,
2542+
DataType::Int8,
2543+
Operator::Eq,
2544+
DataType::Boolean
2545+
);
2546+
test_coercion_binary_rule!(
2547+
int_type,
2548+
DataType::Boolean,
2549+
Operator::Eq,
2550+
DataType::Boolean
2551+
);
2552+
}
2553+
25132554
// Negative test: inner_timestamp_field and inner_field are not compatible because their inner types are not compatible
25142555
let inner_timestamp_field = Arc::new(Field::new_list_field(
25152556
DataType::Timestamp(TimeUnit::Microsecond, None),

0 commit comments

Comments
 (0)