Skip to content

Analyzer: fix FINAL leaking onto other tables of a JOIN - #108979

Merged
novikd merged 2 commits into
masterfrom
vdimir/fix-analyzer-final-join-prewhere-pushdown
Jul 9, 2026
Merged

novikd merged 2 commits into
masterfrom
vdimir/fix-analyzer-final-join-prewhere-pushdown

Conversation

@vdimir

@vdimir vdimir commented Jun 30, 2026 •

Copy link
Copy Markdown
Member

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

  • Fixed a bug in the new analyzer where FINAL on one table of a JOIN (e.g. FROM t1 FINAL JOIN t2) was incorrectly applied to the other joined tables as well, what could made such queries slower.

Version info

  • Merged into: 26.7.1.719 (included in 26.7 and later)
  • Backported to: 26.6.2.56

@clickhouse-gh

clickhouse-gh Bot commented Jun 30, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [3103ca6]

Summary: ✅


AI Review

Summary

This PR fixes the analyzer so FINAL stays attached only to the table expression it was written on, and it also preserves the forced-FINAL path for Merge reads over MaterializedPostgreSQL. The current head includes focused stateless coverage for left/right/both-side FINAL in JOINs plus an integration test for the Merge/MaterializedPostgreSQL corner case from the earlier review thread. I did not find any remaining correctness or safety issues in the current diff.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jun 30, 2026
Comment thread src/Storages/SelectQueryInfo.cpp Resolved
@vdimir

vdimir commented Jul 1, 2026

Copy link
Copy Markdown
Member Author

@groeneai create repro for Unit tests (asan_ubsan, function_prop_fuzzer) failure and check out other ones

@groeneai

groeneai commented Jul 1, 2026

Copy link
Copy Markdown
Collaborator

@vdimir Triaged all 4 reds on 3f738b226a; none are caused by this PR (analyzer-only change).

1. Unit tests (asan_ubsan, function_prop_fuzzer) -> FunctionsStress.stress / AllTests

The failing fuzzer query is reinterpret(<Decimal>, 'Decimal(76, 24)') over Array(Decimal(76, 41)) -> Code: 117 ... function returned column of unexpected type or scale: Decimal256 instead of Decimal(76, 24).

Deterministic repro (validated in #108878):

SELECT reinterpret(a, CAST('Decimal128(2)' AS String))
FROM system.one
ARRAY JOIN CAST([0.0816, 1036.36, -0.0200, 0.00265] AS Array(Decimal(38, 33))) AS a;

Root cause: FunctionReinterpret::executeImpl's same-physical-type branch (is_same_v<From,To>) returns the source column verbatim, so the result column keeps the source scale while its declared type carries the target scale. The SQL values are correct (reinterpret is a bit relabel), so a plain client run shows no error; only the strict columnMatchesType gate at gtest_functions_stress.cpp:2427 (added by #108551) catches the internal scale divergence.

Not PR-caused: the same shape hit master 8x and 52 distinct PRs on 2026-06-30 (the day this ran). Fix: #108878 (ours, open) rebuilds the column at the target scale; its regression unit test fails without the fix and passes with it.

2 & 3. Stress test (amd_tsan / arm_tsan) -> "Hung check failed, possible deadlock found"

Chronic hung-check family, issue #107941 (~490 hits / ~170 distinct PRs / 49 on master on 2026-06-30 alone). Fixing PRs: #108212 (merged) and #105905 (open).

4. BuzzHouse (amd_tsan) -> ThreadSanitizer: data race (STID 2604-385d)

Race copying the live query Context during RESTORE: Context::createCopy -> ContextData copy reads shared_ptr<MergeTreeTransaction>, reached from RestorerFromBackup::createTable. Cross-PR flake (9 hits / 9 distinct PRs / 0 master, 2026-06-17..06-30). Fixing PR: #108765 (open).

@novikd novikd self-assigned this Jul 2, 2026

@novikd novikd 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.

The idea for a fix makes sense, but corner cases should be handled correctly.

Comment thread src/Storages/SelectQueryInfo.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.60% 85.60% +0.00%
Functions 92.70% 92.70% +0.00%
Branches 77.70% 77.70% +0.00%

Changed lines: Changed C/C++ lines covered: 13/20 (65.00%) · Uncovered code

Full report · Diff report

@vdimir
vdimir requested a review from novikd July 7, 2026 14:30
@novikd
novikd added this pull request to the merge queue Jul 9, 2026
Merged via the queue into master with commit 75ace66 Jul 9, 2026
343 of 344 checks passed
@novikd
novikd deleted the vdimir/fix-analyzer-final-join-prewhere-pushdown branch July 9, 2026 16:02
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Jul 9, 2026
@robot-clickhouse robot-clickhouse added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Jul 9, 2026
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 9, 2026
clickhouse-gh Bot pushed a commit that referenced this pull request Jul 9, 2026
clickhouse-gh Bot added a commit that referenced this pull request Jul 9, 2026
Backport #108979 to 26.6: Analyzer: fix FINAL leaking onto other tables of a JOIN
@fm4v

fm4v commented Jul 23, 2026

Copy link
Copy Markdown
Member

I'm wrapping this logic in the setting analyzer_compatibility_apply_final_to_all_joined_tables (#111589) for two reasons:

  1. I want to backport it to 26.4. (no logic change by default)
  2. It causes duplicates in query results between 26.4-26.6 for queries like this: https://github.com/ClickHouse/clickhouse-private/issues/65599

rorylshanks pushed a commit to rorylshanks/ClickHouse that referenced this pull request Jul 26, 2026
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.
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
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.
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…zer-final-join-prewhere-pushdown

Analyzer: fix FINAL leaking onto other tables of a JOIN
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-bugfix Pull request with bugfix, not backported by default 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.6-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants