function.go 3.1 KB

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