logicalPlan.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package planner
  2. import "github.com/emqx/kuiper/xsql"
  3. type LogicalPlan interface {
  4. Children() []LogicalPlan
  5. SetChildren(children []LogicalPlan)
  6. // PushDownPredicate pushes down the filter in the filter/where/on/having clauses as deeply as possible.
  7. // It will accept a condition that is an expression slice, and return the expressions that can't be pushed.
  8. // It also return the new tree of plan as it can possibly change the tree
  9. PushDownPredicate(xsql.Expr) (xsql.Expr, LogicalPlan)
  10. }
  11. type baseLogicalPlan struct {
  12. children []LogicalPlan
  13. // Can be used to return the derived instance from the base type
  14. self LogicalPlan
  15. }
  16. func (p *baseLogicalPlan) Children() []LogicalPlan {
  17. return p.children
  18. }
  19. func (p *baseLogicalPlan) SetChildren(children []LogicalPlan) {
  20. p.children = children
  21. }
  22. // By default, push down the predicate to the first child instead of the children
  23. // as most plan cannot have multiple children
  24. func (p *baseLogicalPlan) PushDownPredicate(condition xsql.Expr) (xsql.Expr, LogicalPlan) {
  25. if len(p.children) == 0 {
  26. return condition, p.self
  27. }
  28. rest := condition
  29. for i, child := range p.children {
  30. var newChild LogicalPlan
  31. rest, newChild = child.PushDownPredicate(rest)
  32. p.children[i] = newChild
  33. }
  34. return rest, p.self
  35. }