functions.go 2.3 KB

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