Skip to content

Fix incorrect JOIN results caused by silently dropped WHERE filters - #101652

Merged
alexey-milovidov merged 9 commits into
ClickHouse:masterfrom
wudidapaopao:fix_join_query
May 1, 2026
Merged

alexey-milovidov merged 9 commits into
ClickHouse:masterfrom
wudidapaopao:fix_join_query

Conversation

@wudidapaopao

@wudidapaopao wudidapaopao commented Apr 2, 2026 •

Copy link
Copy Markdown
Contributor

Fixed #102002

When extractActionsForJoinCondition encountered an equals conjunct with mismatched argument types (e.g. UInt32 vs UInt64), it would continue without adding the conjunct to rejected_conjuncts. If another conjunct in the same WHERE clause was successfully extracted into the JOIN condition, the type-mismatched one was silently lost and the FilterStep could be replaced by an ExpressionStep, producing incorrect query results.

Reproducer: https://fiddle.clickhouse.com/3306ebe1-2dc7-402b-84da-145ed91e0c7e

DROP TABLE IF EXISTS t1;
DROP TABLE IF EXISTS t2;

CREATE TABLE t1 (c1 UInt32, c2 String, c3 String) ENGINE = Memory;
CREATE TABLE t2 (c1 UInt64, c2 String, c3 String) ENGINE = Memory;

INSERT INTO t1 VALUES (1, 'x', 'x'), (2, 'y', 'y');
INSERT INTO t2 VALUES (1, 'x', 'x'), (3, 'y', 'y');

SELECT * FROM t1 INNER JOIN t2 ON t1.c3 = t2.c3 WHERE t1.c1 = t2.c1 AND t1.c2 = t2.c2;
-- incorrect output: 
-- 1	x	x	1	x	x
-- 2	y	y	3	y	y


DROP TABLE t1;
DROP TABLE t2;

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

Fix incorrect JOIN query results when mergeFilterIntoJoinCondition optimization silently dropped a WHERE equality condition with mismatched types.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.5.1.239

@clickhouse-gh

clickhouse-gh Bot commented Apr 2, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [805ba1d]

Summary: ✅


AI Review

Summary

This PR fixes a real correctness bug in mergeFilterIntoJoinCondition: when an equals conjunct had mismatched argument types, that conjunct could be silently dropped if other conjuncts were pushed into JOIN, leading to incorrect query results. The fix is surgical and correct (rejected_conjuncts now retains such conjuncts), and the added stateless test reproduces and verifies the behavior.

ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ✅
Compilation time ✅
No large/binary files ✅
Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Apr 2, 2026
@wudidapaopao wudidapaopao changed the title Fix silently dropped WHERE condition in mergeFilterIntoJoinCondition Fix incorrect JOIN results caused by silently dropped WHERE filters Apr 3, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member

The Stress test (arm_msan) failure is fixed by #101239, which should be merged first. After it is merged, please update the branch to include the fix.

@alexey-milovidov

Copy link
Copy Markdown
Member

The test 02859_replicated_db_name_zookeeper is fixed in #101952

@alexey-milovidov

Copy link
Copy Markdown
Member

The MSan stress test failure (MemorySanitizer: use-of-uninitialized-value, STID 4179-5154 or 4148-3044) is a known pre-existing issue unrelated to this PR. Fix: #102158

@alexey-milovidov

Copy link
Copy Markdown
Member

The flaky failure of 02494_query_cache_http_introspection in this PR's CI is addressed by #102165.

@wudidapaopao

Copy link
Copy Markdown
Contributor Author

Hi @novikd, this is an easy-to-review fix. Feel free to take a look when you have time.

@clickhouse-gh

clickhouse-gh Bot commented Apr 27, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.00% 84.10% +0.10%
Functions 91.10% 91.10% +0.00%
Branches 76.50% 76.60% +0.10%

Changed lines: 100.00% (4/4) · Uncovered code

Full report · Diff report

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue May 1, 2026
@alexey-milovidov alexey-milovidov self-assigned this May 1, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member

Magnificent! Thank you for the fix!

Merged via the queue into ClickHouse:master with commit b7507c4 May 1, 2026
165 checks passed
@clickgapai

Copy link
Copy Markdown
Contributor

Hi — this PR may need backporting to 26.3 (LTS), 26.2, 26.1, 25.8 (LTS), but no backport label was found.

Affected code: extractActionsForJoinCondition in src/Processors/QueryPlan/Optimizations/mergeFilterIntoJoinCondition.cpp — introduced in 25.4.

Why: The buggy file src/Processors/QueryPlan/Optimizations/mergeFilterIntoJoinCondition.cpp was first added 2025-04-09 (commit 3ce3dc3, 'Merge filter into JOIN condition'), before the 25.8 LTS release (2025-08-29). All four currently supported release branches (25.8 lts, 26.1, 26.2, 26.3 lts) contain the unfixed code path that silently drops type-mismatched WHERE equality conjuncts, producing incorrect JOIN results — classic P0 silent-wrong-results bug.

If this should be backported, consider adding pr-must-backport or a version-specific label (e.g. v26.3-must-backport). Ignore this if backporting is not applicable.

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

Labels

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.

Type-mismatched equality conjunct silently dropped when another conjunct is pushed into JOIN

4 participants