function.go 2.5 KB

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