Skip to content

Enable query_plan_top_k_through_array_join by default - #128705

Merged
nihalzp merged 1 commit into
masterfrom
yarik/enable-top-k-through-array-join
Nov 4, 2026
Merged

nihalzp merged 1 commit into
masterfrom
yarik/enable-top-k-through-array-join

Conversation

@yariks5s

@yariks5s yariks5s commented Nov 2, 2026 •

Copy link
Copy Markdown
Member

Related: #116550
Related: #115157

Changelog category (leave one):

  • Performance Improvement

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

Enable query_plan_top_k_through_array_join by default. A query with ARRAY JOIN and ORDER BY ... LIMIT on columns that are not joined now sorts and limits the rows before ARRAY JOIN, which uses less memory.


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

Version info

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

@clickhouse-gh

clickhouse-gh Bot commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [dd38098]

Summary: ✅


AI Review

Summary

Enables query_plan_top_k_through_array_join by default. Reviewed head dd3809881ef8dc9eb5d7b00c7f16bd00c9045a1f; there were no prior review threads. The default change introduces a reproduced memory regression that turns a successful query into MEMORY_LIMIT_EXCEEDED, so the default flip should be blocked until retention is bounded.

PR Metadata

The Performance Improvement category fits the intended change, but the required changelog entry incorrectly promises lower memory use generally. If enabling proceeds after addressing the finding, replace the entry with:

Enable query_plan_top_k_through_array_join by default. Eligible queries with ARRAY JOIN and ORDER BY ... LIMIT on columns independent of the expansion can sort before expanding arrays, reducing sorting work. Sorting complete arrays can increase memory use; set query_plan_top_k_through_array_join = 0 to restore the previous behavior.

Findings

❌ Blockers

  • src/Core/Settings.cpp:7907: Enabling the rewrite unconditionally increases retained sort payload from expanded elements to complete arrays. On the existing topk_array_join_long benchmark fixture, the exact-head Fast Test binary used 44.59 MiB with the setting off versus 160.37 MiB on, across three runs. SELECT k, arr FROM topk_array_join_long ARRAY JOIN arr ORDER BY k LIMIT 1000 SETTINGS max_threads = 1, max_memory_usage = 100000000 FORMAT Null succeeds with the setting off and raises MEMORY_LIMIT_EXCEEDED with it on or omitted. Keep the default disabled until eligibility or retention bounds prevent this regression.
Tests
  • ⚠️ Add a benchmark arm that omits query_plan_top_k_through_array_join and compare peak memory as well as time. The current XML explicitly sets 0 and 1 on both binaries, masking the effect of changing the default in the normal before/after comparison.
  • ⚠️ Release-build performance comparisons for both architectures were pending when inspected. The local Fast Test reproduction establishes the memory failure, but does not establish release-build speedups or their magnitude.
Final Verdict

Status: ❌ Block. Keep the setting disabled or bound the memory retained by the rewrite before enabling it globally; validate the actual default behavior and correct the changelog's unconditional memory claim.

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Nov 2, 2026
Comment thread src/Core/Settings.cpp
)", 0, \
{"26.5", false, true, "New setting to enable a query-plan-level optimization that pushes ORDER BY ... LIMIT n through a LEFT/RIGHT join when the sort key only references the preserved side."}) \
DECLARE(Bool, query_plan_top_k_through_array_join, false, R"(
DECLARE(Bool, query_plan_top_k_through_array_join, true, R"(

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.

[P1] Preserve bounded memory before enabling this by default

This makes an existing long-array query exceed its memory limit by default. Using the exact-head Fast Test binary and the topk_array_join_long fixture from tests/performance/top_k_through_array_join.xml (8,192 rows, arrays of 4,096–4,097 UInt32 elements), I measured 44.59 MiB peak with the setting off versus 160.37 MiB with it on, consistently across three runs.

SELECT k, arr
FROM topk_array_join_long ARRAY JOIN arr
ORDER BY k LIMIT 1000
SETTINGS max_threads = 1, max_memory_usage = 100000000
FORMAT Null;

This succeeds with query_plan_top_k_through_array_join = 0, but raises MEMORY_LIMIT_EXCEEDED with 1 or with the new default. The moved SortingStep retains up to limit + offset complete arrays rather than that many expanded elements, and its eligibility checks impose no bound on that retained payload. The changelog's claim that this uses less memory therefore also does not hold generally.

Please keep the default disabled until the rewrite has a memory-aware eligibility check or bounded retention strategy. Include a benchmark arm without an explicit setting override when validating the flip: the existing XML forces 0 and 1 on both binaries, so its normal before/after comparison masks the default change.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will be covered by #128926

@clickhouse-gh clickhouse-gh Bot added the comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... label Nov 2, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing dd3809881 with master c9d6538bf (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, 18 s compile time in total, 7 of them have a recent master baseline.

Job report

@nihalzp nihalzp self-assigned this Nov 4, 2026
@nihalzp
nihalzp added this pull request to the merge queue Nov 4, 2026
Merged via the queue into master with commit 10746f6 Nov 4, 2026
179 checks passed
@nihalzp
nihalzp deleted the yarik/enable-top-k-through-array-join branch November 4, 2026 13:49
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 4, 2026
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-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