Skip to content

Commit

Permalink
MQE: Fix iterating over scalar values in Clamp
Browse files Browse the repository at this point in the history
  • Loading branch information
jhesketh committed Oct 16, 2024
1 parent a5eca9a commit 74b9939
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
22 changes: 19 additions & 3 deletions pkg/streamingpromql/functions/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,25 @@ var Clamp InstantVectorSeriesFunction = func(seriesData types.InstantVectorSerie
outputIdx := 0
minArg := scalarArgsData[0]
maxArg := scalarArgsData[1]
for step, data := range seriesData.Floats {
minVal := minArg.Samples[step].F
maxVal := maxArg.Samples[step].F

// There will always be a scalar at every step of the query.
// However, there may not be a sample at a step. So we need to
// keep track of where we are up to step-wise with the scalars,
// incrementing through the scalars until their timestamp matches
// the samples.
minSampleIdx := 0
maxSampleIdx := 0

for _, data := range seriesData.Floats {
for data.T > minArg.Samples[minSampleIdx].T {
minSampleIdx++
}
for data.T > maxArg.Samples[maxSampleIdx].T {
maxSampleIdx++
}
minVal := minArg.Samples[minSampleIdx].F
maxVal := maxArg.Samples[maxSampleIdx].F

if maxVal < minVal {
// Drop this point as there is no valid answer
continue
Expand Down
10 changes: 10 additions & 0 deletions pkg/streamingpromql/testdata/ours/functions.test
Original file line number Diff line number Diff line change
Expand Up @@ -414,3 +414,13 @@ load 5m

eval range from 0 to 60m step 5m clamp(series, scalar(mins), scalar(maxes))
{} 0 10 5 _ _ -5 40 NaN NaN NaN NaN NaN NaN

clear

load 6m
mins 1 2 3 4 0 0 _ 0 5
maxes 9 8 7 6 5 4 3 _ 10
series 0 _ 0 0 10 10 10 10 2

eval range from 0 to 48m step 6m clamp(series, scalar(mins), scalar(maxes))
{} 1 _ 3 4 5 4 NaN NaN 5

0 comments on commit 74b9939

Please sign in to comment.