Class: Piglet::Relation::Group

Inherits:
Object
  • Object
show all
Includes:
Relation
Defined in:
lib/piglet/relation/group.rb

Overview

:nodoc:

Instance Attribute Summary

Attributes included from Relation

#sources

Instance Method Summary collapse

Methods included from Relation

#[], #alias, #cogroup, #cross, #distinct, #eql?, #field, #filter, #foreach, #group, #hash, #join, #limit, #method_missing, #order, #sample, #split, #stream, #union

Constructor Details

#initialize(relation, grouping, options = {}) ⇒ Group

Returns a new instance of Group.



8
9
10
11
# File 'lib/piglet/relation/group.rb', line 8

def initialize(relation, grouping, options={})
  options ||= {}
  @sources, @grouping, @parallel = [relation], grouping, options[:parallel]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Piglet::Relation::Relation

Instance Method Details

#schemaObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/piglet/relation/group.rb', line 13

def schema
  parent = @sources.first
  parent_schema = parent.schema
  if @grouping.size == 1
    group_type = parent.schema.field_type(@grouping.first)
  else
    group_type = Piglet::Schema::Tuple.parse(
      @grouping.map { |field| [field, parent_schema.field_type(field)] }
    )
  end
  Piglet::Schema::Tuple.parse([
    [:group, group_type],
    [parent.alias.to_sym, Piglet::Schema::Bag.new(parent_schema)]
  ])
end

#to_sObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/piglet/relation/group.rb', line 29

def to_s
  str = "GROUP #{@sources.first.alias} BY "
  if @grouping.size > 1
    str << "(#{@grouping.join(', ')})"
  else
    str << @grouping.first.to_s
  end
  str << " PARALLEL #{@parallel}" if @parallel
  str
end