Skip to content

Check the top-K threshold again when delivering a Parquet row group - #131143

Merged
alexey-milovidov merged 7 commits into
masterfrom
parquet-columns-cache-top-k-skip
Nov 18, 2026
Merged

alexey-milovidov merged 7 commits into
masterfrom
parquet-columns-cache-top-k-skip

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

TopN dynamic filtering checks a Parquet row group against the top-K threshold before reading it. A row group served from the columns cache is ready almost at once, so all of them pass that check while the sorting transforms are still sorting the first chunks and the threshold is still loose. With the cache, ParquetTopKSkippedRowGroups dropped from about 160 to 3 of 209 row groups, and the query sorted about five times more rows.

Now the threshold is checked once more in ReadManager::read when a chunk is delivered: by then it reflects everything delivered before, and the check costs nothing. Skipping a row group whose statistics bound is beyond the threshold is valid at any moment, since the threshold only tightens; the top-K query condition cache stays correct (such a row group holds no row of the result).

This was found by the performance comparison of #123890 (enabling the columns cache by default): parquet_top_k_threshold_streams was 4x slower. Measured locally on that query: without the cache 0.019-0.025 s; with the cache, before 0.037 s, after 0.011-0.015 s (ParquetTopKSkippedRowGroups 158-189 again).

The new test 05629_parquet_top_k_columns_cache checks that the results with the cache, cold and warm, equal those without it, with ties on the sort key.

Performance report: https://performance.ci.clickhouse.com/runs/pr-123890-0f4e0e78cb42e31fef159ff3e74a0eec321c772a

Related: #123890

Changelog category (leave one):

  • Performance Improvement

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

ORDER BY ... LIMIT over Parquet files skips row groups by the top-K threshold also when they are served from the columns cache.

🤖 Generated with Claude Code


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

Version info

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

alexey-milovidov and others added 2 commits November 12, 2026 21:05
TopN dynamic filtering checks a row group against the threshold before reading
it. A row group served from the columns cache is ready almost at once, so all of
them pass that check while the sorting transforms are still sorting the first
chunks and the threshold is loose: with the cache, `ParquetTopKSkippedRowGroups`
dropped from about 160 to 3 of 209 row groups and the query read five times more
rows. Check the threshold once more when delivering a chunk: by then it reflects
everything delivered before, and the check costs nothing.

https://performance.ci.clickhouse.com/runs/pr-123890-0f4e0e78cb42e31fef159ff3e74a0eec321c772a
#123890

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

clickhouse-gh Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [969dedc]

Summary: ✅


AI Review

Summary

Reviewed PR #131143 at 969dedc33ce5fb7465d60ee5aa5ba72cef48ac28. The change rechecks the TopK threshold before delivering Parquet chunks and excludes skipped row groups from threshold-dependent query condition cache matches. The previous warm-cache test coverage finding is addressed. The supplied before/after measurements support the performance improvement; no unresolved actionable findings remain.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Nov 12, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-parquet-reader-v3 Issues related to the native Parquet reader. label Nov 12, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 969dedc33 with master 6c9930067 (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.

Compile time of recompiled translation units

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

Job report

alexey-milovidov and others added 2 commits November 14, 2026 11:56
Since the top-K threshold is checked again when a row group is delivered,
a row group can be skipped after its rows were read and counted in
`rows_pass`. `getMatchedBuckets` then reported it as matched, while it never
set `top_k_best_value`, so the TopK query condition cache entry kept it and
often ended up empty. With the columns cache this made
`05339_iceberg_top_k_query_condition_cache` fail (`where_path_2` found no entry).

A row group skipped by a threshold that also filters rows holds no row of the
result, so it is now unmatched, the same as one skipped before reading.

CI: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=131143&sha=d8fc36319742ac5c0c1c650e7134441fedf12626&name_0=PR&name_1=Stateless%20tests%20%28amd_debug%2C%20sequential%29
PR: #131143

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The file was read right after it was written, when its version token is not
settled yet, so the columns cache was not used at all. Rewrite the test in
shell, backdate the file with `touch`, populate the cache with both columns,
enable TopN dynamic filtering explicitly, and assert `ColumnsCacheHits` on the
warm reads.

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

Copy link
Copy Markdown
Member Author

🕵 CI status:

  • 05339_iceberg_top_k_query_condition_cache failed because of this PR (with use_columns_cache = 1 randomized). A row group skipped by top-K when it was delivered still counted as matched in getMatchedBuckets, so the TopK query condition cache entry came out empty. Fixed in fc12078, and I checked locally that the failure reproduces without the fix and goes away with it.
  • La Casa Del Dolor (amd_tsan): the data race in MultipleFileWriter::consume (Iceberg insert) is unrelated to this PR. It is being fixed in Fix data race in Iceberg insert with a text data file format #131515.

alexey-milovidov and others added 2 commits November 16, 2026 09:48
…tion

The columns cache is system-wide and only 100 MB in the test configuration.
The test wrote about 22 MB of columns into it and required every warm read to
hit the cache, but the tests running in parallel (and the concurrent copies of
this test in the flaky check) evict its entries between two queries, so one of
the warm reads sometimes got no hits at all.

Shrink the file tenfold (still 100 row groups, and the descending top-K still
crosses a tie boundary), and require only that at least one of the six warm
reads hits the cache. Every warm read caches what it misses, so this still
proves that the file is eligible for caching and that the cached delivery path
is exercised.

https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=131143&sha=2420e58313d0d06a643f80a29e3ab9e28dd191b7&name_0=PR&name_1=Stateless%20tests%20%28arm_asan_ubsan%2C%20targeted%29
PR: #131143

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

Copy link
Copy Markdown
Member Author

🕵 Merged master and fixed the flaky 05629_parquet_top_k_columns_cache (6cad89a). The test required every one of the six warm reads to hit the columns cache. That cache is system-wide and only 100 MB in the test config, while the test put about 22 MB into it. Parallel tests (and the concurrent copies in the flaky check) evicted its entries between queries, so asc_1 / desc_1 sometimes had zero hits. The file is now ten times smaller (still 100 row groups, and the descending top-K still crosses a tie boundary). The check now requires that at least one warm read hits the cache: every warm read caches what it misses, so this still proves the cached delivery path is exercised. The result comparisons are unchanged.

@clickhouse-gh clickhouse-gh Bot left a comment

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.

AI review approves this commit, see the AI Review comment.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 The only red job was Stateless tests (arm_asan_ubsan, azure, targeted), where 03714_paimon_partition_pruning and 05137_paimon_boolean_type failed. Both fail in that job on many unrelated PRs, and #131696 fixed them on master. I merged master (969dedc) to pick up that fix. Master did not touch the files this PR changes, and both changed translation units still compile.

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Acceptable.

@alexey-milovidov alexey-milovidov self-assigned this Nov 18, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Nov 18, 2026
Merged via the queue into master with commit b372bef Nov 18, 2026
194 checks passed
@alexey-milovidov
alexey-milovidov deleted the parquet-columns-cache-top-k-skip branch November 18, 2026 21:49
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 18, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Nov 19, 2026
…-tsa-annotations

Resolve the conflict with ClickHouse#131143 in ReadManager::read(): keep master's
loop that re-checks the top-K threshold before delivery, and apply this
branch's two edits to it (UniqueLock for delivery_mutex, and
delivery_cv.wait_for on lock.getUnderlyingLock()).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Nov 20, 2026
Since ClickHouse#131143 the reader checks the top-K threshold again when delivering a row
group, and counts a row group it stops there in `ParquetTopKSkippedRowGroups`
although its pages were read. So a read without the late check can now reach 38
skipped row groups, and "skipped plus read pages" is no longer 40 when such a
row group is counted (41-43 in 12 of 40 runs of the merged tree). The arms of
the late skip now wait for a read that reads 1 or 2 pages of the sort column:
without the late check every read reads at least 3 (230 of 230 reads, 12 of 12
test runs fail), with it 79% of the reads under 8 concurrent clients read 1 or 2.

Since ClickHouse#132712 the writer settings are randomized in stateless tests, and a data
page size of 1 or 1024 bytes splits the sort column of a row group into several
pages. The test pins `output_format_parquet_data_page_size` to at least 64 KiB,
one page per row group (measured for every other randomized writer setting).

The conflict in `ReadManager::clearRowSubgroup` is resolved by taking master's
version from ClickHouse#132621, which frees all the storage of the row subgroup and so
also the decoded leaves that the late skip can leave behind.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-parquet-reader-v3 Issues related to the native Parquet reader. pr-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants