Skip to content

Commit 423b7da

Browse files
committed
Fixed bug mdev-11820.
The fields st_select_lex::cond_pushed_into_where and st_select_lex::cond_pushed_into_having should be re-initialized for the unit specifying a derived table at every re-execution of the query that uses this derived table, because the result of condition pushdown may be different for different executions.
1 parent 35760c0 commit 423b7da

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed

mysql-test/r/derived_cond_pushdown.result

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8241,3 +8241,112 @@ SELECT * FROM v1 WHERE v1.d IN ( SELECT MIN(d) FROM t2 WHERE 0 );
82418241
d
82428242
DROP VIEW v1;
82438243
DROP TABLE t1,t2;
8244+
#
8245+
# MDEV-11820: second execution of PS for query
8246+
# with false subquery predicate in WHERE
8247+
#
8248+
CREATE TABLE t1 (c VARCHAR(3)) ENGINE=MyISAM;
8249+
INSERT INTO t1 VALUES ('foo'),('bar');
8250+
CREATE ALGORITHM=TEMPTABLE VIEW v1 AS SELECT * FROM t1;
8251+
CREATE TABLE t2 (a INT);
8252+
INSERT INTO t2 VALUES (3), (4);
8253+
PREPARE stmt1 FROM
8254+
" SELECT * FROM v1 WHERE 1 IN (SELECT a FROM t2) OR c = 'foo'";
8255+
PREPARE stmt2 FROM
8256+
"EXPLAIN FORMAT=JSON
8257+
SELECT * FROM v1 WHERE 1 IN (SELECT a FROM t2) OR c = 'foo'";
8258+
EXECUTE stmt1;
8259+
c
8260+
foo
8261+
EXECUTE stmt2;
8262+
EXPLAIN
8263+
{
8264+
"query_block": {
8265+
"select_id": 1,
8266+
"table": {
8267+
"table_name": "<derived3>",
8268+
"access_type": "ALL",
8269+
"rows": 2,
8270+
"filtered": 100,
8271+
"attached_condition": "v1.c = 'foo'",
8272+
"materialized": {
8273+
"query_block": {
8274+
"select_id": 3,
8275+
"table": {
8276+
"table_name": "t1",
8277+
"access_type": "ALL",
8278+
"rows": 2,
8279+
"filtered": 100,
8280+
"attached_condition": "t1.c = 'foo'"
8281+
}
8282+
}
8283+
}
8284+
},
8285+
"subqueries": [
8286+
{
8287+
"query_block": {
8288+
"select_id": 2,
8289+
"table": {
8290+
"table_name": "t2",
8291+
"access_type": "ALL",
8292+
"rows": 2,
8293+
"filtered": 100,
8294+
"attached_condition": "1 = t2.a"
8295+
}
8296+
}
8297+
}
8298+
]
8299+
}
8300+
}
8301+
INSERT INTO t2 SELECT a+1 FROM t2;
8302+
INSERT INTO t2 SELECT a+1 FROM t2;
8303+
INSERT INTO t2 SELECT a+1 FROM t2;
8304+
INSERT INTO t2 SELECT a+1 FROM t2;
8305+
INSERT INTO t2 SELECT a+1 FROM t2;
8306+
INSERT INTO t2 SELECT a+1 FROM t2;
8307+
EXECUTE stmt1;
8308+
c
8309+
foo
8310+
EXECUTE stmt2;
8311+
EXPLAIN
8312+
{
8313+
"query_block": {
8314+
"select_id": 1,
8315+
"table": {
8316+
"table_name": "<derived3>",
8317+
"access_type": "ALL",
8318+
"rows": 2,
8319+
"filtered": 100,
8320+
"attached_condition": "<in_optimizer>(1,<exists>(subquery#2)) or v1.c = 'foo'",
8321+
"materialized": {
8322+
"query_block": {
8323+
"select_id": 3,
8324+
"table": {
8325+
"table_name": "t1",
8326+
"access_type": "ALL",
8327+
"rows": 2,
8328+
"filtered": 100
8329+
}
8330+
}
8331+
}
8332+
},
8333+
"subqueries": [
8334+
{
8335+
"query_block": {
8336+
"select_id": 2,
8337+
"table": {
8338+
"table_name": "t2",
8339+
"access_type": "ALL",
8340+
"rows": 128,
8341+
"filtered": 100,
8342+
"attached_condition": "1 = t2.a"
8343+
}
8344+
}
8345+
}
8346+
]
8347+
}
8348+
}
8349+
DEALLOCATE PREPARE stmt1;
8350+
DEALLOCATE PREPARE stmt2;
8351+
DROP VIEW v1;
8352+
DROP TABLE t1,t2;

mysql-test/t/derived_cond_pushdown.test

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,3 +1319,37 @@ SELECT * FROM v1 WHERE v1.d IN ( SELECT MIN(d) FROM t2 WHERE 0 );
13191319

13201320
DROP VIEW v1;
13211321
DROP TABLE t1,t2;
1322+
1323+
--echo #
1324+
--echo # MDEV-11820: second execution of PS for query
1325+
--echo # with false subquery predicate in WHERE
1326+
--echo #
1327+
1328+
CREATE TABLE t1 (c VARCHAR(3)) ENGINE=MyISAM;
1329+
INSERT INTO t1 VALUES ('foo'),('bar');
1330+
CREATE ALGORITHM=TEMPTABLE VIEW v1 AS SELECT * FROM t1;
1331+
CREATE TABLE t2 (a INT);
1332+
INSERT INTO t2 VALUES (3), (4);
1333+
1334+
PREPARE stmt1 FROM
1335+
" SELECT * FROM v1 WHERE 1 IN (SELECT a FROM t2) OR c = 'foo'";
1336+
PREPARE stmt2 FROM
1337+
"EXPLAIN FORMAT=JSON
1338+
SELECT * FROM v1 WHERE 1 IN (SELECT a FROM t2) OR c = 'foo'";
1339+
EXECUTE stmt1;
1340+
EXECUTE stmt2;
1341+
INSERT INTO t2 SELECT a+1 FROM t2;
1342+
INSERT INTO t2 SELECT a+1 FROM t2;
1343+
INSERT INTO t2 SELECT a+1 FROM t2;
1344+
INSERT INTO t2 SELECT a+1 FROM t2;
1345+
INSERT INTO t2 SELECT a+1 FROM t2;
1346+
INSERT INTO t2 SELECT a+1 FROM t2;
1347+
EXECUTE stmt1;
1348+
EXECUTE stmt2;
1349+
DEALLOCATE PREPARE stmt1;
1350+
# the result here will change after the merge with the fix for mdev-11859
1351+
DEALLOCATE PREPARE stmt2;
1352+
1353+
DROP VIEW v1;
1354+
DROP TABLE t1,t2;
1355+

sql/sql_derived.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,6 +1099,11 @@ bool mysql_derived_reinit(THD *thd, LEX *lex, TABLE_LIST *derived)
10991099
unit->types.empty();
11001100
/* for derived tables & PS (which can't be reset by Item_subselect) */
11011101
unit->reinit_exec_mechanism();
1102+
for (st_select_lex *sl= unit->first_select(); sl; sl= sl->next_select())
1103+
{
1104+
sl->cond_pushed_into_where= NULL;
1105+
sl->cond_pushed_into_having= NULL;
1106+
}
11021107
unit->set_thd(thd);
11031108
DBUG_RETURN(FALSE);
11041109
}

0 commit comments

Comments
 (0)