Skip to content

Cherry pick #125897 to 26.8: Do not prefetch in a two-level GROUP BY that holds few keys - #129833

Merged
robot-ch-test-poll merged 7 commits into
backport/26.8/125897from
cherrypick/26.8/125897
Nov 11, 2026
Merged

robot-ch-test-poll merged 7 commits into
backport/26.8/125897from
cherrypick/26.8/125897

Conversation

@robot-ch-test-poll4

Copy link
Copy Markdown
Contributor

Original pull-request #125897

Do not merge this PR manually

This pull-request is a first step of an automated backporting.
It contains changes similar to calling git cherry-pick locally.
If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.

The check results does not matter at this step - you can safely ignore them.

Before you resolve anything

Conflicts are often caused by a prerequisite change that has not been backported yet, rather than by a real divergence. The bot re-tries this cherry-pick against the release branch on every run, so if that is the case here it will merge itself as soon as the prerequisite lands, and you will see a comment saying so. Manual resolution is only needed while the conflict persists.

Troubleshooting

If the conflicts were resolved in a wrong way

If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

groeneai and others added 6 commits October 14, 2026 22:01
A two-level hash table allocates all 256 buckets at their initial size up
front, so its getBufferSizeInBytes() is a constant however few keys it holds:
2 MiB for string keys with a saved hash, 2.5 MiB for keys256 with a mapped
value, about 6.5 MiB for TwoLevelStringHashMap. Since #108655 lowered the
aggregation prefetch threshold from 4 x L2 to L2, every host whose L2 is below
that constant prefetches every row of such a table, even with 13 keys. The
look-ahead then hashes each key a second time for probes that touch a handful
of cells (#125845: 2.3x slower on 26.8 than on 26.6).

bytesForPrefetch() measures a two-level table that is still at its initial
allocation by its keys, as the smallest table that holds them at the maximum
load factor 1/2, and every other table by its buffer as before. Both users of
the gate go through it: Aggregator::executeImpl and the self-prefetching
HashMethodSerialized. StringHashTable gets initial_buffer_bytes so that
TwoLevelStringHashTable is covered as well.

Measured on a release build, base vs fix (executed PREFETCH instructions via
perf sw_prefetch_access.any, and time):
- issue query at an emulated 1 MiB L2: 301M -> 1.8M, 0.23 s -> 0.075 s
- Nullable(String), 13 keys, 2 MiB L2: 99.9M -> 0.4M, 0.22 s -> 0.18 s
- keys256, 13 keys, 2 MiB L2: 58.9M -> 0.4M, 0.15 s -> 0.11 s
- unchanged: the #108311 shape, 20K-key two-level tables at 1 MiB L2
  (prefetch stays on), 2M-key single-level tables

Closes #125845

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The gate only decides whether to prefetch, so a functional test cannot observe it. The test instead runs every two-level table shape behind `bytesForPrefetch` with `enable_software_prefetch_in_aggregation` on and off and checks that the rows match: the `LowCardinality(Nullable(String))` key from #125845, `Nullable(String)` (`TwoLevelStringHashMap` and its new `initial_buffer_bytes`), `keys256`, serialized keys (the `HashMethodSerialized` gate), and a two-level table that has outgrown its initial allocation (the fallback branch).

Related: #126578
`05498_group_by_two_level_few_keys_prefetch` failed the debug flaky check with
"Test runs too long (> 180s)" in 1 of 50 runs (201.7 s; the other 49 took at
most 58 s). Its `query_log` shows that run slow in every statement under heavy
MergeTree randomization (`auto_statistics_types`, sparse serialization), so
the cost follows the data size.

Insert 300000 rows instead of 1000000. The 6 reading streams (`EXPLAIN
PIPELINE`) still read about 50000 rows each, more distinct keys of `many`
than the 32768 a two-level table fits in its initial 256 x 256 cells at load
factor 1/2, so the grown case still grows. Pin
`max_block_size = 8192` for the queries: a thread converts to two-level only
after its first block, so the gate sees its two-level table only from the
second block on, and with fewer rows per thread a large random block size
would leave none.

Results do not depend on the data size, so the reference only changes its
counts. Measured on the CI debug binary of 70ff2d5: 2.1x less CPU
locally (2.8x with the failing run's settings); under the flaky-check shape
(8 jobs, 50 runs, thread fuzzer) the p50 drops by about 30%, and with the failing
run's settings it is at least 2.4x faster (600 s against the 1500 s timeout
of every old run).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The previous run's jobs were cancelled by a self-hosted runner outage
and never re-ran, so the head could not finish CI.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Do not prefetch in a two-level GROUP BY that holds few keys
@robot-ch-test-poll4 robot-ch-test-poll4 added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request labels Nov 6, 2026
@groeneai

groeneai commented Nov 6, 2026

Copy link
Copy Markdown
Collaborator

The three conflicts (Prefetching.h, ColumnsHashing.h, Aggregator.cpp) all come from #115840 (minBytesForPrefetch<Data, has_mapped>), which is not on 26.8 and has no backport label. I kept the 26.8 side there: both gates still compare with min_bytes_for_prefetch, and only their size argument becomes bytesForPrefetch(...). bytesForPrefetch and the StringHashTable constants are added as on master, except that its comment names min_bytes_for_prefetch, since minBytesForPrefetch does not exist on 26.8. Against backport/26.8/125897 the merge changes exactly the 7 files of #125897, +240/-2 as on master.

On the 26.8 tree (26.8.35.1, clang-22, debug) 05498_group_by_two_level_few_keys_prefetch passes, 50 of 50 with random settings. That test only checks results, so I also counted executed PREFETCH instructions for a 13-key two-level GROUP BY over 30M rows (2 MiB L2 host), against the same tree with the old Aggregator.cpp gate line: Nullable(String) 29.3M -> 0.33M and keys256 28.2M -> 0.31M (0.33M is the count with enable_software_prefetch_in_aggregation = 0), while a 2M-key table still prefetches (29.9M -> 29.8M).

I cannot push to cherrypick/26.8/125897. The merge (95b3da326c55) is on my fork and its first parent is this PR's head c1c4e0b4306e, so applying it is a fast-forward:

git fetch https://github.com/groeneai/ClickHouse.git groeneai/resolve-26.8-cherrypick-125897
git push origin FETCH_HEAD:cherrypick/26.8/125897

@alexbakharew

Copy link
Copy Markdown
Contributor

@groeneai please resolve conflicts and re-run the CI

@groeneai

groeneai commented Nov 7, 2026

Copy link
Copy Markdown
Collaborator

I don't have push access to this repository, so I can't push to cherrypick/26.8/125897 or re-run CI here. The conflicts are already resolved in 95b3da326c55 on my fork (details in my comment above); its first parent is this PR's head, so applying it is a fast-forward:

git fetch https://github.com/groeneai/ClickHouse.git groeneai/resolve-26.8-cherrypick-125897
git push origin FETCH_HEAD:cherrypick/26.8/125897

After that this PR becomes mergeable, and the robot merges it and opens the 26.8 backport PR, where CI runs.

@robot-clickhouse-ci-1

Copy link
Copy Markdown
Contributor

Dear @nickitat, @nihalzp, @groeneai, this cherry-pick PR has not been updated for 3d0h6m33s. Please resolve the conflicts to backport #125897, or close this PR if the backport is no longer needed. This PR will be automatically closed after 7 days of inactivity.

@nickitat

Copy link
Copy Markdown
Member

@copilot resolve the merge conflicts in this pull request

…use/ClickHouse into cherrypick/26.8/125897

# Conflicts:
#	src/Common/ColumnsHashing.h
#	src/Common/HashTable/Prefetching.h
#	src/Interpreters/Aggregator.cpp

Co-authored-by: nickitat <[email protected]>
@cla-assistant

cla-assistant Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
3 out of 4 committers have signed the CLA.

✅ nickitat
✅ fm4v
✅ groeneai
❌ Copilot
You have signed the CLA already but the status is still pending? Let us recheck it.

Copilot AI commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

@copilot resolve the merge conflicts in this pull request

Resolved the three conflicts in ColumnsHashing.h, Prefetching.h, and Aggregator.cpp in merge commit 63c7a90b. PR #129833 is now clean and mergeable. This staging cherry-pick remains labeled do not test, so no CI rerun was triggered.

Copilot AI requested a review from nickitat November 11, 2026 23:35
@robot-ch-test-poll
robot-ch-test-poll merged commit df2bb81 into backport/26.8/125897 Nov 11, 2026
1 of 2 checks passed
@robot-ch-test-poll
robot-ch-test-poll deleted the cherrypick/26.8/125897 branch November 11, 2026 23:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants