functions.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package xsql
  2. import (
  3. "engine/common"
  4. "engine/common/plugin_manager"
  5. "engine/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 {
  62. if nf, err := plugin_manager.GetPlugin(name, "functions"); err != nil {
  63. return nil, false
  64. }else{
  65. f, ok := nf.(api.Function)
  66. if !ok {
  67. return nil, false
  68. }
  69. result, ok := f.Exec(args)
  70. common.Log.Debugf("run custom function %s, get result %v", name, result)
  71. return result, ok
  72. }
  73. }
  74. }