Repository navigation
Fix a crash when a read of a serialized query plan lands on a Merge table through a proxy table - #119387
Conversation
On the receiving side of a serialized query plan, replaceReadingFromTable rebuilds a read from a step that carries only a table name and the table expression modifiers, so the SelectQueryInfo it builds has no query, no query tree and no syntax analyzer result. It then either synthesizes a SELECT over the table and re-analyzes it, or reads the storage directly with that empty info. The choice was `storage->isRemote() || typeid_cast<const StorageMerge *>(...)`. The typeid_cast is an exact test on the storage that was addressed, not on the storage the read lands on, so a storage that forwards its read took the direct route. When it forwards to a Merge table, StorageMerge::read received an empty SelectQueryInfo and dereferenced its null query at the sampling probe in createChildrenPlans. getHeaderForProcessingStage does not catch this because it only dereferences query for stages above FetchColumns, and the direct route always passes FetchColumns. Guarding that dereference would not have been enough: getModifiedQueryInfo needs either a query tree or an AST plus its TreeRewriterResult, and this path supplies neither, so the fault would reappear in createPlanForTable. With parallel_replicas_plan_based and parallel_replicas_allow_merge_tables enabled it appears even earlier, in InterpreterSelectQuery::isQueryWithFinal, which dereferences query unconditionally. Both are fixed by routing correctly instead. readsThroughMergeTable() is named for what it detects rather than for the requirement it serves, because the same empty SelectQueryInfo also reaches external database storages, which do not override isRemote() either. Those do not crash: transformQueryForExternalDatabase throws UNSUPPORTED_METHOD "Query is not analyzed: no query tree". A predicate named for requiring an analyzed query would have been false for them. The carrier set is closed by census: every delegating isRemote() implementation in src/Storages belongs to one of the five classes touched here. ReadFinalForExternalReplicaStorage forwards read too, but its nested storage is always a ReplacingMergeTree, so it correctly inherits the false default. The StorageMerge.h include in resolveStorages.cpp is replaced by IStorage.h, which the file uses directly and had been getting only transitively.
Internal second-model review (3 findings, 0 blocking after adjudication)1. The route is classified from a name-based target binding that 2. Two test gaps, both deliberate. 3. Changelog category. Left at |
|
Workflow [PR], commit [5330f15] Summary: ✅
AI ReviewSummaryThis PR fixes the Final Verdict
LLVM Coverage Report |
|
cc @davenger @alexey-milovidov, could you review this? With |
The routing fix delegates readsThroughMergeTable() from five classes, but the
regression test only instantiates three of them. StorageProxy's delegation is
reached by a different carrier: in a database with lazy_load_tables = 1 an
unloaded table is a StorageTableProxy, and DatabaseOrdinary wraps a Merge table
in one just as it wraps a MergeTree. That proxy is what the catalog holds, so the
exact typeid_cast this fix replaces never matched it, and StorageTableProxy::read
forwarded the empty SelectQueryInfo straight into StorageMerge::read.
Measured with the shipped test and this one against three binaries: without the
fix both crash at the px != 0 assertion in createChildrenPlans; with it both
pass; with the fix minus the single StorageProxy line the shipped test still
passes and this one crashes with the same assertion and frames as the master CI
report. The line was therefore the one part of the change no test held.
The carrier needs a second database, which needs DETACH DATABASE / ATTACH
DATABASE of an Atomic database and hence no-replicated-database. That tag would
have removed the other arms from the DBReplicated stateless run and from the
one-in-three stress threads that pass --replicated-database, which is the lane
the original report came from, so the arm goes in a separate half instead, as
04652_nested_merge_prewhere_type_mismatch does for the same reason. The second
database is {CLICKHOUSE_DATABASE_1}, unique per run, so the test stays parallel.
Only the engine column of system.tables is read before the query: it is answered
by the proxy itself, while a column such as data_paths would materialize the
nested storage and leave the fixture testing a plain Merge table.
Co-Authored-By: Claude Opus 5 <[email protected]>
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units1283 translation units recompiled, 8402 s compile time in total, 1283 of them have a recent master baseline. |
`resolveStorages.cpp` routes a serialized-plan table read through `InterpreterSelectQueryAnalyzer` when the storage cannot read from a `SelectQueryInfo` that carries no query. That condition was spelled twice: once as `isRemote()`, once as a predicate naming `Merge`. Both terms ask the same question, whether `read` requires an analyzed query, so express that property once and let the base class stop naming one storage engine. `IStorage::readRequiresAnalyzedQuery()` defaults to `isRemote()`, which is why remote storages were in the condition in the first place: `StorageDistributed::read` builds its query tree out of `SelectQueryInfo` in `buildQueryTreeDistributed`. `StorageMerge` overrides it to true, and the four read-forwarding wrappers delegate it exactly as they already delegate `isRemote()`. The call site now reads one term. The routed set is unchanged. Measured on 12 read shapes through a serialized plan (plain `MergeTree`; `Merge`; `Alias`, `Buffer` and `MaterializedView` over `Merge`; `Alias` and `Buffer` over `MergeTree`; `Distributed`; `Alias` and `Buffer` over `Distributed`; and the two non-`Merge` shapes under `parallel_replicas_plan_based`): the receiving-side route recorded in `system.query_log.tables` is identical before and after. Changing the default from `isRemote()` to false segfaults the `Distributed` read in `buildQueryTreeDistributed`, so that default is load-bearing rather than incidental. The predicate is an opt-in claim, not a census: a false answer means the storage has not declared the requirement. External-database storages need an analyzed query too (`transformQueryForExternalDatabase` rejects a query-less `SelectQueryInfo`) and still answer false here, so they keep reporting an error on this path instead of being routed. Routing them is a behaviour change for storages this PR cannot exercise, so it stays out.
|
@alexey-milovidov, moving the design question here as you asked. On 2026-09-12 you called the predicate in The pushed shape is The alternative is no predicate at all: always synthesize the Reusing the existing Nothing is blocked on the answer, the PR stands on the pushed shape. If you would rather have no predicate, say so and I will measure what the extra analysis costs a plain |
CI finish ledger - 1311df6Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
176 checks, all completed: 52 success, 120 skipped (praktika dropped them after the failing shard), Session id: cron:our-pr-ci-monitor:20260922-230208 |
CI finish ledger - 1311df6: fixing-PR link for the NATS rowThe The container death is a heap-use-after-free in the vendored client. The Session id: cron:clickhouse-publish-slot-34:20260923-075700 |
…oxy-serialized-plan
…alized-query-plan-112891 Conflict in src/Processors/QueryPlan/resolveStorages.cpp with ClickHouse#119387, which routes a serialized-plan read through the interpreter when IStorage::readRequiresAnalyzedQuery() holds (remote, Merge, and the wrappers that forward to them) instead of testing isRemote() and an exact StorageMerge. Both sides are kept: the row-policy replan is computed only for a read that does not already go through the interpreter, and the interpreter route is taken for read_via_interpreter || needs_row_policy. A wrapper over Merge was a direct read before ClickHouse#119387, so needs_row_policy could select it; it is now on the interpreter route with default options, which resolve its policy like the Merge route always did. Master's new checkAccessRightsForColumnsResolvedAway sits under the same ignore_table_access_check guard. It checks only PREWHERE and indexHint columns, which the synthesized SELECT of a replanned read does not have. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
The fix is not on
Could this get |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a server crash when a
SELECTwithserialize_query_plan = 1read aMergetable through a table that forwards its reads, such asAlias,Bufferwith a destination, or aMATERIALIZED VIEWwith aTOtarget. Closes #119899.Description
With
serialize_query_plan = 1the receiving side rebuilds each read from a step carrying only a tablename, so its
SelectQueryInfohas no query. Reads needing one get aSELECTover the table synthesizedand re-analyzed; the rest read the storage directly.
That route was chosen by
typeid_cast<const StorageMerge *>, an exact test on the storage addressedrather than the one the read lands on. A table forwarding its read took the direct route, so when it
forwards to a
Mergetable,StorageMerge::readgot a nullqueryand dereferenced it atStorageMerge.cpp:1116. Withparallel_replicas_plan_basedandparallel_replicas_allow_merge_tableson, the same null is dereferenced earlier in the
FINALprobe (StorageMerge.cpp:1049), so guardingthe reported line would only move the crash.
The fix names that precondition once, as
IStorage::readRequiresAnalyzedQuery(): defaultisRemote(), since a remote read builds its query tree out ofSelectQueryInfo;trueforMerge;delegated by each read-forwarding storage for the table its read lands on, as it already delegates
isRemote()on the line above. The routing condition is now that one predicate.The new test covers the three carriers plus the parallel replicas variant and reddens without this
change; routing for a proxy over a non-
Mergetarget is measured unchanged.Reported on master by
Stress test (amd_asan_ubsan)atbf549ba1b4fe0c3de89373f336586671d8af50b8:runtime error: member call on null pointer of type 'DB::TypePromotion<DB::IAST>'atsrc/Storages/StorageMerge.cpp:1116:57(report).
Not addressed here: the same sparse
SelectQueryInforeaches external-database storages such asMySQL, which reportQuery is not analyzed: no query treerather than crashing. They answer false tothe new predicate, so that path is unchanged here.
Workflow [PR]
Sync PR [sync-upstream/pr/119387]
Version info
26.10.1.1227-master(included in26.10and later)