#
# MDEV-32369: Memory leak when executing PS for query with IN subquery
#
CREATE TABLE t1 (a VARCHAR(10)) ENGINE=MYISAM;
CREATE TABLE t2 (b VARCHAR(10) CHARACTER SET utf8) ENGINE=MYISAM;
INSERT INTO t1 VALUES ('b'), ('a'), ('c');
INSERT INTO t2 VALUES ('c'), ('d'), ('b');
PREPARE stmt FROM "SELECT t1.a FROM t1 WHERE t1.a IN (SELECT t2.b FROM t2)";
EXECUTE stmt;
a
c
b
EXECUTE stmt;
a
c
b
DEALLOCATE PREPARE stmt;
DELETE FROM t1;
DELETE FROM t2;
INSERT INTO t1 VALUES ('b');
INSERT INTO t2 VALUES ('b');
PREPARE stmt FROM "SELECT t1.a FROM t1 WHERE t1.a IN (SELECT t2.b FROM t2)";
EXECUTE stmt;
a
b
EXECUTE stmt;
a
b
DEALLOCATE PREPARE stmt;
DROP TABLE t1, t2;
#
# MDEV-32569: Failure when executing PS for query using IN subquery
#
CREATE TABLE t1 (a varchar(10)) ENGINE=MYISAM;
CREATE TABLE t2 (b varchar(10) CHARACTER SET utf8) ENGINE=MYISAM;
INSERT INTO t1 VALUES ('b');
INSERT INTO t2 VALUES ('b');
PREPARE stmt FROM
"SELECT STRAIGHT_JOIN  t1.a FROM t1 WHERE t1.a IN (SELECT t2.b FROM t2)";
EXECUTE stmt;
a
b
EXECUTE stmt;
a
b
DEALLOCATE PREPARE stmt;
DROP TABLE t1,t2;
#
# MDEV-32733: Two JSON related tests running in PS mode fail on server
#              built with -DWITH_PROTECT_STATEMENT_MEMROOT=YES
#
PREPARE stmt FROM "select json_contains_path('{\"key1\":1}', 'oNE', '$.key2[1]') as exp";
EXECUTE stmt;
exp
0
EXECUTE stmt;
exp
0
DEALLOCATE PREPARE stmt;
#
# MDEV-32466: Potential memory leak on execuing of create view statement
#
CREATE FUNCTION f1 () RETURNS VARCHAR(1)
BEGIN
DECLARE rec1 ROW TYPE OF v1;
SELECT z INTO rec1 FROM v1;
RETURN 1;
END|
CREATE FUNCTION f2 () RETURNS VARCHAR(1) RETURN '!';
CREATE VIEW v1 AS SELECT f2() z;
PREPARE stmt FROM "SELECT f1()";
EXECUTE stmt;
f1()
1
EXECUTE stmt;
f1()
1
DEALLOCATE PREPARE stmt;
DROP FUNCTION f1;
DROP VIEW v1;
DROP FUNCTION f2;
#
# MDEV-32867: ASAN errors in Item_func_json_contains_path::val_int upon PS execution
#
CREATE TABLE t1 (f BLOB) ENGINE=MyISAM;
PREPARE stmt FROM "SELECT * FROM t1 WHERE JSON_EXISTS(JSON_ARRAY('[true,1234567890]'), '$**.*') != JSON_CONTAINS_PATH(JSON_INSERT('{}', '$[1]', NULL), 'all', '$[1]')";
EXECUTE stmt;
f
DEALLOCATE PREPARE stmt;
DROP TABLE t1;
# End of 10.4 tests
#
# MDEV-34447: Memory leakage is detected on running the test main.ps against the server 11.1
#
CREATE TABLE t1 (id INT, value INT);
CREATE TABLE t2 (id INT);
PREPARE stmt FROM 'UPDATE t1 SET value = (SELECT 1 FROM t2 WHERE id = t1.id)';
EXECUTE stmt;
INSERT INTO t1 VALUES (1,10),(2,10),(3,10);
INSERT INTO t2 VALUES (1),(2);
EXECUTE stmt;
SELECT * FROM t1;
id	value
1	1
2	1
3	NULL
DEALLOCATE PREPARE stmt;
DROP TABLE t1, t2;
# Memory leak also could take place on running the DELETE statement
# with the LIMIT clause. Check it.
CREATE TABLE t1 (c1 INT);
INSERT INTO t1 (c1) VALUES (1), (2), (3);
CREATE PROCEDURE p1(p1 INT)
DELETE FROM t1 LIMIT p1;
CALL p1(0);
CALL p1(1);
CALL p1(2);
# Clean up
DROP TABLE t1;
DROP PROCEDURE p1;
#
# MDEV-34757: assertion of (mem_root->flags & 4) == 0 fails in 2nd ps execution with partition pruning
#
CREATE TABLE t1 (id INT, value INT);
CREATE TABLE t2 (id INT);
PREPARE stmt FROM 'EXPLAIN UPDATE t1 SET value = (SELECT 1 FROM t2 WHERE id = t1.id)';
EXECUTE stmt;
INSERT INTO t1 VALUES (1,10),(2,10),(3,10);
INSERT INTO t2 VALUES (1),(2);
EXECUTE stmt;
SELECT * FROM t1;
id	value
1	10
2	10
3	10
DEALLOCATE PREPARE stmt;
DROP TABLE t1, t2;
set @var1=5;
set @var2=4;
create table t1 (a int) partition by list(a) (
partition p0 values in (null,1,2),
partition p1 values in (3,4)
);
create table t2 (a int);
insert into t1 values (1),(2),(3),(4);
insert into t2 values (4);
PREPARE stmt FROM 'UPDATE t1 t1 SET a = (SELECT 1 FROM t2 WHERE a = t1.a) where a = ?';
execute stmt using @var1;
select * from t1;
a
1
2
3
4
execute stmt using @var2;
select * from t1;
a
1
2
1
3
deallocate prepare stmt;
drop table t1, t2;
set @var1=5;
set @var2=4;
create table t1 (a int) partition by list(a) (
partition p0 values in (null,1,2),
partition p1 values in (3,4)
);
create table t2 (a int);
insert into t1 values (1),(2),(3),(4);
insert into t2 values (4);
PREPARE stmt FROM 'EXPLAIN UPDATE t1 t1 SET a = (SELECT 1 FROM t2 WHERE a = t1.a) where a = ?';
execute stmt using @var1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No matching rows after partition pruning
select * from t1;
a
1
2
3
4
execute stmt using @var2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where; Using buffer
2	DEPENDENT SUBQUERY	t2	system	NULL	NULL	NULL	NULL	1	
select * from t1;
a
1
2
3
4
deallocate prepare stmt;
drop table t1, t2;
set @var1=1;
set @var2=2;
CREATE TABLE t1 ( id INT(10), value INT(10) );
CREATE TABLE t2 ( id INT(10) );
insert into t1 values (1,10),(2,10),(3,10);
insert into t2 values (1),(2);
PREPARE stmt FROM 'UPDATE t1 t1 SET value = (SELECT 1 FROM t2 WHERE id = t1.id) WHERE  ?=?';
execute stmt using @var1, @var2;
execute stmt using @var1, @var1;
deallocate prepare stmt;
DROP TABLE t1,t2;
set @var1=1;
set @var2=2;
CREATE TABLE t1 ( id INT(10), value INT(10) );
CREATE TABLE t2 ( id INT(10) );
insert into t1 values (1,10),(2,10),(3,10);
insert into t2 values (1),(2);
PREPARE stmt FROM 'EXPLAIN UPDATE t1 t1 SET value = (SELECT 1 FROM t2 WHERE id = t1.id) WHERE  ?=?';
execute stmt using @var1, @var2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
execute stmt using @var1, @var1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	3	
2	DEPENDENT SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where
deallocate prepare stmt;
DROP TABLE t1,t2;
CREATE TABLE t1 (c1 INT);
INSERT INTO t1 (c1) VALUES (1), (2), (3);
CREATE PROCEDURE p1(p1 INT)
EXPLAIN DELETE FROM t1 LIMIT p1;
CALL p1(0);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
CALL p1(1);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	3	
CALL p1(2);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	3	
# Clean up
DROP TABLE t1;
DROP PROCEDURE p1;
set @var1=5;
set @var2=4;
create table t1 (a int) partition by list(a) (
partition p0 values in (1,2),
partition p1 values in (3,4)
);
create table t2 (a int);
insert into t1 values (1),(2),(3),(4);
insert into t2 values (4);
PREPARE stmt FROM 'DELETE FROM t1 where a = ?';
execute stmt using @var1;
select * from t1;
a
1
2
3
4
execute stmt using @var2;
select * from t1;
a
1
2
3
deallocate prepare stmt;
drop table t1, t2;
set @var1=5;
set @var2=4;
create table t1 (a int) partition by list(a) (
partition p0 values in (1,2),
partition p1 values in (3,4)
);
create table t2 (a int);
insert into t1 values (1),(2),(3),(4);
insert into t2 values (4);
PREPARE stmt FROM 'EXPLAIN DELETE FROM t1 where a = ?';
execute stmt using @var1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No matching rows after partition pruning
select * from t1;
a
1
2
3
4
execute stmt using @var2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	2	Using where
select * from t1;
a
1
2
3
4
deallocate prepare stmt;
drop table t1, t2;
set @var1=1;
set @var2=2;
CREATE TABLE t1 ( id INT(10), value INT(10) );
CREATE TABLE t2 ( id INT(10) );
insert into t1 values (1,10),(2,10),(3,10);
insert into t2 values (1),(2);
PREPARE stmt FROM 'DELETE FROM t1 WHERE  ?=?';
execute stmt using @var1, @var2;
execute stmt using @var1, @var1;
deallocate prepare stmt;
DROP TABLE t1,t2;
set @var1=1;
set @var2=2;
CREATE TABLE t1 ( id INT(10), value INT(10) );
CREATE TABLE t2 ( id INT(10) );
insert into t1 values (1,10),(2,10),(3,10);
insert into t2 values (1),(2);
PREPARE stmt FROM 'EXPLAIN DELETE FROM t1 WHERE  ?=?';
execute stmt using @var1, @var2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
execute stmt using @var1, @var1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	3	Deleting all rows
deallocate prepare stmt;
DROP TABLE t1,t2;
#
# MDEV-33858: Assertion `(mem_root->flags & 4) == 0' fails on 2nd execution of PS with -DWITH_PROTECT_STATEMENT_MEMROOT=ON
#
CREATE TABLE t (a INT);
INSERT INTO t VALUES (1),(2);
PREPARE stmt FROM "UPDATE t SET a = 0 LIMIT ?";
EXECUTE stmt USING 0;
EXECUTE stmt USING 1;
DROP TABLE t;
#
# MDEV-35249: Assertion `(mem_root->flags & 4) == 0' failed in convert_subq_to_jtbm
#
CREATE TABLE t (a INT, b INT, c INT);
INSERT INTO t VALUES (1,2,3),(4,5,6);
CREATE ALGORITHM=TEMPTABLE VIEW v1 AS SELECT * FROM t;
CREATE VIEW v2 AS SELECT * FROM v1;
PREPARE stmt FROM 'UPDATE t SET a = 0 WHERE b IN (SELECT c FROM v2)';
EXECUTE stmt;
EXECUTE stmt;
DROP VIEW v2;
DROP VIEW v1;
DROP TABLE t;
# End of 10.5 tests
#
# MDEV-33769: Memory leak found in the test main.rownum run with --ps-protocol against a server built with the option -DWITH_PROTECT_STATEMENT_MEMROOT
#
CREATE OR REPLACE TABLE t1(a INT);
PREPARE stmt FROM 'SELECT 1 FROM t1 WHERE ROWNUM() < 2';
EXECUTE stmt;
1
EXECUTE stmt;
1
INSERT INTO t1 VALUES (1), (2), (3), (4), (5);
PREPARE stmt FROM 'SELECT * FROM t1 WHERE ROWNUM() < ?';
# Expected output is two rows (1), (2)
EXECUTE stmt USING 3;
a
1
2
# Expected output is one row (1)
EXECUTE stmt USING 2;
a
1
# Expected output is three rows (1), (2), (3)
EXECUTE stmt USING 4;
a
1
2
# Clean up
DEALLOCATE PREPARE stmt;
DROP TABLE t1;
#
# MDEV-39265: Assertion `(mem_root->flags & 4) == 0` failed upon 2nd execution `USING DEFAULT` with sequence
#
CREATE SEQUENCE s;
CREATE TABLE t (a INT DEFAULT(NEXTVAL(s)));
INSERT INTO t VALUES (1), (2);
PREPARE stmt FROM "UPDATE t SET a = ?";
# Verify no SIGABRT
EXECUTE stmt USING 3;
EXECUTE stmt USING DEFAULT;
# Verify the there are no repeated allocations on statement mem_root
SET @saved_dbug = @@SESSION.debug_dbug;
SET SESSION debug_dbug="+d,assert_no_alloc_internal_tables";
EXECUTE stmt USING DEFAULT;
EXECUTE stmt USING 4;
EXECUTE stmt USING DEFAULT;
EXECUTE stmt USING DEFAULT;
SET SESSION debug_dbug= @saved_dbug;
DEALLOCATE PREPARE stmt;
DROP SEQUENCE s;
DROP TABLE t;
# End of 10.6 tests
#
# MDEV-34447: Memory leakage is detected on running the test main.ps against the server 11.1
#
CREATE TABLE t1 (id INT, value INT);
CREATE TABLE t2 (id INT);
PREPARE stmt FROM 'UPDATE t1 SET value = (SELECT 1 FROM t2 WHERE id = t1.id)';
EXECUTE stmt;
INSERT INTO t1 VALUES (1,10),(2,10),(3,10);
INSERT INTO t2 VALUES (1),(2);
EXECUTE stmt;
SELECT * FROM t1;
id	value
1	1
2	1
3	NULL
DEALLOCATE PREPARE stmt;
DROP TABLE t1, t2;
# Memory leak also could take place on running the DELETE statement
# with the LIMIT clause. Check it.
CREATE TABLE t1 (c1 INT);
INSERT INTO t1 (c1) VALUES (1), (2), (3);
CREATE PROCEDURE p1(p1 INT)
DELETE FROM t1 LIMIT p1;
CALL p1(0);
CALL p1(1);
CALL p1(2);
# Clean up
DROP TABLE t1;
DROP PROCEDURE p1;
# End of 10.11 tests
#
# MDEV-34649: Memory leaks on running DELETE statement in PS mode with positional parameters
#
CREATE TABLE t1 (a INT, b VARCHAR(30)) CHARSET=utf8mb4;
INSERT INTO t1 VALUES (1, 'one'), (0, NULL), (3, 'three'), (4, 'four');
PREPARE stmt FROM 'DELETE FROM t1 WHERE b=?' ;
SET @arg00=NULL;
EXECUTE stmt USING @arg00;
SET @arg00='one';
EXECUTE stmt USING @arg00;
# Clean up
DEALLOCATE PREPARE stmt;
DROP TABLE t1;
# End of 11.1
#
# MDEV-34517: Memory leak on re-compilation of a failing statement inside a stored routine
#
CREATE TABLE t1 (a INT);
CREATE PROCEDURE p1()
SELECT * FROM t1;
SET @save_dbug = @@debug_dbug;
CALL p1();
a
DROP TABLE t1;
CREATE TABLE t1 (a INT);
SET @@debug_dbug='+d,check_sp_cache_not_invalidated,sp_instr_reparsing_1st_time';
# Recomplilation of the statement 'SELECT * FORM t1' on
# the second run of the procedure p1 shouldn't result in memory leaks
CALL p1();
a
SET @@debug_dbug='-d,sp_instr_reparsing_1st_time';
DROP TABLE t1;
CREATE TABLE t1 (a INT);
# Recompilation is requested the second time
SET @@debug_dbug='+d,sp_instr_reparsing_2nd_time';
CALL p1();
a
SET @@debug_dbug='-d,sp_instr_reparsing_2nd_time';
# Clean up
SET @@debug_dbug=@save_dbug;
DROP TABLE t1;
DROP PROCEDURE p1;
# End of 11.2 tests
