Abhisheklearn12 opened a new pull request, #25771:
URL: https://github.com/apache/datafusion/pull/25771
## Which issue does this PR close?
<!--
We generally require a GitHub issue to be filed for all bug fixes and
enhancements and this helps us generate change logs for our releases. You can
link an issue to this PR using the GitHub syntax. For example `Closes #123`
indicates that this PR will close issue #123.
-->
- Closes #25035.
## Rationale for this change
<!--
Why are you proposing this change? If this is already explained clearly in
the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand your
changes and offer better suggestions for fixes.
Please explain the problem you are trying to solve in terms of the
user-visible
behavior, rather than the implementation.
For example, "The code in `foo.rs` doesn't handle nulls" is a symptom of the
implementation. "COUNT(DISTINCT) returns wrong results when the column
contains
nulls" is the user-visible problem.
-->
In a left-deep `AND` chain, the accumulated prefix eventually drops below
the 20% pre-selection threshold, and from then on every binary node filters the
whole batch (all columns) and scatters back. Cheap predicates on wide batches
get several times slower, and the same predicate costs very different amounts
depending on how the tree is nested.
Option B alone (one n-ary pass, filtering once) makes both shapes equal but
not fast: filtering and scattering cost more than a cheap comparison over all
rows. So this also makes the filtering decision depend on the next conjunct.
## What changes are included in this PR?
<!--
There is no need to duplicate the description in the issue here, but it is
sometimes worth providing a summary of the individual changes in this PR.
-->
`BinaryExpr::evaluate` now evaluates an `AND` and the `AND`s nested under it
as one n-ary conjunction (`evaluate_conjunction`), in order:
- Before each conjunct, the batch is filtered down to the rows not yet
`false`, only when at most 20% of rows remain and the conjunct is not cheap and
infallible (`is_cheap_and_infallible`).
- `NULL` rows stay, since a later `false` still decides them.
- Evaluation stops once every row is `false`, and results are scattered back
once at the end.
The tree, `EXPLAIN`, proto and optimizer are untouched. `OR` is unchanged;
`check_short_circuit` just loses its now unused `AND` branches. A `conjunction`
benchmark is added to `binary_op.rs`.
## Things reviewers should know
1. **Overlap with #22698.** It edits the `AND` branch of
`check_short_circuit` that this PR removes, and exports `and_rhs_evaluation`,
which models the old per-node decision. We need to agree which lands first,
hence the draft.
2. **Contract.** An expensive or fallible conjunct never sees a row that
nested binary evaluation would have skipped, for any tree shape, so no query
can start failing. This relies on the cheap list only holding things that
cannot fail on a row: comparisons, `IN` lists, `NOT`, `IS [NOT] NULL`, lossless
casts (`CastExpr::check_bigger_cast`) and wrapping integer/float `+ - *`. I
assume comparisons only fail on type mismatches, never on values. Please double
check this list.
3. **Behaviour changes, same results:**
- Cheap conjuncts no longer trigger pre-selection.
- A `NULL` in the prefix no longer disables pre-selection before
expensive conjuncts.
- A non-boolean `AND` operand returns an internal error instead of
panicking.
4. **Known loss.** `predicate_eval` `selectivity_q21` is about 10% slower
(1.03 → 1.14 ms): a very selective, clustered prefix on a narrow table, where
filtering before a cheap conjunct still pays off. A calibration sweep showed
filtering first only helps when very few rows survive (about 2 to 5%) on narrow
batches, and costs up to 3.4× otherwise, so I kept the simple rule.
5. `predicate_eval` doesn't run on `main` right now: its template has the
`result` line twice (#25037, #25032). I removed the duplicate locally to
measure and will send that one-line fix separately.
## What is the testing strategy for this PR?
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code
Briefly describe how this PR is tested, and point to the specific tests you
added. For example: 'This new feature is covered by the `sqllogictest` cases
added in `foo.slt`'.
If this PR does not add tests, explain why. For example, if the change is
already covered by existing tests, please mention it.
You should also check the `codecov` bot reply on this PR to confirm the
changed code is exercised.
-->
New tests in `binary.rs`:
- Randomized check against a reference Kleene `AND` over left-deep,
right-deep and balanced trees, with NULLs and literals.
- Rows seen by each conjunct, identical for every tree shape.
- NULL rows stay undecided.
- A division-by-zero conjunct only sees the rows left.
- The cheap/fallible classification and the filtering decision.
- A non-boolean operand errors instead of panicking.
The shape, NULL and non-boolean tests fail on `main`.
`cargo fmt`, `cargo clippy --all-targets --all-features -- -D warnings` and
the extended test run (`--profile ci` with `extended_tests`, including all 524
sqllogictest files) pass: 12,159 tests. The only failure,
`sort_with_mem_limit_1_runner`, fails the same way on `main` on my machine (an
RSS limit, with no `AND` in the query).
### Benchmarks
i7-11700F, Linux 6.1, rustc 1.98.1. Micro: Criterion means, 8192-row
batches, 70% pass rate per conjunct unless noted. End to end: `release-nonlto`,
interleaved runs with a `main` vs `main` control.
`cargo bench --bench binary_op -- conjunction`, µs, left-deep / right-deep:
| Chain | `main` | This PR |
|---|---|---|
| 8 × `c < x` | 87 / 35 | 31 / 32 |
| 8 × `c < x`, 32 unused columns | 204 / 35 | 32 / 32 |
| 16 × `c < x`, 32 unused columns | 438 / 72 | 60 / 59 |
| 8 × `CAST(c AS BIGINT) < x` | 102 / 59 | 54 / 58 |
| 8 × `c IN (...)` | 110 / 70 | 69 / 68 |
| 8 × `c + 1 < x` | 95 / 57 | 47 / 44 |
| 8 × `c < x`, then a regex | 406 / 489 | 282 / 282 |
| 4 nullable `c < x` at 50%, then a regex | 441 / 460 | 332 / 353 |
| 8 × `c < x` at 90% (never filters) | 36 / 35 | 31 / 33 |
The existing `short_circuit/and` benchmarks are unchanged or up to 8% faster.
TPC-H (Parquet), ms:
| Query | SF1 `main` → PR | SF10 `main` → PR |
|---|---|---|
| Q6 | 49.0 → 42.0 (−14%) | 386 → 327 (−15%) |
| Q12 | 44.6 → 38.5 (−14%) | 383 → 320 (−17%) |
| Q19 | 90.5 → 82.7 (−9%) | 849 → 744 (−12%) |
These three were faster in every round. At SF1, Q16 is 4% faster (5 of 6
rounds) and every other query is within ±4%; at SF10, Q1 and Q14 are within 2%.
`predicate_eval` (1M rows, 8 rounds, results validated): geometric mean
0.974 (`main` vs `main`: 1.005), `cardinality_q33_k16` 13.1 → 5.5 ms (2.4×).
Apart from `selectivity_q21` above, no query is more than about 5% slower,
except `drift_q83`, which is just as noisy `main` vs `main`.
## Are there any user-facing changes?
<!--
If there are user-facing changes then we may require documentation to be
updated before approving the PR.
If there are any breaking changes to public APIs, please add the `api
change` label.
-->
No API changes. `AND` filters get faster, and fallible predicates are never
evaluated on more rows than before.
--
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]