windowPlan.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 WindowPlan struct {
  20. baseLogicalPlan
  21. triggerCondition ast.Expr
  22. condition ast.Expr
  23. wtype ast.WindowType
  24. delay int64
  25. length int
  26. interval int // If interval is not set, it is equals to Length
  27. timeUnit ast.Token
  28. limit int // If limit is not positive, there will be no limit
  29. isEventTime bool
  30. stateFuncs []*ast.Call
  31. }
  32. func (p WindowPlan) Init() *WindowPlan {
  33. p.baseLogicalPlan.self = &p
  34. return &p
  35. }
  36. func (p *WindowPlan) PushDownPredicate(condition ast.Expr) (ast.Expr, LogicalPlan) {
  37. // not time window depends on the event, so should not filter any
  38. if p.wtype == ast.COUNT_WINDOW || p.wtype == ast.SLIDING_WINDOW {
  39. return condition, p
  40. } else if p.isEventTime {
  41. // TODO event time filter, need event window op support
  42. //p.condition = combine(condition, p.condition)
  43. //// push nil condition won't return any
  44. //p.baseLogicalPlan.PushDownPredicate(nil)
  45. // return nil, p
  46. return condition, p
  47. } else {
  48. // Presume window condition are only one table related.
  49. // TODO window condition validation
  50. a := combine(condition, p.condition)
  51. p.condition, _ = p.baseLogicalPlan.PushDownPredicate(a)
  52. return nil, p
  53. }
  54. }
  55. func (p *WindowPlan) PruneColumns(fields []ast.Expr) error {
  56. f := getFields(p.condition)
  57. f = append(f, getFields(p.triggerCondition)...)
  58. return p.baseLogicalPlan.PruneColumns(append(fields, f...))
  59. }
  60. func (p *WindowPlan) ExtractStateFunc() {
  61. aliases := make(map[string]ast.Expr)
  62. ast.WalkFunc(p.triggerCondition, func(n ast.Node) bool {
  63. switch f := n.(type) {
  64. case *ast.Call:
  65. p.transform(f)
  66. case *ast.FieldRef:
  67. if f.AliasRef != nil {
  68. aliases[f.Name] = f.AliasRef.Expression
  69. }
  70. }
  71. return true
  72. })
  73. for _, ex := range aliases {
  74. ast.WalkFunc(ex, func(n ast.Node) bool {
  75. switch f := n.(type) {
  76. case *ast.Call:
  77. p.transform(f)
  78. }
  79. return true
  80. })
  81. }
  82. }
  83. func (p *WindowPlan) transform(f *ast.Call) {
  84. if _, ok := xsql.ImplicitStateFuncs[f.Name]; ok {
  85. f.Cached = true
  86. p.stateFuncs = append(p.stateFuncs, &ast.Call{
  87. Name: f.Name,
  88. FuncId: f.FuncId,
  89. FuncType: f.FuncType,
  90. })
  91. }
  92. }