binder.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. "errors"
  17. "fmt"
  18. "github.com/lf-edge/ekuiper/internal/binder"
  19. "github.com/lf-edge/ekuiper/internal/plugin"
  20. "github.com/lf-edge/ekuiper/pkg/api"
  21. "github.com/lf-edge/ekuiper/pkg/ast"
  22. )
  23. var ( // init once and read only
  24. funcFactories []binder.FuncFactory
  25. funcFactoriesNames []string
  26. )
  27. func init() {
  28. f := binder.FactoryEntry{
  29. Name: "built-in",
  30. Factory: GetManager(),
  31. }
  32. applyFactory(f)
  33. }
  34. // Initialize Only call once when server starts
  35. func Initialize(factories []binder.FactoryEntry) error {
  36. for _, f := range factories {
  37. applyFactory(f)
  38. }
  39. return nil
  40. }
  41. func applyFactory(f binder.FactoryEntry) {
  42. if s, ok := f.Factory.(binder.FuncFactory); ok {
  43. funcFactories = append(funcFactories, s)
  44. funcFactoriesNames = append(funcFactoriesNames, f.Name)
  45. }
  46. }
  47. func Function(name string) (api.Function, error) {
  48. var errs error
  49. for i, sf := range funcFactories {
  50. r, err := sf.Function(name)
  51. if err != nil {
  52. errs = errors.Join(errs, fmt.Errorf("%s:%v", funcFactoriesNames[i], err))
  53. }
  54. if r != nil {
  55. return r, errs
  56. }
  57. }
  58. return nil, errs
  59. }
  60. func GetFunctionPlugin(name string) (plugin.EXTENSION_TYPE, string, string) {
  61. for _, sf := range funcFactories {
  62. t, s1, s2 := sf.FunctionPluginInfo(name)
  63. if t == plugin.NONE_EXTENSION {
  64. continue
  65. }
  66. return t, s1, s2
  67. }
  68. return plugin.NONE_EXTENSION, "", ""
  69. }
  70. func HasFunctionSet(name string) bool {
  71. for _, sf := range funcFactories {
  72. r := sf.HasFunctionSet(name)
  73. if r {
  74. return r
  75. }
  76. }
  77. return false
  78. }
  79. func ConvName(name string) (string, bool) {
  80. for _, sf := range funcFactories {
  81. r, ok := sf.ConvName(name)
  82. if ok {
  83. return r, ok
  84. }
  85. }
  86. return name, false
  87. }
  88. type multiAggFunc interface {
  89. GetFuncType(name string) ast.FuncType
  90. }
  91. func IsAggFunc(funcName string) bool {
  92. f, _ := Function(funcName)
  93. if f != nil {
  94. if mf, ok := f.(multiAggFunc); ok {
  95. return mf.GetFuncType(funcName) == ast.FuncTypeAgg
  96. } else {
  97. return f.IsAggregate()
  98. }
  99. }
  100. return false
  101. }
  102. func GetFuncType(funcName string) ast.FuncType {
  103. f, _ := Function(funcName)
  104. if f != nil {
  105. if mf, ok := f.(multiAggFunc); ok {
  106. return mf.GetFuncType(funcName)
  107. }
  108. if f.IsAggregate() {
  109. return ast.FuncTypeAgg
  110. }
  111. return ast.FuncTypeScalar
  112. }
  113. return ast.FuncTypeUnknown
  114. }