binder.go 2.8 KB

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