Skip to content

Fix FINAL returning a superseded row with a skip index on a float primary key column - #126118

Merged
alexey-milovidov merged 5 commits into
ClickHouse:masterfrom
groeneai:groeneai/skip-index-final-float-primary-key
Oct 19, 2026
Merged

alexey-milovidov merged 5 commits into
ClickHouse:masterfrom
groeneai:groeneai/skip-index-final-float-primary-key

Conversation

@groeneai

@groeneai groeneai commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #122346
Related: #93899

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fixed SELECT ... FINAL returning stale rows when a data-skipping index is on a floating-point primary key column and one key has versions with 0.0 and -0.0, or with different NaN payloads. Closes #122346.

Description

On 26.8.18.2 (also 26.3.42.3 and 26.9.16.11), with ReplacingMergeTree(v) ORDER BY (id, f), f Float64, INDEX ix_f f TYPE set(0) and the rows (1, 0.0, 1) and (1, -0.0, 2) in two parts, SELECT * FROM t FINAL WHERE toString(f) = '0' returns the superseded row 1 0 1 instead of nothing. Two NaN payloads of one key give the same result.

#93899 skips the exact-mode recovery pass (PrimaryKeyExpand) when every column of every useful skip index is in the primary key, because the rows of one FINAL group have equal key values. That is not true for floats: FINAL merges -0.0 with 0.0, and NaN payloads with each other, while a skip index keeps them apart. The index drops the granule with the winning row, keeps the one with the old row, and the winner never reaches the merge.

areAllSkipIndexColumnsInPrimaryKey now builds its set of primary key columns with sortingKeyNamesSafeBeforeFinal, which already excludes float columns for the same reason in deferred row policies (#121782). Indexes on non-float key columns keep the shortcut.

Cost: the recovery pass cannot order float keys (nor Nullable or reverse-order keys), so for them it read the whole surviving parts; it now reads the ranges selected by the primary key, as use_skip_indexes = 0 does. On 2 parts of 1M rows with a pruning set index on f: WHERE f = 3/7 reads 2,000,000 rows instead of 212,992 on master, the same as use_skip_indexes = 0; WHERE id BETWEEN 300000 AND 310000 AND f = 3/7 reads 32,768 rows on master and with the fix. With the index on a non-key column instead, that query reads 32,768 rows instead of 2,000,000 on master.

Test 05401_skip_index_final_float_primary_key fails on master and passes with the fix, including 50 randomized runs.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.11.1.178-master (included in 26.11 and later)

groeneai and others added 3 commits October 15, 2026 18:20
…mary key column

`buildIndexes` turns `use_skip_indexes_if_final_exact_mode` off when every
column of every useful skip index is in the primary key (ClickHouse#93899): rows of one
FINAL group have equal key values, so such an index cannot drop the winner of
a group while keeping an older version.

For floats that premise is false. The FINAL merge treats -0.0 and 0.0, and all
NaN payloads, as one key, while a skip index stores the actual values. With
rows (1, 0.0, 1) and (1, -0.0, 2) in two parts and `INDEX ix_f f TYPE set(0)`
on key column `f`, `FINAL WHERE toString(f) = '0'` drops the granule with the
winner and returns the superseded row; SummingMergeTree returns a partial sum.

Build the set of primary key columns with `sortingKeyNamesSafeBeforeFinal`,
which excludes float-containing columns for the same reason in deferred row
policies (ClickHouse#121782). A skip index on a float key column now keeps the exact-mode
recovery pass on; for such keys it reads the whole surviving parts, as it
already does for an index on a non-key column of these tables. Indexes on
non-float key columns keep the shortcut.

Closes ClickHouse#122346

Co-Authored-By: Claude Opus 5.5 <[email protected]>
For a primary key the recovery pass cannot sort (floats, Nullable, Variant,
reverse order), and for parts without a final mark,
`findPKRangesForFinalAfterSkipIndex` returned every surviving part in full,
discarding the primary key analysis. With a selective key prefix, FINAL then
read more with the skip index than without it. The previous commit enables the
pass for skip indexes on float key columns, so those queries hit it too.

Return the ranges selected by the primary key instead. They are captured for
every part in exact mode and are what FINAL reads with `use_skip_indexes = 0`,
so this adds no correctness assumption; the sortable branch already restricts
its candidates to the same ranges.

On 2 parts of 1M rows with `ORDER BY (id, f)`,
`WHERE id BETWEEN 300000 AND 310000 AND f = 3/7` reads 32,768 rows instead of
2,000,000, both for a set index on `f` and for one on a non-key column.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`force_data_skipping_indices` only checks that the index is chosen for the
query; an index that is declined per part, or prunes nothing, leaves the
ranges unchanged without an error. The signed-zero and NaN oracles expect an
empty result, which is also what any binary returns when the index prunes
nothing, so each float fixture now asserts with `EXPLAIN indexes = 1` that
`ix_f` drops one of its two granules (`Granules: 1/2`). The read_rows check
also requires both query_log rows to be present.

The fallback of the FINAL exact-mode recovery throws LOGICAL_ERROR when a
part has no ranges selected by the primary key, like the sortable branch,
instead of silently reading the whole part (the snapshot is always set when
exact mode is on). The `_no_final_mark` test comment describes the new
fallback.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review (click to expand)

Round r0 (head 0df0eb902099c), bounced to a fix round:

  • ⚠️ AGREE: for float primary keys the exact-mode recovery read whole parts and dropped the primary key pruning, so a FINAL query with a selective key prefix read more than use_skip_indexes = 0. Fix round: the fallback keeps the ranges the primary key selected.
  • ⚠️ AGREE (in part): the test oracles now force the index they depend on (force_data_skipping_indices). DISAGREE on a separate SummingMergeTree fixture: the decision does not depend on the table engine; the changelog entry no longer names an engine.

Round r1 (head a4dd75ffeebc9), bounced to a fix round:

  • ⚠️ AGREE: force_data_skipping_indices only checks that the index is applicable, not that it pruned, so each float fixture now asserts that ix_f drops one of its two granules.
  • 💡 AGREE (nits): the fallback throws the same LOGICAL_ERROR as the sortable branch on a missing primary key snapshot; the read_rows check requires both log rows; the no-final-mark test comment describes the new fallback.

Round r2 (head 1386de4359b96): no findings from my own review or the second model; handed to publication.

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. Two parts (1, 0.0, 1) / (1, -0.0, 2) (or two NaN payloads) with INDEX ix_f f TYPE set(0) on key column f: FINAL WHERE toString(f) = '0' returns 1 0 1 on every run on master and on 26.3.42.3, 26.8.18.2, 26.9.16.11.
b Root cause explained? #93899 turns use_skip_indexes_if_final_exact_mode off when all skip-index columns are in the primary key, assuming equal key values inside a FINAL group. FINAL merges -0.0/0.0 and NaN payloads as one key while the index keeps them apart, so the index drops the winner's granule and the recovery pass that would restore it is skipped.
c Fix matches root cause? Yes. The shortcut's set of primary key columns now excludes float columns via sortingKeyNamesSafeBeforeFinal, the helper already used for the same property by deferred row policies. No guard at the read site.
d Test intent preserved / new tests added? New test 05401_skip_index_final_float_primary_key: signed zeros with a positive control, NaN payloads, an EXPLAIN check that PrimaryKeyExpand runs for the float column and still does not run for an index on an integer key column, and a system.query_log check that FINAL with the index reads the same rows as use_skip_indexes = 0. Each oracle query forces its index with force_data_skipping_indices, and each float fixture first checks with EXPLAIN indexes = 1 that ix_f really drops one of its two granules (Granules: 1/2), since the force setting only checks that the index is chosen. Only one existing test changed: a comment in 03244_..._no_final_mark now describes the new fallback.
e Both directions demonstrated? Master FAIL, the fixed tree with the ReadFromMergeTree.cpp hunk reverted FAIL (stale rows), the fixed tree with only the PartsSplitter.cpp hunk reverted FAIL on the read check (20,000 rows vs 256), fixed OK 50/50 with randomized settings. With ix_f left unmaterialized (exclude_materialize_skip_indexes_on_insert), both Granules: 1/2 checks print 0 while force_data_skipping_indices still accepts the queries. Server Build-IDs checked before each arm.
f Fix is general across code paths? The other pre-FINAL decisions with the same premise (partition pruning, deferred row policy and PREWHERE atoms, PREWHERE before FINAL) already exclude floats. The recovery pass for a key it cannot sort compares no values; it now returns the ranges selected by the primary key in all three of its fallbacks (unsortable key, missing final mark, undefined end mark) instead of whole parts, which also covers indexes on non-key columns of such tables. Index type and engine agnostic.
g Fix generalizes across inputs (params/datatypes/wrappers)? Checked on both binaries: Float32, BFloat16, Nullable(Float64), a multi-column (id, f) index, bloom_filter with NaN, and SummingMergeTree (master returns a partial sum). All wrong on master, all correct with the fix.
h Backward compatible? (maintainer-approved exception only) Yes. No setting, format or on-disk change; the change can only keep the existing recovery pass on, never turn it off.
i Invariants and contracts preserved? A filter decision taken before the FINAL merge must be the same for every row of one group; this now holds for skip indexes on float key columns. Every query that used the recovery pass before still does, and a part without its primary key selection is a LOGICAL_ERROR in every branch of that pass. Its fallback reads exactly what FINAL reads with use_skip_indexes = 0: on 2 parts of 1M rows, WHERE f = 3/7 reads 2,000,000 rows (master 212,992), and WHERE id BETWEEN 300000 AND 310000 AND f = 3/7 reads 32,768 (master 32,768, a whole-part read 2,000,000).

Session id: cron:clickhouse-impl-slot-5:20261015-195000

@clickhouse-gh

clickhouse-gh Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [1386de4]

Summary: ✅


AI Review

Summary

PR #126118 preserves exact-mode recovery for skip indexes on floating-point primary-key columns, preventing stale FINAL results for signed zeros and distinct NaN payloads. Recovery for unsortable keys and parts without a final mark now retains primary-key pruning. No actionable findings at commit 1386de4359b9686d30258cb768202ddaef6284ac.

Missing context / blind spots
  • ⚠️ Broader CI bugfix-validation jobs were dropped after Config Workflow. The new regression test passed in Fast test; baseline failures and repeated fixed runs are documented by the author, but were not independently rerun locally.
Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Oct 15, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @shankar-iyer @yariks5s, could you review this? The #93899 shortcut that skips the FINAL exact-mode recovery pass now ignores float primary key columns (via sortingKeyNamesSafeBeforeFinal), because FINAL merges 0.0/-0.0 and NaN payloads that a skip index keeps apart. For keys the pass cannot sort, its fallback now reads the ranges selected by the primary key instead of whole parts.

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Oct 15, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. label Oct 15, 2026

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ok.

@alexey-milovidov alexey-milovidov self-assigned this Oct 18, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 18, 2026
alexey-milovidov and others added 2 commits October 19, 2026 05:51
…nal mark

The skip indexes of the existing table are on primary key columns, so the
`findPKRangesForFinalAfterSkipIndex` pass is skipped there since ClickHouse#93899 and
the comment-only change of this test made the bugfix validation job red.
Add a table with a skip index on a column outside of the primary key: the
pass now runs, meets the part without the final mark and must keep the
ranges selected by the primary key. On master it reads the whole part and
fails with `TOO_MANY_ROWS` under `max_rows_to_read = 1000`.

https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=126118&sha=1386de4359b9686d30258cb768202ddaef6284ac&name_0=PR&name_1=Bugfix%20validation%20(functional%20tests%2C%20amd64)
ClickHouse#126118

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 The only red check on 1386de4 was Bugfix validation: the comment-only edit made 03244_skip_index_in_final_query_with_pk_rescan_no_final_mark count as a changed test, and it passes on master. Its skip indexes are all on primary key columns, so the exact-mode pass is skipped there anyway since #93899. ae464e2 adds a table with a skip index on a column outside the primary key. With that, the pass runs, reaches the part without the final mark, and has to keep the ranges the primary key selected. On master that query reads the whole part and fails with TOO_MANY_ROWS under max_rows_to_read = 1000; with this PR it returns 1. I ran it locally against both builds.

Merged via the queue into ClickHouse:master with commit eb2b999 Oct 19, 2026
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 19, 2026
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 3, 2026
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 3, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 3, 2026
Backport #126118 to 26.10: Fix FINAL returning a superseded row with a skip index on a float primary key column
leshikus pushed a commit that referenced this pull request Nov 13, 2026
…a skip index on a float primary key column
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.10-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FINAL returns stale row when skip index sits on a Float64 primary-key column

5 participants