Skip to content

Fix Field::operator<= comparing a Float64 with itself - #113010

Merged
alexey-milovidov merged 4 commits into
masterfrom
fix-field-float64-le
Aug 5, 2026
Merged

alexey-milovidov merged 4 commits into
masterfrom
fix-field-float64-le

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Aug 2, 2026 •

Copy link
Copy Markdown
Member

Closes: #112778
Caused by: #58959

The Float64 branch of Field::operator<= read both operands from *this:

Float64 f1 = get<Float64>();
Float64 f2 = get<Float64>();   /// should be rhs.get<Float64>()

so it compared a value with itself: less was always false, equals was always true, and the operator returned true for every rhs. Field::operator>= is implemented as rhs <= *this, so it inherited the same defect. Field::operator< and Field::operator== were already correct, and every other case of the same switch uses rhs.get<…>().

The fix reads the second operand from rhs.

There are no Field-to-Field uses of <= or >= anywhere in src/, programs/ or utils/ today - every nearby <=/>= operates on a value already extracted with get<T> or safeGet<T> - so this cannot change any query result. It removes a trap for whoever adds the first caller.

Added unit tests in src/Core/tests/gtest_field.cpp pin the full ordering of Float64 fields for <, <=, >, >=, == and !=, including NaN on either or both sides (nan_direction_hint is 1, so NaN sorts after every number and is equal to itself).

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):

...

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Backported to: 26.7.3.17, 26.6.3.11, 26.5.7.13

The `Float64` branch of `Field::operator<=` read both operands from
`*this`, so it compared a value with itself and returned `true` for every
`rhs`. `Field::operator>=` is implemented as `rhs <= *this`, so it
inherited the same defect.

Read the second operand from `rhs`, and pin the ordering of `Float64`
fields - including `NaN`, which is ordered after every number and is
equal to itself - with unit tests.

There are no `Field`-to-`Field` uses of `<=` or `>=` in the codebase
today, so no query result can change; this removes a trap for the first
caller.

Closes: #112778
Caused by: #58959

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Aug 2, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [8c8ec99]

Summary: ✅


AI Review

Summary

This PR fixes the Float64 arm of Field::operator<= so it reads the right-hand operand from rhs, which also restores Field::operator>= because that operator delegates to rhs <= *this. I checked the current diff against the surrounding Field ordering code and the prior discussion, and I did not find any remaining correctness, metadata, or coverage issues in the current version.

Final Verdict

✅ No new inline findings. The current change looks correct as written.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.50% 86.50% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 78.70% 78.80% +0.10%

Changed lines: Changed C/C++ lines covered: 71/71 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Aug 2, 2026
@tiandiwonder tiandiwonder self-assigned this Aug 3, 2026
Comment thread src/Core/tests/gtest_field.cpp Resolved
alexey-milovidov and others added 2 commits August 3, 2026 23:16
Address review: pin the `which`-prefix ordering across different Field
types and the `UUID` branch of `operator<=` / `operator>=`, which
compares `toUnderType()` because `StrongTypedef` has `operator<` but no
`operator<=`.

Co-Authored-By: Claude Fable 5 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=113010&sha=5887e1dcc68fed26b2b3d5126e5ec8c12bcc5fa9&name_0=PR&name_1=Stateless%20tests%20%28arm_asan_ubsan%2C%20targeted%29 and provide a fix in a separate PR. If the fix is already in progress, link it here.

04329_iceberg_insert_stale_schema_field_id_107268 failed 5 times on the targeted job (the expected two INCORRECT_DATA errors did not fire, i.e. the INSERT took the fresh-read path instead of the stale-cache path). Evidence it is unrelated to this PR:

  • The same test failed the same way yesterday on the targeted job of Finish the LONG_IS_A_DISTINCT_TYPE conversion, and fix it for wasm64 #112866, a NOLINT-comment-only change.
  • This PR only fixes Field::operator<= for Float64, and there are no Field-to-Field <=/>= callers in the tree.
  • Locally the test passes repeatedly with the exact randomized settings line from the failing runs, both on current master and with the Fix Iceberg trivial count bug #111617 Iceberg changes reverted — so the trigger looks specific to the arm ASan/UBSan environment (timing or shared Iceberg metadata-cache pressure), not to a settings combination.

@groeneai

groeneai commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator

No fix PR is open yet. This test is mine (added in #107279), the defect is in its oracle, and I am preparing the fix. I will link it here.

Your read of the failure mode is right, and the discriminator turns out to be the targeted job's flaky-check target set rather than any diff. On both failing heads the job ran 5 sibling Iceberg tests at 50x each (03565, 03581, 04337, 04545, 04561, so 250 sibling executions). Every passing head had at most 3 siblings at 50x, or fewer. Over 25 days of Stateless tests (arm_asan_ubsan, targeted):

sibling Iceberg executions heads 04329 FAIL 04329 OK
250 2 (this PR, and 112866) 7 0
<= 150 18 0 99

04329 is never the flaky-check target itself there; it runs 2 to 16 times as a co-selected test. no-parallel only serialises its own copies, so the 100 extra IcebergLocal lifecycles contributed by 04545 and 04561 run on parallel workers against the same server-global Iceberg metadata files cache (1000 entries / 128 MiB) and evict the entry the test needs to survive between its warm-up SELECT and the INSERT. That is also why it passes standalone and, on this same binary, in the sequential jobs.

Two things are wrong here, both mine:

  1. The oracle is INSERT ... 2>&1 | grep -oF "INCORRECT_DATA". It prints nothing for a different error code and nothing for a fully successful INSERT, so it cannot distinguish "wrong error" from "no error", and in the opposite cache state it passes vacuously. It should assert the actual outcome.
  2. The test inherits ambient cache warmth instead of owning the state it needs. The stale read has to be manufactured deterministically.

I will validate the fix against the failing condition itself, requiring the current form to fail and the new one to pass on that same state, so it preserves the regression it was written to catch rather than just going green.

@groeneai

groeneai commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator

Fix PR: #113232

Root cause is in my test, not in this PR. 04329 needs INSERT analysis to serve a stale schema
while the sink force-reads the latest one, and it built that by warming the server-global Iceberg
metadata files cache and relying on the entry surviving to the INSERT. That cache is SLRU-bounded
at 1000 entries and shared with every other test, so when earlier tests leave it at its bound the
warmed entry is evicted, the INSERT reads fresh, both views agree, and the rejection correctly does
not fire. That matches your reading exactly: the INSERT took the fresh-read path.

A second defect made it silent rather than diagnosable: grep -oF "INCORRECT_DATA" prints nothing
for a successful INSERT and nothing for a different error code, so the reference diff could not
distinguish a wrong code from no error at all.

The fix clears the cache immediately before each warm-up SELECT, so the test owns the state it
depends on, and reports the actual outcome for every outcome. .reference is unchanged, so the
rejection must still fire twice; a run that stops exercising it now fails and names why. With the
cache bound lowered to 40 and pre-filled to 40/40, the old form fails with a diff byte-identical to
the failure here and the new form passes on that same state.

Your negative local repro was useful: it is consistent with this being residual cache occupancy
rather than a settings combination, so I did not spend time on a settings repro.

pull Bot pushed a commit to weiyilai/ClickHouse that referenced this pull request Aug 4, 2026
The test needs INSERT analysis to serve a stale schema while the sink
force-reads the latest one. It built that by warming the server-global
Iceberg metadata files cache and relying on the entry surviving to the
INSERT. That cache is SLRU-bounded at 1000 entries and shared with every
other test on the server, so a cache left at its bound by earlier tests
evicts the entry: the INSERT reads fresh, both views agree, and the
rejection correctly does not fire.

A second defect made that silent. Both oracles were
`grep -oF "INCORRECT_DATA"`, which prints nothing for a successful INSERT
and nothing for a different error code, so the reference diff could not
distinguish a wrong error code from no error at all, and in the opposite
cache state the test passed vacuously.

Clear the cache immediately before each warm-up SELECT so the test owns
the state it depends on, and report the actual outcome for every outcome
(the error code name, or NO_ERROR). The reference file is unchanged, so
the rejection must still fire in both scenarios; a run that stops
exercising it now fails and names what happened instead of passing.

no-parallel is kept, and its comment now states the real reason: the
test issues a server-global cache clear, so it must be alone on the
server. That is the same pairing 03272_prewarm_mark_cache_add_column.sql
uses for the mark cache.

Validation: with the cache bound lowered to 40 and pre-filled to 40/40
occupancy (measured via IcebergMetadataFilesCacheFiles), the old form
fails with a stdout diff byte-identical to the CI failure and the new
form passes on that same injected state. 50/50 randomized runs pass.
The only other tests using iceberg_metadata_staleness_ms, 04214 and
04337, are not exposed to residual occupancy and are unchanged.

Failed 7 times on Stateless tests (arm_asan_ubsan, targeted) across two
unrelated PRs, ClickHouse#113010 and ClickHouse#112866.
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 The AST fuzzer (amd_debug) failure (report) is unrelated to this PR. The crashing query is

SELECT DISTINCT id FROM merge(currentDatabase(), '^03402_data__fuzz_1$') ORDER BY ALL DESC NULLS FIRST LIMIT true
SETTINGS max_streams_to_max_threads_ratio = 2147483648, max_threads = 4

and the mechanism never touches Field comparisons: the planner computes max_streams = max_threads * max_streams_to_max_threads_ratio = 2^33, StorageMerge::createPlanForTable truncates it with an ancient UInt32(streams_num) cast when calling storage->read — 2^33 mod 2^32 = 0 — and the child ReadFromMergeTree then divides by zero in spreadMarkRangesAmongStreamsWithOrder ((info.sum_marks - 1) / num_streams). Reproduced locally on a debug build via gdb: ReadFromMerge receives num_streams = 4294967296 and the child ReadFromMergeTree receives 0. I am opening a separate PR that removes the truncating cast and adds a regression test, and will link it here.

The 04329_iceberg_insert_stale_schema_field_id_107268 failure on the targeted job is the known cache-warmth flake — its fix #113232 merged to master today and I have merged master into this branch, so the next CI run picks it up.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 The fix for the unrelated AST fuzzer crash (division by zero via UInt32 stream-count truncation in StorageMerge) is up: #113382

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Aug 5, 2026
Merged via the queue into master with commit 38768fd Aug 5, 2026
180 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-field-float64-le branch August 5, 2026 14:40
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 5, 2026
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Aug 5, 2026
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Aug 5, 2026
clickhouse-gh Bot pushed a commit that referenced this pull request Aug 5, 2026
clickhouse-gh Bot added a commit that referenced this pull request Aug 5, 2026
Backport #113010 to 26.7: Fix `Field::operator<=` comparing a `Float64` with itself
PedroTadim added a commit that referenced this pull request Aug 6, 2026
Backport #113010 to 26.6: Fix `Field::operator<=` comparing a `Float64` with itself
PedroTadim added a commit that referenced this pull request Aug 6, 2026
Backport #113010 to 26.5: Fix `Field::operator<=` comparing a `Float64` with itself
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Aug 24, 2026
andybradshaw pushed a commit to andybradshaw/ClickHouse that referenced this pull request Aug 25, 2026
With `max_threads = 4` and `max_streams_to_max_threads_ratio = 1073741824`,
the planner computes `max_streams = 2^32`, and the `UInt32(streams_num)` cast
in `ReadFromMerge::createPlanForTable` truncated it to 0. The child
`ReadFromMergeTree` then divided by zero in
`spreadMarkRangesAmongStreamsWithOrder` (`(info.sum_marks - 1) / num_streams`),
crashing debug and sanitizer builds (release builds eliminate the dead
division). `IStorage::read` takes `size_t num_streams`, so the cast was a
pointless leftover.

Found by the AST fuzzer on an unrelated PR:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=113010&sha=59507af89c33f62942e855b50b3e66704cbcf2e9&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%29
Related PR: ClickHouse#113010

Co-Authored-By: Claude Fable 5 <[email protected]>
VighneshPath pushed a commit to VighneshPath/ClickHouse that referenced this pull request Sep 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR 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 v26.4-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Field::operator<= and Field::operator>= compare a Float64 with itself, so both always return true

7 participants