funcs_analytic.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // Copyright 2022-2023 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. "strconv"
  19. "github.com/lf-edge/ekuiper/pkg/api"
  20. "github.com/lf-edge/ekuiper/pkg/ast"
  21. "github.com/lf-edge/ekuiper/pkg/cast"
  22. )
  23. // registerAnalyticFunc registers the analytic functions
  24. // The last parameter of the function is always the partition key
  25. func registerAnalyticFunc() {
  26. builtins["changed_col"] = builtinFunc{
  27. fType: ast.FuncTypeScalar,
  28. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  29. ignoreNull, ok := args[0].(bool)
  30. if !ok {
  31. return fmt.Errorf("first arg is not a bool but got %v", args[0]), false
  32. }
  33. if ignoreNull && args[1] == nil {
  34. return nil, true
  35. }
  36. validData, ok := args[len(args)-2].(bool)
  37. if !ok {
  38. return fmt.Errorf("when arg is not a bool but got %v", args[len(args)-2]), false
  39. }
  40. if !validData {
  41. return nil, true
  42. }
  43. key := args[len(args)-1].(string)
  44. lv, err := ctx.GetState(key)
  45. if err != nil {
  46. return err, false
  47. }
  48. if !reflect.DeepEqual(args[1], lv) {
  49. err := ctx.PutState(key, args[1])
  50. if err != nil {
  51. return err, false
  52. }
  53. return args[1], true
  54. }
  55. return nil, true
  56. },
  57. val: func(_ api.FunctionContext, args []ast.Expr) error {
  58. if err := ValidateLen(2, len(args)); err != nil {
  59. return err
  60. }
  61. if ast.IsNumericArg(args[0]) || ast.IsTimeArg(args[0]) || ast.IsStringArg(args[0]) {
  62. return ProduceErrInfo(0, "boolean")
  63. }
  64. return nil
  65. },
  66. }
  67. builtins["had_changed"] = builtinFunc{
  68. fType: ast.FuncTypeScalar,
  69. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  70. l := len(args) - 2
  71. if l <= 1 {
  72. return fmt.Errorf("expect more than one arg but got %d", len(args)), false
  73. }
  74. validData, ok := args[len(args)-2].(bool)
  75. if !ok {
  76. return fmt.Errorf("when arg is not a bool but got %v", args[len(args)-2]), false
  77. }
  78. if !validData {
  79. return false, true
  80. }
  81. ignoreNull, ok := args[0].(bool)
  82. if !ok {
  83. return fmt.Errorf("first arg is not a bool but got %v", args[0]), false
  84. }
  85. key := args[len(args)-1].(string)
  86. paraLen := len(args) - 2
  87. result := false
  88. for i := 1; i < paraLen; i++ {
  89. v := args[i]
  90. k := key + strconv.Itoa(i)
  91. if ignoreNull && v == nil {
  92. continue
  93. }
  94. lv, err := ctx.GetState(k)
  95. if err != nil {
  96. return fmt.Errorf("error getting state for %s: %v", k, err), false
  97. }
  98. if !reflect.DeepEqual(v, lv) {
  99. result = true
  100. err := ctx.PutState(k, v)
  101. if err != nil {
  102. return fmt.Errorf("error setting state for %s: %v", k, err), false
  103. }
  104. }
  105. }
  106. return result, true
  107. },
  108. val: func(_ api.FunctionContext, args []ast.Expr) error {
  109. if len(args) <= 1 {
  110. return fmt.Errorf("expect more than one arg but got %d", len(args))
  111. }
  112. if ast.IsNumericArg(args[0]) || ast.IsTimeArg(args[0]) || ast.IsStringArg(args[0]) {
  113. return ProduceErrInfo(0, "bool")
  114. }
  115. return nil
  116. },
  117. }
  118. builtins["lag"] = builtinFunc{
  119. fType: ast.FuncTypeScalar,
  120. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  121. l := len(args) - 2
  122. if l != 1 && l != 2 && l != 3 {
  123. return fmt.Errorf("expect one two or three args but got %d", l), false
  124. }
  125. key := args[len(args)-1].(string)
  126. v, err := ctx.GetState(key)
  127. if err != nil {
  128. return fmt.Errorf("error getting state for %s: %v", key, err), false
  129. }
  130. validData, ok := args[len(args)-2].(bool)
  131. if !ok {
  132. return fmt.Errorf("when arg is not a bool but got %v", args[len(args)-2]), false
  133. }
  134. paraLen := len(args) - 2
  135. var rq *ringqueue = nil
  136. var rtnVal interface{} = nil
  137. // first time call, need create state for lag
  138. if v == nil {
  139. size := 0
  140. var dftVal interface{} = nil
  141. if paraLen == 3 {
  142. dftVal = args[2]
  143. }
  144. if paraLen == 1 {
  145. size = 1
  146. } else {
  147. size, err = cast.ToInt(args[1], cast.STRICT)
  148. if err != nil {
  149. return fmt.Errorf("error converting second arg %v to int: %v", args[1], err), false
  150. }
  151. }
  152. rq = newRingqueue(size)
  153. rq.fill(dftVal)
  154. err := ctx.PutState(key, rq)
  155. if err != nil {
  156. return fmt.Errorf("error setting state for %s: %v", key, err), false
  157. }
  158. } else {
  159. rq, _ = v.(*ringqueue)
  160. }
  161. if validData {
  162. rtnVal, _ = rq.fetch()
  163. rq.append(args[0])
  164. err := ctx.PutState(key, rq)
  165. if err != nil {
  166. return fmt.Errorf("error setting state for %s: %v", key, err), false
  167. }
  168. } else {
  169. rtnVal, _ = rq.peek()
  170. }
  171. return rtnVal, true
  172. },
  173. val: func(_ api.FunctionContext, args []ast.Expr) error {
  174. l := len(args)
  175. if l != 1 && l != 2 && l != 3 {
  176. return fmt.Errorf("expect one two or three args but got %d", l)
  177. }
  178. if l >= 2 {
  179. if ast.IsFloatArg(args[1]) || ast.IsTimeArg(args[1]) || ast.IsBooleanArg(args[1]) || ast.IsStringArg(args[1]) || ast.IsFieldRefArg(args[1]) {
  180. return ProduceErrInfo(1, "int")
  181. }
  182. if s, ok := args[1].(*ast.IntegerLiteral); ok {
  183. if s.Val < 0 {
  184. return fmt.Errorf("the index should not be a nagtive integer")
  185. }
  186. }
  187. }
  188. return nil
  189. },
  190. }
  191. builtins["latest"] = builtinFunc{
  192. fType: ast.FuncTypeScalar,
  193. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  194. l := len(args) - 2
  195. if l != 1 && l != 2 {
  196. return fmt.Errorf("expect one or two args but got %d", l), false
  197. }
  198. paraLen := len(args) - 2
  199. key := args[len(args)-1].(string)
  200. validData, ok := args[len(args)-2].(bool)
  201. if !ok {
  202. return fmt.Errorf("when arg is not a bool but got %v", args[len(args)-2]), false
  203. }
  204. // notice nil is ignored in latest
  205. if validData && args[0] != nil {
  206. ctx.PutState(key, args[0])
  207. return args[0], true
  208. } else {
  209. v, err := ctx.GetState(key)
  210. if err != nil {
  211. return fmt.Errorf("error getting state for %s: %v", key, err), false
  212. }
  213. if v == nil {
  214. if paraLen == 2 {
  215. return args[1], true
  216. } else {
  217. return nil, true
  218. }
  219. } else {
  220. return v, true
  221. }
  222. }
  223. },
  224. val: func(_ api.FunctionContext, args []ast.Expr) error {
  225. l := len(args)
  226. if l != 1 && l != 2 {
  227. return fmt.Errorf("expect one or two args but got %d", l)
  228. }
  229. return nil
  230. },
  231. }
  232. }