ApacheWang opened a new issue, #27822:
URL: https://github.com/apache/datafusion/issues/27822

   ### Describe the bug
   
   `NullableInterval` in `datafusion-expr-common` mishandles singleton 
`MaybeNull` intervals (where `lower == upper` and `null` is also possible, 
representing `{v, NULL}` — for example from row-group/partition statistics 
where `min == max == 5` and `null_count > 0`):
   
   1. **`NullableInterval::single_value()`** 
(`datafusion/expr-common/src/interval_arithmetic.rs`) matches `Self::MaybeNull 
{ values } | Self::NotNull { values }` and returns `Some(values.lower.clone())` 
when `values.lower == values.upper`. A `MaybeNull` interval represents `{v, 
NULL}` (two distinct possible values), not a single value.
      - Because `GuaranteeRewriter` 
(`datafusion/expr/src/expr_rewriter/guarantees.rs`) replaces any expression 
with `interval.single_value() == Some(v)` by `lit(v)`, a column `x` with 
guarantee `MaybeNull { [5, 5] }` is silently rewritten to `lit(5)` — turning `x 
IS NULL` into `false`, `x = 5` into `true`, and replacing NULL rows with `5`.
   2. **`NullableInterval::apply_operator()`** for `Operator::IsDistinctFrom` / 
`Operator::IsNotDistinctFrom` assumes that whenever at least one side is 
`NotNull`, `x IS DISTINCT FROM y` reduces to 
`lhs_values.equal(rhs_values)?.not()?`.
      - For `(MaybeNull { [5, 5] }, NotNull { [5, 5] })`, 
`lhs_values.equal(rhs_values)?.not()?` evaluates to `Interval::FALSE`, so 
`apply_operator` returns `NullableInterval::FALSE` for `IsDistinctFrom` and 
`NullableInterval::TRUE` for `IsNotDistinctFrom`.
      - However, when the `MaybeNull` side is `NULL`, `NULL IS DISTINCT FROM 5` 
is `TRUE` (and `NULL IS NOT DISTINCT FROM 5` is `FALSE`). As a result, 
`GuaranteeRewriter::rewrite_binary_expr` silently folds `x IS DISTINCT FROM 5` 
to `false` and `x IS NOT DISTINCT FROM 5` to `true`.
   
   ### To Reproduce
   
   ```rust
   let maybe_5 = NullableInterval::MaybeNull {
       values: Interval::make(Some(5_i32), Some(5_i32))?,
   };
   let exact_5 = NullableInterval::from(ScalarValue::Int32(Some(5)));
   
   // Actual: Some(Int32(5))  |  Expected: None
   assert_eq!(maybe_5.single_value(), None);
   
   // Actual: NullableInterval::FALSE  |  Expected: 
NullableInterval::TRUE_OR_FALSE
   assert_eq!(
       maybe_5.apply_operator(&Operator::IsDistinctFrom, &exact_5)?,
       NullableInterval::TRUE_OR_FALSE
   );
   ```
   
   And with `rewrite_with_guarantees` on `x ∈ MaybeNull { [5, 5] }`:
   - `col("x")` is rewritten to `lit(5)` (expected: unchanged `col("x")`)
   - `col("x").is_null()` is rewritten to `lit(false)` (expected: unchanged)
   - `x IS DISTINCT FROM 5` is rewritten to `lit(false)` (expected: unchanged)
   
   ### Expected behavior
   
   - `NullableInterval::single_value()` should only return `Some(v)` for 
`NullableInterval::Null` (typed null) and singleton 
`NullableInterval::NotNull`, and return `None` for 
`NullableInterval::MaybeNull`.
   - For `IsDistinctFrom` with `(NotNull { lhs }, MaybeNull { rhs })` or 
`(MaybeNull { lhs }, NotNull { rhs })`:
     - Return `NullableInterval::TRUE` only when `lhs.equal(rhs)? == 
Interval::FALSE` (disjoint non-null ranges, so both the NULL case and all 
non-null pairs are distinct).
     - Otherwise return `NullableInterval::TRUE_OR_FALSE`.
   


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