kosiew opened a new issue, #27992:
URL: https://github.com/apache/datafusion/issues/27992
## Related PR
#27433
## Problem
`datafusion_physical_expr::intervals::cp_solver::propagate_arithmetic`
(`datafusion/physical-expr/src/intervals/cp_solver.rs:240`) is public. For
integer `Multiply` and `Divide` it can narrow a child interval, or report
infeasibility (`None`), even when some operand pair in the input ranges
produces
a value inside the parent interval.
Witnesses (Int64, direct calls, reproduced on `main` at `926901f900`):
| Operator | Parent | Left | Right | Result | Feasible pair dropped |
| --- | --- | --- | --- | --- | --- |
| `Multiply` | `[-3, 0]` | `[-3, -3]` | `[-3, 0]` | `None` | `-3 * 0 = 0` |
| `Divide` | `[0, 0]` | `[2, 2]` | `[3, 10]` | `None` | `2 / 3 = 0`
(truncation) |
| `Divide` | `[1, 1]` | `[2, 3]` | `[2, 2]` | `([2, 2], [2, 2])` | `3 / 2 =
1` (truncation) |
Cause: the solver inverts these operations with interval arithmetic. That
inversion is only valid over the reals and for non-zero factors:
- **Multiply, zero product.** When the parent contains `0` and either child
contains `0`, a zero factor leaves the other child unconstrained. Dividing
the
parent by the other child drops that case.
- **Divide, truncation.** `x / y = p` for integers does not imply `x ∈ y * p`
(`3 / 2 = 1`, but `3 ∉ [2, 2] * [1, 1]`). Likewise `y ∈ x / p` drops
divisors
that truncate the quotient to zero.
`BinaryExpr::propagate_constraints`
(`datafusion/physical-expr/src/expressions/binary.rs:731–756`, added in
#25234)
returns `Some(vec![])` (no refinement) for integer `Divide`, zero products,
and
possible wrapping before it calls `propagate_arithmetic`. It is the only
in-tree caller, so in-tree query paths are safe. The function itself carries
no such guard, and its rustdoc (`cp_solver.rs:222–239`) describes the
inversion rules without stating this precondition.
Tightening `Interval::div` makes this worse. With #27433 (integer divisors
with
a zero endpoint), an enumeration over Int64 ranges within `[-3, 3]` finds
more
dropped witnesses: Multiply goes from 2184 to 5112 and Divide from 6600 to
10964. The failure classes do not change.
## Why it matters
- Any caller outside `BinaryExpr`, such as an external `PhysicalExpr`
implementation or a downstream crate building its own propagation, gets
unsound results. Through interval analysis, that can mean rows excluded
from
a selectivity estimate or wrongly declared impossible.
- The guard sits apart from the function it protects. A new in-tree caller,
or a
refactor of `BinaryExpr::propagate_constraints`, can silently lose it.
- Future precision gains in `Interval::div` or `Interval::mul` add dropped
cases
to this path without any test failing (see #27433).
## Invariant / desired behavior
For integer children, every interval `propagate_arithmetic` returns must
contain
every value of that child that belongs to some feasible pair: `(x, y)` with
`x ∈ left`, `y ∈ right`, `op(x, y)` defined, and `op(x, y) ∈ parent`. It
returns
`None` only when no such pair exists. Leaving the inputs unchanged is always
a
valid answer.
## Proposed direction
Make the arithmetic rule owner enforce the guards:
1. In `propagate_arithmetic`, when both children are integers:
- for `Divide`, return the input children unchanged;
- for `Multiply` where the parent contains `0` and either child contains
`0`, return the input children unchanged.
2. Remove the matching integer-divide and zero-product checks from
`BinaryExpr::propagate_constraints`. Keep `integer_arithmetic_may_wrap`
there, because it depends on `BinaryExpr::fail_on_overflow`, which
`propagate_arithmetic` does not receive.
3. Update the `propagate_arithmetic` rustdoc to state the integer behavior
and
that wrapping overflow is the caller's responsibility.
`BinaryExpr` currently returns `Some(vec![])` for these cases (meaning "no
refinement"). `propagate_arithmetic` must return `Some((left, right))`
unchanged instead. Confirm that `ExprIntervalGraph` treats the two forms the
same way for these nodes.
If the maintainers prefer not to change behavior in a public function, the
fallback is documentation only: add a `# Correctness` section to the rustdoc
stating that integer `Divide` and zero-product `Multiply` results are unsound
unless the caller skips them. Reaching the invariant above requires the code
change.
## Scope
### In
- Integer `Multiply` and `Divide` handling in `propagate_arithmetic`.
- Removing the now-redundant integer-divide and zero-product checks from
`BinaryExpr::propagate_constraints`.
- Rustdoc for `propagate_arithmetic`.
### Out
- Wrap/overflow guard (`integer_arithmetic_may_wrap`): stays in `BinaryExpr`.
- Floating-point, decimal, and temporal propagation (not measured here; file
separately if a witness is found).
- Changes to forward `Interval::mul` / `Interval::div` (e.g. #27433).
- A sound truncating-division inverse that refines instead of bailing out;
possible later, not needed for soundness.
## Acceptance criteria
- [ ] The three witnesses in the table return intervals that contain the
dropped pair (`Some`, with `x` in the left result and `y` in the
right).
- [ ] Brute-force enumeration over Int64 parent, left, and right ranges
within
`[-3, 3]` finds zero dropped feasible pairs for `Multiply` and
`Divide`,
both on `main` and with #27433's `Interval::div` applied.
- [ ] `BinaryExpr::propagate_constraints` no longer has its own
integer-divide
or zero-product check, and its existing propagation tests still pass.
- [ ] `propagate_arithmetic` rustdoc states the integer behavior and the
wrapping precondition.
- [ ] Integer propagation that is still sound keeps refining, e.g.
`Plus p=[5, 5] l=[0, 10] r=[2, 2]` → left `[3, 3]`, and `Multiply`
without a zero.
## Tests / verification
- Unit tests in `cp_solver.rs` that call `propagate_arithmetic` directly
with the
three witnesses. Assert that each returned interval contains its witness
value, not only that the result is `Some`.
- A small exhaustive test (Int64 ranges within `[-3, 3]`, plus a few Int8
ranges at `i8::MIN` / `i8::MAX`) that checks the invariant above for
`Multiply` and `Divide`.
- Existing tests: `cargo test -p datafusion-physical-expr --lib -- intervals
binary`
and `cargo test -p datafusion-expr-common --lib interval_arithmetic`.
- An SLT or `analyze` selectivity check that integer `a * b` / `a / b`
filters
give the same estimates before and after the change, since `BinaryExpr`
already bailed out for these cases.
--
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]