@@ -7,11 +7,12 @@ import (
77 "context"
88 "fmt"
99 "math"
10- "sort"
1110
1211 "github.com/thanos-io/promql-engine/execution/model"
1312 "github.com/thanos-io/promql-engine/execution/parse"
1413 "github.com/thanos-io/promql-engine/execution/warnings"
14+ "github.com/thanos-io/promql-engine/extmath"
15+ "github.com/thanos-io/promql-engine/extwarnings"
1516
1617 "github.com/efficientgo/core/errors"
1718 "github.com/prometheus/prometheus/model/histogram"
@@ -28,7 +29,7 @@ type aggregateTable interface {
2829 // If the table is empty, it returns math.MinInt64.
2930 timestamp () int64
3031 // aggregate aggregates the given vector into the table.
31- aggregate (vector model.StepVector ) warning
32+ aggregate (vector model.StepVector ) error
3233 // toVector writes out the accumulated result to the given vector and
3334 // resets the table.
3435 toVector (ctx context.Context , pool * model.VectorPool ) model.StepVector
@@ -41,7 +42,7 @@ type scalarTable struct {
4142 ts int64
4243 inputs []uint64
4344 outputs []* model.Series
44- accumulators []accumulator
45+ accumulators []extmath. Accumulator
4546}
4647
4748func newScalarTables (stepsBatch int , inputCache []uint64 , outputCache []* model.Series , aggregation parser.ItemType ) ([]aggregateTable , error ) {
@@ -61,7 +62,7 @@ func (t *scalarTable) timestamp() int64 {
6162}
6263
6364func newScalarTable (inputSampleIDs []uint64 , outputs []* model.Series , aggregation parser.ItemType ) (* scalarTable , error ) {
64- accumulators := make ([]accumulator , len (outputs ))
65+ accumulators := make ([]extmath. Accumulator , len (outputs ))
6566 for i := range accumulators {
6667 acc , err := newScalarAccumulator (aggregation )
6768 if err != nil {
@@ -77,27 +78,27 @@ func newScalarTable(inputSampleIDs []uint64, outputs []*model.Series, aggregatio
7778 }, nil
7879}
7980
80- func (t * scalarTable ) aggregate (vector model.StepVector ) warning {
81+ func (t * scalarTable ) aggregate (vector model.StepVector ) error {
8182 t .ts = vector .T
8283
83- var warn warning
84+ var err error
8485 for i := range vector .Samples {
85- warn = coalesceWarn ( warn , t .addSample (vector .SampleIDs [i ], vector .Samples [i ]))
86+ err = extwarnings . Coalesce ( err , t .addSample (vector .SampleIDs [i ], vector .Samples [i ]))
8687 }
8788 for i := range vector .Histograms {
88- warn = coalesceWarn ( warn , t .addHistogram (vector .HistogramIDs [i ], vector .Histograms [i ]))
89+ err = extwarnings . Coalesce ( err , t .addHistogram (vector .HistogramIDs [i ], vector .Histograms [i ]))
8990 }
90- return warn
91+ return err
9192}
9293
93- func (t * scalarTable ) addSample (sampleID uint64 , sample float64 ) warning {
94+ func (t * scalarTable ) addSample (sampleID uint64 , sample float64 ) error {
9495 outputSampleID := t .inputs [sampleID ]
9596 output := t .outputs [outputSampleID ]
9697
9798 return t .accumulators [output .ID ].Add (sample , nil )
9899}
99100
100- func (t * scalarTable ) addHistogram (sampleID uint64 , h * histogram.FloatHistogram ) warning {
101+ func (t * scalarTable ) addHistogram (sampleID uint64 , h * histogram.FloatHistogram ) error {
101102 outputSampleID := t .inputs [sampleID ]
102103 output := t .outputs [outputSampleID ]
103104
@@ -115,16 +116,16 @@ func (t *scalarTable) toVector(ctx context.Context, pool *model.VectorPool) mode
115116 result := pool .GetStepVector (t .ts )
116117 for i , v := range t .outputs {
117118 switch t .accumulators [i ].ValueType () {
118- case NoValue :
119+ case extmath . NoValue :
119120 continue
120- case SingleTypeValue :
121+ case extmath . SingleTypeValue :
121122 f , h := t .accumulators [i ].Value ()
122123 if h == nil {
123124 result .AppendSample (pool , v .ID , f )
124125 } else {
125126 result .AppendHistogram (pool , v .ID , h )
126127 }
127- case MixedTypeValue :
128+ case extmath . MixedTypeValue :
128129 warnings .AddToContext (annotations .NewMixedFloatsHistogramsAggWarning (posrange.PositionRange {}), ctx )
129130 }
130131 }
@@ -181,55 +182,31 @@ func addRatioSample(ratioLimit float64, series labels.Labels) bool {
181182 (ratioLimit < 0 && sampleOffset >= (1.0 + ratioLimit ))
182183}
183184
184- func newScalarAccumulator (expr parser.ItemType ) (accumulator , error ) {
185+ func newScalarAccumulator (expr parser.ItemType ) (extmath. Accumulator , error ) {
185186 t := parser .ItemTypeStr [expr ]
186187 switch t {
187188 case "sum" :
188- return newSumAcc (), nil
189+ return extmath . NewSumAcc (), nil
189190 case "max" :
190- return newMaxAcc (), nil
191+ return extmath . NewMaxAcc (), nil
191192 case "min" :
192- return newMinAcc (), nil
193+ return extmath . NewMinAcc (), nil
193194 case "count" :
194- return newCountAcc (), nil
195+ return extmath . NewCountAcc (), nil
195196 case "avg" :
196- return newAvgAcc (), nil
197+ return extmath . NewAvgAcc (), nil
197198 case "group" :
198- return newGroupAcc (), nil
199+ return extmath . NewGroupAcc (), nil
199200 case "stddev" :
200- return newStdDevAcc (), nil
201+ return extmath . NewStdDevAcc (), nil
201202 case "stdvar" :
202- return newStdVarAcc (), nil
203+ return extmath . NewStdVarAcc (), nil
203204 case "quantile" :
204- return newQuantileAcc (), nil
205+ return extmath . NewQuantileAcc (), nil
205206 case "histogram_avg" :
206- return newHistogramAvg (), nil
207+ return extmath . NewHistogramAvgAcc (), nil
207208 }
208209
209210 msg := fmt .Sprintf ("unknown aggregation function %s" , t )
210211 return nil , errors .Wrap (parse .ErrNotSupportedExpr , msg )
211212}
212-
213- func Quantile (q float64 , points []float64 ) float64 {
214- if len (points ) == 0 || math .IsNaN (q ) {
215- return math .NaN ()
216- }
217- if q < 0 {
218- return math .Inf (- 1 )
219- }
220- if q > 1 {
221- return math .Inf (+ 1 )
222- }
223- sort .Float64s (points )
224-
225- n := float64 (len (points ))
226- // When the quantile lies between two samples,
227- // we use a weighted average of the two samples.
228- rank := q * (n - 1 )
229-
230- lowerIndex := math .Max (0 , math .Floor (rank ))
231- upperIndex := math .Min (n - 1 , lowerIndex + 1 )
232-
233- weight := rank - math .Floor (rank )
234- return points [int (lowerIndex )]* (1 - weight ) + points [int (upperIndex )]* weight
235- }
0 commit comments