funcs_cols.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. "github.com/lf-edge/ekuiper/pkg/api"
  18. "github.com/lf-edge/ekuiper/pkg/ast"
  19. )
  20. type ResultCols map[string]interface{}
  21. // ColFunc Functions which will return columns directly instead of a map
  22. type ColFunc func(ctx api.FunctionContext, args []interface{}, keys []string) (ResultCols, error)
  23. func wrapColFunc(colFunc ColFunc) funcExe {
  24. return func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  25. keys, ok := args[len(args)-1].([]string)
  26. if !ok {
  27. return fmt.Errorf("the last arg is not the key list but got %v", args[len(args)-1]), false
  28. }
  29. r, err := colFunc(ctx, args[:len(args)-1], keys)
  30. if err != nil {
  31. return err, false
  32. }
  33. return r, true
  34. }
  35. }
  36. func registerColsFunc() {
  37. builtins["changed_cols"] = builtinFunc{
  38. fType: FuncTypeCols,
  39. exec: wrapColFunc(changedFunc),
  40. val: func(_ api.FunctionContext, args []ast.Expr) error {
  41. if len(args) <= 2 {
  42. return fmt.Errorf("expect more than two args but got %d", len(args))
  43. }
  44. if ast.IsNumericArg(args[0]) || ast.IsTimeArg(args[0]) || ast.IsBooleanArg(args[0]) {
  45. return ProduceErrInfo(0, "string")
  46. }
  47. if ast.IsNumericArg(args[1]) || ast.IsTimeArg(args[1]) || ast.IsStringArg(args[1]) {
  48. return ProduceErrInfo(1, "bool")
  49. }
  50. return nil
  51. },
  52. }
  53. }
  54. func changedFunc(ctx api.FunctionContext, args []interface{}, keys []string) (ResultCols, error) {
  55. // validation
  56. if len(args) <= 2 {
  57. return nil, fmt.Errorf("expect more than two args but got %d", len(args))
  58. }
  59. prefix, ok := args[0].(string)
  60. if !ok {
  61. return nil, fmt.Errorf("first arg is not a string but got %v", args[0])
  62. }
  63. ignoreNull, ok := args[1].(bool)
  64. if !ok {
  65. return nil, fmt.Errorf("second arg is not a bool but got %v", args[1])
  66. }
  67. if len(args) != len(keys) {
  68. return nil, fmt.Errorf("the length of keys %d does not match the args %d", len(keys), len(args)-2)
  69. }
  70. var r ResultCols
  71. for i := 2; i < len(args); i++ {
  72. k := keys[i]
  73. v := args[i]
  74. if ignoreNull && v == nil {
  75. continue
  76. }
  77. lv, err := ctx.GetState(k)
  78. if err != nil {
  79. return nil, err
  80. }
  81. if lv != v {
  82. if r == nil {
  83. r = make(ResultCols)
  84. }
  85. r[prefix+k] = v
  86. err := ctx.PutState(k, v)
  87. if err != nil {
  88. return nil, err
  89. }
  90. }
  91. }
  92. return r, nil
  93. }