Skip to content

Add setting analyzer_compatibility_apply_final_to_all_joined_tables - #111589

Merged
fm4v merged 7 commits into
ClickHouse:masterfrom
fm4v:final-join-leak-compat-setting
Jul 26, 2026
Merged

fm4v merged 7 commits into
ClickHouse:masterfrom
fm4v:final-join-leak-compat-setting

Conversation

@fm4v

@fm4v fm4v commented Jul 23, 2026 •

Copy link
Copy Markdown
Member

Related: #108979

Changelog category (leave one):

  • Improvement

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

  • Added setting analyzer_compatibility_apply_final_to_all_joined_tables that restores the old behavior, where the FINAL modifier on the left-most table of a JOIN was also applied to the other joined tables. The setting is registered in the settings changes history, so compatibility with versions before 26.6 restores the old semantics automatically.

#108979 fixed FINAL on one table of a JOIN leaking onto the other joined tables. While the new behavior matches the documented semantics (FINAL is a per-table-reference modifier), the fix is result-changing for queries that (perhaps unknowingly) relied on the leak: joining a FINAL table to another ReplacingMergeTree that has unmerged duplicate versions now multiplies the matching rows instead of deduplicating them, e.g.

CREATE TABLE t_left (id Int64, right_id Int64, ver UInt64) ENGINE = ReplacingMergeTree(ver) ORDER BY id;
CREATE TABLE t_right (id Int64, attr String, ver UInt64) ENGINE = ReplacingMergeTree(ver) ORDER BY id;
SYSTEM STOP MERGES t_left;
SYSTEM STOP MERGES t_right;
INSERT INTO t_left VALUES (1, 10, 1);
INSERT INTO t_left VALUES (1, 10, 2);
INSERT INTO t_right VALUES (10, 'car', 1);
INSERT INTO t_right VALUES (10, 'car', 2);

SELECT count() FROM t_left AS l FINAL INNER JOIN t_right AS r ON r.id = l.right_id;
-- before the fix: 1, after the fix: 2

and there was no way to get the old results back other than rewriting the query (adding FINAL to every joined table — which remains the recommended fix).

This PR puts the legacy behavior under the new setting analyzer_compatibility_apply_final_to_all_joined_tables (default false = the fixed behavior) by gating the query-level-FINAL fallback in SelectQueryInfo::isFinal.

The settings changes history is staged for backports of this setting to the release branches:

Version Setting default Behavior by default History entry
26.4 (backport) true old (leak preserved) 26.4: true -> true (introduction)
26.5 (backport) true old (leak preserved) —
26.6 (backport) false fixed 26.6: true -> false
26.7 / master false fixed —

so SET compatibility = '26.5' (or earlier) restores the old semantics without query changes, and the 26.4/26.5 release lines keep the old behavior by default.

Version info

  • Merged into: 26.8.1.167 (included in 26.8 and later)
  • Backported to: 26.7.2.28, 26.6.2.119

PR ClickHouse#108979 fixed FINAL on the left-most table of a JOIN leaking onto the
other joined tables. The fix is result-changing for queries over
ReplacingMergeTree tables with unmerged duplicate rows that relied on the
old behavior, and had no compatibility gate. Put the legacy behavior under
the new setting and register it in SettingsChangesHistory so that
compatibility with versions before 26.7 restores the old semantics.
@clickhouse-gh

clickhouse-gh Bot commented Jul 23, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [fae8978]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_llvm_coverage, old analyzer, s3 storage, DBReplicated, WasmEdge, parallel, 2/3) FAIL
Server died FAIL cidb IGNORED
04541_create_as_select_table_scoped_insert_grant UNKNOWN cidb IGNORED
04627_analyzer_compatibility_final_all_joined_tables UNKNOWN cidb IGNORED
02294_anova_cmp UNKNOWN cidb IGNORED
03236_squashing_high_memory UNKNOWN cidb IGNORED
03915_exchange_tables_race UNKNOWN cidb, issue ISSUE EXISTS
03288_types_binary_encoding_corrupted_data UNKNOWN cidb IGNORED
02479_analyzer_aggregation_totals_rollup_crash_fix UNKNOWN cidb IGNORED
02015_async_inserts_6 UNKNOWN cidb IGNORED
03203_count_with_non_deterministic_function UNKNOWN cidb IGNORED
1 more test cases not shown
Stateless tests (amd_debug, parallel) FAIL
03443_projection_sparse FAIL cidb, issue ISSUE CREATED

AI Review

Summary

This PR adds analyzer_compatibility_apply_final_to_all_joined_tables so queries can opt back into the pre-26.6 behavior where FINAL on the left side of a JOIN leaked onto the other joined tables. The local analyzer path now carries that compatibility bit, but the logical-plan transport path still drops it, so distributed queries with serialize_query_plan=1 and follower-side parallel replicas do not honor the advertised compatibility contract. Status: ⚠️ Request changes

Findings

❌ Blockers

  • src/Planner/PlannerJoinTree.cpp:1759, src/Processors/QueryPlan/ReadFromTableStep.cpp:32, src/Processors/QueryPlan/resolveStorages.cpp:140, src/Storages/SelectQueryInfo.cpp:13 The PR promises that the new setting and compatibility restore the old FINAL-leak semantics, but that only holds before a query is lowered to ReadFromTableStep. PlannerJoinTree serializes every read with value_or(TableExpressionModifiers{}), ReadFromTableStep only preserves concrete modifier bits, and resolveStorages reconstructs SelectQueryInfo with table_expression_modifiers engaged even for tables that originally had no modifiers. SelectQueryInfo::isFinal then returns false immediately for those non-left JOIN tables, so the query-level FINAL is never consulted on the serialized-plan path. As a result, serialize_query_plan=1 distributed queries and the remote side of parallel replicas still run with 26.7 semantics even under compatibility = '26.5' or analyzer_compatibility_apply_final_to_all_joined_tables = 1. Suggested fix: either materialize hasFinal = true onto every affected joined table before emitting logical-plan read steps, or extend the logical-plan encoding so it preserves the nullopt vs empty-modifiers distinction (and the compatibility flag) across ReadFromTableStep / resolveStorages.
Tests

⚠️ The added stateless test only covers the planner-local path. Add a focused regression that exercises serialize_query_plan=1 (or follower-side parallel replicas) so the compatibility behavior is proven across the logical-plan round-trip.

Final Verdict

Status: ⚠️ Request changes

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Jul 23, 2026
Comment thread src/Planner/Utils.cpp
select_query_info.query = queryNodeToSelectQuery(query_tree);
select_query_info.query_tree = query_tree;
select_query_info.planner_context = planner_context;
select_query_info.apply_query_level_final_if_no_modifiers

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.

This only fixes the planner-local path. When the query is lowered to a logical plan (build_logical_plan, e.g. serialize_query_plan=1 distributed queries and the remote side of parallel replicas), the new bit is not serialized: ReadFromTableStep carries only TableExpressionModifiers, and resolveStorages.cpp reconstructs the non-left JOIN tables with {hasFinal = false} and no query_tree. At that point SelectQueryInfo::isFinal returns false before it can fall back to the left table's FINAL, so compatibility = '26.6' still gives 26.7 semantics on those paths. Can we either materialize hasFinal = true onto every affected joined table before emitting the logical-plan read steps, or serialize this compatibility flag alongside them?

… in 26.4

Move the settings-changes-history entry from 26.7 to 26.6 (true -> false),
matching the planned backport of the fix with the setting defaulting to
false on the 26.6 release line. Add an introduction entry to 26.4
(true -> true) for backports to 26.4 and 26.5, where the setting will
default to true and preserve the old behavior. Adjust the test to use
compatibility = '26.5' since 26.6 now maps to the fixed behavior.
@fm4v

fm4v commented Jul 23, 2026

Copy link
Copy Markdown
Member Author

Updated per backport plan: moved the settings-history entry from 26.7 to 26.6 (true -> false) and added an introduction entry to 26.4 (true -> true). The setting will be backported to 26.6 with default false and to 26.5/26.4 with default true (old behavior preserved there). The test now uses compatibility = '26.5' since 26.6 maps to the fixed behavior.

The check requires any setting that is new relative to the last released
version to appear in a history block newer than 26.6. Add a no-op
(false -> false) entry under 26.7; the behavior flip stays recorded under
26.6 and the backport introduction under 26.4, so compatibility
resolution is unchanged.
@clickhouse-gh

clickhouse-gh Bot commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.20% 86.20% +0.00%
Functions 92.00% 92.00% +0.00%
Branches 78.40% 78.30% -0.10%

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

Full report · Diff report

@fm4v
fm4v added this pull request to the merge queue Jul 26, 2026
Merged via the queue into ClickHouse:master with commit 8aab148 Jul 26, 2026
175 of 178 checks passed
@fm4v
fm4v deleted the final-join-leak-compat-setting branch July 26, 2026 19:04
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Jul 26, 2026
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 26, 2026
robot-clickhouse-ci-1 added a commit that referenced this pull request Jul 26, 2026
Cherry pick #111589 to 26.6: Add setting `analyzer_compatibility_apply_final_to_all_joined_tables`
robot-clickhouse added a commit that referenced this pull request Jul 26, 2026
fm4v added a commit that referenced this pull request Jul 27, 2026
Backport #111589 to 26.6: Add setting `analyzer_compatibility_apply_final_to_all_joined_tables`
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Jul 27, 2026
fm4v pushed a commit that referenced this pull request Jul 27, 2026
The automated backport of #111589 to 26.7 imported master's newest
`addSettingsChanges(settings_changes_history, "26.8", {...})` block, which
`origin/26.7` does not have. Four of its five settings do not exist in the 26.7
tree at all, so `SettingsImpl::applyCompatibilitySetting` walks the history
newest-first, calls `getTier()` for every entry above the requested version and
reaches `throwSettingNotFound`, making every `compatibility` value below 26.8
fail with `UNKNOWN_SETTING`.

Only the two entries the backport actually needs are kept: the 26.6 behaviour
flip and the 26.4 introduction. This matches the already-merged 26.6 sibling
backport #111987 byte for byte.

Co-Authored-By: Claude Fable 5 <[email protected]>
fm4v added a commit that referenced this pull request Jul 28, 2026
Backport #111589 to 26.7: Add setting `analyzer_compatibility_apply_final_to_all_joined_tables`
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…-setting

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

Labels

pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-improvement Pull request with some product improvements pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.4-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants