@@ -19,7 +19,9 @@ mod literal_lookup_table;
1919
2020use super :: { Column , Literal } ;
2121use crate :: PhysicalExpr ;
22- use crate :: expressions:: { LambdaVariable , lit, try_cast} ;
22+ use crate :: expressions:: {
23+ CastExpr , LambdaVariable , NegativeExpr , NotExpr , lit, try_cast,
24+ } ;
2325use arrow:: array:: * ;
2426use arrow:: compute:: kernels:: zip:: zip;
2527use arrow:: compute:: {
@@ -1278,7 +1280,11 @@ impl PhysicalExpr for CaseExpr {
12781280 // it would evaluate to null.
12791281
12801282 // Replace the `then` expression with `NULL` in the `when` expression
1281- let with_null = match replace_with_null ( w, t. as_ref ( ) , input_schema) {
1283+ let with_null = match replace_with_null (
1284+ w,
1285+ unwrap_certainly_null_expr ( t. as_ref ( ) ) ,
1286+ input_schema,
1287+ ) {
12821288 Err ( e) => return Some ( Err ( e) ) ,
12831289 Ok ( e) => e,
12841290 } ;
@@ -1537,6 +1543,25 @@ fn replace_with_null(
15371543 Ok ( with_null)
15381544}
15391545
1546+ /// Returns the innermost [`PhysicalExpr`] that is provably null if `expr` is null.
1547+ ///
1548+ /// Keep this in sync with the logical-plan equivalent, `unwrap_certainly_null_expr`
1549+ /// in `datafusion/expr/src/expr_schema.rs`. If the two disagree on which wrappers
1550+ /// are null-preserving, `CASE` nullability computed by the logical and physical
1551+ /// planners can diverge and cause a schema mismatch during planning.
1552+ /// See <https://github.com/apache/datafusion/pull/23844> for rationale.
1553+ fn unwrap_certainly_null_expr ( expr : & dyn PhysicalExpr ) -> & dyn PhysicalExpr {
1554+ if let Some ( expr) = expr. downcast_ref :: < NotExpr > ( ) {
1555+ unwrap_certainly_null_expr ( expr. arg ( ) . as_ref ( ) )
1556+ } else if let Some ( expr) = expr. downcast_ref :: < NegativeExpr > ( ) {
1557+ unwrap_certainly_null_expr ( expr. arg ( ) . as_ref ( ) )
1558+ } else if let Some ( expr) = expr. downcast_ref :: < CastExpr > ( ) {
1559+ unwrap_certainly_null_expr ( expr. expr . as_ref ( ) )
1560+ } else {
1561+ expr
1562+ }
1563+ }
1564+
15401565/// Create a CASE expression
15411566pub fn case (
15421567 expr : Option < Arc < dyn PhysicalExpr > > ,
@@ -2577,10 +2602,45 @@ mod tests {
25772602 let zero = lit ( 0 ) ;
25782603 let foo_eq_zero =
25792604 binary ( Arc :: clone ( & foo) , Operator :: Eq , Arc :: clone ( & zero) , & schema) ?;
2605+ let cast_foo = cast ( Arc :: clone ( & foo) , & schema, DataType :: Int64 ) ?;
2606+ let negative_foo = expressions:: negative ( Arc :: clone ( & foo) , & schema) ?;
25802607
25812608 assert_not_nullable ( when_then_else ( & foo_is_not_null, & foo, & zero) ?, & schema) ;
25822609 assert_not_nullable ( when_then_else ( & not_foo_is_null, & foo, & zero) ?, & schema) ;
25832610 assert_not_nullable ( when_then_else ( & foo_eq_zero, & foo, & zero) ?, & schema) ;
2611+ assert_not_nullable (
2612+ when_then_else ( & foo_is_not_null, & cast_foo, & lit ( 0i64 ) ) ?,
2613+ & schema,
2614+ ) ;
2615+ assert_not_nullable (
2616+ when_then_else ( & foo_is_not_null, & negative_foo, & zero) ?,
2617+ & schema,
2618+ ) ;
2619+
2620+ // Nested null-preserving wrappers must be unwrapped recursively. `CAST(-foo)`
2621+ // still collapses `foo IS NOT NULL` to `false`, so the branch is
2622+ // unreachable-as-null and the `CASE` is not nullable.
2623+ let cast_negative_foo = cast (
2624+ expressions:: negative ( Arc :: clone ( & foo) , & schema) ?,
2625+ & schema,
2626+ DataType :: Int64 ,
2627+ ) ?;
2628+ assert_not_nullable (
2629+ when_then_else ( & foo_is_not_null, & cast_negative_foo, & lit ( 0i64 ) ) ?,
2630+ & schema,
2631+ ) ;
2632+
2633+ // `TRY_CAST` is intentionally NOT treated as null-preserving: it yields
2634+ // NULL on a failed cast even for a non-null input, so a guarded `TRY_CAST`
2635+ // branch is still reachable-as-null and the `CASE` stays nullable. This must
2636+ // stay consistent with the logical planner (`unwrap_certainly_null_expr` in
2637+ // `datafusion/expr/src/expr_schema.rs`); unwrapping it on only one side would
2638+ // reintroduce a logical/physical schema mismatch.
2639+ let try_cast_foo = try_cast ( Arc :: clone ( & foo) , & schema, DataType :: Int64 ) ?;
2640+ assert_nullable (
2641+ when_then_else ( & foo_is_not_null, & try_cast_foo, & lit ( 0i64 ) ) ?,
2642+ & schema,
2643+ ) ;
25842644
25852645 assert_not_nullable (
25862646 when_then_else (
@@ -2702,6 +2762,23 @@ mod tests {
27022762 & schema,
27032763 ) ;
27042764
2765+ let boolean_schema =
2766+ Schema :: new ( vec ! [ Field :: new( "predicate" , DataType :: Boolean , true ) ] ) ;
2767+ let predicate = col ( "predicate" , & boolean_schema) ?;
2768+ let predicate_is_not_null = is_not_null ( Arc :: clone ( & predicate) ) ?;
2769+ let not_predicate = expressions:: not ( Arc :: clone ( & predicate) ) ?;
2770+ assert_not_nullable (
2771+ when_then_else ( & predicate_is_not_null, & not_predicate, & lit ( false ) ) ?,
2772+ & boolean_schema,
2773+ ) ;
2774+
2775+ // Nested `NOT` is likewise unwrapped recursively.
2776+ let not_not_predicate = expressions:: not ( Arc :: clone ( & not_predicate) ) ?;
2777+ assert_not_nullable (
2778+ when_then_else ( & predicate_is_not_null, & not_not_predicate, & lit ( false ) ) ?,
2779+ & boolean_schema,
2780+ ) ;
2781+
27052782 Ok ( ( ) )
27062783 }
27072784
0 commit comments