Class: ActiveDataFrame::Table
- Inherits:
-
DataFrameProxy
- Object
- DataFrameProxy
- ActiveDataFrame::Table
- Defined in:
- lib/active_data_frame/table.rb
Instance Attribute Summary
Attributes inherited from DataFrameProxy
#block_type, #block_type_name, #data_frame_type, #plural_df_name, #singular_df_name, #value_map
Instance Method Summary collapse
- #AggregateProxy(agg) ⇒ Object
- #avg ⇒ Object
- #build_case_map(all_bounds) ⇒ Object
- #column_cases(cases, aggregation_function = nil) ⇒ Object
- #get(ranges) ⇒ Object
- #idx_where_sum_gte(*ranges, max) ⇒ Object
- #idx_where_sum_lte(*ranges, min) ⇒ Object
- #inspect ⇒ Object
- #max ⇒ Object
- #min ⇒ Object
- #set(from, values, trim: false) ⇒ Object
- #sum ⇒ Object
Methods inherited from DataFrameProxy
#[], #[]=, #blocks_between, #clear, #column_map, #column_name_map, #database, #extract_ranges, #get_bounds, get_bounds, #initialize, iterate_bounds, #iterate_bounds, #match_range, #method_missing, #range_size, #reverse_column_map, #reverse_value_map, suppress_logs, #unmap_ranges, #verify_and_cleanse_hash_values
Constructor Details
This class inherits a constructor from ActiveDataFrame::DataFrameProxy
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class ActiveDataFrame::DataFrameProxy
Instance Method Details
#AggregateProxy(agg) ⇒ Object
168 169 170 171 172 173 174 175 |
# File 'lib/active_data_frame/table.rb', line 168 def AggregateProxy(agg) proxy = Object.new aggregate, extract_ranges = method(:aggregate), method(:extract_ranges) proxy.define_singleton_method(:[]) do |*ranges| aggregate[extract_ranges[ranges], agg] end proxy end |
#avg ⇒ Object
177 178 179 |
# File 'lib/active_data_frame/table.rb', line 177 def avg @avg ||= AggregateProxy('AVG') end |
#build_case_map(all_bounds) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/active_data_frame/table.rb', line 14 def build_case_map(all_bounds) map = block_type::COLUMNS.map{|col| [col, []]}.to_h all_bounds.each do |bound| case bound.from.index when bound.to.index (bound.from.offset+1..bound.to.offset+1).each do |col_idx| map["t#{col_idx}"] << (bound.from.index..bound.from.index) end else (bound.from.offset+1..block_type::COLUMNS.size).each do |col_idx| map["t#{col_idx}"] << (bound.from.index..bound.from.index) end (1..block_type::COLUMNS.size).each do |col_idx| map["t#{col_idx}"] << (bound.from.index.succ..bound.to.index-1) end if bound.from.index.succ != bound.to.index (1..bound.to.offset+1).each do |col_idx| map["t#{col_idx}"] << (bound.to.index..bound.to.index) end end end map end |
#column_cases(cases, aggregation_function = nil) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/active_data_frame/table.rb', line 38 def column_cases(cases, aggregation_function=nil) block_type::COLUMNS.map do |col| col_cases = cases[col].sort_by(&:begin).reduce([]) do |agg, col_case| if agg.empty? agg << col_case else if agg[-1].end.succ == col_case.begin agg[-1] = (agg[-1].begin..col_case.end) else agg << col_case end agg end end if aggregation_function case col_cases.length when 0 then "0.0 as #{col}" else case_str = col_cases.map do |match| case when match.begin == match.end then "period_index = #{match.begin}" else "period_index BETWEEN #{match.begin} AND #{match.end}" end end.join(" OR ") "CASE WHEN #{case_str} THEN #{aggregation_function}(#{col}) ELSE NULL END" end else case col_cases.length when 0 then "NULL as #{col}" else case_str = col_cases.map do |match| case when match.begin == match.end then "period_index = #{match.begin}" else "period_index BETWEEN #{match.begin} AND #{match.end}" end end.join(" OR ") "CASE WHEN #{case_str} THEN #{col} ELSE NULL END" end end end.map(&Arel.method(:sql)) end |
#get(ranges) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/active_data_frame/table.rb', line 81 def get(ranges) ranges = extract_ranges(ranges) all_bounds = ranges.map.with_index do |range, index| get_bounds(range.first, range.exclude_end? ? range.end - 1 : range.end, index) end case_map = build_case_map(all_bounds) existing_blocks = Hash.new{|h, index| h[index] = {}} index_map = {} res = ActiveRecord::Base.transaction do ids = data_frame_type.pluck(:id) as_sql = blocks_between( all_bounds, block_scope: data_frame_type.unscoped.where( "#{data_frame_type.table_name}.id IN (SELECT id FROM (#{data_frame_type.select(:id).to_sql}) airport_ids)" ).joins("LEFT JOIN #{block_type.table_name} ON #{data_frame_type.table_name}.id = #{block_type.table_name}.data_frame_id") ).where( block_type.table_name => {data_frame_type: data_frame_type.name } ).select(:period_index, :data_frame_id, *column_cases(case_map)).to_sql index_map = ids.each_with_index.to_h ActiveRecord::Base.connection.execute(as_sql) end case ActiveRecord::Base.connection_db_config.adapter when 'postgresql'.freeze res.each_row {|pi, data_frame_id, *values| existing_blocks[pi][data_frame_id] = values } when 'mysql2'.freeze res.each {|pi, data_frame_id, *values| existing_blocks[pi][data_frame_id] = values } when 'sqlite3'.freeze res.map(&:values).each {|pi, data_frame_id, *values| existing_blocks[pi][data_frame_id] = values } end result = M.blank(typecode: block_type::TYPECODE, columns: all_bounds.map(&:length).sum, rows: index_map.size) iterate_bounds(all_bounds) do |index, left, right, cursor, size| if blocks = existing_blocks[index] blocks.each do |data_frame_id, block| row = index_map[data_frame_id] next unless row chunk = block[left..right] result.narray[cursor...cursor + size, row] = chunk end end end if column_map && !column_map.default_proc total = 0 range_sizes = ranges.map do |range, memo| last_total = total total += range.size [range.first, range.size, last_total] end index_of = ->(column){ selected = range_sizes.find{|start, size| start <= column && start + size >= column} if selected start, _, total = selected (column - start) + total else nil end } result.column_map = column_map.map do |name, column| [name, index_of[column_map[name]]] end.to_h end result.row_map = Hash.new do |h ,k| h[k] = begin case k when ActiveRecord::Base then index_map.fetch(k.id) when ActiveRecord::Relation then k.pluck(:id).map{|i| index_map.fetch(i) } when ->(list){ list.kind_of?(Array) && list.all?{|v| v.kind_of?(ActiveRecord::Base)} } then k.map{|i| index_map.fetch(i.id) } when Numeric then index_map.fetch(k) end end end result end |
#idx_where_sum_gte(*ranges, max) ⇒ Object
160 161 162 |
# File 'lib/active_data_frame/table.rb', line 160 def idx_where_sum_gte(*ranges, max) select_agg_indices(extract_ranges(ranges), 'SUM', ->(x, y){ x <= y } , 'SUM(%) >= :max', max: max) end |
#idx_where_sum_lte(*ranges, min) ⇒ Object
164 165 166 |
# File 'lib/active_data_frame/table.rb', line 164 def idx_where_sum_lte(*ranges, min) select_agg_indices(extract_ranges(ranges), 'SUM', ->(x, y){ x >= y } , 'SUM(%) <= :min', min: min) end |
#inspect ⇒ Object
10 11 12 |
# File 'lib/active_data_frame/table.rb', line 10 def inspect "#{data_frame_type.name} Table" end |
#max ⇒ Object
185 186 187 |
# File 'lib/active_data_frame/table.rb', line 185 def max @max ||= AggregateProxy('MAX') end |
#min ⇒ Object
189 190 191 |
# File 'lib/active_data_frame/table.rb', line 189 def min @min ||= AggregateProxy('MIN') end |
#set(from, values, trim: false) ⇒ Object
4 5 6 7 8 |
# File 'lib/active_data_frame/table.rb', line 4 def set(from, values, trim: false) ActiveDataFrame::Database.batch do Row.set_all(data_frame_type, self.block_type, self.data_frame_type, from, values, trim: trim) end end |
#sum ⇒ Object
181 182 183 |
# File 'lib/active_data_frame/table.rb', line 181 def sum @sum ||= AggregateProxy('SUM') end |