JackieTien97 opened a new issue, #17976: URL: https://github.com/apache/iotdb/issues/17976
### Search before asking - [x] I searched in the [issues](https://github.com/apache/iotdb/issues) and found nothing similar. ### Motivation ## Description IoTDB should support Prometheus-compatible semantics for the PromQL `rate()` and `irate()` functions on counter range vectors. The expected behavior should follow Prometheus: - `rate(v range-vector)` returns the per-second average rate of increase over the selected range. - `irate(v range-vector)` returns the per-second instant rate based on the last two samples in the selected range. - Both functions should handle counter resets correctly. - `rate()` should use all samples in the range and extrapolate to the range boundaries. - `irate()` should use only the last two samples and should not extrapolate to range boundaries. ## Expected Implementation Semantics For `rate()` on float counters: 1. Require at least two samples. 2. Compute the raw increase as `last - first`. 3. For each adjacent sample pair, detect counter reset when `current < previous`. 4. For each detected reset, add the previous value to the corrected increase. 5. If start timestamp metadata is available, also treat start timestamp reset as a counter reset. 6. Apply Prometheus-compatible boundary extrapolation: - Compute the sampled interval between the first and last samples. - Compute the average interval between samples. - If the first/last sample is close enough to the range boundary, extrapolate to the boundary. - If it is too far away, extrapolate only by half of the average sample interval. - For counters, avoid extrapolating the left boundary below zero. 7. Divide by the full range duration in seconds. For `irate()` on float counters: 1. Require at least two samples. 2. Use only the last two samples in the range. 3. If no reset is detected, return `(last - previous) / intervalSeconds`. 4. If reset is detected, return `last / intervalSeconds`. 5. Do not perform range-boundary extrapolation. ## Test Cases Please add compatibility tests for: - `rate()` without reset. - `rate()` with one counter reset. - `rate()` with multiple counter resets. - `rate()` boundary extrapolation when samples are close to the range boundaries. - `rate()` boundary extrapolation when samples are far from the range boundaries. - `rate()` counter zero-point extrapolation guard. - `irate()` without reset. - `irate()` with reset between the last two samples. - `irate()` ignoring older samples except for selecting the last two points. - Empty result when fewer than two samples are available. ## References - Prometheus `rate()` documentation: https://prometheus.io/docs/prometheus/latest/querying/functions/#rate - Prometheus `irate()` documentation: https://prometheus.io/docs/prometheus/latest/querying/functions/#irate - Prometheus `resets()` documentation: https://prometheus.io/docs/prometheus/latest/querying/functions/#resets - Prometheus `rate()` implementation: https://github.com/prometheus/prometheus/blob/main/promql/functions.go#L2991-L3249 - Prometheus `irate()` implementation: https://github.com/prometheus/prometheus/blob/main/promql/functions.go#L3521-L3758 - Prometheus start timestamp reset detection: https://github.com/prometheus/prometheus/blob/main/promql/functions.go#L3445-L3496 ### Solution _No response_ ### Alternatives _No response_ ### Are you willing to submit a PR? - [ ] I'm willing to submit a PR! -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
