Class: Bmg::Operator::Union

Inherits:
Object
  • Object
show all
Includes:
Nary
Defined in:
lib/bmg/operator/union.rb

Overview

Union operator.

Returns all tuples of the left operand followed by all tuples from the right operand.

This implementation is actually a NAry-Union, since it handles an arbitrary number of operands.

By default, this operator strips duplicates, as of relational theory. Please set the ‘:all` option to true to avoid this behavior and save execution time.

Constant Summary collapse

DEFAULT_OPTIONS =
{
  all: false
}

Instance Attribute Summary

Attributes included from Bmg::Operator

#type

Instance Method Summary collapse

Methods included from Nary

#bind

Methods included from Bmg::Operator

#inspect, #to_s

Methods included from Relation

#_count, #bind, #count, #debug, #delete, empty, #empty?, #insert, new, #one, #one_or_nil, #to_csv, #to_json, #to_xlsx, #type, #update, #visit, #with_type, #with_type_attrlist, #with_typecheck, #without_typecheck, #y_by_x, #ys_by_x

Methods included from Algebra

#allbut, #autosummarize, #autowrap, #constants, #extend, #group, #image, #join, #left_join, #matching, #materialize, #minus, #not_matching, #page, #project, #rename, #restrict, #spied, #summarize, #transform, #ungroup, #union, #unspied, #unwrap

Methods included from Algebra::Shortcuts

#cross_product, #exclude, #image, #images, #join, #left_join, #matching, #not_matching, #prefix, #rxmatch, #suffix, #ungroup, #unwrap, #where

Constructor Details

#initialize(type, operands, options = {}) ⇒ Union

Returns a new instance of Union.



23
24
25
26
27
# File 'lib/bmg/operator/union.rb', line 23

def initialize(type, operands, options = {})
  @type = type
  @operands = operands
  @options = DEFAULT_OPTIONS.merge(options)
end

Instance Method Details

#all?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/bmg/operator/union.rb', line 35

def all?
  @options[:all]
end

#each(&bl) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/bmg/operator/union.rb', line 39

def each(&bl)
  return to_enum unless block_given?
  if all?
    operands.each do |op|
      op.each(&bl)
    end
  else
    seen = {}
    operands.each do |op|
      op.each do |tuple|
        yield(tuple) unless seen.has_key?(tuple)
        seen[tuple] = true
      end
    end
  end
end

#to_astObject



56
57
58
# File 'lib/bmg/operator/union.rb', line 56

def to_ast
  [ :union ] + operands.map(&:to_ast) + [ options.dup ]
end