pepijnve commented on code in PR #17813:
URL: https://github.com/apache/datafusion/pull/17813#discussion_r2388499076
##########
datafusion/expr/src/expr_schema.rs:
##########
@@ -830,6 +883,150 @@ mod tests {
assert!(expr.nullable(&get_schema(false)).unwrap());
}
+ fn check_nullability(
+ expr: Expr,
+ nullable: bool,
+ get_schema: fn(bool) -> MockExprSchema,
+ ) -> Result<()> {
+ assert_eq!(
+ expr.nullable(&get_schema(true))?,
+ nullable,
+ "Nullability of '{expr}' should be {nullable} when column is
nullable"
+ );
+ assert!(
+ !expr.nullable(&get_schema(false))?,
+ "Nullability of '{expr}' should be false when column is not
nullable"
+ );
+ Ok(())
+ }
+
+ #[test]
+ fn test_case_expression_nullability() -> Result<()> {
+ let get_schema = |nullable| {
+ MockExprSchema::new()
+ .with_data_type(DataType::Int32)
+ .with_nullable(nullable)
+ };
+
+ check_nullability(
+ when(is_not_null(col("foo")), col("foo")).otherwise(lit(0))?,
+ false,
+ get_schema,
+ )?;
+
+ check_nullability(
+ when(not(is_null(col("foo"))), col("foo")).otherwise(lit(0))?,
+ false,
+ get_schema,
+ )?;
+
+ check_nullability(
+ when(binary_expr(col("foo"), Operator::Eq, lit(5)), col("foo"))
+ .otherwise(lit(0))?,
+ true,
Review Comment:
Agreed, the const evaluation is far from complete. I tried to do something
good enough for the coalesce simplification initially.
I was wondering the whole time if there isn't some existing null analysis
logic somewhere in the codebase we could reuse. The best I could come up with
is rewriting the full expression by replacing the `then` expression with
literal `NULL` and then attempting const evaluation. But that got me worrying
about planning overhead again.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]