functions.go 2.4 KB

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