Repository navigation
Fix column count mismatch in HashJoin::getNonJoinedBlocks after join side swap - #100617
alexey-milovidov wants to merge 35 commits into
Conversation
…n side swap After join side swapping (e.g., LEFT ANTI → RIGHT ANTI for GLOBAL NOT IN), a column name can appear in both the left block and `sample_block_with_columns_to_add` or `required_right_keys`. There can also be duplicates within the left block itself. The `result_sample_block` deduplicates such columns, so the previous check that compared raw column counts (left + right_keys + right_add) against `result_sample_block.columns()` was too strict and triggered a LOGICAL_ERROR exception. Replace the raw-sum check with a unique-name-set comparison that correctly handles all deduplication cases. Closes #100422 https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=100378&sha=ce356689f6a6d126e078f5cbfcbab8b3849de673&name_0=PR&name_1=AST%20fuzzer%20%28amd_tsan%29 Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
|
Workflow [PR], commit [29b559f] Summary: ❌
AI ReviewSummaryThis PR removes a brittle column-count assertion in ClickHouse Rules
Final Verdict
|
|
Hi @alexey-milovidov, the fix direction is correct but the NameSet approach over-deduplicates. Here's why the 8 tests fail and a suggested correction. Why the 8 Tests FailThe 8 failing tests ( SELECT 'y', * FROM (SELECT count('y'), count('y'), 2 AS x) AS t1
RIGHT JOIN (SELECT count('x'), count('y'), 3 AS x) AS t2 ON t1.x = t2.x;Here The Root Difference
Suggested Fix: Cross-Side Overlap OnlyInstead of using NameSet for everything, subtract only the cross-side overlap (names that appear in both the left side and the right side / keys): size_t left_columns_count = left_sample_block.columns();
if (canRemoveColumnsFromLeftBlock())
left_columns_count = table_join->getOutputColumns(JoinTableSide::Left).size();
// Build unique name set for left side only
NameSet left_names;
if (canRemoveColumnsFromLeftBlock())
for (const auto & col : table_join->getOutputColumns(JoinTableSide::Left))
left_names.insert(col.name);
else
for (const auto & name : left_sample_block.getNames())
left_names.insert(name);
// Count names that appear in BOTH left and (right_keys ∪ to_add)
size_t cross_side_overlap = 0;
for (size_t i = 0; i < required_right_keys.columns(); ++i)
if (left_names.count(required_right_keys.getByPosition(i).name))
++cross_side_overlap;
for (size_t i = 0; i < sample_block_with_columns_to_add.columns(); ++i)
if (left_names.count(sample_block_with_columns_to_add.getByPosition(i).name))
++cross_side_overlap;
size_t expected_columns_count = left_columns_count
+ required_right_keys.columns()
+ sample_block_with_columns_to_add.columns()
- cross_side_overlap;This:
|
…rlap-getNonJoinedBlocks
The previous fix compared `expected_names.size()` (unique) against `result_sample_block.columns()` (raw count including duplicates). When duplicate column names exist in the result block (e.g., after join side swap), `result_sample_block` also has duplicates, so the comparison was still incorrect. Compare unique name sets on both sides instead. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
After join side swapping (e.g., LEFT ANTI → RIGHT ANTI via GLOBAL NOT IN), column names can appear in both the left block and sample_block_with_columns_to_add. AddedColumns correctly skips right-side columns whose renamed name already exists in the left block, but the assertion in getNonJoinedBlocks computed the expected column count as a naive sum (left + right_keys + columns_to_add) without accounting for this cross-side overlap. The fix counts right-side columns whose renamed name appears in the left block (matching the exact deduplication logic in AddedColumns) and subtracts this overlap from the expected count. Unlike the NameSet-based approach in PR ClickHouse#100617, this preserves within-left duplicate column names which are legitimate in queries with subqueries. Fixes STID 2980-385f — P0 recurring AST fuzzer crash hitting 20+ PRs and master over the last 12 days.
… check The previous commit constructed `NameSet result_names` from `result_sample_block.getNames().begin()` and `.end()` — two calls to `getNames` produce two different temporary `Names` vectors, so iterating between iterators of different containers is undefined behaviour. In the `fast_build` CI (debug ASan), this caused a fatal crash inside `std::unordered_set::insert` during the regression test `02534_join_prewhere_bug`. Store `getNames` once and construct the set from the single vector. Also rewords the exception: the invariant is a name-set equality, not a column count, so the message now says "Unexpected columns" and prints the observed and expected name lists directly instead of an arithmetic sum that was no longer meaningful after deduplication. CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=100617&sha=1ebb3a8182645266a98b8547d92f8b74258139fc&name_0=PR&name_1=Fast%20test Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
The previous version discarded the query output with `>/dev/null 2>&1 ||:`, so the test would have passed even if the `LOGICAL_ERROR` this PR is fixing came back — defeating the purpose of the regression check. Use `set -e` and a helper that captures stderr and fails the test if it contains `LOGICAL_ERROR` or the specific `Unexpected columns in result sample block` / `Unexpected number of columns in result sample block` messages. Other errors (type mismatch, syntax, etc.) from these fuzzer-generated queries are still acceptable — only the regressed exception must not reappear. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
…rlap-getNonJoinedBlocks
…rlap-getNonJoinedBlocks
The check asserted `left_columns_count + required_right_keys.columns() +
sample_block_with_columns_to_add.columns() == result_sample_block.columns()`
— originally a raw sum, then a unique-name-set equality in this PR. Both
forms are too strict:
- Names can overlap across sides (e.g. after `LEFT ANTI → RIGHT ANTI`
swapping for `GLOBAL NOT IN`), and `result_sample_block` deduplicates,
so the raw sum overshoots. This was the original `STID 2980-385f`
exception.
- But names can also overlap and get a qualified rename (`a` → `t2.a`)
rather than deduplicating, so the result has more names than the
unique set. The name-set variant introduced earlier in this PR broke
`02497_storage_join_right_assert`, which exercises exactly this case:
result is `[key, a, t2.a]` while left ∪ right-keys ∪ columns-to-add =
`{key, a}`.
There is no single arithmetic or set-level relationship that holds in
every case. The real mapping invariant lives in the `NotJoinedBlocks`
constructor, which already checks
`column_indices_left.size() + column_indices_right.size() +
same_result_keys.size() == result_sample_block.columns()` after
actually building the index tables. That check is both necessary and
sufficient for downstream code.
Drop the outer check entirely, leaving the `NotJoinedBlocks` check as
the sole guard. Verified `04057_global_not_in_join_swap_column_overlap`,
`02497_storage_join_right_assert`, `00370_duplicate_columns_in_subqueries`,
`03993_join_no_overlapping_columns_85622`, and
`04050_join_merge_table_duplicate_columns` all pass.
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
…rlap-getNonJoinedBlocks
…rlap-getNonJoinedBlocks
…rlap-getNonJoinedBlocks
…rlap-getNonJoinedBlocks
…rlap-getNonJoinedBlocks
…rlap-getNonJoinedBlocks
…rlap-getNonJoinedBlocks
…rlap-getNonJoinedBlocks
…rlap-getNonJoinedBlocks
…rlap-getNonJoinedBlocks
…rlap-getNonJoinedBlocks
…rlap-getNonJoinedBlocks
…rlap-getNonJoinedBlocks
The grep in `04057_global_not_in_join_swap_column_overlap.sh` matched any `LOGICAL_ERROR`, which contradicts the test's stated intent of guarding only against the original `Unexpected number of columns in result sample block` regression. Fuzzer-style reproducers can trigger unrelated logical errors (e.g. assertions in other components), so the broad pattern made the test non-deterministic. Drop `LOGICAL_ERROR` from the alternation and keep only the message-specific patterns. The combined regex `'Unexpected (columns|number of columns) in result sample block'` covers both historical wordings. Addresses PR #100617 review feedback.
…rlap-getNonJoinedBlocks
…rlap-getNonJoinedBlocks
…rlap-getNonJoinedBlocks
…rlap-getNonJoinedBlocks
…rlap-getNonJoinedBlocks
Aligns `.github/workflows/vectorsearchstress.yml` with the praktika source (`runs-on: arm-large-storage`) so the `Check Workflows` job stops failing, and restores the rerun-skip block in `create_release.yml`. These files were previously held at the prior PR tip as a workaround for `gh` CLI tokens lacking the `workflow` scope.
…rlap-getNonJoinedBlocks
Reverts the master merge's `retry_infra_failures.yml` change so the push does not require the `workflow` token scope. The file is byte-identical to the previous PR tip (`d738e2ce1d6`).
…wap_column_overlap` Add `DROP TABLE IF EXISTS temp` before the second reproducer's `CREATE TABLE temp ...` so the test stays deterministic across local re-runs in the same server. Without this, a previous failed run that did not reach the trailing cleanup would make the next run fail early on `Table default.temp already exists`, masking the real regression signal.
…rlap-getNonJoinedBlocks
LLVM Coverage Report
Changed lines: 100.00% (5/5) · Uncovered code |
|
As we can't reproduce it - closing. |
The grep in `04057_global_not_in_join_swap_column_overlap.sh` matched any `LOGICAL_ERROR`, which contradicts the test's stated intent of guarding only against the original `Unexpected number of columns in result sample block` regression. Fuzzer-style reproducers can trigger unrelated logical errors (e.g. assertions in other components), so the broad pattern made the test non-deterministic. Drop `LOGICAL_ERROR` from the alternation and keep only the message-specific patterns. The combined regex `'Unexpected (columns|number of columns) in result sample block'` covers both historical wordings. Addresses PR ClickHouse#100617 review feedback.
The check
left_columns_count + required_right_keys.columns() + sample_block_with_columns_to_add.columns() == result_sample_block.columns()inHashJoin::getNonJoinedBlocksis too strict and triggers aLOGICAL_ERRORexception in valid scenarios:LEFT ANTI→RIGHT ANTIforGLOBAL NOT IN), a column name can appear in both the left block andsample_block_with_columns_to_addorrequired_right_keys. Theresult_sample_blockdeduplicates such overlaps, so the raw sum overshoots — this is the originalSTID 2980-385fexception.a→t2.a) rather than deduplicating, so the result has more names than the unique set. A name-set variant of the check would break tests that exercise this case (e.g.,02497_storage_join_right_assert, where the result is[key, a, t2.a]while left ∪ right-keys ∪ columns-to-add ={key, a}).There is no single arithmetic or set-level relationship that holds in every case. The real mapping invariant lives in the
NotJoinedBlocksconstructor, which already checkscolumn_indices_left.size() + column_indices_right.size() + same_result_keys.size() == result_sample_block.columns()after actually building the index tables. That check is both necessary and sufficient for downstream code.Drop the outer check entirely, leaving the
NotJoinedBlockscheck as the sole guard.Closes #100422
CI reports:
Notes on the current CI failures (none caused by this PR):
Bugfix validation (functional tests)reports "Failed to reproduce the bug" for04057_global_not_in_join_swap_column_overlapbecause the AST-fuzzer-generated reproducers in the test do not deterministically trigger the originalSTID 2980-385fexception on the inverted (master)arm_releasebuild. The test is a smoke check that the queries run cleanly on the fixed code path — the bug it guards against isSTID 2980-385f, originally observed only onamd_debug.Stateless tests (amd_debug, parallel)reports02270_errors_in_files_s3"result differs with reference": unrelated, this is a known flaky test tracked in Flaky test: 02270_errors_in_files_s3 #104116. Theurl('http://localhost:11111/test/{a,tsv_with_header}.tsv')query intermittently fails to surface the URL of the missing-file error.Upgrade check (amd_release)reportsNo stream (ba1.bin) file checksum for column bfortest_3.wrong_metadata(Code: 226): unrelated, this PR does not touch part loading or column rename metadata. The narrow upgrade-check allowlist for this signature is being widened in Allowlist02555_davengers_rename_chainupgrade-check errors #104037.Upgrade check (amd_release)reportedTuple element name 'null' is reserved ... Cannot attach table ... t_tuple_null ... (BAD_ARGUMENTS): unrelated, addressed in AllowlistTuple element name 'null' is reservedin upgrade check #103917.Unit tests (asan_ubsan, function_prop_fuzzer): unrelated UBSan inparseDateTimeBestEffort.cpp:79—parseDateTimeBestEffortis not touched by this PR.Stress test (arm_release)Logical error: Port is already connectedinLazyUnorderedReadFromMergeTreeSource::expandPipeline(STID: 0996-327d): unrelated, addressed in Fix Port is already connected in LazyUnorderedReadFromMergeTreeSource::expandPipeline #103230.AST fuzzer (amd_debug, targeted)failure (ChunkBuffer::extractNextforReadFromCommonBufferSource,STID 2651-2cfd): unrelated, addressed in Make correlated-subquery decorrelation joins ignore user join size limits #103322.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix
LOGICAL_ERRORexception "Unexpected number of columns in result sample block" inHashJoin::getNonJoinedBlocksthat could occur withGLOBAL IN/GLOBAL NOT INqueries when the join optimizer swaps sides.Documentation entry for user-facing changes