Skip to content

Fix ReadInOrder for merge table with filter push-down to prewhere - #105867

Merged
KochetovNicolai merged 2 commits into
masterfrom
fix-merge-read-in-order-86313
May 27, 2026
Merged

KochetovNicolai merged 2 commits into
masterfrom
fix-merge-read-in-order-86313

Conversation

@KochetovNicolai

@KochetovNicolai KochetovNicolai commented May 26, 2026 •

Copy link
Copy Markdown
Member
Claude

When a query through the merge table function has a WHERE clause that can be pushed down to the prewhere of the underlying MergeTree, the read-in-order optimization was silently disabled. The plan ended up sorting rows even though the underlying parts were already sorted by the requested prefix of the primary key.

Two pieces together broke the sort-key match:

  1. buildSortingDAG collected expression and prewhere chains only above ReadFromMerge. After filter push-down, the column-renaming and materialize aliasing performed by convertAndFilterSourceStream was hidden inside the merge node, so the outer plan no longer contained the alias chain from __table1.X back to the storage's sort-key column.

  2. matchTrees treated materialize/identity as monotonic functions with is_strict = false. Even when the chain was visible, that non-strict match truncated the matched sort-key prefix to a single column.

Fix:

  • Walk into each child plan of ReadFromMerge while building the per-table DAG. The outer DAG is cloned and merged on top of the inner DAG; outer_fixed_columns pointers are remapped through the clone mapping and the mergeInplace input map.
  • Teach matchTrees to look through single-argument materialize / identity calls and pass the matched child through, like ALIAS.

Closes: #86313

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 read-in-order for Merge tables (with a new analyzer).

Version info

  • Merged into: 26.6.1.190

When a query through the `merge` table function has a WHERE clause that
can be pushed down to the prewhere of the underlying `MergeTree`, the
read-in-order optimization was silently disabled. The plan ended up
sorting rows even though the underlying parts were already sorted by
the requested prefix of the primary key.

Two pieces together broke the sort-key match:

1. `buildSortingDAG` collected expression and prewhere chains only above
   `ReadFromMerge`. After filter push-down, the column-renaming and
   `materialize` aliasing performed by `convertAndFilterSourceStream`
   was hidden inside the merge node, so the outer plan no longer
   contained the alias chain from `__table1.X` back to the storage's
   sort-key column.

2. `matchTrees` treated `materialize`/`identity` as monotonic functions
   with `is_strict = false`. Even when the chain was visible, that
   non-strict match truncated the matched sort-key prefix to a single
   column.

Fix:

- Walk into each child plan of `ReadFromMerge` while building the
  per-table DAG. The outer DAG is cloned and merged on top of the
  inner DAG; `outer_fixed_columns` pointers are remapped through the
  clone mapping and the `mergeInplace` input map.
- Teach `matchTrees` to look through single-argument `materialize` /
  `identity` calls and pass the matched child through, like `ALIAS`.

Closes: #86313

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented May 26, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [b4d7e56]

Summary: ❌

job_name test_name status info comment
Fast test (arm_darwin) DROPPED IGNORED

AI Review

Summary

This PR fixes ReadInOrder detection for merge reads when filter push-down moves aliases/materialization into child plans, by combining child and outer DAGs per table and making matchTrees treat materialize/identity as pass-through wrappers. The change is coherent with the reported failure mode, and the added regression test covers the affected query shape. No unresolved contract violations were identified in the current diff.

Final Verdict

Status: ✅ Approve

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

clickhouse-gh Bot commented May 26, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.20% 84.10% -0.10%
Functions 91.40% 91.40% +0.00%
Branches 76.60% 76.60% +0.00%

Changed lines: 77.53% (69/89) | lost baseline coverage: 1 line(s) · Uncovered code

Full report · Diff report

@Avogar Avogar self-assigned this May 27, 2026
@KochetovNicolai
KochetovNicolai added this pull request to the merge queue May 27, 2026
Merged via the queue into master with commit 8432c4b May 27, 2026
324 of 327 checks passed
@KochetovNicolai
KochetovNicolai deleted the fix-merge-read-in-order-86313 branch May 27, 2026 14:41
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label May 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Merge table engine sorting not using ReadInOrder in some plans

3 participants