Skip to content

Cover hive partition columns in the TopK query condition cache key of file - #127745

Merged
alexey-milovidov merged 5 commits into
masterfrom
fix-file-topk-qcc-hive-partitioning
Oct 30, 2026
Merged

alexey-milovidov merged 5 commits into
masterfrom
fix-file-topk-qcc-hive-partitioning

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Oct 24, 2026 •

Copy link
Copy Markdown
Member

The file table function creates its storage per query, and the per-query use_hive_partitioning decides whether a column such as key comes from the path or from the file. The TopK query condition cache key of ReadFromFile covers the whole filter, including conjuncts on hive partition columns. But neither that key nor the synthetic table_id from QueryConditionCache::getTableIdForFileEntries covered the hive partition columns. So the same ORDER BY ... LIMIT query under the other mode reused the cached TopK verdicts and skipped the row groups that hold the result: it returned [] instead of [999,998,997] in the reproducer from the issue.

The fix hashes the names and types of the storage's hive partition columns into the TopK key. Plain-predicate entries are not affected, because getFormatFilter strips the hive conjuncts, so the table_id stays the same and they are still shared. The regression was introduced by #122127 and is not in any release yet.

Closes: #127721
Related: #122127

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

🤖 Generated with Claude Code


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

Version info

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

… `file`

The `file` table function creates its storage per query, and the per-query
`use_hive_partitioning` decides whether a column such as `key` is read from
the path or from the file. The TopK key covers the whole filter including
conjuncts on hive partition columns, but neither the key nor the synthetic
`table_id` covered the hive partition columns, so a query under one mode
reused the TopK verdicts of the other and skipped row groups holding the
result.

Closes: #127721

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

clickhouse-gh Bot commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [56fe31f]

Summary: ✅


AI Review

Summary

The PR prevents incorrect ORDER BY ... LIMIT results from file when queries alternate use_hive_partitioning, by including hive partition column names and types in the TopK query condition cache key. No actionable findings.

Missing context / blind spots
  • ⚠️ Stateless CI for the reviewed head, 56fe31f31e2ee3939cee8bfe87e8b48fc18a4d0d, is pending. The regression test passed twice in the debug CI run for 523162a9f1ca0b3f089e505dbcf17a12c7808838; both the test and cache-key implementation are unchanged since that run. Completed stateless CI on the current head would close this gap.
Final Verdict

Status: ✅ Approve.

@clickhouse-gh clickhouse-gh Bot added pr-not-for-changelog This PR should not be mentioned in the changelog comp-table-functions Table functions for temporary read/write to external systems (FROM ... tableFunction()). labels Oct 24, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 56fe31f31 with master 7fc4e86ed (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

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

Job report

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=127745&sha=latest&name_0=PR&name_1=Stateless%20tests%20(amd_debug%2C%20parallel) (05055_format_schema_source_query_background_access, also failing in Stateless tests (arm_binary, ParallelReplicas, s3 storage, parallel)) and provide a fix in a separate PR. It is unrelated to this PR (which only touches the TopK query condition cache key in StorageFile). The fix appears to be in progress in #127797 - if so, please confirm here.

@groeneai

Copy link
Copy Markdown
Collaborator

Confirmed: #127797 is the fix for both failures here.

Both runs executed the test between 22:00 and 23:00 UTC with session_timezone = Africa/Khartoum (UTC+2). The test's start time rendered as 2026-10-26 00:10:57 and 2026-10-26 00:54:24, while the server (Europe/Amsterdam, UTC+1 since DST ended on 2026-10-25) was still on 2026-10-25. event_date >= toDate('<start>') then pruned every system.text_log part, so the refusal row was logged but never matched. #127797 makes the time bound independent of the session time zone and uses event_date >= yesterday(); it is test-only.

This hour repeats every day (again today at 22:00 UTC, report), so it keeps failing until #127797 is merged. Its CI has not started yet: it needs the can be tested label, which I cannot add.

@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.

This is correct. However, I didn't like using the weak boost::hash_combine on top of SipHash-es.

@alexey-milovidov alexey-milovidov self-assigned this Oct 30, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 30, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member Author

@groeneai, after this is merged, make a follow-up PR according to my pedantic observation.

@groeneai

Copy link
Copy Markdown
Collaborator

Acknowledged. My open #127688 already builds this key with a single SipHash instead of the boost::hash_combine calls (it came from your same question on #122127). After this PR merges, I will resolve the conflict there by feeding the hive partition columns into that same SipHash, and reply here with the commit.

Merged via the queue into master with commit 8a59824 Oct 30, 2026
180 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-file-topk-qcc-hive-partitioning branch October 30, 2026 08:12
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 30, 2026
@groeneai

Copy link
Copy Markdown
Collaborator

Done in #127688 (a7173b1): the hive partition columns go into the same single SipHash as the rest of the File TopK key, so the key has no boost::hash_combine left.

While there, I found the same unprefixed name and type strings in the GROUP BY ... LIMIT salt (optimizeGroupByTopK.cpp): on 26.10.2.103-stable, GROUP BY c ORDER BY c DESC LIMIT 2 with a WHERE outside PREWHERE, run after the same query by cU, returns [1] instead of [200, 199]. Fixed in the same PR with a test (03c1778).

The MergeTree TopK salts still combine SipHash values with boost::hash_combine; I left them as they are. Tell me if you want them moved to SipHash too.

groeneai added a commit to groeneai/ClickHouse that referenced this pull request Oct 30, 2026
…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]>
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Oct 30, 2026
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-table-functions Table functions for temporary read/write to external systems (FROM ... tableFunction()). pr-not-for-changelog This PR should not be mentioned in the changelog 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.

file() TopK query condition cache key omits use_hive_partitioning

3 participants