binder.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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.GetFunctionPlugin(name)
  62. if t == plugin.NONE_EXTENSION {
  63. continue
  64. } else {
  65. return t, s1, s2
  66. }
  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. }