Repository navigation
Fix merge('', regexp) reading the default database of other servers in distributed queries - #131528
Conversation
… 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]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 0 findings) plus my own cold review (1 nit).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261114-042800 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-6:20261114-025700 |
|
Workflow [PR], commit [b80e0b6] Summary: ✅
AI ReviewSummaryThe PR binds the database resolved by Missing context / blind spots
Final VerdictStatus: ✅ Approve. |
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 units7 translation units recompiled, 20 s compile time in total, 7 of them have a recent master baseline. |
A database that evaluates to `''` is not always a `ConstantNode`: a `LowCardinality` scalar subquery,
`merge((SELECT toLowCardinality('')), re)`, stays a `__getScalar` call in the query tree, so the previous
check skipped it and the other servers again read their own default database (`0 1` instead of `5 2`
in an `IN` subquery, a wrong `JOIN` result, `NOT_FOUND_COLUMN_IN_BLOCK` over two shards).
For any database other than a `REGEXP`, the name `merge` resolved is the absolute database it read, so
it now replaces the database argument whatever its form. A `REGEXP` is the one form that is not
analyzed (`TableFunctionMerge::skipAnalysisForArguments`) and is kept. For a non-empty name the value
does not change; only the tree form does. The secondary-query guard is unchanged.
Found by the automated review of ClickHouse#131528.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2381469
…-remote Needed by the next commit, which replaces the merge binder that ClickHouse#131528 changed.
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]>
Closes: #131316
Related: #122985
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed the
mergetable function with an empty database,merge('', 'regexp'), in a subquery or aJOINof a query that is sent to other servers (remote, aDistributedtable,*Clustertable functions, parallel replicas): those servers read the tables of 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.10.2.103 and master, with a current database other than
default:mergetreats the database''as the current database, but the query tree keeps'', so the query sent to another server saysmerge('', '^t_')and that server reads its own default database. #122985 fixed the same for the one-argument form by writing the resolved database into the query tree inQueryAnalyzer::resolveTableFunction; the same place now also writes the resolved database over the database argument, whatever its form, unless it is aREGEXP. As before, it is the tree that changes (INset names are hashed from it), and a secondary query is left as sent, so mixed versions keep working (measured both ways).The test
05300_merge_table_function_implicit_database_remotegets themerge('', ...)variants of itsIN,JOIN, two-shardcountIfandfileClusterqueries, plus''as aLowCardinalityscalar subquery (it stays a__getScalarcall in the query tree); they fail without the fix. Also measured:serialize_query_plan, parallel replicas,''spelled asconcat('', ''),toNullable('')or a query parameter, and unchanged results for other names,currentDatabase()andREGEXP.loop(t)anddictionary('d'), also named in the issue, read the evaluating server's current database through other code and are fixed separately.Workflow [PR]
Sync PR [sync-upstream/pr/131528]
Version info
26.11.1.2421-master(included in26.11and later)