Repository navigation
Fix topK dynamic filtering crash with Dynamic/Variant columns - #100742
Conversation
When use_top_k_dynamic_filtering is enabled and the ORDER BY column has Dynamic or Variant type, the __topKFilter function's vectorized path calls lessOrEquals which returns Nullable(UInt8) instead of UInt8, triggering a "Unexpected return type" logical error (STID 1611-483a). Fix: skip the dynamic-filtering prewhere optimization for Dynamic and Variant sort columns in the query plan optimizer, and route these types to the general comparison path in FunctionTopKFilter as defense-in-depth. Co-Authored-By: Claude Opus 4.6 <[email protected]>
Pre-PR Validation Gate (session: cron:clickhouse-ci-task-worker:20260325-182556)a) Deterministic repro? b) Root cause explained? c) Fix matches root cause? d) Test intent preserved? e) Demonstrated in both directions?
|
|
Workflow [PR], commit [163f6c6] Summary: ✅ AI ReviewSummaryThis PR fixes a real Missing context
ClickHouse Rules
Final Verdict
|
- Make ORDER BY deterministic (add payload tiebreaker) to fix flaky output when multiple rows share the same sort key value - Add Variant(Int64, String) test case alongside the existing Dynamic test to cover both code paths in the optimization skip - Fix typo: "a Unexpected" → "an Unexpected" Co-Authored-By: Claude Opus 4.6 <[email protected]>
LLVM Coverage Report
PR changed lines: PR changed-lines coverage: 100.00% (11/11, 0 noise lines excluded) |
cac197c
…ains a Nullable FunctionTopKFilter::getReturnTypeImpl declares UInt8 unconditionally, but executeVectorized returns whatever the lessOrEquals/greaterOrEquals it delegates to resolves to. FunctionComparison::getReturnTypeImpl propagates Nullable transitively out of tuple elements, so for a sort column that contains a Nullable without being Nullable the fast path produces Nullable(UInt8) and the columnMatchesType guard in ExpressionActions throws LOGICAL_ERROR. The guard meant to route such columns to the safe path tested only top level nullability, and DataTypeTuple does not override IDataType::isNullable(). Test the resolved comparison type where it is already computed and fall back to executeGeneral, which builds a ColumnUInt8 directly and cannot diverge. Keying on the resolved type rather than on the argument's structure is deliberate: a structural predicate such as hasNullable is also true for Array(Nullable(T)) and Map(K, Nullable(V)), whose comparisons resolve to plain UInt8 and work correctly today, so it would silently disable use_top_k_dynamic_filtering for them. executeGeneral also applies nulls_direction, which the lessOrEquals fast path did not, so the general path is both safe and more correct for these types. It is row at a time, but it now runs only for arguments that throw today, and Nullable and collated columns already took it unconditionally. Found by the AST fuzzer on an unrelated PR, STID 1611-483a, reachable from ordinary DDL with no opt-in setting since use_top_k_dynamic_filtering defaults to true. ClickHouse#100742 was an incomplete earlier fix for the same STID: it enumerated the two reported types, Dynamic and Variant, instead of keying on the causal property, so the Tuple carrier remained. Its regression test is extended here with the Tuple, Tuple-of-LowCardinality(Nullable) and NULL bearing arms. The LowCardinality arm pins max_threads = 1 because without it the threshold is not always set before the remaining parts are read, and the arm only reaches the filter in about seven runs out of ten.
Changelog category (pick one):
Changelog entry (a user-readable short description of the changes):
Fix server crash (logical error "Unexpected return type from __topKFilter") when
use_top_k_dynamic_filteringis enabled and theORDER BYcolumn hasDynamicorVarianttype.Summary
When
use_top_k_dynamic_filtering = 1is used with anORDER BYon aDynamic(max_types=...)column, the__topKFilterinternal function callslessOrEqualswhich returnsNullable(UInt8)instead of the expectedUInt8, causing:This is AST fuzzer finding STID 1611-483a, recurring across multiple unrelated PRs.
Root cause
DynamicandVarianttypes wrap their inner values inNullable. When the vectorized comparison path inFunctionTopKFiltercallslessOrEqualson aDynamiccolumn, the result isNullable(UInt8)— but the query plan expectsUInt8from__topKFilter, producing a logical error.Fix
Two layers of defense:
Query plan optimizer (
optimizeTopK.cpp): Skip the dynamic-filtering prewhere optimization entirely when the sort column isDynamicorVariant. These types don't benefit from the optimization anyway since their comparison semantics are complex.Runtime guard (
FunctionTopKFilter.cpp): RouteDynamic/Variantarguments to the general comparison path (which returns a cleanColumnUInt8) instead of the vectorized path that delegates tolessOrEquals.Test plan
04058_topk_dynamic_filtering_with_dynamic_typeVersion info
26.4.1.286