Skip to content

Commit 687c8be

Browse files
MDEV-19269 Pushdown into IN subquery is not made on the second execution of stmt
Item_in_subselect::is_jtbm_const_tab left initialized from previous execution of prepared statement. approved by Dave Gosselin (dave.gosselin@mariadb.com) PR#4215
1 parent 55e0c34 commit 687c8be

File tree

3 files changed

+175
-0
lines changed

3 files changed

+175
-0
lines changed

mysql-test/main/having_cond_pushdown.result

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5826,3 +5826,149 @@ SELECT LOAD_FILE('') AS f, a FROM t1 GROUP BY f, a HAVING f = a;
58265826
f a
58275827
DROP TABLE t1;
58285828
End of 10.5 tests
5829+
#
5830+
# MDEV-19269 Pushdown into IN subquery is not made on the second
5831+
# execution of stmt
5832+
#
5833+
create table t1 (a int, b int);
5834+
create table t2 (x int, y int);
5835+
insert into t1 values (1,1),(2,2);
5836+
insert into t2 values (1,1),(2,2),(2,3);
5837+
prepare stmt from "
5838+
EXPLAIN FORMAT=JSON
5839+
SELECT * FROM t1
5840+
WHERE a = b
5841+
AND (a,b) IN (SELECT t2.x, COUNT(t2.y) FROM t2 WHERE @a=1 GROUP BY t2.x);";
5842+
set @a=2;
5843+
execute stmt;
5844+
EXPLAIN
5845+
{
5846+
"query_block": {
5847+
"select_id": 1,
5848+
"table": {
5849+
"message": "Impossible WHERE noticed after reading const tables"
5850+
},
5851+
"subqueries": [
5852+
{
5853+
"materialization": {
5854+
"query_block": {
5855+
"select_id": 2,
5856+
"table": {
5857+
"message": "Impossible WHERE"
5858+
}
5859+
}
5860+
}
5861+
}
5862+
]
5863+
}
5864+
}
5865+
set @a=1;
5866+
# we expect to see having_condition in both the below statements
5867+
execute stmt;
5868+
EXPLAIN
5869+
{
5870+
"query_block": {
5871+
"select_id": 1,
5872+
"nested_loop": [
5873+
{
5874+
"table": {
5875+
"table_name": "t1",
5876+
"access_type": "ALL",
5877+
"rows": 2,
5878+
"filtered": 100,
5879+
"attached_condition": "t1.b = t1.a and t1.a is not null and t1.a is not null"
5880+
}
5881+
},
5882+
{
5883+
"table": {
5884+
"table_name": "<subquery2>",
5885+
"access_type": "eq_ref",
5886+
"possible_keys": ["distinct_key"],
5887+
"key": "distinct_key",
5888+
"key_length": "12",
5889+
"used_key_parts": ["x", "COUNT(t2.y)"],
5890+
"ref": ["test.t1.a", "test.t1.a"],
5891+
"rows": 1,
5892+
"filtered": 100,
5893+
"attached_condition": "t1.a = `<subquery2>`.`COUNT(t2.y)`",
5894+
"materialized": {
5895+
"unique": 1,
5896+
"materialization": {
5897+
"query_block": {
5898+
"select_id": 2,
5899+
"having_condition": "`COUNT(t2.y)` = t2.x",
5900+
"temporary_table": {
5901+
"nested_loop": [
5902+
{
5903+
"table": {
5904+
"table_name": "t2",
5905+
"access_type": "ALL",
5906+
"rows": 3,
5907+
"filtered": 100
5908+
}
5909+
}
5910+
]
5911+
}
5912+
}
5913+
}
5914+
}
5915+
}
5916+
}
5917+
]
5918+
}
5919+
}
5920+
execute stmt;
5921+
EXPLAIN
5922+
{
5923+
"query_block": {
5924+
"select_id": 1,
5925+
"nested_loop": [
5926+
{
5927+
"table": {
5928+
"table_name": "t1",
5929+
"access_type": "ALL",
5930+
"rows": 2,
5931+
"filtered": 100,
5932+
"attached_condition": "t1.b = t1.a and t1.a is not null and t1.a is not null"
5933+
}
5934+
},
5935+
{
5936+
"table": {
5937+
"table_name": "<subquery2>",
5938+
"access_type": "eq_ref",
5939+
"possible_keys": ["distinct_key"],
5940+
"key": "distinct_key",
5941+
"key_length": "12",
5942+
"used_key_parts": ["x", "COUNT(t2.y)"],
5943+
"ref": ["test.t1.a", "test.t1.a"],
5944+
"rows": 1,
5945+
"filtered": 100,
5946+
"attached_condition": "t1.a = `<subquery2>`.`COUNT(t2.y)`",
5947+
"materialized": {
5948+
"unique": 1,
5949+
"materialization": {
5950+
"query_block": {
5951+
"select_id": 2,
5952+
"having_condition": "`COUNT(t2.y)` = t2.x",
5953+
"temporary_table": {
5954+
"nested_loop": [
5955+
{
5956+
"table": {
5957+
"table_name": "t2",
5958+
"access_type": "ALL",
5959+
"rows": 3,
5960+
"filtered": 100
5961+
}
5962+
}
5963+
]
5964+
}
5965+
}
5966+
}
5967+
}
5968+
}
5969+
}
5970+
]
5971+
}
5972+
}
5973+
drop table t1, t2;
5974+
End of 10.11 tests

mysql-test/main/having_cond_pushdown.test

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,3 +1601,31 @@ SELECT LOAD_FILE('') AS f, a FROM t1 GROUP BY f, a HAVING f = a;
16011601
DROP TABLE t1;
16021602

16031603
--echo End of 10.5 tests
1604+
1605+
--echo #
1606+
--echo # MDEV-19269 Pushdown into IN subquery is not made on the second
1607+
--echo # execution of stmt
1608+
--echo #
1609+
1610+
create table t1 (a int, b int);
1611+
create table t2 (x int, y int);
1612+
1613+
insert into t1 values (1,1),(2,2);
1614+
insert into t2 values (1,1),(2,2),(2,3);
1615+
1616+
prepare stmt from "
1617+
EXPLAIN FORMAT=JSON
1618+
SELECT * FROM t1
1619+
WHERE a = b
1620+
AND (a,b) IN (SELECT t2.x, COUNT(t2.y) FROM t2 WHERE @a=1 GROUP BY t2.x);";
1621+
1622+
set @a=2;
1623+
execute stmt;
1624+
set @a=1;
1625+
--echo # we expect to see having_condition in both the below statements
1626+
execute stmt;
1627+
execute stmt;
1628+
1629+
drop table t1, t2;
1630+
1631+
--echo End of 10.11 tests

sql/item_subselect.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,7 @@ class Item_in_subselect :public Item_exists_subselect
639639
value= 0;
640640
null_value= 0;
641641
was_null= 0;
642+
is_jtbm_const_tab= 0;
642643
}
643644
bool select_transformer(JOIN *join) override;
644645
bool create_in_to_exists_cond(JOIN *join_arg);

0 commit comments

Comments
 (0)