Repository navigation
Support negative LIMIT BY - #103222
Support negative LIMIT BY#103222
LIMIT BY#103222Conversation
|
Workflow [PR], commit [6ca4c09] Summary: ✅ AI ReviewSummaryThis PR adds support for negative Findings
ClickHouse Rules
Final Verdict
|
| { | ||
| if (limit_by_limit > std::numeric_limits<UInt64>::max() - limit_by_offset) | ||
| return; | ||
| if (limit_by_offset > 0) |
There was a problem hiding this comment.
do_not_skip_offset currently folds any non-zero LIMIT BY offset into limit_by_length (limit_by_length += limit_by_offset) and then forces is_offset_negative = false. This is only semantics-preserving for positive offsets.
For negative offset, this changes query meaning on distributed first stage. Concrete example:
- input clause:
LIMIT -2 OFFSET -3 BY id - after this rewrite:
LIMIT -5 OFFSET 0 BY id
These are not equivalent: original means “drop last 3 per group, then keep last 2 of the remainder”, while rewritten means “keep last 5”.
Please gate this rewrite to the positive-offset case only (or skip first-stage LIMIT BY rewrite when offset is negative).
There was a problem hiding this comment.
This is also semantic preserving.
There was a problem hiding this comment.
I re-checked the current code and this is still not resolved.
In addLimitByStep, the do_not_skip_offset rewrite still runs for any non-zero limit_by_offset and unconditionally forces is_offset_negative = false:
if (do_not_skip_offset)
{
if (limit_by_offset > 0)
{
...
limit_by_length += limit_by_offset;
limit_by_offset = 0;
is_offset_negative = false;
}
}For LIMIT -2 OFFSET -3 BY id this still rewrites to NegativeLimitByStep(limit=5, offset=0), which is not equivalent (it keeps 5 tail rows instead of dropping 3 then keeping 2).
Please gate this rewrite to !is_offset_negative (positive offset only), and keep negative-offset forms on the negative-offset execution path.
| } | ||
| } | ||
|
|
||
| AggregatedDataVariants::Type AggregatedDataVariants::chooseMethod( |
There was a problem hiding this comment.
Ok - this PR contains a few small, unrelated changes from Aggregator refactoring. This is alright.
There was a problem hiding this comment.
The refactoring allows negative LIMIT BY to use the same hash tables as the aggregator; allowing LIMIT BY to benefit from years of optimizations in AggregatedDataVariants. Same change is also present in the LIMIT BY optimization PR #103349.
| /// https://github.com/ClickHouse/ClickHouse/blob/67c1e89d90ef576e62f8b1c68269742a3c6f9b1e/src/Interpreters/InterpreterSelectQuery.cpp#L1697-L1705 | ||
| /// Let's be optimistic and only don't skip offset (it will be skipped on the initiator). | ||
| addLimitByStep(query_plan, limit_by_analysis_result, query_node, true /*do_not_skip_offset*/); | ||
| addLimitByStep(query_plan, limit_by_analysis_result, query_analysis_result, true /*do_not_skip_offset*/); |
There was a problem hiding this comment.
addLimitByStep(..., do_not_skip_offset = true) is now used for negative LIMIT BY forms on the shard stage too, but negative offsets are not safe to pre-apply per shard.
For LIMIT N OFFSET -M BY key, this lowers to NegativeLimitByStep(MAX, M) + LimitByStep(N, 0). Running that on each shard drops each shard-local tail, not the global tail, so rows can be lost before the initiator sees them.
Concrete example for one group ordered by v:
- shard A:
[1, 100] - shard B:
[2, 3] - query:
LIMIT 10 OFFSET -1 BY g - correct global result:
[1, 2, 3](drop only100) - with shard-preliminary step: A ->
[1], B ->[2], initiator can no longer recover3.
Please disable preliminary LIMIT BY for cases with negative offset (or otherwise keep it semantically lossless for distributed first stage).
There was a problem hiding this comment.
When do_not_skip_offset = true, offset is set to 0 and limit becomes limit + offset. This means offset is never applied on shard. We also have tests that exercise the exact path.
There was a problem hiding this comment.
This concern is still live in current code.
addPreliminarySortOrDistinctOrLimitStepsIfNeeded still always calls:
addLimitByStep(..., true /*do_not_skip_offset*/);for first-stage distributed planning, including negative-offset forms. Combined with the current do_not_skip_offset rewrite in addLimitByStep, this pre-applies LIMIT BY semantics per shard for negative-offset cases, which is not globally lossless.
Please disable shard-stage LIMIT BY pre-application for negative-offset forms (or otherwise ensure semantic equivalence before initiator merge).
LLVM Coverage Report
Changed lines: 95.95% (521/543) | lost baseline coverage: 8 line(s) · Uncovered code |
|
@groeneai Investigate why the performance test CI failed. |
|
@nihalzp — investigated. It's an infrastructure flake on the Root causeAttempt 1, shard S3 ( This is independent of your changes — the same failure mode hit PR #104523 ( Re-run is cleanAttempt 2's shard 1/6 (job
PR-level summary across all 5 completed arm shards: The 4 "slower" entries are all 0.3–0.6 percentage points above their own noise thresholds and live in tests ( Suggested actionNothing to do on your end for the perf failure — the re-run cleared the shard. If you want a defense-in-depth fix on the CI side, raising the retry count for the Unrelated note while you're hereThe same attempt-1 job log contains the AI — @groeneai, session |
896e7f7
Given,
We can now do:
The implementation is highly optimized; in many cases, this transform is much faster than
LIMIT BYwith positive values.Negative
LIMIT BY:Elapsed: 0.078 sec.
LIMIT BY:Elapsed: 0.233 sec.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Support negative values in the
LIMIT BYclause to select rows from the end of each group instead of the beginning. For example,LIMIT -2 BY idreturns the last two rows perid. Negative offsets (LIMIT -1 OFFSET -1 BY id) and mixed signs (LIMIT -2 OFFSET 1 BY id) are supported as well.Documentation entry for user-facing changes
Version info
26.5.1.506