functions.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. }
  27. var mathFuncMap = map[string]string{"abs": "", "acos": "", "asin": "", "atan": "", "atan2": "",
  28. "bitand": "", "bitor": "", "bitxor": "", "bitnot": "",
  29. "ceil": "", "cos": "", "cosh": "",
  30. "exp": "",
  31. "ln": "", "log": "",
  32. "mod": "",
  33. "power": "",
  34. "rand": "", "round": "",
  35. "sign": "", "sin": "", "sinh": "", "sqrt": "",
  36. "tan": "", "tanh": "",
  37. }
  38. var strFuncMap = map[string]string{"concat": "",
  39. "endswith": "",
  40. "format_time": "",
  41. "indexof": "",
  42. "length": "", "lower": "", "lpad": "", "ltrim": "",
  43. "numbytes": "",
  44. "regexp_matches": "", "regexp_replace": "", "regexp_substr": "", "rpad": "", "rtrim": "",
  45. "substring": "", "startswith": "", "split_value": "",
  46. "trim": "",
  47. "upper": "",
  48. }
  49. var convFuncMap = map[string]string{"concat": "", "cast": "", "chr": "",
  50. "encode": "",
  51. "trunc": "",
  52. }
  53. var hashFuncMap = map[string]string{"md5": "",
  54. "sha1": "", "sha256": "", "sha384": "", "sha512": "",
  55. }
  56. var jsonFuncMap = map[string]string{
  57. "json_path_query": "", "json_path_query_first": "", "json_path_exists": "",
  58. }
  59. var otherFuncMap = map[string]string{"isnull": "",
  60. "newuuid": "", "tstamp": "", "mqtt": "", "meta": "",
  61. }
  62. func (fv *FunctionValuer) Call(name string, args []interface{}) (interface{}, bool) {
  63. lowerName := strings.ToLower(name)
  64. if _, ok := mathFuncMap[lowerName]; ok {
  65. return mathCall(name, args)
  66. } else if _, ok := strFuncMap[lowerName]; ok {
  67. return strCall(lowerName, args)
  68. } else if _, ok := convFuncMap[lowerName]; ok {
  69. return convCall(lowerName, args)
  70. } else if _, ok := hashFuncMap[lowerName]; ok {
  71. return hashCall(lowerName, args)
  72. } else if _, ok := jsonFuncMap[lowerName]; ok {
  73. return jsonCall(lowerName, args)
  74. } else if _, ok := otherFuncMap[lowerName]; ok {
  75. return otherCall(lowerName, args)
  76. } else if _, ok := aggFuncMap[lowerName]; ok {
  77. return nil, false
  78. } else {
  79. nf, fctx, err := fv.funcPlugins.GetFuncFromPlugin(name)
  80. if err != nil {
  81. return err, false
  82. }
  83. if nf.IsAggregate() {
  84. return nil, false
  85. }
  86. logger := fctx.GetLogger()
  87. logger.Debugf("run func %s", name)
  88. result, ok := nf.Exec(args, fctx)
  89. logger.Debugf("run custom function %s, get result %v", name, result)
  90. return result, ok
  91. }
  92. }