func_operator.go 2.9 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 FuncOp struct {
  22. IsAgg bool
  23. CallExpr *ast.Call
  24. Name string
  25. }
  26. func (p *FuncOp) Apply(ctx api.StreamContext, data interface{}, fv *xsql.FunctionValuer, afv *xsql.AggregateFunctionValuer) interface{} {
  27. ctx.GetLogger().Debugf("FuncOp receive: %v", data)
  28. switch input := data.(type) {
  29. case error:
  30. return input
  31. case xsql.TupleRow:
  32. ve := &xsql.ValuerEval{Valuer: xsql.MultiValuer(input, fv)}
  33. result := ve.Eval(p.CallExpr)
  34. if e, ok := result.(error); ok {
  35. return e
  36. }
  37. input.Set(p.Name, result)
  38. case xsql.SingleCollection:
  39. var err error
  40. if p.IsAgg {
  41. input.SetIsAgg(true)
  42. err = input.GroupRange(func(_ int, aggRow xsql.CollectionRow) (bool, error) {
  43. afv.SetData(aggRow)
  44. ve := &xsql.ValuerEval{Valuer: xsql.MultiAggregateValuer(aggRow, fv, aggRow, fv, afv, &xsql.WildcardValuer{Data: aggRow})}
  45. result := ve.Eval(p.CallExpr)
  46. if e, ok := result.(error); ok {
  47. return false, e
  48. }
  49. aggRow.Set(p.Name, result)
  50. return true, nil
  51. })
  52. } else {
  53. err = input.RangeSet(func(_ int, row xsql.Row) (bool, error) {
  54. ve := &xsql.ValuerEval{Valuer: xsql.MultiValuer(row, &xsql.WindowRangeValuer{WindowRange: input.GetWindowRange()}, fv, &xsql.WildcardValuer{Data: row})}
  55. result := ve.Eval(p.CallExpr)
  56. if e, ok := result.(error); ok {
  57. return false, e
  58. }
  59. row.Set(p.Name, result)
  60. return true, nil
  61. })
  62. }
  63. if err != nil {
  64. return err
  65. }
  66. case xsql.GroupedCollection: // The order is important, because single collection usually is also a groupedCollection
  67. if !p.IsAgg {
  68. return fmt.Errorf("FuncOp: GroupedCollection is not supported for non-aggregate function")
  69. }
  70. err := input.GroupRange(func(_ int, aggRow xsql.CollectionRow) (bool, error) {
  71. afv.SetData(aggRow)
  72. ve := &xsql.ValuerEval{Valuer: xsql.MultiAggregateValuer(aggRow, fv, aggRow, fv, afv, &xsql.WildcardValuer{Data: aggRow})}
  73. result := ve.Eval(p.CallExpr)
  74. if e, ok := result.(error); ok {
  75. return false, e
  76. }
  77. aggRow.Set(p.Name, result)
  78. return true, nil
  79. })
  80. if err != nil {
  81. return err
  82. }
  83. default:
  84. return fmt.Errorf("run func error: invalid input %[1]T(%[1]v)", input)
  85. }
  86. return data
  87. }