Class: NoSE::Plans::QueryPlanTree

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/nose/plans/query_planner.rb

Overview

A tree of possible query plans

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#partitions, #prefixes, #product_by, #sum_by

Constructor Details

#initialize(state, cost_model) ⇒ QueryPlanTree

Returns a new instance of QueryPlanTree.



92
93
94
95
# File 'lib/nose/plans/query_planner.rb', line 92

def initialize(state, cost_model)
  @root = RootPlanStep.new(state)
  @cost_model = cost_model
end

Instance Attribute Details

#cost_modelObject

Returns the value of attribute cost_model.



90
91
92
# File 'lib/nose/plans/query_planner.rb', line 90

def cost_model
  @cost_model
end

#rootObject (readonly)

Returns the value of attribute root.



89
90
91
# File 'lib/nose/plans/query_planner.rb', line 89

def root
  @root
end

Instance Method Details

#eachObject

Enumerate all plans in the tree



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/nose/plans/query_planner.rb', line 114

def each
  nodes = [@root]

  until nodes.empty?
    node = nodes.pop
    if node.children.empty?
      # This is just an extra check to make absolutely
      # sure we never consider invalid statement plans
      fail unless node.state.answered?

      yield node.parent_steps @cost_model
    else
      nodes.concat node.children.to_a
    end
  end
end

#queryQuery

The query this tree of plans is generated for

Returns:



109
110
111
# File 'lib/nose/plans/query_planner.rb', line 109

def query
  @root.state.query
end

#select_using_indexes(indexes) ⇒ Object

Select all plans which use only a given set of indexes



98
99
100
101
102
103
104
105
# File 'lib/nose/plans/query_planner.rb', line 98

def select_using_indexes(indexes)
  select do |plan|
    plan.all? do |step|
      !step.is_a?(Plans::IndexLookupPlanStep) ||
        indexes.include?(step.index)
    end
  end
end

#sizeInteger

Return the total number of plans for this statement

Returns:



133
134
135
# File 'lib/nose/plans/query_planner.rb', line 133

def size
  to_a.size
end

#to_color(step = nil, indent = 0) ⇒ Object

:nocov:



138
139
140
141
142
143
144
145
146
# File 'lib/nose/plans/query_planner.rb', line 138

def to_color(step = nil, indent = 0)
  step = @root if step.nil?
  this_step = '  ' * indent + step.to_color
  this_step << " [yellow]$#{step.cost.round 5}[/]" \
    unless step.is_a?(RootPlanStep) || step.cost.nil?
  this_step + "\n" + step.children.map do |child_step|
    to_color child_step, indent + 1
  end.reduce('', &:+)
end