funcs_agg.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. "github.com/montanaflynn/stats"
  18. "github.com/lf-edge/ekuiper/pkg/api"
  19. "github.com/lf-edge/ekuiper/pkg/ast"
  20. "github.com/lf-edge/ekuiper/pkg/cast"
  21. )
  22. func registerAggFunc() {
  23. builtins["avg"] = builtinFunc{
  24. fType: ast.FuncTypeAgg,
  25. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  26. arg0 := args[0].([]interface{})
  27. c := getCount(arg0)
  28. if c > 0 {
  29. v := getFirstValidArg(arg0)
  30. switch v.(type) {
  31. case int, int64:
  32. if r, err := sliceIntTotal(arg0); err != nil {
  33. return err, false
  34. } else {
  35. return r / int64(c), true
  36. }
  37. case float64:
  38. if r, err := sliceFloatTotal(arg0); err != nil {
  39. return err, false
  40. } else {
  41. return r / float64(c), true
  42. }
  43. case nil:
  44. return nil, true
  45. default:
  46. return fmt.Errorf("run avg function error: found invalid arg %[1]T(%[1]v)", v), false
  47. }
  48. }
  49. return nil, true
  50. },
  51. val: ValidateOneNumberArg,
  52. check: returnNilIfHasAnyNil,
  53. }
  54. builtins["count"] = builtinFunc{
  55. fType: ast.FuncTypeAgg,
  56. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  57. arg0 := args[0].([]interface{})
  58. return getCount(arg0), true
  59. },
  60. val: ValidateOneArg,
  61. check: returnNilIfHasAnyNil,
  62. }
  63. builtins["max"] = builtinFunc{
  64. fType: ast.FuncTypeAgg,
  65. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  66. arg0 := args[0].([]interface{})
  67. return max(arg0)
  68. },
  69. val: ValidateOneNumberArg,
  70. check: returnNilIfHasAnyNil,
  71. }
  72. builtins["min"] = builtinFunc{
  73. fType: ast.FuncTypeAgg,
  74. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  75. arg0 := args[0].([]interface{})
  76. return min(arg0)
  77. },
  78. val: ValidateOneNumberArg,
  79. check: returnNilIfHasAnyNil,
  80. }
  81. builtins["sum"] = builtinFunc{
  82. fType: ast.FuncTypeAgg,
  83. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  84. arg0 := args[0].([]interface{})
  85. if len(arg0) > 0 {
  86. v := getFirstValidArg(arg0)
  87. switch v.(type) {
  88. case int, int64:
  89. if r, err := sliceIntTotal(arg0); err != nil {
  90. return err, false
  91. } else {
  92. return r, true
  93. }
  94. case float64:
  95. if r, err := sliceFloatTotal(arg0); err != nil {
  96. return err, false
  97. } else {
  98. return r, true
  99. }
  100. case nil:
  101. return nil, true
  102. default:
  103. return fmt.Errorf("run sum function error: found invalid arg %[1]T(%[1]v)", v), false
  104. }
  105. }
  106. return nil, true
  107. },
  108. val: ValidateOneNumberArg,
  109. check: returnNilIfHasAnyNil,
  110. }
  111. builtins["collect"] = builtinFunc{
  112. fType: ast.FuncTypeAgg,
  113. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  114. if len(args) > 0 {
  115. return args[0], true
  116. }
  117. return make([]interface{}, 0), true
  118. },
  119. val: ValidateOneArg,
  120. }
  121. builtins["merge_agg"] = builtinFunc{
  122. fType: ast.FuncTypeAgg,
  123. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  124. data, ok := args[0].([]interface{})
  125. if ok {
  126. result := make(map[string]interface{})
  127. for _, ele := range data {
  128. if m, ok := ele.(map[string]interface{}); ok {
  129. for k, v := range m {
  130. result[k] = v
  131. }
  132. }
  133. }
  134. return result, true
  135. }
  136. return nil, true
  137. },
  138. val: ValidateOneArg,
  139. check: returnNilIfHasAnyNil,
  140. }
  141. builtins["deduplicate"] = builtinFunc{
  142. fType: ast.FuncTypeAgg,
  143. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  144. v1, ok1 := args[0].([]interface{})
  145. v2, ok2 := args[1].([]interface{})
  146. v3a, ok3 := args[2].([]interface{})
  147. if ok1 && ok2 && ok3 && len(v3a) > 0 {
  148. v3, ok4 := getFirstValidArg(v3a).(bool)
  149. if ok4 {
  150. if r, err := dedup(v1, v2, v3); err != nil {
  151. return err, false
  152. } else {
  153. return r, true
  154. }
  155. }
  156. }
  157. return fmt.Errorf("Invalid argument type found."), false
  158. },
  159. val: func(_ api.FunctionContext, args []ast.Expr) error {
  160. if err := ValidateLen(2, len(args)); err != nil {
  161. return err
  162. }
  163. if !ast.IsBooleanArg(args[1]) {
  164. return ProduceErrInfo(1, "bool")
  165. }
  166. return nil
  167. },
  168. check: returnNilIfHasAnyNil,
  169. }
  170. builtins["stddev"] = builtinFunc{
  171. fType: ast.FuncTypeAgg,
  172. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  173. arg0 := args[0].([]interface{})
  174. if len(arg0) > 0 {
  175. float64Slice, err := cast.ToFloat64Slice(arg0, cast.CONVERT_SAMEKIND)
  176. if err != nil {
  177. return fmt.Errorf("requires float64 slice but found %[1]T(%[1]v)", arg0), false
  178. }
  179. deviation, err := stats.StandardDeviation(float64Slice)
  180. if err != nil {
  181. if err == stats.EmptyInputErr {
  182. return nil, true
  183. }
  184. return fmt.Errorf("StandardDeviation exec with error: %v", err), false
  185. }
  186. return deviation, true
  187. }
  188. return nil, true
  189. },
  190. val: ValidateOneNumberArg,
  191. check: returnNilIfHasAnyNil,
  192. }
  193. builtins["stddevs"] = builtinFunc{
  194. fType: ast.FuncTypeAgg,
  195. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  196. arg0 := args[0].([]interface{})
  197. if len(arg0) > 0 {
  198. float64Slice, err := cast.ToFloat64Slice(arg0, cast.CONVERT_SAMEKIND)
  199. if err != nil {
  200. return fmt.Errorf("requires float64 slice but found %[1]T(%[1]v)", arg0), false
  201. }
  202. deviation, err := stats.StandardDeviationSample(float64Slice)
  203. if err != nil {
  204. if err == stats.EmptyInputErr {
  205. return nil, true
  206. }
  207. return fmt.Errorf("StandardDeviationSample exec with error: %v", err), false
  208. }
  209. return deviation, true
  210. }
  211. return nil, true
  212. },
  213. val: ValidateOneNumberArg,
  214. check: returnNilIfHasAnyNil,
  215. }
  216. builtins["var"] = builtinFunc{
  217. fType: ast.FuncTypeAgg,
  218. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  219. arg0 := args[0].([]interface{})
  220. if len(arg0) > 0 {
  221. float64Slice, err := cast.ToFloat64Slice(arg0, cast.CONVERT_SAMEKIND)
  222. if err != nil {
  223. return fmt.Errorf("requires float64 slice but found %[1]T(%[1]v)", arg0), false
  224. }
  225. deviation, err := stats.Variance(float64Slice)
  226. if err != nil {
  227. if err == stats.EmptyInputErr {
  228. return nil, true
  229. }
  230. return fmt.Errorf("PopulationVariance exec with error: %v", err), false
  231. }
  232. return deviation, true
  233. }
  234. return nil, true
  235. },
  236. val: ValidateOneNumberArg,
  237. check: returnNilIfHasAnyNil,
  238. }
  239. builtins["vars"] = builtinFunc{
  240. fType: ast.FuncTypeAgg,
  241. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  242. arg0 := args[0].([]interface{})
  243. if len(arg0) > 0 {
  244. float64Slice, err := cast.ToFloat64Slice(arg0, cast.CONVERT_SAMEKIND)
  245. if err != nil {
  246. return fmt.Errorf("requires float64 slice but found %[1]T(%[1]v)", arg0), false
  247. }
  248. deviation, err := stats.SampleVariance(float64Slice)
  249. if err != nil {
  250. if err == stats.EmptyInputErr {
  251. return nil, true
  252. }
  253. return fmt.Errorf("SampleVariance exec with error: %v", err), false
  254. }
  255. return deviation, true
  256. }
  257. return nil, true
  258. },
  259. val: ValidateOneNumberArg,
  260. check: returnNilIfHasAnyNil,
  261. }
  262. builtins["percentile_cont"] = builtinFunc{
  263. fType: ast.FuncTypeAgg,
  264. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  265. if err := ValidateLen(2, len(args)); err != nil {
  266. return err, false
  267. }
  268. var arg1Float64 float64 = 1
  269. arg0 := args[0].([]interface{})
  270. arg1 := args[1].([]interface{})
  271. if len(arg1) > 0 {
  272. v1 := getFirstValidArg(arg1)
  273. val, err := cast.ToFloat64(v1, cast.CONVERT_SAMEKIND)
  274. if err != nil {
  275. return fmt.Errorf("the second parameter requires float64 but found %[1]T(%[1]v)", arg1), false
  276. }
  277. arg1Float64 = val
  278. }
  279. if len(arg0) > 0 {
  280. float64Slice, err := cast.ToFloat64Slice(arg0, cast.CONVERT_SAMEKIND)
  281. if err != nil {
  282. return fmt.Errorf("requires float64 slice but found %[1]T(%[1]v)", arg0), false
  283. }
  284. deviation, err := stats.Percentile(float64Slice, arg1Float64*100)
  285. if err != nil {
  286. if err == stats.EmptyInputErr {
  287. return nil, true
  288. }
  289. return fmt.Errorf("percentile exec with error: %v", err), false
  290. }
  291. return deviation, true
  292. }
  293. return nil, true
  294. },
  295. val: ValidateTwoNumberArg,
  296. check: returnNilIfHasAnyNil,
  297. }
  298. builtins["percentile_disc"] = builtinFunc{
  299. fType: ast.FuncTypeAgg,
  300. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  301. if err := ValidateLen(2, len(args)); err != nil {
  302. return err, false
  303. }
  304. var arg1Float64 float64 = 1
  305. arg0 := args[0].([]interface{})
  306. arg1 := args[1].([]interface{})
  307. if len(arg1) > 0 {
  308. v1 := getFirstValidArg(arg1)
  309. val, err := cast.ToFloat64(v1, cast.CONVERT_SAMEKIND)
  310. if err != nil {
  311. return fmt.Errorf("the second parameter requires float64 but found %[1]T(%[1]v)", arg1), false
  312. }
  313. arg1Float64 = val
  314. }
  315. if len(arg0) > 0 {
  316. float64Slice, err := cast.ToFloat64Slice(arg0, cast.CONVERT_SAMEKIND)
  317. if err != nil {
  318. return fmt.Errorf("requires float64 slice but found %[1]T(%[1]v)", arg0), false
  319. }
  320. deviation, err := stats.PercentileNearestRank(float64Slice, arg1Float64*100)
  321. if err != nil {
  322. if err == stats.EmptyInputErr {
  323. return nil, true
  324. }
  325. return fmt.Errorf("PopulationVariance exec with error: %v", err), false
  326. }
  327. return deviation, true
  328. }
  329. return nil, true
  330. },
  331. val: ValidateTwoNumberArg,
  332. check: returnNilIfHasAnyNil,
  333. }
  334. builtins["last_value"] = builtinFunc{
  335. fType: ast.FuncTypeAgg,
  336. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  337. arg0, ok := args[0].([]interface{})
  338. if !ok {
  339. return fmt.Errorf("Invalid argument type found."), false
  340. }
  341. args1, ok := args[1].([]interface{})
  342. if !ok {
  343. return fmt.Errorf("Invalid argument type found."), false
  344. }
  345. arg1, ok := getFirstValidArg(args1).(bool)
  346. if !ok {
  347. return fmt.Errorf("Invalid argument type found."), false
  348. }
  349. if len(arg0) == 0 {
  350. return nil, true
  351. }
  352. if arg1 {
  353. for i := len(arg0) - 1; i >= 0; i-- {
  354. if arg0[i] != nil {
  355. return arg0[i], true
  356. }
  357. }
  358. }
  359. return arg0[len(arg0)-1], true
  360. },
  361. val: func(_ api.FunctionContext, args []ast.Expr) error {
  362. if err := ValidateLen(2, len(args)); err != nil {
  363. return err
  364. }
  365. if !ast.IsBooleanArg(args[1]) {
  366. return ProduceErrInfo(1, "bool")
  367. }
  368. return nil
  369. },
  370. check: returnNilIfHasAnyNil,
  371. }
  372. }