Skip to content

Fix Merge tables logical error - #112326

Merged
PedroTadim merged 31 commits into
masterfrom
fix-merge-table
Aug 3, 2026
Merged

PedroTadim merged 31 commits into
masterfrom
fix-merge-table

Conversation

@PedroTadim

@PedroTadim PedroTadim commented Jul 28, 2026 •

Copy link
Copy Markdown
Member

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

Fixes a logical error when PREWHERE is used on a Merge table over another Merge table or a materialized view whose column type differs. Also fixes a row policy being silently ignored on tables that read from remote servers (a Distributed table, or a table wrapping one), which could expose rows the policy was meant to hide; such queries are now rejected with an ILLEGAL_PREWHERE error. Define these policies on the underlying local tables on each remote server instead; note that such a policy is still not applied to reads shipped with serialize_query_plan = 1, which is a separate, pre-existing bug.

Closes: #112315
Related: #112891

Version info

  • Merged into: 26.8.1.656 (included in 26.8 and later)

@clickhouse-gh

clickhouse-gh Bot commented Jul 28, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [53fa42b]

Summary: ✅


AI Review

Summary

This PR hardens the PREWHERE / row-policy pushdown contract across delegating storages (Merge, MaterializedView, Buffer, proxies, and table-function proxies), rejects unsafe remote-backed row-policy pushdown with an ILLEGAL_PREWHERE exception instead of silently dropping the filter, and documents the serialize_query_plan = 1 limitation. I reviewed the current diff and prior thread history against the live code; the earlier correctness gaps I checked now appear addressed, and the added stateless/integration coverage matches the bug-fix contract.

Final Verdict
  • Status: ✅ Approve

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.60% 86.50% -0.10%
Functions 91.90% 91.90% +0.00%
Branches 78.80% 78.80% +0.00%

Changed lines: Changed C/C++ lines covered: 254/269 (94.42%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 28, 2026
Comment thread src/Storages/StorageMerge.cpp Resolved
Comment thread src/Storages/StorageMerge.cpp Resolved
Comment thread src/Storages/StorageProxy.h Resolved
Comment thread src/Storages/StorageProxy.h Resolved

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now good.

@alexey-milovidov alexey-milovidov self-assigned this Jul 29, 2026
@PedroTadim
PedroTadim enabled auto-merge July 29, 2026 07:49
Comment thread src/Storages/StorageProxy.h Resolved
Comment thread src/Storages/StorageProxy.h Resolved
Comment thread src/Storages/StorageMerge.cpp Resolved
Comment thread src/Storages/StorageTableFunction.h Resolved
Comment thread src/Storages/StorageBuffer.cpp Resolved
@PedroTadim
PedroTadim requested a review from a team as a code owner July 31, 2026 14:01
…lize_query_plan = 1` limitation

A row policy defined on the shard-local table is not applied to a read
shipped to a non-local replica when `serialize_query_plan = 1`: the
receiving server executes an already-built plan and never applies its own
row policies. That is a pre-existing bug on master, filed as
#112891, but it makes the
"define the policy on the underlying local tables" migration path this
change documents incomplete.

Qualify it everywhere it is stated: the two `ILLEGAL_PREWHERE` messages,
`docs/reference/statements/create/row-policy.mdx`,
`docs/concepts/features/security/access-rights.mdx` and
`docs/resources/support-center/knowledge-base/security/row-column-policy.mdx`.

Also add the "Distributed and remote-backed tables" section to the live
Mintlify page `docs/reference/statements/create/row-policy.mdx`; it had
only been added to the legacy, unpublished `docs/en` tree.
@clickhouse-gh

clickhouse-gh Bot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.50% 86.50% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 78.70% 78.70% +0.00%

Changed lines: Changed C/C++ lines covered: 255/269 (94.80%) · Uncovered code

Full report · Diff report

@PedroTadim
PedroTadim added this pull request to the merge queue Aug 3, 2026
Merged via the queue into master with commit f45cb41 Aug 3, 2026
179 checks passed
@PedroTadim
PedroTadim deleted the fix-merge-table branch August 3, 2026 02:05
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 3, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Aug 3, 2026
…0216

Master (ClickHouse#112326) reworked the PREWHERE contract and the row-level-security
pushdown gate that this branch had also touched, so the shared parts are now
taken from master and only the Iceberg-specific half plus the snapshot
threading remain here.

Resolution:

* supportedPrewhereColumns keeps this branch's StorageSnapshotPtr parameter.
  Master derives the answer from a fresh getInMemoryMetadataPtr(), which for a
  data lake can observe a commit that landed after analysis picked the schema;
  the pinned snapshot is what closes that window. The parameter is threaded
  through every override master added since the fork (Buffer, Proxy,
  TableFunction, DatabaseURL) and into master's own recursive calls in Merge,
  MaterializedView and Buffer, which previously asked children with no snapshot.
* InterpreterSelectQuery::canPushRowPolicyToSource is deleted in favour of
  master's shouldPushRowLevelFilterToStorage, which is a superset: it also
  refuses remote storages and resolves subcolumns through
  prewhereSupportedColumnsContain. It is called from four sites, so the local
  gate would have been a second, weaker copy. Its addPrewhereAliasActions
  guard is likewise dropped: master's get_prewhere_columns already excludes a
  policy that will not be pushed.
* The child/target intersections this branch added to StorageMerge and
  StorageMaterializedView are dropped: master added equivalent ones, plus an
  undeclared-column check this branch lacked.
* supportedPrewhereColumnsIncludeSubcolumns, new in master, is kept everywhere.
* contrib/libpng, whose submodule master removed in 9573cb0, was left on
  disk by the merge as an untracked directory. utils/list-licenses walks
  contrib/ on the filesystem rather than the index, so the stale directory
  failed the license scan and blocked cmake; it is moved aside.

Verified on the merged tree against a same-day master build, on an Iceberg
table whose identity-partition column is absent from the data files: master
reads the column empty and returns no rows for a filter on it, the merged
branch backfills it and returns the rows with optimize_move_to_prewhere both
on and off, and an explicit PREWHERE on that column still raises
ILLEGAL_PREWHERE.
pull Bot pushed a commit to peijunlin2008/ClickHouse that referenced this pull request Aug 4, 2026
vdimir added a commit that referenced this pull request Aug 18, 2026
Without plan optimizations the chain below the sort re-materializes the
constant of a constant `SELECT` item while still taking a same-named
input, so an `ExpressionStep` DAG holds a `COLUMN` node named as one of
its inputs. Splitting such a DAG for lazy materialization renamed the
node promoted to the lazy half (`avoid_duplicate_inputs`), and the
re-stacked `Sorting` step no longer resolved its sort description:
`Not found column 2_UInt8 in block ... 2_UInt8_0`.

Fixed indirectly by #112326, whose `hasInputNameShadowedByComputedNode`
declines such plans. Its own test covers only the row-policy
`CAST(x, ...) AS x` shape, so pin the constant `SELECT` item with
`ORDER BY ALL` as well.

Related: #112209
Related: #112326

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
robinld pushed a commit to robinld/ClickHouse that referenced this pull request Oct 14, 2026
…processes the query

`ReadFromMerge::convertAndFilterSourceStream` added every child's row policy as a
`FilterStep` on top of the child plan, whatever built that plan.

When a `Merge` table also reads a `Distributed` table its stage is above
`FetchColumns`, so `createPlanForTable` builds a local child (e.g. MergeTree) with
`InterpreterSelectQueryAnalyzer`, whose planner already applies the child's policy
below its own aggregation. The `Merge` then applied it a second time over the
processed header, and every query failed with `NOT_FOUND_COLUMN_IN_BLOCK`. The child
plan now records that the interpreter built it, and the `Merge` skips its own copy.

A child that reports a stage above `FetchColumns` itself (`Distributed`, and tables
over one) sorts, limits, aggregates and computes extremes before the `Merge` sees a
row, so a filter above it was applied too late: `ORDER BY ... LIMIT` returned no
rows, `extremes = 1` printed values of rows the policy hides or threw
`Got 1 in extremes chunk, expected 2`, and aggregates failed with
`NOT_FOUND_COLUMN_IN_BLOCK`. Such a child is now rejected with `ILLEGAL_PREWHERE`,
the rule a direct read of that table follows since ClickHouse#112326.

Related: ClickHouse#125580
Related: ClickHouse#112326

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

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.

Logical error: Unexpected return type from A. Expected B. Got C (STID: 3262-472c)

3 participants