functions.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package xsql
  2. import (
  3. "github.com/emqx/kuiper/common"
  4. "github.com/emqx/kuiper/plugins"
  5. "github.com/emqx/kuiper/xstream/api"
  6. "strings"
  7. )
  8. type FunctionValuer struct{}
  9. func (*FunctionValuer) Value(key string) (interface{}, bool) {
  10. return nil, false
  11. }
  12. func (*FunctionValuer) Meta(key string) (interface{}, bool) {
  13. return nil, false
  14. }
  15. var aggFuncMap = map[string]string{"avg": "",
  16. "count": "",
  17. "max": "", "min": "",
  18. "sum": "",
  19. }
  20. var mathFuncMap = map[string]string{"abs": "", "acos": "", "asin": "", "atan": "", "atan2": "",
  21. "bitand": "", "bitor": "", "bitxor": "", "bitnot": "",
  22. "ceil": "", "cos": "", "cosh": "",
  23. "exp": "",
  24. "ln": "", "log": "",
  25. "mod": "",
  26. "power": "",
  27. "rand": "", "round": "",
  28. "sign": "", "sin": "", "sinh": "", "sqrt": "",
  29. "tan": "", "tanh": "",
  30. }
  31. var strFuncMap = map[string]string{"concat": "",
  32. "endswith": "",
  33. "format_time": "",
  34. "indexof": "",
  35. "length": "", "lower": "", "lpad": "", "ltrim": "",
  36. "numbytes": "",
  37. "regexp_matches": "", "regexp_replace": "", "regexp_substr": "", "rpad": "", "rtrim": "",
  38. "substring": "", "startswith": "", "split_value": "",
  39. "trim": "",
  40. "upper": "",
  41. }
  42. var convFuncMap = map[string]string{"concat": "", "cast": "", "chr": "",
  43. "encode": "",
  44. "trunc": "",
  45. }
  46. var hashFuncMap = map[string]string{"md5": "",
  47. "sha1": "", "sha256": "", "sha384": "", "sha512": "",
  48. }
  49. var otherFuncMap = map[string]string{"isnull": "",
  50. "newuuid": "", "timestamp": "", "mqtt": "", "meta": "",
  51. }
  52. func (*FunctionValuer) Call(name string, args []interface{}) (interface{}, bool) {
  53. lowerName := strings.ToLower(name)
  54. if _, ok := mathFuncMap[lowerName]; ok {
  55. return mathCall(name, args)
  56. } else if _, ok := strFuncMap[lowerName]; ok {
  57. return strCall(lowerName, args)
  58. } else if _, ok := convFuncMap[lowerName]; ok {
  59. return convCall(lowerName, args)
  60. } else if _, ok := hashFuncMap[lowerName]; ok {
  61. return hashCall(lowerName, args)
  62. } else if _, ok := otherFuncMap[lowerName]; ok {
  63. return otherCall(lowerName, args)
  64. } else if _, ok := aggFuncMap[lowerName]; ok {
  65. return nil, false
  66. } else {
  67. common.Log.Debugf("run func %s", name)
  68. if nf, err := plugins.GetPlugin(name, plugins.FUNCTION); err != nil {
  69. return err, false
  70. } else {
  71. f, ok := nf.(api.Function)
  72. if !ok {
  73. return nil, false
  74. }
  75. if f.IsAggregate() {
  76. return nil, false
  77. }
  78. result, ok := f.Exec(args)
  79. common.Log.Debugf("run custom function %s, get result %v", name, result)
  80. return result, ok
  81. }
  82. }
  83. }