var-nan commented on code in PR #3262:
URL: https://github.com/apache/kvrocks/pull/3262#discussion_r2590522097
##########
src/types/redis_timeseries.cc:
##########
@@ -97,7 +101,96 @@ std::vector<TSSample>
AggregateSamplesByRangeOption(std::vector<TSSample> sample
}
return 0;
};
+ // Linear interpolation.
+ auto interpolate_sample = [](const TSSample &left_nb, uint64_t ts, const
TSSample &right_nb) {
+ auto y_diff = right_nb.v - left_nb.v;
+ auto x_diff = static_cast<double>(right_nb.ts - left_nb.ts);
+ auto x_diff_prime = static_cast<double>(ts - left_nb.ts);
+ auto y_diff_prime = (x_diff_prime * y_diff) / x_diff;
+ TSSample sample;
+ sample.ts = ts;
+ sample.v = y_diff_prime + left_nb.v;
+ return sample;
+ };
+ // Computes the TWA of empty bucket from its neighbor samples.
+ auto empty_bucket_twa = [&interpolate_sample](const TSSample &left_nb,
uint64_t bucket_left, uint64_t bucket_right,
+ const TSSample &right_nb) {
+ auto left = interpolate_sample(left_nb, bucket_left, right_nb);
+ auto right = interpolate_sample(left_nb, bucket_right, right_nb);
+ return Reducer::Area(std::array<TSSample, 2>{left, right}) /
static_cast<double>(bucket_right - bucket_left);
+ };
+
+ // Retrieve prev_sample and next_sample from samples when TWA aggregation.
+ TSSample prev_sample, next_sample;
+ bool is_twa_aggregator = aggregator.type == TSAggregatorType::TWA,
prev_available = false, next_available = false;
+ if (is_twa_aggregator) {
+ const bool discard_boundaries = !option.filter_by_ts.empty() ||
option.filter_by_value.has_value();
+ next_sample = samples.back();
+ samples.pop_back();
+ prev_sample = samples.back();
+ samples.pop_back();
+ // When FILTER_BY_TS/FILTER_BY_VALUE is enabled, discard out-of-boundary
samples.
+ prev_available = discard_boundaries ? false : !samples.empty() &&
(samples.front().ts != prev_sample.ts);
+ next_available = discard_boundaries ? false : !samples.empty() &&
(samples.back().ts != next_sample.ts);
+ }
Review Comment:
Hi @yezhizi the `prev_sample` and `next_sample` are properly initialized as
neighbors to the filtered range, as long as the entire dataset is not empty. In
other cases, those two are initialized with MAX_TIMESTAMP.
I declared the struct as
```
struct TWABounds{
TSSample prev_sample;
TSSample next_sample;
}
```
Is there any other need for them to be declared as `optional`
--
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]