Skip to content

Do not call a function per matched row in hash joins that output no right-table column - #126173

Merged
harikrishnan94 merged 2 commits into
ClickHouse:masterfrom
groeneai:inline-added-columns-append-from-block
Oct 19, 2026
Merged

harikrishnan94 merged 2 commits into
ClickHouse:masterfrom
groeneai:inline-added-columns-append-from-block

Conversation

@groeneai

@groeneai groeneai commented Oct 16, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #125949 (comment)
Related: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=122184&sha=e2db640da2cfb61837f14e5451dc416acbf86161&name_0=PR&name_1=Performance%20Comparison%20%28amd_release%2C%20master_head%2C%205%2F6%29
Related: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=120758&sha=3451905c0a13c854593622761c4fc79a1deefcbd&name_0=PR&name_1=Performance%20Comparison%20%28amd_release%2C%20master_head%2C%206%2F6%29

Changelog category (leave one):

  • Performance Improvement

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

Hash joins that output no column of the right table, for example SELECT count() FROM t LEFT JOIN r ON t.x = r.y, no longer call a function for every matched row, which makes their probe loop up to about 20% faster.

Description

The hash join probe loops call AddedColumns::appendFromBlock out of line for every matched row. When the join outputs no right-table column, record_row_refs is false and the callee only tests that flag and returns. The cost of this call depends on where the linker places the loop and the callee: join_squashing_blocks #0/#1 ran 17-20% slower in about one build in 64, which is what made the two amd_release reds linked above (on #120758 it survived the confirmation restart), and 1.4x slower in some process starts.

appendFromBlock is now inline, so the flag is tested in the loop. Its recording part, LazyOutput::addRef, stays out of line with NO_INLINE: inlining its body into every probe loop made hashjoin_low_cardinality_key #0 5-10% slower in an -O3 build and pushed processMatch out of line, even in loops where addRef never runs. Joins that record row refs keep one call per match, to the same body.

Measured on a local x86-64 ThinLTO -g0 build in the PR CI configuration, 20 interleaved rounds per query: join_squashing_blocks #0/#1 0.81x, hashjoin_low_cardinality_key #0/#1 0.88x/0.87x, join_main_loop 0.86x to 1.01x, and two queries that output a right column (join_append_block #0 and an ALL INNER JOIN returning r.w) 1.00x/1.01x. For join_squashing_blocks #0 the retired near calls drop from 331.9M to 31.8M (300M probe rows). The 1334 join/asof stateless tests show no new failure against master in a debug build. The placement effect does not reproduce on my machine, so the amd/arm numbers are in this PR's Performance Comparison.

🤖 Generated with Claude Code


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

Version info

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

…ight-table column

The hash join probe loops called the out-of-line AddedColumns::appendFromBlock
for every matched row. When the join outputs no column of the right table,
record_row_refs is false and the callee returned immediately, but the call
stayed in the loop, and its cost depended on where the linker placed the loop
and the callee: join_squashing_blocks #0/ClickHouse#1 ran 17-20% slower in about one
build in 64 (the amd_release reds on ClickHouse#122184 and ClickHouse#120758), and 1.4x slower in
some process starts.

Make appendFromBlock ALWAYS_INLINE so the record_row_refs test happens in the
loop, and keep the recording part, LazyOutput::addRef, out of line with
NO_INLINE: inlining its body into every probe loop made
hashjoin_low_cardinality_key #0 5-10% slower and pushed processMatch out of
line, even in loops where addRef never runs. Joins that record row refs keep
one call per match, to the same body as before.

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

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 0 findings; bounded: 1 full pass + 1 fix round + 1 delta recheck), plus my own cold review of the same head (0 blocker, 0 major).

# Sev Finding Verdict Evidence / action
- - No findings - -

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-slot-10:20261016-010311

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? For the precondition, yes: perf stat -e br_inst_retired.near_call:u of one join_squashing_blocks #0 run gives 331.9M calls before and 31.8M after, in 3 of 3 runs. The CI placement penalty itself does not reproduce on my x86-64 host; it was measured from CI runs (10 of 10 slow master builds share the call/callee placement, 0 of 12 fast ones).
b Root cause explained? The probe loop calls AddedColumns::appendFromBlock out of line for every matched row. With no right-table column in the output the callee only tests record_row_refs and returns, and the cost of that call depends on where the linker places the loop and the callee.
c Fix matches root cause? Yes. The flag test is inline, so loops that record nothing execute no call. No fixture, harness, linker or setting change.
d Test intent preserved / new tests added? No test changes. join_squashing_blocks, hashjoin_low_cardinality_key, join_main_loop and join_append_block already measure this in the Performance Comparison; there is no behavior change to assert.
e Both directions demonstrated? Local ThinLTO build in the PR CI configuration, 20 interleaved rounds: join_squashing_blocks #0/#1 0.81x (20 of 20 rounds faster), hashjoin_low_cardinality_key #0/#1 0.88x/0.87x; near calls 331.9M to 31.8M.
f Fix is general across code paths? Every caller of appendFromBlock (ASOF, INNER ANY, LEFT ANY/SEMI, the additional filter path, the ALL path) gets the inline test; no call to it is left in any of the 2400 probe functions of the ThinLTO binary. appendDefaultRow already had this shape.
g Fix generalizes across inputs (params/datatypes/wrappers)? The change does not depend on the key type. Measured on UInt64, LowCardinality(String) and LowCardinality(FixedString(16)) keys, with hash and parallel_hash, for LEFT, INNER, INNER ANY, RIGHT and OR conditions, with and without right-table output columns.
h Backward compatible? (maintainer-approved exception only) Yes. No setting, format or behavior change.
i Invariants and contracts preserved? LazyOutput::addRef keeps its body (chassert(ref_word != 0), row_refs, row_count, hash_table_matches) and is reached under the same flag as before. The debug-only checkColumns still runs for every ref word, and EXPLAIN ANALYZE match statistics still take the recording path.

Session id: cron:clickhouse-impl-slot-7:20261015-225400

@clickhouse-gh

clickhouse-gh Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [6c65317]

Summary: ✅


AI Review

Summary

The PR inlines AddedColumns::appendFromBlock and keeps LazyOutput::addRef out of line, avoiding per-match calls when a hash join does not record right-side row references. No actionable findings in the current diff or surrounding callers. The reported before/after measurements and completed amd64/ARM performance results support the improvement.

Missing context / blind spots
  • ⚠️ The CI rerun for 6c65317ca113 is still running. The preceding commit contains the same code changes and has completed debug and sanitizer stateless coverage, but seven performance shards ended with infrastructure errors. Completing the rerun would close the remaining CI coverage gap; this review did not run local builds or tests.
Final Verdict

Status: ✅ Approve. No code changes requested.

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Oct 16, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @harikrishnan94 @nickitat, could you review this? It makes AddedColumns::appendFromBlock inline again, so hash join probe loops that output no right-table column test record_row_refs in the loop instead of calling a function for every matched row; LazyOutput::addRef stays out of line.

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Oct 16, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-joins JOINs end-to-end (planning hooks + runtime join operators/algorithms). Single bucket to avoid pla... label Oct 16, 2026
@harikrishnan94 harikrishnan94 self-assigned this Oct 16, 2026
@harikrishnan94

Copy link
Copy Markdown
Contributor

@groeneai Make CI green

@harikrishnan94 harikrishnan94 left a comment

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.

LGTM

The previous run's Performance Comparison shards timed out while the
CI logs cluster check was blocked, before the perf queries finished.

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

Copy link
Copy Markdown
Collaborator Author

All 7 red shards are job timeouts, not perf results. In each of them at least one of the two Check CI Log cluster... probes in ci/jobs/scripts/functional_tests/setup_log_cluster.sh blocked for about an hour (3645 s, ending in ATTEMPT_TO_READ_AFTER_EOF) while the CI logs cluster was overloaded, so the job hit its 7200 s limit before the perf queries finished (arm 6/6 never reached them). The other 5 shards lost at most 20 minutes there and finished green. About 100 other Performance Comparison jobs (18 PRs and 7 master commits) ended the same way between 01:40 and 03:40 UTC.

In the finished shards hashjoin_low_cardinality_key #0/#1 ran at 0.73/0.85 (amd) and 0.84/0.85 (arm) of the reference, and no join query was more than 10% slower. join_squashing_blocks was in the timed-out 4/6 shards.

I can't re-run jobs, so I pushed an empty commit (6c65317) to re-run CI. A fix that bounds this probe is in progress; I'll link its PR here when it opens.

@groeneai

Copy link
Copy Markdown
Collaborator Author

Fixing PR for the Performance Comparison job timeouts (CI logs cluster check in setup_log_cluster.sh): #126217

@clickhouse-gh

clickhouse-gh Bot commented Oct 16, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

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

⚠️ Significant changes: object file sizes.

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.

Object file sizes ⚠️

1045 object files changed (-2.43 MiB total), 4 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/DataTypes/TypeFunctions.cpp.o new 1.00 MiB +1.00 MiB
src/CMakeFiles/dbms.dir/Interpreters/Aggregator.cpp.o 25.56 MiB 26.43 MiB +895.89 KiB (+3.42%)
src/CMakeFiles/dbms.dir/DataTypes/TypeMatchers.cpp.o new 692.78 KiB +692.78 KiB
src/CMakeFiles/dbms.dir/DataTypes/FunctionSignature.cpp.o new 417.14 KiB +417.14 KiB
src/Functions/array/CMakeFiles/clickhouse_functions_array.dir/arrayDotProduct.cpp.o 1.75 MiB 1.55 MiB -201.52 KiB (-11.24%)
src/CMakeFiles/dbms.dir/Functions/FunctionsConversion_impl2.cpp.o 1.31 MiB 1.14 MiB -174.78 KiB (-13.07%)
src/Functions/CMakeFiles/clickhouse_functions_obj.dir/FunctionsBitmap.cpp.o 2.58 MiB 2.41 MiB -167.40 KiB (-6.35%)
src/CMakeFiles/dbms.dir/Functions/FunctionsConversion_impl1.cpp.o 1.12 MiB 986.07 KiB -160.78 KiB (-14.02%)
src/Functions/CMakeFiles/clickhouse_functions_obj.dir/vectorFunctions.cpp.o 3.29 MiB 3.43 MiB +148.59 KiB (+4.41%)
src/Functions/array/CMakeFiles/clickhouse_functions_array.dir/arrayCumSumNonNegative.cpp.o 603.16 KiB 466.82 KiB -136.34 KiB (-22.60%)
src/Functions/array/CMakeFiles/clickhouse_functions_array.dir/arrayCumSum.cpp.o 673.15 KiB 538.16 KiB -134.99 KiB (-20.05%)
src/Functions/array/CMakeFiles/clickhouse_functions_array.dir/arrayAggregation.cpp.o 1.29 MiB 1.16 MiB -134.86 KiB (-10.22%)
src/Functions/CMakeFiles/clickhouse_functions_obj.dir/FunctionsCodingIP.cpp.o 1.27 MiB 1.13 MiB -133.67 KiB (-10.32%)
src/Functions/array/CMakeFiles/clickhouse_functions_array.dir/arrayDifference.cpp.o 592.61 KiB 478.04 KiB -114.56 KiB (-19.33%)
src/Functions/CMakeFiles/clickhouse_functions_obj.dir/FunctionsJSON.cpp.o 4.26 MiB 4.37 MiB +111.80 KiB (+2.57%)
src/Functions/CMakeFiles/clickhouse_functions_obj.dir/FunctionsHashingMisc.cpp.o 5.53 MiB 5.61 MiB +83.25 KiB (+1.47%)
src/Functions/array/CMakeFiles/clickhouse_functions_array.dir/arrayDistance.cpp.o 10.19 MiB 10.11 MiB -78.98 KiB (-0.76%)
src/Functions/CMakeFiles/clickhouse_functions_obj.dir/FunctionsConversion_reg.cpp.o 6.83 MiB 6.75 MiB -78.39 KiB (-1.12%)
src/CMakeFiles/dbms.dir/Functions/FunctionsConversion_impl3.cpp.o 1.87 MiB 1.80 MiB -76.37 KiB (-3.98%)
src/CMakeFiles/dbms.dir/Functions/IFunction.cpp.o 555.11 KiB 631.16 KiB +76.05 KiB (+13.70%)

719 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

2853 translation units recompiled, 16566 s compile time in total, 2848 of them have a recent master baseline.

Translation units without a recent master baseline:

  • programs/keeper/Keeper.cpp (clickhouse-keeper): 8.5 s
  • src/DataTypes/TypeFunctions.cpp: 7.0 s

Job report

@harikrishnan94
harikrishnan94 added this pull request to the merge queue Oct 18, 2026
Merged via the queue into ClickHouse:master with commit 807a057 Oct 19, 2026
178 checks passed
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-joins JOINs end-to-end (planning hooks + runtime join operators/algorithms). Single bucket to avoid pla... pr-performance Pull request with some performance improvements 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.

4 participants