functions.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright 2021 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package ast
  15. import (
  16. "github.com/lf-edge/ekuiper/pkg/api"
  17. "strings"
  18. "sync"
  19. )
  20. type FuncType int
  21. const (
  22. NotFoundFunc FuncType = iota - 1
  23. AggFunc
  24. MathFunc
  25. StrFunc
  26. ConvFunc
  27. HashFunc
  28. JsonFunc
  29. OtherFunc
  30. )
  31. var maps = []map[string]string{
  32. aggFuncMap, mathFuncMap, strFuncMap, convFuncMap, hashFuncMap, jsonFuncMap, otherFuncMap,
  33. }
  34. var aggFuncMap = map[string]string{"avg": "",
  35. "count": "",
  36. "max": "", "min": "",
  37. "sum": "",
  38. "collect": "",
  39. "deduplicate": "",
  40. }
  41. var funcWithAsteriskSupportMap = map[string]string{
  42. "collect": "",
  43. "count": "",
  44. }
  45. var mathFuncMap = map[string]string{"abs": "", "acos": "", "asin": "", "atan": "", "atan2": "",
  46. "bitand": "", "bitor": "", "bitxor": "", "bitnot": "",
  47. "ceil": "", "cos": "", "cosh": "",
  48. "exp": "",
  49. "ln": "", "log": "",
  50. "mod": "",
  51. "power": "",
  52. "rand": "", "round": "",
  53. "sign": "", "sin": "", "sinh": "", "sqrt": "",
  54. "tan": "", "tanh": "",
  55. }
  56. var strFuncMap = map[string]string{"concat": "",
  57. "endswith": "",
  58. "format_time": "",
  59. "indexof": "",
  60. "length": "", "lower": "", "lpad": "", "ltrim": "",
  61. "numbytes": "",
  62. "regexp_matches": "", "regexp_replace": "", "regexp_substr": "", "rpad": "", "rtrim": "",
  63. "substring": "", "startswith": "", "split_value": "",
  64. "trim": "",
  65. "upper": "",
  66. }
  67. var convFuncMap = map[string]string{"concat": "", "cast": "", "chr": "",
  68. "encode": "",
  69. "trunc": "",
  70. }
  71. var hashFuncMap = map[string]string{"md5": "",
  72. "sha1": "", "sha256": "", "sha384": "", "sha512": "",
  73. }
  74. var jsonFuncMap = map[string]string{
  75. "json_path_query": "", "json_path_query_first": "", "json_path_exists": "",
  76. }
  77. var otherFuncMap = map[string]string{"isnull": "",
  78. "newuuid": "", "tstamp": "", "mqtt": "", "meta": "", "cardinality": "",
  79. "window_start": "",
  80. "window_end": "",
  81. }
  82. type FuncRuntime interface {
  83. Get(name string) (api.Function, api.FunctionContext, error)
  84. }
  85. var (
  86. once sync.Once
  87. ff *FuncFinder
  88. )
  89. // FuncFinder Singleton, must be initiated when starting
  90. type FuncFinder struct {
  91. runtime FuncRuntime
  92. }
  93. // InitFuncFinder must be called when starting
  94. func InitFuncFinder(runtime FuncRuntime) {
  95. once.Do(func() {
  96. ff = &FuncFinder{runtime: runtime}
  97. })
  98. ff.runtime = runtime
  99. }
  100. // FuncFinderSingleton must be inited before calling this
  101. func FuncFinderSingleton() *FuncFinder {
  102. return ff
  103. }
  104. func (ff *FuncFinder) IsAggFunc(f *Call) bool {
  105. fn := strings.ToLower(f.Name)
  106. if _, ok := aggFuncMap[fn]; ok {
  107. return true
  108. } else if _, ok := strFuncMap[fn]; ok {
  109. return false
  110. } else if _, ok := convFuncMap[fn]; ok {
  111. return false
  112. } else if _, ok := hashFuncMap[fn]; ok {
  113. return false
  114. } else if _, ok := otherFuncMap[fn]; ok {
  115. return false
  116. } else if _, ok := mathFuncMap[fn]; ok {
  117. return false
  118. } else {
  119. if nf, _, err := ff.runtime.Get(f.Name); err == nil {
  120. if nf.IsAggregate() {
  121. //Add cache
  122. aggFuncMap[fn] = ""
  123. return true
  124. }
  125. }
  126. }
  127. return false
  128. }
  129. func (ff *FuncFinder) FuncType(name string) FuncType {
  130. for i, m := range maps {
  131. if _, ok := m[strings.ToLower(name)]; ok {
  132. return FuncType(i)
  133. }
  134. }
  135. return NotFoundFunc
  136. }