binder.go 2.6 KB

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