having_operator.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2021 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 HavingOp struct {
  22. Condition ast.Expr
  23. }
  24. func (p *HavingOp) Apply(ctx api.StreamContext, data interface{}, fv *xsql.FunctionValuer, afv *xsql.AggregateFunctionValuer) interface{} {
  25. log := ctx.GetLogger()
  26. log.Debugf("having plan receive %s", data)
  27. switch input := data.(type) {
  28. case error:
  29. return input
  30. case xsql.GroupedTuplesSet:
  31. r := xsql.GroupedTuplesSet{}
  32. for _, v := range input {
  33. afv.SetData(v)
  34. ve := &xsql.ValuerEval{Valuer: xsql.MultiAggregateValuer(v, fv, v.Content[0], fv, afv, &xsql.WildcardValuer{Data: v.Content[0]})}
  35. result := ve.Eval(p.Condition)
  36. switch val := result.(type) {
  37. case error:
  38. return fmt.Errorf("run Having error: %s", val)
  39. case bool:
  40. if val {
  41. r = append(r, v)
  42. }
  43. default:
  44. return fmt.Errorf("run Having error: invalid condition that returns non-bool value %[1]T(%[1]v)", val)
  45. }
  46. }
  47. if len(r) > 0 {
  48. return r
  49. }
  50. case xsql.WindowTuplesSet:
  51. if len(input.Content) != 1 {
  52. return fmt.Errorf("run Having error: input WindowTuplesSet with multiple tuples cannot be evaluated")
  53. }
  54. ms := input.Content[0].Tuples
  55. v := ms[0]
  56. afv.SetData(input)
  57. ve := &xsql.ValuerEval{Valuer: xsql.MultiAggregateValuer(input, fv, &v, fv, afv, &xsql.WildcardValuer{Data: &v})}
  58. result := ve.Eval(p.Condition)
  59. switch val := result.(type) {
  60. case error:
  61. return fmt.Errorf("run Having error: %s", val)
  62. case bool:
  63. if val {
  64. return input
  65. }
  66. default:
  67. return fmt.Errorf("run Having error: invalid condition that returns non-bool value %[1]T(%[1]v)", val)
  68. }
  69. case *xsql.JoinTupleSets:
  70. ms := input.Content
  71. r := ms[:0]
  72. afv.SetData(input)
  73. for _, v := range ms {
  74. ve := &xsql.ValuerEval{Valuer: xsql.MultiAggregateValuer(input, fv, &v, fv, afv, &xsql.WildcardValuer{Data: &v})}
  75. result := ve.Eval(p.Condition)
  76. switch val := result.(type) {
  77. case error:
  78. return fmt.Errorf("run Having error: %s", val)
  79. case bool:
  80. if val {
  81. r = append(r, v)
  82. }
  83. default:
  84. return fmt.Errorf("run Having error: invalid condition that returns non-bool value %[1]T(%[1]v)", val)
  85. }
  86. }
  87. input.Content = r
  88. if len(r) > 0 {
  89. return input
  90. }
  91. default:
  92. return fmt.Errorf("run Having error: invalid input %[1]T(%[1]v)", input)
  93. }
  94. return nil
  95. }