function.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. )
  25. type builtinFunc struct {
  26. fType ast.FuncType
  27. exec funcExe
  28. val funcVal
  29. }
  30. var (
  31. builtins map[string]builtinFunc
  32. builtinStatfulFuncs map[string]func() api.Function
  33. )
  34. func init() {
  35. builtins = make(map[string]builtinFunc)
  36. builtinStatfulFuncs = make(map[string]func() api.Function)
  37. registerAggFunc()
  38. registerMathFunc()
  39. registerStrFunc()
  40. registerMiscFunc()
  41. registerAnalyticFunc()
  42. registerColsFunc()
  43. registerSetReturningFunc()
  44. }
  45. //var funcWithAsteriskSupportMap = map[string]string{
  46. // "collect": "",
  47. // "count": "",
  48. //}
  49. var analyticFuncs = map[string]struct{}{
  50. "lag": {},
  51. "changed_col": {},
  52. "had_changed": {},
  53. "latest": {},
  54. }
  55. const AnalyticPrefix = "$$a"
  56. func IsAnalyticFunc(name string) bool {
  57. _, ok := analyticFuncs[name]
  58. return ok
  59. }
  60. type Manager struct{}
  61. // Function the name is converted to lowercase if needed during parsing
  62. func (m *Manager) Function(name string) (api.Function, error) {
  63. _, ok := builtins[name]
  64. if ok {
  65. return staticFuncExecutor, nil
  66. }
  67. ff, ok := builtinStatfulFuncs[name]
  68. if ok {
  69. return ff(), nil
  70. }
  71. return nil, nil
  72. }
  73. func (m *Manager) HasFunctionSet(name string) bool {
  74. return name == "internal"
  75. }
  76. func (m *Manager) FunctionPluginInfo(funcName string) (plugin.EXTENSION_TYPE, string, string) {
  77. _, ok := m.ConvName(funcName)
  78. if !ok {
  79. return plugin.NONE_EXTENSION, "", ""
  80. } else {
  81. return plugin.INTERNAL, "", ""
  82. }
  83. }
  84. func (m *Manager) ConvName(n string) (string, bool) {
  85. name := strings.ToLower(n)
  86. _, ok := builtins[name]
  87. if ok {
  88. return name, true
  89. }
  90. _, ok = builtinStatfulFuncs[name]
  91. return name, ok
  92. }
  93. var m = &Manager{}
  94. func GetManager() *Manager {
  95. return m
  96. }