Class: Masamune::Schema::Fact
- Inherits:
-
Table
- Object
- Table
- Masamune::Schema::Fact
show all
- Defined in:
- lib/masamune/schema/fact.rb
Constant Summary
collapse
- SUPPORTED_GRAINS =
i[transaction hourly daily monthly].freeze
Constants inherited
from Table
Table::DEFAULT_ATTRIBUTES
Instance Attribute Summary collapse
Attributes inherited from Table
#children
Instance Method Summary
collapse
Methods inherited from Table
#aliased_rows, #auto_surrogate_keys, #columns=, #defined_columns, #denormalized_column_names, #denormalized_columns, #dereference_column, #dereference_column_name, #enum_columns, #foreign_key_columns, #foreign_key_constraints, #id=, #index_columns, #insert_references, #insert_rows, #lock_id, #name, #natural_keys, #partitions, #reference_columns, #references=, #reserved_columns, #rows=, #sequence_columns, #shared_columns, #surrogate_key, #temporary?, #unique_columns, #unique_constraints, #unreserved_columns
#last_element
Constructor Details
#initialize(opts = {}) ⇒ Fact
Returns a new instance of Fact.
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/masamune/schema/fact.rb', line 31
def initialize(opts = {})
opts.symbolize_keys!
self.grain = opts.delete(:grain)
@partition = opts.delete(:partition)
super opts.reverse_merge(type: :fact)
initialize_fact_columns!
reference_columns.each do |column|
column.index.clear
column.index << column.name
if type == :stage
column.index << "#{column.name}_time_key"
time_key.index << "#{column.name}_time_key"
end
end
time_key.index << time_key.name
end
|
Instance Attribute Details
#grain ⇒ Object
Returns the value of attribute grain.
27
28
29
|
# File 'lib/masamune/schema/fact.rb', line 27
def grain
@grain
end
|
#partition ⇒ Object
Returns the value of attribute partition.
28
29
30
|
# File 'lib/masamune/schema/fact.rb', line 28
def partition
@partition
end
|
#range ⇒ Object
Returns the value of attribute range.
29
30
31
|
# File 'lib/masamune/schema/fact.rb', line 29
def range
@range
end
|
Instance Method Details
#date_column ⇒ Object
63
64
65
|
# File 'lib/masamune/schema/fact.rb', line 63
def date_column
columns.select { |_, column| column && column.reference && column.reference.type == :date }.values.first
end
|
#id ⇒ Object
48
49
50
|
# File 'lib/masamune/schema/fact.rb', line 48
def id
[@id, grain].compact.join('_').to_sym
end
|
#inheritance_constraints ⇒ Object
109
110
111
112
|
# File 'lib/masamune/schema/fact.rb', line 109
def inheritance_constraints
return unless range
"CHECK (time_key >= #{range.start_time.to_i} AND time_key < #{range.stop_time.to_i})"
end
|
#measures ⇒ Object
105
106
107
|
# File 'lib/masamune/schema/fact.rb', line 105
def measures
columns.select { |_, column| column.measure }
end
|
#partition_table(date = nil) ⇒ Object
88
89
90
91
92
93
94
|
# File 'lib/masamune/schema/fact.rb', line 88
def partition_table(date = nil)
return unless partition
return unless date
partition_range = partition_rule.bind_date_or_time(date)
@partition_tables ||= {}
@partition_tables[partition_range] ||= self.class.new(id: @id, store: store, columns: partition_table_columns, parent: self, range: partition_range, grain: grain, inherit: true)
end
|
#partition_tables(start_date = nil, stop_date = nil) ⇒ Object
96
97
98
99
100
101
102
103
|
# File 'lib/masamune/schema/fact.rb', line 96
def partition_tables(start_date = nil, stop_date = nil)
return unless partition
return unless start_date && stop_date
(start_date..stop_date).each do |date|
next unless date.day == 1
yield partition_table(date)
end
end
|
#primary_keys ⇒ Object
67
68
69
|
# File 'lib/masamune/schema/fact.rb', line 67
def primary_keys
[]
end
|
#reserved_column_ids ⇒ Object
114
115
116
117
118
119
120
121
|
# File 'lib/masamune/schema/fact.rb', line 114
def reserved_column_ids
case type
when :fact
i[time_key last_modified_at]
else
super
end
end
|
#stage_table(options = {}) ⇒ Object
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/masamune/schema/fact.rb', line 75
def stage_table(options = {})
super(options).tap do |stage|
stage.id = @id
stage.suffix = options[:suffix]
stage.store = store
stage.range = range
stage.grain = grain
stage.columns.each do |_, column|
column.unique = false
end
end
end
|
#suffix ⇒ Object
58
59
60
61
|
# File 'lib/masamune/schema/fact.rb', line 58
def suffix
inherited = super
[*inherited.split('_'), range.try(:suffix)].compact.uniq.join('_')
end
|
#time_key ⇒ Object
71
72
73
|
# File 'lib/masamune/schema/fact.rb', line 71
def time_key
columns.values.detect { |column| column.id == :time_key }
end
|