Repository navigation
Report the missing named collection in remote()/Remote instead of reparsing the call positionally - #113510
Conversation
…positionally
parseRemoteFunctionArguments looked the named collection up with
throw_unknown_collection = false, so a missing collection returned nullptr, the
named-collection branch was skipped, and parsing fell through to the positional
branch. There an override argument such as `database = merge('d', '^t$')` is not
a table function, so it is analyzed as an ordinary data expression and `merge` is
resolved against FunctionFactory instead of TableFunctionFactory, giving
``Code: 46 UNKNOWN_FUNCTION: Function with name `merge` does not exist``. A
`sharding_key = rand()` override fails in the same place with `Code: 47
UNKNOWN_IDENTIFIER`. Since ATTACH TABLE replays on-disk metadata through this
parser, an operator whose collection went missing sees the misleading error while
the server refuses to start (722 <- 695 <- 46).
The first argument of remote() may also be a cluster name, and that form
legitimately needs the positional branch, so the lookup cannot simply be made
fatal: before this change 11 files under tests/queries/0_stateless already used
`remote(<cluster name>, ...)`, and test_shard_localhost is a cluster rather than a
named collection. The two shapes are distinguishable without resolving anything,
because a `key = value` argument is an `equals` ASTFunction and no positional
signature accepts one as the second argument: args[1] is consumed by the
database/table arm before the credentials and sharding-key block, which only runs
with arg_num >= 2. The lookup is therefore made fatal exactly for that shape and
NamedCollectionFactory::get raises the existing NAMED_COLLECTION_DOESNT_EXIST.
The check is deliberately restricted to args[1] rather than scanning every later
argument: sharding_key accepts an arbitrary AST, so
`remote(cluster, db, table, x = 1)` is a valid positional call whose sharding key
is an equality expression, and a broader scan would reject it. That form is
covered by a test arm.
No currently-succeeding query changes behaviour: every affected call already
failed with 46 or 47, so only the error code and message change. No explicit
check that the first argument is an identifier is needed, because
getCollectionName returns nullopt for a non-identifier before the flag is read,
which leaves the `remote('host:port', ...)` form untouched.
This fixes the parse and the error, not startup survival: a Remote table whose
collection is missing still throws at load time, now naming the right thing.
StorageURL.cpp is the only other call site passing false and is left alone, since
it belongs to separate work on tolerating a missing collection at startup.
Validated on a debug build with the collection missing: the engine and table
function, with both a table-function and a non-table-function override, report
669 and name the collection, against 46/47 before the change. The cluster-name
form, an equality expression used as a positional sharding key, an unknown
cluster (CLUSTER_DOESNT_EXIST) and a live collection are unchanged, as is
04511_remote_named_collection_complex_overrides. Three mutants each redden a
different test arm. 100 of 100 randomized runs of the new test plus 04511.
Internal second-model review (click to expand)Two review rounds by a model independent of the one that wrote the patch, plus two
Independently re-derived rather than taken from the patch author's notes:
Deliberately not claimed in this PR: startup survival (a Session id: cron:clickhouse-review-slot-9:20260805-133800 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-review-slot-9:20260805-133800 |
|
cc @azat @KochetovNicolai, could you review this? |
|
Workflow [PR], commit [93007dc] Summary: ❌
AI ReviewSummaryThis PR makes Final Verdict
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 8/8 (100.00%) · Uncovered code |
CI finish ledger - 93007dcEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task whose
All 174 check-runs completed and Session id: cron:our-pr-ci-monitor:20260805-230000 |
| NamedCollectionPtr named_collection; | ||
| VectorWithMemoryTracking<std::pair<std::string, ASTPtr>> complex_args; | ||
| if (!is_cluster_function && (named_collection = tryGetNamedCollectionWithOverrides(args, context, false, &complex_args, dependent_table_id))) | ||
| /// A `key = value` second argument occurs only in the named-collection form: no positional |
There was a problem hiding this comment.
@groeneai are you sure this issue doesn't exist with other table functions with key = value arguments?
There was a problem hiding this comment.
Fair question, and I had not measured it, so I did. No: remote/remoteSecure are the only ones. key = value support is not the discriminator -- the caller must also resolve the collection non-fatally, and only these two did.
The helper defaults to throw_unknown_collection = true (NamedCollectionsHelpers.h:31), routing to NamedCollectionFactory::get, which raises NAMED_COLLECTION_DOESNT_EXIST (NamedCollectionsFactory.cpp:54-66). Only a caller passing false gets tryGet's nullptr (:85-93), which is what makes the if fail and lets parsing continue positionally. So the census is just the flag:
git grep -n tryGetNamedCollectionWithOverrides -- src/
27 AST-form call sites on master: 25 pass true, exactly 2 pass false -- parseRemoteFunctionArguments.cpp:91 (this PR) and StorageURL.cpp:2163. The 6 dictionary sites use the config-form overload, which always calls get. :2163 is not a parsing path: it is inside overrideURLInEngineArgs, which reads the url key opportunistically; the URL engine parses at :2195 with true. After this PR :2163 is the only false left.
Not wanting to leave that as a code-reading argument, I probed every registered table function and engine on a pristine master build against a collection that does not exist. Of 93 table functions, 48 report 669 (including 17 of the 18 *Cluster variants) and 41 never attempt a lookup; the only 4 doing neither are remote, remoteSecure, cluster, clusterAllReplicas. Of 83 engines, 30 report 669 and only Remote/RemoteSecure do not.
cluster/clusterAllReplicas never reach the collection branch, which is guarded by !is_cluster_function (:91, comment at :86). Measured rather than assumed: cluster() gives the same 170 BAD_GET when the collection exists, and for a string-literal first argument, so it just means "key = value is not a cluster() argument". The nearest counterexample is mergeTreeIndex, which does take key = value arguments but whose first argument is a database name, so there is no lookup to fall through from. Both unchanged.
One behaviour change worth flagging rather than letting you find it: remote(<cluster name>, database = 'd') also moves from 170 to 669 There is no named collection `<cluster name>` . That combination has no valid reading -- no positional signature accepts key = value second, and the collection form needs the collection to exist -- so both are rejections and 669 is the accurate one. Plain positional remote(<cluster name>, db, table) is untouched, which is why the false was there originally.
Base vs this build, per shape
The symptom on master is three different errors depending on which key is overridden, all invented by the positional reparse:
| shape | master | this PR |
|---|---|---|
remote(nc_missing, database = 'd') |
170 BAD_GET |
669 |
remote(nc_missing, database = merge('x','^y$')) |
46 Function with name `merge` does not exist |
669 |
remote(nc_missing, sharding_key = rand()) |
47 Unknown ... identifier `sharding_key` |
669 |
remoteSecure(...), ENGINE = Remote(...), ENGINE = RemoteSecure(...) |
170 |
669 |
url/s3/mysql/mongodb and their engines |
669 |
669, unchanged |
BACKUP ... TO S3(nc_missing, ...) |
36, already names the collection |
36, unchanged |
Probe (*Cluster variants take the collection at argument 2):
SELECT * FROM <tf>(nc_missing, database = 'd');
CREATE TABLE t (x UInt8) ENGINE = <E>(nc_missing, database = 'd');
SELECT * FROM <tf>('some_cluster', nc_missing, database = 'd');f845f92
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed the error reported when the
remote/remoteSecuretable functions or theRemote/RemoteSecuretable engines are given a named collection that does not exist together with akey = valueoverride. Such a call was silently reparsed as a positional call, so the override was analyzed as a data expression and the reported error named something unrelated (for exampleFunction with name `merge` does not existforremote(collection, database = merge(...))). The missing named collection is now reported.Description
parseRemoteFunctionArgumentslooked the collection up withthrow_unknown_collection = false, so a missing collection returnednullptr, the named-collection branch was skipped, and parsing fell through to the positional branch. There the override is analyzed as an ordinary expression, sodatabase = merge(...)resolvesmergeagainstFunctionFactoryinstead ofTableFunctionFactory(46),database = 'd'folds to aUInt8where aStringis read (170), andsharding_key = rand()has no resolvable identifier (47).ATTACH TABLEreplays on-disk metadata through this parser, so an operator whose collection went missing sees the misleading error while the server refuses to start.The first argument may also be a cluster name, and that form needs the positional branch, so the lookup cannot simply be made fatal. The two shapes are distinguishable without resolving anything: a
key = valuesecond argument is anequalsASTFunction, and no positional signature accepts one there (the second argument is consumed as the database/table before any sharding key). The lookup is made fatal exactly for that shape, andNamedCollectionFactory::getraises the existingNAMED_COLLECTION_DOESNT_EXIST.Only the error changes: every affected call already failed, with
170 BAD_GET,46 UNKNOWN_FUNCTIONor47 UNKNOWN_IDENTIFIERdepending on the override. A positional sharding key that is an equality expression (remote(cluster, db, table, x = 1)) keeps working because it is not the second argument.This fixes the parse and the error, not startup survival: #110529 is doing that for the URL engine, and
StorageURL.cppis deliberately left alone here.Validated on a debug build: 4 arms report 669 with the fix and 46/47 without it (170 for a plain literal); the cluster-name form, the positional-equality-sharding-key form, an unknown cluster (
CLUSTER_DOESNT_EXIST) and a live collection are unchanged. Three mutants each redden a different arm. 100/100 randomized runs of the new test plus04511.Version info
26.8.1.908(included in26.8and later)