Repository navigation
Fix the one-argument merge table function reading the default database of other servers in distributed queries - #122985
Conversation
`merge('regexp')` takes its database from the current database of the
server that resolves it. The analyzer resolved the node with the
initiator's current database but kept the single argument in the query
tree, so every rendering of the query for another server (Distributed,
remote, cluster, `*Cluster` table functions, query-based parallel
replicas, serialized plans) sent `merge('regexp')`, and the receiver
matched the regexp against its own default database: wrong rows, or
CANNOT_EXTRACT_TABLE_STRUCTURE when that database has no matching table.
After resolving a one-argument `merge`, insert the source database as
the first argument of the node. The change is made in the query tree and
not only in the rendered text, because an `IN` set is named after the
tree hash of its subquery and that name appears in the columns of the
WithMergeableState header that the initiator matches by name: with a
render-only change, `countIf(x IN (SELECT ... FROM merge('re')))` over
two shards fails with NOT_FOUND_COLUMN_IN_BLOCK.
A secondary query keeps the form it was sent in, so that its tree hashes
like the sender's: an initiator of an older version sends one argument,
and a receiver that inserted the database would fail the same way.
This is the resolve-time part of ClickHouse#110972, which ClickHouse#115001 superseded
without carrying it.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Drop the clause narrating the inserted argument and state the secondary-query condition as one invariant line. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 0 findings), plus my own review of the same head (1 nit).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20260929-221600 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-6:20260929-213700 |
|
Workflow [PR], commit [ad1a658] Summary: ✅
|
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Slowest function optimization changes (ThinLTO)Median per-function time ratio to the master baseline:
Compile time of recompiled translation units19 translation units recompiled, 178 s compile time in total, 19 of them have a recent master baseline. |
CI finish ledger - b8a7c50Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
146 of 146 check runs completed (106 passed, 39 not selected, 1 red: the shard above), Session id: cron:our-pr-ci-monitor:20260930-033029 |
…e-function-implicit-database-remote
|
@groeneai is this PR still needed? |
|
🕵️ Reviewed, @groeneai — correct and well-targeted fix. CI all-green. ✅ Approve. The fix rewrites the one-argument
💡 Nit (optional, defensive)The argument is inserted at the front after |
|
Yes. Today's master still resolves a one-argument
On the nit, I kept the order. For a one-argument call, index 0 can enter the skip set only as Not covered by this PR, same query shape on today's master: |
… in distributed queries
`merge` treats the database `''` as the current database: `TableFunctionMerge::parseArguments`
resolves it through `evaluateConstantExpressionForDatabaseName`, but only in its AST copy and in
the `StorageMerge` it builds. The query tree keeps `ConstantNode('')`, and the tree is what is
rendered or serialized for the other servers of a distributed query. Their current database is
their own default (`ClusterProxy::stripDatabaseSetting`), so each of them read its own `default`:
`CANNOT_EXTRACT_TABLE_STRUCTURE`, or wrong rows when `default` had matching tables.
ClickHouse#122985 fixed the one-argument form in `QueryAnalyzer::resolveTableFunction` by writing the
resolved database into the tree. The same block now also replaces a two-argument `merge` whose
database argument is a constant empty `String` with the resolved database. `''` is the only value
whose meaning depends on the evaluating server: other strings are absolute names, constant
expressions are folded on the initiator, and `REGEXP(...)` is not a constant. Keying on the value
covers every spelling that folds to `''` (`concat('', '')`, `toNullable('')`, a query parameter).
The replacement is in place, so the skipped-argument indexes keep their positions.
As in ClickHouse#122985 the tree changes, not only the sent text, because `IN` set names are hashed from
the tree; a secondary query is left as sent, so an older initiator with a newer shard keeps
working, and a newer initiator sends an ordinary `merge('<db>', re)` to an older shard.
Closes ClickHouse#131316
Co-Authored-By: Claude Opus 5.5 <[email protected]>
A table function that reads the current database, through a name without
a database (`loop(t)`, `dictionary('d')`, `timeSeriesSamples(ts)`) or
through '' (`mergeTreeIndex('', t)`), kept that argument as written in the
query tree. That tree is what remote(), Distributed, *Cluster and
parallel replicas send to other servers, and each of them resolved the
name in its own default database: an IN or JOIN subquery of a
distributed query read other rows or failed with UNKNOWN_TABLE.
As asked in
ClickHouse#131589 (comment),
the knowledge of which arguments name the database moves to the table
function, which computes it in parseArguments: the new virtual
ITableFunction::getQualifiedArguments returns the leading arguments with
that database written out, and QueryAnalyzer::resolveTableFunction
substitutes them in one place. This replaces the `merge` binder of ClickHouse#122985
and ClickHouse#131528 and the `loop` / `dictionary` binder of the first version of
this PR; StorageMerge::getSourceDatabaseNameOrRegexp, which only the
`merge` binder used, is removed. Calls already written that way keep
their nodes. The exclusions are unchanged: the table of remote() or
cluster(), a query printed from a resolved tree (serialize_query_plan
shard plans) and a secondary query.
Implemented by merge, loop, dictionary (a database name containing a dot
stays unqualified), mergeTreeIndex, mergeTreeAnalyzeIndexes,
mergeTreeCodecBlockCounts, mergeTreeTextIndex, mergeTreeProjection,
timeSeriesSamples / Tags / TimeRanges / MetricFamilies (and their
aliases), timeSeriesSelector, prometheusQuery and prometheusQueryRange.
The TimeSeries functions use the `num_table_arguments` names of ClickHouse#128194.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: #122931 (comment)
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed the one-argument form of the
mergetable function,merge('regexp'), in a subquery, aJOINor a CTE of a query that is sent to other servers (remote,cluster, aDistributedtable,*Clustertable functions, parallel replicas): those servers matched the regexp against their own default database instead of the current database of the query, which returned wrong results or failed withCANNOT_EXTRACT_TABLE_STRUCTURE.Description
On 26.8.2.7 and master, with a current database other than
default:The analyzer resolves
merge('regexp')against the current database, but keeps the single argument in the query tree. So the query sent to another server saysmerge('regexp'), and that server resolves it against its own default database (the session database is not sent, on purpose).QueryAnalyzer::resolveTableFunctionnow adds the resolved database as the first argument of the node. It goes into the query tree, not only into the sent text, becauseINset names are hashed from the tree and must match between the initiator and the shards: a text-only change failscountIf(x IN (SELECT ... FROM merge('regexp')))over two shards withNOT_FOUND_COLUMN_IN_BLOCK(measured). A secondary query keeps the form it was sent in, so an older initiator keeps working with a newer shard (without that condition the same query fails). This is the resolve-time part of #110972, which was closed when #115001 superseded it.The new test covers
remotewithIN, a CTE, aJOIN,countIfover two shards, andfileCluster; each query fails on master and passes here. Also measured: aDistributedtable,cluster(), parallel replicas andserialize_query_plan.Not changed:
loop(t),dictionary('d')andjoinGet('t', ...)in a query sent to another server are resolved in its default database too.Workflow [PR]
Sync PR [sync-upstream/pr/122985]
Version info
26.11.1.2168-master(included in26.11and later)