Repository navigation
Do not invert an ordered comparison that can see a NaN in the CNF form - #118216
Conversation
`convert_query_to_cnf = 1` rewrote `NOT (x < c)` into `x >= c` and `NOT (x > c)` into `x <= c`. `NaN` fails every ordered comparison, so `NOT (nan < c)` is true while `nan >= c` is false: the two operators are complementary over a totally ordered domain only, and the rewrite silently dropped every `NaN` row from the result of a query whose `WHERE` used the negated form. The analyzer knows the argument types, so it keeps inverting a comparison that cannot see a `NaN`: an argument that is a floating point column - or holds one inside an `Array`, `Tuple`, `Map`, `Nullable` or `LowCardinality` - now declines the inversion, and a floating point literal declines it only when it really is a `NaN`. The legacy AST stage in `TreeCNFConverter` has no types, so it cannot tell a floating point comparison from an integer one and now leaves every ordered comparison alone; the equality, `IN`, `LIKE` and `empty` operators keep being inverted there, since those answer false, not NULL, for a `NaN`. Closes: #112036 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Workflow [PR], commit [f119d14] Summary: ❌
AI ReviewSummaryThis PR fixes the main Findings❌ Blockers
Tests
Final Verdict
LLVM Coverage ReportMeasured on commit f119d14.
Changed lines: Changed C/C++ lines covered: 40/41 (97.56%) · Uncovered code |
|
TL;DR: the new guard misses
CREATE TABLE t (d Dynamic) ENGINE = Memory;
INSERT INTO t VALUES (nan::Float64), (1.0::Float64), (100.0::Float64);
SELECT count() FROM t WHERE NOT (d < 65.5) SETTINGS convert_query_to_cnf = 0; -- 2, correct
SELECT count() FROM t WHERE NOT (d < 65.5) SETTINGS convert_query_to_cnf = 1; -- 1, the nan row is droppedWith this PR the second query keeps returning 1: both arguments pass
|
Build profile diff (arm_release)Comparing commit See the job log for details. |
…d, drop the old analyzer from the test `typeCanHoldNaN` judged a type by its own floating point kind and by the kinds of its children, which missed `Dynamic` and `JSON`: `DataTypeDynamic::forEachChild` enumerates nothing and `DataTypeObject::forEachChild` enumerates only the typed paths, yet both carry values of any type, and a comparison over a `Dynamic` value is executed per inner type, so a `Float64` `NaN` inside it fails the ordered comparison the same way a plain `Float64` does. `NOT (d < c)` over a `Dynamic` column was still inverted into `d >= c` and dropped the `NaN` rows. Both types are now judged by the type itself, also when nested in a carrier such as `Array(Dynamic)`. The test used `enable_analyzer = 0` for the legacy path and `EXPLAIN QUERY TREE` without forcing the analyzer, which failed in the old analyzer CI job: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118216&sha=0f068278873adb476e3487270d8e43a9815269c9&name_0=PR&name_1=Stateless%20tests%20%28amd_llvm_coverage%2C%20old%20analyzer%2C%20s3%20storage%2C%20DBReplicated%2C%20parallel%2C%201%2F3%29 `enable_analyzer = 0` is rejected on master since 26.9, so those queries are removed, and the `EXPLAIN QUERY TREE` checks set `enable_analyzer = 1` explicitly. `Dynamic` and `Array(Dynamic)` cases are added; a `NaN` cannot be stored in a `JSON` path (the conversion drops it), so `JSON` is covered by the guard only. #118216 Co-Authored-By: Claude Fable 5.1 <[email protected]>
|
🕵 Thanks, @zlareb1 — adopted in 4ebcb44: |
| @@ -323,14 +328,10 @@ void pushNotIn(CNFQueryAtomicFormula & atom) | |||
|
|
|||
| static const std::unordered_map<std::string, std::string> inverse_relations = { | |||
There was a problem hiding this comment.
ConstraintsDescription::buildGraph still assumes pushNotIn leaves ordered comparisons non-negative before they are added to the ComparisonGraph (src/Storages/ConstraintsDescription.cpp:142-148). After removing the ordered entries here, a constraint like CHECK NOT (i < 5) stays as {negative = true, ast = less(i, 5)}; in release builds the chassert(!atom.negative) is compiled out and we push plain less(i, 5) into the graph. That inverts the meaning of the constraint, so WhereConstraintsOptimizer can treat i < 5 as always true and drop or weaken filters incorrectly.
The graph builder needs a real if (!atom.negative) guard (or another safe representation for negated ordered comparisons) before inserting the atom, and this path needs a focused regression test with a negated ordered CHECK.
| return true; | ||
|
|
||
| for (const auto & argument : function_node.getArguments()) | ||
| if (argumentCanBeNaN(argument)) |
There was a problem hiding this comment.
The analyzer-side constraint graph still relies on the stronger precondition that pushNotIntoFunction returns a non-negative ordered comparison before ConstraintsDescription::QueryTreeData::buildGraph inserts it (src/Storages/ConstraintsDescription.cpp:286-293). Once this new NaN guard declines NOT (x < 5) for a Float64, Dynamic, or JSON constraint, the atom stays negative; in release builds the chassert(!atom.negative) disappears and we still push plain less(x, 5) into the ComparisonGraph.
That records the opposite invariant from the actual CHECK and lets constraint-based rewrites misoptimize queries over floating-point data. Please either skip still-negative ordered atoms when building the graph or teach the graph to represent them explicitly, and add a regression test for a negated ordered floating-point constraint.
Edited by the nightly changelog CI job following .claude/skills/edit-changelog/SKILL.md. Changelog-revert: 119274 118919 Revert "Carry the mutation version onto a rewritten lightweight `DELETE`" Changelog-revert: 119306 119274 Revert "Revert "Carry the mutation version onto a rewritten lightweight `DELETE`"" Changelog-revert: 119334 117356 Revert "Unify the spill trigger for hash joins" Changelog-revert: 119335 119334 Revert "Revert "Unify the spill trigger for hash joins"" Changelog-revert: 121135 118279 Revert "Test the ESCAPE route into the ngram LIKE-pattern tokenizer" Changelog-revert: 121136 121135 Revert "Revert "Test the ESCAPE route into the ngram LIKE-pattern tokenizer"" Changelog-revert: 121258 118216 Revert "Do not invert an ordered comparison that can see a `NaN` in the CNF form" Changelog-revert: 121298 117578 Revert "Bound the docker buildx retry ladder by wall clock, not by the timeout divisor" Changelog-revert: 121443 114465 Revert "Fix an error reading an Iceberg table whose `current-snapshot-id` is JSON null" Changelog-revert: 121477 121096 Revert "Trim `test_max_bytes_ratio_before_external_distinct` to fit the client timeout" Changelog-revert: 121901 121869 Revert "docs(managed-postgres): updated Private Service Connect" Changelog-revert: 122110 118223 Revert "Correct nightly coverage selection and share PR test manifests" Changelog-revert: 122140 120312 Revert "Refuse a `Set` table on the right side of `IN` when a row policy applies to it" Changelog-deleted-entry: 114465 [Bug Fix (user-visible misbehavior in an official stable release)] [raw] * Fixed an error when reading an Iceberg table whose `current-snapshot-id` is JSON null, which some writers emit for a table with no current snapshot. [#114465](#114465) ([Shaohua Wang](https://github.com/tiandiwonder)). Changelog-deleted-entry: 118216 [Bug Fix (user-visible misbehavior in an official stable release)] [raw] * Fixed `convert_query_to_cnf = 1` silently dropping rows with a `NaN` in a floating point column: the conversion rewrote `NOT (x < c)` into `x >= c`, which is not an equivalent condition for `NaN`. [#118216](#118216) ([Alexey Milovidov](https://github.com/alexey-milovidov)). Changelog-deleted-entry: 118223 [NOT FOR CHANGELOG / INSIGNIFICANT] [raw] * Correct nightly coverage selection and share PR test manifests. [#118223](#118223) ([Nikita Fomichev](https://github.com/fm4v)). Changelog-deleted-entry: 120312 [Bug Fix (user-visible misbehavior in an official stable release)] [raw] * A row policy on a `Set`-engine table was accepted but never applied: the right side of `IN` read the prebuilt set directly and returned the rows the policy hides. Such a query now fails with `ACCESS_DENIED`, because the `Set` engine has no read path that could filter the rows. Queries that used a `Set` table covered by a database-wide row policy ran unfiltered before and now throw. [#120312](#120312) ([Harikrishnan Prabakaran](https://github.com/harikrishnan94)). Changelog-deleted-entry: 121096 [NOT FOR CHANGELOG / INSIGNIFICANT] [raw] * Sized `test_max_bytes_ratio_before_external_distinct` to fit the integration harness's 600 second per-query timeout. [#121096](#121096) ([Groene AI](https://github.com/groeneai)).
…graph test #118216 was reverted by #121258, so the CNF conversion inverts `NOT (a < b)` into `a >= b` again even for `Float64`. The result of `WHERE a >= b` on the table with a `NaN` row depends on that, which is not what this test is about, so it is no longer checked. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=121255&sha=4ce0e19ad7da1a501b440283b2607270955e32b7&name_0=PR&name_1=Fast%20test PR: #121255 Co-Authored-By: Claude Opus 5.5 <[email protected]>
`NOT (x < c)` is not `x >= c` when an argument can be a `NaN`, so ClickHouse#118216 stopped the CNF conversion from inverting an ordered comparison. `pushNotIn` and `Analyzer::CNF::pushNotIntoFunction` can therefore return an atom that is still negative, which the two places that build the constraints comparison graph did not expect: they asserted `!atom.negative` and then took the atom in as if it were positive. So `CREATE TABLE t (a Int64, b Int64, CONSTRAINT c ASSUME NOT (a < b))` is an exception in a build with assertions, and in a release build it puts `a < b` into the graph for a constraint that states the opposite - after which `optimize_using_constraints` answers `WHERE a < b` from the graph as always true and `WHERE a >= b` as always false. The comparison graph stores plain relations and has nowhere to put a negation, so such an atom is now left out of it. For the same reason the index-hint groups, which read the relation off the function name, no longer derive a hint from a negated atom. Found by BuzzHouse on master: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=a78f42eea9cea494806b50ec12b74dc72367944e&name_0=MasterCI&name_1=BuzzHouse%20%28amd_msan%29 https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=ed38f61d0941cf0a81dd0b410b3a01afbd60eba0&name_0=MasterCI&name_1=BuzzHouse%20%28amd_debug%29 https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=3c54270f07bd67f052bc900f371312f59f73505f&name_0=MasterCI&name_1=BuzzHouse%20%28arm_asan_ubsan%29 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
convert_query_to_cnf = 1rewroteNOT (x < c)intox >= candNOT (x > c)intox <= c.NaNfails every ordered comparison, soNOT (nan < c)is true whilenan >= cis false: the two operators are complementary over a totally ordered domain only, and the rewrite silently dropped everyNaNrow from the result.The analyzer knows the argument types, so it keeps inverting a comparison that cannot see a
NaN: an argument that is a floating point column - or holds one inside anArray,Tuple,Map,NullableorLowCardinality- declines the inversion, and a floating point literal declines it only when it really is aNaN. The legacy AST stage inTreeCNFConverterhas no types, so it cannot tell a floating point comparison from an integer one and now leaves every ordered comparison alone; the equality,IN,LIKEandemptyoperators keep being inverted there, since those answer false, not NULL, for aNaN.Closes: #112036
Related: #111939
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
Fixed
convert_query_to_cnf = 1silently dropping rows with aNaNin a floating point column: the conversion rewroteNOT (x < c)intox >= c, which is not an equivalent condition forNaN.Workflow [PR]
Sync PR [sync-upstream/pr/118216]
Version info
26.10.1.306-master(included in26.10and later)