Skip to content

Commit 7b44028

Browse files
committed
Allow to compare bool and int
1 parent e524173 commit 7b44028

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

datafusion/src/physical_plan/expressions/binary.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ use crate::scalar::ScalarValue;
5050

5151
use super::coercion::{eq_coercion, numerical_coercion, order_coercion, string_coercion};
5252
use crate::physical_plan::expressions::coercion::{is_numeric, string_implicit_cast};
53+
use arrow::compute::{eq_bool, neq_bool};
5354

5455
/// Binary expression
5556
#[derive(Debug)]
@@ -659,6 +660,28 @@ impl PhysicalExpr for BinaryExpr {
659660
right_value.into_array(batch.num_rows()),
660661
);
661662

663+
// Handle boolean comparisons.
664+
if left.data_type() == &DataType::Boolean {
665+
let boolean_eq = |is_eq: bool| {
666+
let left = left.as_any().downcast_ref::<BooleanArray>().unwrap();
667+
let right = right.as_any().downcast_ref::<BooleanArray>().unwrap();
668+
if is_eq {
669+
eq_bool(left, right)
670+
} else {
671+
neq_bool(left, right)
672+
}
673+
};
674+
match self.op {
675+
Operator::Eq => {
676+
return Ok(ColumnarValue::Array(Arc::new(boolean_eq(true)?)))
677+
}
678+
Operator::NotEq => {
679+
return Ok(ColumnarValue::Array(Arc::new(boolean_eq(false)?)))
680+
}
681+
_ => {}
682+
}
683+
}
684+
662685
let result: Result<ArrayRef> = match &self.op {
663686
Operator::Like => binary_string_array_op!(left, right, like),
664687
Operator::NotLike => binary_string_array_op!(left, right, nlike),

datafusion/src/physical_plan/expressions/coercion.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,20 @@ pub fn eq_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option<DataType>
241241
return Some(lhs_type.clone());
242242
}
243243
numerical_coercion(lhs_type, rhs_type)
244+
.or_else(|| eq_bool_coercion(lhs_type, rhs_type))
244245
.or_else(|| dictionary_coercion(lhs_type, rhs_type))
245246
.or_else(|| temporal_coercion(lhs_type, rhs_type))
246247
.or_else(|| string_implicit_cast(lhs_type, rhs_type))
247248
}
248249

250+
fn eq_bool_coercion(l: &DataType, r: &DataType) -> Option<DataType> {
251+
if l == &DataType::Boolean || r == &DataType::Boolean {
252+
Some(DataType::Boolean)
253+
} else {
254+
None
255+
}
256+
}
257+
249258
// coercion rules that assume an ordered set, such as "less than".
250259
// These are the union of all numerical coercion rules and all string coercion rules
251260
pub fn order_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option<DataType> {

0 commit comments

Comments
 (0)