Skip to content

Do not execute the PrimaryKeyExpand step if skip index is already part of primary key - #93899

Merged
rschu1ze merged 4 commits into
ClickHouse:masterfrom
shankar-iyer:fix_skip_index_column_part_of_pk
Jan 20, 2026
Merged

rschu1ze merged 4 commits into
ClickHouse:masterfrom
shankar-iyer:fix_skip_index_column_part_of_pk

Conversation

@shankar-iyer

@shankar-iyer shankar-iyer commented Jan 12, 2026 •

Copy link
Copy Markdown
Member

Reverts #89395
Re-reverts #88368 with extra fortification

Details

Assume a ReplacingMergeTree table with ORDER BY (id1, id2) and two skip indexes: id2_idx on id2 (which is part of the primary key) and v_idx on v (which is not part of primary key).

With rows (1,1,'blue') and (1,1,'red') in different parts, the second row is a modification of the first row. Using skip index v_idx in a FINAL query with WHERE v = 'blue' requires the PrimaryKeyExpand step (use_skip_indexes_if_final_exact_mode=1) to return correct results.

With rows (1,1,'blue') and (1,2,'blue') in different parts, the second row is a different row. Skip index id2_idx can be safely used without needing the primary key intersection step for a FINAL query with e.g WHERE id2 = 2

Changelog category (leave one):

  • Improvement

Changelog entry (a [user-readable short description]

If a skip index used in a FINAL query is on a column that is part of the primary key, the additional step to check for primary key intersection in other parts is unnecessary and now not performed. Resolves #85897

Version info

  • Merged into: 26.1.1.711
  • Backported to: 25.12.5.19

@shankar-iyer
shankar-iyer marked this pull request as draft January 12, 2026 10:29
@clickhouse-gh

clickhouse-gh Bot commented Jan 12, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [919f2ce]

Summary: ❌

job_name test_name status info comment
BuzzHouse (amd_debug) failure
Logical error: 'Inconsistent AST formatting: the query: (STID: 1941-1bfa) FAIL cidb, issue ISSUE EXISTS

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Jan 12, 2026
@shankar-iyer

Copy link
Copy Markdown
Member Author

Clean CI run at a49b9f3

@shankar-iyer
shankar-iyer marked this pull request as ready for review January 13, 2026 04:46
@rschu1ze rschu1ze self-assigned this Jan 14, 2026
@rschu1ze
rschu1ze added this pull request to the merge queue Jan 20, 2026
Merged via the queue into ClickHouse:master with commit 210ebef Jan 20, 2026
127 of 128 checks passed
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jan 20, 2026
@robot-ch-test-poll1 robot-ch-test-poll1 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Jan 23, 2026
robot-clickhouse added a commit that referenced this pull request Jan 26, 2026
… skip index is already part of primary key
@robot-ch-test-poll robot-ch-test-poll added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Jan 26, 2026
shankar-iyer added a commit that referenced this pull request Jan 26, 2026
Backport #93899 to 25.12: Do not execute the PrimaryKeyExpand step if skip index is already part of primary key
@Algunenano

Copy link
Copy Markdown
Member

Please beware that this is introducing a bug:

-- Reproduces a bug where mixed PK and non-PK minmax skip indexes break FINAL queries.
--
-- The minmax index on `sign` evaluates the `sign = 1` filter and drops the part
-- containing the delete marker (sign = -1). The recovery mechanism
-- (`findPKRangesForFinalAfterSkipIndex`) is disabled because the minmax index on
-- `key` (a PK column) is also useful, causing `areSkipIndexColumnsInPrimaryKey`
-- to return true.

DROP TABLE IF EXISTS t_final_minmax;

CREATE TABLE t_final_minmax
(
    key Int32,
    value String,
    sign Int8,
    ver UInt64,
    INDEX idx_key key TYPE minmax,
    INDEX idx_sign sign TYPE minmax
)
ENGINE = ReplacingMergeTree(ver)
PARTITION BY key
ORDER BY key;

SYSTEM STOP MERGES t_final_minmax;

-- Part 1: original row
INSERT INTO t_final_minmax VALUES (2, 'original', 1, 1);

-- Part 2: delete marker (higher version, sign = -1)
INSERT INTO t_final_minmax VALUES (2, '', -1, 2);

-- FINAL should keep only the row with highest version (ver=2, sign=-1).
-- Then sign = 1 filter should exclude it, returning empty result.
-- Bug: idx_sign drops the delete-marker part, and the recovery is disabled
-- because idx_key (on a PK column) is also useful.
SELECT value FROM t_final_minmax FINAL WHERE key = 2 AND sign = 1;

DROP TABLE t_final_minmax;

I'll send a possible fix, but please keep in in mind for any release where this change, or anything related/superseeded by it, was backported

@shankar-iyer

Copy link
Copy Markdown
Member Author

Thanks @Algunenano !

@KochetovNicolai KochetovNicolai added the post-approved Approved, but after the PR is merged. label Mar 13, 2026
alexey-milovidov added a commit to groeneai/ClickHouse that referenced this pull request Oct 19, 2026
…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]>
UberDever pushed a commit to UberDever/ClickHouse that referenced this pull request Oct 19, 2026
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

post-approved Approved, but after the PR is merged. pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-improvement Pull request with some product improvements 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 v25.12-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

use_skip_indexes_if_final_exact_mode incorrectly applied for skip index over ORDER BY column FINAL

7 participants