Class: Bmg::Operator::Group

Inherits:
Object
  • Object
show all
Includes:
Unary
Defined in:
lib/bmg/operator/group.rb

Overview

Group operator.

Groups some operand attributes as a new Relation-valued attribute

Constant Summary collapse

DEFAULT_OPTIONS =
{

  # Whether we need to convert each group as an Array,
  # instead of keeping a Relation instance
  array: false

}

Instance Attribute Summary

Attributes included from Bmg::Operator

#type

Instance Method Summary collapse

Methods included from Unary

#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, operand, attrs, as, options) ⇒ Group

Returns a new instance of Group.



20
21
22
23
24
25
26
# File 'lib/bmg/operator/group.rb', line 20

def initialize(type, operand, attrs, as, options)
  @type = type
  @operand = operand
  @attrs = attrs
  @as = as
  @options = DEFAULT_OPTIONS.merge(options)
end

Instance Method Details

#each(&bl) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/bmg/operator/group.rb', line 34

def each(&bl)
  return to_enum unless block_given?
  index = Hash.new{|h,k| h[k] = k.merge(as => empty_group) }
  operand.each do |tuple|
    key = TupleAlgebra.allbut(tuple, attrs)
    sub = TupleAlgebra.project(tuple, attrs)
    index[key][as].operand << sub
  end
  if options[:array]
    index.values.each do |tuple|
      tuple[as] = tuple[as].to_a
      yield(tuple)
    end
  else
    index.values.each(&bl)
  end
end

#to_astObject



52
53
54
# File 'lib/bmg/operator/group.rb', line 52

def to_ast
  [ :group, operand.to_ast, attrs.dup, as, options.dup ]
end