logicalPlan.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2021-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 planner
  15. import "github.com/lf-edge/ekuiper/pkg/ast"
  16. type LogicalPlan interface {
  17. Children() []LogicalPlan
  18. SetChildren(children []LogicalPlan)
  19. // PushDownPredicate pushes down the filter in the filter/where/on/having clauses as deeply as possible.
  20. // It will accept a condition that is an expression slice, and return the expressions that can't be pushed.
  21. // It also return the new tree of plan as it can possibly change the tree
  22. PushDownPredicate(ast.Expr) (ast.Expr, LogicalPlan)
  23. // Prune the unused columns in the data source level, by pushing all needed columns down
  24. PruneColumns(fields []ast.Expr) error
  25. }
  26. type baseLogicalPlan struct {
  27. children []LogicalPlan
  28. // Can be used to return the derived instance from the base type
  29. self LogicalPlan
  30. }
  31. func (p *baseLogicalPlan) Children() []LogicalPlan {
  32. return p.children
  33. }
  34. func (p *baseLogicalPlan) SetChildren(children []LogicalPlan) {
  35. p.children = children
  36. }
  37. // PushDownPredicate By default, push down the predicate to the first child instead of the children
  38. // as most plan cannot have multiple children
  39. func (p *baseLogicalPlan) PushDownPredicate(condition ast.Expr) (ast.Expr, LogicalPlan) {
  40. if len(p.children) == 0 {
  41. return condition, p.self
  42. }
  43. rest := condition
  44. for i, child := range p.children {
  45. var newChild LogicalPlan
  46. rest, newChild = child.PushDownPredicate(rest)
  47. p.children[i] = newChild
  48. }
  49. return rest, p.self
  50. }
  51. func (p *baseLogicalPlan) PruneColumns(fields []ast.Expr) error {
  52. for _, child := range p.children {
  53. err := child.PruneColumns(fields)
  54. if err != nil {
  55. return err
  56. }
  57. }
  58. return nil
  59. }