sunchao commented on code in PR #27454:
URL: https://github.com/apache/datafusion/pull/27454#discussion_r4791848678


##########
datafusion/physical-expr/src/expressions/case.rs:
##########
@@ -1837,6 +1840,29 @@ fn replace_with_null(
     Ok(with_null)
 }
 
+/// Returns true if `expr` evaluates to NULL for every input row.
+///
+/// Keep this in sync with the logical-plan equivalent, `is_null` in
+/// `datafusion/expr/src/predicate_bounds.rs`, for the same reason as
+/// [`unwrap_certainly_null_expr`].
+fn is_certainly_null(expr: &dyn PhysicalExpr) -> bool {
+    if let Some(lit) = expr.downcast_ref::<Literal>() {
+        lit.value().is_null()
+    } else if let Some(expr) = expr.downcast_ref::<BinaryExpr>() {
+        expr.op().returns_null_on_null()
+            && (is_certainly_null(expr.left().as_ref())
+                || is_certainly_null(expr.right().as_ref()))
+    } else if let Some(expr) = expr.downcast_ref::<NotExpr>() {
+        is_certainly_null(expr.arg().as_ref())
+    } else if let Some(expr) = expr.downcast_ref::<NegativeExpr>() {
+        is_certainly_null(expr.arg().as_ref())
+    } else if let Some(expr) = expr.downcast_ref::<CastExpr>() {

Review Comment:
   Can we restrict this to casts that preserve top-level NULLs? For nullable `a 
INT` and `b INT[]`, consider
   
   ```sql
   SELECT CASE WHEN CAST(a AS INT[]) <> b THEN a ELSE 0 END AS x FROM t
   ```
   
   On `(NULL, [1])`, [Arrow wraps the null integer in a valid 
list](https://github.com/apache/arrow-rs/blob/60.2.0/arrow-cast/src/cast/list.rs#L8-L28).
 `[NULL] <> [1]` is true, so the result is NULL. Previously the remaining 
reference to `b` kept the physical field nullable. This branch now rules out 
TRUE and Arrow rejects the projected batch.
   
   The reverse assumption in `unwrap_certainly_null_expr` also needs a guard. 
With `a, b INT[1]` holding `[NULL], [1]`, `CASE WHEN a <> b THEN CAST(a AS INT) 
ELSE 0 END` selects [the null 
child](https://github.com/apache/arrow-rs/blob/60.2.0/arrow-cast/src/cast/list.rs#L62-L69)
 of a valid list. Could we cover both directions and keep the corresponding 
logical rules in sync?



##########
datafusion/physical-expr/src/expressions/case.rs:
##########
@@ -1837,6 +1840,29 @@ fn replace_with_null(
     Ok(with_null)
 }
 
+/// Returns true if `expr` evaluates to NULL for every input row.
+///
+/// Keep this in sync with the logical-plan equivalent, `is_null` in
+/// `datafusion/expr/src/predicate_bounds.rs`, for the same reason as
+/// [`unwrap_certainly_null_expr`].
+fn is_certainly_null(expr: &dyn PhysicalExpr) -> bool {
+    if let Some(lit) = expr.downcast_ref::<Literal>() {
+        lit.value().is_null()
+    } else if let Some(expr) = expr.downcast_ref::<BinaryExpr>() {
+        expr.op().returns_null_on_null()

Review Comment:
   One narrower exception to this operator rule is a dictionary of structs. 
Take two columns `d` and `e` of the same dictionary type. Let `d` have a valid 
key pointing to a null struct (as in 
[dict_struct_nullable](https://github.com/apache/datafusion/blob/330cccd2e6025b89646790dcc5e5391a6323fed1/datafusion/sqllogictest/src/test_context.rs#L824-L860))
 and let `e` point to a valid struct. [The nested 
comparison](https://github.com/apache/datafusion/blob/330cccd2e6025b89646790dcc5e5391a6323fed1/datafusion/physical-expr-common/src/datum.rs#L162-L208)
 compares them as unequal but masks the result with key validity only, so `d <> 
e` returns TRUE.
   
   In a CASE with that condition, a THEN that decodes `d` to the plain struct 
and a non-null ELSE, the selected value is NULL. The comparator behavior 
predates this PR, but the new inference now declares that output non-nullable. 
Could we keep this case conservative until the comparison uses logical nulls, 
or fix the mask with a regression test?



##########
datafusion/physical-expr/src/expressions/case.rs:
##########
@@ -1808,6 +1808,9 @@ fn evaluate_predicate(predicate: &Arc<dyn PhysicalExpr>) 
-> Result<NullableInter
             if let Some(expr) = predicate.downcast_ref::<NotExpr>() {
                 return evaluate_predicate(expr.arg())?.not();
             }
+            if is_certainly_null(predicate.as_ref()) {

Review Comment:
   This makes the volatile evaluation in #27411 affect a case that stays 
nullable on the base branch. In `CASE WHEN a < b OR random() < 0.5 THEN a ELSE 
1 END`, the left side now gives UNKNOWN when we substitute NULL for `a`. If the 
dummy batch gets FALSE on the right, we rule out TRUE and declare the result 
non-nullable. A fresh draw at execution can select a null `a`.
   
   Could we coordinate with #27412 so volatile subexpressions keep conservative 
bounds without losing the valid proof for `NULL < b`? A volatile test UDF that 
returns FALSE on its first call would make this deterministic. Please also 
check volatility before substituting a nullable THEN expression. In `CASE WHEN 
v() < b THEN v() ELSE 1 END`, the calls can return different values and 
substitution erases the volatile call before this check sees it.



-- 
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]

Reply via email to