Skip to content

Fix Invalid number of rows in Chunk in JoiningTransform with additional join filter - #106928

Merged
alexey-milovidov merged 5 commits into
ClickHouse:masterfrom
groeneai:groeneai/test-join-global-left-semi-chunk-3366-2d1d
Jul 30, 2026
Merged

alexey-milovidov merged 5 commits into
ClickHouse:masterfrom
groeneai:groeneai/test-join-global-left-semi-chunk-3366-2d1d

Conversation

@groeneai

@groeneai groeneai commented Jun 10, 2026 •

Copy link
Copy Markdown
Collaborator

A LEFT JOIN with unique right keys is promoted to RIGHT ANY (need_replication = false). When such a join also has a mixed ON condition (equality plus inequality), the probe goes through joinRightColumnsWithAdditionalFilter. There added_columns.filter is sized for the full selector when a right key column is selected, but the probe loop can break early at max_joined_block_size_rows and return a smaller left_block_rows. The trailing filter.resize(left_block_rows) was only done under need_replication, so for the RIGHT ANY case the filter kept the full size while the left block was cut downstream. The required right key column rebuilt from the oversized filter then mismatched its siblings, tripping Chunk::checkNumRowsIsConsistent (LOGICAL_ERROR in JoiningTransform).

Fix: trim the filter to left_block_rows in the non-replication case too.

Deterministic repro on debug with max_joined_block_size_rows = 1000:

SELECT t1.generate_series
FROM numbers(41712) t0
LEFT JOIN generateSeries(5297, 67368) t1
  ON (t0.number <= t1.generate_series) AND (t1.generate_series = t0.number);

Found by the server-side BuzzHouse fuzzer on master, amd_msan (STID 3366-2d1d).

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 to CHANGELOG.md):

Fix a LOGICAL_ERROR (Invalid number of rows in Chunk) in JoiningTransform for a LEFT JOIN with unique right keys, a mixed ON condition, and a small max_joined_block_size_rows.

Version info

  • Merged into: 26.8.1.461 (included in 26.8 and later)
  • Backported to: 26.7.2.46, 26.6.2.141, 26.5.7.7, 26.3.17.110, 25.8.29.51

@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate

# Question Answer
a Deterministic repro? No. The BuzzHouse finding does not reproduce on master HEAD after 260+ runs (debug + ASAN+UBSAN) varying the relevant join settings. The full trigger needed BuzzHouse stateful workload that is no longer available (CI artifacts expired, 48 days old).
b Root cause explained? Partial. The crash is in the eager join output path (appendRightColumns offsets-replication in HashJoinResult.cpp), where one output column is sized by the right table size (14075 = numbers_mt(14075)) while a sibling holds the matched count (6784). Exact corrupting state not pinned without the stateful repro. The 2026-06-02 revert of #98883 (lazy indexing) is unrelated: ancestry check shows #98883 was not on master at the 2026-04-23 failure.
c Fix matches root cause? N/A. No code change. A speculative code fix for a non-reproducing bug is intentionally avoided.
d Test intent preserved / new tests added? Yes. Adds a stateless regression test (the captured query) so the CI random-settings sweep covers this join shape permanently.
e Both directions demonstrated? N/A for the failing direction (does not reproduce). The test passes deterministically on HEAD (8817 rows, no crash) across the randomized join settings the CI runner applies.
f Fix is general, not a narrow patch? N/A. No code change.

Session id: cron:clickhouse-worker-slot-7:20260610-072600

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @vdimir could you review this? It is a test-only PR adding a stateless regression test for a rare BuzzHouse-found Invalid number of rows in Chunk in JoiningTransform on a GLOBAL LEFT JOIN + SEMI LEFT JOIN. The finding does not reproduce on HEAD (260+ debug/ASAN runs), so the goal is just permanent CI coverage of the join shape, no code change. Picked you since you fixed the same signature in #84213.

@Algunenano Algunenano added the can be tested Allows running workflows for external contributors label Jun 10, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 10, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [b9318a1]

Summary: ✅


AI Review

Summary

The fix in src/Interpreters/HashJoin/HashJoinMethodsImpl.h closes the row-count invariant that was still broken in the non-replication additional-filter path: once the probe loop stops early at max_joined_block_rows, added_columns.filter now gets trimmed to left_block_rows before HashJoinResult uses it to rebuild required right-key columns. That matches the downstream left-block split and is consistent with the already-existing replication branch. The regression test in tests/queries/0_stateless/04330_join_global_left_semi_chunk_consistency.sql exercises both the minimized reproducer and the original fuzzer-shaped query, and the current PR CI is green.

Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Jun 10, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

Update: I found a deterministic single-threaded reproducer, so this is a real master-HEAD bug, not a non-reproducing artifact. The flaky-check on this PR confirmed it (the added test crashed the server under msan/tsan), and I then reduced it to:

SELECT t1.generate_series
FROM numbers(41712) AS t0
LEFT JOIN generateSeries(5297, 67368) AS t1
  ON (t0.number <= t1.generate_series) AND (t1.generate_series = t0.number)
FORMAT Null
SETTINGS max_joined_block_size_rows = 1000;

About 100% abort on a debug build, with max_threads = 1. Necessary conditions (each verified by ablation): LEFT JOIN with unique right keys (so ALL is promoted to RightAny in onBuildPhaseFinish), a mixed ON residual referencing both tables (routes through joinRightColumnsWithAdditionalFilter), and max_joined_block_size_rows below the joined row count (enters the splitting / early-break path).

Mechanism: in joinRightColumnsWithAdditionalFilter the loop breaks early on current_added_rows >= max_joined_rows (pre-filter candidate count) and returns a truncated left-row count that joinBlockImpl uses to split the left block, but the appended columns are sized by output rows actually produced, so left and right column sizes diverge and Chunk::checkNumRowsIsConsistent throws at JoiningTransform.cpp:236.

Because the test reliably crashes the server, it should not be merged as a passing stateless test until the code is fixed. I am working on the fix to the accounting in that path, after which the test will pass and serve as the regression guard. Full details are in the updated PR description.

…al filter and small max_joined_block_size_rows

A LEFT JOIN whose right keys are unique gets promoted to RIGHT ANY
(MapsAll, need_replication = false). When such a join also has a mixed
ON condition referencing both tables (equality plus an inequality), the
probe goes through HashJoinMethods::joinRightColumnsWithAdditionalFilter.

In that path added_columns.filter is allocated for the full selector size
(selector.size()) whenever need_filter is set, which happens when a right
key column is selected (has_required_right_keys). The probe loop can break
early at max_joined_block_size_rows, so it processes only a prefix of the
left rows and returns that smaller left_block_rows. The trailing resize of
added_columns.filter was done only under "if constexpr (need_replication)".
For the RIGHT ANY case need_replication is false, so the filter kept the
full selector size while the left block was cut to left_block_rows
downstream. The required right key column is rebuilt from this oversized
filter via filterWithBlanks, producing a column larger than its siblings,
which trips Chunk::checkNumRowsIsConsistent (LOGICAL_ERROR
"Invalid number of rows in Chunk ... in JoiningTransform").

Trim added_columns.filter to left_block_rows in the non-replication case as
well, so the required right key column matches the left block size.

Reproduces deterministically on a debug build with
max_joined_block_size_rows = 1000:

  SELECT t1.generate_series
  FROM numbers(41712) t0
  LEFT JOIN generateSeries(5297, 67368) t1
    ON (t0.number <= t1.generate_series) AND (t1.generate_series = t0.number)

Found by the server-side BuzzHouse fuzzer (STID 3366-2d1d) on master,
amd_msan. The error sizes observed in CI (matched count vs full left size:
e.g. 6297 vs 41712, 19265 vs 24515, 27810 vs 41712) match this root cause.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai
groeneai force-pushed the groeneai/test-join-global-left-semi-chunk-3366-2d1d branch from a4d2594 to fba8b16 Compare June 10, 2026 12:41
@groeneai groeneai changed the title Add regression test for GLOBAL LEFT JOIN + SEMI LEFT JOIN chunk consistency Fix Invalid number of rows in Chunk in JoiningTransform with additional join filter Jun 10, 2026
@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default and removed pr-ci labels Jun 10, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate

# Question Answer
a Deterministic repro? Yes. On a debug build, clickhouse-local --max_joined_block_size_rows=1000 -q "SELECT t1.generate_series FROM numbers(41712) t0 LEFT JOIN generateSeries(5297,67368) t1 ON (t0.number<=t1.generate_series) AND (t1.generate_series=t0.number) FORMAT Null" aborts every run (6/6 on the unfixed binary).
b Root cause explained? LEFT JOIN with unique right keys is promoted to RIGHT ANY (need_replication=false). A mixed ON (equality + inequality) routes through joinRightColumnsWithAdditionalFilter, which allocates added_columns.filter for the full selector size when a right key column is selected. The probe loop breaks early at max_joined_block_size_rows and returns a smaller left_block_rows, but filter.resize(left_block_rows) ran only under if constexpr(need_replication). For the RIGHT ANY case the filter stayed full-size while the left block was cut downstream; the required right key column rebuilt from the oversized filter (filterWithBlanks) then exceeded its siblings, tripping Chunk::checkNumRowsIsConsistent in JoiningTransform.
c Fix matches root cause? Yes. The filter is trimmed to left_block_rows in the non-replication branch too, so the right key column matches the cut left block. No symptom-masking.
d Test intent preserved / new tests added? New stateless test 04330_join_global_left_semi_chunk_consistency added (the minimal LEFT JOIN repro + the original BuzzHouse query), pinned to a small max_joined_block_size_rows so it deterministically exercises the split path.
e Both directions demonstrated? Yes. Unfixed binary (master fc73f46): minimal repro 6/6 abort, both test queries rc=134. Fixed rebuild: minimal repro 0/8 abort, test outputs 41712 / 8817; the fixed split path returns results identical to the no-split (max_joined_block_size_rows=0) baseline.
f Fix is general, not a narrow patch? Verified by ablation that the bug is specific to this branch (RIGHT ANY + additional filter + required right key + early break); the sibling joinRightColumns path is unaffected because there properties.need_filter=true bypasses filterWithBlanks. INNER joins (filter_ptr=nullptr), equi-only joins, and non-unique-right (true ALL, need_replication=true, already resized) do not hit it. Correctness of unrelated join variants confirmed identical with/without the fix. The fix is at the source of the size inconsistency, not a downstream guard.

Session id: cron:clickhouse-worker-slot-1:20260610-113200

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @vdimir @KochetovNicolai — could you review this? It fixes a LOGICAL_ERROR (Invalid number of rows in Chunk in JoiningTransform) in the additional-filter join path: joinRightColumnsWithAdditionalFilter left added_columns.filter sized for the full selector when the probe loop broke early at max_joined_block_size_rows and need_replication was false (LEFT JOIN promoted to RIGHT ANY). @vdimir — same signature as the one you fixed in #84213.

@vdimir vdimir self-assigned this Jun 10, 2026
The regression test uses a mixed JOIN ON condition (equality plus an
inequality: t0.number <= t1.generate_series). That shape is only
supported by the new analyzer; the old analyzer rejects it in
CollectJoinOnKeysMatcher with INVALID_JOIN_ON_EXPRESSION (code 403).

The CI old-analyzer variant therefore failed on this test (return code
147). Tag the test no-old-analyzer so it only runs under the new
analyzer, which is the path the fixed bug lives on.

Code unchanged; this only adjusts the test's run tags.

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

Copy link
Copy Markdown
Collaborator Author

Pushed a follow-up commit (7b91eab) adding no-old-analyzer to the regression test 04330.

The CI old-analyzer variant (Stateless tests (..., old analyzer, ...)) failed because the test's repro query uses a mixed JOIN ON condition (equality plus the inequality t0.number <= t1.generate_series). That shape is only supported by the new analyzer; the old analyzer rejects it in CollectJoinOnKeysMatcher with INVALID_JOIN_ON_EXPRESSION (code 403). Since the fixed bug lives on the new-analyzer JOIN ON path, the test is tagged to run there only.

Code is unchanged from the approved commit (fba8b16) — this only adjusts the test's run tags. Verified locally: under enable_analyzer=0 the query errors with INVALID_JOIN_ON_EXPRESSION; under the default analyzer with the fix it returns the expected 41712 / 8817.

@clickhouse-gh

clickhouse-gh Bot commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Dear @vdimir, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself.

@robot-clickhouse robot-clickhouse added pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-synced-to-cloud The PR is synced to the cloud repo labels Jul 30, 2026
clickhouse-gh Bot added a commit that referenced this pull request Jul 31, 2026
Backport #106928 to 26.7: Fix Invalid number of rows in Chunk in JoiningTransform with additional join filter
clickhouse-gh Bot pushed a commit that referenced this pull request Aug 2, 2026
…ingTransform with additional join filter
clickhouse-gh Bot added a commit that referenced this pull request Aug 2, 2026
Backport #106928 to 26.6: Fix Invalid number of rows in Chunk in JoiningTransform with additional join filter
@nickitat

nickitat commented Aug 3, 2026

Copy link
Copy Markdown
Member

@vdimir wdyt about backporting it to earlier versions?

@vdimir vdimir added pr-must-backport Pull request should be backported intentionally. Use this label with great care! and removed pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR labels Aug 3, 2026
@vdimir

vdimir commented Aug 3, 2026

Copy link
Copy Markdown
Member

Let's try, added pr-must-backport

@robot-clickhouse robot-clickhouse added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Aug 3, 2026
@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 Aug 3, 2026
alexey-milovidov added a commit that referenced this pull request Aug 4, 2026
Backport #106928 to 25.8: Fix Invalid number of rows in Chunk in JoiningTransform with additional join filter
alexey-milovidov added a commit that referenced this pull request Aug 4, 2026
Backport #106928 to 26.3: Fix Invalid number of rows in Chunk in JoiningTransform with additional join filter
alexey-milovidov added a commit that referenced this pull request Aug 4, 2026
Backport #106928 to 26.5: Fix Invalid number of rows in Chunk in JoiningTransform with additional join filter
joelynch pushed a commit to aiven/ClickHouse that referenced this pull request Aug 17, 2026
…nk in JoiningTransform with additional join filter
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Aug 24, 2026
…nk in JoiningTransform with additional join filter
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Aug 24, 2026
…nk in JoiningTransform with additional join filter
@PedroTadim PedroTadim added pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR and removed pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR labels Sep 14, 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 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 Pull request should be backported intentionally. Use this label with great care! 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.

9 participants