function.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. "fmt"
  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. "strings"
  21. )
  22. type funcExe func(ctx api.FunctionContext, args []interface{}) (interface{}, bool)
  23. type funcVal func(ctx api.FunctionContext, args []ast.Expr) error
  24. type builtinFunc struct {
  25. fType ast.FuncType
  26. exec funcExe
  27. val funcVal
  28. }
  29. var builtins map[string]builtinFunc
  30. func init() {
  31. builtins = make(map[string]builtinFunc)
  32. registerAggFunc()
  33. registerMathFunc()
  34. registerStrFunc()
  35. registerMiscFunc()
  36. registerAnalyticFunc()
  37. registerColsFunc()
  38. }
  39. //var funcWithAsteriskSupportMap = map[string]string{
  40. // "collect": "",
  41. // "count": "",
  42. //}
  43. var analyticFuncs = map[string]struct{}{
  44. "lag": {},
  45. "changed_col": {},
  46. "had_changed": {},
  47. "latest": {},
  48. }
  49. const AnalyticPrefix = "$$a"
  50. func IsAnalyticFunc(name string) bool {
  51. _, ok := analyticFuncs[name]
  52. return ok
  53. }
  54. type funcExecutor struct{}
  55. func (f *funcExecutor) ValidateWithName(args []ast.Expr, name string) error {
  56. fs, ok := builtins[name]
  57. if !ok {
  58. return fmt.Errorf("validate function %s error: unknown name", name)
  59. }
  60. var eargs []ast.Expr
  61. for _, arg := range args {
  62. if t, ok := arg.(ast.Expr); ok {
  63. eargs = append(eargs, t)
  64. } else {
  65. // should never happen
  66. return fmt.Errorf("receive invalid arg %v", arg)
  67. }
  68. }
  69. // TODO pass in ctx
  70. return fs.val(nil, eargs)
  71. }
  72. func (f *funcExecutor) Validate(_ []interface{}) error {
  73. return fmt.Errorf("unknow name")
  74. }
  75. func (f *funcExecutor) Exec(_ []interface{}, _ api.FunctionContext) (interface{}, bool) {
  76. return fmt.Errorf("unknow name"), false
  77. }
  78. func (f *funcExecutor) ExecWithName(args []interface{}, ctx api.FunctionContext, name string) (interface{}, bool) {
  79. fs, ok := builtins[name]
  80. if !ok {
  81. return fmt.Errorf("unknow name"), false
  82. }
  83. return fs.exec(ctx, args)
  84. }
  85. func (f *funcExecutor) IsAggregate() bool {
  86. return false
  87. }
  88. func (f *funcExecutor) GetFuncType(name string) ast.FuncType {
  89. fs, ok := builtins[name]
  90. if !ok {
  91. return ast.FuncTypeUnknown
  92. }
  93. return fs.fType
  94. }
  95. var staticFuncExecutor = &funcExecutor{}
  96. type Manager struct{}
  97. // Function the name is converted to lowercase if needed during parsing
  98. func (m *Manager) Function(name string) (api.Function, error) {
  99. _, ok := builtins[name]
  100. if !ok {
  101. return nil, nil
  102. }
  103. return staticFuncExecutor, nil
  104. }
  105. func (m *Manager) HasFunctionSet(name string) bool {
  106. return name == "internal"
  107. }
  108. func (m *Manager) GetFunctionPlugin(funcName string) (plugin.EXTENSION_TYPE, string, string) {
  109. _, ok := builtins[funcName]
  110. if !ok {
  111. return plugin.NONE_EXTENSION, "", ""
  112. } else {
  113. return plugin.INTERNAL, "", ""
  114. }
  115. }
  116. func (m *Manager) ConvName(n string) (string, bool) {
  117. name := strings.ToLower(n)
  118. _, ok := builtins[name]
  119. return name, ok
  120. }
  121. var m = &Manager{}
  122. func GetManager() *Manager {
  123. return m
  124. }