Skip to content

Commit 9f36221

Browse files
committed
Fixed the bug mdev-12845.
This patch fills in a serious flaw in the code that supports condition pushdown into materialized views / derived tables. If a predicate happened to contain a reference to a mergeable view / derived table and it does not depended directly on the target materialized view / derived table then the predicate was not considered as a subject to pusdown to this view / derived table.
1 parent a8131e7 commit 9f36221

13 files changed

+568
-147
lines changed

mysql-test/r/derived.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ EXPLAIN SELECT * FROM (SELECT * FROM t1) AS table1,
554554
id select_type table type possible_keys key key_len ref rows Extra
555555
1 PRIMARY t1 system NULL NULL NULL NULL 1
556556
1 PRIMARY <derived3> ref key0 key0 5 const 0
557-
3 DERIVED t2 ALL NULL NULL NULL NULL 2 Using temporary
557+
3 DERIVED t2 ALL NULL NULL NULL NULL 2 Using where; Using temporary
558558
Warnings:
559559
Note 1249 Select 4 was reduced during optimization
560560
DROP TABLE t1, t2;

mysql-test/r/derived_cond_pushdown.result

Lines changed: 227 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7234,6 +7234,7 @@ EXPLAIN
72347234
"materialized": {
72357235
"query_block": {
72367236
"select_id": 5,
7237+
"having_condition": "s > 2",
72377238
"filesort": {
72387239
"sort_key": "t4.d",
72397240
"temporary_table": {
@@ -7605,6 +7606,7 @@ EXPLAIN
76057606
"materialized": {
76067607
"query_block": {
76077608
"select_id": 3,
7609+
"having_condition": "s < 50",
76087610
"filesort": {
76097611
"sort_key": "t3.a",
76107612
"temporary_table": {
@@ -7755,9 +7757,14 @@ EXPLAIN
77557757
"select_id": 4,
77567758
"table": {
77577759
"table_name": "t",
7758-
"access_type": "ALL",
7760+
"access_type": "range",
7761+
"possible_keys": ["PRIMARY"],
7762+
"key": "PRIMARY",
7763+
"key_length": "4",
7764+
"used_key_parts": ["pk"],
77597765
"rows": 2,
7760-
"filtered": 100
7766+
"filtered": 100,
7767+
"index_condition": "t.pk > 2"
77617768
}
77627769
}
77637770
}
@@ -8447,3 +8454,221 @@ WHERE row <> order_number;
84478454
row order_number
84488455
14 51
84498456
DROP TABLE sales_documents;
8457+
#
8458+
# MDEV-12845: pushdown from merged derived using equalities
8459+
#
8460+
create table t1 (a int);
8461+
insert into t1 values
8462+
(4), (8), (5), (3), (10), (2), (7);
8463+
create table t2 (b int, c int);
8464+
insert into t2 values
8465+
(2,1), (5,2), (2,2), (4,1), (4,3),
8466+
(5,3), (2,4), (4,6), (2,1);
8467+
create view v1 as
8468+
select b, sum(c) as s from t2 group by b;
8469+
create view v2 as
8470+
select distinct b, c from t2;
8471+
create view v3 as
8472+
select b, max(c) as m from t2 group by b;
8473+
select b
8474+
from ( select t1.a, v1.b, v1.s from t1, v1 where t1.a = v1.b ) as t
8475+
where b > 2;
8476+
b
8477+
4
8478+
5
8479+
explain format=json select b
8480+
from ( select t1.a, v1.b, v1.s from t1, v1 where t1.a = v1.b ) as t
8481+
where b > 2;
8482+
EXPLAIN
8483+
{
8484+
"query_block": {
8485+
"select_id": 1,
8486+
"table": {
8487+
"table_name": "t1",
8488+
"access_type": "ALL",
8489+
"rows": 7,
8490+
"filtered": 100,
8491+
"attached_condition": "t1.a > 2 and t1.a is not null"
8492+
},
8493+
"table": {
8494+
"table_name": "<derived3>",
8495+
"access_type": "ref",
8496+
"possible_keys": ["key0"],
8497+
"key": "key0",
8498+
"key_length": "5",
8499+
"used_key_parts": ["b"],
8500+
"ref": ["test.t1.a"],
8501+
"rows": 2,
8502+
"filtered": 100,
8503+
"materialized": {
8504+
"query_block": {
8505+
"select_id": 3,
8506+
"filesort": {
8507+
"sort_key": "t2.b",
8508+
"temporary_table": {
8509+
"table": {
8510+
"table_name": "t2",
8511+
"access_type": "ALL",
8512+
"rows": 9,
8513+
"filtered": 100,
8514+
"attached_condition": "t2.b > 2"
8515+
}
8516+
}
8517+
}
8518+
}
8519+
}
8520+
}
8521+
}
8522+
}
8523+
select a
8524+
from ( select t1.a, v1.b, v1.s from t1, v1 where t1.a = v1.b ) as t
8525+
where a > 2;
8526+
a
8527+
4
8528+
5
8529+
explain format=json select a
8530+
from ( select t1.a, v1.b, v1.s from t1, v1 where t1.a = v1.b ) as t
8531+
where a > 2;
8532+
EXPLAIN
8533+
{
8534+
"query_block": {
8535+
"select_id": 1,
8536+
"table": {
8537+
"table_name": "t1",
8538+
"access_type": "ALL",
8539+
"rows": 7,
8540+
"filtered": 100,
8541+
"attached_condition": "t1.a > 2 and t1.a is not null"
8542+
},
8543+
"table": {
8544+
"table_name": "<derived3>",
8545+
"access_type": "ref",
8546+
"possible_keys": ["key0"],
8547+
"key": "key0",
8548+
"key_length": "5",
8549+
"used_key_parts": ["b"],
8550+
"ref": ["test.t1.a"],
8551+
"rows": 2,
8552+
"filtered": 100,
8553+
"materialized": {
8554+
"query_block": {
8555+
"select_id": 3,
8556+
"filesort": {
8557+
"sort_key": "t2.b",
8558+
"temporary_table": {
8559+
"table": {
8560+
"table_name": "t2",
8561+
"access_type": "ALL",
8562+
"rows": 9,
8563+
"filtered": 100,
8564+
"attached_condition": "t2.b > 2"
8565+
}
8566+
}
8567+
}
8568+
}
8569+
}
8570+
}
8571+
}
8572+
}
8573+
select a
8574+
from ( select t1.a, v2.b, v2.c from t1, v2 where t1.a = v2.b ) as t
8575+
where a > 2;
8576+
a
8577+
4
8578+
4
8579+
4
8580+
5
8581+
5
8582+
explain format=json select a
8583+
from ( select t1.a, v2.b, v2.c from t1, v2 where t1.a = v2.b ) as t
8584+
where a > 2;
8585+
EXPLAIN
8586+
{
8587+
"query_block": {
8588+
"select_id": 1,
8589+
"table": {
8590+
"table_name": "t1",
8591+
"access_type": "ALL",
8592+
"rows": 7,
8593+
"filtered": 100,
8594+
"attached_condition": "t1.a > 2 and t1.a is not null"
8595+
},
8596+
"table": {
8597+
"table_name": "<derived3>",
8598+
"access_type": "ref",
8599+
"possible_keys": ["key0"],
8600+
"key": "key0",
8601+
"key_length": "5",
8602+
"used_key_parts": ["b"],
8603+
"ref": ["test.t1.a"],
8604+
"rows": 2,
8605+
"filtered": 100,
8606+
"materialized": {
8607+
"query_block": {
8608+
"select_id": 3,
8609+
"temporary_table": {
8610+
"table": {
8611+
"table_name": "t2",
8612+
"access_type": "ALL",
8613+
"rows": 9,
8614+
"filtered": 100,
8615+
"attached_condition": "t2.b > 2"
8616+
}
8617+
}
8618+
}
8619+
}
8620+
}
8621+
}
8622+
}
8623+
select a
8624+
from ( select t1.a, v3.b, v3.m from t1, v3 where t1.a = v3.m ) as t
8625+
where a > 2;
8626+
a
8627+
4
8628+
3
8629+
explain format=json select a
8630+
from ( select t1.a, v3.b, v3.m from t1, v3 where t1.a = v3.m ) as t
8631+
where a > 2;
8632+
EXPLAIN
8633+
{
8634+
"query_block": {
8635+
"select_id": 1,
8636+
"table": {
8637+
"table_name": "t1",
8638+
"access_type": "ALL",
8639+
"rows": 7,
8640+
"filtered": 100,
8641+
"attached_condition": "t1.a > 2 and t1.a is not null"
8642+
},
8643+
"table": {
8644+
"table_name": "<derived3>",
8645+
"access_type": "ref",
8646+
"possible_keys": ["key0"],
8647+
"key": "key0",
8648+
"key_length": "5",
8649+
"used_key_parts": ["m"],
8650+
"ref": ["test.t1.a"],
8651+
"rows": 2,
8652+
"filtered": 100,
8653+
"materialized": {
8654+
"query_block": {
8655+
"select_id": 3,
8656+
"having_condition": "m > 2",
8657+
"filesort": {
8658+
"sort_key": "t2.b",
8659+
"temporary_table": {
8660+
"table": {
8661+
"table_name": "t2",
8662+
"access_type": "ALL",
8663+
"rows": 9,
8664+
"filtered": 100
8665+
}
8666+
}
8667+
}
8668+
}
8669+
}
8670+
}
8671+
}
8672+
}
8673+
drop view v1,v2,v3;
8674+
drop table t1,t2;

mysql-test/r/derived_view.result

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ EXPLAIN
900900
"access_type": "ALL",
901901
"rows": 11,
902902
"filtered": 100,
903-
"attached_condition": "t1.f1 in (2,3)"
903+
"attached_condition": "t1.f1 < 7 and t1.f1 in (2,3)"
904904
}
905905
}
906906
}
@@ -1107,7 +1107,7 @@ id select_type table type possible_keys key key_len ref rows Extra
11071107
1 PRIMARY t1 system NULL NULL NULL NULL 1
11081108
1 PRIMARY t2 ref a a 4 const 1 Using index
11091109
1 PRIMARY <derived2> ref key0 key0 8 const,const 1
1110-
2 DERIVED t3 ALL NULL NULL NULL NULL 12 Using temporary; Using filesort
1110+
2 DERIVED t3 ALL NULL NULL NULL NULL 12 Using where; Using temporary; Using filesort
11111111
SELECT * FROM t1, t2, v1 WHERE t2.a=t1.a AND t2.a=v1.a AND t2.a=v1.b;
11121112
a a a b
11131113
c c c c

mysql-test/t/derived_cond_pushdown.test

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,3 +1452,60 @@ SELECT * FROM
14521452
WHERE row <> order_number;
14531453

14541454
DROP TABLE sales_documents;
1455+
1456+
--echo #
1457+
--echo # MDEV-12845: pushdown from merged derived using equalities
1458+
--echo #
1459+
1460+
create table t1 (a int);
1461+
insert into t1 values
1462+
(4), (8), (5), (3), (10), (2), (7);
1463+
1464+
create table t2 (b int, c int);
1465+
insert into t2 values
1466+
(2,1), (5,2), (2,2), (4,1), (4,3),
1467+
(5,3), (2,4), (4,6), (2,1);
1468+
1469+
create view v1 as
1470+
select b, sum(c) as s from t2 group by b;
1471+
1472+
create view v2 as
1473+
select distinct b, c from t2;
1474+
1475+
create view v3 as
1476+
select b, max(c) as m from t2 group by b;
1477+
1478+
let $q1=
1479+
select b
1480+
from ( select t1.a, v1.b, v1.s from t1, v1 where t1.a = v1.b ) as t
1481+
where b > 2;
1482+
1483+
eval $q1;
1484+
eval explain format=json $q1;
1485+
1486+
let $q2=
1487+
select a
1488+
from ( select t1.a, v1.b, v1.s from t1, v1 where t1.a = v1.b ) as t
1489+
where a > 2;
1490+
1491+
eval $q2;
1492+
eval explain format=json $q2;
1493+
1494+
let $q3=
1495+
select a
1496+
from ( select t1.a, v2.b, v2.c from t1, v2 where t1.a = v2.b ) as t
1497+
where a > 2;
1498+
1499+
eval $q3;
1500+
eval explain format=json $q3;
1501+
1502+
let $q4=
1503+
select a
1504+
from ( select t1.a, v3.b, v3.m from t1, v3 where t1.a = v3.m ) as t
1505+
where a > 2;
1506+
1507+
eval $q4;
1508+
eval explain format=json $q4;
1509+
1510+
drop view v1,v2,v3;
1511+
drop table t1,t2;

0 commit comments

Comments
 (0)