Skip to content

Inline the per-row emplace of a GROUP BY without aggregate functions - #116267

Merged
nickitat merged 2 commits into
masterfrom
agg-inline-no-aggregates-emplace
Aug 25, 2026
Merged

nickitat merged 2 commits into
masterfrom
agg-inline-no-aggregates-emplace

Conversation

@nickitat

@nickitat nickitat commented Aug 25, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Performance Improvement

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

Speed up GROUP BY without aggregates by inlining some functions.

Closes: #115804


Screenshot 2026-08-25 at 20 34 47 Screenshot 2026-08-25 at 20 34 33

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

Version info

  • Merged into: 26.9.1.124 (included in 26.9 and later)
  • Backported to: 26.8.2.3

`executeImplBatchNoAggregates` reached the hash table through an `emplace` lambda that
returned the `EmplaceResult` of either the map or the set branch. For the string methods
the compiler outlined that lambda and called it once per row - 448 out-of-line
instantiations in the binary - and the call costs more than the work it wraps. Let the
lambda return nothing, do the `if constexpr` split inside it and force-inline it: no
outlined copy is left and `.text` grows by 0.09%.

Measured with `clickhouse local`, 100M/200M rows, min of 5, two builds of the same
configuration:

    `transform(...)` GROUP BY, `key_string`        0.719 -> 0.492 s  (-32%)
    same, `key_packed_string`                      0.671 -> 0.523 s  (-22%)
    two-group short string key                     0.744 -> 0.600 s  (-19%)
    `UInt64`, 100k distinct keys (set method)      0.541 -> 0.404 s  (-25%)

This is the slowdown reported in #115804,
which is not the `HashSet` switch itself - single string keys still use the map methods -
but the lambda introduced alongside it.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013zKvKuuxDYe2HxRi3hiUJq
@clickhouse-gh

clickhouse-gh Bot commented Aug 25, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [9833d46]

Summary: ✅


AI Review

Summary

This PR removes the per-row outlined lambda call from executeImplBatchNoAggregates and adds a focused performance test for the short-string GROUP BY regression reported in #115804. I did not find a correctness or safety problem in the refactor itself, but the PR metadata is still incomplete, so I cannot approve it as-is.

PR Metadata
  • Changelog category: Performance Improvement matches the actual change.
  • Changelog entry: required for this category, but the current todo placeholder is not a user-readable changelog entry and would produce a broken release note.
  • Exact replacement text: Improved performance of \GROUP BY` queries without aggregate functions over short string keys.`
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required actions: replace the placeholder changelog entry with a specific user-facing description.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 87.20% 87.30% +0.10%
Functions 91.90% 91.90% +0.00%
Branches 79.40% 79.40% +0.00%

Changed lines: Changed C/C++ lines covered: 16/21 (76.19%) · Uncovered code

Full report · Diff report

…tring keys

The queries from #115804, on both string
map methods: `key_packed_string` and, with `enable_packed_string_keys_in_aggregation = 0`,
`key_string`.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013zKvKuuxDYe2HxRi3hiUJq
@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Aug 25, 2026
The queries are the ones from https://github.com/ClickHouse/ClickHouse/issues/115804.
-->

<query>SELECT length(URL) > 1000 ? 'LONG' : 'SHORT' as x FROM hits_100m_single GROUP BY x FORMAT Null</query>

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.

Performance Improvement requires a concrete changelog entry, but the PR body still has todo. Please replace it with a user-readable line such as Improved performance of \GROUP BY` queries without aggregate functions over short string keys.`

@clickhouse-gh

clickhouse-gh Bot commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 9833d46e6 with master a6b366453 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of a835d3c55; compile times per translation unit against the most recent warmup build that recompiled it).

⚠️ Significant changes: object file sizes.

Binary sizes
Binary Master PR Δ
programs/clickhouse-stripped 709.96 MiB 706.86 MiB -3.10 MiB (-0.44%)

Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction.

Object file sizes ⚠️

67 object files changed (+278.88 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Interpreters/Aggregator.cpp.o 20.06 MiB 20.33 MiB +271.64 KiB (+1.32%)

737 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

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

Job report

@nickitat
nickitat requested a review from thevar1able August 25, 2026 18:36
@nickitat
nickitat marked this pull request as ready for review August 25, 2026 18:36
@clickhouse-gh

clickhouse-gh Bot commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 38 queries analysed

This PR is a targeted inlining refactor of the no-aggregate GROUP BY execution path (executeImplBatchNoAggregates), aimed at the string-key case, plus a new perf test. The three flagged ClickBench queries (Q18, Q33, Q34) all group with COUNT() and therefore run the separate aggregating path this change does not touch, so their ~6% deltas fall inside master's current variance band and read as run-to-run noise, not a real regression. The deterministic within-envelope suppression is consistent with the diff; no directional verdict is warranted.

clickbench

⚠️ 3 inconclusive

Flagged queries (3 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 18 not_sure 1034 1104 +6.8% <0.0001 aggregation: Q18 groups with COUNT() and runs the aggregating path; this PR only refactors the no-aggregate GROUP BY path, so the +6.8% sits inside master's variance band and is not a PR effect.
⚠️ 33 not_sure 1192 1269 +6.5% <0.0001 aggregation: Q33 (GROUP BY URL with COUNT()) exercises the aggregating code path, which this PR does not touch; the +6.5% is within master's normal run-to-run range.
⚠️ 34 not_sure 1187 1260 +6.1% <0.0001 aggregation: Q34 (GROUP BY 1, URL with COUNT()) also uses the aggregating path left unchanged by this PR; the +6.1% is within-envelope variance, not attributable to the diff.

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_official

🟢 No significant changes

Debug info
  • StressHouse run: 6f85ad6a-8c8d-4201-af95-575f9e3d1992
  • MIRAI run: db78d515-492a-4ca0-ab47-f28d9322649b
  • PR check IDs:
    • clickbench_371845_1787691863
    • clickbench_371856_1787691864
    • clickbench_371863_1787691864
    • tpch_adapted_1_official_371880_1787691864
    • tpch_adapted_1_official_371910_1787691864
    • tpch_adapted_1_official_371924_1787691864

@thevar1able thevar1able self-assigned this Aug 25, 2026
@nickitat
nickitat added this pull request to the merge queue Aug 25, 2026
Merged via the queue into master with commit 66e8bc0 Aug 25, 2026
350 of 352 checks passed
@nickitat
nickitat deleted the agg-inline-no-aggregates-emplace branch August 25, 2026 23:09
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 25, 2026
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Aug 30, 2026
@robot-clickhouse robot-clickhouse added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Aug 30, 2026
clickhouse-gh Bot added a commit that referenced this pull request Aug 30, 2026
Backport #116267 to 26.8: Inline the per-row emplace of a `GROUP BY` without aggregate functions
@nickitat

Copy link
Copy Markdown
Member Author

@fm4v why do we backport perf?

@fm4v

fm4v commented Aug 31, 2026

Copy link
Copy Markdown
Member

Yes if this is regression fix (in 26.8): #115804

kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Sep 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo v26.8-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GROUP BY without aggregate functions is 23-30% slower on short string keys after the HashSet change

6 participants