Skip to content

Commit

Permalink
Merge pull request #9632 from Ahmah2009/master
Browse files Browse the repository at this point in the history
Implement floor, ceil, and round functions
  • Loading branch information
jsternberg authored Apr 4, 2018
2 parents ef8f6b7 + 7968e21 commit 733f286
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion query/math.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package query

import (
"fmt"
"math"

"github.com/influxdata/influxql"
)

func isMathFunction(call *influxql.Call) bool {
switch call.Name {
case "sin", "cos", "tan":
case "sin", "cos", "tan", "floor", "ceil", "round":
return true
}
return false
Expand All @@ -24,6 +25,13 @@ func (MathTypeMapper) CallType(name string, args []influxql.DataType) (influxql.
switch name {
case "sin", "cos", "tan":
return influxql.Float, nil
case "floor", "ceil", "round":
switch args[0] {
case influxql.Float, influxql.Integer, influxql.Unsigned, influxql.Unknown:
return args[0], nil
default:
return influxql.Unknown, fmt.Errorf("invalid argument type for first argument in %s(): %s", name, args[0])
}
}
return influxql.Unknown, nil
}
Expand All @@ -46,6 +54,33 @@ func (v MathValuer) Call(name string, args []interface{}) (interface{}, bool) {
return v.callTrigFunction(math.Cos, arg0)
case "tan":
return v.callTrigFunction(math.Tan, arg0)
case "floor":
switch arg0 := arg0.(type) {
case float64:
return math.Floor(arg0), true
case int64, uint64:
return arg0, true
default:
return nil, true
}
case "ceil":
switch arg0 := arg0.(type) {
case float64:
return math.Ceil(arg0), true
case int64, uint64:
return arg0, true
default:
return nil, true
}
case "round":
switch arg0 := arg0.(type) {
case float64:
return round(arg0), true
case int64, uint64:
return arg0, true
default:
return nil, true
}
}
}
return nil, false
Expand All @@ -63,3 +98,11 @@ func (MathValuer) callTrigFunction(fn func(x float64) float64, arg0 interface{})
}
return fn(value), true
}

func round(x float64) float64 {
t := math.Trunc(x)
if math.Abs(x-t) >= 0.5 {
return t + math.Copysign(1, x)
}
return t
}

0 comments on commit 733f286

Please sign in to comment.