Skip to content

Remove the legacy read-in-order and optimize-to-prewhere implementations - #121390

Merged
CurtizJ merged 16 commits into
ClickHouse:masterfrom
CurtizJ:remove-legacy-read-in-order-and-prewhere
Oct 2, 2026
Merged

CurtizJ merged 16 commits into
ClickHouse:masterfrom
CurtizJ:remove-legacy-read-in-order-and-prewhere

Conversation

@CurtizJ

@CurtizJ CurtizJ commented Sep 21, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Backward Incompatible Change

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

Settings query_plan_read_in_order, query_plan_optimize_prewhere and query_plan_aggregation_in_order are obsolete and no longer have any effect. They used to select the legacy implementations of the read-in-order, aggregation-in-order and WHERE to PREWHERE optimizations in the old analyzer, which were removed. Use optimize_read_in_order, optimize_aggregation_in_order and optimize_move_to_prewhere to disable the corresponding optimization.


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

Version info

  • Merged into: 26.10.1.1234-master (included in 26.10 and later)

…where` obsolete

Both settings selected between the query plan implementation of an
optimization and the legacy implementation in `InterpreterSelectQuery`.
The plan-level implementations have been the default for a long time,
so the legacy ones are removed and the settings are made obsolete.

- Remove `ReadInOrderOptimizer` and `SelectQueryInfo::order_optimizer`
  together with the old-interpreter plumbing (`executeOrderOptimized`,
  `order_by_elements_actions`, `group_by_elements_actions`). The plan
  optimization is now gated by `optimize_read_in_order` alone.
- Remove the AST-based `MergeTreeWhereOptimizer::optimize` and the
  `try_move_to_prewhere` pass of `InterpreterSelectQuery`. The plan
  optimization is now gated by `optimize_move_to_prewhere`, which
  `optimizePrewhere` already re-checks per read step.
- Remove the AST side of `RPNBuilderTreeNode` and `RPNBuilderTreeContext`,
  leaving only `ActionsDAG` nodes. `KeyCondition::getConstant` and the
  `tryGetPreparedSet(DataTypes)` overload go with it. The statistics unit
  tests build an `ActionsDAG` via `TreeRewriter` and `ExpressionAnalyzer`
  instead of wrapping an AST.
- Stop randomizing `query_plan_optimize_prewhere` in `clickhouse-test` and
  drop both settings from the tests, replacing
  `query_plan_optimize_prewhere = 0` with `optimize_move_to_prewhere = 0`
  and `query_plan_read_in_order = 0` with `optimize_read_in_order = 0`.

With `query_plan_enable_optimizations = 0` the old interpreter previously
still moved conditions to `PREWHERE` through the AST path; it no longer does.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [c3d49d4]

Summary: ❌

job_name test_name status info comment
Stateless tests (arm_binary, sequential) FAIL
00899_long_attach_memory_limit FAIL cidb IGNORED

AI Review

Summary

This PR removes the legacy interpreter/AST implementations behind query_plan_read_in_order, query_plan_optimize_prewhere, and query_plan_aggregation_in_order, and makes those three settings obsolete. The cleanup direction is sound, but the current branch still leaves user-visible contract gaps: the migration docs were not regenerated, the new behavior silently adds an undocumented dependency on query_plan_enable_optimizations, and Buffer reads still lose optimizer coverage once the plan goes through a UnionStep.

PR Metadata
  • ⚠️ Changelog category is correct, but the Changelog entry is incomplete: it tells users to switch to optimize_read_in_order, optimize_move_to_prewhere, and optimize_aggregation_in_order, while the current code also makes all three plan implementations depend on query_plan_enable_optimizations = 1. Suggested replacement text: Settings query_plan_read_in_order, query_plan_optimize_prewhere, and query_plan_aggregation_in_order are obsolete and ignored. Use optimize_read_in_order, optimize_move_to_prewhere, and optimize_aggregation_in_order to control the corresponding plan-level optimizations when query_plan_enable_optimizations = 1.
Missing context / blind spots
  • ⚠️ No local build was available in this checkout, so I could not run a live stateless repro for the Buffer-specific regressions. A one-test run that fills the buffer before the SELECT would close that gap, but the current code paths below already make the regression visible.
Findings

⚠️ Majors

  • [src/Processors/QueryPlan/Optimizations/optimizePrewhere.cpp:184] [dismissed by author -- https://github.com/ClickHouse/ClickHouse/pull/121390#discussion_r4077025846] Automatic PREWHERE still stops at a UnionStep. That was masked before by the legacy AST rewrite, but after this PR a non-empty Buffer table has no remaining path that can populate query_info.prewhere_info: StorageBuffer::read only consumes precomputed prewhere_info, and optimizePrewhere still only handles FilterStep -> SourceStepWithFilter and explicitly does not descend through the UnionStep that Buffer builds once it has in-memory rows. This turns optimize_move_to_prewhere = 1 into a silent no-op for non-empty Buffer reads.
  • [src/Storages/StorageBuffer.cpp:512] [dismissed by author -- https://github.com/ClickHouse/ClickHouse/pull/121390#discussion_r4067466836] StorageBuffer still knows how to use query_info.input_order_info, but nothing in the current planner can set it for GROUP BY over the UnionStep emitted by a non-empty Buffer table: optimizeAggregationInOrder still has TODO: maybe add support for UNION later. The old interpreter path used to supply that order info; after its removal the same query silently falls back from streaming aggregation to hash aggregation as soon as buffered rows are present.
  • [src/Processors/QueryPlan/Optimizations/QueryPlanOptimizationSettings.cpp:250] [dismissed by author -- https://github.com/ClickHouse/ClickHouse/pull/121390#discussion_r4093625033] optimize_move_to_prewhere now silently depends on query_plan_enable_optimizations = 1. With the legacy AST fallback gone, this line makes optimize_move_to_prewhere = 1, query_plan_enable_optimizations = 0 a no-op, but neither the replacement setting description in src/Core/Settings.cpp nor the PR body says that the migration now requires the global query-plan switch too.
  • [src/Processors/QueryPlan/Optimizations/QueryPlanOptimizationSettings.cpp:261] optimize_read_in_order and optimize_aggregation_in_order have the same new hidden dependency on query_plan_enable_optimizations = 1. The obsolete-setting text in src/Core/Settings.cpp tells users to move to the optimize_* knobs, but the source descriptions for those knobs still present them as direct toggles with no mention of the global gate, so the advertised compatibility path is incomplete.
  • [src/Core/Settings.cpp:10772] The settings docs/snippets were not regenerated from the new obsoletion metadata. docs/reference/settings/session-settings/query-plan.mdx still documents query_plan_read_in_order, query_plan_optimize_prewhere, and query_plan_aggregation_in_order as live query-plan toggles, and docs/snippets/components/SessionSettingsExplorer/SessionSettingsExplorer.jsx still advertises the same entries. After this PR the public docs would tell upgraders to flip three no-op settings.
Tests
  • ⚠️ [src/Core/Settings.cpp:10777] There is still no focused stateless test that the three obsolete settings are accepted after upgrade, reported as is_obsolete, and ignored in favor of optimize_read_in_order, optimize_move_to_prewhere, and optimize_aggregation_in_order. The current test edits mostly remove old call-site pins, so the promised compatibility path itself remains unpinned.
Final Verdict

⚠️ Changes requested: the obsoletion itself looks reasonable, but the current branch still has undocumented behavior changes around query_plan_enable_optimizations, stale public settings docs, and Buffer-specific optimizer regressions that were previously covered by the legacy paths.

LLVM Coverage Report

Measured on commit c3d49d4.

Metric Baseline Current Δ
Lines 88.30% 88.30% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 79.70% 79.70% +0.00%

Changed lines: Changed C/C++ lines covered: 232/263 (88.21%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-backward-incompatible Pull request with backwards incompatible changes label Sep 21, 2026
@CurtizJ CurtizJ changed the title Make settings query_plan_read_in_order and query_plan_optimize_prewhere obsolete and remove the legacy read-in-order and PREWHERE implementations Remove the legacy read-in-order and optimize-to-prewhere implementations Sep 21, 2026
Comment thread src/Interpreters/ExpressionAnalyzer.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing c3d49d45a with master d96270ddd (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.

Object file sizes

148 object files changed (-215.86 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Interpreters/InterpreterSelectQuery.cpp.o 1.14 MiB 1.06 MiB -77.10 KiB (-6.62%)
src/CMakeFiles/dbms.dir/Storages/MergeTree/RPNBuilder.cpp.o 185.67 KiB 146.05 KiB -39.62 KiB (-21.34%)
src/CMakeFiles/dbms.dir/Storages/MergeTree/MergeTreeWhereOptimizer.cpp.o 231.01 KiB 206.85 KiB -24.16 KiB (-10.46%)

717 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

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

Job report

CurtizJ and others added 2 commits September 21, 2026 22:16
The previous commit removed the legacy interpreter-level aggregation-in-order
path (`group_by_elements_actions`), but `QueryPlanOptimizationSettings` still
required `query_plan_aggregation_in_order = 1` to enable the query plan
implementation. With the legacy path gone, `query_plan_aggregation_in_order = 0`
silently turned `optimize_aggregation_in_order` into a no-op instead of
selecting the legacy implementation.

Make the setting obsolete, the same way as `query_plan_read_in_order` and
`query_plan_optimize_prewhere`, and gate the plan optimization by
`optimize_aggregation_in_order` alone. Drop the setting from BuzzHouse and
from the tests that set it explicitly.

Addresses review comment:
ClickHouse#121390 (comment)

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Comment thread src/Storages/StorageBuffer.cpp Resolved
After the AST-based index analysis path was removed, `RPNBuilderTreeContext`
held nothing but a `ContextPtr`: `block_with_constants` and `prepared_sets`
were only ever valid for the AST tree, and constants and sets are read from
the `COLUMN` nodes of the `ActionsDAG` instead.

`RPNBuilderTreeNode` now inherits from `WithContext` and carries the query
context itself, so callers no longer have to keep a separate context object
alive next to the node. The unused `getSettings` is dropped along with it.

Co-Authored-By: Claude Opus 5 <[email protected]>
@clickhouse-gh clickhouse-gh Bot added the comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... label Sep 22, 2026
CurtizJ and others added 5 commits September 23, 2026 11:51
CurtizJ and others added 5 commits September 24, 2026 23:02
… line in `Settings.cpp`

ClickHouse#121390

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…d-in-order-and-prewhere

Settings history moved inline into `DECLARE`/`MAKE_OBSOLETE` on master, so the 26.10
records for `query_plan_read_in_order`, `query_plan_optimize_prewhere` and
`query_plan_aggregation_in_order` are now attached to their `MAKE_OBSOLETE` entries,
together with the earlier history records from master.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…d-in-order-and-prewhere

Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts:
#	src/Storages/MergeTree/MergeTreeIndexConditionText.cpp
#	src/Storages/MergeTree/RPNBuilder.cpp
#	src/Storages/MergeTree/RPNBuilder.h
Comment thread src/Core/Settings.cpp Resolved
Comment thread src/Core/Settings.cpp Resolved
@CurtizJ
CurtizJ added this pull request to the merge queue Oct 1, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Oct 1, 2026
@CurtizJ
CurtizJ added this pull request to the merge queue Oct 1, 2026
Merged via the queue into ClickHouse:master with commit 6b33d32 Oct 2, 2026
147 of 149 checks passed
@CurtizJ
CurtizJ deleted the remove-legacy-read-in-order-and-prewhere branch October 2, 2026 00:56
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 2, 2026
@CurtizJ CurtizJ self-assigned this Oct 4, 2026
fm4v pushed a commit to BoloniniD/ClickHouse that referenced this pull request Oct 12, 2026
…subcolumns

Conflicts:
- StorageMaterializedView.cpp, StorageMerge.cpp: master removed the legacy
  `order_optimizer` read-in-order path (ClickHouse#121390); kept the access-check changes.
- TableFunctionMergeTreeIndex.cpp, TableFunctionMergeTreeTextIndex.cpp: master wraps
  `getTable` in `resolveStorageProxyLoading` (ClickHouse#121332); kept the `SHOW_TABLES` check
  before the lookup and the pinned metadata snapshot.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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-backward-incompatible Pull request with backwards incompatible changes 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