Class: Alf::Optimizer::Project

Inherits:
Base show all
Defined in:
lib/alf/optimizer/project.rb

Instance Method Summary collapse

Methods inherited from Base

#call

Methods included from Lang::Functional

#Heading, #Relation, #Tuple, def_aggregator_method, def_operator_method

Methods inherited from Algebra::Rewriter

#call, #not_supported, #on_shortcut

Methods included from Algebra::Visitor

#copy_and_apply

Instance Method Details

#_call(project, search) ⇒ Object


17
18
19
# File 'lib/alf/optimizer/project.rb', line 17

def _call(project, search)
  apply(project.operand, project.attributes, project.allbut, search)
end

#apply(expr, attributes, allbut, search) ⇒ Object


21
22
23
24
25
# File 'lib/alf/optimizer/project.rb', line 21

def apply(expr, attributes, allbut, search)
  check_stop(expr, attributes, allbut, search){ super }
rescue NotSupportedError
  project(expr, attributes, allbut: allbut)
end

#check_stop(expr, attributes, allbut, search) ⇒ Object


27
28
29
30
31
32
33
# File 'lib/alf/optimizer/project.rb', line 27

def check_stop(expr, attributes, allbut, search)
  return search.call(expr) if (allbut  && attributes.empty?)
  return search.call(expr) if (!allbut && attributes==expr.attr_list)
  yield
rescue NotSupportedError
  yield
end

#on_binary_splitable(expr, attributes, allbut, search) ⇒ Object Also known as: on_matching, on_not_matching


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/alf/optimizer/project.rb', line 103

def on_binary_splitable(expr, attributes, allbut, search)
  commons = expr.left.attr_list & expr.right.attr_list

  # compute inside and outside projection attributes such as preserving
  # commons
  inside  = allbut ? attributes - commons : attributes + commons
  outside = allbut ? attributes - inside  : attributes

  # apply on left operand with inside attributes
  left  = apply(expr.left, inside, allbut, search)
  right = expr.right

  # rewrite it now
  rw = expr.class.new([left, right])

  # add the outside projection unless the job is already done by the
  # inside projection
  rw = project(rw, outside, allbut: allbut) unless inside == attributes

  rw
end

#on_clip(expr, attributes, allbut, search) ⇒ Object Also known as: on_project


48
49
50
51
52
53
54
55
56
57
# File 'lib/alf/optimizer/project.rb', line 48

def on_clip(expr, attributes, allbut, search)
  inside, outside = expr.attributes, attributes
  if expr.allbut != allbut
    attributes = allbut ? inside - outside : outside - inside
    allbut = false
  else
    attributes = allbut ? inside | outside : inside & outside
  end
  apply(expr.operand, attributes, allbut, search)
end

#on_extend(expr, attributes, allbut, search) ⇒ Object


63
64
65
66
67
68
69
70
71
# File 'lib/alf/optimizer/project.rb', line 63

def on_extend(expr, attributes, allbut, search)
  ext = expr.ext.project(attributes, allbut)
  if ext.empty?
    apply(expr.operand, attributes, allbut, search)
  else
    op = search.call(expr.operand)
    project(extend(op, ext), attributes, allbut: allbut)
  end
end

#on_join(expr, attributes, allbut, search) ⇒ Object


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/alf/optimizer/project.rb', line 79

def on_join(expr, attributes, allbut, search)
  commons = expr.left.attr_list & expr.right.attr_list

  # compute actual projection attributes on left and right
  proj_a  = allbut ? attributes - commons : attributes + commons
  left_a  = expr.left.attr_list & proj_a
  right_a = expr.right.attr_list & proj_a

  # make both projections
  left  = apply(expr.left, left_a, allbut, search)
  right = apply(expr.right, right_a, allbut, search)

  # re-apply the join
  rw = join(left, right)

  # add the outer projection unless the job has already been done
  unless attributes == (left_a + right_a)
    outside = allbut ? attributes - left_a - right_a : attributes
    rw = project(rw, outside, allbut: allbut)
  end

  rw
end

#on_rename(expr, attributes, allbut, search) ⇒ Object


132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/alf/optimizer/project.rb', line 132

def on_rename(expr, attributes, allbut, search)
  inside = expr.renaming.invert.rename_attr_list(attributes)

  # project the operand on renamed attributes
  rw = apply(expr.operand, inside, allbut, search)

  # apply the renaming unless all attributes have been projected away
  renaming = inside.project_tuple(expr.renaming.to_hash, allbut)
  unless renaming.empty?
    rw = rename(rw, renaming)
  end

  rw
end

#on_sort(expr, attributes, allbut, search) ⇒ Object


149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/alf/optimizer/project.rb', line 149

def on_sort(expr, attributes, allbut, search)
  sort_a = expr.ordering.to_attr_list

  # compute inside projection attributes
  inside = allbut ? attributes - sort_a : attributes + sort_a

  # project the operand and sort the result
  rw = apply(expr.operand, inside, allbut, search)
  rw = sort(rw, expr.ordering)

  # project unless the job has already been done
  unless inside == attributes
    outside = allbut ? attributes - inside : attributes
    rw = project(rw, outside, allbut: allbut)
  end

  rw
end

#on_union(expr, attributes, allbut, search) ⇒ Object


171
172
173
174
175
# File 'lib/alf/optimizer/project.rb', line 171

def on_union(expr, attributes, allbut, search)
  left  = apply(expr.left, attributes, allbut, search)
  right = apply(expr.right, attributes, allbut, search)
  union(left, right)
end

#on_unoptimizable(expr, attributes, allbut, search) ⇒ Object Also known as: on_missing, on_todo, on_leaf_operand, on_generator, on_hierarchize, on_intersect, on_minus

callbacks


37
38
39
# File 'lib/alf/optimizer/project.rb', line 37

def on_unoptimizable(expr, attributes, allbut, search)
  project(search.call(expr), attributes, allbut: allbut)
end

#project(operand, attributes, options) ⇒ Object


7
8
9
# File 'lib/alf/optimizer/project.rb', line 7

def project(operand, attributes, options)
  options[:allbut] ? allbut(operand, attributes) : super
end

#search_predicateObject

overridings


13
14
15
# File 'lib/alf/optimizer/project.rb', line 13

def search_predicate
  Algebra::Project
end