From 0e05ba18fe495659fec9bf5d139f225b5de6af70 Mon Sep 17 00:00:00 2001 From: Andrew Lamb Date: Thu, 30 Jun 2022 07:31:03 -0400 Subject: [PATCH] Fix nullability calculation for `CASE` expressions (#2814) * Fix nullability calculation for CASE statements * Remove println --- datafusion/core/src/logical_plan/expr_schema.rs | 4 +++- datafusion/physical-expr/src/expressions/case.rs | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/datafusion/core/src/logical_plan/expr_schema.rs b/datafusion/core/src/logical_plan/expr_schema.rs index 1a8adbf0426..da0b6f7eabc 100644 --- a/datafusion/core/src/logical_plan/expr_schema.rs +++ b/datafusion/core/src/logical_plan/expr_schema.rs @@ -172,7 +172,9 @@ impl ExprSchemable for Expr { } else if let Some(e) = else_expr { e.nullable(input_schema) } else { - Ok(false) + // CASE produces NULL if there is no `else` expr + // (aka when none of the `when_then_exprs` match) + Ok(true) } } Expr::Cast { expr, .. } => expr.nullable(input_schema), diff --git a/datafusion/physical-expr/src/expressions/case.rs b/datafusion/physical-expr/src/expressions/case.rs index c0b3aa4b588..0595b30db86 100644 --- a/datafusion/physical-expr/src/expressions/case.rs +++ b/datafusion/physical-expr/src/expressions/case.rs @@ -406,7 +406,9 @@ impl PhysicalExpr for CaseExpr { } else if let Some(e) = &self.else_expr { e.nullable(input_schema) } else { - Ok(false) + // CASE produces NULL if there is no `else` expr + // (aka when none of the `when_then_exprs` match) + Ok(true) } }