funcs_str.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. "bytes"
  17. "fmt"
  18. "regexp"
  19. "strings"
  20. "unicode"
  21. "unicode/utf8"
  22. "github.com/lf-edge/ekuiper/pkg/api"
  23. "github.com/lf-edge/ekuiper/pkg/ast"
  24. "github.com/lf-edge/ekuiper/pkg/cast"
  25. )
  26. func registerStrFunc() {
  27. builtins["concat"] = builtinFunc{
  28. fType: ast.FuncTypeScalar,
  29. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  30. var b bytes.Buffer
  31. for _, arg := range args {
  32. b.WriteString(cast.ToStringAlways(arg))
  33. }
  34. return b.String(), true
  35. },
  36. val: func(_ api.FunctionContext, args []ast.Expr) error {
  37. if len(args) == 0 {
  38. return fmt.Errorf("The arguments should be at least one.")
  39. }
  40. for i, a := range args {
  41. if ast.IsNumericArg(a) || ast.IsTimeArg(a) || ast.IsBooleanArg(a) {
  42. return ProduceErrInfo(i, "string")
  43. }
  44. }
  45. return nil
  46. },
  47. }
  48. builtins["endswith"] = builtinFunc{
  49. fType: ast.FuncTypeScalar,
  50. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  51. arg0, arg1 := cast.ToStringAlways(args[0]), cast.ToStringAlways(args[1])
  52. return strings.HasSuffix(arg0, arg1), true
  53. },
  54. val: ValidateTwoStrArg,
  55. check: returnFalseIfHasAnyNil,
  56. }
  57. builtins["indexof"] = builtinFunc{
  58. fType: ast.FuncTypeScalar,
  59. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  60. if args[0] == nil || args[1] == nil {
  61. return -1, true
  62. }
  63. arg0, arg1 := cast.ToStringAlways(args[0]), cast.ToStringAlways(args[1])
  64. return strings.Index(arg0, arg1), true
  65. },
  66. val: ValidateTwoStrArg,
  67. }
  68. builtins["length"] = builtinFunc{
  69. fType: ast.FuncTypeScalar,
  70. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  71. arg0 := cast.ToStringAlways(args[0])
  72. switch v := args[0].(type) {
  73. case []interface{}:
  74. return len(v), true
  75. case map[string]interface{}:
  76. return len(v), true
  77. case nil:
  78. return 0, true
  79. default:
  80. }
  81. return utf8.RuneCountInString(arg0), true
  82. },
  83. val: ValidateOneArg,
  84. check: return0IfHasAnyNil,
  85. }
  86. builtins["lower"] = builtinFunc{
  87. fType: ast.FuncTypeScalar,
  88. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  89. arg0 := cast.ToStringAlways(args[0])
  90. return strings.ToLower(arg0), true
  91. },
  92. val: ValidateOneStrArg,
  93. check: returnNilIfHasAnyNil,
  94. }
  95. builtins["lpad"] = builtinFunc{
  96. fType: ast.FuncTypeScalar,
  97. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  98. arg0 := cast.ToStringAlways(args[0])
  99. arg1, err := cast.ToInt(args[1], cast.STRICT)
  100. if err != nil {
  101. return err, false
  102. }
  103. return strings.Repeat(" ", arg1) + arg0, true
  104. },
  105. val: ValidateOneStrOneInt,
  106. check: returnNilIfHasAnyNil,
  107. }
  108. builtins["ltrim"] = builtinFunc{
  109. fType: ast.FuncTypeScalar,
  110. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  111. arg0 := cast.ToStringAlways(args[0])
  112. return strings.TrimLeftFunc(arg0, unicode.IsSpace), true
  113. },
  114. val: ValidateOneStrArg,
  115. check: returnNilIfHasAnyNil,
  116. }
  117. builtins["numbytes"] = builtinFunc{
  118. fType: ast.FuncTypeScalar,
  119. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  120. arg0 := cast.ToStringAlways(args[0])
  121. return len(arg0), true
  122. },
  123. val: ValidateOneStrArg,
  124. check: return0IfHasAnyNil,
  125. }
  126. builtins["format_time"] = builtinFunc{
  127. fType: ast.FuncTypeScalar,
  128. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  129. arg0, err := cast.InterfaceToTime(args[0], "")
  130. if err != nil {
  131. return err, false
  132. }
  133. arg1 := cast.ToStringAlways(args[1])
  134. if s, err := cast.FormatTime(arg0, arg1); err == nil {
  135. return s, true
  136. } else {
  137. return err, false
  138. }
  139. },
  140. val: func(_ api.FunctionContext, args []ast.Expr) error {
  141. if err := ValidateLen(2, len(args)); err != nil {
  142. return err
  143. }
  144. if ast.IsNumericArg(args[0]) || ast.IsStringArg(args[0]) || ast.IsBooleanArg(args[0]) {
  145. return ProduceErrInfo(0, "datetime")
  146. }
  147. if ast.IsNumericArg(args[1]) || ast.IsTimeArg(args[1]) || ast.IsBooleanArg(args[1]) {
  148. return ProduceErrInfo(1, "string")
  149. }
  150. return nil
  151. },
  152. check: returnNilIfHasAnyNil,
  153. }
  154. builtins["regexp_matches"] = builtinFunc{
  155. fType: ast.FuncTypeScalar,
  156. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  157. arg0, arg1 := cast.ToStringAlways(args[0]), cast.ToStringAlways(args[1])
  158. if matched, err := regexp.MatchString(arg1, arg0); err != nil {
  159. return err, false
  160. } else {
  161. return matched, true
  162. }
  163. },
  164. val: ValidateTwoStrArg,
  165. check: returnFalseIfHasAnyNil,
  166. }
  167. builtins["regexp_replace"] = builtinFunc{
  168. fType: ast.FuncTypeScalar,
  169. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  170. arg0, arg1, arg2 := cast.ToStringAlways(args[0]), cast.ToStringAlways(args[1]), cast.ToStringAlways(args[2])
  171. if re, err := regexp.Compile(arg1); err != nil {
  172. return err, false
  173. } else {
  174. return re.ReplaceAllString(arg0, arg2), true
  175. }
  176. },
  177. val: func(_ api.FunctionContext, args []ast.Expr) error {
  178. if err := ValidateLen(3, len(args)); err != nil {
  179. return err
  180. }
  181. for i := 0; i < 3; i++ {
  182. if ast.IsNumericArg(args[i]) || ast.IsTimeArg(args[i]) || ast.IsBooleanArg(args[i]) {
  183. return ProduceErrInfo(i, "string")
  184. }
  185. }
  186. return nil
  187. },
  188. check: returnNilIfHasAnyNil,
  189. }
  190. builtins["regexp_substr"] = builtinFunc{
  191. fType: ast.FuncTypeScalar,
  192. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  193. arg0, arg1 := cast.ToStringAlways(args[0]), cast.ToStringAlways(args[1])
  194. if re, err := regexp.Compile(arg1); err != nil {
  195. return err, false
  196. } else {
  197. return re.FindString(arg0), true
  198. }
  199. },
  200. val: ValidateTwoStrArg,
  201. check: returnNilIfHasAnyNil,
  202. }
  203. builtins["rpad"] = builtinFunc{
  204. fType: ast.FuncTypeScalar,
  205. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  206. arg0 := cast.ToStringAlways(args[0])
  207. arg1, err := cast.ToInt(args[1], cast.STRICT)
  208. if err != nil {
  209. return err, false
  210. }
  211. return arg0 + strings.Repeat(" ", arg1), true
  212. },
  213. val: ValidateOneStrOneInt,
  214. check: returnNilIfHasAnyNil,
  215. }
  216. builtins["rtrim"] = builtinFunc{
  217. fType: ast.FuncTypeScalar,
  218. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  219. arg0 := cast.ToStringAlways(args[0])
  220. return strings.TrimRightFunc(arg0, unicode.IsSpace), true
  221. },
  222. val: ValidateOneStrArg,
  223. check: returnNilIfHasAnyNil,
  224. }
  225. builtins["substring"] = builtinFunc{
  226. fType: ast.FuncTypeScalar,
  227. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  228. arg0 := cast.ToStringAlways(args[0])
  229. arg1, err := cast.ToInt(args[1], cast.STRICT)
  230. if err != nil {
  231. return err, false
  232. }
  233. if arg1 < 0 {
  234. return fmt.Errorf("start index must be a positive number"), false
  235. }
  236. if len(args) > 2 {
  237. arg2, err := cast.ToInt(args[2], cast.STRICT)
  238. if err != nil {
  239. return err, false
  240. }
  241. if arg2 < 0 {
  242. return fmt.Errorf("end index must be a positive number"), false
  243. }
  244. if arg1 > arg2 {
  245. return fmt.Errorf("start index must be smaller than end index"), false
  246. }
  247. if arg1 > len(arg0) {
  248. return "", true
  249. }
  250. if arg2 > len(arg0) {
  251. return arg0[arg1:], true
  252. }
  253. return arg0[arg1:arg2], true
  254. } else {
  255. if arg1 > len(arg0) {
  256. return "", true
  257. }
  258. return arg0[arg1:], true
  259. }
  260. },
  261. val: func(_ api.FunctionContext, args []ast.Expr) error {
  262. l := len(args)
  263. if l != 2 && l != 3 {
  264. return fmt.Errorf("the arguments for substring should be 2 or 3")
  265. }
  266. if ast.IsNumericArg(args[0]) || ast.IsTimeArg(args[0]) || ast.IsBooleanArg(args[0]) {
  267. return ProduceErrInfo(0, "string")
  268. }
  269. for i := 1; i < l; i++ {
  270. if ast.IsFloatArg(args[i]) || ast.IsTimeArg(args[i]) || ast.IsBooleanArg(args[i]) || ast.IsStringArg(args[i]) {
  271. return ProduceErrInfo(i, "int")
  272. }
  273. }
  274. if s, ok := args[1].(*ast.IntegerLiteral); ok {
  275. sv := s.Val
  276. if sv < 0 {
  277. return fmt.Errorf("The start index should not be a nagtive integer.")
  278. }
  279. if l == 3 {
  280. if e, ok1 := args[2].(*ast.IntegerLiteral); ok1 {
  281. ev := e.Val
  282. if ev < sv {
  283. return fmt.Errorf("The end index should be larger than start index.")
  284. }
  285. }
  286. }
  287. }
  288. return nil
  289. },
  290. check: returnNilIfHasAnyNil,
  291. }
  292. builtins["startswith"] = builtinFunc{
  293. fType: ast.FuncTypeScalar,
  294. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  295. arg0, arg1 := cast.ToStringAlways(args[0]), cast.ToStringAlways(args[1])
  296. return strings.HasPrefix(arg0, arg1), true
  297. },
  298. val: ValidateTwoStrArg,
  299. check: returnFalseIfHasAnyNil,
  300. }
  301. builtins["split_value"] = builtinFunc{
  302. fType: ast.FuncTypeScalar,
  303. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  304. arg0, arg1 := cast.ToStringAlways(args[0]), cast.ToStringAlways(args[1])
  305. ss := strings.Split(arg0, arg1)
  306. v, _ := cast.ToInt(args[2], cast.STRICT)
  307. if v > (len(ss) - 1) {
  308. return fmt.Errorf("%d out of index array (size = %d)", v, len(ss)), false
  309. } else {
  310. return ss[v], true
  311. }
  312. },
  313. val: func(_ api.FunctionContext, args []ast.Expr) error {
  314. l := len(args)
  315. if l != 3 {
  316. return fmt.Errorf("the arguments for split_value should be 3")
  317. }
  318. if ast.IsNumericArg(args[0]) || ast.IsTimeArg(args[0]) || ast.IsBooleanArg(args[0]) {
  319. return ProduceErrInfo(0, "string")
  320. }
  321. if ast.IsNumericArg(args[1]) || ast.IsTimeArg(args[1]) || ast.IsBooleanArg(args[1]) {
  322. return ProduceErrInfo(1, "string")
  323. }
  324. if ast.IsFloatArg(args[2]) || ast.IsTimeArg(args[2]) || ast.IsBooleanArg(args[2]) || ast.IsStringArg(args[2]) {
  325. return ProduceErrInfo(2, "int")
  326. }
  327. if s, ok := args[2].(*ast.IntegerLiteral); ok {
  328. if s.Val < 0 {
  329. return fmt.Errorf("The index should not be a nagtive integer.")
  330. }
  331. }
  332. return nil
  333. },
  334. check: returnNilIfHasAnyNil,
  335. }
  336. builtins["trim"] = builtinFunc{
  337. fType: ast.FuncTypeScalar,
  338. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  339. arg0 := cast.ToStringAlways(args[0])
  340. return strings.TrimSpace(arg0), true
  341. },
  342. val: ValidateOneStrArg,
  343. check: returnNilIfHasAnyNil,
  344. }
  345. builtins["upper"] = builtinFunc{
  346. fType: ast.FuncTypeScalar,
  347. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  348. arg0 := cast.ToStringAlways(args[0])
  349. return strings.ToUpper(arg0), true
  350. },
  351. val: ValidateOneStrArg,
  352. check: returnNilIfHasAnyNil,
  353. }
  354. }