Repository navigation
Fix ORDER BY ... LIMIT over an Interval column with top-K dynamic filtering - #125221
Conversation
… filtering `SELECT d FROM t ORDER BY d DESC LIMIT n` over a column of an `Interval` type (or `Nullable`, `LowCardinality`, `Tuple` or `Array` of one) failed with `TYPE_MISMATCH` once the top-K threshold was set: "Type mismatch in IN or VALUES section. Expected: IntervalNanosecond. Got: Int64: while executing 'FUNCTION __topKFilter(d)'". It is reachable with default settings since 26.5, where `use_top_k_dynamic_filtering` became enabled by default. The threshold is published as a `Field` taken from a column of the sort column's type. A `Field` has no `Interval` kind, so an `IntervalX` value is a plain `Int64`. `__topKFilter` then converted it with `convertFieldToType(threshold, column_type)` and no source type, i.e. as an untyped literal, and `convertFieldToType` rejects `Int64 -> Interval` (the same error as `toIntervalSecond(1) IN (1, 2)`). The threshold tracker now keeps the type of the sort column it is created for, and `__topKFilter` passes that type, without `LowCardinality` (which the function's default implementation removes from its argument), as `from_type_hint`. For a column of that type the conversion returns the value unchanged, for every type; for other types it already returned the same value. Teaching `convertFieldToType` an `Int64 -> Interval` conversion would change `IN` / `VALUES` semantics for every caller, and excluding `Interval` types from the optimization would disable it for exactly the reported workload (`ORDER BY duration DESC` on tracing data). The test covers all eleven `Interval` units in both directions, `Nullable` (general comparison path, both `NULLS` orders), `Tuple(IntervalHour, IntervalMinute)`, `Array(IntervalSecond)`, `GROUP BY ... ORDER BY ... LIMIT`, an `Interval` primary key, a `minmax` skip index, the `Memory` engine and `LowCardinality` (also of `Nullable`). Each of its queries fails with `TYPE_MISMATCH` without the fix. Closes ClickHouse#125197 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review: my own cold review plus an independent model (engine: codex; 2 findings over 2 rounds).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20261011-162200 |
|
Workflow [PR], commit [cf976d1] Summary: ✅
AI ReviewSummaryI reviewed the current diff and prior PR discussion. The change keeps the sort-column type in Final Verdict✅ Looks good to merge. |
|
I'd also like to request this be backported to stable and LTS versions since the pre-PR behavior breaks queries |
|
Measured with the repro from #125197 on the branches that still get backports: the query fails on 26.7.6.57, 26.8.21.12 and 26.9.7.6 with default settings, and on 26.3.33.24 when |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units175 translation units recompiled, 1762 s compile time in total, 175 of them have a recent master baseline. |
alexey-milovidov
left a comment
There was a problem hiding this comment.
This is straightforward. Thanks!
|
@EmeraldShift, will do. |
Resolve the conflicts of the 26.9 cherry-pick of #125221 ("Fix `ORDER BY ... LIMIT` over an `Interval` column with top-K dynamic filtering"). The conflicts come from #116512, which on master split `TopKThresholdTracker` into `ITopKThresholdTracker` with the lock-free `TopKThresholdTrackerNumeric` and `TopKThresholdTrackerGeneric`, created by `createTopKThresholdTracker`. 26.9 has the single `TopKThresholdTracker` struct, and #116512 is not marked for backport, so the resolution keeps the 26.9 tracker and applies only what #125221 changes: - `TopKThresholdTracker` takes and keeps the type of the sort column (`getDataType`), as `ITopKThresholdTracker` does on master. - `optimizeTopK.cpp` passes `sort_column.type` where it creates the tracker. On master this is done inside `createTopKThresholdTracker`, which 26.9 does not have. - `FunctionTopKFilter.cpp` merged without conflicts (the hunks of #125221 verbatim). - `TopKThresholdTracker.cpp` keeps the 26.9 version: its conflicting hunks are #116512's. - The test drops its `GROUP BY` query and that query's three reference lines, because it uses `enable_group_by_top_k_dynamic_filtering`, which 26.9 does not have. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Cherry pick #125221 to 26.9: Fix `ORDER BY ... LIMIT` over an `Interval` column with top-K dynamic filtering
… column with top-K dynamic filtering
Resolve the conflicts of the 26.8 cherry-pick of #125221 ("Fix `ORDER BY ... LIMIT` over an `Interval` column with top-K dynamic filtering"). The conflicts come from #116512, which on master split `TopKThresholdTracker` into `ITopKThresholdTracker` with the lock-free `TopKThresholdTrackerNumeric` and `TopKThresholdTrackerGeneric`, created by `createTopKThresholdTracker`. 26.8 has the single `TopKThresholdTracker` struct, and #116512 is not marked for backport, so the resolution keeps the 26.8 tracker and applies only what #125221 changes: - `TopKThresholdTracker` takes and keeps the type of the sort column (`getDataType`), as `ITopKThresholdTracker` does on master. - `optimizeTopK.cpp` passes `sort_column.type` where it creates the tracker. On master this is done inside `createTopKThresholdTracker`, which 26.8 does not have. - `FunctionTopKFilter.cpp` merged without conflicts (the hunks of #125221 verbatim). - `TopKThresholdTracker.cpp` keeps the 26.8 version: its conflicting hunks are #116512's. - The test drops its `GROUP BY` query and that query's three reference lines, because it uses `enable_group_by_top_k_dynamic_filtering`, which 26.8 does not have. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Cherry pick #125221 to 26.8: Fix `ORDER BY ... LIMIT` over an `Interval` column with top-K dynamic filtering
… column with top-K dynamic filtering
Resolve the conflicts of the 26.7 cherry-pick of #125221 ("Fix `ORDER BY ... LIMIT` over an `Interval` column with top-K dynamic filtering"). The conflicts come from #116512, which on master split `TopKThresholdTracker` into `ITopKThresholdTracker` with the lock-free `TopKThresholdTrackerNumeric` and `TopKThresholdTrackerGeneric`, created by `createTopKThresholdTracker`. 26.7 has the single `TopKThresholdTracker` struct, and #116512 is not marked for backport, so the resolution keeps the 26.7 tracker and applies only what #125221 changes: - `TopKThresholdTracker` takes and keeps the type of the sort column (`getDataType`), as `ITopKThresholdTracker` does on master. - `optimizeTopK.cpp` passes `sort_column.type` where it creates the tracker. On master this is done inside `createTopKThresholdTracker`, which 26.7 does not have. - `FunctionTopKFilter.cpp` merged without conflicts (the hunks of #125221 verbatim). - `TopKThresholdTracker.cpp` keeps the 26.7 version: its conflicting hunks are #116512's. - The test drops its `GROUP BY` query and that query's three reference lines, because it uses `enable_group_by_top_k_optimization` and `enable_group_by_top_k_dynamic_filtering`, which 26.7 does not have. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Cherry pick #125221 to 26.7: Fix `ORDER BY ... LIMIT` over an `Interval` column with top-K dynamic filtering
… column with top-K dynamic filtering
Resolve the conflicts of the 26.3 cherry-pick of #125221 ("Fix `ORDER BY ... LIMIT` over an `Interval` column with top-K dynamic filtering"). The conflicts come from master-only changes to the top-K filter: #116512 split `TopKThresholdTracker` into `ITopKThresholdTracker` with lock-free implementations in a new `TopKThresholdTracker.cpp`, and later PRs rewrote `FunctionTopKFilter`. 26.3 has the original header-only `TopKThresholdTracker` constructed from the sort direction, and a `FunctionTopKFilter` with a single comparison path. The resolution keeps both and applies only what #125221 changes: - `TopKThresholdTracker` takes and keeps the type of the sort column (`getDataType`). - `optimizeTopK.cpp` passes `sort_column.type` where it creates the tracker. On master this is done inside `createTopKThresholdTracker`, which 26.3 does not have. - `FunctionTopKFilter` gets the threshold type from the tracker, without `LowCardinality`, and passes it as the source type of the threshold conversion, as on master. - `TopKThresholdTracker.cpp` stays absent, as it is on 26.3. - The test enables `use_top_k_dynamic_filtering`, which is off by default on 26.3 and not randomized by its test runner, and drops two queries with their reference lines: the `Array` one uses `use_top_k_dynamic_filtering_for_variable_length_types` and the `GROUP BY` one uses `enable_group_by_top_k_*`, none of which 26.3 has. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Cherry pick #125221 to 26.3: Fix `ORDER BY ... LIMIT` over an `Interval` column with top-K dynamic filtering
… column with top-K dynamic filtering
Backport #125221 to 26.9: Fix `ORDER BY ... LIMIT` over an `Interval` column with top-K dynamic filtering
Backport #125221 to 26.8: Fix `ORDER BY ... LIMIT` over an `Interval` column with top-K dynamic filtering
Backport #125221 to 26.7: Fix `ORDER BY ... LIMIT` over an `Interval` column with top-K dynamic filtering
Backport #125221 to 26.3: Fix `ORDER BY ... LIMIT` over an `Interval` column with top-K dynamic filtering
Closes: #125197
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
ORDER BY ... LIMITover a column of anIntervaltype (orNullable,LowCardinality,TupleorArrayof one) failing withTYPE_MISMATCHwhenuse_top_k_dynamic_filteringis enabled, which is the default. Closes #125197.Description
On 26.8.18.2,
SELECT d FROM t ORDER BY d DESC LIMIT 3over anIntervalNanosecondcolumn of aMergeTreetable fails with default settings:Code: 53 ... Type mismatch in IN or VALUES section. Expected: IntervalNanosecond. Got: Int64: while executing 'FUNCTION __topKFilter(d : 0)'. It affects every release since 26.5, whereuse_top_k_dynamic_filteringbecame enabled by default.Root cause: the top-K threshold is published as a
Field, and aFieldhas noIntervalkind, so anIntervalXvalue is a plainInt64.__topKFilterthen calledconvertFieldToType(threshold, column_type)without a source type, which treats the value as an untyped literal, andconvertFieldToTyperejectsInt64 -> Interval(as intoIntervalSecond(1) IN (1, 2)).Change: the threshold tracker keeps the type of the sort column it is created for, and
__topKFilterpasses that type, withoutLowCardinality(which the function's default implementation removes from its argument), as the source type of the conversion. For the column of that same type the conversion is now the identity, for every type.Validation: the new test covers all eleven
Intervalunits in both directions,Nullable(bothNULLSorders),Tuple(IntervalHour, IntervalMinute),Array(IntervalSecond),GROUP BY ... ORDER BY ... LIMIT, anIntervalprimary key, aminmaxskip index, theMemoryengine andLowCardinality(also ofNullable). Each of its 42 queries fails withCode: 53on unfixed master. The test fails 50/50 randomized runs on master and passes 50/50 with the fix. Results equal those withuse_top_k_dynamic_filtering = 0. The 166 existing top-K tests show no new failure (those that fail locally need S3 or cluster config and fail the same way on master).🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/125221]
Version info
26.10.1.2434-master(included in26.10and later)26.9.20.23,26.8.29.10,26.7.34.3,26.3.56.3