Skip to content

Keep sorting below PASTE JOIN in removeRedundantSorting - #128556

Merged
alexey-milovidov merged 3 commits into
ClickHouse:masterfrom
hsusul:fix/paste-join-redundant-sorting
Nov 3, 2026
Merged

alexey-milovidov merged 3 commits into
ClickHouse:masterfrom
hsusul:fix/paste-join-redundant-sorting

Conversation

@hsusul

@hsusul hsusul commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Closes: #72693

Why

PASTE JOIN pairs rows by position, so the ORDER BY in its subqueries defines the result. removeRedundantSorting removed those sorts when an order-independent aggregate or an outer ORDER BY sat above the join. SELECT sum(a * b) over ORDER BY a DESC paste-joined with ORDER BY b returns 30 instead of 10. Sorting below a PASTE JOIN is now kept.

What changed

Scope

  • Only sorts on the path through a PASTE JOIN. Other joins keep the optimization, and the test checks this with an INNER JOIN.

Verification

  • The new test fails on master (26.11.1.986) in the aggregation, outer ORDER BY and EXPLAIN cases.
  • Each expected value is the master output with query_plan_remove_redundant_sorting = 0, and matches the arithmetic.

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fixed wrong results of PASTE JOIN when the ORDER BY in its subqueries was removed by the query_plan_remove_redundant_sorting optimization, for example under an aggregation or an outer ORDER BY.


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

Version info

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

PASTE JOIN matches rows by position, so ORDER BY in its inputs defines the result and is not redundant even under an order-independent aggregation or an outer ORDER BY. Check both JoinStepLogical and JoinStep for the PASTE kind, as filterPushDown already does.
@alexey-milovidov alexey-milovidov self-assigned this Nov 2, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Nov 2, 2026
@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

Workflow [PR], commit [7f0f00c]

Summary: ✅


AI Review

Summary

PR #128556 preserves input sorting that determines row pairing in PASTE JOIN, protecting both logical and physical join steps from removeRedundantSorting. No actionable findings at head 7f0f00cafd08ac125624903ec3e55841a895a150. The regression test 05492_paste_join_redundant_sorting passes in the Fast test report.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Nov 2, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 7f0f00caf with master ab83663d4 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of e720b7367; 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

@clickhouse-gh clickhouse-gh Bot added the external Author is not a member of the ClickHouse organization label Nov 2, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Nov 2, 2026
Merged via the queue into ClickHouse:master with commit 546a185 Nov 3, 2026
179 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 Nov 3, 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 3, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 3, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 3, 2026
Backport #128556 to 26.10: Keep sorting below PASTE JOIN in removeRedundantSorting
leshikus pushed a commit that referenced this pull request Nov 13, 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-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... external Author is not a member of the ClickHouse organization pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR 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.

aggregation on paste join does not work correctly

5 participants