Skip to content

Count patch part reads in the AutoPR input bytes - #133609

Draft
nickitat wants to merge 10 commits into
autopr-count-text-index-readsfrom
autopr-count-patch-reads
Draft

nickitat wants to merge 10 commits into
autopr-count-text-index-readsfrom
autopr-count-patch-reads

Conversation

@nickitat

@nickitat nickitat commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Related: #127586
Related: #133839

Changelog category (leave one):

  • Improvement

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

The input bytes automatic parallel replicas measure now include what the query reads from patch parts of lightweight updates, so a query whose unmerged patches dominate its read is no longer under-priced.


Stacked on #133839 (text index reads), on #133777 (the limit on the registered ranges), on #127586 (the input bytes measured as the compressed blocks the readers load); the base of this PR is the branch of #133839, so the diff shows only the patch parts.

The patch readers of a read task got no ReadRangesRegistry and never registered what they read, so the bytes read from patch parts did not reach the input or the duplicated bytes. The MergeTreeReadTask now gives the registry to the patch readers with the main and PREWHERE readers, and MergeTreeReadersChain::readPatches calls registerReadRanges of each one after it reads. A replica reading the same granules applies the same patches, so they are counted with the read. Patches applied in Join mode go through the PatchJoinCache shared within the read; the reader that loads a range counts it, once.

05840_autopr_input_bytes_patch_parts: an unmerged UPDATE replaces 100000 empty strings with payloads that hardly compress. The statistics record 3.5 MB with the change and 110 KB without it.

🤖 Generated with Claude Code


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

nickitat and others added 2 commits November 22, 2026 21:36
`modernize-use-nodiscard` flags them in every translation unit that includes the header. The PR changed the
header, so the clang-tidy cache no longer hid them.

https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=127586&sha=141c98b0e47f6cb31f8858d2d53c64949e2e624a&name_0=PR
#127586

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The patch readers of a read task got no `CountedCompressedBlocks` and were never flushed, so the bytes of patch parts
were missing from the input bytes of the statistics: an unmerged lightweight update could dominate the read while the
statistics described only the base part. A replica reading the same granules applies the same patches, so they are
counted with the read.

Related: #127586

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

clickhouse-gh Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [7c93f25]

Summary: ❌

job_name test_name status info comment
Stateless tests (arm_binary, ParallelReplicas, plan-based, s3 storage, parallel) FAIL
05182_compress_per_substream_in_compact_parts FAIL cidb
05452_autopr_subcolumn_input_bytes FAIL cidb
05059_promql_empty_by_group NOT_FAILED cidb
05211_promql_present_over_time_absent_over_time NOT_FAILED cidb
04836_time_series_v10_selector_whole_metric_pk_range NOT_FAILED cidb
05350_comparison_tuple_elimination_shared_alias NOT_FAILED cidb
05234_promql_result_timestamp_type NOT_FAILED cidb
05033_lazy_materialization_column_function_structure_equals NOT_FAILED cidb
05273_promql_instant_vector_sorted_by_tags NOT_FAILED cidb
05241_prometheus_query_timestamp NOT_FAILED cidb
19 more test cases not shown
La Casa Del Dolor (amd_debug) FAIL
Server shutdown FAIL cidb

AI Review

Summary

Adds patch-part reads to the compressed-byte statistics used by automatic parallel replicas. Reviewed the full diff and current reader, range-selection, cache, and cost-model paths at 7c93f2537a80b4f8bae418561f5b667551dc2166. One existing Major remains: patch work repeated by every replica is counted as work that replicas divide among themselves. No new inline findings; the existing thread remains open.

Missing context / blind spots
  • ⚠️ Current-head functional tests are pending in the Praktika report. Passing results for 05840_autopr_input_bytes_patch_parts would establish execution coverage for the added test. Local execution was unavailable in this review job.
Findings

⚠️ Majors

  • src/Storages/MergeTree/MergeTreeReadTask.cpp:128–129: Repeated patch loads are incorrectly classified as divisible input. The coordinated read's registry places all patch bytes in input_bytes. For v2 patches on a table with ORDER BY tuple(), getRangesInPatchPartMergeOnKey selects the entire patch for every nonempty task, and MergeTreePatchReaderMergeOnKey::needNewPatch consumes every range. Each replica therefore reads the full patch, while ReadRangesRegistry counts its bytes once and decideAtBoundary divides them by the replica count. Join patches have the same problem when their statistics cannot prune the shared ranges; PatchJoinCache is local to each read pool. Patch-dominated reads can consequently enable replicas on nonexistent read savings and evade automatic_parallel_replicas_max_duplicated_read_ratio. Account for this work as duplicated, or decline automatic pricing when it cannot be split. This is the existing unresolved finding, verified against the current code.
Tests
  • ⚠️ 05840_autopr_input_bytes_patch_parts uses a unique sorting key and asserts only an input-byte lower bound. Add a focused stateless case with a patch-dominated ORDER BY tuple() table or an overlapping Join patch, and verify that automatic selection accounts for duplicated patch work or declines the plan. The current assertion passes with the misclassification above.
Final Verdict

Status: ⚠️ Request changes

Correct the accounting of patch work repeated across replicas, or exclude those cases from automatic pricing, and cover that decision with a focused regression test.

LLVM Coverage Report

Measured on commit 7c93f25.

Metric Baseline Current Δ
Lines 91.90% 91.90% +0.00%
Functions 92.20% 92.20% +0.00%
Branches 81.60% 81.60% +0.00%

Changed lines: Changed C/C++ lines covered: 31/33 (93.94%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Nov 22, 2026
@nickitat
nickitat changed the base branch from master to fix-stats-for-string-filters November 22, 2026 22:05
@clickhouse-gh clickhouse-gh Bot added the comp-parallel-replicas Parallel reading from replicas for query speed (not data replication consistency). label Nov 22, 2026
reader->countCompressedBytesRead(counted);
/// A replica reading the same granules reads their patches too.
for (auto & patch : patches)
patch->getReader()->countCompressedBytesRead(counted);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[Major] Keep replicated patch loads out of splittable input bytes

counted assigns these bytes to the coordinated read's input_bytes, which decideAtBoundary divides by the replica count. Patch reads do not always split with the base granules: for a v2 table with ORDER BY tuple(), getRangesInPatchPartMergeOnKey returns the entire patch for every nonempty task, and MergeTreePatchReaderMergeOnKey::needNewPatch consumes all its ranges. Each replica therefore loads the full patch, while CountedCompressedBlocks records one copy and the model prices it as patch_bytes / num_replicas. Legacy Join patches have the same issue when their min/max statistics cannot prune the shared ranges; PatchJoinCache is local to each read pool.

For patch-dominated reads this can enable replicas based on nonexistent savings and bypass automatic_parallel_replicas_max_duplicated_read_ratio. Please account for nonsplittable patch work as duplicated bytes, or decline automatic pricing for these cases. Add a focused stateless regression with a constant sorting key or a Join patch; the added test's unique sorting key and input-byte lower bound do not exercise this distinction.

@nickitat
nickitat marked this pull request as draft November 22, 2026 22:35
@clickhouse-gh

clickhouse-gh Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 7c93f2537 with master 2b066904b (stripped binary size, per-symbol sizes and ThinLTO time; 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.

Object file sizes

121 object files changed (+63.62 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Storages/MergeTree/MergeTreeReaderStream.cpp.o 175.88 KiB 232.59 KiB +56.71 KiB (+32.25%)
src/CMakeFiles/dbms.dir/Storages/MergeTree/MergeTreeReadTask.cpp.o 296.85 KiB 257.85 KiB -39.00 KiB (-13.14%)
src/CMakeFiles/dbms.dir/Storages/MergeTree/MergeTreeReaderWide.cpp.o 464.24 KiB 486.77 KiB +22.53 KiB (+4.85%)

716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

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

Job report

…reads

# Conflicts:
#	src/Storages/MergeTree/MergeTreeReadTask.cpp
@nickitat
nickitat changed the base branch from fix-stats-for-string-filters to autopr-count-text-index-reads November 23, 2026 21:50
@nickitat
nickitat added this pull request to stack #133889 November 23, 2026 21:51
@nickitat
nickitat removed this pull request from stack #133889 November 23, 2026 21:54
@nickitat
nickitat added this pull request to stack #133890 November 23, 2026 21:54
@nickitat nickitat added the ci-force-all Do not skip any jobs when running label Nov 23, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci-force-all Do not skip any jobs when running comp-parallel-replicas Parallel reading from replicas for query speed (not data replication consistency). pr-improvement Pull request with some product improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant