Skip to content

Commit

Permalink
Don't exclude division by 0
Browse files Browse the repository at this point in the history
Signed-off-by: Jeanette Tan <jeanette.tan@grafana.com>
  • Loading branch information
zenador committed Apr 24, 2023
1 parent e9e2fa2 commit 41272a6
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 17 deletions.
2 changes: 1 addition & 1 deletion promql/engine.go
Expand Up @@ -2439,7 +2439,7 @@ func vectorElemBinop(op parser.ItemType, lhs, rhs float64, hlhs, hrhs *histogram
}
return lhs * rhs, nil, true
case parser.DIV:
if hlhs != nil && hrhs == nil && rhs != 0 {
if hlhs != nil && hrhs == nil {
return 0, hlhs.Copy().Div(rhs), true
}
return lhs / rhs, nil, true
Expand Down
68 changes: 52 additions & 16 deletions promql/engine_test.go
Expand Up @@ -38,6 +38,22 @@ import (
"github.com/prometheus/prometheus/util/stats"
)

var originalHistogram = histogram.Histogram{
Schema: 0,
Count: 21,
Sum: 33,
ZeroThreshold: 0.001,
ZeroCount: 3,
PositiveSpans: []histogram.Span{
{Offset: 0, Length: 3},
},
PositiveBuckets: []int64{3, 0, 0},
NegativeSpans: []histogram.Span{
{Offset: 0, Length: 3},
},
NegativeBuckets: []int64{3, 0, 0},
}

func TestMain(m *testing.M) {
goleak.VerifyTestMain(m)
}
Expand Down Expand Up @@ -4238,22 +4254,8 @@ func TestNativeHistogram_MulDivOperator(t *testing.T) {
expectedDiv histogram.FloatHistogram
}{
{
scalar: 3,
histogram: histogram.Histogram{
Schema: 0,
Count: 21,
Sum: 33,
ZeroThreshold: 0.001,
ZeroCount: 3,
PositiveSpans: []histogram.Span{
{Offset: 0, Length: 3},
},
PositiveBuckets: []int64{3, 0, 0},
NegativeSpans: []histogram.Span{
{Offset: 0, Length: 3},
},
NegativeBuckets: []int64{3, 0, 0},
},
scalar: 3,
histogram: originalHistogram,
expectedMul: histogram.FloatHistogram{
Schema: 0,
Count: 63,
Expand Down Expand Up @@ -4285,6 +4287,40 @@ func TestNativeHistogram_MulDivOperator(t *testing.T) {
NegativeBuckets: []float64{1, 1, 1},
},
},
{
scalar: 0,
histogram: originalHistogram,
expectedMul: histogram.FloatHistogram{
Schema: 0,
Count: 0,
Sum: 0,
ZeroThreshold: 0.001,
ZeroCount: 0,
PositiveSpans: []histogram.Span{
{Offset: 0, Length: 3},
},
PositiveBuckets: []float64{0, 0, 0},
NegativeSpans: []histogram.Span{
{Offset: 0, Length: 3},
},
NegativeBuckets: []float64{0, 0, 0},
},
expectedDiv: histogram.FloatHistogram{
Schema: 0,
Count: math.Inf(1),
Sum: math.Inf(1),
ZeroThreshold: 0.001,
ZeroCount: math.Inf(1),
PositiveSpans: []histogram.Span{
{Offset: 0, Length: 3},
},
PositiveBuckets: []float64{math.Inf(1), math.Inf(1), math.Inf(1)},
NegativeSpans: []histogram.Span{
{Offset: 0, Length: 3},
},
NegativeBuckets: []float64{math.Inf(1), math.Inf(1), math.Inf(1)},
},
},
}

idx0 := int64(0)
Expand Down

0 comments on commit 41272a6

Please sign in to comment.