Repository navigation
Cherry pick #125221 to 26.3: Fix ORDER BY ... LIMIT over an Interval column with top-K dynamic filtering - #125923
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 #125197 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…reshold-type Fix `ORDER BY ... LIMIT` over an `Interval` 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]>
|
I resolved the conflict and pushed the result to my fork, because I cannot push to The head is The conflict comes from master-only changes to the top-K filter that are not on 26.3. The main one, #116512 (the lock-free
Validation on this tree (debug build, clang-21): the test passes 50 of 50 randomized runs and fails with |
|
🕵 Thanks @groeneai. I reviewed the resolution ( |
Original pull-request #125221
Do not merge this PR manually
This pull-request is a first step of an automated backporting.
It contains changes similar to calling
git cherry-picklocally.If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.
The check results does not matter at this step - you can safely ignore them.
Before you resolve anything
Conflicts are often caused by a prerequisite change that has not been backported yet, rather than by a real divergence. The bot re-tries this cherry-pick against the release branch on every run, so if that is the case here it will merge itself as soon as the prerequisite lands, and you will see a comment saying so. Manual resolution is only needed while the conflict persists.
Troubleshooting
If the conflicts were resolved in a wrong way
If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:
pr-cherrypicklabel from the PRYou also need to check the Original pull-request for
pr-backports-createdlabel, and delete if it's presented thereThe PR source
The PR is created in the CI job