function.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. }
  47. const AnalyticPrefix = "$$a"
  48. func IsAnalyticFunc(name string) bool {
  49. _, ok := analyticFuncs[name]
  50. return ok
  51. }
  52. type funcExecutor struct{}
  53. func (f *funcExecutor) ValidateWithName(args []ast.Expr, name string) error {
  54. fs, ok := builtins[name]
  55. if !ok {
  56. return fmt.Errorf("validate function %s error: unknown name", name)
  57. }
  58. var eargs []ast.Expr
  59. for _, arg := range args {
  60. if t, ok := arg.(ast.Expr); ok {
  61. eargs = append(eargs, t)
  62. } else {
  63. // should never happen
  64. return fmt.Errorf("receive invalid arg %v", arg)
  65. }
  66. }
  67. // TODO pass in ctx
  68. return fs.val(nil, eargs)
  69. }
  70. func (f *funcExecutor) Validate(_ []interface{}) error {
  71. return fmt.Errorf("unknow name")
  72. }
  73. func (f *funcExecutor) Exec(_ []interface{}, _ api.FunctionContext) (interface{}, bool) {
  74. return fmt.Errorf("unknow name"), false
  75. }
  76. func (f *funcExecutor) ExecWithName(args []interface{}, ctx api.FunctionContext, name string) (interface{}, bool) {
  77. fs, ok := builtins[name]
  78. if !ok {
  79. return fmt.Errorf("unknow name"), false
  80. }
  81. return fs.exec(ctx, args)
  82. }
  83. func (f *funcExecutor) IsAggregate() bool {
  84. return false
  85. }
  86. func (f *funcExecutor) GetFuncType(name string) ast.FuncType {
  87. fs, ok := builtins[name]
  88. if !ok {
  89. return ast.FuncTypeUnknown
  90. }
  91. return fs.fType
  92. }
  93. var staticFuncExecutor = &funcExecutor{}
  94. type Manager struct{}
  95. // Function the name is converted to lowercase if needed during parsing
  96. func (m *Manager) Function(name string) (api.Function, error) {
  97. _, ok := builtins[name]
  98. if !ok {
  99. return nil, nil
  100. }
  101. return staticFuncExecutor, nil
  102. }
  103. func (m *Manager) HasFunctionSet(name string) bool {
  104. return name == "internal"
  105. }
  106. func (m *Manager) ConvName(n string) (string, bool) {
  107. name := strings.ToLower(n)
  108. _, ok := builtins[name]
  109. return name, ok
  110. }
  111. var m = &Manager{}
  112. func GetManager() *Manager {
  113. return m
  114. }