Module: ObjectTable::Joining::ClassMethods

Included in:
ObjectTable
Defined in:
lib/object_table/joining.rb

Instance Method Summary collapse

Instance Method Details

#join(left, right, *keys, type: 'inner') ⇒ Object

-1 in an index indicates a missing value



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/object_table/joining.rb', line 13

def join(left, right, *keys, type: 'inner')
  incl_left = incl_right = false
  case type
  when 'left' then incl_left = true
  when 'right' then incl_right = true
  when 'outer' then incl_left = incl_right = true
  when 'inner'
  else raise "Expected one of (inner, left, outer, right), got #{type.inspect}"
  end

  lkeys = Util.get_rows(left, keys)
  rkeys = Util.get_rows(right, keys)

  rgroups = Util.group_indices(rkeys)
  rgroups.default = (incl_left ? [-1] : []) # keep left missing values if incl_left

  lindex = rgroups.values_at(*lkeys)
  rindex = lindex.flatten
  lindex = lindex.each_with_index.flat_map{|r, i| r.fill(i)}

  if incl_right
    # rindex may have missing values
    # so add a dud value at the end ...
    missing = NArray.int(right.nrows + 1).fill!(1)
    missing[rindex] = 0
    missing[-1] = 0 # ... that we always exclude
    missing = missing.where
    rindex.concat( missing.to_a )
    lindex.concat( Array.new(missing.length, -1) )
  end

  index = [lindex, rindex].map{|ix| NArray.to_na(ix)}
  blanks = index.map{|ix| ix.eq(-1).where}

  colnames = [left.colnames, right.colnames - left.colnames]
  data = [left, right].zip(index, blanks).zip(colnames).flat_map do |args, cols|
    cols.map{|k| [k, Joining.copy_column(k, *args)] }
  end

  table = __table_cls__.new(data)
  if incl_right
    keys.each do |k|
      table[k][false, blanks[0]] = right[k][false, missing]
    end
  end

  table
end