Skip to content

TopK through multiple ARRAY JOINs - #131004

Merged
nihalzp merged 5 commits into
masterfrom
yarik/top-k-through-array-join-chain
Nov 13, 2026
Merged

nihalzp merged 5 commits into
masterfrom
yarik/top-k-through-array-join-chain

Conversation

@yariks5s

@yariks5s yariks5s commented Nov 12, 2026 •

Copy link
Copy Markdown
Member
SELECT id, src, dst
FROM t
LEFT ARRAY JOIN sources AS src
LEFT ARRAY JOIN destinations AS dst
ORDER BY name
LIMIT 31
before: Limit -> ArrayJoin (destinations) -> Sorting -> ArrayJoin (sources) -> ReadFromMergeTree
after:  Limit -> ArrayJoin (destinations) -> ArrayJoin (sources) -> Sorting -> ReadFromMergeTree

Related: #116550
Related: #128705
Related: #128926

Changelog category (leave one):

  • Performance Improvement

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

ORDER BY ... LIMIT over several ARRAY JOIN clauses now sorts and limits the rows before all of them, not only before the last one, when the sort key does not use the joined columns.


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

Version info

  • Merged into: 26.11.1.2221-master (included in 26.11 and later)
  • Backported to: 26.10.13.7

With the re-optimization depth from #128926 the notEmpty filter of the first
move goes below the next ARRAY JOIN, so the sort of an inner chain moves again.
The case showed the sort between the two ARRAY JOIN steps. Now it shows both
above one limited sort with the filter notEmpty(arr2) AND notEmpty(arr).
@clickhouse-gh

clickhouse-gh Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [ac55c5e]

Summary: ✅


AI Review

Summary

PR #131004 extends bounded sorting pushdown through consecutive ARRAY JOIN steps, reusing the sort's existing limit. Reviewed head ac55c5e96b9546c928703bf7e7ee4d1abebc1e96. The added benchmarks address the earlier coverage gap, but the claimed performance improvement still needs measured evidence. No new inline findings.

Findings

⚠️ Majors

  • tests/performance/top_k_through_array_join.xml:65: Performance benefit and memory tradeoff remain unverified. The three new chained benchmarks cover ordinary and long arrays, addressing the coverage portion of the existing finding. However, no base-versus-head timings or peak-memory measurements are available in the discussion or current performance reports; the selected performance jobs remain pending. Moving the sort below another expansion makes it retain whole arrays, so reduced row counts alone do not establish the runtime and memory effect. Provide those measurements for the added queries, including the long-array cases. The existing thread remains open; no duplicate comment was posted.
Tests
  • ⚠️ Run the added chained benchmarks against the base and head with query_plan_top_k_through_array_join = 1, and report elapsed time and peak memory. Comparing the setting off and on only on head would also include the pre-existing single-step optimization. The head fast-test report passes all five relevant functional tests, but does not measure the performance claim.
Final Verdict

Status: ⚠️ Request changes. Supply base-versus-head runtime and peak-memory results for the chained benchmarks, and address any material regression they reveal.

@clickhouse-gh clickhouse-gh Bot added pr-performance Pull request with some performance improvements comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... labels Nov 12, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing ac55c5e96 with master 9b41283be (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.

Slowest function optimization changes (ThinLTO)

Median per-function time ratio to the master baseline: programs/clickhouse ×0.76, programs/clickhouse-keeper ×0.86 (each binary is linked separately, on a different machine and with different build flags); deltas below are relative to it.

Binary Function Master PR Δ vs median
programs/clickhouse DB::registerFunctionConversion(DB::FunctionFactory&) 5.7 s 16.5 s +12.2 s (+280%)
programs/clickhouse _ZN6detail10static_forITk7is_enumN2DB23AttributeUnderlyingTypeEZNS1_29callOnDictionaryAttributeType… 3.4 s 9.9 s +7.4 s (+287%)
programs/clickhouse _ZN6detail10static_forITk7is_enumN2DB23AttributeUnderlyingTypeEZNS1_29callOnDictionaryAttributeType… 2.6 s 7.2 s +5.2 s (+266%)
Compile time of recompiled translation units

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

Job report

@yariks5s
yariks5s requested a review from nihalzp November 13, 2026 13:42
@nihalzp nihalzp self-assigned this Nov 13, 2026
@nihalzp
nihalzp added this pull request to the merge queue Nov 13, 2026
Merged via the queue into master with commit 9cd0b1d Nov 13, 2026
203 checks passed
@nihalzp
nihalzp deleted the yarik/top-k-through-array-join-chain branch November 13, 2026 17:59
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 13, 2026
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 24, 2026
robot-ch-test-poll2 added a commit that referenced this pull request Nov 24, 2026
Cherry pick #131004 to 26.10: TopK through multiple ARRAY JOINs
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 24, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 24, 2026
Backport #131004 to 26.10: TopK through multiple ARRAY JOINs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... 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.10-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants