From 4e3abc6cbf7f1d25499f9be0a9838683b0754468 Mon Sep 17 00:00:00 2001 From: beorn7 Date: Tue, 8 Nov 2016 21:03:31 +0100 Subject: [PATCH] Simply use `math.Mod(float64, float64)` after all This circumvents all the problems with int overflow, plus it is what was originally intended. --- promql/engine.go | 10 ++-------- promql/testdata/operators.test | 19 ++++--------------- 2 files changed, 6 insertions(+), 23 deletions(-) diff --git a/promql/engine.go b/promql/engine.go index 82548e0c4..253dc4245 100644 --- a/promql/engine.go +++ b/promql/engine.go @@ -1037,10 +1037,7 @@ func scalarBinop(op itemType, lhs, rhs model.SampleValue) model.SampleValue { case itemPOW: return model.SampleValue(math.Pow(float64(lhs), float64(rhs))) case itemMOD: - if int64(rhs) != 0 && convertibleToInt64(lhs) && convertibleToInt64(rhs) { - return model.SampleValue(int64(lhs) % int64(rhs)) - } - return model.SampleValue(math.NaN()) + return model.SampleValue(math.Mod(float64(lhs), float64(rhs))) case itemEQL: return btos(lhs == rhs) case itemNEQ: @@ -1071,10 +1068,7 @@ func vectorElemBinop(op itemType, lhs, rhs model.SampleValue) (model.SampleValue case itemPOW: return model.SampleValue(math.Pow(float64(lhs), float64(rhs))), true case itemMOD: - if int64(rhs) != 0 && convertibleToInt64(lhs) && convertibleToInt64(rhs) { - return model.SampleValue(int64(lhs) % int64(rhs)), true - } - return model.SampleValue(math.NaN()), true + return model.SampleValue(math.Mod(float64(lhs), float64(rhs))), true case itemEQL: return lhs, lhs == rhs case itemNEQ: diff --git a/promql/testdata/operators.test b/promql/testdata/operators.test index c14386aa9..2807991f3 100644 --- a/promql/testdata/operators.test +++ b/promql/testdata/operators.test @@ -35,8 +35,8 @@ eval instant at 50m SUM(http_requests) BY (job) % 3 {job="app-server"} 2 eval instant at 50m SUM(http_requests) BY (job) % 0.3 - {job="api-server"} NaN - {job="app-server"} NaN + {job="api-server"} 0.1 + {job="app-server"} 0.2 eval instant at 50m SUM(http_requests) BY (job) ^ 2 {job="api-server"} 1000000 @@ -54,10 +54,9 @@ eval instant at 50m SUM(http_requests) BY (job) % 2 ^ 3 ^ 2 {job="api-server"} 488 {job="app-server"} 40 -# int64 overflow on RHS of %. eval instant at 50m SUM(http_requests) BY (job) % 2 ^ 3 ^ 2 ^ 2 - {job="api-server"} NaN - {job="app-server"} NaN + {job="api-server"} 1000 + {job="app-server"} 2600 eval instant at 50m COUNT(http_requests) BY (job) ^ COUNT(http_requests) BY (job) {job="api-server"} 256 @@ -352,13 +351,3 @@ eval instant at 5m metricA + ignoring() metricB eval instant at 5m metricA + metricB {baz="meh"} 7 - -clear - -load 5m - finite{foo="bar"} 42 - almost_zero{foo="bar"} 0.123 - -# MOD by "almost zero" with vector. -eval instant at 5m finite % almost_zero - {foo="bar"} NaN \ No newline at end of file