mirror of
https://github.com/pikami/cosmium.git
synced 2025-12-19 08:50:46 +00:00
Implement Mathematical Functions
This commit is contained in:
615
query_executors/memory_executor/math_functions.go
Normal file
615
query_executors/memory_executor/math_functions.go
Normal file
@@ -0,0 +1,615 @@
|
||||
package memoryexecutor
|
||||
|
||||
import (
|
||||
"math"
|
||||
"math/rand"
|
||||
|
||||
"github.com/pikami/cosmium/internal/logger"
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
)
|
||||
|
||||
func (c memoryExecutorContext) math_Abs(arguments []interface{}, row RowType) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := c.getFieldValue(exItem, row)
|
||||
|
||||
switch val := ex.(type) {
|
||||
case float64:
|
||||
return math.Abs(val)
|
||||
case int:
|
||||
if val < 0 {
|
||||
return -val
|
||||
}
|
||||
return val
|
||||
default:
|
||||
logger.Debug("math_Abs - got parameters of wrong type")
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) math_Acos(arguments []interface{}, row RowType) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := c.getFieldValue(exItem, row)
|
||||
|
||||
val, valIsNumber := numToFloat64(ex)
|
||||
if !valIsNumber {
|
||||
logger.Debug("math_Acos - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
if val < -1 || val > 1 {
|
||||
logger.Debug("math_Acos - value out of domain for acos")
|
||||
return nil
|
||||
}
|
||||
|
||||
return math.Acos(val) * 180 / math.Pi
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) math_Asin(arguments []interface{}, row RowType) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := c.getFieldValue(exItem, row)
|
||||
|
||||
val, valIsNumber := numToFloat64(ex)
|
||||
if !valIsNumber {
|
||||
logger.Debug("math_Asin - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
if val < -1 || val > 1 {
|
||||
logger.Debug("math_Asin - value out of domain for acos")
|
||||
return nil
|
||||
}
|
||||
|
||||
return math.Asin(val) * 180 / math.Pi
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) math_Atan(arguments []interface{}, row RowType) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := c.getFieldValue(exItem, row)
|
||||
|
||||
val, valIsNumber := numToFloat64(ex)
|
||||
if !valIsNumber {
|
||||
logger.Debug("math_Atan - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return math.Atan(val) * 180 / math.Pi
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) math_Ceiling(arguments []interface{}, row RowType) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := c.getFieldValue(exItem, row)
|
||||
|
||||
switch val := ex.(type) {
|
||||
case float64:
|
||||
return math.Ceil(val)
|
||||
case int:
|
||||
return val
|
||||
default:
|
||||
logger.Debug("math_Ceiling - got parameters of wrong type")
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) math_Cos(arguments []interface{}, row RowType) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := c.getFieldValue(exItem, row)
|
||||
|
||||
val, valIsNumber := numToFloat64(ex)
|
||||
if !valIsNumber {
|
||||
logger.Debug("math_Cos - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return math.Cos(val)
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) math_Cot(arguments []interface{}, row RowType) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := c.getFieldValue(exItem, row)
|
||||
|
||||
val, valIsNumber := numToFloat64(ex)
|
||||
if !valIsNumber {
|
||||
logger.Debug("math_Cot - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
if val == 0 {
|
||||
logger.Debug("math_Cot - cotangent undefined for zero")
|
||||
return nil
|
||||
}
|
||||
|
||||
return 1 / math.Tan(val)
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) math_Degrees(arguments []interface{}, row RowType) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := c.getFieldValue(exItem, row)
|
||||
|
||||
val, valIsNumber := numToFloat64(ex)
|
||||
if !valIsNumber {
|
||||
logger.Debug("math_Degrees - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return val * (180 / math.Pi)
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) math_Exp(arguments []interface{}, row RowType) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := c.getFieldValue(exItem, row)
|
||||
|
||||
val, valIsNumber := numToFloat64(ex)
|
||||
if !valIsNumber {
|
||||
logger.Debug("math_Exp - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return math.Exp(val)
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) math_Floor(arguments []interface{}, row RowType) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := c.getFieldValue(exItem, row)
|
||||
|
||||
switch val := ex.(type) {
|
||||
case float64:
|
||||
return math.Floor(val)
|
||||
case int:
|
||||
return val
|
||||
default:
|
||||
logger.Debug("math_Floor - got parameters of wrong type")
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) math_IntBitNot(arguments []interface{}, row RowType) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := c.getFieldValue(exItem, row)
|
||||
|
||||
switch val := ex.(type) {
|
||||
case int:
|
||||
return ^val
|
||||
default:
|
||||
logger.Debug("math_IntBitNot - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) math_Log10(arguments []interface{}, row RowType) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := c.getFieldValue(exItem, row)
|
||||
|
||||
val, valIsNumber := numToFloat64(ex)
|
||||
if !valIsNumber {
|
||||
logger.Debug("math_Log10 - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
if val <= 0 {
|
||||
logger.Debug("math_Log10 - value must be greater than 0")
|
||||
return nil
|
||||
}
|
||||
|
||||
return math.Log10(val)
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) math_Radians(arguments []interface{}, row RowType) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := c.getFieldValue(exItem, row)
|
||||
|
||||
val, valIsNumber := numToFloat64(ex)
|
||||
if !valIsNumber {
|
||||
logger.Debug("math_Radians - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return val * (math.Pi / 180.0)
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) math_Round(arguments []interface{}, row RowType) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := c.getFieldValue(exItem, row)
|
||||
|
||||
switch val := ex.(type) {
|
||||
case float64:
|
||||
return math.Round(val)
|
||||
case int:
|
||||
return val
|
||||
default:
|
||||
logger.Debug("math_Round - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) math_Sign(arguments []interface{}, row RowType) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := c.getFieldValue(exItem, row)
|
||||
|
||||
switch val := ex.(type) {
|
||||
case float64:
|
||||
if val > 0 {
|
||||
return 1
|
||||
} else if val < 0 {
|
||||
return -1
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
case int:
|
||||
if val > 0 {
|
||||
return 1
|
||||
} else if val < 0 {
|
||||
return -1
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
default:
|
||||
logger.Debug("math_Sign - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) math_Sin(arguments []interface{}, row RowType) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := c.getFieldValue(exItem, row)
|
||||
|
||||
val, valIsNumber := numToFloat64(ex)
|
||||
if !valIsNumber {
|
||||
logger.Debug("math_Sin - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return math.Sin(val)
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) math_Sqrt(arguments []interface{}, row RowType) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := c.getFieldValue(exItem, row)
|
||||
|
||||
val, valIsNumber := numToFloat64(ex)
|
||||
if !valIsNumber {
|
||||
logger.Debug("math_Sqrt - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return math.Sqrt(val)
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) math_Square(arguments []interface{}, row RowType) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := c.getFieldValue(exItem, row)
|
||||
|
||||
val, valIsNumber := numToFloat64(ex)
|
||||
if !valIsNumber {
|
||||
logger.Debug("math_Square - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return math.Pow(val, 2)
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) math_Tan(arguments []interface{}, row RowType) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := c.getFieldValue(exItem, row)
|
||||
|
||||
val, valIsNumber := numToFloat64(ex)
|
||||
if !valIsNumber {
|
||||
logger.Debug("math_Tan - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return math.Tan(val)
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) math_Trunc(arguments []interface{}, row RowType) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := c.getFieldValue(exItem, row)
|
||||
|
||||
switch val := ex.(type) {
|
||||
case float64:
|
||||
return math.Trunc(val)
|
||||
case int:
|
||||
return float64(val)
|
||||
default:
|
||||
logger.Debug("math_Trunc - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) math_Atn2(arguments []interface{}, row RowType) interface{} {
|
||||
exItem1 := arguments[0].(parsers.SelectItem)
|
||||
exItem2 := arguments[1].(parsers.SelectItem)
|
||||
ex1 := c.getFieldValue(exItem1, row)
|
||||
ex2 := c.getFieldValue(exItem2, row)
|
||||
|
||||
y, yIsNumber := numToFloat64(ex1)
|
||||
x, xIsNumber := numToFloat64(ex2)
|
||||
|
||||
if !yIsNumber || !xIsNumber {
|
||||
logger.Debug("math_Atn2 - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return math.Atan2(y, x)
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) math_IntAdd(arguments []interface{}, row RowType) interface{} {
|
||||
exItem1 := arguments[0].(parsers.SelectItem)
|
||||
exItem2 := arguments[1].(parsers.SelectItem)
|
||||
ex1 := c.getFieldValue(exItem1, row)
|
||||
ex2 := c.getFieldValue(exItem2, row)
|
||||
|
||||
ex1Number, ex1IsNumber := numToInt(ex1)
|
||||
ex2Number, ex2IsNumber := numToInt(ex2)
|
||||
|
||||
if !ex1IsNumber || !ex2IsNumber {
|
||||
logger.Debug("math_IntAdd - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return ex1Number + ex2Number
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) math_IntBitAnd(arguments []interface{}, row RowType) interface{} {
|
||||
exItem1 := arguments[0].(parsers.SelectItem)
|
||||
exItem2 := arguments[1].(parsers.SelectItem)
|
||||
ex1 := c.getFieldValue(exItem1, row)
|
||||
ex2 := c.getFieldValue(exItem2, row)
|
||||
|
||||
ex1Int, ex1IsInt := numToInt(ex1)
|
||||
ex2Int, ex2IsInt := numToInt(ex2)
|
||||
|
||||
if !ex1IsInt || !ex2IsInt {
|
||||
logger.Debug("math_IntBitAnd - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return ex1Int & ex2Int
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) math_IntBitLeftShift(arguments []interface{}, row RowType) interface{} {
|
||||
exItem1 := arguments[0].(parsers.SelectItem)
|
||||
exItem2 := arguments[1].(parsers.SelectItem)
|
||||
ex1 := c.getFieldValue(exItem1, row)
|
||||
ex2 := c.getFieldValue(exItem2, row)
|
||||
|
||||
num1, num1IsInt := numToInt(ex1)
|
||||
num2, num2IsInt := numToInt(ex2)
|
||||
|
||||
if !num1IsInt || !num2IsInt {
|
||||
logger.Debug("math_IntBitLeftShift - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return num1 << uint(num2)
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) math_IntBitOr(arguments []interface{}, row RowType) interface{} {
|
||||
exItem1 := arguments[0].(parsers.SelectItem)
|
||||
exItem2 := arguments[1].(parsers.SelectItem)
|
||||
ex1 := c.getFieldValue(exItem1, row)
|
||||
ex2 := c.getFieldValue(exItem2, row)
|
||||
|
||||
num1, num1IsInt := ex1.(int)
|
||||
num2, num2IsInt := ex2.(int)
|
||||
|
||||
if !num1IsInt || !num2IsInt {
|
||||
logger.Debug("math_IntBitOr - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return num1 | num2
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) math_IntBitRightShift(arguments []interface{}, row RowType) interface{} {
|
||||
exItem1 := arguments[0].(parsers.SelectItem)
|
||||
exItem2 := arguments[1].(parsers.SelectItem)
|
||||
ex1 := c.getFieldValue(exItem1, row)
|
||||
ex2 := c.getFieldValue(exItem2, row)
|
||||
|
||||
num1, num1IsInt := numToInt(ex1)
|
||||
num2, num2IsInt := numToInt(ex2)
|
||||
|
||||
if !num1IsInt || !num2IsInt {
|
||||
logger.Debug("math_IntBitRightShift - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return num1 >> uint(num2)
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) math_IntBitXor(arguments []interface{}, row RowType) interface{} {
|
||||
exItem1 := arguments[0].(parsers.SelectItem)
|
||||
exItem2 := arguments[1].(parsers.SelectItem)
|
||||
ex1 := c.getFieldValue(exItem1, row)
|
||||
ex2 := c.getFieldValue(exItem2, row)
|
||||
|
||||
num1, num1IsInt := ex1.(int)
|
||||
num2, num2IsInt := ex2.(int)
|
||||
|
||||
if !num1IsInt || !num2IsInt {
|
||||
logger.Debug("math_IntBitXor - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return num1 ^ num2
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) math_IntDiv(arguments []interface{}, row RowType) interface{} {
|
||||
exItem1 := arguments[0].(parsers.SelectItem)
|
||||
exItem2 := arguments[1].(parsers.SelectItem)
|
||||
ex1 := c.getFieldValue(exItem1, row)
|
||||
ex2 := c.getFieldValue(exItem2, row)
|
||||
|
||||
num1, num1IsInt := ex1.(int)
|
||||
num2, num2IsInt := ex2.(int)
|
||||
|
||||
if !num1IsInt || !num2IsInt || num2 == 0 {
|
||||
logger.Debug("math_IntDiv - got parameters of wrong type or divide by zero")
|
||||
return nil
|
||||
}
|
||||
|
||||
return num1 / num2
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) math_IntMul(arguments []interface{}, row RowType) interface{} {
|
||||
exItem1 := arguments[0].(parsers.SelectItem)
|
||||
exItem2 := arguments[1].(parsers.SelectItem)
|
||||
ex1 := c.getFieldValue(exItem1, row)
|
||||
ex2 := c.getFieldValue(exItem2, row)
|
||||
|
||||
num1, num1IsInt := ex1.(int)
|
||||
num2, num2IsInt := ex2.(int)
|
||||
|
||||
if !num1IsInt || !num2IsInt {
|
||||
logger.Debug("math_IntMul - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return num1 * num2
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) math_IntSub(arguments []interface{}, row RowType) interface{} {
|
||||
exItem1 := arguments[0].(parsers.SelectItem)
|
||||
exItem2 := arguments[1].(parsers.SelectItem)
|
||||
ex1 := c.getFieldValue(exItem1, row)
|
||||
ex2 := c.getFieldValue(exItem2, row)
|
||||
|
||||
num1, num1IsInt := ex1.(int)
|
||||
num2, num2IsInt := ex2.(int)
|
||||
|
||||
if !num1IsInt || !num2IsInt {
|
||||
logger.Debug("math_IntSub - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return num1 - num2
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) math_IntMod(arguments []interface{}, row RowType) interface{} {
|
||||
exItem1 := arguments[0].(parsers.SelectItem)
|
||||
exItem2 := arguments[1].(parsers.SelectItem)
|
||||
ex1 := c.getFieldValue(exItem1, row)
|
||||
ex2 := c.getFieldValue(exItem2, row)
|
||||
|
||||
num1, num1IsInt := ex1.(int)
|
||||
num2, num2IsInt := ex2.(int)
|
||||
|
||||
if !num1IsInt || !num2IsInt || num2 == 0 {
|
||||
logger.Debug("math_IntMod - got parameters of wrong type or divide by zero")
|
||||
return nil
|
||||
}
|
||||
|
||||
return num1 % num2
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) math_Power(arguments []interface{}, row RowType) interface{} {
|
||||
exItem1 := arguments[0].(parsers.SelectItem)
|
||||
exItem2 := arguments[1].(parsers.SelectItem)
|
||||
ex1 := c.getFieldValue(exItem1, row)
|
||||
ex2 := c.getFieldValue(exItem2, row)
|
||||
|
||||
base, baseIsNumber := numToFloat64(ex1)
|
||||
exponent, exponentIsNumber := numToFloat64(ex2)
|
||||
|
||||
if !baseIsNumber || !exponentIsNumber {
|
||||
logger.Debug("math_Power - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return math.Pow(base, exponent)
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) math_Log(arguments []interface{}, row RowType) interface{} {
|
||||
exItem1 := arguments[0].(parsers.SelectItem)
|
||||
ex := c.getFieldValue(exItem1, row)
|
||||
|
||||
var base float64 = math.E
|
||||
if len(arguments) > 1 {
|
||||
exItem2 := arguments[1].(parsers.SelectItem)
|
||||
baseValueObject := c.getFieldValue(exItem2, row)
|
||||
baseValue, baseValueIsNumber := numToFloat64(baseValueObject)
|
||||
|
||||
if !baseValueIsNumber {
|
||||
logger.Debug("math_Log - base parameter must be a numeric value")
|
||||
return nil
|
||||
}
|
||||
|
||||
if baseValue > 0 && baseValue != 1 {
|
||||
base = baseValue
|
||||
} else {
|
||||
logger.Debug("math_Log - base must be greater than 0 and not equal to 1")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
num, numIsNumber := numToFloat64(ex)
|
||||
if !numIsNumber || num <= 0 {
|
||||
logger.Debug("math_Log - parameter must be a positive numeric value")
|
||||
return nil
|
||||
}
|
||||
|
||||
return math.Log(num) / math.Log(base)
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) math_NumberBin(arguments []interface{}, row RowType) interface{} {
|
||||
exItem1 := arguments[0].(parsers.SelectItem)
|
||||
ex := c.getFieldValue(exItem1, row)
|
||||
|
||||
binSize := 1.0
|
||||
|
||||
if len(arguments) > 1 {
|
||||
exItem2 := arguments[1].(parsers.SelectItem)
|
||||
binSizeValueObject := c.getFieldValue(exItem2, row)
|
||||
binSizeValue, binSizeValueIsNumber := numToFloat64(binSizeValueObject)
|
||||
|
||||
if !binSizeValueIsNumber {
|
||||
logger.Debug("math_NumberBin - base parameter must be a numeric value")
|
||||
return nil
|
||||
}
|
||||
|
||||
if binSizeValue != 0 {
|
||||
binSize = binSizeValue
|
||||
} else {
|
||||
logger.Debug("math_NumberBin - base must not be equal to 0")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
num, numIsNumber := numToFloat64(ex)
|
||||
if !numIsNumber {
|
||||
logger.Debug("math_NumberBin - parameter must be a numeric value")
|
||||
return nil
|
||||
}
|
||||
|
||||
return math.Floor(num/binSize) * binSize
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) math_Pi() interface{} {
|
||||
return math.Pi
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) math_Rand() interface{} {
|
||||
return rand.Float64()
|
||||
}
|
||||
|
||||
func numToInt(ex interface{}) (int, bool) {
|
||||
switch val := ex.(type) {
|
||||
case float64:
|
||||
return int(val), true
|
||||
case int:
|
||||
return val, true
|
||||
default:
|
||||
return 0, false
|
||||
}
|
||||
}
|
||||
|
||||
func numToFloat64(num interface{}) (float64, bool) {
|
||||
switch val := num.(type) {
|
||||
case float64:
|
||||
return val, true
|
||||
case int:
|
||||
return float64(val), true
|
||||
default:
|
||||
return 0, false
|
||||
}
|
||||
}
|
||||
269
query_executors/memory_executor/math_functions_test.go
Normal file
269
query_executors/memory_executor/math_functions_test.go
Normal file
@@ -0,0 +1,269 @@
|
||||
package memoryexecutor_test
|
||||
|
||||
import (
|
||||
"math"
|
||||
"testing"
|
||||
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
memoryexecutor "github.com/pikami/cosmium/query_executors/memory_executor"
|
||||
)
|
||||
|
||||
func Test_Execute_MathFunctions(t *testing.T) {
|
||||
mockData := []memoryexecutor.RowType{
|
||||
map[string]interface{}{"id": 1, "value": 0.0},
|
||||
map[string]interface{}{"id": 2, "value": 1.0},
|
||||
map[string]interface{}{"id": 3, "value": -1.0},
|
||||
map[string]interface{}{"id": 4, "value": 0.5},
|
||||
map[string]interface{}{"id": 5, "value": -0.5},
|
||||
map[string]interface{}{"id": 6, "value": 0.707},
|
||||
map[string]interface{}{"id": 7, "value": -0.707},
|
||||
map[string]interface{}{"id": 8, "value": 0.866},
|
||||
map[string]interface{}{"id": 9, "value": -0.866},
|
||||
}
|
||||
|
||||
mockDataInts := []memoryexecutor.RowType{
|
||||
map[string]interface{}{"id": 1, "value": -1},
|
||||
map[string]interface{}{"id": 2, "value": 0},
|
||||
map[string]interface{}{"id": 3, "value": 1},
|
||||
map[string]interface{}{"id": 4, "value": 5},
|
||||
}
|
||||
|
||||
t.Run("Should execute function ABS(value)", func(t *testing.T) {
|
||||
testMathFunctionExecute(
|
||||
t,
|
||||
parsers.FunctionCallMathAbs,
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"value": 0.0, "result": 0.0},
|
||||
map[string]interface{}{"value": 1.0, "result": 1.0},
|
||||
map[string]interface{}{"value": -1.0, "result": 1.0},
|
||||
map[string]interface{}{"value": 0.5, "result": 0.5},
|
||||
map[string]interface{}{"value": -0.5, "result": 0.5},
|
||||
map[string]interface{}{"value": 0.707, "result": 0.707},
|
||||
map[string]interface{}{"value": -0.707, "result": 0.707},
|
||||
map[string]interface{}{"value": 0.866, "result": 0.866},
|
||||
map[string]interface{}{"value": -0.866, "result": 0.866},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function ACOS(cosine)", func(t *testing.T) {
|
||||
testMathFunctionExecute(
|
||||
t,
|
||||
parsers.FunctionCallMathAcos,
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"value": 0.0, "result": math.Acos(0.0) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": 1.0, "result": math.Acos(1.0) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": -1.0, "result": math.Acos(-1.0) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": 0.5, "result": math.Acos(0.5) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": -0.5, "result": math.Acos(-0.5) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": 0.707, "result": math.Acos(0.707) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": -0.707, "result": math.Acos(-0.707) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": 0.866, "result": math.Acos(0.866) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": -0.866, "result": math.Acos(-0.866) * 180 / math.Pi},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function ASIN(value)", func(t *testing.T) {
|
||||
testMathFunctionExecute(
|
||||
t,
|
||||
parsers.FunctionCallMathAsin,
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"value": 0.0, "result": math.Asin(0.0) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": 1.0, "result": math.Asin(1.0) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": -1.0, "result": math.Asin(-1.0) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": 0.5, "result": math.Asin(0.5) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": -0.5, "result": math.Asin(-0.5) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": 0.707, "result": math.Asin(0.707) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": -0.707, "result": math.Asin(-0.707) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": 0.866, "result": math.Asin(0.866) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": -0.866, "result": math.Asin(-0.866) * 180 / math.Pi},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function ATAN(tangent)", func(t *testing.T) {
|
||||
testMathFunctionExecute(
|
||||
t,
|
||||
parsers.FunctionCallMathAtan,
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"value": 0.0, "result": math.Atan(0.0) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": 1.0, "result": math.Atan(1.0) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": -1.0, "result": math.Atan(-1.0) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": 0.5, "result": math.Atan(0.5) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": -0.5, "result": math.Atan(-0.5) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": 0.707, "result": math.Atan(0.707) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": -0.707, "result": math.Atan(-0.707) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": 0.866, "result": math.Atan(0.866) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": -0.866, "result": math.Atan(-0.866) * 180 / math.Pi},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function COS(value)", func(t *testing.T) {
|
||||
testMathFunctionExecute(
|
||||
t,
|
||||
parsers.FunctionCallMathCos,
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"value": 0.0, "result": math.Cos(0.0)},
|
||||
map[string]interface{}{"value": 1.0, "result": math.Cos(1.0)},
|
||||
map[string]interface{}{"value": -1.0, "result": math.Cos(-1.0)},
|
||||
map[string]interface{}{"value": 0.5, "result": math.Cos(0.5)},
|
||||
map[string]interface{}{"value": -0.5, "result": math.Cos(-0.5)},
|
||||
map[string]interface{}{"value": 0.707, "result": math.Cos(0.707)},
|
||||
map[string]interface{}{"value": -0.707, "result": math.Cos(-0.707)},
|
||||
map[string]interface{}{"value": 0.866, "result": math.Cos(0.866)},
|
||||
map[string]interface{}{"value": -0.866, "result": math.Cos(-0.866)},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function COT(value)", func(t *testing.T) {
|
||||
testMathFunctionExecute(
|
||||
t,
|
||||
parsers.FunctionCallMathCot,
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"value": 0.0, "result": nil},
|
||||
map[string]interface{}{"value": 1.0, "result": 1 / math.Tan(1.0)},
|
||||
map[string]interface{}{"value": -1.0, "result": 1 / math.Tan(-1.0)},
|
||||
map[string]interface{}{"value": 0.5, "result": 1 / math.Tan(0.5)},
|
||||
map[string]interface{}{"value": -0.5, "result": 1 / math.Tan(-0.5)},
|
||||
map[string]interface{}{"value": 0.707, "result": 1 / math.Tan(0.707)},
|
||||
map[string]interface{}{"value": -0.707, "result": 1 / math.Tan(-0.707)},
|
||||
map[string]interface{}{"value": 0.866, "result": 1 / math.Tan(0.866)},
|
||||
map[string]interface{}{"value": -0.866, "result": 1 / math.Tan(-0.866)},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function Degrees(value)", func(t *testing.T) {
|
||||
testMathFunctionExecute(
|
||||
t,
|
||||
parsers.FunctionCallMathDegrees,
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"value": 0.0, "result": 0.0 * (180 / math.Pi)},
|
||||
map[string]interface{}{"value": 1.0, "result": 1.0 * (180 / math.Pi)},
|
||||
map[string]interface{}{"value": -1.0, "result": -1.0 * (180 / math.Pi)},
|
||||
map[string]interface{}{"value": 0.5, "result": 0.5 * (180 / math.Pi)},
|
||||
map[string]interface{}{"value": -0.5, "result": -0.5 * (180 / math.Pi)},
|
||||
map[string]interface{}{"value": 0.707, "result": 0.707 * (180 / math.Pi)},
|
||||
map[string]interface{}{"value": -0.707, "result": -0.707 * (180 / math.Pi)},
|
||||
map[string]interface{}{"value": 0.866, "result": 0.866 * (180 / math.Pi)},
|
||||
map[string]interface{}{"value": -0.866, "result": -0.866 * (180 / math.Pi)},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function EXP(value)", func(t *testing.T) {
|
||||
testMathFunctionExecute(
|
||||
t,
|
||||
parsers.FunctionCallMathExp,
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"value": 0.0, "result": math.Exp(0.0)},
|
||||
map[string]interface{}{"value": 1.0, "result": math.Exp(1.0)},
|
||||
map[string]interface{}{"value": -1.0, "result": math.Exp(-1.0)},
|
||||
map[string]interface{}{"value": 0.5, "result": math.Exp(0.5)},
|
||||
map[string]interface{}{"value": -0.5, "result": math.Exp(-0.5)},
|
||||
map[string]interface{}{"value": 0.707, "result": math.Exp(0.707)},
|
||||
map[string]interface{}{"value": -0.707, "result": math.Exp(-0.707)},
|
||||
map[string]interface{}{"value": 0.866, "result": math.Exp(0.866)},
|
||||
map[string]interface{}{"value": -0.866, "result": math.Exp(-0.866)},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function FLOOR(value)", func(t *testing.T) {
|
||||
testMathFunctionExecute(
|
||||
t,
|
||||
parsers.FunctionCallMathFloor,
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"value": 0.0, "result": math.Floor(0.0)},
|
||||
map[string]interface{}{"value": 1.0, "result": math.Floor(1.0)},
|
||||
map[string]interface{}{"value": -1.0, "result": math.Floor(-1.0)},
|
||||
map[string]interface{}{"value": 0.5, "result": math.Floor(0.5)},
|
||||
map[string]interface{}{"value": -0.5, "result": math.Floor(-0.5)},
|
||||
map[string]interface{}{"value": 0.707, "result": math.Floor(0.707)},
|
||||
map[string]interface{}{"value": -0.707, "result": math.Floor(-0.707)},
|
||||
map[string]interface{}{"value": 0.866, "result": math.Floor(0.866)},
|
||||
map[string]interface{}{"value": -0.866, "result": math.Floor(-0.866)},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function IntBitNot(value)", func(t *testing.T) {
|
||||
testMathFunctionExecute(
|
||||
t,
|
||||
parsers.FunctionCallMathIntBitNot,
|
||||
mockDataInts,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"value": -1, "result": ^-1},
|
||||
map[string]interface{}{"value": 0, "result": ^0},
|
||||
map[string]interface{}{"value": 1, "result": ^1},
|
||||
map[string]interface{}{"value": 5, "result": ^5},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function LOG10(value)", func(t *testing.T) {
|
||||
testMathFunctionExecute(
|
||||
t,
|
||||
parsers.FunctionCallMathLog10,
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"value": 0.0, "result": nil},
|
||||
map[string]interface{}{"value": 1.0, "result": math.Log10(1.0)},
|
||||
map[string]interface{}{"value": -1.0, "result": nil},
|
||||
map[string]interface{}{"value": 0.5, "result": math.Log10(0.5)},
|
||||
map[string]interface{}{"value": -0.5, "result": nil},
|
||||
map[string]interface{}{"value": 0.707, "result": math.Log10(0.707)},
|
||||
map[string]interface{}{"value": -0.707, "result": nil},
|
||||
map[string]interface{}{"value": 0.866, "result": math.Log10(0.866)},
|
||||
map[string]interface{}{"value": -0.866, "result": nil},
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
func testMathFunctionExecute(
|
||||
t *testing.T,
|
||||
functionCallType parsers.FunctionCallType,
|
||||
data []memoryexecutor.RowType,
|
||||
expectedData []memoryexecutor.RowType,
|
||||
) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
{
|
||||
Alias: "result",
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: functionCallType,
|
||||
Arguments: []interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
},
|
||||
data,
|
||||
expectedData,
|
||||
)
|
||||
}
|
||||
@@ -284,6 +284,79 @@ func (c memoryExecutorContext) getFieldValue(field parsers.SelectItem, row RowTy
|
||||
case parsers.FunctionCallSetUnion:
|
||||
return c.set_Union(typedValue.Arguments, rowValue)
|
||||
|
||||
case parsers.FunctionCallMathAbs:
|
||||
return c.math_Abs(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallMathAcos:
|
||||
return c.math_Acos(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallMathAsin:
|
||||
return c.math_Asin(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallMathAtan:
|
||||
return c.math_Atan(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallMathCeiling:
|
||||
return c.math_Ceiling(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallMathCos:
|
||||
return c.math_Cos(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallMathCot:
|
||||
return c.math_Cot(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallMathDegrees:
|
||||
return c.math_Degrees(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallMathExp:
|
||||
return c.math_Exp(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallMathFloor:
|
||||
return c.math_Floor(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallMathIntBitNot:
|
||||
return c.math_IntBitNot(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallMathLog10:
|
||||
return c.math_Log10(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallMathRadians:
|
||||
return c.math_Radians(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallMathRound:
|
||||
return c.math_Round(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallMathSign:
|
||||
return c.math_Sign(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallMathSin:
|
||||
return c.math_Sin(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallMathSqrt:
|
||||
return c.math_Sqrt(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallMathSquare:
|
||||
return c.math_Square(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallMathTan:
|
||||
return c.math_Tan(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallMathTrunc:
|
||||
return c.math_Trunc(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallMathAtn2:
|
||||
return c.math_Atn2(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallMathIntAdd:
|
||||
return c.math_IntAdd(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallMathIntBitAnd:
|
||||
return c.math_IntBitAnd(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallMathIntBitLeftShift:
|
||||
return c.math_IntBitLeftShift(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallMathIntBitOr:
|
||||
return c.math_IntBitOr(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallMathIntBitRightShift:
|
||||
return c.math_IntBitRightShift(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallMathIntBitXor:
|
||||
return c.math_IntBitXor(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallMathIntDiv:
|
||||
return c.math_IntDiv(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallMathIntMod:
|
||||
return c.math_IntMod(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallMathIntMul:
|
||||
return c.math_IntMul(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallMathIntSub:
|
||||
return c.math_IntSub(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallMathPower:
|
||||
return c.math_Power(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallMathLog:
|
||||
return c.math_Log(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallMathNumberBin:
|
||||
return c.math_NumberBin(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallMathPi:
|
||||
return c.math_Pi()
|
||||
case parsers.FunctionCallMathRand:
|
||||
return c.math_Rand()
|
||||
|
||||
case parsers.FunctionCallAggregateAvg:
|
||||
return c.aggregate_Avg(typedValue.Arguments, row)
|
||||
case parsers.FunctionCallAggregateCount:
|
||||
|
||||
Reference in New Issue
Block a user