Module: ActiveWarehouse::Aggregate::DwarfCommon

Included in:
DwarfAggregate, PidAggregate
Defined in:
lib/active_warehouse/aggregate/dwarf_common.rb

Overview

Common methods for use inside dwarf implementations

Instance Method Summary collapse

Instance Method Details

#create_tuple(row) ⇒ Object

Create a tuple from a row



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/active_warehouse/aggregate/dwarf_common.rb', line 28

def create_tuple(row)
  fact_class = cube_class.fact_class
  tuple = []
  dimension_order.each do |d|
    column_name = fact_class.foreign_key_for(d)
    tuple << fact_class.columns_hash[column_name].type_cast(row[column_name])
  end
  fact_class.aggregate_fields.each do |f|
    tuple << fact_class.columns_hash[f.to_s].type_cast(row[f.to_s])
  end
  #puts "tuple: #{tuple.inspect}"
  tuple
end

#dimension_orderObject

Get the dimension order, defaults to sorting from highest cardinality to lowest



6
7
8
# File 'lib/active_warehouse/aggregate/dwarf_common.rb', line 6

def dimension_order
  @dimension_order ||= cube_class.dimension_classes.sort { |a, b| a.count <=> b.count }.reverse
end

#dimension_order=(dimensions) ⇒ Object

Set the dimension order



11
12
13
# File 'lib/active_warehouse/aggregate/dwarf_common.rb', line 11

def dimension_order=(dimensions)
  @dimension_order = dimensions
end

#sorted_factsObject

Get the sorted fact rows for this cube, sorted by dimensions returned from dimension_order.



16
17
18
19
20
21
22
23
24
25
# File 'lib/active_warehouse/aggregate/dwarf_common.rb', line 16

def sorted_facts
  #puts "dimension order: #{dimension_order.inspect}"
  # Determine the dimension to order by (high cardinality)
  order_by = dimension_order.collect { |d| cube_class.fact_class.foreign_key_for(d) }.join(",")
  
  # Get the sorted fact table
  # TODO: determine if querying with select_all will bring the entire result set into memory
  sql = "SELECT * FROM #{cube_class.fact_class.table_name} ORDER BY #{order_by}"
  cube_class.connection.select_all(sql)
end