Skip to content

Bind an unqualified dictGet name to the current database - #115541

Merged
alexey-milovidov merged 1 commit into
masterfrom
fix-dictget-database-in-distributed-queries
Aug 20, 2026
Merged

alexey-milovidov merged 1 commit into
masterfrom
fix-dictget-database-in-distributed-queries

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Aug 20, 2026 •

Copy link
Copy Markdown
Member

Closes: #85181
Related: #44301
Related: #46424

The dictionary name of dictGet and its variations is resolved against the current database of the server that evaluates the function. A shard of a Distributed table evaluates it in a session whose current database comes from the cluster configuration — default unless <default_database> is set — and not from the initiator. So:

USE demo;
SELECT code, dictGet('dict_test', 'value', code) FROM demo.test;

fails on the shards with

Code: 36. DB::Exception: Dictionary (`dict_test`) not found: In scope
SELECT __table1.code AS code, dictGet('dict_test', 'value', __table1.code) ... FROM demo.test_local AS __table1. (BAD_ARGUMENTS)

and — worse — when a dictionary of the same name also exists in the shard's own database, no error is raised at all and the values of the wrong dictionary are returned silently.

This binds the name in the analyzer, while the current database of the initiator is still known, so the bound name travels to the shards inside the query tree. The old analyzer already does the same in AddDefaultDatabaseVisitor for the query it sends to the shards.

The binding happens after the projection names of the arguments have been calculated, so the column name of the expression stays exactly as it was written by the user: dictGet('dict_test', 'value', code), not dictGet('demo.dict_test', 'value', code). qualifyDictionaryNameWithDatabase leaves the name alone when it is already qualified, when it belongs to an XML dictionary, and when no such dictionary exists in the current database — in the last case the name may still be meant for a dictionary that only exists on the shards.

Notes:

  • The EXPLAIN SYNTAX run_query_tree_passes=1 references of the inverse dictionary lookup tests now show the bound name; only the printed query changes there, no result changes.
  • Existing test 04054_dictget_unqualified_name_distributed deliberately runs in the default database so that the shard can resolve the unqualified name, which is why it never caught this. The new test runs in the test's own database with prefer_localhost_replica = 0, which is what makes the shard query travel over a real connection.
  • The old analyzer keeps failing here with NOT_FOUND_COLUMN_IN_BLOCK, because the header it computes on the initiator keeps the unqualified column name while the query it ships to the shards carries the qualified one. Fixing that needs a separate change, so the new test is tagged no-old-analyzer.
  • joinGet has the same defect (Table default.jt does not exist. Maybe you meant demo.jt?); its first argument is a table name with temporary-table and access-control semantics of its own, so it is left for a separate change.

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 dictGet and its variations in distributed queries: an unqualified dictionary name is now bound to the current database of the initiator, instead of being resolved against the current database of each shard, where it either was not found or resolved to a different dictionary of the same name.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

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

Version info

  • Merged into: 26.8.1.1789 (included in 26.8 and later)

The dictionary name of `dictGet` and its variations is resolved against the
current database of the server that evaluates the function. A shard of a
`Distributed` table evaluates it in a session whose current database comes from
the cluster configuration - `default` unless `<default_database>` is set - and
not from the initiator, so a query written as

    USE demo;
    SELECT code, dictGet('dict_test', 'value', code) FROM demo.test;

fails on the shards with `Dictionary (dict_test) not found`, or - when a
dictionary of the same name also exists in the shard's own database - silently
returns the values of the wrong dictionary.

Bind the name in the analyzer, while the current database of the initiator is
still known, so the bound name travels to the shards inside the query tree. The
old analyzer already does the same in `AddDefaultDatabaseVisitor` for the query
it sends to the shards.

The binding happens after the projection names of the arguments have been
calculated, so the column name of the expression stays exactly as it was written
by the user. `qualifyDictionaryNameWithDatabase` leaves the name alone when it is
already qualified, when it belongs to an XML dictionary, and when no such
dictionary exists in the current database.

The `EXPLAIN SYNTAX run_query_tree_passes=1` references of the inverse dictionary
lookup tests now show the bound name; only the printed query changes there.

The old analyzer keeps failing with `NOT_FOUND_COLUMN_IN_BLOCK` because the
header it computes on the initiator keeps the unqualified column name, so the
new test is tagged `no-old-analyzer`.

Closes: #85181
Related: #44301
Related: #46424

Co-Authored-By: Claude Fable 5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Aug 20, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [78e6ef0]

Summary: ✅


AI Review

Summary

This PR fixes the main literal/identifier form of distributed dictGet name binding by qualifying the dictionary on the initiator before the query tree is sent to shards. The implementation is close, but it still leaves one supported const-string carrier unresolved, so the initiator/shard split the PR is trying to remove is still observable.

Findings

⚠️ Majors

  • [src/Analyzer/Resolve/resolveFunction.cpp:1422] The new binding only runs when the first argument is already a ConstantNode. Supported const-string expressions such as dictGet(currentDatabase() || '.dict', ...) are still a live FunctionNode here because currentDatabase is non-deterministic and is not folded by the later constant-folding step, so a real remote shard still re-evaluates the expression in its own session and resolves default.dict instead of the initiator's database. The bug fix is therefore incomplete for one of the existing dictionary-name forms.
    Suggested fix: bind or substitute server-context const-string expressions on the initiator before remote serialization, at least for currentDatabase and its aliases, using the same approach as AddDefaultDatabaseVisitor::substituteCurrentDatabase.
Final Verdict

Status: ⚠️ Request changes

Minimum required action: close the remaining initiator/shard divergence for const-string dictionary-name expressions (or explicitly narrow the implementation and regression coverage if that shape is intentionally out of scope).

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.90% 86.90% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 79.20% 79.20% +0.00%

Changed lines: Changed C/C++ lines covered: 11/11 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Aug 20, 2026
auto & dict_get_arguments = function_node_ptr->getArguments().getNodes();
if (!dict_get_arguments.empty())
{
const auto * dictionary_name_node = dict_get_arguments[0]->as<ConstantNode>();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dictGet still has one initiator/shard divergence here: this only binds carriers that have already become a ConstantNode. Supported const-string expressions like dictGet(currentDatabase() || '.dict', ...) (see tests/queries/0_stateless/01552_dict_fixedstring.sql) are still a live FunctionNode at this point because currentDatabase is explicitly non-deterministic (src/Functions/currentDatabase.cpp:43) and the later constant-folding step will not fold it into a literal on the initiator (resolveFunction.cpp:2468-2517). On a real remote connection the shard will re-evaluate that expression in its own session and still pick default.dict, so the bug remains for that name form. This needs an initiator-side substitution/binding for server-context const-string expressions, similar to AddDefaultDatabaseVisitor::substituteCurrentDatabase, before the query tree is serialized to shards.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a secondary problem. If it is even slightly non-trivial to fix, let's pass.

@clickhouse-gh

clickhouse-gh Bot commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 78e6ef004 with master 3ca258fd4 (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
Binary Master PR Δ
programs/clickhouse-stripped 703.89 MiB 700.85 MiB -3.04 MiB (-0.43%)

Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction.

Compile time of recompiled translation units

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

Job report

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix is correct, the comment is a little bit redundant. LGTM.

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

Labels

pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Database context not preserved in distributed queries when using dictGet function

3 participants