Skip to content

Do not put a negated comparison into the constraints comparison graph - #121255

Merged
alexey-milovidov merged 4 commits into
masterfrom
fix-comparison-graph-negative-atom
Oct 3, 2026
Merged

alexey-milovidov merged 4 commits into
masterfrom
fix-comparison-graph-negative-atom

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Sep 21, 2026 •

Copy link
Copy Markdown
Member

MasterCI is red: BuzzHouse hits Logical error: '!atom.negative' in ConstraintsDescription::buildGraph on CREATE TABLE.

CREATE TABLE t (a Int64, b Int64, CONSTRAINT c ASSUME NOT (a < b)) ENGINE = MergeTree ORDER BY tuple();

NOT (x < c) is not x >= c when an argument can be a NaN, so #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 in a build with assertions the statement above is an exception, 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 and would otherwise derive a hint from a constraint that says the opposite, no longer derive one from a negated atom.

CI reports:

Update (2026-10-03): #118216 has since been reverted by #121258, so on current master the CNF conversion inverts ordered comparisons again and the exception no longer happens. This change keeps the comparison graph and the index hints correct for a negated atom regardless, so that re-landing the NaN-aware inversion (e.g. #112989) does not bring the exception back. The test no longer pins the result of WHERE a >= b over a NaN row, which depends on whether the inversion is in place.

Related: #118216
Related: #121258

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

...

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/121255]

Version info

  • Merged into: 26.10.1.1448-master (included in 26.10 and later)

`NOT (x < c)` is not `x >= c` when an argument can be a `NaN`, so
#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]>
@clickhouse-gh

clickhouse-gh Bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [24de7c4]

Summary: ✅


AI Review

Summary

This PR hardens the constraints comparison-graph and indexHint derivation against negated comparison atoms by skipping graph/hint generation when a negation survives CNF rewriting, and it adds focused regression coverage around the affected constraint-optimization behavior. Given the current diff, the prior PR discussion, and the all-green CI, I do not see an unresolved correctness, safety, or performance issue in the proposed change.

Final Verdict
  • Status: ✅ Approve

LLVM Coverage Report

⚠️ No coverage measurement for commit 24de7c4: incomplete coverage measurement: 1 of 21 shard profiles are missing: LLVM_COVERAGE_FILE_it_5.profdata.

@clickhouse-gh clickhouse-gh Bot added pr-not-for-changelog This PR should not be mentioned in the changelog comp-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). labels Sep 21, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 24de7c459 with master cbb01f279 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 87e4b7437; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

9 translation units recompiled, 17 s compile time in total, 9 of them have a recent master baseline.

Job report

alexey-milovidov and others added 3 commits September 26, 2026 00:56
…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]>

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a pessimization. However, we can accept the fix for an assertion.

@alexey-milovidov alexey-milovidov self-assigned this Oct 3, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 3, 2026
Merged via the queue into master with commit 67a4052 Oct 3, 2026
150 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-comparison-graph-negative-atom branch October 3, 2026 21:43
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). pr-not-for-changelog This PR should not be mentioned in the changelog pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants