Skip to content

Don't Force Normal Projection Optimization to Have Filter - #104680

Merged
Michicosun merged 5 commits into
masterfrom
do_not_force_projection_usage_to_have_filter
May 12, 2026
Merged

Michicosun merged 5 commits into
masterfrom
do_not_force_projection_usage_to_have_filter

Conversation

@Michicosun

@Michicosun Michicosun commented May 12, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

This patch changes when optimizeUseNormalProjections can be used and allows a specific case where nothing was filtered, but the projection's sorting key can help remove the sorting step of a query.

Version info

  • Merged into: 26.5.1.579

@Michicosun Michicosun changed the title Do't Force Normal Projection Optimization to Have Filter Don't Force Normal Projection Optimization to Have Filter May 12, 2026
@clickhouse-gh

clickhouse-gh Bot commented May 12, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [0524f16]

Summary: ✅


AI Review

Summary

This PR relaxes optimizeUseNormalProjections so normal projections can be considered when a query has no filter but has an outer ORDER BY that may benefit from read-in-order, and it adds a stateless regression test for that path. I did not find unresolved correctness, safety, or evidence gaps in the current diff.

Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label May 12, 2026
@yariks5s yariks5s self-assigned this May 12, 2026
@clickhouse-gh

clickhouse-gh Bot commented May 12, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 91.10% 91.10% +0.00%
Branches 76.50% 76.50% +0.00%

Changed lines: 100.00% (12/12) · Uncovered code

Full report · Diff report

@Michicosun
Michicosun enabled auto-merge May 12, 2026 18:16
@Michicosun
Michicosun added this pull request to the merge queue May 12, 2026
Merged via the queue into master with commit 12f07ad May 12, 2026
165 checks passed
@Michicosun
Michicosun deleted the do_not_force_projection_usage_to_have_filter branch May 12, 2026 19:06
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label May 12, 2026
alexey-milovidov pushed a commit to tuanpach/ClickHouse that referenced this pull request Jul 20, 2026
…ow expects

Projection metadata is re-derived from the projection query text at every
table load, so the believed column set of a projection can drift from the
stored column set of existing projection parts. Two native triggers: parts
written by <= 26.2 for a projection selecting an ALIAS column store only the
sort key (26.3+ derive the alias source as a physical projection column), and
ALTER TABLE ... MODIFY COLUMN re-pointing an ALIAS used by a projection is
allowed without a projection rebuild. Since 26.5 the planner also selects
normal projections for sorting without a filter (ClickHouse#103723, ClickHouse#104680), so a
plain SELECT reads the drifted part through the projection, fills the missing
column with the type default, and silently returns wrong data. Background
merges that merge projection parts directly read the same default fill and
bake the wrong values permanently into the merged projection part. The same
shape breaks aggregate projections: the aggregate-state column name embeds
the expanded alias (`sum(plus(b, 1))`), so a re-pointed alias leaves old
parts without the state column the metadata now expects and reads return
default aggregate states.

A column is missing legitimately only when it is a parent-table column the
parent part also lacks (added by `ALTER ADD COLUMN` after the part was
written) - the default fill is then correct and identical on either read
path. Everything else counts as drift: `analyzeProjectionCandidate` now reads
such parts from the base table (covers normal and aggregate projections;
virtual columns are exempt since the reading step provides them), and
`prepareProjectionsToMergeAndRebuild` rebuilds the projection from the merged
parent data instead of merging drifted projection parts directly (skipped
under `deduplicate_merge_projection_mode = 'ignore'`, mirroring the
expired-columns rule).

Co-Authored-By: Claude Fable 5 <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…ow expects

Projection metadata is re-derived from the projection query text at every
table load, so the believed column set of a projection can drift from the
stored column set of existing projection parts. Two native triggers: parts
written by <= 26.2 for a projection selecting an ALIAS column store only the
sort key (26.3+ derive the alias source as a physical projection column), and
ALTER TABLE ... MODIFY COLUMN re-pointing an ALIAS used by a projection is
allowed without a projection rebuild. Since 26.5 the planner also selects
normal projections for sorting without a filter (ClickHouse#103723, ClickHouse#104680), so a
plain SELECT reads the drifted part through the projection, fills the missing
column with the type default, and silently returns wrong data. Background
merges that merge projection parts directly read the same default fill and
bake the wrong values permanently into the merged projection part. The same
shape breaks aggregate projections: the aggregate-state column name embeds
the expanded alias (`sum(plus(b, 1))`), so a re-pointed alias leaves old
parts without the state column the metadata now expects and reads return
default aggregate states.

A column is missing legitimately only when it is a parent-table column the
parent part also lacks (added by `ALTER ADD COLUMN` after the part was
written) - the default fill is then correct and identical on either read
path. Everything else counts as drift: `analyzeProjectionCandidate` now reads
such parts from the base table (covers normal and aggregate projections;
virtual columns are exempt since the reading step provides them), and
`prepareProjectionsToMergeAndRebuild` rebuilds the projection from the merged
parent data instead of merging drifted projection parts directly (skipped
under `deduplicate_merge_projection_mode = 'ignore'`, mirroring the
expired-columns rule).

Co-Authored-By: Claude Fable 5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator

26.3 is still affected by the crash that the second hunk of this PR fixes. On 26.3.60.2-lts (the latest 26.3 release) this aborts (measured with clickhouse local):

CREATE TABLE t (a UInt64, PROJECTION p (SELECT * ORDER BY a)) ENGINE = MergeTree ORDER BY a;
INSERT INTO t VALUES (1);
SELECT count() OVER () FROM t SETTINGS force_optimize_projection = 1;
contrib/llvm-project/libcxx/include/__vector/vector.h:436: libc++ Hardening assertion !empty() failed: front() called on an empty vector

Without a filter, optimizeUseNormalProjections still sets projection_query_info.filter_actions_dag from query.dag. Here that DAG is empty (no nodes, no outputs), and ReadFromMergeTree::buildIndexes (via analyzeProjectionCandidate -> estimateNumMarksToRead) calls getOutputs().front() on it. if (query.dag && query.filter_node) from this PR removes that. The other hunk is inside if (!force_optimize_projection), so it does not matter for this query.

Measured with official and CI amd_release binaries:

  • 26.3.60.2 and 26.4.1.1141 abort; 26.5.1.882, 26.8.34.13, 26.9.25.15 and 26.10.2.103 return 1.
  • The merge commit of this PR (12f07ad) returns 1 3/3; its first parent (04b22c4) aborts 3/3.

A plain v26.3-must-backport on this PR would not build on 26.3: its first hunk uses outer_sorting_step from #103723, which 26.3 does not have.

Does 26.3 need this fix, and in which form: (a) v26.3-must-backport on #103723 and on this PR, (b) only the one-line if (query.dag && query.filter_node) change on 26.3, or (c) no backport, since it needs force_optimize_projection = 1?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-not-for-changelog This PR should not be mentioned in the changelog 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.

4 participants