Class: Bmg::Operator::Join

Inherits:
Object
  • Object
show all
Includes:
Binary
Defined in:
lib/bmg/operator/join.rb

Overview

Join operator.

Natural join, following relational algebra

Constant Summary collapse

DEFAULT_OPTIONS =
{}

Instance Attribute Summary

Attributes included from Bmg::Operator

#type

Instance Method Summary collapse

Methods included from Binary

#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, #not_matching, #page, #project, #rename, #restrict, #spied, #summarize, #transform, #ungroup, #union, #unspied, #unwrap

Methods included from Algebra::Shortcuts

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

Constructor Details

#initialize(type, left, right, on, options = {}) ⇒ Join

Returns a new instance of Join.



13
14
15
16
17
18
19
# File 'lib/bmg/operator/join.rb', line 13

def initialize(type, left, right, on, options = {})
  @type = type
  @left = left
  @right = right
  @on = on
  @options = DEFAULT_OPTIONS.merge(options)
end

Instance Method Details

#eachObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/bmg/operator/join.rb', line 27

def each
  return to_enum unless block_given?
  index = Hash.new
  right.each_with_object(index) do |t, index|
    key = tuple_project(t, on)
    index[key] ||= []
    index[key] << t
  end
  left.each do |tuple|
    key = tuple_project(tuple, on)
    if to_join = index[key]
      to_join.each do |right|
        yield right.merge(tuple)
      end
    elsif left_join?
      yield(tuple.merge(default_right_tuple))
    end
  end
end

#to_astObject



47
48
49
# File 'lib/bmg/operator/join.rb', line 47

def to_ast
  [ :join, left.to_ast, right.to_ast, on, extra_opts ].compact
end