functions.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package xsql
  2. import (
  3. "strings"
  4. )
  5. // ONLY use NewFunctionValuer function to initialize
  6. type FunctionValuer struct {
  7. funcPlugins *funcPlugins
  8. }
  9. //Should only be called by stream to make sure a single instance for an operation
  10. func NewFunctionValuer(p *funcPlugins) *FunctionValuer {
  11. fv := &FunctionValuer{
  12. funcPlugins: p,
  13. }
  14. return fv
  15. }
  16. func (*FunctionValuer) Value(_ string) (interface{}, bool) {
  17. return nil, false
  18. }
  19. func (*FunctionValuer) Meta(_ string) (interface{}, bool) {
  20. return nil, false
  21. }
  22. var aggFuncMap = map[string]string{"avg": "",
  23. "count": "",
  24. "max": "", "min": "",
  25. "sum": "",
  26. "collect": "",
  27. }
  28. var mathFuncMap = map[string]string{"abs": "", "acos": "", "asin": "", "atan": "", "atan2": "",
  29. "bitand": "", "bitor": "", "bitxor": "", "bitnot": "",
  30. "ceil": "", "cos": "", "cosh": "",
  31. "exp": "",
  32. "ln": "", "log": "",
  33. "mod": "",
  34. "power": "",
  35. "rand": "", "round": "",
  36. "sign": "", "sin": "", "sinh": "", "sqrt": "",
  37. "tan": "", "tanh": "",
  38. }
  39. var strFuncMap = map[string]string{"concat": "",
  40. "endswith": "",
  41. "format_time": "",
  42. "indexof": "",
  43. "length": "", "lower": "", "lpad": "", "ltrim": "",
  44. "numbytes": "",
  45. "regexp_matches": "", "regexp_replace": "", "regexp_substr": "", "rpad": "", "rtrim": "",
  46. "substring": "", "startswith": "", "split_value": "",
  47. "trim": "",
  48. "upper": "",
  49. }
  50. var convFuncMap = map[string]string{"concat": "", "cast": "", "chr": "",
  51. "encode": "",
  52. "trunc": "",
  53. }
  54. var hashFuncMap = map[string]string{"md5": "",
  55. "sha1": "", "sha256": "", "sha384": "", "sha512": "",
  56. }
  57. var jsonFuncMap = map[string]string{
  58. "json_path_query": "", "json_path_query_first": "", "json_path_exists": "",
  59. }
  60. var otherFuncMap = map[string]string{"isnull": "",
  61. "newuuid": "", "tstamp": "", "mqtt": "", "meta": "",
  62. }
  63. func (fv *FunctionValuer) Call(name string, args []interface{}) (interface{}, bool) {
  64. lowerName := strings.ToLower(name)
  65. if _, ok := mathFuncMap[lowerName]; ok {
  66. return mathCall(name, args)
  67. } else if _, ok := strFuncMap[lowerName]; ok {
  68. return strCall(lowerName, args)
  69. } else if _, ok := convFuncMap[lowerName]; ok {
  70. return convCall(lowerName, args)
  71. } else if _, ok := hashFuncMap[lowerName]; ok {
  72. return hashCall(lowerName, args)
  73. } else if _, ok := jsonFuncMap[lowerName]; ok {
  74. return jsonCall(lowerName, args)
  75. } else if _, ok := otherFuncMap[lowerName]; ok {
  76. return otherCall(lowerName, args)
  77. } else if _, ok := aggFuncMap[lowerName]; ok {
  78. return nil, false
  79. } else {
  80. nf, fctx, err := fv.funcPlugins.GetFuncFromPlugin(name)
  81. if err != nil {
  82. return err, false
  83. }
  84. if nf.IsAggregate() {
  85. return nil, false
  86. }
  87. logger := fctx.GetLogger()
  88. logger.Debugf("run func %s", name)
  89. result, ok := nf.Exec(args, fctx)
  90. logger.Debugf("run custom function %s, get result %v", name, result)
  91. return result, ok
  92. }
  93. }