Skip to content

Carry the packed String join key settings in the serialized query plan - #130750

Merged
harikrishnan94 merged 2 commits into
ClickHouse:masterfrom
groeneai:packed-string-keys-in-serialized-plan
Nov 11, 2026
Merged

harikrishnan94 merged 2 commits into
ClickHouse:masterfrom
groeneai:packed-string-keys-in-serialized-plan

Conversation

@groeneai

@groeneai groeneai commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #128972

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

...

Description

Follow-up to #128972, as asked in #128972 (comment). A node that runs a deserialized query plan (serialize_query_plan, distributed plans) built its JoinSettings with enable_packed_string_keys_in_hash_join = false and min_rows_for_packed_string_keys_in_hash_join = 0 hard coded, so its hash joins on a single String key compared the keys exactly. Results were correct; the remote joins only missed the speedup.

Both settings are now plan settings with defaults false and 0, written by JoinSettings::updatePlanSettings and read by the constructor that takes plan settings. There is no version gate: a reader skips a plan setting name it does not know unless it is IMPORTANT (BaseSettings::read), so a node without this change compares exactly. Join tables and joinGet stay exact.

The tasks of a distributed query plan (make_distributed_plan) get an already optimized join without a row estimate, so min_rows_for_packed_string_keys_in_hash_join never applied there. The initiator applies it and sends the decision in the join's flags byte (the row estimate itself is not serialized), so a small right side stays exact in the tasks, as on a single node. Plan-based parallel replicas ship the join already optimized too, so the same decision covers them.

#128972 is only on master, hence Not for changelog.

Validation: the gtest JoinPackedStringKeysPlanSetting.RoundTrip fails on master and passes with the change. In 05620_join_packed_string_keys_serialized_plan, the remote queries of the cluster() join with serialize_query_plan = 1 give 1 0 0 before the change and 1 1 1 after, and the tasks of the make_distributed_plan join with a 1000-row right side give 1 1 1 without the decision and 1 0 0 with it; 40 randomized runs pass. Mixed versions work in both directions, with exact keys on the old node.


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

Version info

  • Merged into: 26.11.1.1992-master (included in 26.11 and later)

A node that runs a deserialized query plan (`serialize_query_plan`, distributed
plans) built its `JoinSettings` from the plan settings with
`enable_packed_string_keys_in_hash_join = false` and
`min_rows_for_packed_string_keys_in_hash_join = 0` hard coded, because ClickHouse#128972
left both out of `QueryPlanSerializationSettings`. Its hash joins on a single
`String` key therefore compared the keys exactly whatever the initiator set:
correct results, but no speedup on the remote nodes.

Add both to `QueryPlanSerializationSettings` with defaults `false` and `0`,
write them in `JoinSettings::updatePlanSettings` and read them in the
constructor that takes plan settings. A plan without the names keeps exact keys.

No version gate, as asked in
ClickHouse#128972 (comment):
plan settings are written by name with flags, and `BaseSettings::read` skips an
unknown name that is not declared `IMPORTANT`, so an older reader logs
`Unknown settings` and keeps its exact keys. Measured with a server built from
the previous master as the only shard (and as the initiator): the join succeeds
and the old node records no packed keys. The row estimate is not serialized,
and `Join` tables and `joinGet` stay exact.

The gtest round trips both values and reads plan settings without the names
back as `false` and `0`. The stateless test runs a `cluster()` join with
`serialize_query_plan = 1` and checks the remote queries' packed-key profile
events; on the previous master they are 0 with the setting on, while the same
join over the query text path counts them, so the test checks the plan path.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

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

Pre-publication review by an independent model (engine: codex; 0 findings) plus my own cold review (1 nit).

# Sev Finding Verdict Evidence / action
1 💡 The "Rules for this version" note in src/Core/ProtocolDefines.h still says a new plan setting name needs the version bump DISAGREE (out of scope) The note predates version 21, which writes plan settings by name; BaseSettings::read skips an unknown name that is not IMPORTANT, and 21 is the oldest supported plan version. The follow-up spec asks for no change in ProtocolDefines.h.

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

Session id: cron:clickhouse-review-slot-9:20261111-102300

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. The new gtest and 05620_join_packed_string_keys_serialized_plan fail on every run against master (no randomization needed): the remote queries record no packed keys with the setting on.
b Root cause explained? The constructor that reads plan settings hard coded enable_packed_string_keys_in_hash_join = false and min_rows_for_packed_string_keys_in_hash_join = 0, and updatePlanSettings did not write them, so a node running a deserialized plan built its hash join with exact String keys.
c Fix matches root cause? Yes. Both values are now plan settings, written by updatePlanSettings and read by that constructor.
d Test intent preserved / new tests added? New gtest JoinPackedStringKeysPlanSetting.RoundTrip (both values round trip; plan settings without the names read back as false / 0) and a stateless test counting JoinPackedStringKeysInline / JoinPackedStringKeysHashed on the remote queries.
e Both directions demonstrated? Yes. On master the gtest fails and the stateless test gives 1 0 0; with the change both pass. Flipping the plan default to true fails the gtest's without_names check. The same join over the query text path counts packed keys on master too, so the test checks the plan path.
f Fix is general across code paths? JoinStepLogical is the only step that writes and reads join plan settings, through these two functions. Join tables and joinGet do not use them and stay exact, as asked.
g Fix generalizes across inputs (params/datatypes/wrappers)? The test covers inline (up to 15 bytes) and hashed (16 bytes or more) keys with the setting on and off; the gtest round trips several values of both settings. Other key types are unchanged.
h Backward compatible? (maintainer-approved exception only) Yes. No default changes. A plan without the names keeps exact keys; an older node skips the names (they are not IMPORTANT) and keeps exact keys, measured with a server built from master as the shard and as the initiator.
i Invariants and contracts preserved? The plan defaults equal the old hard coded values, so plans from older initiators and existing callers of the constructor read the same as before.

Session id: cron:clickhouse-impl-slot-40:20261111-095300

@clickhouse-gh

clickhouse-gh Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [77fcd03]

Summary: ✅


AI Review

Summary

Reviewed #130750 at 77fcd036d28f03adc663ee381574b7362a4ec72a. The change carries packed String join-key settings through serialized query plans and preserves the initiator's decision to keep exact keys for an estimated small build side. No actionable findings remain in the current code.

Missing context / blind spots
  • ⚠️ No local ClickHouse binary or build directory was available, so tests were not rerun. Runtime evidence comes from the PR's recorded before/after and mixed-version runs and its CI reports; a local rerun of the added regression tests would provide independent confirmation.
Final Verdict

✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-not-for-changelog This PR should not be mentioned in the changelog can be tested Allows running workflows for external contributors comp-joins JOINs end-to-end (planning hooks + runtime join operators/algorithms). Single bucket to avoid pla... labels Nov 11, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 77fcd036d with master cd9725caf (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.

Slowest function optimization changes (ThinLTO)

Median per-function time ratio to the master baseline: programs/clickhouse ×0.58, programs/clickhouse-keeper ×0.82 (each binary is linked separately, on a different machine and with different build flags); deltas below are relative to it.

Binary Function Master PR Δ vs median
programs/clickhouse DB::registerFunctionConversion(DB::FunctionFactory&) 6.8 s 11.2 s +7.3 s (+186%)
programs/clickhouse _ZN6detail10static_forITk7is_enumN2DB23AttributeUnderlyingTypeEZNS1_29callOnDictionaryAttributeType… 3.8 s 7.9 s +5.7 s (+258%)
Compile time of recompiled translation units

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

Job report

@harikrishnan94 harikrishnan94 left a comment

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.

@groeneai not sure about a plan that's already optimized.

Comment thread src/Interpreters/JoinOperator.cpp Resolved
…ed plan

The tasks of a distributed query plan (`make_distributed_plan`) receive a join
that the initiator already optimized, and they do not optimize the join order
again, so the right side has no row estimate there.
`min_rows_for_packed_string_keys_in_hash_join` then never applied: a join whose
right side is estimated below the minimum compared its keys exactly on a single
node but used packed keys in the tasks.

The initiator has the estimate, so it takes the decision: a `JoinStepLogical`
whose right side is estimated below the minimum sets a fourth bit in the byte
that already carries the join decisions taken from a row estimate, and the
receiver then compares the keys exactly. A reader that does not know the bit
ignores it and never reads the packed key settings either, so it keeps exact
keys. The row estimate itself is still not serialized.

Plan-based parallel replicas are not affected: they ship the logical plan
before join order optimization, the replicas optimize it themselves and have
the estimate (measured: a 1000-row right side stays exact on the replicas with
the default minimum, and is packed with a minimum of 0).

Measured with `distributed_plan_execute_locally = 1`, a 1000-row right side and
the default minimum: the tasks inserted 500 inline + 500 hashed keys before, 0
after; a single-node run inserts 0 either way. A 200000-row right side is still
packed in the tasks (100000 + 100000), as on a single node.

The stateless test gets the two cases.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@harikrishnan94
harikrishnan94 added this pull request to the merge queue Nov 11, 2026
Merged via the queue into ClickHouse:master with commit 24ed195 Nov 11, 2026
203 checks passed
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 11, 2026
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-joins JOINs end-to-end (planning hooks + runtime join operators/algorithms). Single bucket to avoid pla... pr-not-for-changelog This PR should not be mentioned in the changelog 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.

3 participants