Skip to content

Qualify the dictionary name of assignCentroid in new stored definitions - #124989

Open
groeneai wants to merge 4 commits into
ClickHouse:masterfrom
groeneai:assign-centroid-qualify-new-stored-definitions
Open

groeneai wants to merge 4 commits into
ClickHouse:masterfrom
groeneai:assign-centroid-qualify-new-stored-definitions

Conversation

@groeneai

@groeneai groeneai commented Oct 9, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #123711
Related: #123711 (comment)

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fix Dictionary not found, or silently reading a same-named dictionary of another database, when a view, a materialized view or a column DEFAULT/MATERIALIZED/ALIAS expression that calls assignCentroid(vec, 'dict') is used from another current database, and fix ALTER TABLE ... UPDATE ... = assignCentroid(vec, 'dict') mutations failing with Dictionary not found. The dictionary name is now qualified with the database when the definition is created, like the one of dictGet.

Description

On 26.9.9.28, CREATE VIEW a.v AS SELECT id, assignCentroid(v, 'centroids_dict') AS c FROM a.vectors read while the current database is b fails with Code: 36 ... Dictionary (centroids_dict) not found, and returns the centroids of b.centroids_dict if b has one. ALTER TABLE a.t UPDATE c = assignCentroid(v, 'centroids_dict') WHERE 1 fails its mutation even when issued from a, because a mutation runs in the server context.

AddDefaultDatabaseVisitor qualified the dictionary name of stored definitions only for dictGet (argument 0). assignCentroid takes it in argument 1, so the bare name was stored and later resolved in the current database of whoever used the definition. #123711 fixed the same problem at query time.

The visitor qualifies the assignCentroid literal only in a new definition: a CREATE that is not a replay of stored metadata (isReplayedTableDefinition), and every ALTER, which binds it to the altered table's database also when issued from another current database, as a Replicated database already does (dictGet keeps the session database, where its ALTER validation resolves it). A short ATTACH, server startup, RESTORE and Replicated replay keep the stored name: without a loading dependency the dictionary may not be loaded yet, so qualifying there would depend on the attach order. RESTORE ... AS renames a qualified name.

The dependency graphs are not changed: a loading dependency like the one of dictGet would refuse a dictionary/table cycle that is accepted today. Definitions stored by older versions keep resolving in the reader's database. I can make it identical to dictGet (dependency plus legacy repair) if refusing that cycle is acceptable.

Validation: the new test fails on master and passes with the fix, 50/50 with randomized settings and 20/20 with a Replicated database. Old metadata loads unchanged across 3 restarts.


Workflow [PR]
Sync PR [sync-upstream/pr/124989]

groeneai and others added 2 commits October 9, 2026 19:18
A view, materialized view, column DEFAULT/MATERIALIZED/ALIAS expression or
mutation command stored `assignCentroid(v, 'dict')` with the bare name,
because AddDefaultDatabaseVisitor qualified only the first argument of
dictGet. The name was then resolved in the current database of whoever used
the definition: reading the view or inserting from another database failed
with `Dictionary not found` or used a same-named dictionary there, and a
mutation, which runs in the server context, always failed.

getDictionaryNameArgumentPosition gives the position of the name for both
functions. The visitor qualifies the String literal of assignCentroid only in
a new definition: a CREATE that is not a replay of stored metadata
(isReplayedTableDefinition), and every ALTER, which is never replayed from
metadata. A replayed definition (short ATTACH, startup, RESTORE, Replicated
replay and recovery) keeps the stored name: without a loading dependency the
dictionary may not be registered yet, so qualifying there would depend on the
attach order, and RESTORE refuses a created definition that differs from the
one in the backup. RESTORE ... AS renames a qualified name, for a literal
only, because an identifier in that position is an expression.

Deliberately unchanged: the dependency visitors, since a loading edge like
the one of dictGet would refuse a dictionary <-> table cycle that is accepted
today, at CREATE and at startup; and the legacy metadata repair, which would
need that edge to be deterministic. Definitions stored by older versions keep
resolving in the reader's database. During a rolling upgrade of a Replicated
database, an ALTER applied by an old replica stores the bare name there.

Addresses ClickHouse#123711 (comment)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Drop the self-describing helper comment and the ALTER flag narration; condense the setIsNewDefinition comment to one line.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-review-bot PR origin: review-bot finding on a groeneai PR labels Oct 9, 2026
@groeneai

groeneai commented Oct 9, 2026

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 1 finding) plus my own cold review (3 nits).

# Sev Finding Verdict Evidence / action
1 ❌ A name wrapped as toNullable('d') or toLowCardinality('d') in a new definition stays bare (AddDefaultDatabaseVisitor.h) DISAGREE The documented type of the name is String (assignCentroid.cpp:569-578). The natural source of a wrapped name, a scalar subquery, holds the name as data, which no CREATE-time rewrite can bind. dictGet's stored branch also binds only the literal/identifier spellings. Such a name keeps resolving in the reader's database, as before.
2 💡 An ALTER issued from another current database qualifies against that database, not the table's AGREE, not changed Same overload and behaviour as dictGet in the same visitor.
3 💡 Rolling upgrade of a Replicated database: an ALTER applied by an old replica stores the bare name there AGREE, not changed Disclosed in the commit message; same class as any ALTER-time normalization.
4 💡 The replay arm of the test expects reader-relative results AGREE, not changed Intended: it fails if a replayed definition is rewritten (then RESTORE refuses the table).

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are
terminal per finding.

Session id: cron:clickhouse-review-slot-8:20261009-192200

@groeneai

groeneai commented Oct 9, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. The new test fails on every run on master (c873902) at the mutation arm: Code: 341 ... Code: 36 Dictionary (centroids_dict) not found (UNFINISHED). The same shape fails on the official 26.9.9.28 image.
b Root cause explained? AddDefaultDatabaseVisitor qualified the stored dictionary name only for dictGet (argument 0); assignCentroid takes it in argument 1, so the bare name was stored and resolved later in the current database of the reader, inserter or background mutation.
c Fix matches root cause? Yes. The name is qualified in the same visitor and with the same call as for dictGet, for new definitions (fresh CREATE, every ALTER). No guard at the use site.
d Test intent preserved / new tests added? New test 05353_assign_centroid_stored_definition_dictionary_name: view, materialized view, DEFAULT/MATERIALIZED, ALTER ... ALIAS, mutation, stored text, a same-named dictionary in the reader's database, RESTORE ... AS rename, and replayed definitions keeping the stored name.
e Both directions demonstrated? Master: fails (mutation error; stored text unqualified; view, MV, DEFAULT and ALIAS fail from another database; wrong results with a same-named dictionary). Fix: passes, 50/50 randomized, 20/20 with a Replicated database. Five mutants each fail their arm.
f Fix is general across code paths? All AddDefaultDatabaseVisitor users in CREATE (select, columns, post-UDF pass) and ALTER, plus the RESTORE ... AS renamer; the query-time analyzer path shares the new helper. Dependency visitors unchanged on purpose (a loading edge would refuse a dictionary/table cycle accepted today).
g Fix generalizes across inputs (params/datatypes/wrappers)? Only a String literal is a dictionary name for assignCentroid (argument 1 must be constant). Already-qualified names are kept, an unknown name stays bare, a wrapped name like toNullable('d') is left as for dictGet (bound at query time since #123711).
h Backward compatible? (maintainer-approved exception only) Yes. No setting. Definitions stored by older versions load unchanged and keep their behaviour (3 restarts measured); 26.9 reads the qualified form.
i Invariants and contracts preserved? A replayed definition (short ATTACH, startup, RESTORE, Replicated replay) keeps its stored text byte for byte, so RESTORE's definition check still holds and nothing depends on attach order. No new dependency edge, so cycle checks are unchanged.

Session id: cron:clickhouse-impl-slot-4:20261009-180500

@clickhouse-gh

clickhouse-gh Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [fe8c6db]

Summary: ✅


AI Review

Summary

The PR qualifies literal dictionary names in new stored assignCentroid expressions and renames them during backup/restore, while preserving replayed metadata. The previous cross-database ALTER finding is fixed and covered by the new regression test. No actionable findings remain. The focused stateless test passes in Fast test for the current PR head.

Final Verdict

✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Oct 9, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). label Oct 9, 2026
…tabase

An ALTER issued from another current database qualified the name against the
session database. assignCentroid is not validated against that database at
ALTER time, so a name that exists only in the table's database was stored
bare: the ALIAS, DEFAULT or mutation then failed with Code 36 when used from
any other database, and a same-named dictionary in the session database was
bound instead of the table's one.

Bind it to the altered table's database, like the table names of the same
ALTER, and like a Replicated database already does, whose replicas run the
ALTER with that database as the current one. dictGet keeps the session
database, because its ALTER validation resolves the name there. CREATE is
unchanged: the visitor database is the session database there.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh clickhouse-gh Bot added comp-ddl DDL command coordination and execution (ON CLUSTER, DDL queue). and removed comp-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). labels Oct 9, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing fe8c6dba0 with master f30d7ec4f (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

41 translation units recompiled, 383 s compile time in total, 41 of them have a recent master baseline.

Job report

…lify-new-stored-definitions

Conflicts in AddDefaultDatabaseVisitor.h and InterpreterCreateQuery.cpp:
master removed the post-UDF qualification pass (ClickHouse#130531, the revert of
ClickHouse#114923), and this branch had added its assignCentroid literal branch
and a setIsNewDefinition call there. Resolved by taking master's removal:
like dictGet, a dictionary name brought in by a SQL UDF body is no longer
qualified at CREATE. The full traversal and the ALTER path keep this
branch's qualification unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh clickhouse-gh Bot added comp-dictionary Dictionaries (in-memory key-value, periodically refreshed from sources). and removed comp-ddl DDL command coordination and execution (ON CLUSTER, DDL queue). labels Nov 15, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-dictionary Dictionaries (in-memory key-value, periodically refreshed from sources). groeneai-origin-review-bot PR origin: review-bot finding on a groeneai PR pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant