diff --git a/src/aggregation/metric/min.rs b/src/aggregation/metric/min.rs index 45ffdcb9d..1e94e45f7 100644 --- a/src/aggregation/metric/min.rs +++ b/src/aggregation/metric/min.rs @@ -1,8 +1,10 @@ -use std::fmt::Debug; +use std::{fmt::Debug, borrow::BorrowMut}; use serde::{Deserialize, Serialize}; -use super::IntermediateStats; +use crate::aggregation::intermediate_agg_result::IntermediateMetricResult; + +use super::{IntermediateStats, IntermediateInnerCollector}; /// A single-value metric aggregation that computes the minimum of numeric values that are /// extracted from the aggregated documents. @@ -63,3 +65,48 @@ impl IntermediateMin { self.stats.finalize().min } } +/* +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct IntermediateMin { + min: f64, + is_some: bool, +} + +impl Default for IntermediateMin { + fn default() -> Self { + Self { + min: f64::MAX, + is_some: false, + } + } +} + +impl IntermediateMin { + /// Merges the other intermediate result into self. + pub fn merge_fruits(&mut self, other: IntermediateMin) { + self.min = self.min.min(other.min); + } + /// Computes the final minimum value. + pub fn finalize(&self) -> Option { + if self.is_some { + Some(self.min) + } else { + None + } + } + + +} + +impl IntermediateInnerCollector for IntermediateMin { + #[inline] + fn collect(&mut self, value: f64) { + self.is_some=true; + self.min=self.min.min(value); + } + + fn into_intermediate_metric_result(self) -> IntermediateMetricResult { + IntermediateMetricResult::Min(self) + } +} +*/ diff --git a/src/aggregation/metric/stats.rs b/src/aggregation/metric/stats.rs index e5e1fe694..582b6e6fe 100644 --- a/src/aggregation/metric/stats.rs +++ b/src/aggregation/metric/stats.rs @@ -303,11 +303,13 @@ impl IntermediateStats { self.count += 1; // kahan algorithm for sum + let y = value - self.delta; let t = self.sum + y; self.delta = (t - self.sum) - y; self.sum = t; - + + //self.sum+=value; self.min = self.min.min(value); self.max = self.max.max(value); } @@ -396,7 +398,7 @@ impl IntermediateExtendedStats { + other.intermediate_stats.sum as f64) / new_count as f64; self.intermediate_stats.sum += other.intermediate_stats.sum; - self.intermediate_stats.delta += other.intermediate_stats.delta; + //self.intermediate_stats.delta += other.intermediate_stats.delta; self.sum_of_squares_elastic += other.sum_of_squares_elastic; self.delta_sum_for_squares_elastic += other.delta_sum_for_squares_elastic } @@ -545,12 +547,12 @@ impl IntermediateInnerCollector for IntermediateInnerStatsCollector { IntermediateMetricResult::Min(IntermediateMin::from_stats(self.stats)) } SegmentStatsType::Stats => IntermediateMetricResult::Stats(self.stats), - SegmentStatsType::ExtendedStats => { - panic!("cannot create IntermediateMetricResult for ExtendStats from Stats"); - } SegmentStatsType::Sum => { IntermediateMetricResult::Sum(IntermediateSum::from_stats(self.stats)) } + _ => { + panic!("cannot create IntermediateMetricResult for ExtendStats/Min from Stats"); + } } } } diff --git a/src/aggregation/segment_agg_result.rs b/src/aggregation/segment_agg_result.rs index 071a49706..863eb24c4 100644 --- a/src/aggregation/segment_agg_result.rs +++ b/src/aggregation/segment_agg_result.rs @@ -17,7 +17,7 @@ use super::metric::{ }; use crate::aggregation::bucket::TermMissingAgg; use crate::aggregation::metric::{ - IntermediateInnerExtendedStatsCollector, IntermediateInnerStatsCollector, + IntermediateInnerExtendedStatsCollector, IntermediateInnerStatsCollector, IntermediateMin, }; pub(crate) trait SegmentAggregationCollector: CollectorClone + Debug {