Repository navigation
Fix Bad cast in KeyCondition for Variant constant with LowCardinality member - #107111
Conversation
… member A constant whose type is a Variant containing a LowCardinality member, compared to a key column with a non-monotonic deterministic key expression (e.g. a minmax skip index on sipHash64(col)), aborted the server with 'Bad cast from type DB::ColumnString to DB::ColumnLowCardinality' in KeyCondition::applyDeterministicDagToColumn. The function materialized the constant column with convertToFullIfNeeded(), which recurses into ColumnVariant members and strips their inner LowCardinality, while the type was stripped with recursiveRemoveLowCardinality which keeps LowCardinality inside Variant (it has no Variant branch). The resulting column member was a ColumnString while its type still said LowCardinality(String), so the Variant->String cast wrapper performed a typeid_cast to ColumnLowCardinality on a ColumnString. Strip only outer-level wrappers (Const/Replicated/Sparse/LowCardinality) and use removeLowCardinality on the type, keeping column and type symmetric. This mirrors the existing fix in applyFunctionChainToColumn. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate
Session id: cron:clickhouse-worker-slot-29:20260610-225430 |
|
cc @nihalzp @CurtizJ — could you review this? It applies the same outer-only LowCardinality stripping you reviewed in #103211 (applyFunctionChainToColumn) to the sibling applyDeterministicDagToColumn path. A |
|
Workflow [PR], commit [67f14fe] Summary: ❌
AI ReviewSummaryThis PR fixes a PR Metadata
Missing context / blind spots
Final VerdictStatus: ✅ Approve |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 14/14 (100.00%) | Lost baseline coverage: none · Uncovered code |
|
Why not do it the other way around by striping |
|
Good question. I went this way on purpose, because stripping
So the fix keeps column and type symmetric by preserving inner LC on both. The actual bug was that a |
|
CI status for 67f14fe: all required jobs green and the AI review approved. The single red check, |
abf970f
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix
LOGICAL ERROR(Bad cast from type DB::ColumnString to DB::ColumnLowCardinality) when aVariantconstant containing aLowCardinalitymember is compared to a key column whose key expression is a non-monotonic deterministic function (for example aminmaxskip index oversipHash64(col)).Description
Found by the server-side AST fuzzer on master (
Stress test (experimental, serverfuzz, amd_tsan), STID 4054-600e).Report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=02e4401eea6456ba042a6da9d32e7c2a75e85685&name_0=MasterCI&name_1=Stress%20test%20%28experimental%2C%20serverfuzz%2C%20amd_tsan%29
Fuzzed query:
Minimal reproducer (a plain
SELECT ... WHEREis enough; the table function is not required):Root cause:
KeyCondition::applyDeterministicDagToColumnmaterialized the constant column withconvertToFullIfNeeded(), which recurses intoColumnVariantmembers and strips their innerLowCardinality, while the type was stripped withrecursiveRemoveLowCardinality, which keepsLowCardinalityinside aVariant(it has noVariantbranch). The constant column member therefore became aColumnStringwhile its type still saidLowCardinality(String), so theVariant -> Stringcast wrapper performed atypeid_casttoColumnLowCardinalityon aColumnStringand aborted.Fix: strip only outer-level wrappers (
Const/Replicated/Sparse/LowCardinality) from the column and useremoveLowCardinalityon the type, so column and type stay symmetric (both keepLowCardinalityinside containers such asVariant). This mirrors the existing fix in the sibling functionapplyFunctionChainToColumn(#103211), which handled the monotonic-function path; the same problem remained in the deterministic-function path that this fuzzer sighting reached.A new stateless regression test
04329_key_condition_variant_low_cardinalityis added. It crashes on master HEAD and passes with this fix.Version info
26.6.1.632