Skip to content

Cherry pick #101494 to 26.2: Defer dedup data hash computation to eliminate squashing overhead - #101734

Merged
CheSema merged 23 commits into
backport/26.2/101494from
cherrypick/26.2/101494
Apr 7, 2026
Merged

CheSema merged 23 commits into
backport/26.2/101494from
cherrypick/26.2/101494

Conversation

@robot-ch-test-poll4

Copy link
Copy Markdown
Contributor

Original pull-request #101494

Do not merge this PR manually

This pull-request is a first step of an automated backporting.
It contains changes similar to calling git cherry-pick locally.
If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.

The check results does not matter at this step - you can safely ignore them.

Troubleshooting

If the conflicts were resolved in a wrong way

If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

CheSema and others added 22 commits April 1, 2026 14:03
…ng squashing

During INSERT with `deduplicate_insert=enable`, `calculateDataHash` was called
eagerly in `updateOriginalBlock` on every squashing step. For 10M rows this
meant ~17 intermediate hash computations (each O(rows * columns) with SipHash),
all discarded except the last. This caused ~1.7s overhead (2.06s vs 0.35s for
Engine=Null, 2.27s vs 0.67s for MergeTree).

The fix defers hash computation: `updateOriginalBlock` stores the block with
COW-shared columns instead of computing the hash eagerly and discarding.
The hash is computed lazily on first access in the sink via `calculateDataHash`,
which caches the result and then releases the block columns.

After the fix, dedup ON/OFF performance is identical (0.19s/0.36s Null,
0.66s/0.66s MergeTree for 10M rows, 13 columns).

Ref: #99241

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Adds a performance test comparing INSERT with `deduplicate_insert=enable`
vs `disable` on both Engine=Null (isolates hash overhead) and MergeTree
(combined overhead with part writes). Uses 5M rows, 13 columns, INSERT
SELECT with ORDER BY ALL to make the query deduplication-eligible.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Prevents parts from piling up across iterations, keeping each run
independent.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Use rand() in one column so each run produces unique data hashes,
preventing the dedup log from suppressing writes on subsequent
iterations. Also truncate MergeTree table between runs to avoid
accumulating parts.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Without non_replicated_deduplication_window the dedup log is inactive,
so getDeduplicationHashes returns empty and the hash is never computed —
making the MergeTree benchmark meaningless. Also switch to ORDER BY ()
to isolate dedup overhead from sorting cost.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Add `IColumn::updateHashWithValueRange` virtual method that hashes a
range of rows in one call. The default implementation loops row-by-row;
overrides in `ColumnVector`, `ColumnDecimal`, `ColumnFixedString`,
`ColumnString`, `ColumnArray`, `ColumnNullable`, `ColumnTuple`, and
`ColumnMap` feed contiguous memory to SipHash, eliminating per-row
virtual dispatch.

`InsertDeduplication` now uses `calculateDataHashBatch` (calling the new
method) for the unified hash path. The old row-by-row path is kept for
backward-compatible hashes during the `COMPATIBLE_DOUBLE_HASHES` period.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Add Decimal64, Float64, DateTime64, FixedString, String, Array,
Nullable, Tuple, and Map columns to exercise all `updateHashWithValueRange`
overrides. Add a query with `insert_deduplication_token` to show zero
hash overhead when a user token is set.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Add tables for Decimal64, Float64, FixedString, String, Array,
Nullable, Tuple, Map, and a mixed-types table to
`03711_deduplication_blocks_part_log`. This verifies that
`updateHashWithValueRange` batch hashing produces stable deduplication
hashes for every overridden column type.

Update reference hashes: the unified hash changed for tables containing
String and other variable-length columns because batch hashing feeds
data differently to SipHash. The old (row-by-row) hash is unchanged.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Add async insert tables for UInt32, String, Array, and mixed types to
`03711_deduplication_blocks_part_log`. Async inserts compute data hash
directly from the block via `calculateDataHashBatch`, exercising the
batch hashing path independently from the sync MergeTreePartWriter path.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Co-authored-by: clickhouse-gh[bot] <173705893+clickhouse-gh[bot]@users.noreply.github.com>
`doExtend` resets `data_hash` when the token range changes but missed
resetting `data_hash_batch`, which could leave a stale cached value
for the unified hash path.

#101494 (comment)

Co-Authored-By: Claude Opus 4.6 <[email protected]>
The comment said `calculateDataHash` releases columns, but after
refactoring the release moved to `getDeduplicationHashes`.

#101494 (comment)

Co-Authored-By: Claude Opus 4.6 <[email protected]>
…ults

`calculateDataHash` → `calculateDataHashRowWise` (row-major iteration)
`calculateDataHashBatch` → `calculateDataHashColumnWise` (column-major iteration)

The naming makes it obvious that these are two distinct hash algorithms
producing different values, not just different implementations of the same hash.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
…heck

The flaky check randomizes MergeTree settings, which changes serialized
bytes and deduplication hashes. Pin all serialization-affecting settings
at the table level:

- `index_granularity = 8192` on all tables (compact parts interleave
  columns per-granule, so different boundaries change the hash)
- `dynamic_serialization_version = 'v2'` on Dynamic table
- `object_serialization_version`, `object_shared_data_serialization_version`,
  `object_shared_data_serialization_version_for_zero_level_parts` on JSON tables

Also qualify all table names with `03711_database.` prefix — the flaky
check uses a randomized default database, so unqualified names landed
in the wrong database and the `part_log` query returned empty results.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
…head

Defer dedup data hash computation to eliminate squashing overhead
@robot-ch-test-poll4 robot-ch-test-poll4 added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request labels Apr 3, 2026
@robot-ch-test-poll3

Copy link
Copy Markdown
Contributor

Dear @Avogar, @CheSema, this cherry-pick PR has not been updated for 3d0h3m8s. Please resolve the conflicts to backport #101494, or close this PR if the backport is no longer needed. This PR will be automatically closed after 7 days of inactivity.

@alexey-milovidov

Copy link
Copy Markdown
Member

The Stress test (arm_msan) failure is fixed by #101239, which should be merged first. After it is merged, please update the branch to include the fix.

@CheSema
CheSema merged this pull request into backport/26.2/101494 Apr 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants