Apache DataFusion SQL Query Engine
Support Type Coercion for NULL in Binary Arithmetic Expressions (#16761)
kosiew86744547faff
diff --git a/datafusion/expr-common/src/type_coercion/binary.rs b/datafusion/expr-common/src/type_coercion/binary.rsindex 098a930842..061c01236c 100644--- a/datafusion/expr-common/src/type_coercion/binary.rs+++ b/datafusion/expr-common/src/type_coercion/binary.rs@@ -250,6 +250,18 @@ impl<'a> BinaryTypeCoercer<'a> {} else if let Some(numeric) = mathematics_numerical_coercion(self.lhs, self.rhs) {// Numeric arithmetic, e.g. Int32 + Int32Ok(Signature::uniform(numeric))+ } else if let Some(coerced) = null_coercion(self.lhs, self.rhs) {+ // One side is NULL, cast it to the other's type+ let ret = get_result(&coerced, &coerced).map_err(|e| {+ plan_datafusion_err!(+ "Cannot get result type for null arithmetic {coerced} {} {coerced}: {e}", self.op+ )+ })?;+ Ok(Signature {+ lhs: coerced.clone(),+ rhs: coerced,+ ret,+ })} else {plan_err!("Cannot coerce arithmetic expression {} {} {} to valid types", self.lhs, self.op, self.rhsdiff --git a/datafusion/sqllogictest/test_files/dates.slt b/datafusion/sqllogictest/test_files/dates.sltindex 148f0dfe64..2e91a0363d 100644--- a/datafusion/sqllogictest/test_files/dates.slt+++ b/datafusion/sqllogictest/test_files/dates.slt@@ -108,6 +108,17 @@ SELECT '2023-01-01T00:00:00'::timestamp - DATE '2021-01-01';----730 days 0 hours 0 mins 0.000000000 secs+# NULL with DATE arithmetic should yield NULL+query ?+SELECT NULL - DATE '1984-02-28';+----+NULL++query ?+SELECT DATE '1984-02-28' - NULL+----+NULL+# to_date_teststatement okcreate table to_date_t1(ts bigint) as VALUES