common_array_funcs.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // Copyright 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/lf-edge/ekuiper/pkg/cast"
  18. )
  19. // The functions here are used to implement the array functions to be referred in
  20. // 1. Aggregate function
  21. // 2. Array function
  22. func max(arr []interface{}) (interface{}, bool) {
  23. if len(arr) > 0 {
  24. v := getFirstValidArg(arr)
  25. switch t := v.(type) {
  26. case int:
  27. if r, err := sliceIntMax(arr, int64(t)); err != nil {
  28. return err, false
  29. } else {
  30. return r, true
  31. }
  32. case int64:
  33. if r, err := sliceIntMax(arr, t); err != nil {
  34. return err, false
  35. } else {
  36. return r, true
  37. }
  38. case float64:
  39. if r, err := sliceFloatMax(arr, t); err != nil {
  40. return err, false
  41. } else {
  42. return r, true
  43. }
  44. case string:
  45. if r, err := sliceStringMax(arr, t); err != nil {
  46. return err, false
  47. } else {
  48. return r, true
  49. }
  50. case nil:
  51. return nil, true
  52. default:
  53. return fmt.Errorf("found invalid arg %[1]T(%[1]v)", v), false
  54. }
  55. }
  56. return nil, true
  57. }
  58. func min(arr []interface{}) (interface{}, bool) {
  59. if len(arr) > 0 {
  60. v := getFirstValidArg(arr)
  61. switch t := v.(type) {
  62. case int:
  63. if r, err := sliceIntMin(arr, int64(t)); err != nil {
  64. return err, false
  65. } else {
  66. return r, true
  67. }
  68. case int64:
  69. if r, err := sliceIntMin(arr, t); err != nil {
  70. return err, false
  71. } else {
  72. return r, true
  73. }
  74. case float64:
  75. if r, err := sliceFloatMin(arr, t); err != nil {
  76. return err, false
  77. } else {
  78. return r, true
  79. }
  80. case string:
  81. if r, err := sliceStringMin(arr, t); err != nil {
  82. return err, false
  83. } else {
  84. return r, true
  85. }
  86. case nil:
  87. return nil, true
  88. default:
  89. return fmt.Errorf("found invalid arg %[1]T(%[1]v)", v), false
  90. }
  91. }
  92. return nil, true
  93. }
  94. func getCount(s []interface{}) int {
  95. c := 0
  96. for _, v := range s {
  97. if v != nil {
  98. c++
  99. }
  100. }
  101. return c
  102. }
  103. func getFirstValidArg(s []interface{}) interface{} {
  104. for _, v := range s {
  105. if v != nil {
  106. return v
  107. }
  108. }
  109. return nil
  110. }
  111. func sliceIntTotal(s []interface{}) (int64, error) {
  112. var total int64
  113. for _, v := range s {
  114. if v == nil {
  115. continue
  116. }
  117. vi, err := cast.ToInt64(v, cast.CONVERT_SAMEKIND)
  118. if err == nil {
  119. total += vi
  120. } else if v != nil {
  121. return 0, fmt.Errorf("requires int but found %[1]T(%[1]v)", v)
  122. }
  123. }
  124. return total, nil
  125. }
  126. func sliceFloatTotal(s []interface{}) (float64, error) {
  127. var total float64
  128. for _, v := range s {
  129. if v == nil {
  130. continue
  131. }
  132. if vf, ok := v.(float64); ok {
  133. total += vf
  134. } else if v != nil {
  135. return 0, fmt.Errorf("requires float64 but found %[1]T(%[1]v)", v)
  136. }
  137. }
  138. return total, nil
  139. }
  140. func sliceIntMax(s []interface{}, max int64) (int64, error) {
  141. for _, v := range s {
  142. if v == nil {
  143. continue
  144. }
  145. vi, err := cast.ToInt64(v, cast.CONVERT_SAMEKIND)
  146. if err == nil {
  147. if vi > max {
  148. max = vi
  149. }
  150. } else if v != nil {
  151. return 0, fmt.Errorf("requires int64 but found %[1]T(%[1]v)", v)
  152. }
  153. }
  154. return max, nil
  155. }
  156. func sliceFloatMax(s []interface{}, max float64) (float64, error) {
  157. for _, v := range s {
  158. if v == nil {
  159. continue
  160. }
  161. if vf, ok := v.(float64); ok {
  162. if max < vf {
  163. max = vf
  164. }
  165. } else if v != nil {
  166. return 0, fmt.Errorf("requires float64 but found %[1]T(%[1]v)", v)
  167. }
  168. }
  169. return max, nil
  170. }
  171. func sliceStringMax(s []interface{}, max string) (string, error) {
  172. for _, v := range s {
  173. if v == nil {
  174. continue
  175. }
  176. if vs, ok := v.(string); ok {
  177. if max < vs {
  178. max = vs
  179. }
  180. } else if v != nil {
  181. return "", fmt.Errorf("requires string but found %[1]T(%[1]v)", v)
  182. }
  183. }
  184. return max, nil
  185. }
  186. func sliceIntMin(s []interface{}, min int64) (int64, error) {
  187. for _, v := range s {
  188. if v == nil {
  189. continue
  190. }
  191. vi, err := cast.ToInt64(v, cast.CONVERT_SAMEKIND)
  192. if err == nil {
  193. if vi < min {
  194. min = vi
  195. }
  196. } else if v != nil {
  197. return 0, fmt.Errorf("requires int64 but found %[1]T(%[1]v)", v)
  198. }
  199. }
  200. return min, nil
  201. }
  202. func sliceFloatMin(s []interface{}, min float64) (float64, error) {
  203. for _, v := range s {
  204. if v == nil {
  205. continue
  206. }
  207. if vf, ok := v.(float64); ok {
  208. if min > vf {
  209. min = vf
  210. }
  211. } else if v != nil {
  212. return 0, fmt.Errorf("requires float64 but found %[1]T(%[1]v)", v)
  213. }
  214. }
  215. return min, nil
  216. }
  217. func sliceStringMin(s []interface{}, min string) (string, error) {
  218. for _, v := range s {
  219. if v == nil {
  220. continue
  221. }
  222. if vs, ok := v.(string); ok {
  223. if vs < min {
  224. min = vs
  225. }
  226. } else if v != nil {
  227. return "", fmt.Errorf("requires string but found %[1]T(%[1]v)", v)
  228. }
  229. }
  230. return min, nil
  231. }
  232. func dedup(r []interface{}, col []interface{}, all bool) (interface{}, error) {
  233. keyset := make(map[string]bool)
  234. result := make([]interface{}, 0)
  235. for i, m := range col {
  236. key := fmt.Sprintf("%v", m)
  237. if _, ok := keyset[key]; !ok {
  238. if all {
  239. result = append(result, r[i])
  240. } else if i == len(col)-1 {
  241. result = append(result, r[i])
  242. }
  243. keyset[key] = true
  244. }
  245. }
  246. if !all {
  247. if len(result) == 0 {
  248. return nil, nil
  249. } else {
  250. return result[0], nil
  251. }
  252. } else {
  253. return result, nil
  254. }
  255. }