Repository navigation
Conversation
… TopK key
`topKPlanHash` salts every TopK query condition cache key: the File key,
the data lake key (`FormatTopKFilterInfo::plan_hash`) and the MergeTree
TopK salts (`TopKFilterInfo::condition_hash`). It fed the sort column name
and the type name to SipHash back to back, without their lengths, so
`c UInt8` and `cU Int8` were the same bytes ("cUInt8"). The two TopK plans
then shared one cache key, and `ORDER BY c DESC LIMIT 3` reused the "no row
of the result here" entries that `ORDER BY cU DESC LIMIT 3` had recorded
against its own threshold, skipping the row group / granule with the top
values of `c`. Measured on a `File(Parquet)` table, and on MergeTree with a
WHERE kept as a separate filter (its entries are keyed by the WHERE hash
and the TopK plan hash). Prefix the name, the type name and the collation
locale with their lengths, as `getTableIdForFileEntries` does.
The weak combinations asked about in ClickHouse#122127 are replaced as well, though
they were not a practical collision (their inputs are SipHash values, and
the Boost 1.90 `hash_combine` mixer is a bijection):
- `ReadFromFile::makeTopKQueryConditionCacheKey` builds the key with one
SipHash over the condition hash, the plan hash, the files and the table
name, like `ReadFromObjectStorageStep::makeTopKQueryConditionCacheKey`,
instead of three `boost::hash_combine`.
- `queryConditionCacheSettingsSalt(s) ^ fileFormatPruningSettingsSalt(s)`
(in `FormatFilterInfo::computeConditionHash` and in the no-filter branch
of the File TopK key) becomes one SipHash, `queryConditionCacheHashForFiles`.
The MergeTree TopK salts keep `boost::hash_combine`: their writers and
readers in five files must nest it identically.
Iceberg and Delta Lake tables cannot hit the collision: none of their type
names ends with another one. 26.9.25.15 does not show it on MergeTree; the
File TopK cache (ClickHouse#122064) is in the 26.10 branch.
Related: ClickHouse#122127 (comment)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 1 finding) plus my own cold review (3 nits).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261024-054400 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20261024-032500 |
|
Workflow [PR], commit [2a4f284] Summary: ✅
AI ReviewSummaryThe PR fixes deterministic TopK query condition cache collisions by length-prefixing column names, type names, and collation locales, and builds the Missing context / blind spots
Final Verdict✅ Approve. No changes requested; current-head CI remains pending. |
|
cc @KochetovNicolai, could you review this and add the |
CI finish ledger - 65d2bdaEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
CIDB has no other Session id: cron:our-pr-ci-monitor:20261025-140005 |
…o the File TopK SipHash Resolves the conflict with ClickHouse#127745, which added the hive partition columns to the File TopK query condition cache key as a fourth `boost::hash_combine` of a separate SipHash. They now go into the same single SipHash as the rest of the key (name and type name, each length-prefixed), so the key has no `boost::hash_combine` left. The file list is now followed by more variable-length fields in that one stream, so it is prefixed with its count to keep the encoding unambiguous. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…salt `tryAttachDynamicFilter` in `optimizeGroupByTopK.cpp` streamed the first ranked key and every grouping key as name and type name without their lengths, so `GROUP BY c` (`UInt8`) and `GROUP BY cU` (`Int8`) produced the same salt. With the `WHERE` kept as a separate filter, its query condition cache entry is keyed by the plain predicate and this salt only, so `GROUP BY c ORDER BY c DESC LIMIT 2` after the same query by `cU` reused the granules emptied by the `cU` boundary and returned `[1]` instead of `[200, 199]` (measured on master and on 26.10.2.103-stable with the cache and TopK settings at their defaults). Same fix as `topKPlanHash`; the test gets a `GROUP BY` arm. Found by the second-model review of the ClickHouse#127745 conflict resolution. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Slowest function optimization changes (ThinLTO)Median per-function time ratio to the master baseline:
Compile time of recompiled translation units157 translation units recompiled, 1666 s compile time in total, 157 of them have a recent master baseline. Median compile-time ratio to the baselines is ×1.06 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio. |
Conflict in src/Formats/FormatFilterInfo.cpp only: master changed the parameter of ColumnMapper::setStorageColumnEncoding to ColumnNameToFieldId next to the new queryConditionCacheHashForFiles; both kept. ReadFromFile::makeTopKQueryConditionCacheKey merged cleanly and still builds the key with one SipHash (hive partition columns, paths.size(), no boost::hash_combine). The object-storage TopK key that master added since the last merge already takes top_k_filter->plan_hash, so it gets the length-prefixed topKPlanHash as well. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed wrong results of
ORDER BY ... LIMITandGROUP BY ... LIMITqueries with the query condition cache for TopK (use_query_condition_cache_for_top_k): when the name and type of one key column ran into the same string as those of another, for examplec UInt8andcU Int8, the query could reuse the cache entries of the other column and skip rows of its result.Description
Follow-up to #122127 (comment) and #127745 (comment).
On 26.10.2.103-stable, with default cache and TopK settings,
SELECT c FROM t ORDER BY c DESC LIMIT 3afterSELECT cU FROM t ORDER BY cU DESC LIMIT 3returns1, 1, 1instead of200, 200, 200: on aFile(Parquet)table, and onMergeTreewith aWHEREthat stays out ofPREWHERE.topKPlanHash, the TopK part of these keys, hashed the column name and type name without their lengths, so both gave the bytescUInt8. The salt ofGROUP BY ... LIMIThad the same defect:GROUP BY c ORDER BY c DESC LIMIT 2returns[1]instead of[200, 199]when theWHEREstays a separate filter. 26.9.25.15 returns the right rows.Changes:
topKPlanHashprefixes the column name, the type name and the collation locale with their lengths. The File, data lake and MergeTree TopK keys all take this hash.GROUP BY ... LIMITsalt (optimizeGroupByTopK.cpp) prefixes its column names and type names the same way.ReadFromFile::makeTopKQueryConditionCacheKeybuilds the key with one SipHash, likeReadFromObjectStorageStep, instead of fourboost::hash_combine, including the hive partition columns from Cover hive partition columns in the TopK query condition cache key offile#127745. The file list is prefixed with its count.queryConditionCacheSettingsSalt(s) ^ fileFormatPruningSettingsSalt(s)become one SipHash inqueryConditionCacheHashForFiles.The MergeTree TopK salts still use
boost::hash_combine(8 calls in 5 files that must nest alike on write and read); left as is.Validation: the new test fails on master in 80 of 80 randomized runs (its
GROUP BYarm in 10 of 10 without that fix) and passes with the fix in 50 of 50; reverting only thetopKPlanHashchange makes it fail again. The test of #127745 passes, and fails when the hive columns are left out of the key. The query condition cache and TopK stateless tests pass.Workflow [PR]
Sync PR [sync-upstream/pr/127688]