#!/bin/bash

run=0

while /bin/true; do

        run=$((run+1))

        date

        echo RUN $run

        NULL_PCT=$((RANDOM % 100))
        SUM_PCT=$((RANDOM % 100))
        ROWS=10000

        echo "-- $ROWS $NULL_PCT $SUM_PCT" > init-$run.sql

        echo 'drop table if exists t;' >> init-$run.sql
        echo 'create table t (a int) with (fillfactor = 10);' >> init-$run.sql
        echo 'create index t_a_idx on t using brin (a) with (pages_per_range=1);' >> init-$run.sql

        for v in `seq 1 $ROWS`; do
                echo "insert into t values (case when random() * 100 < $NULL_PCT then null else 1000 * random() end);" >> init-$run.sql

                r=$((RANDOM % 100))

                if [ $r -lt $SUM_PCT ]; then
                        echo "select brin_summarize_new_values('t_a_idx');" >> init-$run.sql
                fi
        done

        psql test < init-$run.sql > init.log 2>&1

        psql test -t -A -c 'select distinct a from t order by a' > values.txt

        while read line; do

                if [ "$line" == "" ]; then

                        psql -t -A test > seqscan.txt <<EOF
set enable_bitmapscan = off;
set max_parallel_workers_per_gather = 0;
explain select count(*) from t where a is null;
select count(*) from t where a is null;
EOF

                        psql -t -A test > index.txt <<EOF
set enable_seqscan = off;
set max_parallel_workers_per_gather = 0;
explain select count(*) from t where a is null;
select count(*) from t where a is null;
EOF

                        a=`tail -n 1 seqscan.txt`
                        b=`tail -n 1 index.txt`

                        # cat seqscan.txt index.txt

                        if [ "$a" != "$b" ]; then
                                echo "'$line'" $a $b
                                echo "FAIL $run"
                        fi

                        psql -t -A test > seqscan.txt <<EOF
set enable_bitmapscan = off;
set max_parallel_workers_per_gather = 0;
explain select count(*) from t where a is not null;
select count(*) from t where a is not null;
EOF

                        psql -t -A test > index.txt <<EOF
set enable_seqscan = off;
set max_parallel_workers_per_gather = 0;
explain select count(*) from t where a is not null;
select count(*) from t where a is not null;
EOF

                        a=`tail -n 1 seqscan.txt`
                        b=`tail -n 1 index.txt`

                        # cat seqscan.txt index.txt

                        if [ "$a" != "$b" ]; then
                                echo "'$line'" $a $b
                                echo "FAIL $run"
                        fi

                else

                        psql -t -A test > seqscan.txt <<EOF
set enable_bitmapscan = off;
set max_parallel_workers_per_gather = 0;
explain select count(*) from t where a = $line;
select count(*) from t where a = $line;
EOF

                        psql -t -A test > index.txt <<EOF
set enable_seqscan = off;
set max_parallel_workers_per_gather = 0;
explain select count(*) from t where a = $line;
select count(*) from t where a = $line;
EOF

                fi

                a=`tail -n 1 seqscan.txt`
                b=`tail -n 1 index.txt`

                # cat seqscan.txt index.txt

                if [ "$a" != "$b" ]; then
                        echo "'$line'" $a $b
                        echo "FAIL $run"
                fi

        done < values.txt

        psql test -c 'drop index t_a_idx';
        psql test -c 'create index t_a_idx on t using brin (a) with (pages_per_range=1)'

        while read line; do

                if [ "$line" == "" ]; then

                        psql -t -A test > seqscan.txt <<EOF
set enable_bitmapscan = off;
set max_parallel_workers_per_gather = 0;
explain select count(*) from t where a is null;
select count(*) from t where a is null;
EOF

                        psql -t -A test > index.txt <<EOF
set enable_seqscan = off;
set max_parallel_workers_per_gather = 0;
explain select count(*) from t where a is null;
select count(*) from t where a is null;
EOF

                        a=`tail -n 1 seqscan.txt`
                        b=`tail -n 1 index.txt`

                        # cat seqscan.txt index.txt

                        if [ "$a" != "$b" ]; then
                                echo "'$line'" $a $b
                                echo "FAIL $run"
                        fi

                        psql -t -A test > seqscan.txt <<EOF
set enable_bitmapscan = off;
set max_parallel_workers_per_gather = 0;
explain select count(*) from t where a is not null;
select count(*) from t where a is not null;
EOF

                        psql -t -A test > index.txt <<EOF
set enable_seqscan = off;
set max_parallel_workers_per_gather = 0;
explain select count(*) from t where a is not null;
select count(*) from t where a is not null;
EOF

                        a=`tail -n 1 seqscan.txt`
                        b=`tail -n 1 index.txt`

                        # cat seqscan.txt index.txt

                        if [ "$a" != "$b" ]; then
                                echo "'$line'" $a $b
                                echo "FAIL $run"
                        fi

                else

                        psql -t -A test > seqscan.txt <<EOF
set enable_bitmapscan = off;
set max_parallel_workers_per_gather = 0;
explain select count(*) from t where a = $line;
select count(*) from t where a = $line;
EOF

                        psql -t -A test > index.txt <<EOF
set enable_seqscan = off;
set max_parallel_workers_per_gather = 0;
explain select count(*) from t where a = $line;
select count(*) from t where a = $line;
EOF

                fi

                a=`tail -n 1 seqscan.txt`
                b=`tail -n 1 index.txt`

                # cat seqscan.txt index.txt

                if [ "$a" != "$b" ]; then
                        echo "'$line'" $a $b
                        echo "FAIL $run"
                fi

        done < values.txt

done
