havingPlan.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2021-2023 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 planner
  15. import (
  16. "github.com/lf-edge/ekuiper/internal/xsql"
  17. "github.com/lf-edge/ekuiper/pkg/ast"
  18. )
  19. type HavingPlan struct {
  20. baseLogicalPlan
  21. condition ast.Expr
  22. stateFuncs []*ast.Call
  23. }
  24. func (p HavingPlan) Init() *HavingPlan {
  25. p.baseLogicalPlan.self = &p
  26. return &p
  27. }
  28. func (p *HavingPlan) PruneColumns(fields []ast.Expr) error {
  29. f := getFields(p.condition)
  30. return p.baseLogicalPlan.PruneColumns(append(fields, f...))
  31. }
  32. func (p *HavingPlan) ExtractStateFunc() {
  33. aliases := make(map[string]ast.Expr)
  34. ast.WalkFunc(p.condition, func(n ast.Node) bool {
  35. switch f := n.(type) {
  36. case *ast.Call:
  37. p.transform(f)
  38. case *ast.FieldRef:
  39. if f.AliasRef != nil {
  40. aliases[f.Name] = f.AliasRef.Expression
  41. }
  42. }
  43. return true
  44. })
  45. for _, ex := range aliases {
  46. ast.WalkFunc(ex, func(n ast.Node) bool {
  47. switch f := n.(type) {
  48. case *ast.Call:
  49. p.transform(f)
  50. }
  51. return true
  52. })
  53. }
  54. }
  55. func (p *HavingPlan) transform(f *ast.Call) {
  56. if _, ok := xsql.ImplicitStateFuncs[f.Name]; ok {
  57. f.Cached = true
  58. p.stateFuncs = append(p.stateFuncs, &ast.Call{
  59. Name: f.Name,
  60. FuncId: f.FuncId,
  61. FuncType: f.FuncType,
  62. })
  63. }
  64. }