function.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright 2022-2023 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 function
  15. import (
  16. "strings"
  17. "github.com/lf-edge/ekuiper/internal/plugin"
  18. "github.com/lf-edge/ekuiper/pkg/api"
  19. "github.com/lf-edge/ekuiper/pkg/ast"
  20. )
  21. type (
  22. funcExe func(ctx api.FunctionContext, args []interface{}) (interface{}, bool)
  23. funcVal func(ctx api.FunctionContext, args []ast.Expr) error
  24. funcCheckNil func(args []interface{}) (interface{}, bool)
  25. )
  26. type builtinFunc struct {
  27. fType ast.FuncType
  28. exec funcExe
  29. val funcVal
  30. check funcCheckNil
  31. }
  32. var (
  33. builtins map[string]builtinFunc
  34. builtinStatfulFuncs map[string]func() api.Function
  35. )
  36. func init() {
  37. builtins = make(map[string]builtinFunc)
  38. builtinStatfulFuncs = make(map[string]func() api.Function)
  39. registerAggFunc()
  40. registerMathFunc()
  41. registerStrFunc()
  42. registerMiscFunc()
  43. registerAnalyticFunc()
  44. registerColsFunc()
  45. registerSetReturningFunc()
  46. registerArrayFunc()
  47. registerObjectFunc()
  48. registerGlobalStateFunc()
  49. }
  50. //var funcWithAsteriskSupportMap = map[string]string{
  51. // "collect": "",
  52. // "count": "",
  53. //}
  54. var analyticFuncs = map[string]struct{}{
  55. "lag": {},
  56. "changed_col": {},
  57. "had_changed": {},
  58. "latest": {},
  59. }
  60. const AnalyticPrefix = "$$a"
  61. func IsAnalyticFunc(name string) bool {
  62. _, ok := analyticFuncs[name]
  63. return ok
  64. }
  65. type Manager struct{}
  66. // Function the name is converted to lowercase if needed during parsing
  67. func (m *Manager) Function(name string) (api.Function, error) {
  68. _, ok := builtins[name]
  69. if ok {
  70. return staticFuncExecutor, nil
  71. }
  72. ff, ok := builtinStatfulFuncs[name]
  73. if ok {
  74. return ff(), nil
  75. }
  76. return nil, nil
  77. }
  78. func (m *Manager) HasFunctionSet(name string) bool {
  79. return name == "internal"
  80. }
  81. func (m *Manager) FunctionPluginInfo(funcName string) (plugin.EXTENSION_TYPE, string, string) {
  82. _, ok := m.ConvName(funcName)
  83. if !ok {
  84. return plugin.NONE_EXTENSION, "", ""
  85. } else {
  86. return plugin.INTERNAL, "", ""
  87. }
  88. }
  89. func (m *Manager) ConvName(n string) (string, bool) {
  90. name := strings.ToLower(n)
  91. _, ok := builtins[name]
  92. if ok {
  93. return name, true
  94. }
  95. _, ok = builtinStatfulFuncs[name]
  96. return name, ok
  97. }
  98. var m = &Manager{}
  99. func GetManager() *Manager {
  100. return m
  101. }
  102. func returnNilIfHasAnyNil(args []interface{}) (returned interface{}, skipExec bool) {
  103. for _, arg := range args {
  104. if arg == nil {
  105. return nil, true
  106. }
  107. }
  108. return nil, false
  109. }
  110. func returnFalseIfHasAnyNil(args []interface{}) (returned interface{}, skipExec bool) {
  111. for _, arg := range args {
  112. if arg == nil {
  113. return false, true
  114. }
  115. }
  116. return nil, false
  117. }
  118. func return0IfHasAnyNil(args []interface{}) (returned interface{}, skipExec bool) {
  119. for _, arg := range args {
  120. if arg == nil {
  121. return 0, true
  122. }
  123. }
  124. return nil, false
  125. }