# This test checks various optimizer-related functions using
# the debug-sync facility.

--source include/have_debug_sync.inc
--source include/have_innodb.inc
--source include/have_materialization.inc
--source include/not_embedded.inc

--echo
--echo BUG#11763382 Assertion 'inited==INDEX' on SELECT MAX(...)
--echo

CREATE TABLE t(i INT NOT NULL PRIMARY KEY, f INT) ENGINE = InnoDB;
INSERT INTO t VALUES (1,1),(2,2);

BEGIN;
UPDATE t SET f=100 WHERE i=2;

--connect (con1,localhost,root,,)

set optimizer_switch='semijoin=off,subquery_materialization_cost_based=off';

SET DEBUG_SYNC='before_index_end_in_subselect WAIT_FOR callit';
--send SELECT f FROM t WHERE i IN ( SELECT i FROM t )

--connection default
let $show_statement= SHOW PROCESSLIST;
let $field= State;
let $condition= = 'debug sync point: before_index_end_in_subselect';
--source include/wait_show_condition.inc

--connect (con2,localhost,root,,)
--send SELECT MAX(i) FROM t FOR UPDATE

--connect (con3,localhost,root,,)
--send SELECT MAX(i) FROM t FOR UPDATE

--connection default

let $wait_condition=
  SELECT COUNT(*) = 2 FROM information_schema.processlist 
  WHERE state = 'Optimizing' and info = 'SELECT MAX(i) FROM t FOR UPDATE';
--source include/wait_condition.inc

SET DEBUG_SYNC='now SIGNAL callit';
COMMIT;

--connection con1
--reap
SET DEBUG_SYNC='RESET';

--connection con2
--reap

--connection con3
--reap

--connection default
DROP TABLE t;
--disconnect con1
--disconnect con2
--disconnect con3


--echo # End of BUG#56080
--echo #
--echo # Bug #13536661: VALGRIND: DEFINITELY LOST: 552 BYTES IN 1 BLOCKS IN
--echo # CREATE_TMP_TABLE AND HIGHER
--echo #
CREATE TABLE t1 ( a INT, b INT );
INSERT INTO t1 VALUES (4, 40), (1, 10), (2, 20), (2, 20), (3, 30);

connect(con1,localhost,root,,);
SET debug_sync = "tmp_table_created SIGNAL parked WAIT_FOR go";
--echo # This should not leak memory.
send SELECT b, COUNT(DISTINCT b) FROM t1 GROUP BY b ORDER BY -b;

connection default;
SET debug_sync = "now WAIT_FOR parked";
--echo # Set locally to shadow the global variable.
SET debug = '';
SET GLOBAL debug = '+d,simulate_out_of_memory';

SET debug_sync = "now SIGNAL go";

connection con1;
--error ER_OUT_OF_RESOURCES
reap;
SET GLOBAL debug = '';

DROP TABLE t1;
--disconnect con1
--connection default

