Repository navigation
Decline divide/multiply monotonicity when an endpoint maps to NaN - #118468
Conversation
Index analysis models `divide` and `multiply` by a constant as monotonic from the constant alone. A
`Float` key column may hold `±inf`, and `-inf / inf`, `inf * 0` and `0 * inf` are all `NaN`, so the
transformed range endpoint became `NaN` - a range no point compares into - and every part and granule
was pruned:
CREATE TABLE t (k Float64) ENGINE = MergeTree ORDER BY k;
INSERT INTO t VALUES (-inf), (1), (2), (3);
SELECT count() FROM t WHERE k / inf = 0; -- 0, expected 3
SELECT count() FROM t WHERE k * 0 = 0; -- 0, expected 3
Both pruning layers were affected on their own, the primary key index and the statistics-based part
pruning, so the query silently returned nothing at default settings.
Only the transform can tell, so it is now evaluated at both endpoints - as the `plus`/`minus` branches
already do for their own overflow question - and monotonicity is declined when either result is `NaN`.
The check is restricted to a `Float` result: no other result type has a `NaN`, and an integer division
by a zero constant would raise here rather than answer one.
Closes: #118121
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Workflow [PR], commit [d3b9acd] Summary: ✅
AI ReviewSummaryThe implementation matches the reported Tests
Final VerdictNeeds one more regression test for the LLVM Coverage ReportMeasured on commit d3b9acd.
Changed lines: Changed C/C++ lines covered: 19/19 (100.00%) · Uncovered code |
| INSERT INTO t_monotonicity_nan VALUES (-inf), (1), (2), (3); | ||
|
|
||
| SELECT 'a stored -inf endpoint mapped to NaN by the transform'; | ||
| SELECT count() FROM t_monotonicity_nan WHERE k / inf = 0; |
There was a problem hiding this comment.
This covers the variable / constant divide case, but the new guard also changes the separate const / variable branch. Please add one focused regression such as WHERE inf / k = inf over (1), (2), (3), (inf): after KeyCondition applies the decreasing transform it inverts the range, so a transformed NaN max becomes the left bound, which is the same false-pruning shape this PR is fixing.
There was a problem hiding this comment.
🕵 Good catch, and you are right that the guard changes the const / variable branch too — inf / k over a range ending at +inf gives a decreasing transform whose NaN max becomes the left bound after inversion, which is the same false-pruning shape.
This PR had already merged by the time I got here, so I added the coverage in a follow-up: #119461 (test 05182_arithmetic_monotonicity_nan_const_dividend). It pins WHERE inf / k = inf over (1), (2), (3), (inf) as you suggested, plus const * variable (0 * k) and each pruning layer on its own. Verified with controls against a binary built from current master: with the guard, EXPLAIN indexes = 1 reports Granules: 100/100 and the query returns all matching rows, while a finite constant dividend (100 / k > 2) still prunes to Granules: 49/100.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes17 object files changed (+96.15 KiB total), 0 added.
716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units34 translation units recompiled, 491 s compile time in total, 34 of them have a recent master baseline. Median compile-time ratio to the baselines is ×1.06 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio. |
alexey-milovidov
left a comment
There was a problem hiding this comment.
First of all, the current behavior is not incorrect - we allow implementation specific behavor on NaN even when it is not consistent among different implementations.
But after these changes, it is more consistent, and the changes don't harm.
```sql CREATE TABLE t (x Float64) ENGINE = MergeTree ORDER BY x; INSERT INTO t VALUES (-inf), (1), (2), (3); SELECT x / inf AS n FROM t ORDER BY n; -- nan, 0, 0, 0 SELECT x / inf AS n FROM t ORDER BY n LIMIT 2; -- nan, 0 ``` No `NaN` is in the data or in the query: the key holds `-inf` and the `ORDER BY` expression turns it into one. The read-in-order match asks for monotonicity with Null endpoints (`matchTrees` passes empty fields), `divide` by a positive constant answers "monotonically increasing", so the forward read of the key is kept - while the transformed value of the `-inf` row sorts last, and nothing sorts the stream afterwards. The check added in ClickHouse#118468 declines when a concrete endpoint maps to `NaN`, and cannot fire here because the endpoints are unknown. So the constant itself is examined: `divide`/`multiply` of a `Float` result by `0`, `±inf` or `NaN` can turn a legal input into `NaN` (`±inf / ±inf`, `0 * ±inf`), and only those constants can, so an ordinary `x / 2` still reads in order. Closes: ClickHouse#118472 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The `NaN`-endpoint guard added in ClickHouse#118468 declines `divide`/`multiply` monotonicity for both operand orders, but `05113_arithmetic_monotonicity_nan_endpoint` only exercised `variable / const`. The `const / variable` branch has its own false-pruning shape: for `inf / k` over a range whose right end is `+inf` the transform is decreasing, so `KeyCondition` inverts the range and the transformed `NaN` becomes the left bound. Verified against the current binary with positive and negative controls: with the guard, `EXPLAIN indexes = 1` for `inf / k = inf` reports `Granules: 100/100` and the query returns all matching rows, while a finite constant dividend (`100 / k > 2`) still prunes down to `Granules: 49/100`. Related: ClickHouse#118468 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Index analysis models
divideandmultiplyby a constant as monotonic from the constant alone. AFloatkey column may hold
±inf, and-inf / inf,inf * 0and0 * infare allNaN, so the transformed rangeendpoint became
NaN- a range no point compares into - and every part and granule was pruned:Both pruning layers were affected on their own - the primary key index and the statistics-based part pruning
(
use_statistics_for_part_pruning, on by default) - so the query silently returned nothing at defaultsettings. No
NaNappears in the data or in the query: a legal±infin the column is enough.Only the transform can tell, so it is now evaluated at both endpoints - as the
plus/minusbranchesalready do for their own overflow question - and monotonicity is declined when either result is
NaN. Thecheck is restricted to a
Floatresult: no other result type has aNaN, and an integer division by a zeroconstant would raise here rather than answer one. Pruning for ranges that stay finite is unchanged, which
the test pins.
Closes: #118121
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
Fixed queries silently returning no rows when a
Floatprimary key column holds±infand the predicate'sarithmetic maps that endpoint to
NaN, e.g.WHERE k / inf = 0orWHERE k * 0 = 0: index and statisticspruning dropped every part and granule.
Workflow [PR]
Sync PR [sync-upstream/pr/118468]