ApacheWang opened a new pull request, #27823:
URL: https://github.com/apache/datafusion/pull/27823
## Which issue does this PR close?
- Closes #27822.
## Rationale for this change
`NullableInterval::MaybeNull { values }` represents the value set `values ∪
{NULL}`. When `values.lower == values.upper` (for example, from Parquet
row-group or partition statistics where `min == max == 5` and `null_count >
0`), the column can take **two** distinct values (`5` or `NULL`), not a single
value.
Two places in `NullableInterval` treated singleton `MaybeNull` intervals as
if they could not be `NULL`, causing `GuaranteeRewriter` (and
`ExprSimplifier::with_guarantees`) to silently rewrite queries into wrong
results:
1. **`NullableInterval::single_value()`** matched `Self::MaybeNull { values
} | Self::NotNull { values }` and returned `Some(values.lower.clone())`,
causing `GuaranteeRewriter` to fold `col("x")` to `lit(5)` (making `x IS NULL`
evaluate to `false` and `x = 5` evaluate to `true` even when rows are `NULL`).
2. **`NullableInterval::apply_operator()`** for `Operator::IsDistinctFrom` /
`Operator::IsNotDistinctFrom` reduced any pair where at least one side was
`NotNull` to `lhs_values.equal(rhs_values)?.not()?`. For `(MaybeNull { [5, 5]
}, NotNull { [5, 5] })`, that yielded `Interval::FALSE` (`IsDistinctFrom`) and
`Interval::TRUE` (`IsNotDistinctFrom`), ignoring that `NULL IS DISTINCT FROM 5`
is `TRUE`. Consequently, `GuaranteeRewriter::rewrite_binary_expr` folded `x IS
DISTINCT FROM 5` to `false` and `x IS NOT DISTINCT FROM 5` to `true`.
## What changes are included in this PR?
- `datafusion/expr-common/src/interval_arithmetic.rs`:
- Restrict `NullableInterval::single_value()` to return `Some(v)` only for
`NullableInterval::Null` (typed null) and singleton
`NullableInterval::NotNull`, returning `None` for
`NullableInterval::MaybeNull`. Updated the doc example accordingly.
- Fix `NullableInterval::apply_operator()` for `Operator::IsDistinctFrom`
(and by delegation `Operator::IsNotDistinctFrom`) so that `(NotNull,
MaybeNull)` / `(MaybeNull, NotNull)` returns `Interval::TRUE` only when
`lhs.equal(rhs)? == Interval::FALSE` (disjoint non-null ranges), and
`Interval::TRUE_OR_FALSE` otherwise.
- `datafusion/expr/src/expr_rewriter/guarantees.rs`:
- Add end-to-end `GuaranteeRewriter` regression test confirming singleton
`MaybeNull` guarantees do not fold `x`, `x IS NULL`, `x IS NOT NULL`, `x = 5`,
or `x IS (NOT) DISTINCT FROM 5`, while still folding disjoint `x IS (NOT)
DISTINCT FROM 6`.
## What is the testing strategy for this PR?
- Added unit test
`interval_arithmetic::tests::nullable_interval_singleton_maybe_null` and
updated the `NullableInterval::single_value` doctest in
`datafusion/expr-common/src/interval_arithmetic.rs`.
- Added
`expr_rewriter::guarantees::tests::test_singleton_maybe_null_not_folded` in
`datafusion/expr/src/expr_rewriter/guarantees.rs`.
- Verified both tests fail before the fix and pass after:
- `cargo test -p datafusion-expr-common` (`252 passed; 0 failed` unit
tests + `11 passed; 0 failed` doctests)
- `cargo test -p datafusion-expr --lib` (`310 passed; 0 failed`)
- `cargo clippy -p datafusion-expr-common -p datafusion-expr --all-targets
-- -D warnings` & `cargo fmt --check`
## Are there any user-facing changes?
Bug fix: prevents unsound expression simplification for columns with
singleton `MaybeNull` guarantees (e.g., `min == max` with nulls present).
--
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]