function.go 2.6 KB

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