function.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2022 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. }
  42. //var funcWithAsteriskSupportMap = map[string]string{
  43. // "collect": "",
  44. // "count": "",
  45. //}
  46. var analyticFuncs = map[string]struct{}{
  47. "lag": {},
  48. "changed_col": {},
  49. "had_changed": {},
  50. "latest": {},
  51. }
  52. const AnalyticPrefix = "$$a"
  53. func IsAnalyticFunc(name string) bool {
  54. _, ok := analyticFuncs[name]
  55. return ok
  56. }
  57. type Manager struct{}
  58. // Function the name is converted to lowercase if needed during parsing
  59. func (m *Manager) Function(name string) (api.Function, error) {
  60. _, ok := builtins[name]
  61. if ok {
  62. return staticFuncExecutor, nil
  63. }
  64. ff, ok := builtinStatfulFuncs[name]
  65. if ok {
  66. return ff(), nil
  67. }
  68. return nil, nil
  69. }
  70. func (m *Manager) HasFunctionSet(name string) bool {
  71. return name == "internal"
  72. }
  73. func (m *Manager) FunctionPluginInfo(funcName string) (plugin.EXTENSION_TYPE, string, string) {
  74. _, ok := m.ConvName(funcName)
  75. if !ok {
  76. return plugin.NONE_EXTENSION, "", ""
  77. } else {
  78. return plugin.INTERNAL, "", ""
  79. }
  80. }
  81. func (m *Manager) ConvName(n string) (string, bool) {
  82. name := strings.ToLower(n)
  83. _, ok := builtins[name]
  84. if ok {
  85. return name, true
  86. }
  87. _, ok = builtinStatfulFuncs[name]
  88. return name, ok
  89. }
  90. var m = &Manager{}
  91. func GetManager() *Manager {
  92. return m
  93. }