Skip to content

Commit 8674454

Browse files
authored
Support Type Coercion for NULL in Binary Arithmetic Expressions (#16761)
Added logic in BinaryTypeCoercer to handle NULL in binary arithmetic expressions by coercing it to the non-null side's type. Updated signature resolution to include this new coercion case. Added SQL logic tests verifying expected behavior for NULL arithmetic with DATE values.
1 parent 14df97b commit 8674454

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,18 @@ impl<'a> BinaryTypeCoercer<'a> {
250250
} else if let Some(numeric) = mathematics_numerical_coercion(self.lhs, self.rhs) {
251251
// Numeric arithmetic, e.g. Int32 + Int32
252252
Ok(Signature::uniform(numeric))
253+
} else if let Some(coerced) = null_coercion(self.lhs, self.rhs) {
254+
// One side is NULL, cast it to the other's type
255+
let ret = get_result(&coerced, &coerced).map_err(|e| {
256+
plan_datafusion_err!(
257+
"Cannot get result type for null arithmetic {coerced} {} {coerced}: {e}", self.op
258+
)
259+
})?;
260+
Ok(Signature {
261+
lhs: coerced.clone(),
262+
rhs: coerced,
263+
ret,
264+
})
253265
} else {
254266
plan_err!(
255267
"Cannot coerce arithmetic expression {} {} {} to valid types", self.lhs, self.op, self.rhs

datafusion/sqllogictest/test_files/dates.slt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,17 @@ SELECT '2023-01-01T00:00:00'::timestamp - DATE '2021-01-01';
108108
----
109109
730 days 0 hours 0 mins 0.000000000 secs
110110

111+
# NULL with DATE arithmetic should yield NULL
112+
query ?
113+
SELECT NULL - DATE '1984-02-28';
114+
----
115+
NULL
116+
117+
query ?
118+
SELECT DATE '1984-02-28' - NULL
119+
----
120+
NULL
121+
111122
# to_date_test
112123
statement ok
113124
create table to_date_t1(ts bigint) as VALUES

0 commit comments

Comments
 (0)