pepijnve commented on code in PR #17813:
URL: https://github.com/apache/datafusion/pull/17813#discussion_r2388664096
##########
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,
+ get_schema,
+ )?;
+
+ check_nullability(
+ when(
+ and(
+ is_not_null(col("foo")),
+ binary_expr(col("foo"), Operator::Eq, lit(5)),
+ ),
+ col("foo"),
+ )
+ .otherwise(lit(0))?,
+ false,
+ get_schema,
+ )?;
+
+ check_nullability(
+ when(
+ and(
+ binary_expr(col("foo"), Operator::Eq, lit(5)),
+ is_not_null(col("foo")),
+ ),
+ col("foo"),
+ )
+ .otherwise(lit(0))?,
+ false,
+ get_schema,
+ )?;
+
+ check_nullability(
+ when(
+ or(
+ is_not_null(col("foo")),
+ binary_expr(col("foo"), Operator::Eq, lit(5)),
+ ),
+ col("foo"),
+ )
+ .otherwise(lit(0))?,
+ true,
+ get_schema,
+ )?;
+
+ check_nullability(
+ when(
+ or(
+ binary_expr(col("foo"), Operator::Eq, lit(5)),
+ is_not_null(col("foo")),
+ ),
+ col("foo"),
+ )
+ .otherwise(lit(0))?,
+ true,
+ get_schema,
+ )?;
+
+ check_nullability(
+ when(
+ or(
+ is_not_null(col("foo")),
+ binary_expr(col("foo"), Operator::Eq, lit(5)),
+ ),
+ col("foo"),
+ )
+ .otherwise(lit(0))?,
+ true,
+ get_schema,
+ )?;
+
+ check_nullability(
+ when(
+ or(
+ binary_expr(col("foo"), Operator::Eq, lit(5)),
+ is_not_null(col("foo")),
+ ),
+ col("foo"),
+ )
+ .otherwise(lit(0))?,
+ true,
+ get_schema,
+ )?;
Review Comment:
I've added this test case
--
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]