# This test tests by executing basic SQL
--disable_warnings
--disable_query_log
--disable_result_log
--source test_init.inc
--enable_result_log
--enable_query_log

--echo
--echo drop and create databases
--connection master_1
DROP DATABASE IF EXISTS auto_test_local;
CREATE DATABASE auto_test_local;
USE auto_test_local;
  --connection child2_1
  DROP DATABASE IF EXISTS auto_test_remote;
  CREATE DATABASE auto_test_remote;
  USE auto_test_remote;
  --connection child2_2
  DROP DATABASE IF EXISTS auto_test_remote2;
  CREATE DATABASE auto_test_remote2;
  USE auto_test_remote2;
--enable_warnings

--echo
--echo test select 1
--connection master_1
SELECT 1;
    --disable_query_log
    --disable_result_log
  --connection child2_1
  SELECT 1;
  --connection child2_2
  SELECT 1;
    --enable_query_log
    --enable_result_log

--connection master_1
--disable_warnings
DROP TABLE IF EXISTS tb_l;
--enable_warnings
--disable_query_log
echo CREATE TABLE tb_l (
  a INT,
  b CHAR(1),
  c DATETIME,
  PRIMARY KEY(a)
) MASTER_1_ENGINE2 MASTER_1_CHARSET2;
CREATE TABLE tb_l (
  a INT,
  b CHAR(1),
  c DATETIME,
  PRIMARY KEY(a)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
--enable_query_log
INSERT INTO tb_l (a, b, c) VALUES
  (1, 'f', '2008-07-01 10:21:39'),
  (2, 'g', '2000-02-01 00:00:00'),
  (3, 'j', '2007-05-04 20:03:11'),
  (4, 'i', '2003-10-30 05:01:03'),
  (5, 'h', '2001-10-31 23:59:59');

--echo
--echo create table with partition and select test
      --disable_query_log
      --disable_result_log
    --connection child2_2
    --disable_warnings
DROP TABLE IF EXISTS ta_r3;
    --enable_warnings
CREATE TABLE ta_r3 (
a INT DEFAULT 10,
b CHAR(1) DEFAULT 'c',
c DATETIME DEFAULT '1999-10-10 10:10:10',
PRIMARY KEY(a)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
      TRUNCATE TABLE mysql.general_log;
    --connection child2_1
    --disable_warnings
DROP TABLE IF EXISTS ta_r2;
    --enable_warnings
CREATE TABLE ta_r2 (
a INT DEFAULT 10,
b CHAR(1) DEFAULT 'c',
c DATETIME DEFAULT '1999-10-10 10:10:10',
PRIMARY KEY(a)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
      TRUNCATE TABLE mysql.general_log;
      --enable_query_log
      --enable_result_log
  --connection master_1
  --disable_query_log
  echo CREATE TABLE ta_l2 (
    PRIMARY KEY(a)
  ) MASTER_1_ENGINE MASTER_1_COMMENT_P_2_1
  SELECT a, b, c FROM tb_l;
CREATE TABLE ta_l2 (
      PRIMARY KEY(a)
) ENGINE=Spider COMMENT='table "ta_r3"'
  PARTITION BY KEY(a) (
PARTITION pt1 COMMENT='srv "s_2_1", table "ta_r2",
    priority "1000"',
PARTITION pt2 COMMENT='srv "s_2_2", priority "1000001"'
  )
    SELECT a, b, c FROM tb_l;
  --enable_query_log
      --disable_query_log
      --disable_result_log
    --connection child2_2
      SELECT argument FROM mysql.general_log WHERE command_type != 'Execute' AND argument LIKE '%insert %';
SELECT a, b, date_format(c, '%Y-%m-%d %H:%i:%s') FROM ta_r3 ORDER BY a;
    --connection child2_1
      SELECT argument FROM mysql.general_log WHERE command_type != 'Execute' AND argument LIKE '%insert %';
SELECT a, b, date_format(c, '%Y-%m-%d %H:%i:%s') FROM ta_r2 ORDER BY a;
      --enable_query_log
      --enable_result_log
  --connection master_1
  SELECT a, b, date_format(c, '%Y-%m-%d %H:%i:%s') FROM ta_l2 ORDER BY a;

--echo
--echo select partition using pushdown
      --disable_query_log
      --disable_result_log
    --connection child2_2
      TRUNCATE TABLE mysql.general_log;
    --connection child2_1
      TRUNCATE TABLE mysql.general_log;
      --enable_query_log
      --enable_result_log
  --connection master_1
  SELECT a.a, a.b, date_format(a.c, '%Y-%m-%d %H:%i:%s') FROM ta_l2 a WHERE
  a.b = 'g' ORDER BY a.a;
      --disable_query_log
      --disable_result_log
    --connection child2_2
      SELECT argument FROM mysql.general_log WHERE command_type != 'Execute' AND argument LIKE '%select %';
    --connection child2_1
      SELECT argument FROM mysql.general_log WHERE command_type != 'Execute' AND argument LIKE '%select %';
      --enable_query_log
      --enable_result_log

--echo
--echo select partition using index pushdown
      --disable_query_log
      --disable_result_log
    --connection child2_2
      TRUNCATE TABLE mysql.general_log;
    --connection child2_1
      TRUNCATE TABLE mysql.general_log;
      --enable_query_log
      --enable_result_log
  --connection master_1
  SELECT a.a, a.b, date_format(a.c, '%Y-%m-%d %H:%i:%s') FROM ta_l2 a WHERE
  a.a > 0 AND a.b = 'g' ORDER BY a.a;
      --disable_query_log
      --disable_result_log
    --connection child2_2
      SELECT argument FROM mysql.general_log WHERE command_type != 'Execute' AND argument LIKE '%select %';
    --connection child2_1
      SELECT argument FROM mysql.general_log WHERE command_type != 'Execute' AND argument LIKE '%select %';
      --enable_query_log
      --enable_result_log

--echo
--echo update partition pushdown
      --disable_query_log
      --disable_result_log
    --connection child2_2
      TRUNCATE TABLE mysql.general_log;
    --connection child2_1
      TRUNCATE TABLE mysql.general_log;
      --enable_query_log
      --enable_result_log
  --connection master_1
  UPDATE ta_l2 SET b = 'e', c = '2009-03-03 03:03:03' WHERE b = 'j';
      --disable_query_log
      --disable_result_log
    --connection child2_2
      SELECT argument FROM mysql.general_log WHERE command_type != 'Execute' AND argument LIKE '%select %';
      SELECT argument FROM mysql.general_log WHERE command_type != 'Execute' AND argument LIKE 'update %';
SELECT a, b, date_format(c, '%Y-%m-%d %H:%i:%s') FROM ta_r3 ORDER BY a;
    --connection child2_1
      SELECT argument FROM mysql.general_log WHERE command_type != 'Execute' AND argument LIKE '%select %';
      SELECT argument FROM mysql.general_log WHERE command_type != 'Execute' AND argument LIKE 'update %';
SELECT a, b, date_format(c, '%Y-%m-%d %H:%i:%s') FROM ta_r2 ORDER BY a;
      --enable_query_log
      --enable_result_log
  --connection master_1
  SELECT a, b, date_format(c, '%Y-%m-%d %H:%i:%s') FROM ta_l2 ORDER BY a;

--echo
--echo update partition index pushdown
      --disable_query_log
      --disable_result_log
    --connection child2_2
      TRUNCATE TABLE mysql.general_log;
    --connection child2_1
      TRUNCATE TABLE mysql.general_log;
      --enable_query_log
      --enable_result_log
  --connection master_1
  UPDATE ta_l2 SET b = 'j', c = '2009-03-03 03:03:03' WHERE a > 0 AND b = 'e';
      --disable_query_log
      --disable_result_log
    --connection child2_2
      SELECT argument FROM mysql.general_log WHERE command_type != 'Execute' AND argument LIKE '%select %';
      SELECT argument FROM mysql.general_log WHERE command_type != 'Execute' AND argument LIKE 'update %';
SELECT a, b, date_format(c, '%Y-%m-%d %H:%i:%s') FROM ta_r3 ORDER BY a;
    --connection child2_1
      SELECT argument FROM mysql.general_log WHERE command_type != 'Execute' AND argument LIKE '%select %';
      SELECT argument FROM mysql.general_log WHERE command_type != 'Execute' AND argument LIKE 'update %';
SELECT a, b, date_format(c, '%Y-%m-%d %H:%i:%s') FROM ta_r2 ORDER BY a;
      --enable_query_log
      --enable_result_log
  --connection master_1
  SELECT a, b, date_format(c, '%Y-%m-%d %H:%i:%s') FROM ta_l2 ORDER BY a;

--echo
--echo delete partition pushdown
  TRUNCATE TABLE ta_l2;
  INSERT INTO ta_l2 SELECT a, b, c FROM tb_l;
      --disable_query_log
      --disable_result_log
    --connection child2_2
      TRUNCATE TABLE mysql.general_log;
    --connection child2_1
      TRUNCATE TABLE mysql.general_log;
      --enable_query_log
      --enable_result_log
  --connection master_1
  DELETE FROM ta_l2 WHERE b = 'g';
      --disable_query_log
      --disable_result_log
    --connection child2_2
      SELECT argument FROM mysql.general_log WHERE command_type != 'Execute' AND argument LIKE '%select %';
      SELECT argument FROM mysql.general_log WHERE command_type != 'Execute' AND argument LIKE 'delete %';
SELECT a, b, date_format(c, '%Y-%m-%d %H:%i:%s') FROM ta_r3 ORDER BY a;
    --connection child2_1
      SELECT argument FROM mysql.general_log WHERE command_type != 'Execute' AND argument LIKE '%select %';
      SELECT argument FROM mysql.general_log WHERE command_type != 'Execute' AND argument LIKE 'delete %';
SELECT a, b, date_format(c, '%Y-%m-%d %H:%i:%s') FROM ta_r2 ORDER BY a;
      --enable_query_log
      --enable_result_log
  --connection master_1
  SELECT a, b, date_format(c, '%Y-%m-%d %H:%i:%s') FROM ta_l2 ORDER BY a;

--echo
--echo delete partition index pushdown
  TRUNCATE TABLE ta_l2;
  INSERT INTO ta_l2 SELECT a, b, c FROM tb_l;
      --disable_query_log
      --disable_result_log
    --connection child2_2
      TRUNCATE TABLE mysql.general_log;
    --connection child2_1
      TRUNCATE TABLE mysql.general_log;
      --enable_query_log
      --enable_result_log
  --connection master_1
  DELETE FROM ta_l2 WHERE a > 0 AND b = 'g';
      --disable_query_log
      --disable_result_log
    --connection child2_2
      SELECT argument FROM mysql.general_log WHERE command_type != 'Execute' AND argument LIKE '%select %';
      SELECT argument FROM mysql.general_log WHERE command_type != 'Execute' AND argument LIKE 'delete %';
SELECT a, b, date_format(c, '%Y-%m-%d %H:%i:%s') FROM ta_r3 ORDER BY a;
    --connection child2_1
      SELECT argument FROM mysql.general_log WHERE command_type != 'Execute' AND argument LIKE '%select %';
      SELECT argument FROM mysql.general_log WHERE command_type != 'Execute' AND argument LIKE 'delete %';
SELECT a, b, date_format(c, '%Y-%m-%d %H:%i:%s') FROM ta_r2 ORDER BY a;
      --enable_query_log
      --enable_result_log
  --connection master_1
  SELECT a, b, date_format(c, '%Y-%m-%d %H:%i:%s') FROM ta_l2 ORDER BY a;

--echo
--echo deinit
--disable_warnings
--connection master_1
DROP DATABASE IF EXISTS auto_test_local;
  --connection child2_1
  DROP DATABASE IF EXISTS auto_test_remote;
  --connection child2_2
  DROP DATABASE IF EXISTS auto_test_remote2;
--disable_query_log
--disable_result_log
--source test_deinit.inc
--enable_result_log
--enable_query_log
--enable_warnings
--echo
--echo end of test
