Skip to content

Add settings for SQL compatible behaviour of SEMI and ANTI joins - #96662

Merged
alexey-milovidov merged 83 commits into
ClickHouse:masterfrom
zhangyifan27:semi_join_return_one_table_column
Oct 5, 2026
Merged

alexey-milovidov merged 83 commits into
ClickHouse:masterfrom
zhangyifan27:semi_join_return_one_table_column

Conversation

@zhangyifan27

@zhangyifan27 zhangyifan27 commented Feb 11, 2026 •

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

  • Improvement

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

Add settings semi_join_include_columns_from_both_sides and anti_join_include_columns_from_both_sides (enabled by default) for SEMI and ANTI joins in the analyzer. When enabled, behavior is unchanged: columns from both sides are accessible after the join. Disable either setting to enforce SQL-standard semantics, where only the preserved side's columns are visible after the join.

Related: #95738


Note

Medium Risk
Touches query analysis/identifier resolution around JOINs and can change query results or raise UNKNOWN_IDENTIFIER when the new settings are enabled. Default behavior remains unchanged, but the JOIN-scoping logic is subtle and may have edge cases in nested joins and qualified matchers.

Overview
Adds semi_join_compatibility and anti_join_compatibility settings to optionally enforce SQL-standard semantics for SEMI/ANTI JOINs, where only the preserved-side columns are visible in SELECT, WHERE, and qualified matchers.

Implements preserved-side filtering during identifier resolution and SELECT * expansion, while explicitly allowing both sides inside the current JOIN’s ON expression via a new resolving_join_on_expression scope marker. Updates settings docs/history and adds stateless tests covering column visibility, qualified t.*, USING, WHERE, and nested-join scenarios.

Written by Cursor Bugbot for commit a81b4d5. This will update automatically on new commits. Configure here.


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

Version info

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

@qoega

qoega commented Feb 11, 2026

Copy link
Copy Markdown
Member

IMHO it is backward incompatible change and you need to add a setting to control this behaviour. It can be a good change, but you don't want it to be reverted if there is a big set of users that use the other bevahiour.

@zhangyifan27

Copy link
Copy Markdown
Contributor Author

IMHO it is backward incompatible change and you need to add a setting to control this behaviour. It can be a good change, but you don't want it to be reverted if there is a big set of users that use the other bevahiour.

Thanks for the feedback! That's a fair point. We should add a setting to keep backward compatibility.

@novikd novikd self-assigned this Feb 11, 2026
@novikd novikd added the can be tested Allows running workflows for external contributors label Feb 11, 2026
@clickhouse-gh

clickhouse-gh Bot commented Feb 11, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [2d5eed9]

Summary: ✅


AI Review

Summary

This PR adds semi_join_include_columns_from_both_sides and anti_join_include_columns_from_both_sides so SEMI / ANTI joins keep the legacy behavior by default while allowing SQL-standard preserved-side-only visibility when either setting is disabled. I re-read the current diff, the changed code paths, and all prior review threads, and I do not see any remaining correctness, compatibility, or planner issues in the current version.

Final Verdict

✅ No new blockers or majors found in the current diff.

LLVM Coverage Report

Measured on commit 2d5eed9.

Metric Baseline Current Δ
Lines 88.20% 88.20% +0.00%
Functions 92.10% 92.10% +0.00%
Branches 79.60% 79.60% +0.00%

Changed lines: Changed C/C++ lines covered: 360/371 (97.04%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Feb 11, 2026
@zhangyifan27
zhangyifan27 force-pushed the semi_join_return_one_table_column branch 2 times, most recently from bed8e61 to 9bd4eac Compare February 16, 2026 17:25
@clickhouse-gh clickhouse-gh Bot added the manual approve Manual approve required to run CI label Feb 16, 2026
@zhangyifan27
zhangyifan27 force-pushed the semi_join_return_one_table_column branch from 9bd4eac to ebe707c Compare February 20, 2026 01:50
Comment thread src/Core/SettingsChangesHistory.cpp Resolved
Comment thread src/Core/Settings.cpp Resolved
@zhangyifan27
zhangyifan27 force-pushed the semi_join_return_one_table_column branch from ebe707c to 838d1a9 Compare February 25, 2026 10:03
@zhangyifan27
zhangyifan27 requested a review from novikd February 26, 2026 02:56
Comment thread src/Core/Settings.cpp Resolved
@novikd novikd changed the title Fix SELECT * returning right table columns for SEMI and ANTI JOIN Add settings for SQL compatible behaviour of SEMI and ANTI joins Feb 26, 2026
@novikd
novikd added this pull request to the merge queue Feb 28, 2026
@novikd
novikd removed this pull request from the merge queue due to a manual request Feb 28, 2026
Comment thread src/Core/SettingsChangesHistory.cpp Resolved
@zhangyifan27
zhangyifan27 force-pushed the semi_join_return_one_table_column branch from a3df5c9 to 16f6813 Compare February 28, 2026 17:53
@clickhouse-gh clickhouse-gh Bot added pr-improvement Pull request with some product improvements and removed pr-bugfix Pull request with bugfix, not backported by default labels Feb 28, 2026
@zhangyifan27
zhangyifan27 force-pushed the semi_join_return_one_table_column branch from ecb8162 to e2a5772 Compare March 4, 2026 04:03
@clickhouse-gh clickhouse-gh Bot added pr-backward-incompatible Pull request with backwards incompatible changes and removed pr-improvement Pull request with some product improvements labels Mar 4, 2026
@zhangyifan27
zhangyifan27 requested a review from rschu1ze March 6, 2026 04:05
valerypetrov pushed a commit to valerypetrov/ClickHouse that referenced this pull request Sep 24, 2026
… wall clock

`Build profile diff` is SIGKILLed at its 1800 s job timeout while retrying CI
logs cluster reads, so it reds a pull request for an outage of an unrelated
shared service and reports no reason at all.

LogCluster.select bounds a read by attempt count only (retries=8, timeout=60,
plus a 5+10+...+40 = 180 s backoff, so about 684 s worst case for one read, and
requests' timeout bounds one socket read rather than the request, so a
trickling connection was measured at 185 s for a single attempt). It is never
bounded by wall clock, while build_profile_diff_job.py issues 16 db.query reads
inside a job whose timeout is 1800 s. Three degraded reads exhaust the window,
praktika SIGTERM/SIGKILLs the process, and select never returns and never
raises: Db.query cannot propagate, main()'s except LogClusterUnavailable never
runs, and the green report_cluster_unavailable completion added in ClickHouse#121048 is
unreachable in exactly the window it was written for. The kill also skips
update_comment, leaving the comparison comment of an earlier commit of the same
pull request in place as if it were the current one.

Observed on PR ClickHouse#96662 at head 72541f0, which already contains ClickHouse#121048 and
ClickHouse#121393: 18 requests.exceptions.ReadTimeout, zero "LogCluster not ready", and
the job killed exactly 1800 s after its process started with no Result at all.
Reads were succeeding in between, so the job was crawling at about 420 s per
read rather than blocked, which is why the budget is shared across the job's
reads instead of applied to each one.

A LogCluster instance now takes an optional read budget (default None, so every
other caller is unchanged) and select gains one guard at the top of its retry
loop, on a spent budget AND a POST already attempted. Both conditions are
load-bearing rather than conveniences, for the same reason: the post-loop
discriminator is defined by what the attempts did, so any read that leaves the
loop having attempted no POST classifies as LogClusterNotReady (red) even though
the cluster was answering. Exempting attempt 0 covers the read that starts after
the deadline; requiring post_attempted covers the read whose first readiness
probe fails transiently, which would otherwise break at the second iteration
with nothing attempted. So the budget stops POST retries only, and the readiness
schedule, which exists because is_ready fails during the same pressure spikes as
the query itself, always keeps its full count. The added cost is bounded and
paid at most once per job: a read that never becomes ready spends 8 probes plus
the 180 s backoff, about 204 s, and then ends the job. No new green path appears,
LogClusterNotReady stays red and TRANSIENT_EXCEPTION_CODES is untouched, so
both the rule that only "the cluster never answered" is green and ClickHouse#121393's
narrowing that keeps 159 and 209 hard continue to hold.

The number is derived from the job's own configured timeout via
Info().job_config, following stateful_prep_step_timeout in
ci/jobs/functional_tests.py, minus a fixed reserve of 550 s, so 1250 s at the
current timeout=1800. The reserve is fixed rather than a fraction because every
component of it is independent of the timeout: interpreter startup before the
first read, one in-flight POST overrunning its 60 s socket timeout, one
already-started backoff, and the reporting tail. The docker image pull is not
charged to it, because job.timeout only starts with the job process.

Applied to the ClickHouse#96662 timeline the read raises at about 13:21:20 instead, 8.5
minutes before the kill, and the check completes green with the reason and a
refreshed comment. The failed-attempt spacing there (65/70/75/80/86/90 s, the
60 s socket timeout plus 5*(retry+1)) identifies the 13:20:43 failure as the
seventh attempt of that read, so the guard is next reached after its 35 s
backoff; 13:23:14 is the first attempt of the following read, which the change
never reaches.

Validated against a local HTTP stub over three runs with identical
classifications, with a control on the pre-change file that matches wherever no
budget is passed, with assertions on the derivation keyed on the real
JobConfigs.build_profile_diff_job.timeout, and with a case that pins the
readiness behaviour: a spent budget plus one transient probe failure returns the
body, while the same case without the post_attempted condition raises
LogClusterNotReady.

Co-Authored-By: Claude Opus 5 <[email protected]>
alexey-milovidov and others added 2 commits September 26, 2026 03:10
…e_table_column

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…e_table_column

Resolve the conflict with the move of the settings changes history into the setting declarations
(ee6b44f): the history records this PR added to `SettingsChangesHistory.cpp` now live as
trailing arguments of the `DECLARE` of each setting.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@alexey-milovidov alexey-milovidov self-assigned this Oct 1, 2026

@alexey-milovidov alexey-milovidov 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.

Ok, but I don't like setting names. Instead of ...compatibility let's unwrap the meaning of them.

@alexey-milovidov

Copy link
Copy Markdown
Member

semi_join_include_columns_from_both_sides = true
anti_join_include_columns_from_both_sides = true

…th_sides` / `anti_join_include_columns_from_both_sides`

Replace `semi_join_compatibility` and `anti_join_compatibility` with settings
whose names state their meaning. The new settings have the inverted meaning:
they are enabled by default (legacy behavior, columns from both sides of a
`SEMI` / `ANTI` JOIN are accessible), and disabling them restricts access to
the preserved side in accordance with the SQL standard.

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

Copy link
Copy Markdown
Member

🕵 Renamed the settings as requested (ffb21dc2f47): semi_join_compatibility / anti_join_compatibility are replaced by semi_join_include_columns_from_both_sides / anti_join_include_columns_from_both_sides. The meaning is inverted: they default to true, which keeps the existing behavior (columns from both sides are accessible), and setting them to false restricts access to the preserved side as the SQL standard requires. I updated the settings docs, the JOIN docs note, code comments, the test, and the changelog entry. 03913_semi_anti_join_column_access passes locally.

… JOIN

Address review: pin down that a sibling table on the hidden side of an outer
`SEMI` JOIN cannot leak into a nested `JOIN ON`. Parenthesized joined-table
groups become subqueries and comma joins are folded into the left operand of
the next `JOIN`, so the only reachable shapes are a preceding table (part of
the nested join's own left operand, visible), a later-joined table (not
visible, as with the settings enabled), and the hidden side of an earlier
`SEMI` JOIN (still denied).

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

Copy link
Copy Markdown
Member

🕵 The only failure on c0ff03873e5 is 05241_external_distinct_coalescing in Stateless tests (amd_debug, parallel). It is unrelated to this PR: the test's SET max_memory_usage = 134217728 also applies to its system.query_log check, which hit MEMORY_LIMIT_EXCEEDED. @groeneai already has a fix open: #123286. The 03913_semi_anti_join_column_access test passes locally with current master merged in. The requested renames (semi_join_include_columns_from_both_sides / anti_join_include_columns_from_both_sides) are in, and the old names are gone from the tree.

@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 Merged master (clean, no conflicts) as a9e60297a09 to pick up the latest fixes and re-run CI; the PR's three-dot diff against master is unchanged (same 12 files). Rebuilt and 03913_semi_anti_join_column_access passes locally. The previous red job (05241_external_distinct_coalescing in Stateless tests (amd_debug, parallel)) is unrelated; its fix is still open in #123286. The requested settings rename (semi_join_include_columns_from_both_sides / anti_join_include_columns_from_both_sides) is already in (ffb21dc2f47), so the CHANGES_REQUESTED review can be re-reviewed/dismissed by the reviewer.

# Conflicts:
#	src/Common/ErrorCodes.cpp
@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 Merged master as da0a2c467fb to resolve a conflict in src/Common/ErrorCodes.cpp: master took code 1021 for AZURE_OBJECT_CHANGED_DURING_READ, so SEMI_ANTI_JOIN_COLUMN_ACCESS_DENIED now uses 1022. The PR's diff against master is otherwise unchanged (same 12 files), the settings history check passes, and 03913_semi_anti_join_column_access passes locally on the rebuilt binary. The requested settings rename (semi_join_include_columns_from_both_sides / anti_join_include_columns_from_both_sides) is already in ffb21dc2f47, and there are no unresolved review threads.

Comment thread src/Analyzer/Resolve/QueryAnalyzer.cpp Resolved
With `analyzer_compatibility_prefer_alias_over_subcolumn = 1`, when the
qualifier of `t.*` names both a table and a Tuple column, the matcher only
ran the SEMI/ANTI access check for the table and then still expanded the
Tuple subcolumns. Skip the Tuple expansion in that case and fall through to
the table-expression path, which also performs the access check. Remove the
duplicated Tuple expansion code and add regressions for the accessible-table
case.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Address review: `tryBindIdentifierToArrayJoinExpressions` still treated the
aliases of an `ARRAY JOIN` on the hidden side of a `SEMI`/`ANTI` JOIN as live
binders, so with `semi_join_include_columns_from_both_sides = 0` a
preserved-side column of the same name was qualified by `SELECT *` (e.g. as
`l.a`) and an outer `SELECT a` failed. Skip `ArrayJoinNode`s hidden by
`isTableExpressionHiddenBySemiAntiJoin`, as `tryBindIdentifierToTableExpressions`
already does.

Such an `ARRAY JOIN` could not be resolved at all before: its own expression
(`ARRAY JOIN arr` over the hidden table) was denied. Resolve the `ARRAY JOIN`
expressions with `resolving_join_on_expression` pointing at the `ARRAY JOIN`
node, so `SemiAntiJoinSideChecker` exposes only the side that contains its
input, the same way a nested JOIN's `ON` expression is handled. The early
`AND`/`OR` short-circuit exclusion stays limited to JOIN `ON` expressions.

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

Copy link
Copy Markdown
Member

🕵 Pushed 2d5eed9b0d4 to address the last AI-review finding: aliases of an ARRAY JOIN on the hidden side of a SEMI/ANTI JOIN no longer make a preserved-side column ambiguous. Such an ARRAY JOIN can now also resolve its own input. Details are in the thread. 03913_semi_anti_join_column_access passes locally with the new cases. The default behavior (both settings true) is unchanged.

The only CI failure on 229689d23e4 is 05218_text_index_substreams_opened_lazily in Stateless tests (amd_debug, parallel). It is unrelated (text index substreams, and all reruns passed); @groeneai already has a fix open: #123823.

@alexey-milovidov alexey-milovidov 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.

Not too bad.

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 5, 2026
Merged via the queue into ClickHouse:master with commit 78adf94 Oct 5, 2026
149 checks passed
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 5, 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... manual approve Manual approve required to run CI pr-improvement Pull request with some product improvements 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.

8 participants