Repository navigation
Do not rebuild a parameterized identifier as a resolved table name - #112704
PedroTadim merged 5 commits into
Conversation
An ordinary view keeps the query-parameter placeholders of its SELECT body unsubstituted: they are the view's parameterizable interface, resolved when the view is called. A placeholder part is stored as an empty string in `name_parts` while its expression lives in `children`, and the compound `ASTIdentifier` ctor supports that only while the parameters travel with the parts. `ASTIdentifier::createTable` rebuilt the node from `name_parts` alone, so a parameterized identifier re-entered that ctor with an empty part and no parameters, took the branch that requires every part to be non-empty, and tripped `chassert(!part.empty())`. Make it return `nullptr` instead, which is the function's documented "not a table" answer and what it already returned for every other arity it cannot represent. All ten callers already handle `nullptr` with the same fallback they use for an unresolvable name. `AddDefaultDatabaseVisitor` reaches the same invariant by a second route: it qualifies a `dictGet` dictionary name through `identifier->name()` and a fresh `ASTIdentifier`, without going through `createTable`. Skip a parameterized name there too, next to the existing `compound()` skip. Beyond the assertion, the old rebuild produced an identifier whose database part degraded to the empty string, so the dependency scanner substituted the current database and recorded a referential dependency on a table the view may never read. A release build therefore refused to drop that unrelated table with HAVE_DEPENDENT_OBJECTS while leaving the database the parameter actually names unguarded. The target is only knowable at call time, so recording no dependency is the sound answer; that fabricated edge disappears. The stored placeholder is valid metadata a release build writes, so a debug or sanitizer build could not even start against such a data directory: the same rebuild runs from TablesLoader::buildDependencyGraph during table loading. Co-Authored-By: Claude Fable 5 <[email protected]>
Review round 2 found a fourth producer of the same `chassert(!part.empty())` that neither earlier hunk covered, and it is reachable. `AddDefaultDatabaseVisitor::visit(const ASTTableIdentifier &)` rebuilds a one-part name through `name` and the constructor rather than `createTable`, so it drops `name_params` the same way. A bare parameterized table reference normally never gets that far: `ApplyWithSubqueryVisitor` calls `getTableId` one statement earlier, and the resulting `StorageID` constructor calls `assertNotEmpty`, which throws `UNKNOWN_TABLE` for an empty name. That gate has an exemption: `StorageID::empty` is `table_name.empty() && !hasUUID()`, so an explicit non-Nil `UUID '...'` clause passes it, and the second check needs a non-empty database name that a one-part `getTableId` does not produce either. The same parser path accepts that clause on the same shape, parts first and the optional clause after, so the name reaches the rebuild one statement later. Measured on a pre-fix sanitizer build, three such shapes abort with `!part.empty()` at that site, while the same statements with an all-zero UUID, which parses to `Nil`, keep returning `UNKNOWN_TABLE` on both builds. That control pins the UUID value rather than the presence of the clause as the discriminating condition. Removing only the new early-out reddens exactly those three shapes and leaves the two previously fixed mechanisms green. The new cases live in their own test file because they need `no-debug`: the formatter drops an explicit `UUID` clause on a table reference, so the format-parse-format check in `executeQueryImpl`, which is compiled only when `NDEBUG` is not defined, raises `Inconsistent AST formatting` first. That is a separate pre-existing defect, reproducible with no query parameter at all. Sanitizer builds define `NDEBUG` yet still enable `chassert` and report `BUILD_TYPE` as `RelWithDebInfo`, so they run the file and keep the assertion covered, and the sibling test still carries every case a debug build can run. Also backtick the literal names in the comments touched by this change.
The `04655` stateless test needed an explicit `UUID '...'` clause on a table
reference to reach the `AddDefaultDatabaseVisitor` rebuild site: without the
clause `StorageID::assertNotEmpty` rejects the empty name first, and
`StorageID::empty` exempts a non-Nil UUID. Any statement carrying that clause
aborts a debug binary before execution for an unrelated pre-existing reason -
the formatter never emits the clause while `ASTTableIdentifier::updateTreeHashImpl`
hashes it, so the format-parse-format check in `executeQueryImpl` raises
`Inconsistent AST formatting`.
The `no-debug` tag that dodged this does not hold on Stress jobs: the whole
`no-{build_flag}` skip loop in `tests/clickhouse-test` is disabled under
`--stress-tests`, which `stress.py` passes for every thread, and both
`Stress test (amd_debug)` and `(arm_debug)` exist. There the test would kill
the server, and a `SERVER_DIED` result raises `StopTesting`, terminating the
whole run rather than failing one test. `no-stress` cannot be used: it is
rejected outright because stress tests must be able to run every test.
Replace it with a unit test that parses the statement and runs the visitor
directly, which never enters the format check. This is the carve-out of the
"prefer SQL tests" rule: the behaviour is not observable from SQL on the build
type where the assertion is live. The guard itself is unchanged and stays
load-bearing - reverting only it makes the new test abort at the same
`!part.empty()` assertion, while the ordinary-name control keeps passing.
The formatter defect is tracked separately; it reproduces with no query
parameters at all.
The comment above the parameterized-name test said the behaviour is "not
observable from SQL on the build type where the assertion is live". That is
wrong for sanitizer builds, which are such a build type: `chassert` is live
whenever `DEBUG_OR_SANITIZER_BUILD` is defined, and `base/base/sanitizer_defs.h`
defines it when any of `ADDRESS_SANITIZER`, `THREAD_SANITIZER`,
`MEMORY_SANITIZER` or `UNDEFINED_BEHAVIOR_SANITIZER` is set, even with `NDEBUG`
present. The format-parse-format check that makes such a statement abort early
is guarded by a plain `#ifndef NDEBUG` in `executeQueryImpl` instead, and
sanitizer builds do define `NDEBUG`. So on a sanitizer build the formatter check
is compiled out while the assertion stays, and the abort is reachable from SQL
there. The comment now says that, which is what the earlier commit of this
branch already stated.
The reason the coverage is a unit test rather than a stateless test is
unchanged, and the rationale moves here from the code. Reaching the
`AddDefaultDatabaseVisitor` rebuild site needs an explicit `UUID '...'` clause
on the table reference, because `StorageID::assertNotEmpty` throws
`UNKNOWN_TABLE` for an empty name and `StorageID::empty` is
`table_name.empty() && !hasUUID()`, so a non-Nil UUID passes it. On a debug
binary any statement carrying that clause hits a separate pre-existing formatter
defect first: the formatter never emits the clause while
`ASTTableIdentifier::updateTreeHashImpl` hashes it, so the format-parse-format
check raises `Inconsistent AST formatting`. The `04655` stateless test that
dodged this with `no-debug` could not stay, because the whole `no-{build_flag}`
skip loop in `tests/clickhouse-test` is disabled under `--stress-tests`, which
`stress.py` passes for every thread, and `Stress test (amd_debug)` exists. There
the test would kill the server, and a `SERVER_DIED` result raises `StopTesting`,
terminating the whole run rather than failing one test. `no-stress` is rejected
outright. The unit test parses the statement and runs the visitor directly, so
it never enters the format check, and it ships in `unit_tests_dbms`, which runs
as `Unit tests (asan_ubsan / tsan / msan)`, exactly the build types where the
assertion is live.
Comment text only. The guard, both tests, and the stateless test are unchanged.
Internal second-model review (round 5): 0 source blockers, 0 source majorsIndependent cold review plus an adversarial second-model gate, adjudicated against the Round 5 changed comment text only. The comment-stripped md5 of the one touched file is
Earlier rounds, for continuity: round 4 corrected the description to name both non- Verified independently this round, and deliberately not flagged:
Session id: cron:clickhouse-review-slot-48:20260731-033800 |
Pre-PR validation gate (click to expand)
Additional validation: 50 randomized runs of Session id: cron:clickhouse-review-slot-48:20260731-033800 |
|
cc @alexey-milovidov @SmitaRKulkarni, could you review this? |
|
Workflow [PR], commit [b4db0d4] Summary: ❌
AI ReviewSummaryThis PR fixes the Findings
Tests
Final VerdictChanges requested. The DDL-time assertion and dependency bug look fixed, but the legacy analyzer still appears to reject the |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 16/17 (94.12%) · Uncovered code |
CI finish ledger - b4db0d4Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task
Nothing here is caused by this PR's own diff: the diff is 5 files ( Session id: cron:our-pr-ci-monitor:20260801-023000 |
3f837bd
Related: #105799
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixes a logical error and a wrong dependency record when an ordinary
VIEWbody references a table or dictionary through a query parameter, as inCREATE VIEW v AS SELECT x FROM {db:Identifier}.t. Such a view no longer records a referential dependency on a same-named table of the current database, so unrelated tables can be dropped again.Description
An ordinary view keeps the query-parameter placeholders of its
SELECTbody unsubstituted: they are its parameterizable interface, resolved at call time. Such a part is stored as an empty string inname_partswhile its expression lives inchildren, and the compoundASTIdentifierconstructor supports that only while the parameters travel with the parts.ASTIdentifier::createTablerebuilt the node fromname_partsalone and dropped the parameters, so such an identifier re-entered that constructor with an empty part and no parameters, took the branch requiring every part to be non-empty, and trippedchassert(!part.empty()). It now returnsnullptr, the function's documented "not a table" answer and already its result for every other arity it cannot represent. All ten callers handlenullptrwith the fallback they use for an unresolvable name.AddDefaultDatabaseVisitorreaches the same invariant without going throughcreateTable, both for a bare one-part table name and when qualifying adictGetdictionary name, so a parameterized name is skipped in both places.Beyond the assertion, the old rebuild yielded an identifier whose database part degraded to the empty string, so the dependency scanner substituted the current database and recorded a referential dependency on a table the view may never read. On 26.7 that makes
DROP TABLEof an unrelated table fail withHAVE_DEPENDENT_OBJECTSwhile the database the parameter names stays unguarded. Since the target is only knowable at call time, no dependency is the sound answer. A view over a resolvable name keeps its dependency, pinned both ways by the new tests.That placeholder is also valid metadata a release build writes, and the same rebuild runs from
TablesLoader::buildDependencyGraph, so a debug or sanitizer build could not start against such a data directory: evenSELECT 1aborted. The new stateless test aborts a pre-fix server and passes on a fixed one.