Skip to content

Apache DataFusion SQL Query Engine

Fix nullability calculation for `CASE` expressions (#2814)

Andrew Lamb379f42ca444d
diff --git a/datafusion/expr/src/expr_schema.rs b/datafusion/expr/src/expr_schema.rs
index 2433024e38..bbb414655c 100644
--- a/datafusion/expr/src/expr_schema.rs
+++ b/datafusion/expr/src/expr_schema.rs
@@ -170,7 +170,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 aa6d05ab97..4c62c1d575 100644
--- a/datafusion/physical-expr/src/expressions/case.rs
+++ b/datafusion/physical-expr/src/expressions/case.rs
@@ -269,7 +269,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)
}
}