analyticfuncs_operator.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 operator
  15. import (
  16. "fmt"
  17. "github.com/lf-edge/ekuiper/internal/xsql"
  18. "github.com/lf-edge/ekuiper/pkg/api"
  19. "github.com/lf-edge/ekuiper/pkg/ast"
  20. )
  21. type AnalyticFuncsOp struct {
  22. Funcs []*ast.Call
  23. FieldFuncs []*ast.Call
  24. }
  25. func (p *AnalyticFuncsOp) evalTupleFunc(calls []*ast.Call, ve *xsql.ValuerEval, input xsql.TupleRow) (xsql.TupleRow, error) {
  26. for _, call := range calls {
  27. f := call
  28. result := ve.Eval(f)
  29. if e, ok := result.(error); ok {
  30. return nil, e
  31. }
  32. input.Set(f.CachedField, result)
  33. }
  34. return input, nil
  35. }
  36. func (p *AnalyticFuncsOp) evalCollectionFunc(calls []*ast.Call, fv *xsql.FunctionValuer, input xsql.SingleCollection) (xsql.SingleCollection, error) {
  37. err := input.RangeSet(func(_ int, row xsql.Row) (bool, error) {
  38. ve := &xsql.ValuerEval{Valuer: xsql.MultiValuer(row, &xsql.WindowRangeValuer{WindowRange: input.GetWindowRange()}, fv, &xsql.WildcardValuer{Data: row})}
  39. for _, call := range calls {
  40. f := call
  41. result := ve.Eval(f)
  42. if e, ok := result.(error); ok {
  43. return false, e
  44. }
  45. row.Set(f.CachedField, result)
  46. }
  47. return true, nil
  48. })
  49. if err != nil {
  50. return nil, err
  51. }
  52. return input, nil
  53. }
  54. func (p *AnalyticFuncsOp) Apply(ctx api.StreamContext, data interface{}, fv *xsql.FunctionValuer, _ *xsql.AggregateFunctionValuer) interface{} {
  55. ctx.GetLogger().Debugf("AnalyticFuncsOp receive: %v", data)
  56. var err error
  57. switch input := data.(type) {
  58. case error:
  59. return input
  60. case xsql.TupleRow:
  61. ve := &xsql.ValuerEval{Valuer: xsql.MultiValuer(input, fv)}
  62. input, err = p.evalTupleFunc(p.FieldFuncs, ve, input)
  63. if err != nil {
  64. return err
  65. }
  66. input, err = p.evalTupleFunc(p.Funcs, ve, input)
  67. if err != nil {
  68. return err
  69. }
  70. data = input
  71. case xsql.SingleCollection:
  72. input, err = p.evalCollectionFunc(p.FieldFuncs, fv, input)
  73. if err != nil {
  74. return err
  75. }
  76. input, err = p.evalCollectionFunc(p.Funcs, fv, input)
  77. if err != nil {
  78. return err
  79. }
  80. data = input
  81. default:
  82. return fmt.Errorf("run analytic funcs op error: invalid input %[1]T(%[1]v)", input)
  83. }
  84. return data
  85. }