Skip to content

Cherry pick #125221 to 26.7: Fix ORDER BY ... LIMIT over an Interval column with top-K dynamic filtering - #125924

Merged
robot-ch-test-poll2 merged 3 commits into
backport/26.7/125221from
cherrypick/26.7/125221
Oct 16, 2026
Merged

robot-ch-test-poll2 merged 3 commits into
backport/26.7/125221from
cherrypick/26.7/125221

Conversation

@robot-ch-test-poll

Copy link
Copy Markdown
Contributor

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-pick locally.
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:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

groeneai and others added 2 commits October 11, 2026 16:13
… 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
@robot-ch-test-poll robot-ch-test-poll added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default labels Oct 15, 2026
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]>
@groeneai

Copy link
Copy Markdown
Collaborator

I resolved the conflict and pushed the result to my fork, because I cannot push to cherrypick/26.7/125221:

git fetch https://github.com/groeneai/ClickHouse.git groeneai/resolve-26.7-cherrypick-125221
git push origin FETCH_HEAD:cherrypick/26.7/125221

The head is e999db0c3133, a merge of backport/26.7/125221 into the cherry-pick head 16da7cdb, so the push is a fast-forward. Compared to backport/26.7/125221 it changes only FunctionTopKFilter.cpp, TopKThresholdTracker.h, optimizeTopK.cpp and the new test.

The conflict comes from #116512 (the lock-free ITopKThresholdTracker split), which is not on 26.7 and has no backport label, so the automatic retry cannot clear it. The resolution keeps the 26.7 TopKThresholdTracker and adds the sort column type to it, passes sort_column.type at its only construction site in optimizeTopK.cpp (on master this happens inside createTopKThresholdTracker), and takes the FunctionTopKFilter.cpp hunks unchanged. The test drops its GROUP BY query, which uses enable_group_by_top_k_optimization, not present on 26.7.

Validation on this tree (debug build): the test passes 50 of 50 randomized runs and fails with Code: 53 when the src/ change is reverted. The official 26.7.31.11 binary fails the query from #125197 with default settings.

@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 Thanks @groeneai. I reviewed your resolution and pushed e999db0c3133 to cherrypick/26.7/125221 as a fast-forward. The diff against backport/26.7/125221 is limited to FunctionTopKFilter.cpp, TopKThresholdTracker.h, optimizeTopK.cpp and the new test. sort_column.type is the only construction site, and both changed translation units pass a syntax-only compile against the 26.7 tree.

@robot-ch-test-poll2
robot-ch-test-poll2 merged commit 76cdbf1 into backport/26.7/125221 Oct 16, 2026
1 check passed
@robot-ch-test-poll2
robot-ch-test-poll2 deleted the cherrypick/26.7/125221 branch October 16, 2026 08:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants