Repository navigation
Query condition cache for TopK reads of Parquet files - #122064
Conversation
A TopN (`ORDER BY ... LIMIT n`) read of Parquet files through the `File` engine now writes query condition cache entries for the row groups that held no row of the result, and a rerun of the same query skips them. The entries depend on the running TopK threshold, which comes from the rows of every file the query reads, so they are stored under a key that covers the TopK plan, the predicate (a read without one is cached too), and the version tokens of all the files; the key is made only when every token has settled. The plain predicate key keeps getting only threshold-oblivious entries. A cancelled read no longer writes entries, as the row groups it did not read report no matching rows either. Closes: #121876 Related: #115798 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…mpty ones With parallel reading, the Parquet reader returns most row groups before the first threshold is published, so hardly any row group ends up without a returned row, and the TopK entries recorded almost nothing. Now the reader remembers the best returned value of the sort column in each row group, and `StorageFileSource` writes the entries only when it is destroyed, dropping the row groups whose best value sorts strictly beyond the threshold at that time. The threshold only ever tightens, so this is sound at any point, and the final `MergingSortedTransform` now publishes the exact final threshold, which the per-stream sorting transforms never did. On a 20M-row file with 153 row groups, a rerun of `SELECT * ... ORDER BY k LIMIT 10` now reads 10 row groups (7.6 MiB instead of 115 MiB, 17 ms instead of 55 ms). Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
|
Preview deployment for your docs. Learn more about Mintlify Previews.
|
|
Workflow [PR], commit [5b5a17d] Summary: ✅
AI ReviewSummaryThis PR extends the query condition cache to Findings
Final Verdict
LLVM Coverage ReportMeasured on commit 5b5a17d.
Changed lines: Changed C/C++ lines covered: 332/348 (95.40%) · Uncovered code |
Build profile diff (arm_release)Commit See the job log for details. |
|
📊 Cloud Performance Report ✅ AI verdict: This PR adds query condition cache entries for TopK (ORDER BY col LIMIT n) reads of Parquet files through the File engine. It also publishes the exact TopK threshold from the sort and merge transforms, but only when a threshold tracker exists. None of the four flagged improvements run that code. ClickBench Q33/Q34 order by an aggregate after GROUP BY, and TPC-H Q1/Q7 have no LIMIT. All four are downgraded to not sure. The ClickBench pair does show consistent CPU drops of about 12%, which more likely comes from a build or base difference than from this diff. The TPC-H deltas come from base medians at or above master's recent range. clickbenchFlagged queries (2 of 43)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. tpch_adapted_1_officialFlagged queries (2 of 22)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. Debug info
|
The TopK query condition cache key covers the versions of all files, but each file only checked its own token. With several streams, one file could already skip row groups by an entry while another file of the glob was rewritten before being opened, and the threshold then came from rows the entry never described. Now the first file read in another version (at open, or rewritten while read) invalidates the key for the whole query: nothing is consulted or written under it afterwards. If an entry has already been used by then, the query fails with `FILE_CHANGED_DURING_READ` rather than return what the stale entry left of the result; the pair of sequentially consistent flags makes sure one of the two sides notices. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The `engine_file_empty_if_not_exists` and `engine_file_skip_empty_files` paths skipped a file without the check that invalidates the TopK query condition cache key, although a file that disappeared or was emptied after the key was made changes the threshold just as a rewrite does. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…e stream The TopK key covered every path of the table, although a `_path` / `_file` filter can leave the query only some of them: a change of a file it never reads (or one not settled yet) cost it the cache. The key now covers the files the iterator returns when they are known up front. With a single stream there is no final `MergingSortedTransform`, and the exact threshold was only published if `remerge` happened to run. `MergeSortingTransform` now publishes the value at the limit when it generates the sorted result: exact with one stream, never tighter than the final one with several. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
With external sorting the sorted result of a stream comes out of `external_merging_sorted` rather than `generate`, so give that merge the tracker as well. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
With the `invalidated` and `consulted` flags, a lookup that raced with an invalidation set `consulted` and then dropped its entry, and the next failed check of a file then threw `FILE_CHANGED_DURING_READ` although no entry had been used. One atomic state (`Valid`, `Used`, `Invalidated`) makes the two sides linearizable: a lookup uses its entry only if it moves the state to `Used` (or finds it there), and an invalidation fails the query only if it replaces `Used`. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
A TopK entry that excludes no row group beyond the verdict of the predicate alone changed nothing, yet it moved the key to `Used`, and a later change of a file then failed the query with `FILE_CHANGED_DURING_READ`. Such an entry is now not used at all, so a later change only invalidates the key. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
A file whose last row groups reach the sorting one row at a time, so that only `MergeSortingTransform::generate` publishes the exact threshold with `max_threads = 1`. Without that publication the entry is not written. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…-condition-cache Resolved conflicts in `StorageFile`: keep the TopK cache write before the now exception-guarded `beforeDestroy`, and read the paths for the TopK cache key from `paths_snapshot`, since `StorageFile::paths` is now guarded by `paths_mutex` on master. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=122064&sha=416cf15dc5cc433b307b145617f6c6ed1bd25fa6&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%29 ( |
# Conflicts: # src/Processors/Formats/AggregateFunctionStatesFromValuesInputFormat.h # src/Processors/Formats/IInputFormat.h # src/Processors/Formats/Impl/ParquetV3BlockInputFormat.cpp # src/Processors/Formats/Impl/ParquetV3BlockInputFormat.h
…ThresholdTracker` Master turned `TopKThresholdTracker` into the `ITopKThresholdTracker` interface. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Let an exception from `writePendingTopKQueryConditionCacheEntries` propagate to the existing handler in `~StorageFileSource` instead of catching it in the function. It runs after `beforeDestroy`, so a failure does not skip the rename of the processed files. Drop the settle-window requirement from the TopK key: the version tokens have sub-second precision, a file whose token has not settled neither reads nor writes entries anyway, and a file rewritten while it is read is caught by the re-stat at the end of its read. This removes `isFileCacheVersionTokenSettled`. Address review: #122064 (comment) Address review: #122064 (comment) Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@alexey-milovidov — ClickGap found the following in this PR:
Close anything that's wrong or already addressed. |
A TopN (`ORDER BY ... LIMIT n`) read of a data lake table (`Iceberg`, `DeltaLake`, `Paimon`) with Parquet data files now records in the query condition cache the row groups which hold no row of the result, and a rerun of the same query skips them, also without a `WHERE`. The entries are keyed by the TopK plan, the predicate, and the snapshot of the table: the snapshot identifies the set of data files (and deletes), which are never modified in place, and the threshold of the TopN filter comes from the rows of all of them. The same was done for the `File` engine in ClickHouse#122064. Related: ClickHouse#124785 Co-Authored-By: Claude Opus 5.5 <[email protected]>
The query condition cache skipped every table without a UUID, which is the table behind a table function (`file`, `s3`, `icebergS3`, ...). For file-backed data the table is not needed: the entries are keyed by the location of the file together with its version (the local version token or the ETag), or by the location alone for immutable data lake files. Such tables now keep their entries under a fixed table id (`QueryConditionCache::getTableIdForFileEntries`), and object storage qualifies the path with the storage it is in (endpoint, bucket), which the table UUID used to provide. Related: ClickHouse#122064 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Implements the query condition cache for TopN (
ORDER BY <column> LIMIT n) reads of Parquet files through theFileengine. So far such reads could only consult entries written by plain reads with the same predicate, and never wrote any themselves, because which row groups the TopN filter lets through depends on the running threshold.A TopN read now records the row groups which hold no row of its result, and a rerun of the same query skips them - also without any
WHERE, e.g.SELECT * FROM hits ORDER BY EventTime LIMIT 10.FormatTopKFilterInfo::track_row_group_best_values). A row group that returned no row, or whose best value sorts strictly beyond the threshold, holds no row of the result: the threshold only ever tightens towards the final one, and__topKFilterkeeps rows equal to it. Relying only on "no row returned" is not enough - with parallel reading, the reader returns most row groups before the first threshold is published.StorageFileSourcewrites the entries when it is destroyed, not when it finishes a file: at that point the rows of the file may still be waiting for the sorting transforms. For the threshold to be exact by then, the finalMergingSortedTransformnow publishes the value in the last row within the limit. Before, only the per-stream sorting transforms published their own, much looser, thresholds. A cancelled read writes nothing (for the plain entries too), as the row groups it did not reach report no matching rows either.LIMIT, direction, NULLS, collation - the same hash theMergeTreepath uses, now shared), the predicate if any, and the version tokens of all files of the table. The key is made only when every token has settled and the query has no row policy, and a file whose token changed after the key was made does not use it. The plain predicate key keeps getting only threshold-oblivious entries. A TopN read consults both keys; a row group is skipped if either verdict excludes it.use_query_condition_cache_for_top_k = 0keeps TopN reads out of the cache entirely, as before.On a 20M-row file with 153 row groups (random order of the sort key), with default settings:
SELECT * FROM t ORDER BY k LIMIT 10SELECT * FROM t WHERE n % 7 = 1 ORDER BY k DESC LIMIT 10The test covers both shapes, a different
LIMIT/ direction / plain read not reusing the entries, the setting, and a rewrite of one file of a glob: the stale entries of the unchanged file must not be reused, since the result now lies in the row group they recorded as holding none. Without the file versions in the key the test returns9, 9, 9instead of100999, 100998, 100997(checked by building with them removed).Closes: #121876
Related: #115798
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
The query condition cache now also works for
ORDER BY ... LIMIT nqueries over Parquet files ofFiletables, with or without aWHEREclause: a repeated query reads only the row groups which can hold rows of its result.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/122064]
Version info
26.10.1.1872-master(included in26.10and later)