Skip to content

Do not read in order across exchanges kept by distributed_plan_optimize_exchanges = 0 - #127220

Open
shankar-iyer wants to merge 3 commits into
ClickHouse:masterfrom
shankar-iyer:read_in_order_without_optimize_exchanges
Open

shankar-iyer wants to merge 3 commits into
ClickHouse:masterfrom
shankar-iyer:read_in_order_without_optimize_exchanges

Conversation

@shankar-iyer

@shankar-iyer shankar-iyer commented Oct 21, 2026 •

Copy link
Copy Markdown
Member

With distributed_plan_read_in_order = 1, the read-in-order optimization descends a keyless scatter/gather pair whose bucket counts match and lets the read read in order, assuming optimizeExchanges will collapse the pair. With distributed_plan_optimize_exchanges = 0 that never happens: the pair stays between the read and the sorting and delivers rows in arrival order, while the sorting trusts the input order and skips sorting. ORDER BY id LIMIT 20 OFFSET 24990 over a table with several parts returned rows from the wrong part of the table in every run locally.

The fix descends such pairs only when exchanges are optimized. The new integration test test_read_in_order_with_exchanges_not_optimized covers it.

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):

Fix wrong results of ORDER BY ... LIMIT in a distributed query plan with distributed_plan_read_in_order = 1 and distributed_plan_optimize_exchanges = 0: the read was read in order across an exchange that reorders rows.

🤖 Generated with Claude Code


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

…ze_exchanges = 0

With `distributed_plan_read_in_order = 1`, `findReadingStep` descends a keyless scatter/gather
pair with equal bucket counts and lets the read read in order, assuming `optimizeExchanges`
collapses the pair. With `distributed_plan_optimize_exchanges = 0` the pair stays in the plan
and delivers rows in arrival order, while the sorting skips sorting, so `ORDER BY ... LIMIT`
returned wrong rows. Descend such pairs only when exchanges are optimized.

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

clickhouse-gh Bot commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [0e5ecda]

Summary: ✅


AI Review

Summary

Prevents read-order optimizations from crossing exchanges that survive when distributed_plan_optimize_exchanges = 0, fixing incorrect ORDER BY ... LIMIT results. No remaining actionable findings. The prior regression-coverage concern is addressed: the strengthened test fails on the unfixed reference and passes on the PR build.

Final Verdict

✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... labels Oct 21, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 0e5ecda10 with master 7be300255 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 7fc386e15; 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, 19 s compile time in total, 7 of them have a recent master baseline.

Job report

`test_read_in_order_with_exchanges_not_optimized` compared results only, and
those depend on the arrival order of the rows, so it also passed on builds
without the fix. Check the plan instead: with `optimize_read_in_order = 1`
pinned, the read must not be `InOrder` and the sorting must not have a
`Prefix sort description`. Without the fix both are present above the
surviving `ScatterExchange`.

Verified with the CI binaries: fails with master `f1c2aa5dd79`, passes with
the PR build of `0485391fcef`.

Addresses the AI review comment on ClickHouse#127220
Bugfix validation: https://s3.amazonaws.com/clickhouse-test-reports/PRs/127220/0485391fcef347e83b5043dec30211df2f2571f8/pr/bugfix_validation_integration_tests_amd64/job.log

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

This branch has not been deployed

No deployments
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-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant