master
Oliver Kennedy 2016-10-07 23:10:21 -04:00
parent 404aca80e7
commit b5995c89be
1 changed files with 59 additions and 0 deletions

View File

@ -0,0 +1,59 @@
$files = {
cust: [ :c_acctbal, :c_address, :c_comment, :c_custkey, :c_mktsegment, :c_name, :c_nationkey, :c_phone ],
lineitem: [ :l_comment, :l_commitdate, :l_discount, :l_extendedprice, :l_linenumber, :l_linestatus, :l_orderkey, :l_partkey, :l_quantity, :l_receiptdate, :l_returnflag, :l_shipdate, :l_shipinstruct, :l_shipmode, :l_suppkey, :l_tax ],
nation: [ :n_nationkey, :n_name, :n_regionkey, :n_comment ],
orders: [ :o_clerk, :o_comment, :o_custkey, :o_orderdate, :o_orderkey, :o_orderpriority, :o_orderstatus, :o_shippriority, :o_totalprice ],
part: [ :p_brand, :p_comment, :p_container, :p_mfgr, :p_name, :p_partkey, :p_retailprice, :p_size, :p_type ],
psupp: [ :ps_availqty, :ps_comment, :ps_partkey, :ps_suppkey, :ps_supplycost ],
region: [ :r_comment, :r_name, :r_regionkey ],
supp: [ :s_acctbal, :s_address, :s_comment, :s_name, :s_nationkey, :s_phone, :s_suppkey ]
}
def init_data
# TUPLE_ID -> CONFLICT_GROUP -> WORLD -> FIELD -> VALUE
Hash.new {|h,k| h[k] = Hash.new {|h,k| h[k] = Hash.new {|h,k| h[k] = Hash.new } } }
end
def load(data, table, field)
puts "Loading #{table}_#{field}.tbl"
File.open("input/#{table}_#{field}.tbl") do |f|
f.readlines.each do |l|
conflict_group, world, tuple_id, value = l.chomp.split(/\|/)
data[tuple_id.to_i][conflict_group.to_i][world.to_i][field] = value
end
end
end
def render(data)
puts "Rendering data..."
data.map do |tuple_id, conflict_groups|
conflict_groups = conflict_groups.values.to_a
world_ids = conflict_groups.map { |worlds| worlds.keys }
# compute the cartesian product of the resulting key sets
world_groups = [ [nil] ]
world_ids.each { |id_set| world_groups = world_groups.product(id_set) }
world_groups = world_groups.map { |x| x.flatten[1..-1] }
# render the corresponding tuple
world_groups.map do |per_group_world_ids|
conflict_groups.zip(per_group_world_ids).map { |worlds, world_id| worlds[world_id].to_a }.flatten(1)
end.map { |tuple| [tuple_id, tuple.to_h] }
end.flatten(1)
end
$files.each do |table, fields|
file "output/#{table}.tbl" do
data = init_data
fields.each { |field| load(data, table, field) }
File.open("output/#{table}.tbl", "w+") do |f|
f.puts "tid|#{fields.join("|")}"
render(data).each do |tid, tuple|
f.puts "#{tid}|#{fields.map {|f| tuple[f]}.join("|")}"
end
end
end
task table => "output/#{table}.tbl"
end