Skip to content

Fallback to local execution when distributed plan references dictionary - #121198

Merged
antoniofilipovic merged 10 commits into
masterfrom
afilipovic-distributed-plan-dictionary-fallback
Sep 22, 2026
Merged

antoniofilipovic merged 10 commits into
masterfrom
afilipovic-distributed-plan-dictionary-fallback

Conversation

@antoniofilipovic

@antoniofilipovic antoniofilipovic commented Sep 20, 2026 •

Copy link
Copy Markdown
Member

Fallback to local execution when dictionary is referenced in the plan execution. Falls back following queries: dictGet, dictHas, dictIsIn

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):

...
Fall back to local execution for make_distributed_plan queries that call dictionary functions, avoiding Dictionary (...) not found failures on worker nodes.


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

Version info

  • Merged into: 26.10.1.394-master (included in 26.10 and later)

@clickhouse-gh

clickhouse-gh Bot commented Sep 20, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f16afc9]

Summary: ✅


AI Review

Summary

This PR teaches the distributed-plan fallback to keep several dictionary-using plan fragments on the initiator instead of trying to serialize them to workers. The direct detection coverage is much better now, but one remote-filter pushdown path still lets a dictionary predicate move back onto the shard after the fallback decision, so the fallback to local execution guarantee is not correct yet.

Findings

❌ Blockers

  • [src/Processors/QueryPlan/Optimizations/makeDistributed.cpp:127-137] The fallback decision happens before QueryPlan::optimize, but optimizePrimaryKeyConditionAndLimit still pushes enclosing FilterSteps into any SourceStepWithFilterBase, including ReadFromRemote (src/Processors/QueryPlan/Optimizations/optimizeTree.cpp:265-266, src/Processors/QueryPlan/Optimizations/optimizePrimaryKeyConditionAndLimit.cpp:18-118). ReadFromRemote::addPipe then injects filter_actions_dag back into the shard query (src/Processors/QueryPlan/ReadFromRemote.cpp:758-759, src/Processors/QueryPlan/ReadFromRemote.cpp:879-880). As a result, SELECT ... FROM remote(...) WHERE dictHas(...) SETTINGS make_distributed_plan = 1 can still execute the dictionary lookup on the remote server and fail when the dictionary exists only on the initiator, which is exactly what this fallback is supposed to avoid. Suggested fix: after a fallback, either prevent dictionary-bearing filters from being pushed into ReadFromRemote / other remote SourceStepWithFilterBase implementations, or rerun the dictionary must stay local check after primary-key filter pushdown and reject remote placement there as well.
Tests
  • ⚠️ Add a focused integration test over remote() or a Distributed table with a dictionary present only on the initiator, make_distributed_plan = 1, and the optimizer path that produces filter_actions_dag, then assert the query succeeds and the dictionary predicate is not injected into the remote shard query.
Final Verdict
  • Status: ⚠️ Request changes
  • Keep dictionary predicates out of the post-fallback remote-filter pushdown path, and add a regression that covers ReadFromRemote / Distributed execution rather than only the local replicated-table plan.

LLVM Coverage Report

Measured on commit f16afc9.

Metric Baseline Current Δ
Lines 88.00% 88.00% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 79.30% 79.30% +0.00%

Changed lines: Changed C/C++ lines covered: 73/85 (85.88%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... label Sep 20, 2026
@antoniofilipovic
antoniofilipovic marked this pull request as ready for review September 21, 2026 09:19
@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Sep 21, 2026

@alesapin alesapin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@alesapin alesapin self-assigned this Sep 21, 2026

@groeneai groeneai left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reviewed at your request. The lambda gap is answered in the thread above, with a pointer to an existing
helper that already does that traversal. One test point on top of it.

test_dict_get_falls_back and test_dict_get_in_filter_falls_back assert
ProfileEvents['DistributedPlanRemoteTasks'] == 0 and zero worker query_log rows. Both hold when the
fallback works, and both also hold if this rig stops distributing anything at all: a missing key in a
ProfileEvents map reads as 0 (SELECT map('a',1)['b'] is 0), and a worker that never receives a task
logs nothing. test_strict_mode_throws does not separate the two cases either, because the throw comes
from the planning decision and needs no worker. A positive control in the same module, same settings, with
no dictionary reference (SELECT count() FROM t), asserting DistributedPlanRemoteTasks > 0 on the
initiator and one worker query_log row, makes those zeros load-bearing; the ProfileEvent is documented
for exactly that ("a non-zero value means the query was actually executed distributedly"). It matters more
once the lambda shapes are added to this file, since those are the cases where a plan really does reach a worker.

Three things I checked that need nothing: all 43 dict% functions in system.functions match
functionIsDictGet; reading a dictionary as a table (FROM d, dictionary(d)) plans as
ReadFromStorage (Dictionary), which ReadFromStorageStep::isSerializable already rejects (it accepts
SystemOne only); and WITH TOTALS is rejected earlier in getReasonStepCannotBeDistributed, so the
TotalsHaving actions sitting outside findDictionaryFunction is moot.

@clickhouse-gh

clickhouse-gh Bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing f16afc9ae with master 69ee3e445 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 9449c3a89; 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

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

Median compile-time ratio to the baselines is ×0.94 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio.
The matched translation units cost -4.5 s (-5%) in total before that adjustment.

Job report

@antoniofilipovic

Copy link
Copy Markdown
Member Author

LGTM, but let's fix bot comment. It refers to this code https://github.com/ClickHouse/clickhouse/blob/master/src/Processors/QueryPlan/Optimizations/mergeFilterIntoJoinCondition.cpp#L150-L253

Thanks, I took into account @groeneai's comment here, since machinery already exists: #121198 (comment)

if (const auto & row_level_filter = source->getRowLevelFilter())
if (auto name = find_in_dag(row_level_filter->actions))
return name;
if (const auto & filter_dag = source->getFilterActionsDAG())

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.

Disabling make_distributed_plan here does not fully preserve the "dictionary stays local" invariant for remote sources. applyDistributedPlanFallbackToLocal runs before QueryPlan::optimize, but later optimizePrimaryKeyConditionAndLimit still folds enclosing FilterSteps into any SourceStepWithFilterBase when query_plan_optimize_primary_key is enabled. For ReadFromRemote, addPipe then injects filter_actions_dag back into the shard query, so a query like SELECT count() FROM remote(...) WHERE dictHas(...) SETTINGS make_distributed_plan = 1 can still evaluate the dictionary on the remote host and fail if the dictionary exists only on the initiator. That breaks the fallback invariant this PR is trying to establish.

Please keep dictionary-bearing predicates out of remote filter pushdown (or re-check after that pushdown), and add a regression that exercises remote() / Distributed rather than only the local replicated-table plan.

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.

remote() node is something user references, so I am keeping this out of scope of this PR.

@antoniofilipovic
antoniofilipovic added this pull request to the merge queue Sep 21, 2026
Merged via the queue into master with commit b08f5fd Sep 22, 2026
347 of 349 checks passed
@antoniofilipovic
antoniofilipovic deleted the afilipovic-distributed-plan-dictionary-fallback branch September 22, 2026 00:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-distributed-query-exchange Distributed Query Execution with Exchange operators: cloud-native query distribution using stream... 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.

4 participants