logicalPlan.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. "bytes"
  17. "encoding/json"
  18. "github.com/lf-edge/ekuiper/pkg/ast"
  19. )
  20. type LogicalPlan interface {
  21. ExplainInfo
  22. Children() []LogicalPlan
  23. SetChildren(children []LogicalPlan)
  24. // PushDownPredicate pushes down the filter in the filter/where/on/having clauses as deeply as possible.
  25. // It will accept a condition that is an expression slice, and return the expressions that can't be pushed.
  26. // It is also return the new tree of plan as it can possibly change the tree
  27. PushDownPredicate(ast.Expr) (ast.Expr, LogicalPlan)
  28. // PruneColumns Prune the unused columns in the data source level, by pushing all needed columns down
  29. PruneColumns(fields []ast.Expr) error
  30. }
  31. type baseLogicalPlan struct {
  32. children []LogicalPlan
  33. // Can be used to return the derived instance from the base type
  34. self LogicalPlan
  35. // Interface for explaining
  36. ExplainInfo PlanExplainInfo
  37. }
  38. type ExplainInfo interface {
  39. ID() int64
  40. Type() string
  41. ChildrenID() []int64
  42. Explain() string
  43. BuildExplainInfo(id int64)
  44. }
  45. type PlanExplainInfo struct {
  46. T PlanType `json:"type"`
  47. Info string `json:"info"`
  48. ID int64 `json:"id"`
  49. Children []int64 `json:"children"`
  50. }
  51. func (p *baseLogicalPlan) Explain() string {
  52. p.ExplainInfo.Children = p.ChildrenID()
  53. bf := bytes.NewBuffer([]byte{})
  54. jsonEncoder := json.NewEncoder(bf)
  55. jsonEncoder.SetEscapeHTML(true)
  56. jsonEncoder.Encode(p.ExplainInfo)
  57. return bf.String()
  58. }
  59. func (p *baseLogicalPlan) BuildExplainInfo(id int64) {
  60. p.self.BuildExplainInfo(id)
  61. }
  62. func (p *baseLogicalPlan) Type() string {
  63. return string(p.ExplainInfo.T)
  64. }
  65. func (p *baseLogicalPlan) ID() int64 {
  66. return p.ExplainInfo.ID
  67. }
  68. func (p *baseLogicalPlan) ChildrenID() []int64 {
  69. var children []int64
  70. for _, child := range p.Children() {
  71. children = append(children, child.ID())
  72. }
  73. return children
  74. }
  75. func (p *baseLogicalPlan) setPlanType(planType PlanType) {
  76. p.ExplainInfo.T = planType
  77. }
  78. func (p *baseLogicalPlan) Children() []LogicalPlan {
  79. return p.children
  80. }
  81. func (p *baseLogicalPlan) SetChildren(children []LogicalPlan) {
  82. p.children = children
  83. }
  84. // PushDownPredicate By default, push down the predicate to the first child instead of the children
  85. // as most plan cannot have multiple children
  86. func (p *baseLogicalPlan) PushDownPredicate(condition ast.Expr) (ast.Expr, LogicalPlan) {
  87. if len(p.children) == 0 {
  88. return condition, p.self
  89. }
  90. rest := condition
  91. for i, child := range p.children {
  92. var newChild LogicalPlan
  93. rest, newChild = child.PushDownPredicate(rest)
  94. p.children[i] = newChild
  95. }
  96. return rest, p.self
  97. }
  98. func (p *baseLogicalPlan) PruneColumns(fields []ast.Expr) error {
  99. for _, child := range p.children {
  100. err := child.PruneColumns(fields)
  101. if err != nil {
  102. return err
  103. }
  104. }
  105. return nil
  106. }
  107. type PlanType string
  108. const (
  109. AGGREGATE PlanType = "AggregatePlan"
  110. ANALYTICFUNCS PlanType = "AnalyticFuncsPlan"
  111. DATASOURCE PlanType = "DataSourcePlan"
  112. FILTER PlanType = "FilterPlan"
  113. HAVING PlanType = "HavingPlan"
  114. JOINALIGN PlanType = "JoinAlignPlan"
  115. JOIN PlanType = "JoinPlan"
  116. LOOKUP PlanType = "LookupPlan"
  117. ORDER PlanType = "OrderPlan"
  118. PROJECT PlanType = "ProjectPlan"
  119. PROJECTSET PlanType = "ProjectSetPlan"
  120. WINDOW PlanType = "WindowPlan"
  121. )