promql: add limitk aggregation operator (#88) - #442
Conversation
|
Added a draft PR here for review purposes + testing blocking on GreptimeTeam/promql-parser#142 for a new release so we use an official version. |
b9dc872 to
7a19b1a
Compare
v0.9.0 adds grammar rules for limitk and limit_ratio in the aggregate_op production. v0.8.0 declared these as `%expect-unused` tokens but never bound them in the grammar, so `limitk(...)` queries failed at parse time before this bump. The bump also widens the token id type from u8 to u16. Updated two local helper signatures (aggregate_op_name, binary_op_kind) to match. No behavior change.
There was a problem hiding this comment.
Thanks for the PR, @petrpan26 !
I reviewed the production code and had a couple of comments.
I am aware the some comments are not directly related to your code.
| continue; | ||
| } | ||
| // KOrder is unused — limitk never evicts. | ||
| heap.push(KHeapEntry { |
There was a problem hiding this comment.
Why do we need to first push the series samples to a heap and then from the heap to out? Why can we not set the samples directly in out?
There was a problem hiding this comment.
Hi I think I got a very bad case of dumb ai coding in this PR. Upstreams code were reusing the same allocated heap for topk and bottomk and limitk. And i think i blindly follow it. I think our code structure is completely different.
There was a problem hiding this comment.
Leave this here so you can actually review the new logic
This PR adds first-class evaluator support for the limitk aggregation. It hooks AggregateOp::Limitk into the aggregation evaluation path with Prometheus-compatible grouping and filter-shape output. - implement limitk via AggregateKind::Limitk(i64), reusing the topk/bottomk per-group heap as a fixed-capacity push-only buffer (no eviction) - walk inputs in fingerprint-sorted order so selection is stable per query (Blake3 fingerprint already on SeriesSchema; diverges from upstream's xxhash) - extend lower_aggregate's shared K-param block to T_TOPK | T_BOTTOMK | T_LIMITK - extend physical-plan filter-shape to include Limitk - add limitk-focused operator tests covering by/without grouping, k=0/-1/k>n, mixed validity, multi-step independent selection, fingerprint-sort correctness, and out-of-order tile arrival - add limitk parity fixtures in aggregators.test (cardinality assertions; full label assertions where output is fully determined)
|
@cadonna Thanks for the review I think I was too enthusiastic when upstream deps was merged and forgot to do an actual code sweep. |
|
Hi @petrpan26 . This looks good. I think the only thing that's missing is to add a memory reservation, like the TBH, I'm not exactly sure how this memory limits work, but it's better to be consistent here IMO. |
I agree that a test is needed for the memory reservation. However, as limitk does not use a heap, it should only test for the reservation of the grid (line 670 in
@petrpan26 if you have cycles, you are welcome to fix that in a separate PR. |
cadonna
left a comment
There was a problem hiding this comment.
Thanks for the updates @petrpan26 !
The algorithm looks much better now.
I have one minor comment for the algorithm and two comments for tests.
| let grid_base = global_step * in_series_count; | ||
| let out_base = global_step * in_series_count; |
There was a problem hiding this comment.
grid_base and out_base are always equal, right?
Same question about out_idx and cell.
If yes, you can just use grid_base and cell.
| http_requests{group="canary", instance="0", job="app-server"} 700 | ||
| http_requests{group="production", instance="1", job="app-server"} 600 | ||
|
|
||
| # Tests for limitk. |
There was a problem hiding this comment.
For these tests did you also consider the tests here: https://github.com/prometheus/prometheus/blob/main/promql/promqltest/testdata/limit.test ?
|
@petrpan26 do you plan to update this PR? |
|
@cadonna im afk this week. Do you mind if i take this on this weekend. If this block anything pls feel free to merge it |
|
@petrpan26 It is not blocking anything. I just wanted to understand if the PR is still on your radar. Great that you plan to work on the PR! |
|
Looking into this tomorrow ! |
Summary
Adds evaluator support for
limitkwith Prometheus-compatible grouping and filter-shape output.aggregators.testRelated Issues
Fixes #88
Test Plan
cargo test -p opendata-timeseries --libcargo clippy -p opendata-timeseries --lib --all-targets -- -D warningsChecklist
cargo fmtandcargo clippypass