Repository navigation
Conversation
… servers
`loop(t)` / `loop('', t)` and `dictionary('d')` without a database in a subquery
that a distributed query sends to other servers (an `IN` / `JOIN` subquery of
`remote`, a `Distributed` table, `*Cluster` table functions) were resolved by each
receiving server in its own default database instead of the current database of
the query: wrong rows when that database has an object of that name, otherwise
`UNKNOWN_TABLE` / `Dictionary (...) not found`. The analyzer kept the arguments as
written; only the local execution resolved the missing database.
Bind them at resolve time, like the one-argument `merge` (ClickHouse#122985) and `dictGet`:
`loop(t)` / `loop('', t)` become `loop('<current database>', 't')`, and the
`dictionary` name goes through `qualifyDictionaryNameWithDatabase`, the helper of
the `dictGet` binder. Secondary queries are left as sent, so the `IN` set names
hashed from the query tree agree between initiator and shard, also across versions.
The table of `remote` / `remoteSecure` / `cluster` / `clusterAllReplicas`, and
anything nested in it, is not bound: the remote server reads it in its own
database, as in releases. `buildQueryTreeDistributed` resolves it again as the
root of a separate query tree, so a `remote_target` flag (set for that root and
inherited through the argument loop) returns before every binder. This also undoes
the master-only binding of `remote('h', merge('^t'))` from ClickHouse#122985, which bound only
the rows (initiator's database) while the structure came from `DESCRIBE` on the
remote server (its default database).
The rewrites keep argument positions valid: `TableFunctionLoop` and
`TableFunctionDictionary` do not override `skipAnalysisForArguments`, and an
`IdentifierNode` argument never enters the skipped-argument list, so replacing the
one-identifier `loop` argument list leaves that list empty; the other two rewrites
are in place. A nested call binds its own node before the parent renders its AST,
so `loop(dictionary('d'))` carries the bound inner call.
With serialize_query_plan = 1 the plan of each shard is built on the initiator from
the printed query; such an analysis (any query printed from a resolved query tree)
skips the binders as well, since the query already went through them.
The dictionary name is left unqualified when the database name contains a dot: no
dictionary-name syntax can carry such a database, and writing it broke a local
`loop(dictionary(...))` from such a database. Tests: a database with a dot in its
name, and `remote('h', loop(t))`, whose table the remote server still resolves in
its own default database, also through tables created `AS remote('h', loop(t))` /
`AS remote('h', merge('^t'))` with their own columns, which skip the structure
lookup, with and without serialize_query_plan.
Related: ClickHouse#131316
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Drop the cross-file citation from the remote_target caller comment. 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; 3 findings over 3 rounds, the last one clean) plus my own cold reviews and the validation runs (1 more finding).
Also noticed, not raised: Severity: ❌ blocker / |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-6:20261114-095400 |
|
Workflow [PR], commit [427c312] Summary: ✅
AI ReviewSummaryPR #131589 qualifies database-bearing table-function arguments through Consumers of the rewritten arguments traced:
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 Compile time of recompiled translation units120 translation units recompiled, 1184 s compile time in total, 120 of them have a recent master baseline. |
CI finish ledger - a3f0fe2Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20261114-150101 |
|
🕵️ @groeneai The binders look right: The concern is the approach: this is the second per-function fix (after #122985, with #131528 next), and the same class remains in several table functions. Every function whose
Instead of a |
|
Agreed, I'll rework this PR that way. #131528 merged this morning, so its
@ nickitat's draft #128194 adds |
…-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]>
With parallel replicas the TimeSeries functions are sent written out as well (the first problem of ClickHouse#118130). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Drop the QualifiedArguments struct comment (it repeated the method doc) and the skip-index shift comment, shorten the getQualifiedArguments doc and the two num_table_arguments field comments to the invariant. Co-Authored-By: Claude Opus 5.5 <[email protected]>
loop and dictionary table functions reading the default database of other servers in distributed queries
Internal second-model review of the rework: adjudication log (click to expand)Review of the
Severity: ❌ blocker / |
|
cc @vitlibar @yakov-olkhovskiy, could you review this? It replaces the per-function merge, loop and dictionary binders in resolveTableFunction with one ITableFunction::getQualifiedArguments method, so table functions that read the current database (loop, dictionary, the mergeTree* functions, the timeSeries* functions, prometheusQuery) send it written out to remote servers and parallel replicas instead of reading their default database. |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-5:20261116-111200 |
|
Done in 6c528f4 : Implemented by The test now covers one function per kind of argument ( |
Related: #131316
Related: #118130
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed wrong results of table functions that read the current database, such as
loop,dictionary,mergeTreeIndex,mergeTreeProjectionandtimeSeriesSamples, given a name without a database or''in a subquery orJOINof a distributed query (remote,Distributedtables,*Clustertable functions) or with parallel replicas: the other servers read it from their own default database instead, or failed when it did not exist there.Description
On 26.10.2.103, from a database
dbwheredb.tholds2, 3anddefault.tholds0,SELECT sum(number), count() FROM remote('127.0.0.2', numbers(4)) WHERE number IN (SELECT x FROM loop(t) LIMIT 2)returns0 1instead of5 2.mergeTreeProjection('', t, p)anddictionary('d')behave the same; without such an object indefaultthe query fails.Root cause: such a call keeps that argument as written in the query tree, which is what other servers receive, and each resolves it in its own default database. #122985 and #131528 fixed
mergewith a binder of its own.Change: the new
ITableFunction::getQualifiedArgumentsreturns the leading arguments with the database the call resolved written out (loop(t)is'db', 't',dictionary('d')is'db.d'), andQueryAnalyzer::resolveTableFunctionsubstitutes them in one place, replacing themerge,loopanddictionarybinders. With parallel replicas,timeSeriesTags(ts)and the other TimeSeries functions now reach the replicas written out too (#118130's first problem). It is implemented bymerge,loop,dictionary,mergeTreeIndex,mergeTreeAnalyzeIndexes,mergeTreeCodecBlockCounts,mergeTreeTextIndex,mergeTreeProjection, thetimeSeries*functions,timeSeriesSelectorandprometheusQuery(Range); #128194 can use it instead ofgetTableArgument. As before, the table ofremote/clusteris left to the remote server, and neither secondary queries nor the shard plan ofserialize_query_planare rebound.Not changed: names inside a
viewquery (#131770), anddictionaryfrom a database whose name contains a dot.Validation: the new tests fail on master and pass with the fix, 50/50 randomized.
🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/131589]