Class: Hotdog::Expression::BinaryExpressionNode
- Inherits:
-
ExpressionNode
- Object
- ExpressionNode
- Hotdog::Expression::BinaryExpressionNode
- Defined in:
- lib/hotdog/expression/semantics.rb
Instance Attribute Summary collapse
-
#left ⇒ Object
readonly
Returns the value of attribute left.
-
#op ⇒ Object
readonly
Returns the value of attribute op.
-
#right ⇒ Object
readonly
Returns the value of attribute right.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #dump(options = {}) ⇒ Object
- #evaluate(environment, options = {}) ⇒ Object
-
#initialize(op, left, right, options = {}) ⇒ BinaryExpressionNode
constructor
A new instance of BinaryExpressionNode.
- #optimize(options = {}) ⇒ Object
Methods inherited from ExpressionNode
Constructor Details
#initialize(op, left, right, options = {}) ⇒ BinaryExpressionNode
Returns a new instance of BinaryExpressionNode.
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/hotdog/expression/semantics.rb', line 170 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
#left ⇒ Object (readonly)
Returns the value of attribute left.
168 169 170 |
# File 'lib/hotdog/expression/semantics.rb', line 168 def left @left end |
#op ⇒ Object (readonly)
Returns the value of attribute op.
168 169 170 |
# File 'lib/hotdog/expression/semantics.rb', line 168 def op @op end |
#right ⇒ Object (readonly)
Returns the value of attribute right.
168 169 170 |
# File 'lib/hotdog/expression/semantics.rb', line 168 def right @right end |
Instance Method Details
#==(other) ⇒ Object
364 365 366 |
# File 'lib/hotdog/expression/semantics.rb', line 364 def ==(other) self.class === other and @op == other.op and @left == other.left and @right == other.right end |
#dump(options = {}) ⇒ Object
368 369 370 |
# File 'lib/hotdog/expression/semantics.rb', line 368 def dump(={}) {left: @left.dump(), binary_op: @op.to_s, right: @right.dump()} end |
#evaluate(environment, options = {}) ⇒ Object
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 226 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 283 284 285 286 287 288 289 |
# File 'lib/hotdog/expression/semantics.rb', line 186 def evaluate(environment, ={}) case @op when :AND left_values = @left.evaluate(environment, ).tap do |values| environment.logger.debug("lhs(#{values.length})") end if left_values.empty? [] else right_values = @right.evaluate(environment, ).tap do |values| environment.logger.debug("rhs(#{values.length})") 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 sqlite_limit_compound_select = [:sqlite_limit_compound_select] || SQLITE_LIMIT_COMPOUND_SELECT (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 } if 0 < left_selected.length and 0 < right_selected.length 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 } else [] end }.tap do |values| environment.logger.debug("lhs(#{left_values.length}) AND rhs(#{right_values.length}) => #{values.length}") end end end when :OR left_values = @left.evaluate(environment, ).tap do |values| environment.logger.debug("lhs(#{values.length})") end right_values = @right.evaluate(environment, ).tap do |values| environment.logger.debug("rhs(#{values.length})") 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 sqlite_limit_compound_select = [:sqlite_limit_compound_select] || SQLITE_LIMIT_COMPOUND_SELECT (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 } if 0 < left_selected.length or 0 < right_selected.length 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 } else [] end }.tap do |values| environment.logger.debug("lhs(#{left_values.length}) OR rhs(#{right_values.length}) => #{values.length}") end end end when :XOR left_values = @left.evaluate(environment, ).tap do |values| environment.logger.debug("lhs(#{values.length})") end right_values = @right.evaluate(environment, ).tap do |values| environment.logger.debug("rhs(#{values.length})") 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 sqlite_limit_compound_select = [:sqlite_limit_compound_select] || SQLITE_LIMIT_COMPOUND_SELECT (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 } if 0 < left_selected.length or 0 < right_selected.length 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 } else [] end }.tap do |values| environment.logger.debug("lhs(#{left_values.length}) XOR rhs(#{right_values.length}) => #{values.length}") end end end else [] end end |
#optimize(options = {}) ⇒ Object
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
# File 'lib/hotdog/expression/semantics.rb', line 291 def optimize(={}) o_left = @left.optimize() o_right = @right.optimize() case op when :AND case o_left when EverythingNode o_right when NothingNode o_left else if o_left == o_right o_left else BinaryExpressionNode.new( op, o_left, o_right, ).optimize1() end end when :OR case o_left when EverythingNode o_left when NothingNode o_right else if o_left == o_right o_left else if MultinaryExpressionNode === o_left if o_left.op == op o_left.merge(o_right, fallback: self) else BinaryExpressionNode.new( op, o_left, o_right, ).optimize1() end else if MultinaryExpressionNode === o_right if o_right.op == op o_right.merge(o_left, fallback: self) else BinaryExpressionNode.new( op, o_left, o_right, ).optimize1() end else MultinaryExpressionNode.new(op, [o_left, o_right], fallback: self) end end end end when :XOR if o_left == o_right NothingNode.new() else BinaryExpressionNode.new( op, o_left, o_right, ).optimize1() end else self.dup end end |