123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379 |
- // Copyright 2022-2023 EMQ Technologies Co., Ltd.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package function
- import (
- "fmt"
- "github.com/montanaflynn/stats"
- "github.com/lf-edge/ekuiper/pkg/api"
- "github.com/lf-edge/ekuiper/pkg/ast"
- "github.com/lf-edge/ekuiper/pkg/cast"
- )
- func registerAggFunc() {
- builtins["avg"] = builtinFunc{
- fType: ast.FuncTypeAgg,
- exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
- arg0 := args[0].([]interface{})
- c := getCount(arg0)
- if c > 0 {
- v := getFirstValidArg(arg0)
- switch v.(type) {
- case int, int64:
- if r, err := sliceIntTotal(arg0); err != nil {
- return err, false
- } else {
- return r / int64(c), true
- }
- case float64:
- if r, err := sliceFloatTotal(arg0); err != nil {
- return err, false
- } else {
- return r / float64(c), true
- }
- case nil:
- return nil, true
- default:
- return fmt.Errorf("run avg function error: found invalid arg %[1]T(%[1]v)", v), false
- }
- }
- return nil, true
- },
- val: ValidateOneNumberArg,
- check: returnNilIfHasAnyNil,
- }
- builtins["count"] = builtinFunc{
- fType: ast.FuncTypeAgg,
- exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
- arg0 := args[0].([]interface{})
- return getCount(arg0), true
- },
- val: ValidateOneArg,
- check: returnNilIfHasAnyNil,
- }
- builtins["max"] = builtinFunc{
- fType: ast.FuncTypeAgg,
- exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
- arg0 := args[0].([]interface{})
- return max(arg0)
- },
- val: ValidateOneNumberArg,
- check: returnNilIfHasAnyNil,
- }
- builtins["min"] = builtinFunc{
- fType: ast.FuncTypeAgg,
- exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
- arg0 := args[0].([]interface{})
- return min(arg0)
- },
- val: ValidateOneNumberArg,
- check: returnNilIfHasAnyNil,
- }
- builtins["sum"] = builtinFunc{
- fType: ast.FuncTypeAgg,
- exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
- arg0 := args[0].([]interface{})
- if len(arg0) > 0 {
- v := getFirstValidArg(arg0)
- switch v.(type) {
- case int, int64:
- if r, err := sliceIntTotal(arg0); err != nil {
- return err, false
- } else {
- return r, true
- }
- case float64:
- if r, err := sliceFloatTotal(arg0); err != nil {
- return err, false
- } else {
- return r, true
- }
- case nil:
- return nil, true
- default:
- return fmt.Errorf("run sum function error: found invalid arg %[1]T(%[1]v)", v), false
- }
- }
- return nil, true
- },
- val: ValidateOneNumberArg,
- check: returnNilIfHasAnyNil,
- }
- builtins["collect"] = builtinFunc{
- fType: ast.FuncTypeAgg,
- exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
- if len(args) > 0 {
- return args[0], true
- }
- return make([]interface{}, 0), true
- },
- val: ValidateOneArg,
- }
- builtins["merge_agg"] = builtinFunc{
- fType: ast.FuncTypeAgg,
- exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
- data, ok := args[0].([]interface{})
- if ok {
- result := make(map[string]interface{})
- for _, ele := range data {
- if m, ok := ele.(map[string]interface{}); ok {
- for k, v := range m {
- result[k] = v
- }
- }
- }
- return result, true
- }
- return nil, true
- },
- val: ValidateOneArg,
- check: returnNilIfHasAnyNil,
- }
- builtins["deduplicate"] = builtinFunc{
- fType: ast.FuncTypeAgg,
- exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
- v1, ok1 := args[0].([]interface{})
- v2, ok2 := args[1].([]interface{})
- v3a, ok3 := args[2].([]interface{})
- if ok1 && ok2 && ok3 && len(v3a) > 0 {
- v3, ok4 := getFirstValidArg(v3a).(bool)
- if ok4 {
- if r, err := dedup(v1, v2, v3); err != nil {
- return err, false
- } else {
- return r, true
- }
- }
- }
- return fmt.Errorf("Invalid argument type found."), false
- },
- val: func(_ api.FunctionContext, args []ast.Expr) error {
- if err := ValidateLen(2, len(args)); err != nil {
- return err
- }
- if !ast.IsBooleanArg(args[1]) {
- return ProduceErrInfo(1, "bool")
- }
- return nil
- },
- check: returnNilIfHasAnyNil,
- }
- builtins["stddev"] = builtinFunc{
- fType: ast.FuncTypeAgg,
- exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
- arg0 := args[0].([]interface{})
- if len(arg0) > 0 {
- float64Slice, err := cast.ToFloat64Slice(arg0, cast.CONVERT_SAMEKIND)
- if err != nil {
- return fmt.Errorf("requires float64 slice but found %[1]T(%[1]v)", arg0), false
- }
- deviation, err := stats.StandardDeviation(float64Slice)
- if err != nil {
- if err == stats.EmptyInputErr {
- return nil, true
- }
- return fmt.Errorf("StandardDeviation exec with error: %v", err), false
- }
- return deviation, true
- }
- return nil, true
- },
- val: ValidateOneNumberArg,
- check: returnNilIfHasAnyNil,
- }
- builtins["stddevs"] = builtinFunc{
- fType: ast.FuncTypeAgg,
- exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
- arg0 := args[0].([]interface{})
- if len(arg0) > 0 {
- float64Slice, err := cast.ToFloat64Slice(arg0, cast.CONVERT_SAMEKIND)
- if err != nil {
- return fmt.Errorf("requires float64 slice but found %[1]T(%[1]v)", arg0), false
- }
- deviation, err := stats.StandardDeviationSample(float64Slice)
- if err != nil {
- if err == stats.EmptyInputErr {
- return nil, true
- }
- return fmt.Errorf("StandardDeviationSample exec with error: %v", err), false
- }
- return deviation, true
- }
- return nil, true
- },
- val: ValidateOneNumberArg,
- check: returnNilIfHasAnyNil,
- }
- builtins["var"] = builtinFunc{
- fType: ast.FuncTypeAgg,
- exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
- arg0 := args[0].([]interface{})
- if len(arg0) > 0 {
- float64Slice, err := cast.ToFloat64Slice(arg0, cast.CONVERT_SAMEKIND)
- if err != nil {
- return fmt.Errorf("requires float64 slice but found %[1]T(%[1]v)", arg0), false
- }
- deviation, err := stats.Variance(float64Slice)
- if err != nil {
- if err == stats.EmptyInputErr {
- return nil, true
- }
- return fmt.Errorf("PopulationVariance exec with error: %v", err), false
- }
- return deviation, true
- }
- return nil, true
- },
- val: ValidateOneNumberArg,
- check: returnNilIfHasAnyNil,
- }
- builtins["vars"] = builtinFunc{
- fType: ast.FuncTypeAgg,
- exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
- arg0 := args[0].([]interface{})
- if len(arg0) > 0 {
- float64Slice, err := cast.ToFloat64Slice(arg0, cast.CONVERT_SAMEKIND)
- if err != nil {
- return fmt.Errorf("requires float64 slice but found %[1]T(%[1]v)", arg0), false
- }
- deviation, err := stats.SampleVariance(float64Slice)
- if err != nil {
- if err == stats.EmptyInputErr {
- return nil, true
- }
- return fmt.Errorf("SampleVariance exec with error: %v", err), false
- }
- return deviation, true
- }
- return nil, true
- },
- val: ValidateOneNumberArg,
- check: returnNilIfHasAnyNil,
- }
- builtins["percentile_cont"] = builtinFunc{
- fType: ast.FuncTypeAgg,
- exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
- if err := ValidateLen(2, len(args)); err != nil {
- return err, false
- }
- var arg1Float64 float64 = 1
- arg0 := args[0].([]interface{})
- arg1 := args[1].([]interface{})
- if len(arg1) > 0 {
- v1 := getFirstValidArg(arg1)
- val, err := cast.ToFloat64(v1, cast.CONVERT_SAMEKIND)
- if err != nil {
- return fmt.Errorf("the second parameter requires float64 but found %[1]T(%[1]v)", arg1), false
- }
- arg1Float64 = val
- }
- if len(arg0) > 0 {
- float64Slice, err := cast.ToFloat64Slice(arg0, cast.CONVERT_SAMEKIND)
- if err != nil {
- return fmt.Errorf("requires float64 slice but found %[1]T(%[1]v)", arg0), false
- }
- deviation, err := stats.Percentile(float64Slice, arg1Float64*100)
- if err != nil {
- if err == stats.EmptyInputErr {
- return nil, true
- }
- return fmt.Errorf("percentile exec with error: %v", err), false
- }
- return deviation, true
- }
- return nil, true
- },
- val: ValidateTwoNumberArg,
- check: returnNilIfHasAnyNil,
- }
- builtins["percentile_disc"] = builtinFunc{
- fType: ast.FuncTypeAgg,
- exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
- if err := ValidateLen(2, len(args)); err != nil {
- return err, false
- }
- var arg1Float64 float64 = 1
- arg0 := args[0].([]interface{})
- arg1 := args[1].([]interface{})
- if len(arg1) > 0 {
- v1 := getFirstValidArg(arg1)
- val, err := cast.ToFloat64(v1, cast.CONVERT_SAMEKIND)
- if err != nil {
- return fmt.Errorf("the second parameter requires float64 but found %[1]T(%[1]v)", arg1), false
- }
- arg1Float64 = val
- }
- if len(arg0) > 0 {
- float64Slice, err := cast.ToFloat64Slice(arg0, cast.CONVERT_SAMEKIND)
- if err != nil {
- return fmt.Errorf("requires float64 slice but found %[1]T(%[1]v)", arg0), false
- }
- deviation, err := stats.PercentileNearestRank(float64Slice, arg1Float64*100)
- if err != nil {
- if err == stats.EmptyInputErr {
- return nil, true
- }
- return fmt.Errorf("PopulationVariance exec with error: %v", err), false
- }
- return deviation, true
- }
- return nil, true
- },
- val: ValidateTwoNumberArg,
- check: returnNilIfHasAnyNil,
- }
- builtins["last_value"] = builtinFunc{
- fType: ast.FuncTypeAgg,
- exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
- arg0, ok := args[0].([]interface{})
- if !ok {
- return fmt.Errorf("Invalid argument type found."), false
- }
- args1, ok := args[1].([]interface{})
- if !ok {
- return fmt.Errorf("Invalid argument type found."), false
- }
- arg1, ok := getFirstValidArg(args1).(bool)
- if !ok {
- return fmt.Errorf("Invalid argument type found."), false
- }
- if len(arg0) == 0 {
- return nil, true
- }
- if arg1 {
- for i := len(arg0) - 1; i >= 0; i-- {
- if arg0[i] != nil {
- return arg0[i], true
- }
- }
- }
- return arg0[len(arg0)-1], true
- },
- val: func(_ api.FunctionContext, args []ast.Expr) error {
- if err := ValidateLen(2, len(args)); err != nil {
- return err
- }
- if !ast.IsBooleanArg(args[1]) {
- return ProduceErrInfo(1, "bool")
- }
- return nil
- },
- check: returnNilIfHasAnyNil,
- }
- }
|