functions.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package xsql
  2. import (
  3. "github.com/emqx/kuiper/plugins"
  4. "strings"
  5. )
  6. // ONLY use NewFunctionValuer function to initialize
  7. type FunctionValuer struct {
  8. funcPlugins *funcPlugins
  9. }
  10. //Should only be called by stream to make sure a single instance for an operation
  11. func NewFunctionValuer(p *funcPlugins) *FunctionValuer {
  12. fv := &FunctionValuer{
  13. funcPlugins: p,
  14. }
  15. return fv
  16. }
  17. func (*FunctionValuer) Value(_ string) (interface{}, bool) {
  18. return nil, false
  19. }
  20. func (*FunctionValuer) Meta(_ string) (interface{}, bool) {
  21. return nil, false
  22. }
  23. var aggFuncMap = map[string]string{"avg": "",
  24. "count": "",
  25. "max": "", "min": "",
  26. "sum": "",
  27. "collect": "",
  28. "deduplicate": "",
  29. }
  30. var funcWithAsteriskSupportMap = map[string]string{
  31. "collect": "",
  32. "count": "",
  33. }
  34. var mathFuncMap = map[string]string{"abs": "", "acos": "", "asin": "", "atan": "", "atan2": "",
  35. "bitand": "", "bitor": "", "bitxor": "", "bitnot": "",
  36. "ceil": "", "cos": "", "cosh": "",
  37. "exp": "",
  38. "ln": "", "log": "",
  39. "mod": "",
  40. "power": "",
  41. "rand": "", "round": "",
  42. "sign": "", "sin": "", "sinh": "", "sqrt": "",
  43. "tan": "", "tanh": "",
  44. }
  45. var strFuncMap = map[string]string{"concat": "",
  46. "endswith": "",
  47. "format_time": "",
  48. "indexof": "",
  49. "length": "", "lower": "", "lpad": "", "ltrim": "",
  50. "numbytes": "",
  51. "regexp_matches": "", "regexp_replace": "", "regexp_substr": "", "rpad": "", "rtrim": "",
  52. "substring": "", "startswith": "", "split_value": "",
  53. "trim": "",
  54. "upper": "",
  55. }
  56. var convFuncMap = map[string]string{"concat": "", "cast": "", "chr": "",
  57. "encode": "",
  58. "trunc": "",
  59. }
  60. var hashFuncMap = map[string]string{"md5": "",
  61. "sha1": "", "sha256": "", "sha384": "", "sha512": "",
  62. }
  63. var jsonFuncMap = map[string]string{
  64. "json_path_query": "", "json_path_query_first": "", "json_path_exists": "",
  65. }
  66. var otherFuncMap = map[string]string{"isnull": "",
  67. "newuuid": "", "tstamp": "", "mqtt": "", "meta": "", "cardinality": "",
  68. }
  69. func (fv *FunctionValuer) Call(name string, args []interface{}) (interface{}, bool) {
  70. lowerName := strings.ToLower(name)
  71. if _, ok := mathFuncMap[lowerName]; ok {
  72. return mathCall(name, args)
  73. } else if _, ok := strFuncMap[lowerName]; ok {
  74. return strCall(lowerName, args)
  75. } else if _, ok := convFuncMap[lowerName]; ok {
  76. return convCall(lowerName, args)
  77. } else if _, ok := hashFuncMap[lowerName]; ok {
  78. return hashCall(lowerName, args)
  79. } else if _, ok := jsonFuncMap[lowerName]; ok {
  80. return jsonCall(lowerName, args)
  81. } else if _, ok := otherFuncMap[lowerName]; ok {
  82. return otherCall(lowerName, args)
  83. } else if _, ok := aggFuncMap[lowerName]; ok {
  84. return nil, false
  85. } else {
  86. nf, fctx, err := fv.funcPlugins.GetFuncFromPlugin(name)
  87. if err != nil {
  88. return err, false
  89. }
  90. if nf.IsAggregate() {
  91. return nil, false
  92. }
  93. logger := fctx.GetLogger()
  94. logger.Debugf("run func %s", name)
  95. result, ok := nf.Exec(args, fctx)
  96. logger.Debugf("run custom function %s, get result %v", name, result)
  97. return result, ok
  98. }
  99. }
  100. func IsAggStatement(stmt *SelectStatement) bool {
  101. if stmt.Dimensions != nil {
  102. ds := stmt.Dimensions.GetGroups()
  103. if ds != nil && len(ds) > 0 {
  104. return true
  105. }
  106. }
  107. r := false
  108. WalkFunc(stmt.Fields, func(n Node) {
  109. switch f := n.(type) {
  110. case *Call:
  111. if ok := isAggFunc(f); ok {
  112. r = true
  113. return
  114. }
  115. }
  116. })
  117. return r
  118. }
  119. func isAggFunc(f *Call) bool {
  120. fn := strings.ToLower(f.Name)
  121. if _, ok := aggFuncMap[fn]; ok {
  122. return true
  123. } else if _, ok := strFuncMap[fn]; ok {
  124. return false
  125. } else if _, ok := convFuncMap[fn]; ok {
  126. return false
  127. } else if _, ok := hashFuncMap[fn]; ok {
  128. return false
  129. } else if _, ok := otherFuncMap[fn]; ok {
  130. return false
  131. } else if _, ok := mathFuncMap[fn]; ok {
  132. return false
  133. } else {
  134. if nf, err := plugins.GetFunction(f.Name); err == nil {
  135. if nf.IsAggregate() {
  136. //Add cache
  137. aggFuncMap[fn] = ""
  138. return true
  139. }
  140. }
  141. }
  142. return false
  143. }
  144. func HasAggFuncs(node Node) bool {
  145. if node == nil {
  146. return false
  147. }
  148. var r = false
  149. WalkFunc(node, func(n Node) {
  150. if f, ok := n.(*Call); ok {
  151. if ok := isAggFunc(f); ok {
  152. r = true
  153. return
  154. }
  155. }
  156. })
  157. return r
  158. }
  159. func HasNoAggFuncs(node Node) bool {
  160. if node == nil {
  161. return false
  162. }
  163. var r = false
  164. WalkFunc(node, func(n Node) {
  165. if f, ok := n.(*Call); ok {
  166. if ok := isAggFunc(f); !ok {
  167. r = true
  168. return
  169. }
  170. }
  171. })
  172. return r
  173. }