Skip to content

Copy ranges of rows in firstNonDefault instead of two virtual calls per row - #123302

Merged
vdimir merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai-first-non-default-bulk-default-rows
Oct 2, 2026
Merged

vdimir merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai-first-non-default-bulk-default-rows

Conversation

@groeneai

@groeneai groeneai commented Oct 1, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #118757

Changelog category (leave one):

  • Performance Improvement

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

Function firstNonDefault, which computes the USING columns of FULL JOIN, now copies whole ranges of rows instead of making two virtual calls per row. Queries that use a USING column of a FULL JOIN after the join, for example in WHERE, are faster.

Description

Requested in #118757 (comment): joins_in_memory #19/#20 (FULL JOIN ... USING i64,i32,i16,i8 WHERE i32 = 20042) were +24.5% in Performance Comparison (arm_release, master_head, 1/6) on an unrelated PR.

FULL JOIN ... USING computes each USING column as firstNonDefault(l.k, r.k), so this WHERE evaluates it over every joined row (about 1.1e8). For each row it made a virtual isDefaultAt and a virtual insertFrom call, about 40% of the query's CPU in the arm flamegraph, and the whole +24.5% of that run is inside this function. The cost of these calls varies between binaries: in 30 days of arm runs the reference side never exceeded 0.132 s, while the PR side exceeded 0.138 s 22 times across 15 unrelated PRs.

Now the default rows of each argument are counted in bulk (getNumberOfDefaultRows). An argument without default rows is returned without copying, and one that is default in every row is skipped. For the first partially-default argument, getIndicesOfNonDefaultRows gives the rows it decides, which are copied as ranges; the per-row loop remains only for the rows it leaves undecided. Results and types are unchanged.

x86, 16 pinned cores, clickhouse local, medians of 5 interleaved rounds (base / fix):

query wall, s user CPU, s
#19 0.092 / 0.079 0.402 / 0.150
#20 0.092 / 0.078 0.403 / 0.153
#10 (same 4-key join as LEFT JOIN, no firstNonDefault) 0.064 / 0.063 0.115 / 0.114

I measured on x86 only; the arm effect can only be checked in this PR's Performance Comparison. 03522_function_first_non_default gets a 200000-row block checked against multiIf.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.10.1.1291-master (included in 26.10 and later)

`firstNonDefault` made, for every row and argument, a virtual `isDefaultAt`
call on the argument and a virtual `insertFrom` from the casted column. It
computes every `USING` column of a `FULL JOIN`, so a filter on such a column
after the join (`joins_in_memory` ClickHouse#19/ClickHouse#20) spent most of its CPU in that loop,
and the cost of the indirect calls moved with code placement between builds.

Now each argument's default rows are counted once with
`getNumberOfDefaultRows`: an argument with no default rows decides every row
and its casted column is returned without copying, and an argument that is
default everywhere is skipped. For the first partially-default argument,
`getIndicesOfNonDefaultRows` gives the rows it decides, which are copied as
ranges (`insertFrom` for a run of one row); the per-row loop runs only over
the later arguments, for the rows the first one leaves undecided.

Same results for every input: the bulk methods agree with `isDefaultAt` for
every column type (the only column without `getIndicesOfNonDefaultRows`,
`ColumnAggregateFunction`, has no default rows).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-request PR origin: a maintainer pinged or directed groeneai labels Oct 1, 2026
@groeneai

groeneai commented Oct 1, 2026

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

Pre-publication review by an independent model (engine: codex; 2 findings; bounded: 1 full pass + 1 fix round + 1 delta recheck).

# Sev Finding Verdict Evidence / action
1 ❌ A partially-default first argument materializes up to 8 bytes per row of non-default indices (firstNonDefault.cpp:122-124) DISAGREE True, but per call, freed before return and bounded by the block (max_joined_block_size_rows 65409 / max_joined_block_size_bytes 4 MiB, so at most 512 KiB); ColumnTuple/ColumnObject::getNumberOfDefaultRows already do the same (#105890).
2 ⚠️ The new test checks results only and cannot tell whether the bulk path is used DISAGREE The perf oracle is joins_in_memory #19/#20 in Performance Comparison (local x86 A/B: user CPU 0.38x, wall 0.84x, controls 1.00x); the test checks exactness and fails when either new branch is broken.

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

Session id: cron:clickhouse-review-slot-10:20261001-161400

@clickhouse-gh

clickhouse-gh Bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [6193ff4]

Summary: ✅


AI Review

Summary

This PR replaces the row-by-row isDefaultAt / insertFrom loop in src/Functions/firstNonDefault.cpp with bulk default-row detection plus contiguous insertRangeFrom copies, and extends tests/queries/0_stateless/03522_function_first_non_default.sql to cover multi-block integer, string, Nullable, float, array, LowCardinality, and AggregateFunction cases. I did not find a correctness, compatibility, or performance-safety issue in the current code.

Missing context / blind spots
  • ⚠️ There is no local clickhouse binary in this checkout, so I could not rerun 03522_function_first_non_default or inspect the perf shards with .claude/tools/fetch_perf_report.py locally. The current PR CI is green, including the performance-comparison shards, which substantially reduces this gap.
Final Verdict

✅ No blocking or major issues found. The new fast path preserves the existing contract that defaultness is checked on the original argument types, the added stateless coverage exercises the column families touched by the optimization, and the current CI report for PR #123302 is all green.

LLVM Coverage Report

Measured on commit 6193ff4.

Metric Baseline Current Δ
Lines 88.30% 88.30% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 79.70% 79.70% +0.00%

Changed lines: Changed C/C++ lines covered: 39/39 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Oct 1, 2026
@groeneai
groeneai requested review from Algunenano and vdimir October 1, 2026 16:53
@vdimir vdimir self-assigned this Oct 1, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... label Oct 1, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

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

This PR only changes how firstNonDefault builds its result column (it now uses column-level default counts and copies runs of rows) and adds tests for it. Neither benchmark calls this function, so it cannot plausibly move either flagged ClickBench query. Q28 (+6.5%) is not_sure because the two tests disagreed and the timings were very noisy. Q34 (+5.9%) falls inside master's current range for that query, and CPU time barely moved on either one, so no action is needed.

clickbench

⚠️ 2 inconclusive

Flagged queries (2 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 28 not_sure 1058 1127 +6.5% <0.0001 This query does not call firstNonDefault, so the PR's code never runs here. CPU time is nearly flat and the measurements were very noisy, so treat the +6.5% as variance.
⚠️ 34 not_sure 1154 1222 +5.9% <0.0001 The +5.9% is inside master's current range for this query, and the query does not use firstNonDefault. CPU time is almost unchanged, so there is no reason to challenge the suppression.

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: 21da520a-296d-4071-8ec5-813942335053
  • MIRAI run: 0f50fcd7-15cc-48f6-8e95-4117983b7985
  • PR check IDs:
    • clickbench_2774846_1790879407
    • clickbench_2774852_1790879408
    • clickbench_2774859_1790879407
    • tpch_adapted_1_official_2774890_1790879407
    • tpch_adapted_1_official_2774904_1790879407
    • tpch_adapted_1_official_2774918_1790879407

@clickhouse-gh

clickhouse-gh Bot commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 6193ff4a4 with master 0da3d28e1 (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, 7 s compile time in total, 7 of them have a recent master baseline.

Job report

@vdimir
vdimir added this pull request to the merge queue Oct 2, 2026
Merged via the queue into ClickHouse:master with commit 4cbdb04 Oct 2, 2026
149 of 150 checks passed
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 2, 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-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... groeneai-origin-request PR origin: a maintainer pinged or directed groeneai 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.

3 participants