binder.go 2.5 KB

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