Skip to content

Support WITH TIES for negative LIMIT - #100930

Merged
nihalzp merged 51 commits into
ClickHouse:masterfrom
nihalzp:negative-limit-with-ties
Jul 2, 2026
Merged

nihalzp merged 51 commits into
ClickHouse:masterfrom
nihalzp:negative-limit-with-ties

Conversation

@nihalzp

@nihalzp nihalzp commented Mar 27, 2026 •

Copy link
Copy Markdown
Member

The following missing WITH TIES for negative limit is now supported:

SELECT number % 3 AS n FROM numbers(15)
ORDER BY n LIMIT -4 OFFSET -3 WITH TIES;
┌─n─┐
│ 1 │
│ 1 │
│ 1 │
│ 1 │
│ 1 │
│ 2 │
│ 2 │
└───┘

It uses a very memory efficient algorithm so that we only ever keep chunks that we really need in memory and uses binary search to later find the cut point of the run of equal keys instead of doing linear scan.

Changelog category (leave one):

  • New Feature

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Add support for WITH TIES for negative LIMIT.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.7.1.433 (included in 26.7 and later)

@clickhouse-gh

clickhouse-gh Bot commented Mar 27, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [e170290]

Summary: ✅


AI Review

Summary

This PR adds WITH TIES support to negative LIMIT, threads the necessary sort metadata through the planner and query-plan serialization, makes tie comparison collation-aware, and fixes rows_before_limit_at_least accounting for NegativeLimitTransform and FractionalLimitTransform. Re-checking the current code against the prior review history did not surface a remaining correctness, compatibility, or performance issue that warrants a new review comment.

Missing context / blind spots
  • ⚠️ I did not run the stateless tests locally in this checkout because no build artifacts are available here; confidence comes from code inspection, the updated regression tests in the PR, and a randomized chunk-boundary simulation of the new negative-LIMIT WITH TIES algorithm.
Final Verdict

✅ No new blocking findings.

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Mar 27, 2026
@nihalzp nihalzp changed the title Support WITH TIES for Negative LIMIT Support WITH TIES for negative LIMIT Mar 28, 2026
@clickhouse-gh clickhouse-gh Bot added the pr-feature Pull request with new product feature label Jun 12, 2026
@nihalzp

nihalzp commented Jun 14, 2026

Copy link
Copy Markdown
Member Author

@groeneai Why the Darwin fast test failing?

@groeneai

Copy link
Copy Markdown
Collaborator

@nihalzp It is the macOS loopback gap, not your WITH TIES code.

The arm_darwin runner binds only 127.0.0.1 on lo0. Linux auto-routes all of 127.0.0.0/8 to loopback, macOS does not. Your three new distributed tests issue remote()/cluster() queries to 127.0.0.2/127.0.0.3, which time out on macOS:

Code: 209. DB::NetException: Timeout: connect timed out: 127.0.0.2:9000 ... (SOCKET_TIMEOUT)

-> ALL_CONNECTION_TRIES_FAILED -> clickhouse-test return code 23 -> FAIL. macOS-only; the tests themselves are correct.

Failing: 04051_negative_limit_with_ties_distributed, 04052_..._large, 04054_..._large_boundary.

Established pattern: your earlier negative-limit distributed tests are already skipped on Darwin for the same reason (03658, 03811, 03838, 03839, 04105 in ci/defs/darwin.skip). Adding these three there unblocks your PR now.

Longer term, #107435 aliases 127.0.0.2..16 on lo0 in the macOS Fast-test pre-hook so these run instead of being skipped; it is open pending review.

alexey-milovidov and others added 4 commits June 29, 2026 11:22
Address the AI Review concern about cross-version query-plan serialization
of negative `LIMIT` `WITH TIES`.

The trailing `SortDescription` payload is gated by bit 0 of the leading
`flags` byte, which has been part of the `NegativeLimitStep` wire format
since negative `LIMIT` was introduced. The byte stream is self-describing,
so a reader that understands the `flags` byte always knows whether the
trailing payload follows - there is no position from which the rest of the
plan could be decoded incorrectly.

A server that predates `WITH TIES` rejects a non-zero `flags` value with
`CORRUPTED_DATA` before reading `limit`/`offset`, so a newer initiator that
sends a `WITH TIES` negative-`LIMIT` plan to it fails closed. This is
intentionally finer-grained than bumping `DBMS_QUERY_PLAN_SERIALIZATION_VERSION`,
which would reject every serialized plan from the newer initiator, including
plain negative `LIMIT`.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>

@Michicosun Michicosun left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks great!

Comment thread src/Processors/NegativeLimitTransform.h Resolved
@clickhouse-gh

clickhouse-gh Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.40% 85.50% +0.10%
Functions 92.60% 92.60% +0.00%
Branches 77.70% 77.70% +0.00%

Changed lines: Changed C/C++ lines covered: 306/313 (97.76%) · Uncovered code

Full report · Diff report

@Michicosun Michicosun left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@nihalzp
nihalzp added this pull request to the merge queue Jul 2, 2026
Merged via the queue into ClickHouse:master with commit f5c5c61 Jul 2, 2026
347 of 348 checks passed
@nihalzp
nihalzp deleted the negative-limit-with-ties branch July 2, 2026 12:56
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 2, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…-ties

Support `WITH TIES` for negative `LIMIT`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-feature Pull request with new product feature pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants