diff --git a/sql/funcs.go b/sql/funcs.go index 6be16718..4b9a72aa 100644 --- a/sql/funcs.go +++ b/sql/funcs.go @@ -56,6 +56,7 @@ var aggrs = map[string]bool{ "math.midhinge": true, "math.min": true, "math.mode": true, + "math.nearestrank": true, "math.percentile": true, "math.sample": true, "math.spread": true, @@ -139,6 +140,7 @@ var funcs = map[string]map[int]interface{}{ "math.midhinge": {1: nil}, "math.min": {1: nil}, "math.mode": {1: nil}, + "math.nearestrank": {2: nil}, "math.percentile": {2: nil}, "math.round": {1: nil}, "math.sample": {2: nil}, diff --git a/util/fncs/fnc.go b/util/fncs/fnc.go index c9271343..12c4deef 100644 --- a/util/fncs/fnc.go +++ b/util/fncs/fnc.go @@ -146,6 +146,8 @@ func Run(ctx context.Context, name string, args ...interface{}) (interface{}, er return mathMin(ctx, args...) case "math.mode": return mathMode(ctx, args...) + case "math.nearestrank": + return mathNearestRank(ctx, args...) case "math.percentile": return mathPercentile(ctx, args...) case "math.round": diff --git a/util/fncs/math.go b/util/fncs/math.go index e651bfb4..f66d38cd 100644 --- a/util/fncs/math.go +++ b/util/fncs/math.go @@ -115,6 +115,14 @@ func mathMode(ctx context.Context, args ...interface{}) (out []float64, err erro return math.Mode(vals), nil } +func mathNearestRank(ctx context.Context, args ...interface{}) (out interface{}, err error) { + vals := ensureFloats(args[0]) + if perc, ok := ensureFloat(args[1]); ok { + return outputFloat(math.NearestRankPercentile(vals, perc)) + } + return +} + func mathPercentile(ctx context.Context, args ...interface{}) (out interface{}, err error) { vals := ensureFloats(args[0]) if perc, ok := ensureFloat(args[1]); ok { @@ -152,7 +160,7 @@ func mathSqrt(ctx context.Context, args ...interface{}) (out interface{}, err er func mathStddev(ctx context.Context, args ...interface{}) (out interface{}, err error) { vals := ensureFloats(args[0]) - return outputFloat(math.PopulationStandardDeviation(vals)) + return outputFloat(math.SampleStandardDeviation(vals)) } func mathSum(ctx context.Context, args ...interface{}) (out interface{}, err error) { @@ -175,5 +183,5 @@ func mathTrimean(ctx context.Context, args ...interface{}) (out interface{}, err func mathVariance(ctx context.Context, args ...interface{}) (out interface{}, err error) { vals := ensureFloats(args[0]) - return outputFloat(math.PopulationVariance(vals)) + return outputFloat(math.SampleVariance(vals)) } diff --git a/util/fncs/math_test.go b/util/fncs/math_test.go index 7597e68e..b3e57c04 100644 --- a/util/fncs/math_test.go +++ b/util/fncs/math_test.go @@ -179,6 +179,17 @@ func TestMath(t *testing.T) { So(res, ShouldResemble, []float64{1}) }) + Convey("math.nearestrank() works properly", t, func() { + res, _ = Run(context.Background(), "math.nearestrank", "test", 90) + So(res, ShouldEqual, nil) + res, _ = Run(context.Background(), "math.nearestrank", 10, "oops") + So(res, ShouldEqual, nil) + res, _ = Run(context.Background(), "math.nearestrank", 10, 90) + So(res, ShouldEqual, 10) + res, _ = Run(context.Background(), "math.nearestrank", test, 90) + So(res, ShouldEqual, 4.5) + }) + Convey("math.percentile() works properly", t, func() { res, _ = Run(context.Background(), "math.percentile", "test", 90) So(res, ShouldEqual, nil) @@ -232,9 +243,9 @@ func TestMath(t *testing.T) { res, _ = Run(context.Background(), "math.stddev", "test") So(res, ShouldEqual, nil) res, _ = Run(context.Background(), "math.stddev", 10) - So(res, ShouldEqual, 0) + So(res, ShouldEqual, nil) res, _ = Run(context.Background(), "math.stddev", test) - So(res, ShouldEqual, 1.2747548783981961) + So(res, ShouldEqual, 1.4719601443879744) }) Convey("math.sum() works properly", t, func() { @@ -274,9 +285,9 @@ func TestMath(t *testing.T) { res, _ = Run(context.Background(), "math.variance", "test") So(res, ShouldEqual, nil) res, _ = Run(context.Background(), "math.variance", 10) - So(res, ShouldEqual, 0) + So(res, ShouldEqual, nil) res, _ = Run(context.Background(), "math.variance", test) - So(res, ShouldEqual, 1.625) + So(res, ShouldEqual, 2.1666666666666665) }) }