Repository navigation
Cherry pick #125221 to 26.9: Fix ORDER BY ... LIMIT over an Interval column with top-K dynamic filtering - #125926
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.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]>
|
I resolved the conflict and pushed the result to my fork, because I cannot push to The head is The conflict comes from #116512 (the lock-free Validation on this tree (debug build): the test passes 50 of 50 randomized runs and fails with |
|
🕵 Thanks, I reviewed the resolution (it adapts the fix to the 26.9 single-class |
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