feat(ts): add the support of TWA aggregator to Range and MRange#3262
Open
var-nan wants to merge 11 commits intoapache:unstablefrom
Open
feat(ts): add the support of TWA aggregator to Range and MRange#3262var-nan wants to merge 11 commits intoapache:unstablefrom
var-nan wants to merge 11 commits intoapache:unstablefrom
Conversation
rangeCommon() in redis_timeseries.cc will add two samples, 'prev_sample' and 'next_sample' to the 'res' vector when the aggregator is twa. These two samples are used to compute the area of the polygons that are at the front of the first bucket and at the end of last bucket. prev_sample is the biggest sample in the data with timestamp less than or equal to first sample of filtered range and next_sample is the smallest sample in the data with timestamp greater than or equal to the last sample of filtered range. TODO: test TWA with FILTER_BY_TS/FILTER_BY_VALUE option.
When filtered with FILTER_BY_TS/FILTER_BY_VALUE, the `next` and `prev` samples are discarded while computing the area.
Contributor
Author
|
279c9bd will correctly calculate TWA when samples are filtered with |
Member
The CI failed due to a clang-tidy report in the changes. Could you fix it to pass the CI? |
Contributor
Author
|
Some of these errors didn't show up when I ran |
Contributor
|
Sorry for the wait! Been a bit busy these days. I'll review this later today. : ) |
yezhizi
reviewed
Nov 18, 2025
some code is refactored for readability.
Contributor
Author
|
Thanks @yezhizi . I was about to push the clang-tidy fixes. |
yezhizi
reviewed
Nov 27, 2025
yezhizi
reviewed
Nov 27, 2025
|
yezhizi
reviewed
Nov 28, 2025
Comment on lines
177
to
193
| auto non_empty_left_bucket_idx = [&spans](size_t curr) { | ||
| while (--curr && spans[curr].empty()); | ||
| return curr; | ||
| }; | ||
| auto non_empty_right_bucket_idx = [&spans](size_t curr) { | ||
| while (++curr < spans.size() && spans[curr].empty()); | ||
| return curr; | ||
| }; | ||
|
|
||
| std::vector<std::pair<TSSample, TSSample>> neighbors; | ||
| neighbors.reserve(spans.size()); | ||
| for (size_t i = 0; i < spans.size(); i++) { | ||
| TSSample prev = (i != 0) ? spans[non_empty_left_bucket_idx(i)].back() : prev_sample; | ||
| TSSample next = (i != (spans.size() - 1)) ? spans[non_empty_right_bucket_idx(i)].front() : next_sample; | ||
| neighbors.emplace_back(prev, next); | ||
| } | ||
|
|
Contributor
There was a problem hiding this comment.
The nested while loops inside the for loop result in O(N^2) time complexity, which can be optimized to O(N). We could:
- Iterate from 0 to N to resolve all prev neighbors by maintaining a "last seen non-empty" variable.
- Iterate from N to 0 to resolve all next neighbors similarly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



It closes #3217 .
rangeCommon()in redis_timeseries.cc will add two samples,prev_sampleandnext_sampleto theresvector when the aggregator is twa. These two samples are used to compute the area of the polygons that are at the front of the first bucket and at the end of last bucket.prev_sampleis the biggest sample in the data with timestamp less than or equal to first sample of filtered range andnext_sampleis the smallest sample in the data with timestamp greater than or equal to the last sample of filtered range.TODO: test TWA with FILTER_BY_TS/FILTER_BY_VALUE option.