funcs_cols.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2022 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package function
  15. import (
  16. "fmt"
  17. "reflect"
  18. "github.com/lf-edge/ekuiper/pkg/api"
  19. "github.com/lf-edge/ekuiper/pkg/ast"
  20. )
  21. type ResultCols map[string]interface{}
  22. // ColFunc Functions which will return columns directly instead of a map
  23. type ColFunc func(ctx api.FunctionContext, args []interface{}, keys []string) (ResultCols, error)
  24. func wrapColFunc(colFunc ColFunc) funcExe {
  25. return func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  26. keys, ok := args[len(args)-1].([]string)
  27. if !ok {
  28. return fmt.Errorf("the last arg is not the key list but got %v", args[len(args)-1]), false
  29. }
  30. r, err := colFunc(ctx, args[:len(args)-1], keys)
  31. if err != nil {
  32. return err, false
  33. }
  34. return r, true
  35. }
  36. }
  37. func registerColsFunc() {
  38. builtins["changed_cols"] = builtinFunc{
  39. fType: ast.FuncTypeCols,
  40. exec: wrapColFunc(changedFunc),
  41. val: func(_ api.FunctionContext, args []ast.Expr) error {
  42. if len(args) <= 2 {
  43. return fmt.Errorf("expect more than two args but got %d", len(args))
  44. }
  45. if ast.IsNumericArg(args[0]) || ast.IsTimeArg(args[0]) || ast.IsBooleanArg(args[0]) {
  46. return ProduceErrInfo(0, "string")
  47. }
  48. if ast.IsNumericArg(args[1]) || ast.IsTimeArg(args[1]) || ast.IsStringArg(args[1]) {
  49. return ProduceErrInfo(1, "bool")
  50. }
  51. return nil
  52. },
  53. }
  54. }
  55. func changedFunc(ctx api.FunctionContext, args []interface{}, keys []string) (ResultCols, error) {
  56. // validation
  57. if len(args) <= 2 {
  58. return nil, fmt.Errorf("expect more than two args but got %d", len(args))
  59. }
  60. prefix, ok := args[0].(string)
  61. if !ok {
  62. return nil, fmt.Errorf("first arg is not a string but got %v", args[0])
  63. }
  64. ignoreNull, ok := args[1].(bool)
  65. if !ok {
  66. return nil, fmt.Errorf("second arg is not a bool but got %v", args[1])
  67. }
  68. if len(args) != len(keys) {
  69. return nil, fmt.Errorf("the length of keys %d does not match the args %d", len(keys), len(args)-2)
  70. }
  71. var r ResultCols
  72. for i := 2; i < len(args); i++ {
  73. k := keys[i]
  74. v := args[i]
  75. if ignoreNull && v == nil {
  76. continue
  77. }
  78. lv, err := ctx.GetState(k)
  79. if err != nil {
  80. return nil, err
  81. }
  82. if !reflect.DeepEqual(v, lv) {
  83. if r == nil {
  84. r = make(ResultCols)
  85. }
  86. r[prefix+k] = v
  87. err := ctx.PutState(k, v)
  88. if err != nil {
  89. return nil, err
  90. }
  91. }
  92. }
  93. return r, nil
  94. }