Repository navigation
Fix crash in KeyCondition with LowCardinality column in CAST key expression - #101220
alexey-milovidov merged 2 commits into
Conversation
…ession When a MergeTree table has ORDER BY CAST(lc_col, 'Type') where lc_col is LowCardinality, the monotonic function chain contains a FunctionCast built with LowCardinality argument types. In applyFunctionChainToColumn(), the input column is stripped of LowCardinality at the top (convertToFullIfNeeded), but the FunctionCast wrapper still expects a ColumnLowCardinality input (because useDefaultImplementationForLowCardinalityColumns returns false for CAST). This mismatch causes a "Bad cast from ColumnVector to ColumnLowCardinality" crash. Fix: before executing each function in the chain, check if the function expects LowCardinality input but the column is not LowCardinality. If so, wrap the column in LowCardinality to match the function's expectation. The output is already stripped of LowCardinality after execution (line 1138). Co-Authored-By: Claude Opus 4.6 <[email protected]>
Pre-PR Validation Gate (session: cron:clickhouse-ci-task-worker:20260330-131500)a) Deterministic repro? ✅ SET allow_suspicious_low_cardinality_types = 1;
CREATE TABLE t (val LowCardinality(UInt32)) ENGINE = MergeTree() ORDER BY CAST(val, 'UInt64');
INSERT INTO t SELECT number % 100 FROM numbers(1000);
SELECT count() FROM t WHERE val = 42; -- crashes 100% of the time before fixb) Root cause explained? ✅ c) Fix matches root cause? ✅ d) Test intent preserved / New test added? ✅ e) Demonstrated in both directions? ✅
|
|
cc @CurtizJ @KochetovNicolai — could you review this? It fixes a server crash (LOGICAL_ERROR) in |
|
Workflow [PR], commit [99b66ec] Summary: ✅ AI ReviewSummaryThis PR fixes an exception in ClickHouse Rules
Final Verdict
|
LLVM Coverage Report
Changed lines: 92.31% (12/13) | lost baseline coverage: 6 line(s) · Uncovered code |
|
Hi — this PR may need backporting to release branches Why: This fixes a LOGICAL_ERROR crash on a valid SQL query (CAST on LowCardinality column in ORDER BY). The affected code in applyFunctionChainToColumn has existed since LowCardinality + monotonic function chain support was added, which predates all active release branches. If this should be backported, consider adding |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
Fix server crash (LOGICAL_ERROR: Bad cast from ColumnVector to ColumnLowCardinality) when querying a MergeTree table with
ORDER BY CAST(lc_column, 'Type')wherelc_columnhas a LowCardinality type.What this PR fixes
When a MergeTree table uses
ORDER BY CAST(lc_col, 'Type')wherelc_colis aLowCardinalitycolumn, the key expression's monotonic function chain contains aFunctionCastthat was built withLowCardinalityargument types.In
applyFunctionChainToColumn(), the input constant column is stripped ofLowCardinalityat the top viaconvertToFullIfNeeded()(line 1030). However, theFunctionCastin the chain hasuseDefaultImplementationForLowCardinalityColumns() = false, meaning it handles LC internally through a wrapper that was prepared expectingColumnLowCardinalityinput. When the stripped (plainColumnVector) column is passed to this function, the wrapper'stypeid_cast<const ColumnLowCardinality &>fails, crashing the server.Root cause: The
FunctionCastwrapper capturesfrom_low_cardinalityat preparation time (when types include LC), but receives a non-LC column at execution time becauseapplyFunctionChainToColumnstrips LC from the input.Fix: Before executing each function in the chain, check if the function expects LowCardinality input but the column is not LowCardinality. If so, re-wrap the column in
ColumnLowCardinalityto match the function's expectation. The output is already stripped of LC after execution (existing code at line 1138).Reproduction query:
This crash was observed in CI stress tests and AST fuzzer (STID: 4054-600e) across 7 occurrences in 90 days.
Version info
26.4.1.609