Class: Hotdog::Expression::BinaryExpressionNode

Inherits:
ExpressionNode show all
Defined in:
lib/hotdog/expression/semantics.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(op, left, right) ⇒ BinaryExpressionNode

Returns a new instance of BinaryExpressionNode.



122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/hotdog/expression/semantics.rb', line 122

def initialize(op, left, right)
  case (op || "or").to_s
  when "&&", "&", "AND", "and"
    @op = :AND
  when ",", "||", "|", "OR", "or"
    @op = :OR
  when "^", "XOR", "xor"
    @op = :XOR
  else
    raise(SyntaxError.new("unknown binary operator: #{op.inspect}"))
  end
  @left = left
  @right = right
end

Instance Attribute Details

#leftObject (readonly)

Returns the value of attribute left.



120
121
122
# File 'lib/hotdog/expression/semantics.rb', line 120

def left
  @left
end

#opObject (readonly)

Returns the value of attribute op.



120
121
122
# File 'lib/hotdog/expression/semantics.rb', line 120

def op
  @op
end

#rightObject (readonly)

Returns the value of attribute right.



120
121
122
# File 'lib/hotdog/expression/semantics.rb', line 120

def right
  @right
end

Instance Method Details

#==(other) ⇒ Object



284
285
286
# File 'lib/hotdog/expression/semantics.rb', line 284

def ==(other)
  self.class === other and @op == other.op and @left == other.left and @right == other.right
end

#dump(options = {}) ⇒ Object



288
289
290
# File 'lib/hotdog/expression/semantics.rb', line 288

def dump(options={})
  {left: @left.dump(options), binary_op: @op.to_s, right: @right.dump(options)}
end

#evaluate(environment, options = {}) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/hotdog/expression/semantics.rb', line 137

def evaluate(environment, options={})
  case @op
  when :AND
    left_values = @left.evaluate(environment, options).tap do |values|
      environment.logger.debug("lhs: #{values.length} value(s)")
    end
    if left_values.empty?
      []
    else
      right_values = @right.evaluate(environment, options).tap do |values|
        environment.logger.debug("rhs: #{values.length} value(s)")
      end
      if right_values.empty?
        []
      else
        # workaround for "too many terms in compound SELECT"
        min, max = environment.execute("SELECT MIN(id), MAX(id) FROM hosts LIMIT 1;").first.to_a
        (min / ((SQLITE_LIMIT_COMPOUND_SELECT - 2) / 2)).upto(max / ((SQLITE_LIMIT_COMPOUND_SELECT - 2) / 2)).flat_map { |i|
          range = (((SQLITE_LIMIT_COMPOUND_SELECT - 2) / 2) * i)...(((SQLITE_LIMIT_COMPOUND_SELECT - 2) / 2) * (i + 1))
          left_selected = left_values.select { |n| range === n }
          right_selected = right_values.select { |n| range === n }
          q = "SELECT id FROM hosts " \
                "WHERE ? <= id AND id < ? AND ( id IN (%s) AND id IN (%s) );"
          environment.execute(q % [left_selected.map { "?" }.join(", "), right_selected.map { "?" }.join(", ")], [range.first, range.last] + left_selected + right_selected).map { |row| row.first }
        }.tap do |values|
          environment.logger.debug("lhs AND rhs: #{values.length} value(s)")
        end
      end
    end
  when :OR
    left_values = @left.evaluate(environment, options).tap do |values|
      environment.logger.debug("lhs: #{values.length} value(s)")
    end
    right_values = @right.evaluate(environment, options).tap do |values|
      environment.logger.debug("rhs: #{values.length} value(s)")
    end
    if left_values.empty?
      right_values
    else
      if right_values.empty?
        []
      else
        # workaround for "too many terms in compound SELECT"
        min, max = environment.execute("SELECT MIN(id), MAX(id) FROM hosts LIMIT 1;").first.to_a
        (min / ((SQLITE_LIMIT_COMPOUND_SELECT - 2) / 2)).upto(max / ((SQLITE_LIMIT_COMPOUND_SELECT - 2) / 2)).flat_map { |i|
          range = (((SQLITE_LIMIT_COMPOUND_SELECT - 2) / 2) * i)...(((SQLITE_LIMIT_COMPOUND_SELECT - 2) / 2) * (i + 1))
          left_selected = left_values.select { |n| range === n }
          right_selected = right_values.select { |n| range === n }
          q = "SELECT id FROM hosts " \
                "WHERE ? <= id AND id < ? AND ( id IN (%s) OR id IN (%s) );"
          environment.execute(q % [left_selected.map { "?" }.join(", "), right_selected.map { "?" }.join(", ")], [range.first, range.last] + left_selected + right_selected).map { |row| row.first }
        }.tap do |values|
          environment.logger.debug("lhs OR rhs: #{values.length} value(s)")
        end
      end
    end
  when :XOR
    left_values = @left.evaluate(environment, options).tap do |values|
      environment.logger.debug("lhs: #{values.length} value(s)")
    end
    right_values = @right.evaluate(environment, options).tap do |values|
      environment.logger.debug("rhs: #{values.length} value(s)")
    end
    if left_values.empty?
      right_values
    else
      if right_values.empty?
        []
      else
        # workaround for "too many terms in compound SELECT"
        min, max = environment.execute("SELECT MIN(id), MAX(id) FROM hosts LIMIT 1;").first.to_a
        (min / ((SQLITE_LIMIT_COMPOUND_SELECT - 2) / 4)).upto(max / ((SQLITE_LIMIT_COMPOUND_SELECT - 2) / 4)).flat_map { |i|
          range = (((SQLITE_LIMIT_COMPOUND_SELECT - 2) / 4) * i)...(((SQLITE_LIMIT_COMPOUND_SELECT - 2) / 4) * (i + 1))
          left_selected = left_values.select { |n| range === n }
          right_selected = right_values.select { |n| range === n }
          q = "SELECT id FROM hosts " \
                "WHERE ? <= id AND id < ? AND NOT (id IN (%s) AND id IN (%s)) AND ( id IN (%s) OR id IN (%s) );"
          lq = left_selected.map { "?" }.join(", ")
          rq = right_selected.map { "?" }.join(", ")
          environment.execute(q % [lq, rq, lq, rq], [range.first, range.last] + left_selected + right_selected + left_selected + right_selected).map { |row| row.first }
        }.tap do |values|
          environment.logger.debug("lhs XOR rhs: #{values.length} value(s)")
        end
      end
    end
  else
    []
  end
end

#optimize(options = {}) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/hotdog/expression/semantics.rb', line 227

def optimize(options={})
  @left = @left.optimize(options)
  @right = @right.optimize(options)
  case op
  when :AND
    case left
    when EverythingNode
      right
    when NothingNode
      left
    else
      if left == right
        left
      else
        optimize1(options)
      end
    end
  when :OR
    case left
    when EverythingNode
      left
    when NothingNode
      right
    else
      if left == right
        left
      else
        if MultinaryExpressionNode === left
          if left.op == op
            left.merge(right, fallback: self)
          else
            optimize1(options)
          end
        else
          if MultinaryExpressionNode === right
            if right.op == op
              right.merge(left, fallback: self)
            else
              optimize1(options)
            end
          else
            MultinaryExpressionNode.new(op, [left, right], fallback: self)
          end
        end
      end
    end
  when :XOR
    if left == right
      []
    else
      optimize1(options)
    end
  else
    self
  end
end