#!/bin/sh

# Set these according to you local environment.
# src_dir shoul point to a tree with following patch applied:
# - virtual const Item *cond_push(const Item *cond) { return cond; };
# + virtual const Item *cond_push(const Item *cond) { return NULL; };

src_dir=/net/atum17/export/home2/tmp/jw159207/mysql/repo/mysql-trunk
build_dir=/export/home2/tmp/jw159207/mysql/bug58553/build
install_dir=/export/home2/tmp/jw159207/mysql/bug58553/install
data_dir=/export/home2/tmp/jw159207/mysql/bug58553/data

# Should not need to change anything below.

mkdir -p ${build_dir} 
mkdir -p ${install_dir} 
mkdir -p ${data_dir} 

cd ${build_dir}
echo "Running cmake."
cmake ${src_dir} -DCMAKE_BUILD_TYPE=Debug
echo "Compiling."
make
make install DESTDIR=${install_dir}

cat > ${data_dir}/my.cnf <<EOF
[mysqld]                       
datadir=${data_dir}
socket=${data_dir}/mysql.sock
optimizer_search_depth=6
core-file

[client]
socket=${data_dir}/mysql.sock
EOF

echo "Installing database."
${install_dir}/usr/local/mysql/scripts/mysql_install_db --user=${USER} --basedir=${install_dir}/usr/local/mysql --datadir=${data_dir}

echo "Starting database."
${install_dir}/usr/local/mysql/bin/mysqld --defaults-file=${data_dir}/my.cnf  --basedir=${install_dir}/usr/local/mysql --datadir=${data_dir} --socket=${data_dir}/mysql.sock --log-error=${data_dir}/mysqld.err &
sleep 5

echo "Running test."
${install_dir}/usr/local/mysql/bin/mysql --socket ${data_dir}/mysql.sock -u root <<EOF
drop database if exists spj_ndb;
create database spj_ndb;
use spj_ndb;

create table t1 (
  a int not null,
  b int not null,
  c int not null,
  d int not null,
  primary key (a, b)
) engine = ndb;

explain extended select * from t1 join t1 as t2 on t1.c=1 where t2.c=1;

explain extended
select straight_join *
from t1
 join t1 as t2 on t2.a = t1.a and t2.b = t1.b
 join t1 as t3 on t3.a = t1.c and t3.b = t1.d
 join t1 as t4 on t4.a = t3.b and t4.b = t2.c;

explain extended
select t2.c, count(distinct t2.a)
from t1
join t1 as t2 on t1.a = t2.c and t1.b = t2.d
where t2.a = 4
group by t2.c;

explain extended select * from t1 join t1 as t2 on t1.c=1 where t2.c=1;

explain extended
select t2.c, count(distinct t2.a)
from t1
join t1 as t2 on t1.a = t2.c and t1.b = t2.d
where t2.a = 4
group by t2.c;

EOF