#!/usr/bin/env bash

ROWS=$1
RUNS=1

killall -9 postgres
sleep 1

rm -Rf data

PATH_OLD=$PATH
TS=$(date +%Y%m%d%H%M%S)


export PATH=/home/tomas/builds/master/bin:$PATH_OLD

pg_ctl -D data init
echo 'shared_buffers = 8GB' >> data/postgresql.conf

pg_ctl -D data -l pg.log start
createdb test
psql test <<EOF
create table f (id1 text, id2 text, id3 text, r float, padding text);
create table d1 (id text, r float, padding text);
create table d2 (id text, r float, padding text);
create table d3 (id text, r float, padding text);

insert into f select md5((mod(i, $ROWS) + 1)::text), md5((mod(i, $ROWS) + 1)::text), md5((mod(i, $ROWS) + 1)::text), random(), md5(random()::text) from generate_series(1, $ROWS * 10) s(i);
insert into d1 select md5(i::text), random(), md5(random()::text) from generate_series(1, $ROWS) s(i);
insert into d2 select md5(i::text), random(), md5(random()::text) from generate_series(1, $ROWS) s(i);
insert into d3 select md5(i::text), random(), md5(random()::text) from generate_series(1, $ROWS) s(i);

vacuum analyze;
checkpoint;

\d f
\d d1
\d d2
\d d3

\d+
EOF

pg_ctl -D data -l pg.log stop


function run_tests {
	build=$1
	bloom=$2
	r=$3
	frac=$4
	wm=$5

	pg_ctl -D data -l pg.log start

	psql test > tmp.log 2>&1 <<EOF
set max_parallel_workers_per_gather = 0;
set enable_mergejoin = off;
set work_mem = $wm;
set enable_hashjoin_bloom = $bloom;
set join_collapse_limit = 2;

explain (analyze, timing off) select * from f join d1 on (f.id1 = d1.id) where d1.r < $frac / 100.0;
EOF

	t=$(grep 'Execution Time' tmp.log | awk '{print $3}')
	echo text $ROWS $frac $build $bloom $r $wm 1 $t >> results-$TS.csv

	echo "===== $build $bloom $r $wm $frac =====" >> explains-$TS.log
	cat tmp.log >> explains-$TS.log

	pg_ctl -D data -l pg.log restart

	psql test > tmp.log 2>&1 <<EOF
set max_parallel_workers_per_gather = 0;
set enable_mergejoin = off;
set work_mem = $wm;
set enable_hashjoin_bloom = $bloom;
set join_collapse_limit = 2;

explain (analyze, timing off) select * from f join d1 on (f.id1 = d1.id) join d2 on (f.id2 = d2.id) where d1.r < $frac / 100.0 and d2.r < $frac / 100.0;
EOF

	t=$(grep 'Execution Time' tmp.log | awk '{print $3}')
	echo text $ROWS $frac $build $bloom $r $wm 2 $t >> results-$TS.csv

	echo "===== $build $bloom $r $wm $frac =====" >> explains-$TS.log
	cat tmp.log >> explains-$TS.log

	pg_ctl -D data -l pg.log restart

	psql test > tmp.log 2>&1 <<EOF
set max_parallel_workers_per_gather = 0;
set enable_mergejoin = off;
set work_mem = $wm;
set enable_hashjoin_bloom = $bloom;
set join_collapse_limit = 2;

explain (analyze, timing off) select * from f join d1 on (f.id1 = d1.id) join d2 on (f.id2 = d2.id) join d3 on (f.id3 = d3.id) where d1.r < $frac / 100.0 and d2.r < $frac / 100.0 and d3.r < $frac / 100.0;
EOF

	t=$(grep 'Execution Time' tmp.log | awk '{print $3}')
	echo text $ROWS $frac $build $bloom $r $wm 3 $t >> results-$TS.csv

	echo "===== $build $bloom $r $wm $frac =====" >> explains-$TS.log
	cat tmp.log >> explains-$TS.log

	pg_ctl -D data -l pg.log stop

}


for r in $(seq 1 $RUNS); do

	for frac in 1 100 50 10 75 25 90; do

		for wm in 1024 4096 16384 65536 262144 1048576; do

			export PATH=/home/tomas/builds/master/bin:$PATH_OLD

			run_tests master off $r $frac $wm

			export PATH=/home/tomas/builds/bloom/bin:$PATH_OLD

			run_tests patched off $r $frac $wm
			run_tests patched on $r $frac $wm

		done

	done

done
