Skip to content

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

Merged
robot-clickhouse-ci-1 merged 3 commits into
backport/26.3/125221from
cherrypick/26.3/125221
Oct 16, 2026
Merged

robot-clickhouse-ci-1 merged 3 commits into
backport/26.3/125221from
cherrypick/26.3/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.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]>
@groeneai

Copy link
Copy Markdown
Collaborator

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

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

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

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 ITopKThresholdTracker split), has no backport label, so the automatic retry cannot clear it. The resolution keeps the 26.3 header-only TopKThresholdTracker and its single-path FunctionTopKFilter: the tracker gets the sort column type, optimizeTopK.cpp passes sort_column.type where it creates the tracker, and FunctionTopKFilter passes that type, without LowCardinality, as the source type of the threshold conversion, as on master.

use_top_k_dynamic_filtering is off by default on 26.3 and its test runner does not randomize it, so the test sets it explicitly. It also drops the Array and GROUP BY queries, which use settings 26.3 does not have.

Validation on this tree (debug build, clang-21): the test passes 50 of 50 randomized runs and fails with Code: 53 when the src/ change is reverted. The official 26.3.52.8 binary fails the query from #125197 when use_top_k_dynamic_filtering = 1.

@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 Thanks @groeneai. I reviewed the resolution (328cb41d172d, a fast-forward from the cherry-pick head 16da7cdb): compared with backport/26.3/125221 it changes only FunctionTopKFilter.cpp, TopKThresholdTracker.h, optimizeTopK.cpp and the new test. optimizeTopK.cpp is the only place that constructs the tracker, and convertFieldToType on 26.3 already takes the from_type_hint argument. I pushed it to cherrypick/26.3/125221.

@robot-clickhouse-ci-1
robot-clickhouse-ci-1 merged commit 2fc04d9 into backport/26.3/125221 Oct 16, 2026
1 check passed
@robot-clickhouse-ci-1
robot-clickhouse-ci-1 deleted the cherrypick/26.3/125221 branch October 16, 2026 08:12
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