From 1cd2c9e1c60851abadd5876f6fe27a8e2638a784 Mon Sep 17 00:00:00 2001 From: yangjiang Date: Wed, 16 Nov 2022 21:03:06 +0800 Subject: [PATCH 1/2] Support binary boolean operators with nulls Signed-off-by: yangjiang --- datafusion/expr/src/type_coercion/binary.rs | 30 ++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/datafusion/expr/src/type_coercion/binary.rs b/datafusion/expr/src/type_coercion/binary.rs index 45510cb03e0ed..4c69271f6855a 100644 --- a/datafusion/expr/src/type_coercion/binary.rs +++ b/datafusion/expr/src/type_coercion/binary.rs @@ -98,8 +98,12 @@ pub fn coerce_types( | Operator::BitwiseShiftRight | Operator::BitwiseShiftLeft => bitwise_coercion(lhs_type, rhs_type), Operator::And | Operator::Or => match (lhs_type, rhs_type) { - // logical binary boolean operators can only be evaluated in bools + // logical binary boolean operators can only be evaluated in bools or nulls (DataType::Boolean, DataType::Boolean) => Some(DataType::Boolean), + (DataType::Null, DataType::Null) => Some(DataType::Null), + (DataType::Boolean, DataType::Null) | (DataType::Null, DataType::Boolean) => { + Some(DataType::Boolean) + } _ => None, }, // logical comparison operators have their own rules, and always return a boolean @@ -1022,6 +1026,30 @@ mod tests { Operator::Or, DataType::Boolean ); + test_coercion_binary_rule!( + DataType::Boolean, + DataType::Null, + Operator::And, + DataType::Boolean + ); + test_coercion_binary_rule!( + DataType::Boolean, + DataType::Null, + Operator::Or, + DataType::Boolean + ); + test_coercion_binary_rule!( + DataType::Null, + DataType::Null, + Operator::Or, + DataType::Null + ); + test_coercion_binary_rule!( + DataType::Null, + DataType::Null, + Operator::And, + DataType::Null + ); Ok(()) } } From 58520a9edefe8a017cba02798d28dbb9af26c068 Mon Sep 17 00:00:00 2001 From: yangjiang Date: Wed, 16 Nov 2022 22:47:00 +0800 Subject: [PATCH 2/2] add ut Signed-off-by: yangjiang --- datafusion/expr/src/type_coercion/binary.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/datafusion/expr/src/type_coercion/binary.rs b/datafusion/expr/src/type_coercion/binary.rs index 4c69271f6855a..911874fb09ba2 100644 --- a/datafusion/expr/src/type_coercion/binary.rs +++ b/datafusion/expr/src/type_coercion/binary.rs @@ -1050,6 +1050,18 @@ mod tests { Operator::And, DataType::Null ); + test_coercion_binary_rule!( + DataType::Null, + DataType::Boolean, + Operator::And, + DataType::Boolean + ); + test_coercion_binary_rule!( + DataType::Null, + DataType::Boolean, + Operator::Or, + DataType::Boolean + ); Ok(()) } }