1set @@join_buffer_size=256*1024;
2create table t1 (a int, b int, c int);
3create table t2 (a int, b int, c int, d decimal);
4insert into t1 values
5(1,21,345), (1,33,7), (8,33,114), (1,21,500), (1,19,107), (5,14,787),
6(8,33,123), (9,10,211), (5,16,207), (1,33,988), (5,27,132), (1,21,104),
7(6,20,309), (6,20,315), (1,21,101), (8,33,404), (9,10,800), (1,21,123),
8(7,11,708), (6,20,214);
9insert into t2 values
10(2,3,207,207.0000), (1,21,909,12.0000), (7,13,312,406.0000),
11(8,64,248,107.0000), (6,20,315,279.3333), (1,19,203,107.0000),
12(8,80,800,314.0000), (3,12,231,190.0000), (6,23,303,909.0000);
13Warnings:
14Note	1265	Data truncated for column 'd' at row 5
15create table t1_double(a int, b double, c double);
16insert into t1_double values
17(1,23.4,14.3333), (1,12.5,18.9), (3,12.5,18.9),
18(4,33.4,14.3333), (4,14.3333,13.65), (5,17.89,7.22),
19(6,33.4,14.3), (10,33.4,13.65), (11,33.4,13.65);
20create table t2_double(a int, b double, c double);
21insert into t2_double values
22(1,22.4,14.3333), (1,12.5,18.9), (2,22.4,18.9),
23(4,33.4,14.3333), (5,22.4,13.65), (7,17.89,18.9),
24(6,33.4,14.3333), (10,31.4,13.65), (12,33.4,13.65);
25create table t1_char(a char, b char(8), c int);
26insert into t1_char values
27('a','Ivan',1), ('b','Vika',2), ('b','Inga',6), ('c','Vika',7),
28('b','Ivan',7), ('a','Alex',6), ('b','Inga',5), ('d','Ron',9),
29('d','Harry',2), ('d','Hermione',3), ('c','Ivan',3), ('c','Harry',4);
30create table t2_char(a char, b char(8), c int);
31insert into t2_char values
32('b','Ivan',1), ('c','Vinny',3), ('c','Inga',9), ('a','Vika',1),
33('c','Ivan',2), ('b','Ali',6), ('c','Inga',2), ('a','Ron',9),
34('d','Harry',1), ('b','Hermes',3), ('b','Ivan',11), ('b','Harry',4);
35create table t1_decimal (a decimal(3,1), b decimal(3,1), c int);
36insert into t1_decimal values
37(1,1,23),(2,2,11),(3,3,16),
38(1,1,12),(1,1,14),(2,3,15),
39(2,1,13),(2,3,11),(3,3,16);
40create table t2_decimal (a decimal(3,1), b decimal(3,1), c int);
41insert into t2_decimal values
42(2,1,13),(2,2,11),(3,3,16),
43(1,3,22),(1,3,14),(2,2,15),
44(2,1,43),(2,3,11),(2,3,16);
45create view v1 as select a, b, max(c) as max_c, avg(c) as avg_c from t1
46group by a,b having max_c < 707;
47create view v2 as select a, b, max(c) as max_c, avg(c) as avg_c from t1
48where t1.a>5 group by a,b having max_c < 707;
49create view v3 as select a, b, min(c) as min_c from t1
50where t1.a<10 group by a,b having min_c > 109;
51create view v4 as
52select a, b, min(max_c) as min_c from v1
53where (v1.a<15) group by a,b;
54create view v_union as
55select a, b, min(c) as c from t1
56where t1.a<10 group by a,b having c > 109
57union
58select a, b, max(c) as c from t1
59where t1.b>10 group by a,b having c < 300;
60create view v2_union as
61select a, b, min(c) as c from t1
62where t1.a<10 group by a,b having c > 109
63union
64select a, b, max(c) as c from t1
65where t1.b>10 group by a,b having c < 300
66union
67select a, b, avg(c) as c from t1
68where t1.c>300 group by a,b having c < 707;
69create view v3_union as
70select a, b, (a+1) as c from t1
71where t1.a<10
72union
73select a, b, c from t1
74where t1.b>10 and t1.c>100;
75create view v4_union as
76select a, b, max(c)-100 as c from t1
77where t1.a<10 group by a,b having c > 109
78union
79select a, b, (c+100) as c from t1
80where t1.b>10;
81create view v_double as
82select a, avg(a/4) as avg_a, b, c from t1_double
83where (b>12.2) group by b,c having (avg_a<22.333);
84create view v_char as
85select a, b, max(c) as max_c from t1_char
86group by a,b having max_c < 9;
87create view v_decimal as
88select a, b, avg(c) as avg_c from t1_decimal
89group by a,b having (avg_c>12);
90# conjunctive subformula : pushing into HAVING
91set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.max_c>214) and (t2.a>v1.a);
92a	b	max_c	avg_c	a	b	c	d
931	21	500	234.6000	2	3	207	207
941	21	500	234.6000	7	13	312	406
951	21	500	234.6000	8	64	248	107
961	21	500	234.6000	6	20	315	279
971	21	500	234.6000	8	80	800	314
981	21	500	234.6000	3	12	231	190
991	21	500	234.6000	6	23	303	909
1006	20	315	279.3333	7	13	312	406
1016	20	315	279.3333	8	64	248	107
1026	20	315	279.3333	8	80	800	314
103select * from v1,t2 where (v1.max_c>214) and (t2.a>v1.a);
104a	b	max_c	avg_c	a	b	c	d
1051	21	500	234.6000	2	3	207	207
1061	21	500	234.6000	7	13	312	406
1071	21	500	234.6000	8	64	248	107
1081	21	500	234.6000	6	20	315	279
1091	21	500	234.6000	8	80	800	314
1101	21	500	234.6000	3	12	231	190
1111	21	500	234.6000	6	23	303	909
1126	20	315	279.3333	7	13	312	406
1136	20	315	279.3333	8	64	248	107
1146	20	315	279.3333	8	80	800	314
115explain select * from v1,t2 where (v1.max_c>214) and (t2.a>v1.a);
116id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1171	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9
1181	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
1192	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using temporary; Using filesort
120explain format=json select * from v1,t2 where (v1.max_c>214) and (t2.a>v1.a);
121EXPLAIN
122{
123  "query_block": {
124    "select_id": 1,
125    "table": {
126      "table_name": "t2",
127      "access_type": "ALL",
128      "rows": 9,
129      "filtered": 100
130    },
131    "block-nl-join": {
132      "table": {
133        "table_name": "<derived2>",
134        "access_type": "ALL",
135        "rows": 20,
136        "filtered": 100,
137        "attached_condition": "v1.max_c > 214"
138      },
139      "buffer_type": "flat",
140      "buffer_size": "238",
141      "join_type": "BNL",
142      "attached_condition": "t2.a > v1.a",
143      "materialized": {
144        "query_block": {
145          "select_id": 2,
146          "having_condition": "max_c < 707 and max_c > 214",
147          "filesort": {
148            "sort_key": "t1.a, t1.b",
149            "temporary_table": {
150              "table": {
151                "table_name": "t1",
152                "access_type": "ALL",
153                "rows": 20,
154                "filtered": 100
155              }
156            }
157          }
158        }
159      }
160    }
161  }
162}
163set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from
164(select a, b, max(c) as max_c, avg(c) as avg_c from t1
165group by a,b having max_c < 707) v1,
166t2 where (v1.a=t2.a) and (v1.max_c>300);
167a	b	max_c	avg_c	a	b	c	d
1681	21	500	234.6000	1	21	909	12
1698	33	404	213.6667	8	64	248	107
1706	20	315	279.3333	6	20	315	279
1711	21	500	234.6000	1	19	203	107
1728	33	404	213.6667	8	80	800	314
1736	20	315	279.3333	6	23	303	909
174select * from
175(select a, b, max(c) as max_c, avg(c) as avg_c from t1
176group by a,b having max_c < 707) v1,
177t2 where (v1.a=t2.a) and (v1.max_c>300);
178a	b	max_c	avg_c	a	b	c	d
1791	21	500	234.6000	1	21	909	12
1808	33	404	213.6667	8	64	248	107
1816	20	315	279.3333	6	20	315	279
1821	21	500	234.6000	1	19	203	107
1838	33	404	213.6667	8	80	800	314
1846	20	315	279.3333	6	23	303	909
185explain select * from
186(select a, b, max(c) as max_c, avg(c) as avg_c from t1
187group by a,b having max_c < 707) v1,
188t2 where (v1.a=t2.a) and (v1.max_c>300);
189id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1901	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1911	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.a	2	Using where
1922	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using temporary; Using filesort
193explain format=json select * from
194(select a, b, max(c) as max_c, avg(c) as avg_c from t1
195group by a,b having max_c < 707) v1,
196t2 where (v1.a=t2.a) and (v1.max_c>300);
197EXPLAIN
198{
199  "query_block": {
200    "select_id": 1,
201    "table": {
202      "table_name": "t2",
203      "access_type": "ALL",
204      "rows": 9,
205      "filtered": 100,
206      "attached_condition": "t2.a is not null"
207    },
208    "table": {
209      "table_name": "<derived2>",
210      "access_type": "ref",
211      "possible_keys": ["key0"],
212      "key": "key0",
213      "key_length": "5",
214      "used_key_parts": ["a"],
215      "ref": ["test.t2.a"],
216      "rows": 2,
217      "filtered": 100,
218      "attached_condition": "v1.max_c > 300",
219      "materialized": {
220        "query_block": {
221          "select_id": 2,
222          "having_condition": "max_c < 707 and max_c > 300",
223          "filesort": {
224            "sort_key": "t1.a, t1.b",
225            "temporary_table": {
226              "table": {
227                "table_name": "t1",
228                "access_type": "ALL",
229                "rows": 20,
230                "filtered": 100
231              }
232            }
233          }
234        }
235      }
236    }
237  }
238}
239# extracted or formula : pushing into HAVING
240set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where
241((v1.max_c>400) and (t2.a>v1.a)) or ((v1.max_c<135) and (t2.a<v1.a));
242a	b	max_c	avg_c	a	b	c	d
2431	21	500	234.6000	2	3	207	207
2441	21	500	234.6000	7	13	312	406
2451	21	500	234.6000	8	64	248	107
2461	21	500	234.6000	6	20	315	279
2471	21	500	234.6000	8	80	800	314
2481	21	500	234.6000	3	12	231	190
2491	21	500	234.6000	6	23	303	909
2505	27	132	132.0000	2	3	207	207
2515	27	132	132.0000	1	21	909	12
2525	27	132	132.0000	1	19	203	107
2535	27	132	132.0000	3	12	231	190
254select * from v1,t2 where
255((v1.max_c>400) and (t2.a>v1.a)) or ((v1.max_c<135) and (t2.a<v1.a));
256a	b	max_c	avg_c	a	b	c	d
2571	21	500	234.6000	2	3	207	207
2581	21	500	234.6000	7	13	312	406
2591	21	500	234.6000	8	64	248	107
2601	21	500	234.6000	6	20	315	279
2611	21	500	234.6000	8	80	800	314
2621	21	500	234.6000	3	12	231	190
2631	21	500	234.6000	6	23	303	909
2645	27	132	132.0000	2	3	207	207
2655	27	132	132.0000	1	21	909	12
2665	27	132	132.0000	1	19	203	107
2675	27	132	132.0000	3	12	231	190
268explain select * from v1,t2 where
269((v1.max_c>400) and (t2.a>v1.a)) or ((v1.max_c<135) and (t2.a<v1.a));
270id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
2711	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9
2721	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
2732	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using temporary; Using filesort
274explain format=json select * from v1,t2 where
275((v1.max_c>400) and (t2.a>v1.a)) or ((v1.max_c<135) and (t2.a<v1.a));
276EXPLAIN
277{
278  "query_block": {
279    "select_id": 1,
280    "table": {
281      "table_name": "t2",
282      "access_type": "ALL",
283      "rows": 9,
284      "filtered": 100
285    },
286    "block-nl-join": {
287      "table": {
288        "table_name": "<derived2>",
289        "access_type": "ALL",
290        "rows": 20,
291        "filtered": 100,
292        "attached_condition": "v1.max_c > 400 or v1.max_c < 135"
293      },
294      "buffer_type": "flat",
295      "buffer_size": "238",
296      "join_type": "BNL",
297      "attached_condition": "v1.max_c > 400 and t2.a > v1.a or v1.max_c < 135 and t2.a < v1.a",
298      "materialized": {
299        "query_block": {
300          "select_id": 2,
301          "having_condition": "max_c < 707 and (max_c > 400 or max_c < 135)",
302          "filesort": {
303            "sort_key": "t1.a, t1.b",
304            "temporary_table": {
305              "table": {
306                "table_name": "t1",
307                "access_type": "ALL",
308                "rows": 20,
309                "filtered": 100
310              }
311            }
312          }
313        }
314      }
315    }
316  }
317}
318set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where
319((v1.max_c>300) and (v1.avg_c>t2.d) and (v1.b=t2.b)) or
320((v1.max_c<135) and (v1.max_c<t2.c) and (v1.a=t2.a));
321a	b	max_c	avg_c	a	b	c	d
3221	19	107	107.0000	1	21	909	12
3231	19	107	107.0000	1	19	203	107
3241	21	500	234.6000	1	21	909	12
3256	20	315	279.3333	6	20	315	279
326select * from v1,t2 where
327((v1.max_c>300) and (v1.avg_c>t2.d) and (v1.b=t2.b)) or
328((v1.max_c<135) and (v1.max_c<t2.c) and (v1.a=t2.a));
329a	b	max_c	avg_c	a	b	c	d
3301	19	107	107.0000	1	21	909	12
3311	19	107	107.0000	1	19	203	107
3321	21	500	234.6000	1	21	909	12
3336	20	315	279.3333	6	20	315	279
334explain select * from v1,t2 where
335((v1.max_c>300) and (v1.avg_c>t2.d) and (v1.b=t2.b)) or
336((v1.max_c<135) and (v1.max_c<t2.c) and (v1.a=t2.a));
337id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
3381	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9
3391	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
3402	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using temporary; Using filesort
341explain format=json select * from v1,t2 where
342((v1.max_c>300) and (v1.avg_c>t2.d) and (v1.b=t2.b)) or
343((v1.max_c<135) and (v1.max_c<t2.c) and (v1.a=t2.a));
344EXPLAIN
345{
346  "query_block": {
347    "select_id": 1,
348    "table": {
349      "table_name": "t2",
350      "access_type": "ALL",
351      "rows": 9,
352      "filtered": 100
353    },
354    "block-nl-join": {
355      "table": {
356        "table_name": "<derived2>",
357        "access_type": "ALL",
358        "rows": 20,
359        "filtered": 100,
360        "attached_condition": "v1.max_c > 300 or v1.max_c < 135"
361      },
362      "buffer_type": "flat",
363      "buffer_size": "238",
364      "join_type": "BNL",
365      "attached_condition": "v1.b = t2.b and v1.max_c > 300 and v1.avg_c > t2.d or v1.a = t2.a and v1.max_c < 135 and v1.max_c < t2.c",
366      "materialized": {
367        "query_block": {
368          "select_id": 2,
369          "having_condition": "max_c < 707 and (max_c > 300 or max_c < 135)",
370          "filesort": {
371            "sort_key": "t1.a, t1.b",
372            "temporary_table": {
373              "table": {
374                "table_name": "t1",
375                "access_type": "ALL",
376                "rows": 20,
377                "filtered": 100
378              }
379            }
380          }
381        }
382      }
383    }
384  }
385}
386# conjunctive subformula : pushing into WHERE
387set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.a>6) and (t2.b>v1.b);
388a	b	max_c	avg_c	a	b	c	d
3898	33	404	213.6667	8	64	248	107
3908	33	404	213.6667	8	80	800	314
391select * from v1,t2 where (v1.a>6) and (t2.b>v1.b);
392a	b	max_c	avg_c	a	b	c	d
3938	33	404	213.6667	8	64	248	107
3948	33	404	213.6667	8	80	800	314
395explain select * from v1,t2 where (v1.a>6) and (t2.b>v1.b);
396id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
3971	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9
3981	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
3992	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
400explain format=json select * from v1,t2 where (v1.a>6) and (t2.b>v1.b);
401EXPLAIN
402{
403  "query_block": {
404    "select_id": 1,
405    "table": {
406      "table_name": "t2",
407      "access_type": "ALL",
408      "rows": 9,
409      "filtered": 100
410    },
411    "block-nl-join": {
412      "table": {
413        "table_name": "<derived2>",
414        "access_type": "ALL",
415        "rows": 20,
416        "filtered": 100,
417        "attached_condition": "v1.a > 6"
418      },
419      "buffer_type": "flat",
420      "buffer_size": "238",
421      "join_type": "BNL",
422      "attached_condition": "t2.b > v1.b",
423      "materialized": {
424        "query_block": {
425          "select_id": 2,
426          "having_condition": "max_c < 707",
427          "filesort": {
428            "sort_key": "t1.a, t1.b",
429            "temporary_table": {
430              "table": {
431                "table_name": "t1",
432                "access_type": "ALL",
433                "rows": 20,
434                "filtered": 100,
435                "attached_condition": "t1.a > 6"
436              }
437            }
438          }
439        }
440      }
441    }
442  }
443}
444set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v2,t2 where (v2.b>25) and (t2.a<v2.a);
445a	b	max_c	avg_c	a	b	c	d
4468	33	404	213.6667	2	3	207	207
4478	33	404	213.6667	1	21	909	12
4488	33	404	213.6667	7	13	312	406
4498	33	404	213.6667	6	20	315	279
4508	33	404	213.6667	1	19	203	107
4518	33	404	213.6667	3	12	231	190
4528	33	404	213.6667	6	23	303	909
453select * from v2,t2 where (v2.b>25) and (t2.a<v2.a);
454a	b	max_c	avg_c	a	b	c	d
4558	33	404	213.6667	2	3	207	207
4568	33	404	213.6667	1	21	909	12
4578	33	404	213.6667	7	13	312	406
4588	33	404	213.6667	6	20	315	279
4598	33	404	213.6667	1	19	203	107
4608	33	404	213.6667	3	12	231	190
4618	33	404	213.6667	6	23	303	909
462explain select * from v2,t2 where (v2.b>25) and (t2.a<v2.a);
463id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
4641	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9
4651	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
4662	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
467explain format=json select * from v2,t2 where (v2.b>25) and (t2.a<v2.a);
468EXPLAIN
469{
470  "query_block": {
471    "select_id": 1,
472    "table": {
473      "table_name": "t2",
474      "access_type": "ALL",
475      "rows": 9,
476      "filtered": 100
477    },
478    "block-nl-join": {
479      "table": {
480        "table_name": "<derived2>",
481        "access_type": "ALL",
482        "rows": 20,
483        "filtered": 100,
484        "attached_condition": "v2.b > 25"
485      },
486      "buffer_type": "flat",
487      "buffer_size": "238",
488      "join_type": "BNL",
489      "attached_condition": "t2.a < v2.a",
490      "materialized": {
491        "query_block": {
492          "select_id": 2,
493          "having_condition": "max_c < 707",
494          "filesort": {
495            "sort_key": "t1.a, t1.b",
496            "temporary_table": {
497              "table": {
498                "table_name": "t1",
499                "access_type": "ALL",
500                "rows": 20,
501                "filtered": 100,
502                "attached_condition": "t1.a > 5 and t1.b > 25"
503              }
504            }
505          }
506        }
507      }
508    }
509  }
510}
511# extracted or formula : pushing into WHERE
512set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where
513((v1.a>7) and (t2.c<v1.max_c)) or ((v1.a<2) and (t2.b<v1.b));
514a	b	max_c	avg_c	a	b	c	d
5151	19	107	107.0000	2	3	207	207
5161	19	107	107.0000	7	13	312	406
5171	19	107	107.0000	3	12	231	190
5181	21	500	234.6000	2	3	207	207
5191	21	500	234.6000	7	13	312	406
5201	21	500	234.6000	6	20	315	279
5211	21	500	234.6000	1	19	203	107
5221	21	500	234.6000	3	12	231	190
5238	33	404	213.6667	2	3	207	207
5248	33	404	213.6667	7	13	312	406
5258	33	404	213.6667	8	64	248	107
5268	33	404	213.6667	6	20	315	279
5278	33	404	213.6667	1	19	203	107
5288	33	404	213.6667	3	12	231	190
5298	33	404	213.6667	6	23	303	909
530select * from v1,t2 where
531((v1.a>7) and (t2.c<v1.max_c)) or ((v1.a<2) and (t2.b<v1.b));
532a	b	max_c	avg_c	a	b	c	d
5331	19	107	107.0000	2	3	207	207
5341	19	107	107.0000	7	13	312	406
5351	19	107	107.0000	3	12	231	190
5361	21	500	234.6000	2	3	207	207
5371	21	500	234.6000	7	13	312	406
5381	21	500	234.6000	6	20	315	279
5391	21	500	234.6000	1	19	203	107
5401	21	500	234.6000	3	12	231	190
5418	33	404	213.6667	2	3	207	207
5428	33	404	213.6667	7	13	312	406
5438	33	404	213.6667	8	64	248	107
5448	33	404	213.6667	6	20	315	279
5458	33	404	213.6667	1	19	203	107
5468	33	404	213.6667	3	12	231	190
5478	33	404	213.6667	6	23	303	909
548explain select * from v1,t2 where
549((v1.a>7) and (t2.c<v1.max_c)) or ((v1.a<2) and (t2.b<v1.b));
550id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
5511	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9
5521	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
5532	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
554explain format=json select * from v1,t2 where
555((v1.a>7) and (t2.c<v1.max_c)) or ((v1.a<2) and (t2.b<v1.b));
556EXPLAIN
557{
558  "query_block": {
559    "select_id": 1,
560    "table": {
561      "table_name": "t2",
562      "access_type": "ALL",
563      "rows": 9,
564      "filtered": 100
565    },
566    "block-nl-join": {
567      "table": {
568        "table_name": "<derived2>",
569        "access_type": "ALL",
570        "rows": 20,
571        "filtered": 100,
572        "attached_condition": "v1.a > 7 or v1.a < 2"
573      },
574      "buffer_type": "flat",
575      "buffer_size": "238",
576      "join_type": "BNL",
577      "attached_condition": "v1.a > 7 and t2.c < v1.max_c or v1.a < 2 and t2.b < v1.b",
578      "materialized": {
579        "query_block": {
580          "select_id": 2,
581          "having_condition": "max_c < 707",
582          "filesort": {
583            "sort_key": "t1.a, t1.b",
584            "temporary_table": {
585              "table": {
586                "table_name": "t1",
587                "access_type": "ALL",
588                "rows": 20,
589                "filtered": 100,
590                "attached_condition": "t1.a > 7 or t1.a < 2"
591              }
592            }
593          }
594        }
595      }
596    }
597  }
598}
599set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v2,t2 where
600((v2.a>7) and (t2.c<v2.max_c)) or ((v2.a>5) and (t2.b<v2.b));
601a	b	max_c	avg_c	a	b	c	d
6026	20	315	279.3333	2	3	207	207
6036	20	315	279.3333	7	13	312	406
6046	20	315	279.3333	1	19	203	107
6056	20	315	279.3333	3	12	231	190
6068	33	404	213.6667	2	3	207	207
6078	33	404	213.6667	1	21	909	12
6088	33	404	213.6667	7	13	312	406
6098	33	404	213.6667	8	64	248	107
6108	33	404	213.6667	6	20	315	279
6118	33	404	213.6667	1	19	203	107
6128	33	404	213.6667	3	12	231	190
6138	33	404	213.6667	6	23	303	909
614select * from v2,t2 where
615((v2.a>7) and (t2.c<v2.max_c)) or ((v2.a>5) and (t2.b<v2.b));
616a	b	max_c	avg_c	a	b	c	d
6176	20	315	279.3333	2	3	207	207
6186	20	315	279.3333	7	13	312	406
6196	20	315	279.3333	1	19	203	107
6206	20	315	279.3333	3	12	231	190
6218	33	404	213.6667	2	3	207	207
6228	33	404	213.6667	1	21	909	12
6238	33	404	213.6667	7	13	312	406
6248	33	404	213.6667	8	64	248	107
6258	33	404	213.6667	6	20	315	279
6268	33	404	213.6667	1	19	203	107
6278	33	404	213.6667	3	12	231	190
6288	33	404	213.6667	6	23	303	909
629explain select * from v2,t2 where
630((v2.a>7) and (t2.c<v2.max_c)) or ((v2.a>5) and (t2.b<v2.b));
631id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
6321	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9
6331	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
6342	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
635explain format=json select * from v2,t2 where
636((v2.a>7) and (t2.c<v2.max_c)) or ((v2.a>5) and (t2.b<v2.b));
637EXPLAIN
638{
639  "query_block": {
640    "select_id": 1,
641    "table": {
642      "table_name": "t2",
643      "access_type": "ALL",
644      "rows": 9,
645      "filtered": 100
646    },
647    "block-nl-join": {
648      "table": {
649        "table_name": "<derived2>",
650        "access_type": "ALL",
651        "rows": 20,
652        "filtered": 100,
653        "attached_condition": "v2.a > 7 or v2.a > 5"
654      },
655      "buffer_type": "flat",
656      "buffer_size": "238",
657      "join_type": "BNL",
658      "attached_condition": "v2.a > 7 and t2.c < v2.max_c or v2.a > 5 and t2.b < v2.b",
659      "materialized": {
660        "query_block": {
661          "select_id": 2,
662          "having_condition": "max_c < 707",
663          "filesort": {
664            "sort_key": "t1.a, t1.b",
665            "temporary_table": {
666              "table": {
667                "table_name": "t1",
668                "access_type": "ALL",
669                "rows": 20,
670                "filtered": 100,
671                "attached_condition": "t1.a > 5 and (t1.a > 7 or t1.a > 5)"
672              }
673            }
674          }
675        }
676      }
677    }
678  }
679}
680set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where
681((v1.a>4) and (v1.b>t2.b) and (v1.max_c=t2.d)) or
682((v1.a<2) and (v1.max_c<t2.c) and (v1.max_c=t2.d));
683a	b	max_c	avg_c	a	b	c	d
6841	19	107	107.0000	8	64	248	107
6851	19	107	107.0000	1	19	203	107
6865	16	207	207.0000	2	3	207	207
687select * from v1,t2 where
688((v1.a>4) and (v1.b>t2.b) and (v1.max_c=t2.d)) or
689((v1.a<2) and (v1.max_c<t2.c) and (v1.max_c=t2.d));
690a	b	max_c	avg_c	a	b	c	d
6911	19	107	107.0000	8	64	248	107
6921	19	107	107.0000	1	19	203	107
6935	16	207	207.0000	2	3	207	207
694explain select * from v1,t2 where
695((v1.a>4) and (v1.b>t2.b) and (v1.max_c=t2.d)) or
696((v1.a<2) and (v1.max_c<t2.c) and (v1.max_c=t2.d));
697id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
6981	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9
6991	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
7002	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
701explain format=json select * from v1,t2 where
702((v1.a>4) and (v1.b>t2.b) and (v1.max_c=t2.d)) or
703((v1.a<2) and (v1.max_c<t2.c) and (v1.max_c=t2.d));
704EXPLAIN
705{
706  "query_block": {
707    "select_id": 1,
708    "table": {
709      "table_name": "t2",
710      "access_type": "ALL",
711      "rows": 9,
712      "filtered": 100
713    },
714    "block-nl-join": {
715      "table": {
716        "table_name": "<derived2>",
717        "access_type": "ALL",
718        "rows": 20,
719        "filtered": 100,
720        "attached_condition": "v1.a > 4 or v1.a < 2"
721      },
722      "buffer_type": "flat",
723      "buffer_size": "238",
724      "join_type": "BNL",
725      "attached_condition": "v1.a > 4 and v1.b > t2.b and v1.max_c = t2.d or v1.a < 2 and v1.max_c < t2.c and v1.max_c = t2.d",
726      "materialized": {
727        "query_block": {
728          "select_id": 2,
729          "having_condition": "max_c < 707",
730          "filesort": {
731            "sort_key": "t1.a, t1.b",
732            "temporary_table": {
733              "table": {
734                "table_name": "t1",
735                "access_type": "ALL",
736                "rows": 20,
737                "filtered": 100,
738                "attached_condition": "t1.a > 4 or t1.a < 2"
739              }
740            }
741          }
742        }
743      }
744    }
745  }
746}
747# conjunctive subformulas : pushing into HAVING and WHERE
748set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.a<2) and (v1.max_c>400) and (t2.b>v1.b);
749a	b	max_c	avg_c	a	b	c	d
7501	21	500	234.6000	8	64	248	107
7511	21	500	234.6000	8	80	800	314
7521	21	500	234.6000	6	23	303	909
753select * from v1,t2 where (v1.a<2) and (v1.max_c>400) and (t2.b>v1.b);
754a	b	max_c	avg_c	a	b	c	d
7551	21	500	234.6000	8	64	248	107
7561	21	500	234.6000	8	80	800	314
7571	21	500	234.6000	6	23	303	909
758explain select * from v1,t2 where (v1.a<2) and (v1.max_c>400) and (t2.b>v1.b);
759id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
7601	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9
7611	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
7622	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
763explain format=json select * from v1,t2 where (v1.a<2) and (v1.max_c>400) and (t2.b>v1.b);
764EXPLAIN
765{
766  "query_block": {
767    "select_id": 1,
768    "table": {
769      "table_name": "t2",
770      "access_type": "ALL",
771      "rows": 9,
772      "filtered": 100
773    },
774    "block-nl-join": {
775      "table": {
776        "table_name": "<derived2>",
777        "access_type": "ALL",
778        "rows": 20,
779        "filtered": 100,
780        "attached_condition": "v1.a < 2 and v1.max_c > 400"
781      },
782      "buffer_type": "flat",
783      "buffer_size": "238",
784      "join_type": "BNL",
785      "attached_condition": "t2.b > v1.b",
786      "materialized": {
787        "query_block": {
788          "select_id": 2,
789          "having_condition": "max_c < 707 and max_c > 400",
790          "filesort": {
791            "sort_key": "t1.a, t1.b",
792            "temporary_table": {
793              "table": {
794                "table_name": "t1",
795                "access_type": "ALL",
796                "rows": 20,
797                "filtered": 100,
798                "attached_condition": "t1.a < 2"
799              }
800            }
801          }
802        }
803      }
804    }
805  }
806}
807set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v_double as v,t2_double as t where
808(v.a=t.a) and (v.avg_a>0.45) and (v.b>10);
809a	avg_a	b	c	a	b	c
8101	0.50000000	12.5	18.9	1	22.4	14.3333
8111	0.50000000	12.5	18.9	1	12.5	18.9
8124	1.00000000	33.4	14.3333	4	33.4	14.3333
8134	1.00000000	14.3333	13.65	4	33.4	14.3333
8145	1.25000000	17.89	7.22	5	22.4	13.65
8156	1.50000000	33.4	14.3	6	33.4	14.3333
81610	2.62500000	33.4	13.65	10	31.4	13.65
817select * from v_double as v,t2_double as t where
818(v.a=t.a) and (v.avg_a>0.45) and (v.b>10);
819a	avg_a	b	c	a	b	c
8201	0.50000000	12.5	18.9	1	22.4	14.3333
8211	0.50000000	12.5	18.9	1	12.5	18.9
8224	1.00000000	33.4	14.3333	4	33.4	14.3333
8234	1.00000000	14.3333	13.65	4	33.4	14.3333
8245	1.25000000	17.89	7.22	5	22.4	13.65
8256	1.50000000	33.4	14.3	6	33.4	14.3333
82610	2.62500000	33.4	13.65	10	31.4	13.65
827explain select * from v_double as v,t2_double as t where
828(v.a=t.a) and (v.avg_a>0.45) and (v.b>10);
829id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
8301	PRIMARY	t	ALL	NULL	NULL	NULL	NULL	9	Using where
8311	PRIMARY	<derived2>	ref	key0	key0	5	test.t.a	2	Using where
8322	DERIVED	t1_double	ALL	NULL	NULL	NULL	NULL	9	Using where; Using temporary; Using filesort
833explain format=json select * from v_double as v,t2_double as t where
834(v.a=t.a) and (v.avg_a>0.45) and (v.b>10);
835EXPLAIN
836{
837  "query_block": {
838    "select_id": 1,
839    "table": {
840      "table_name": "t",
841      "access_type": "ALL",
842      "rows": 9,
843      "filtered": 100,
844      "attached_condition": "t.a is not null"
845    },
846    "table": {
847      "table_name": "<derived2>",
848      "access_type": "ref",
849      "possible_keys": ["key0"],
850      "key": "key0",
851      "key_length": "5",
852      "used_key_parts": ["a"],
853      "ref": ["test.t.a"],
854      "rows": 2,
855      "filtered": 100,
856      "attached_condition": "v.avg_a > 0.45 and v.b > 10",
857      "materialized": {
858        "query_block": {
859          "select_id": 2,
860          "having_condition": "avg_a < 22.333 and avg_a > 0.45",
861          "filesort": {
862            "sort_key": "t1_double.b, t1_double.c",
863            "temporary_table": {
864              "table": {
865                "table_name": "t1_double",
866                "access_type": "ALL",
867                "rows": 9,
868                "filtered": 100,
869                "attached_condition": "t1_double.b > 12.2 and t1_double.b > 10"
870              }
871            }
872          }
873        }
874      }
875    }
876  }
877}
878set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v_decimal as v,t2_decimal as t where
879(v.a=t.a) and (v.avg_c>15) and (v.b>1);
880a	b	avg_c	a	b	c
8813.0	3.0	16.0000	3.0	3.0	16
882select * from v_decimal as v,t2_decimal as t where
883(v.a=t.a) and (v.avg_c>15) and (v.b>1);
884a	b	avg_c	a	b	c
8853.0	3.0	16.0000	3.0	3.0	16
886explain select * from v_decimal as v,t2_decimal as t where
887(v.a=t.a) and (v.avg_c>15) and (v.b>1);
888id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
8891	PRIMARY	t	ALL	NULL	NULL	NULL	NULL	9	Using where
8901	PRIMARY	<derived2>	ref	key0	key0	3	test.t.a	2	Using where
8912	DERIVED	t1_decimal	ALL	NULL	NULL	NULL	NULL	9	Using where; Using temporary; Using filesort
892explain format=json select * from v_decimal as v,t2_decimal as t where
893(v.a=t.a) and (v.avg_c>15) and (v.b>1);
894EXPLAIN
895{
896  "query_block": {
897    "select_id": 1,
898    "table": {
899      "table_name": "t",
900      "access_type": "ALL",
901      "rows": 9,
902      "filtered": 100,
903      "attached_condition": "t.a is not null"
904    },
905    "table": {
906      "table_name": "<derived2>",
907      "access_type": "ref",
908      "possible_keys": ["key0"],
909      "key": "key0",
910      "key_length": "3",
911      "used_key_parts": ["a"],
912      "ref": ["test.t.a"],
913      "rows": 2,
914      "filtered": 100,
915      "attached_condition": "v.avg_c > 15 and v.b > 1",
916      "materialized": {
917        "query_block": {
918          "select_id": 2,
919          "having_condition": "avg_c > 12 and avg_c > 15",
920          "filesort": {
921            "sort_key": "t1_decimal.a, t1_decimal.b",
922            "temporary_table": {
923              "table": {
924                "table_name": "t1_decimal",
925                "access_type": "ALL",
926                "rows": 9,
927                "filtered": 100,
928                "attached_condition": "t1_decimal.b > 1"
929              }
930            }
931          }
932        }
933      }
934    }
935  }
936}
937# extracted or formula : pushing into HAVING and WHERE
938set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where
939((v1.a>7) and (v1.max_c>300) and (t2.c<v1.max_c)) or
940((v1.a<4) and (v1.max_c<500) and (t2.b<v1.b));
941a	b	max_c	avg_c	a	b	c	d
9421	19	107	107.0000	2	3	207	207
9431	19	107	107.0000	7	13	312	406
9441	19	107	107.0000	3	12	231	190
9458	33	404	213.6667	2	3	207	207
9468	33	404	213.6667	7	13	312	406
9478	33	404	213.6667	8	64	248	107
9488	33	404	213.6667	6	20	315	279
9498	33	404	213.6667	1	19	203	107
9508	33	404	213.6667	3	12	231	190
9518	33	404	213.6667	6	23	303	909
952select * from v1,t2 where
953((v1.a>7) and (v1.max_c>300) and (t2.c<v1.max_c)) or
954((v1.a<4) and (v1.max_c<500) and (t2.b<v1.b));
955a	b	max_c	avg_c	a	b	c	d
9561	19	107	107.0000	2	3	207	207
9571	19	107	107.0000	7	13	312	406
9581	19	107	107.0000	3	12	231	190
9598	33	404	213.6667	2	3	207	207
9608	33	404	213.6667	7	13	312	406
9618	33	404	213.6667	8	64	248	107
9628	33	404	213.6667	6	20	315	279
9638	33	404	213.6667	1	19	203	107
9648	33	404	213.6667	3	12	231	190
9658	33	404	213.6667	6	23	303	909
966explain select * from v1,t2 where
967((v1.a>7) and (v1.max_c>300) and (t2.c<v1.max_c)) or
968((v1.a<4) and (v1.max_c<500) and (t2.b<v1.b));
969id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
9701	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9
9711	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
9722	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
973explain format=json select * from v1,t2 where
974((v1.a>7) and (v1.max_c>300) and (t2.c<v1.max_c)) or
975((v1.a<4) and (v1.max_c<500) and (t2.b<v1.b));
976EXPLAIN
977{
978  "query_block": {
979    "select_id": 1,
980    "table": {
981      "table_name": "t2",
982      "access_type": "ALL",
983      "rows": 9,
984      "filtered": 100
985    },
986    "block-nl-join": {
987      "table": {
988        "table_name": "<derived2>",
989        "access_type": "ALL",
990        "rows": 20,
991        "filtered": 100,
992        "attached_condition": "v1.a > 7 and v1.max_c > 300 or v1.a < 4 and v1.max_c < 500"
993      },
994      "buffer_type": "flat",
995      "buffer_size": "238",
996      "join_type": "BNL",
997      "attached_condition": "v1.a > 7 and v1.max_c > 300 and t2.c < v1.max_c or v1.a < 4 and v1.max_c < 500 and t2.b < v1.b",
998      "materialized": {
999        "query_block": {
1000          "select_id": 2,
1001          "having_condition": "max_c < 707 and (t1.a > 7 and max_c > 300 or t1.a < 4 and max_c < 500)",
1002          "filesort": {
1003            "sort_key": "t1.a, t1.b",
1004            "temporary_table": {
1005              "table": {
1006                "table_name": "t1",
1007                "access_type": "ALL",
1008                "rows": 20,
1009                "filtered": 100,
1010                "attached_condition": "t1.a > 7 or t1.a < 4"
1011              }
1012            }
1013          }
1014        }
1015      }
1016    }
1017  }
1018}
1019set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where ((v1.a<2) and (v1.max_c>120)) or (v1.a>7);
1020a	b	max_c	avg_c	a	b	c	d
10211	21	500	234.6000	2	3	207	207
10221	21	500	234.6000	1	21	909	12
10231	21	500	234.6000	7	13	312	406
10241	21	500	234.6000	8	64	248	107
10251	21	500	234.6000	6	20	315	279
10261	21	500	234.6000	1	19	203	107
10271	21	500	234.6000	8	80	800	314
10281	21	500	234.6000	3	12	231	190
10291	21	500	234.6000	6	23	303	909
10308	33	404	213.6667	2	3	207	207
10318	33	404	213.6667	1	21	909	12
10328	33	404	213.6667	7	13	312	406
10338	33	404	213.6667	8	64	248	107
10348	33	404	213.6667	6	20	315	279
10358	33	404	213.6667	1	19	203	107
10368	33	404	213.6667	8	80	800	314
10378	33	404	213.6667	3	12	231	190
10388	33	404	213.6667	6	23	303	909
1039select * from v1,t2 where ((v1.a<2) and (v1.max_c>120)) or (v1.a>7);
1040a	b	max_c	avg_c	a	b	c	d
10411	21	500	234.6000	2	3	207	207
10421	21	500	234.6000	1	21	909	12
10431	21	500	234.6000	7	13	312	406
10441	21	500	234.6000	8	64	248	107
10451	21	500	234.6000	6	20	315	279
10461	21	500	234.6000	1	19	203	107
10471	21	500	234.6000	8	80	800	314
10481	21	500	234.6000	3	12	231	190
10491	21	500	234.6000	6	23	303	909
10508	33	404	213.6667	2	3	207	207
10518	33	404	213.6667	1	21	909	12
10528	33	404	213.6667	7	13	312	406
10538	33	404	213.6667	8	64	248	107
10548	33	404	213.6667	6	20	315	279
10558	33	404	213.6667	1	19	203	107
10568	33	404	213.6667	8	80	800	314
10578	33	404	213.6667	3	12	231	190
10588	33	404	213.6667	6	23	303	909
1059explain select * from v1,t2 where ((v1.a<2) and (v1.max_c>120)) or (v1.a>7);
1060id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
10611	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9
10621	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
10632	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
1064explain format=json select * from v1,t2 where ((v1.a<2) and (v1.max_c>120)) or (v1.a>7);
1065EXPLAIN
1066{
1067  "query_block": {
1068    "select_id": 1,
1069    "table": {
1070      "table_name": "t2",
1071      "access_type": "ALL",
1072      "rows": 9,
1073      "filtered": 100
1074    },
1075    "block-nl-join": {
1076      "table": {
1077        "table_name": "<derived2>",
1078        "access_type": "ALL",
1079        "rows": 20,
1080        "filtered": 100,
1081        "attached_condition": "v1.a < 2 and v1.max_c > 120 or v1.a > 7"
1082      },
1083      "buffer_type": "flat",
1084      "buffer_size": "238",
1085      "join_type": "BNL",
1086      "attached_condition": "v1.a < 2 and v1.max_c > 120 or v1.a > 7",
1087      "materialized": {
1088        "query_block": {
1089          "select_id": 2,
1090          "having_condition": "max_c < 707 and (t1.a < 2 and max_c > 120 or t1.a > 7)",
1091          "filesort": {
1092            "sort_key": "t1.a, t1.b",
1093            "temporary_table": {
1094              "table": {
1095                "table_name": "t1",
1096                "access_type": "ALL",
1097                "rows": 20,
1098                "filtered": 100,
1099                "attached_condition": "t1.a < 2 or t1.a > 7"
1100              }
1101            }
1102          }
1103        }
1104      }
1105    }
1106  }
1107}
1108# extracted or formulas : pushing into WHERE and HAVING
1109set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where
1110((v1.a<2) and (v1.max_c>120) and (v1.b=t2.b)) or (v1.a>7);
1111a	b	max_c	avg_c	a	b	c	d
11121	21	500	234.6000	1	21	909	12
11138	33	404	213.6667	2	3	207	207
11148	33	404	213.6667	1	21	909	12
11158	33	404	213.6667	7	13	312	406
11168	33	404	213.6667	8	64	248	107
11178	33	404	213.6667	6	20	315	279
11188	33	404	213.6667	1	19	203	107
11198	33	404	213.6667	8	80	800	314
11208	33	404	213.6667	3	12	231	190
11218	33	404	213.6667	6	23	303	909
1122select * from v1,t2 where
1123((v1.a<2) and (v1.max_c>120) and (v1.b=t2.b)) or (v1.a>7);
1124a	b	max_c	avg_c	a	b	c	d
11251	21	500	234.6000	1	21	909	12
11268	33	404	213.6667	2	3	207	207
11278	33	404	213.6667	1	21	909	12
11288	33	404	213.6667	7	13	312	406
11298	33	404	213.6667	8	64	248	107
11308	33	404	213.6667	6	20	315	279
11318	33	404	213.6667	1	19	203	107
11328	33	404	213.6667	8	80	800	314
11338	33	404	213.6667	3	12	231	190
11348	33	404	213.6667	6	23	303	909
1135explain select * from v1,t2 where
1136((v1.a<2) and (v1.max_c>120) and (v1.b=t2.b)) or (v1.a>7);
1137id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
11381	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9
11391	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
11402	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
1141explain format=json select * from v1,t2 where
1142((v1.a<2) and (v1.max_c>120) and (v1.b=t2.b)) or (v1.a>7);
1143EXPLAIN
1144{
1145  "query_block": {
1146    "select_id": 1,
1147    "table": {
1148      "table_name": "t2",
1149      "access_type": "ALL",
1150      "rows": 9,
1151      "filtered": 100
1152    },
1153    "block-nl-join": {
1154      "table": {
1155        "table_name": "<derived2>",
1156        "access_type": "ALL",
1157        "rows": 20,
1158        "filtered": 100,
1159        "attached_condition": "v1.a < 2 and v1.max_c > 120 or v1.a > 7"
1160      },
1161      "buffer_type": "flat",
1162      "buffer_size": "238",
1163      "join_type": "BNL",
1164      "attached_condition": "v1.b = t2.b and v1.a < 2 and v1.max_c > 120 or v1.a > 7",
1165      "materialized": {
1166        "query_block": {
1167          "select_id": 2,
1168          "having_condition": "max_c < 707 and (t1.a < 2 and max_c > 120 or t1.a > 7)",
1169          "filesort": {
1170            "sort_key": "t1.a, t1.b",
1171            "temporary_table": {
1172              "table": {
1173                "table_name": "t1",
1174                "access_type": "ALL",
1175                "rows": 20,
1176                "filtered": 100,
1177                "attached_condition": "t1.a < 2 or t1.a > 7"
1178              }
1179            }
1180          }
1181        }
1182      }
1183    }
1184  }
1185}
1186set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where
1187((v1.a<2) and (v1.max_c<200) and (t2.c>v1.max_c) and (v1.max_c=t2.d)) or
1188((v1.a>4) and (v1.max_c<500) and (t2.b<v1.b) and (v1.max_c=t2.c));
1189a	b	max_c	avg_c	a	b	c	d
11901	19	107	107.0000	8	64	248	107
11911	19	107	107.0000	1	19	203	107
11925	16	207	207.0000	2	3	207	207
1193select * from v1,t2 where
1194((v1.a<2) and (v1.max_c<200) and (t2.c>v1.max_c) and (v1.max_c=t2.d)) or
1195((v1.a>4) and (v1.max_c<500) and (t2.b<v1.b) and (v1.max_c=t2.c));
1196a	b	max_c	avg_c	a	b	c	d
11971	19	107	107.0000	8	64	248	107
11981	19	107	107.0000	1	19	203	107
11995	16	207	207.0000	2	3	207	207
1200explain select * from v1,t2 where
1201((v1.a<2) and (v1.max_c<200) and (t2.c>v1.max_c) and (v1.max_c=t2.d)) or
1202((v1.a>4) and (v1.max_c<500) and (t2.b<v1.b) and (v1.max_c=t2.c));
1203id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
12041	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9
12051	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
12062	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
1207explain format=json select * from v1,t2 where
1208((v1.a<2) and (v1.max_c<200) and (t2.c>v1.max_c) and (v1.max_c=t2.d)) or
1209((v1.a>4) and (v1.max_c<500) and (t2.b<v1.b) and (v1.max_c=t2.c));
1210EXPLAIN
1211{
1212  "query_block": {
1213    "select_id": 1,
1214    "table": {
1215      "table_name": "t2",
1216      "access_type": "ALL",
1217      "rows": 9,
1218      "filtered": 100
1219    },
1220    "block-nl-join": {
1221      "table": {
1222        "table_name": "<derived2>",
1223        "access_type": "ALL",
1224        "rows": 20,
1225        "filtered": 100,
1226        "attached_condition": "v1.a < 2 and v1.max_c < 200 or v1.a > 4"
1227      },
1228      "buffer_type": "flat",
1229      "buffer_size": "238",
1230      "join_type": "BNL",
1231      "attached_condition": "v1.a < 2 and v1.max_c < 200 and t2.c > v1.max_c and v1.max_c = t2.d or v1.max_c = t2.c and v1.a > 4 and t2.c < 500 and t2.b < v1.b",
1232      "materialized": {
1233        "query_block": {
1234          "select_id": 2,
1235          "having_condition": "max_c < 707 and (t1.a < 2 and max_c < 200 or t1.a > 4 and max_c < 500)",
1236          "filesort": {
1237            "sort_key": "t1.a, t1.b",
1238            "temporary_table": {
1239              "table": {
1240                "table_name": "t1",
1241                "access_type": "ALL",
1242                "rows": 20,
1243                "filtered": 100,
1244                "attached_condition": "t1.a < 2 or t1.a > 4"
1245              }
1246            }
1247          }
1248        }
1249      }
1250    }
1251  }
1252}
1253# prepare of a query containing extracted or formula
1254prepare stmt from "select * from v1,t2 where
1255  ((v1.max_c>400) and (t2.a>v1.a)) or ((v1.max_c<135) and (t2.a<v1.a));";
1256execute stmt;
1257a	b	max_c	avg_c	a	b	c	d
12581	21	500	234.6000	2	3	207	207
12591	21	500	234.6000	7	13	312	406
12601	21	500	234.6000	8	64	248	107
12611	21	500	234.6000	6	20	315	279
12621	21	500	234.6000	8	80	800	314
12631	21	500	234.6000	3	12	231	190
12641	21	500	234.6000	6	23	303	909
12655	27	132	132.0000	2	3	207	207
12665	27	132	132.0000	1	21	909	12
12675	27	132	132.0000	1	19	203	107
12685	27	132	132.0000	3	12	231	190
1269execute stmt;
1270a	b	max_c	avg_c	a	b	c	d
12711	21	500	234.6000	2	3	207	207
12721	21	500	234.6000	7	13	312	406
12731	21	500	234.6000	8	64	248	107
12741	21	500	234.6000	6	20	315	279
12751	21	500	234.6000	8	80	800	314
12761	21	500	234.6000	3	12	231	190
12771	21	500	234.6000	6	23	303	909
12785	27	132	132.0000	2	3	207	207
12795	27	132	132.0000	1	21	909	12
12805	27	132	132.0000	1	19	203	107
12815	27	132	132.0000	3	12	231	190
1282deallocate prepare stmt;
1283prepare stmt from
1284"explain format=json select * from v1,t2 where
1285    ((v1.max_c>400) and (t2.a>v1.a)) or ((v1.max_c<135) and (t2.a<v1.a));";
1286execute stmt;
1287EXPLAIN
1288{
1289  "query_block": {
1290    "select_id": 1,
1291    "table": {
1292      "table_name": "t2",
1293      "access_type": "ALL",
1294      "rows": 9,
1295      "filtered": 100
1296    },
1297    "block-nl-join": {
1298      "table": {
1299        "table_name": "<derived2>",
1300        "access_type": "ALL",
1301        "rows": 20,
1302        "filtered": 100,
1303        "attached_condition": "v1.max_c > 400 or v1.max_c < 135"
1304      },
1305      "buffer_type": "flat",
1306      "buffer_size": "238",
1307      "join_type": "BNL",
1308      "attached_condition": "v1.max_c > 400 and t2.a > v1.a or v1.max_c < 135 and t2.a < v1.a",
1309      "materialized": {
1310        "query_block": {
1311          "select_id": 2,
1312          "having_condition": "max_c < 707 and (max_c > 400 or max_c < 135)",
1313          "filesort": {
1314            "sort_key": "t1.a, t1.b",
1315            "temporary_table": {
1316              "table": {
1317                "table_name": "t1",
1318                "access_type": "ALL",
1319                "rows": 20,
1320                "filtered": 100
1321              }
1322            }
1323          }
1324        }
1325      }
1326    }
1327  }
1328}
1329execute stmt;
1330EXPLAIN
1331{
1332  "query_block": {
1333    "select_id": 1,
1334    "table": {
1335      "table_name": "t2",
1336      "access_type": "ALL",
1337      "rows": 9,
1338      "filtered": 100
1339    },
1340    "block-nl-join": {
1341      "table": {
1342        "table_name": "<derived2>",
1343        "access_type": "ALL",
1344        "rows": 20,
1345        "filtered": 100,
1346        "attached_condition": "v1.max_c > 400 or v1.max_c < 135"
1347      },
1348      "buffer_type": "flat",
1349      "buffer_size": "238",
1350      "join_type": "BNL",
1351      "attached_condition": "v1.max_c > 400 and t2.a > v1.a or v1.max_c < 135 and t2.a < v1.a",
1352      "materialized": {
1353        "query_block": {
1354          "select_id": 2,
1355          "having_condition": "max_c < 707 and (max_c > 400 or max_c < 135)",
1356          "filesort": {
1357            "sort_key": "t1.a, t1.b",
1358            "temporary_table": {
1359              "table": {
1360                "table_name": "t1",
1361                "access_type": "ALL",
1362                "rows": 20,
1363                "filtered": 100
1364              }
1365            }
1366          }
1367        }
1368      }
1369    }
1370  }
1371}
1372deallocate prepare stmt;
1373# conjunctive subformula : pushing into WHERE
1374# pushing equalities
1375set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (t2.a=v1.a) and (v1.b=t2.b) and (v1.a=1);
1376a	b	max_c	avg_c	a	b	c	d
13771	21	500	234.6000	1	21	909	12
13781	19	107	107.0000	1	19	203	107
1379select * from v1,t2 where (t2.a=v1.a) and (v1.b=t2.b) and (v1.a=1);
1380a	b	max_c	avg_c	a	b	c	d
13811	21	500	234.6000	1	21	909	12
13821	19	107	107.0000	1	19	203	107
1383explain select * from v1,t2 where (t2.a=v1.a) and (v1.b=t2.b) and (v1.a=1);
1384id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
13851	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
13861	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.b	2	Using where
13872	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
1388explain format=json select * from v1,t2 where (t2.a=v1.a) and (v1.b=t2.b) and (v1.a=1);
1389EXPLAIN
1390{
1391  "query_block": {
1392    "select_id": 1,
1393    "table": {
1394      "table_name": "t2",
1395      "access_type": "ALL",
1396      "rows": 9,
1397      "filtered": 100,
1398      "attached_condition": "t2.a = 1 and t2.b is not null"
1399    },
1400    "table": {
1401      "table_name": "<derived2>",
1402      "access_type": "ref",
1403      "possible_keys": ["key0"],
1404      "key": "key0",
1405      "key_length": "5",
1406      "used_key_parts": ["b"],
1407      "ref": ["test.t2.b"],
1408      "rows": 2,
1409      "filtered": 100,
1410      "attached_condition": "v1.a = 1",
1411      "materialized": {
1412        "query_block": {
1413          "select_id": 2,
1414          "having_condition": "max_c < 707",
1415          "filesort": {
1416            "sort_key": "t1.b",
1417            "temporary_table": {
1418              "table": {
1419                "table_name": "t1",
1420                "access_type": "ALL",
1421                "rows": 20,
1422                "filtered": 100,
1423                "attached_condition": "t1.a = 1"
1424              }
1425            }
1426          }
1427        }
1428      }
1429    }
1430  }
1431}
1432set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.a=5) and (v1.max_c=t2.d);
1433a	b	max_c	avg_c	a	b	c	d
14345	16	207	207.0000	2	3	207	207
1435select * from v1,t2 where (v1.a=5) and (v1.max_c=t2.d);
1436a	b	max_c	avg_c	a	b	c	d
14375	16	207	207.0000	2	3	207	207
1438explain select * from v1,t2 where (v1.a=5) and (v1.max_c=t2.d);
1439id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
14401	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
14411	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.d	2	Using where
14422	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
1443explain format=json select * from v1,t2 where (v1.a=5) and (v1.max_c=t2.d);
1444EXPLAIN
1445{
1446  "query_block": {
1447    "select_id": 1,
1448    "table": {
1449      "table_name": "t2",
1450      "access_type": "ALL",
1451      "rows": 9,
1452      "filtered": 100,
1453      "attached_condition": "t2.d is not null"
1454    },
1455    "table": {
1456      "table_name": "<derived2>",
1457      "access_type": "ref",
1458      "possible_keys": ["key0"],
1459      "key": "key0",
1460      "key_length": "5",
1461      "used_key_parts": ["max_c"],
1462      "ref": ["test.t2.d"],
1463      "rows": 2,
1464      "filtered": 100,
1465      "attached_condition": "v1.a = 5 and v1.max_c = t2.d",
1466      "materialized": {
1467        "query_block": {
1468          "select_id": 2,
1469          "having_condition": "max_c < 707",
1470          "filesort": {
1471            "sort_key": "t1.b",
1472            "temporary_table": {
1473              "table": {
1474                "table_name": "t1",
1475                "access_type": "ALL",
1476                "rows": 20,
1477                "filtered": 100,
1478                "attached_condition": "t1.a = 5"
1479              }
1480            }
1481          }
1482        }
1483      }
1484    }
1485  }
1486}
1487# conjunctive subformula : pushing into WHERE using equalities
1488set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (t2.a<5) and (v1.a=t2.a);
1489a	b	max_c	avg_c	a	b	c	d
14901	21	500	234.6000	1	21	909	12
14911	19	107	107.0000	1	21	909	12
14921	21	500	234.6000	1	19	203	107
14931	19	107	107.0000	1	19	203	107
1494select * from v1,t2 where (t2.a<5) and (v1.a=t2.a);
1495a	b	max_c	avg_c	a	b	c	d
14961	21	500	234.6000	1	21	909	12
14971	19	107	107.0000	1	21	909	12
14981	21	500	234.6000	1	19	203	107
14991	19	107	107.0000	1	19	203	107
1500explain select * from v1,t2 where (t2.a<5) and (v1.a=t2.a);
1501id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
15021	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
15031	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.a	2
15042	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
1505explain format=json select * from v1,t2 where (t2.a<5) and (v1.a=t2.a);
1506EXPLAIN
1507{
1508  "query_block": {
1509    "select_id": 1,
1510    "table": {
1511      "table_name": "t2",
1512      "access_type": "ALL",
1513      "rows": 9,
1514      "filtered": 100,
1515      "attached_condition": "t2.a < 5 and t2.a is not null"
1516    },
1517    "table": {
1518      "table_name": "<derived2>",
1519      "access_type": "ref",
1520      "possible_keys": ["key0"],
1521      "key": "key0",
1522      "key_length": "5",
1523      "used_key_parts": ["a"],
1524      "ref": ["test.t2.a"],
1525      "rows": 2,
1526      "filtered": 100,
1527      "materialized": {
1528        "query_block": {
1529          "select_id": 2,
1530          "having_condition": "max_c < 707",
1531          "filesort": {
1532            "sort_key": "t1.a, t1.b",
1533            "temporary_table": {
1534              "table": {
1535                "table_name": "t1",
1536                "access_type": "ALL",
1537                "rows": 20,
1538                "filtered": 100,
1539                "attached_condition": "t1.a < 5"
1540              }
1541            }
1542          }
1543        }
1544      }
1545    }
1546  }
1547}
1548set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.a=v1.b) and (v1.a=t2.a);
1549a	b	max_c	avg_c	a	b	c	d
1550select * from v1,t2 where (v1.a=v1.b) and (v1.a=t2.a);
1551a	b	max_c	avg_c	a	b	c	d
1552explain select * from v1,t2 where (v1.a=v1.b) and (v1.a=t2.a);
1553id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
15541	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
15551	PRIMARY	<derived2>	ref	key0	key0	10	test.t2.a,test.t2.a	2
15562	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
1557explain format=json select * from v1,t2 where (v1.a=v1.b) and (v1.a=t2.a);
1558EXPLAIN
1559{
1560  "query_block": {
1561    "select_id": 1,
1562    "table": {
1563      "table_name": "t2",
1564      "access_type": "ALL",
1565      "rows": 9,
1566      "filtered": 100,
1567      "attached_condition": "t2.a is not null and t2.a is not null"
1568    },
1569    "table": {
1570      "table_name": "<derived2>",
1571      "access_type": "ref",
1572      "possible_keys": ["key0"],
1573      "key": "key0",
1574      "key_length": "10",
1575      "used_key_parts": ["a", "b"],
1576      "ref": ["test.t2.a", "test.t2.a"],
1577      "rows": 2,
1578      "filtered": 100,
1579      "materialized": {
1580        "query_block": {
1581          "select_id": 2,
1582          "having_condition": "max_c < 707",
1583          "filesort": {
1584            "sort_key": "t1.a, t1.b",
1585            "temporary_table": {
1586              "table": {
1587                "table_name": "t1",
1588                "access_type": "ALL",
1589                "rows": 20,
1590                "filtered": 100,
1591                "attached_condition": "t1.b = t1.a"
1592              }
1593            }
1594          }
1595        }
1596      }
1597    }
1598  }
1599}
1600# conjunctive subformula : pushing into HAVING using equalities
1601set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (t2.c>150) and (v1.max_c=t2.c);
1602a	b	max_c	avg_c	a	b	c	d
16035	16	207	207.0000	2	3	207	207
16046	20	315	279.3333	6	20	315	279
1605select * from v1,t2 where (t2.c>150) and (v1.max_c=t2.c);
1606a	b	max_c	avg_c	a	b	c	d
16075	16	207	207.0000	2	3	207	207
16086	20	315	279.3333	6	20	315	279
1609explain select * from v1,t2 where (t2.c>150) and (v1.max_c=t2.c);
1610id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
16111	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
16121	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.c	2
16132	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using temporary; Using filesort
1614explain format=json select * from v1,t2 where (t2.c>150) and (v1.max_c=t2.c);
1615EXPLAIN
1616{
1617  "query_block": {
1618    "select_id": 1,
1619    "table": {
1620      "table_name": "t2",
1621      "access_type": "ALL",
1622      "rows": 9,
1623      "filtered": 100,
1624      "attached_condition": "t2.c > 150 and t2.c is not null"
1625    },
1626    "table": {
1627      "table_name": "<derived2>",
1628      "access_type": "ref",
1629      "possible_keys": ["key0"],
1630      "key": "key0",
1631      "key_length": "5",
1632      "used_key_parts": ["max_c"],
1633      "ref": ["test.t2.c"],
1634      "rows": 2,
1635      "filtered": 100,
1636      "materialized": {
1637        "query_block": {
1638          "select_id": 2,
1639          "having_condition": "max_c < 707 and max_c > 150",
1640          "filesort": {
1641            "sort_key": "t1.a, t1.b",
1642            "temporary_table": {
1643              "table": {
1644                "table_name": "t1",
1645                "access_type": "ALL",
1646                "rows": 20,
1647                "filtered": 100
1648              }
1649            }
1650          }
1651        }
1652      }
1653    }
1654  }
1655}
1656# extracted and formula : pushing into WHERE
1657# pushing equalities
1658set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.a=v1.b) and (v1.a=t2.a) and (v1.a=3);
1659a	b	max_c	avg_c	a	b	c	d
1660select * from v1,t2 where (v1.a=v1.b) and (v1.a=t2.a) and (v1.a=3);
1661a	b	max_c	avg_c	a	b	c	d
1662explain select * from v1,t2 where (v1.a=v1.b) and (v1.a=t2.a) and (v1.a=3);
1663id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
16641	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
16651	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
16662	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where
1667explain format=json select * from v1,t2 where (v1.a=v1.b) and (v1.a=t2.a) and (v1.a=3);
1668EXPLAIN
1669{
1670  "query_block": {
1671    "select_id": 1,
1672    "table": {
1673      "table_name": "t2",
1674      "access_type": "ALL",
1675      "rows": 9,
1676      "filtered": 100,
1677      "attached_condition": "t2.a = 3"
1678    },
1679    "block-nl-join": {
1680      "table": {
1681        "table_name": "<derived2>",
1682        "access_type": "ALL",
1683        "rows": 20,
1684        "filtered": 100,
1685        "attached_condition": "v1.a = 3 and v1.b = 3"
1686      },
1687      "buffer_type": "flat",
1688      "buffer_size": "238",
1689      "join_type": "BNL",
1690      "materialized": {
1691        "query_block": {
1692          "select_id": 2,
1693          "having_condition": "max_c < 707",
1694          "table": {
1695            "table_name": "t1",
1696            "access_type": "ALL",
1697            "rows": 20,
1698            "filtered": 100,
1699            "attached_condition": "t1.a = 3 and t1.b = 3"
1700          }
1701        }
1702      }
1703    }
1704  }
1705}
1706set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.a=1) and (v1.b=21) and (t2.a=2);
1707a	b	max_c	avg_c	a	b	c	d
17081	21	500	234.6000	2	3	207	207
1709select * from v1,t2 where (v1.a=1) and (v1.b=21) and (t2.a=2);
1710a	b	max_c	avg_c	a	b	c	d
17111	21	500	234.6000	2	3	207	207
1712explain select * from v1,t2 where (v1.a=1) and (v1.b=21) and (t2.a=2);
1713id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
17141	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
17151	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
17162	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where
1717explain format=json select * from v1,t2 where (v1.a=1) and (v1.b=21) and (t2.a=2);
1718EXPLAIN
1719{
1720  "query_block": {
1721    "select_id": 1,
1722    "table": {
1723      "table_name": "t2",
1724      "access_type": "ALL",
1725      "rows": 9,
1726      "filtered": 100,
1727      "attached_condition": "t2.a = 2"
1728    },
1729    "block-nl-join": {
1730      "table": {
1731        "table_name": "<derived2>",
1732        "access_type": "ALL",
1733        "rows": 20,
1734        "filtered": 100,
1735        "attached_condition": "v1.a = 1 and v1.b = 21"
1736      },
1737      "buffer_type": "flat",
1738      "buffer_size": "238",
1739      "join_type": "BNL",
1740      "materialized": {
1741        "query_block": {
1742          "select_id": 2,
1743          "having_condition": "max_c < 707",
1744          "table": {
1745            "table_name": "t1",
1746            "access_type": "ALL",
1747            "rows": 20,
1748            "filtered": 100,
1749            "attached_condition": "t1.a = 1 and t1.b = 21"
1750          }
1751        }
1752      }
1753    }
1754  }
1755}
1756set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v_char as v,t2_char as t where
1757(v.a='c') and (v.b<'Hermes') and ((v.b=t.b) or (v.max_c>20));
1758a	b	max_c	a	b	c
1759c	Harry	4	d	Harry	1
1760c	Harry	4	b	Harry	4
1761select * from v_char as v,t2_char as t where
1762(v.a='c') and (v.b<'Hermes') and ((v.b=t.b) or (v.max_c>20));
1763a	b	max_c	a	b	c
1764c	Harry	4	d	Harry	1
1765c	Harry	4	b	Harry	4
1766explain select * from v_char as v,t2_char as t where
1767(v.a='c') and (v.b<'Hermes') and ((v.b=t.b) or (v.max_c>20));
1768id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
17691	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	12	Using where
17701	PRIMARY	t	ALL	NULL	NULL	NULL	NULL	12	Using where; Using join buffer (flat, BNL join)
17712	DERIVED	t1_char	ALL	NULL	NULL	NULL	NULL	12	Using where; Using temporary; Using filesort
1772explain format=json select * from v_char as v,t2_char as t where
1773(v.a='c') and (v.b<'Hermes') and ((v.b=t.b) or (v.max_c>20));
1774EXPLAIN
1775{
1776  "query_block": {
1777    "select_id": 1,
1778    "table": {
1779      "table_name": "<derived2>",
1780      "access_type": "ALL",
1781      "rows": 12,
1782      "filtered": 100,
1783      "attached_condition": "v.a = 'c' and v.b < 'Hermes'",
1784      "materialized": {
1785        "query_block": {
1786          "select_id": 2,
1787          "having_condition": "max_c < 9",
1788          "filesort": {
1789            "sort_key": "t1_char.b",
1790            "temporary_table": {
1791              "table": {
1792                "table_name": "t1_char",
1793                "access_type": "ALL",
1794                "rows": 12,
1795                "filtered": 100,
1796                "attached_condition": "t1_char.a = 'c' and t1_char.b < 'Hermes'"
1797              }
1798            }
1799          }
1800        }
1801      }
1802    },
1803    "block-nl-join": {
1804      "table": {
1805        "table_name": "t",
1806        "access_type": "ALL",
1807        "rows": 12,
1808        "filtered": 100
1809      },
1810      "buffer_type": "flat",
1811      "buffer_size": "220",
1812      "join_type": "BNL",
1813      "attached_condition": "t.b = v.b or v.max_c > 20"
1814    }
1815  }
1816}
1817# extracted and formula : pushing into WHERE using equalities
1818# pushing equalities
1819set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v_decimal as v,t2_decimal as t where
1820(v.a=v.b) and (v.b=t.b) and ((t.b>1) or (v.a=1));
1821a	b	avg_c	a	b	c
18221.0	1.0	16.3333	2.0	1.0	13
18233.0	3.0	16.0000	3.0	3.0	16
18243.0	3.0	16.0000	1.0	3.0	22
18253.0	3.0	16.0000	1.0	3.0	14
18261.0	1.0	16.3333	2.0	1.0	43
18273.0	3.0	16.0000	2.0	3.0	11
18283.0	3.0	16.0000	2.0	3.0	16
1829select * from v_decimal as v,t2_decimal as t where
1830(v.a=v.b) and (v.b=t.b) and ((t.b>1) or (v.a=1));
1831a	b	avg_c	a	b	c
18321.0	1.0	16.3333	2.0	1.0	13
18333.0	3.0	16.0000	3.0	3.0	16
18343.0	3.0	16.0000	1.0	3.0	22
18353.0	3.0	16.0000	1.0	3.0	14
18361.0	1.0	16.3333	2.0	1.0	43
18373.0	3.0	16.0000	2.0	3.0	11
18383.0	3.0	16.0000	2.0	3.0	16
1839explain select * from v_decimal as v,t2_decimal as t where
1840(v.a=v.b) and (v.b=t.b) and ((t.b>1) or (v.a=1));
1841id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
18421	PRIMARY	t	ALL	NULL	NULL	NULL	NULL	9	Using where
18431	PRIMARY	<derived2>	ref	key0	key0	6	test.t.b,test.t.b	2
18442	DERIVED	t1_decimal	ALL	NULL	NULL	NULL	NULL	9	Using where; Using temporary; Using filesort
1845explain format=json select * from v_decimal as v,t2_decimal as t where
1846(v.a=v.b) and (v.b=t.b) and ((t.b>1) or (v.a=1));
1847EXPLAIN
1848{
1849  "query_block": {
1850    "select_id": 1,
1851    "table": {
1852      "table_name": "t",
1853      "access_type": "ALL",
1854      "rows": 9,
1855      "filtered": 100,
1856      "attached_condition": "(t.b > 1 or t.b = 1) and t.b is not null and t.b is not null"
1857    },
1858    "table": {
1859      "table_name": "<derived2>",
1860      "access_type": "ref",
1861      "possible_keys": ["key0"],
1862      "key": "key0",
1863      "key_length": "6",
1864      "used_key_parts": ["a", "b"],
1865      "ref": ["test.t.b", "test.t.b"],
1866      "rows": 2,
1867      "filtered": 100,
1868      "materialized": {
1869        "query_block": {
1870          "select_id": 2,
1871          "having_condition": "avg_c > 12",
1872          "filesort": {
1873            "sort_key": "t1_decimal.a, t1_decimal.b",
1874            "temporary_table": {
1875              "table": {
1876                "table_name": "t1_decimal",
1877                "access_type": "ALL",
1878                "rows": 9,
1879                "filtered": 100,
1880                "attached_condition": "t1_decimal.b = t1_decimal.a and (t1_decimal.a > 1 or t1_decimal.a = 1)"
1881              }
1882            }
1883          }
1884        }
1885      }
1886    }
1887  }
1888}
1889# extracted or formula : pushing into HAVING using equalities
1890set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2
1891where ((t2.a<4) and (v1.a=t2.a)) or ((t2.c>150) and (v1.max_c=t2.c));
1892a	b	max_c	avg_c	a	b	c	d
18931	19	107	107.0000	1	21	909	12
18941	19	107	107.0000	1	19	203	107
18951	21	500	234.6000	1	21	909	12
18961	21	500	234.6000	1	19	203	107
18975	16	207	207.0000	2	3	207	207
18986	20	315	279.3333	6	20	315	279
1899select * from v1,t2
1900where ((t2.a<4) and (v1.a=t2.a)) or ((t2.c>150) and (v1.max_c=t2.c));
1901a	b	max_c	avg_c	a	b	c	d
19021	19	107	107.0000	1	21	909	12
19031	19	107	107.0000	1	19	203	107
19041	21	500	234.6000	1	21	909	12
19051	21	500	234.6000	1	19	203	107
19065	16	207	207.0000	2	3	207	207
19076	20	315	279.3333	6	20	315	279
1908explain select * from v1,t2
1909where ((t2.a<4) and (v1.a=t2.a)) or ((t2.c>150) and (v1.max_c=t2.c));
1910id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
19111	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
19121	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
19132	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using temporary; Using filesort
1914explain format=json select * from v1,t2
1915where ((t2.a<4) and (v1.a=t2.a)) or ((t2.c>150) and (v1.max_c=t2.c));
1916EXPLAIN
1917{
1918  "query_block": {
1919    "select_id": 1,
1920    "table": {
1921      "table_name": "t2",
1922      "access_type": "ALL",
1923      "rows": 9,
1924      "filtered": 100,
1925      "attached_condition": "t2.a < 4 or t2.c > 150"
1926    },
1927    "block-nl-join": {
1928      "table": {
1929        "table_name": "<derived2>",
1930        "access_type": "ALL",
1931        "rows": 20,
1932        "filtered": 100
1933      },
1934      "buffer_type": "flat",
1935      "buffer_size": "238",
1936      "join_type": "BNL",
1937      "attached_condition": "v1.a = t2.a and t2.a < 4 or v1.max_c = t2.c and t2.c > 150",
1938      "materialized": {
1939        "query_block": {
1940          "select_id": 2,
1941          "having_condition": "max_c < 707 and (t1.a < 4 or max_c > 150)",
1942          "filesort": {
1943            "sort_key": "t1.a, t1.b",
1944            "temporary_table": {
1945              "table": {
1946                "table_name": "t1",
1947                "access_type": "ALL",
1948                "rows": 20,
1949                "filtered": 100
1950              }
1951            }
1952          }
1953        }
1954      }
1955    }
1956  }
1957}
1958# conjunctive subformulas : pushing into WHERE and HAVING using equalities
1959set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2
1960where ((t2.a>5) and (v1.a=t2.a)) and ((t2.c>250) and (v1.max_c=t2.c));
1961a	b	max_c	avg_c	a	b	c	d
19626	20	315	279.3333	6	20	315	279
1963select * from v1,t2
1964where ((t2.a>5) and (v1.a=t2.a)) and ((t2.c>250) and (v1.max_c=t2.c));
1965a	b	max_c	avg_c	a	b	c	d
19666	20	315	279.3333	6	20	315	279
1967explain select * from v1,t2
1968where ((t2.a>5) and (v1.a=t2.a)) and ((t2.c>250) and (v1.max_c=t2.c));
1969id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
19701	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
19711	PRIMARY	<derived2>	ref	key0	key0	10	test.t2.a,test.t2.c	2
19722	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
1973explain format=json select * from v1,t2
1974where ((t2.a>5) and (v1.a=t2.a)) and ((t2.c>250) and (v1.max_c=t2.c));
1975EXPLAIN
1976{
1977  "query_block": {
1978    "select_id": 1,
1979    "table": {
1980      "table_name": "t2",
1981      "access_type": "ALL",
1982      "rows": 9,
1983      "filtered": 100,
1984      "attached_condition": "t2.a > 5 and t2.c > 250 and t2.a is not null and t2.c is not null"
1985    },
1986    "table": {
1987      "table_name": "<derived2>",
1988      "access_type": "ref",
1989      "possible_keys": ["key0"],
1990      "key": "key0",
1991      "key_length": "10",
1992      "used_key_parts": ["a", "max_c"],
1993      "ref": ["test.t2.a", "test.t2.c"],
1994      "rows": 2,
1995      "filtered": 100,
1996      "materialized": {
1997        "query_block": {
1998          "select_id": 2,
1999          "having_condition": "max_c < 707 and max_c > 250",
2000          "filesort": {
2001            "sort_key": "t1.a, t1.b",
2002            "temporary_table": {
2003              "table": {
2004                "table_name": "t1",
2005                "access_type": "ALL",
2006                "rows": 20,
2007                "filtered": 100,
2008                "attached_condition": "t1.a > 5"
2009              }
2010            }
2011          }
2012        }
2013      }
2014    }
2015  }
2016}
2017# conjunctive subformulas : pushing into WHERE and HAVING
2018# pushing equalities
2019set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from
2020(select a, b, max(c) as max_c, avg(c) as avg_c from t1
2021group by a,b having max_c < 707) v1,
2022t2 where (v1.a=8) and (v1.a=t2.a) and (v1.max_c=404);
2023a	b	max_c	avg_c	a	b	c	d
20248	33	404	213.6667	8	64	248	107
20258	33	404	213.6667	8	80	800	314
2026select * from
2027(select a, b, max(c) as max_c, avg(c) as avg_c from t1
2028group by a,b having max_c < 707) v1,
2029t2 where (v1.a=8) and (v1.a=t2.a) and (v1.max_c=404);
2030a	b	max_c	avg_c	a	b	c	d
20318	33	404	213.6667	8	64	248	107
20328	33	404	213.6667	8	80	800	314
2033explain select * from
2034(select a, b, max(c) as max_c, avg(c) as avg_c from t1
2035group by a,b having max_c < 707) v1,
2036t2 where (v1.a=8) and (v1.a=t2.a) and (v1.max_c=404);
2037id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
20381	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
20391	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
20402	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
2041explain format=json select * from
2042(select a, b, max(c) as max_c, avg(c) as avg_c from t1
2043group by a,b having max_c < 707) v1,
2044t2 where (v1.a=8) and (v1.a=t2.a) and (v1.max_c=404);
2045EXPLAIN
2046{
2047  "query_block": {
2048    "select_id": 1,
2049    "table": {
2050      "table_name": "t2",
2051      "access_type": "ALL",
2052      "rows": 9,
2053      "filtered": 100,
2054      "attached_condition": "t2.a = 8"
2055    },
2056    "block-nl-join": {
2057      "table": {
2058        "table_name": "<derived2>",
2059        "access_type": "ALL",
2060        "rows": 20,
2061        "filtered": 100,
2062        "attached_condition": "v1.a = 8 and v1.max_c = 404"
2063      },
2064      "buffer_type": "flat",
2065      "buffer_size": "238",
2066      "join_type": "BNL",
2067      "materialized": {
2068        "query_block": {
2069          "select_id": 2,
2070          "having_condition": "max_c = 404",
2071          "filesort": {
2072            "sort_key": "t1.b",
2073            "temporary_table": {
2074              "table": {
2075                "table_name": "t1",
2076                "access_type": "ALL",
2077                "rows": 20,
2078                "filtered": 100,
2079                "attached_condition": "t1.a = 8"
2080              }
2081            }
2082          }
2083        }
2084      }
2085    }
2086  }
2087}
2088# conjunctive subformulas : pushing into WHERE and HAVING
2089set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where
2090(v1.a>3) and (v1.max_c>200) and (t2.b<v1.b) and (t2.d=v1.max_c);
2091a	b	max_c	avg_c	a	b	c	d
20925	16	207	207.0000	2	3	207	207
2093select * from v1,t2 where
2094(v1.a>3) and (v1.max_c>200) and (t2.b<v1.b) and (t2.d=v1.max_c);
2095a	b	max_c	avg_c	a	b	c	d
20965	16	207	207.0000	2	3	207	207
2097explain select * from v1,t2 where
2098(v1.a>3) and (v1.max_c>200) and (t2.b<v1.b) and (t2.d=v1.max_c);
2099id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
21001	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
21011	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.d	2	Using where
21022	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
2103explain format=json select * from v1,t2 where
2104(v1.a>3) and (v1.max_c>200) and (t2.b<v1.b) and (t2.d=v1.max_c);
2105EXPLAIN
2106{
2107  "query_block": {
2108    "select_id": 1,
2109    "table": {
2110      "table_name": "t2",
2111      "access_type": "ALL",
2112      "rows": 9,
2113      "filtered": 100,
2114      "attached_condition": "t2.d is not null"
2115    },
2116    "table": {
2117      "table_name": "<derived2>",
2118      "access_type": "ref",
2119      "possible_keys": ["key0"],
2120      "key": "key0",
2121      "key_length": "5",
2122      "used_key_parts": ["max_c"],
2123      "ref": ["test.t2.d"],
2124      "rows": 2,
2125      "filtered": 100,
2126      "attached_condition": "v1.a > 3 and v1.max_c > 200 and t2.b < v1.b and t2.d = v1.max_c",
2127      "materialized": {
2128        "query_block": {
2129          "select_id": 2,
2130          "having_condition": "max_c < 707 and max_c > 200",
2131          "filesort": {
2132            "sort_key": "t1.a, t1.b",
2133            "temporary_table": {
2134              "table": {
2135                "table_name": "t1",
2136                "access_type": "ALL",
2137                "rows": 20,
2138                "filtered": 100,
2139                "attached_condition": "t1.a > 3"
2140              }
2141            }
2142          }
2143        }
2144      }
2145    }
2146  }
2147}
2148# conjunctive subformula : pushing into WHERE
2149# extracted or formula : pushing into HAVING using equalities
2150# pushing equalities
2151set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v_double as v,t2_double as t where
2152(v.b=v.c) and (v.c=t.c) and ((t.c>10) or (v.a=1));
2153a	avg_a	b	c	a	b	c
2154select * from v_double as v,t2_double as t where
2155(v.b=v.c) and (v.c=t.c) and ((t.c>10) or (v.a=1));
2156a	avg_a	b	c	a	b	c
2157explain select * from v_double as v,t2_double as t where
2158(v.b=v.c) and (v.c=t.c) and ((t.c>10) or (v.a=1));
2159id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
21601	PRIMARY	t	ALL	NULL	NULL	NULL	NULL	9	Using where
21611	PRIMARY	<derived2>	ref	key0	key0	18	test.t.c,test.t.c	2	Using where
21622	DERIVED	t1_double	ALL	NULL	NULL	NULL	NULL	9	Using where; Using temporary; Using filesort
2163explain format=json select * from v_double as v,t2_double as t where
2164(v.b=v.c) and (v.c=t.c) and ((t.c>10) or (v.a=1));
2165EXPLAIN
2166{
2167  "query_block": {
2168    "select_id": 1,
2169    "table": {
2170      "table_name": "t",
2171      "access_type": "ALL",
2172      "rows": 9,
2173      "filtered": 100,
2174      "attached_condition": "t.c is not null and t.c is not null"
2175    },
2176    "table": {
2177      "table_name": "<derived2>",
2178      "access_type": "ref",
2179      "possible_keys": ["key0"],
2180      "key": "key0",
2181      "key_length": "18",
2182      "used_key_parts": ["b", "c"],
2183      "ref": ["test.t.c", "test.t.c"],
2184      "rows": 2,
2185      "filtered": 100,
2186      "attached_condition": "t.c > 10 or v.a = 1",
2187      "materialized": {
2188        "query_block": {
2189          "select_id": 2,
2190          "having_condition": "avg_a < 22.333 and (t1_double.b > 10 or t1_double.a = 1)",
2191          "filesort": {
2192            "sort_key": "t1_double.b, t1_double.c",
2193            "temporary_table": {
2194              "table": {
2195                "table_name": "t1_double",
2196                "access_type": "ALL",
2197                "rows": 9,
2198                "filtered": 100,
2199                "attached_condition": "t1_double.c = t1_double.b and t1_double.b > 12.2"
2200              }
2201            }
2202          }
2203        }
2204      }
2205    }
2206  }
2207}
2208# conjunctive subformula : pushing into WHERE
2209# extracted or formula : pushing into HAVING using equalities
2210set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v_double as v,t2_double as t where
2211(((v.a>0.2) or (v.b<17)) or (t.c>17)) and (t.c=v.c) and (v.c>18);
2212a	avg_a	b	c	a	b	c
22131	0.50000000	12.5	18.9	1	12.5	18.9
22141	0.50000000	12.5	18.9	2	22.4	18.9
22151	0.50000000	12.5	18.9	7	17.89	18.9
2216select * from v_double as v,t2_double as t where
2217(((v.a>0.2) or (v.b<17)) or (t.c>17)) and (t.c=v.c) and (v.c>18);
2218a	avg_a	b	c	a	b	c
22191	0.50000000	12.5	18.9	1	12.5	18.9
22201	0.50000000	12.5	18.9	2	22.4	18.9
22211	0.50000000	12.5	18.9	7	17.89	18.9
2222explain select * from v_double as v,t2_double as t where
2223(((v.a>0.2) or (v.b<17)) or (t.c>17)) and (t.c=v.c) and (v.c>18);
2224id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
22251	PRIMARY	t	ALL	NULL	NULL	NULL	NULL	9	Using where
22261	PRIMARY	<derived2>	ref	key0	key0	9	test.t.c	2	Using where
22272	DERIVED	t1_double	ALL	NULL	NULL	NULL	NULL	9	Using where; Using temporary; Using filesort
2228explain format=json select * from v_double as v,t2_double as t where
2229(((v.a>0.2) or (v.b<17)) or (t.c>17)) and (t.c=v.c) and (v.c>18);
2230EXPLAIN
2231{
2232  "query_block": {
2233    "select_id": 1,
2234    "table": {
2235      "table_name": "t",
2236      "access_type": "ALL",
2237      "rows": 9,
2238      "filtered": 100,
2239      "attached_condition": "t.c > 18 and t.c is not null"
2240    },
2241    "table": {
2242      "table_name": "<derived2>",
2243      "access_type": "ref",
2244      "possible_keys": ["key0"],
2245      "key": "key0",
2246      "key_length": "9",
2247      "used_key_parts": ["c"],
2248      "ref": ["test.t.c"],
2249      "rows": 2,
2250      "filtered": 100,
2251      "attached_condition": "v.a > 0.2 or v.b < 17 or t.c > 17",
2252      "materialized": {
2253        "query_block": {
2254          "select_id": 2,
2255          "having_condition": "avg_a < 22.333 and (t1_double.a > 0.2 or t1_double.b < 17 or t1_double.c > 17)",
2256          "filesort": {
2257            "sort_key": "t1_double.b, t1_double.c",
2258            "temporary_table": {
2259              "table": {
2260                "table_name": "t1_double",
2261                "access_type": "ALL",
2262                "rows": 9,
2263                "filtered": 100,
2264                "attached_condition": "t1_double.b > 12.2 and t1_double.c > 18"
2265              }
2266            }
2267          }
2268        }
2269      }
2270    }
2271  }
2272}
2273# extracted or formula : pushing into WHERE
2274# conjunctive subformula : pushing into HAVING
2275# pushing equalities
2276set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v_decimal as v,t2_decimal as t where
2277(((v.a>4) or (v.a=2)) or (v.b>3)) and (v.avg_c=13);
2278a	b	avg_c	a	b	c
22792.0	1.0	13.0000	2.0	1.0	13
22802.0	3.0	13.0000	2.0	1.0	13
22812.0	1.0	13.0000	2.0	2.0	11
22822.0	3.0	13.0000	2.0	2.0	11
22832.0	1.0	13.0000	3.0	3.0	16
22842.0	3.0	13.0000	3.0	3.0	16
22852.0	1.0	13.0000	1.0	3.0	22
22862.0	3.0	13.0000	1.0	3.0	22
22872.0	1.0	13.0000	1.0	3.0	14
22882.0	3.0	13.0000	1.0	3.0	14
22892.0	1.0	13.0000	2.0	2.0	15
22902.0	3.0	13.0000	2.0	2.0	15
22912.0	1.0	13.0000	2.0	1.0	43
22922.0	3.0	13.0000	2.0	1.0	43
22932.0	1.0	13.0000	2.0	3.0	11
22942.0	3.0	13.0000	2.0	3.0	11
22952.0	1.0	13.0000	2.0	3.0	16
22962.0	3.0	13.0000	2.0	3.0	16
2297select * from v_decimal as v,t2_decimal as t where
2298(((v.a>4) or (v.a=2)) or (v.b>3)) and (v.avg_c=13);
2299a	b	avg_c	a	b	c
23002.0	1.0	13.0000	2.0	1.0	13
23012.0	3.0	13.0000	2.0	1.0	13
23022.0	1.0	13.0000	2.0	2.0	11
23032.0	3.0	13.0000	2.0	2.0	11
23042.0	1.0	13.0000	3.0	3.0	16
23052.0	3.0	13.0000	3.0	3.0	16
23062.0	1.0	13.0000	1.0	3.0	22
23072.0	3.0	13.0000	1.0	3.0	22
23082.0	1.0	13.0000	1.0	3.0	14
23092.0	3.0	13.0000	1.0	3.0	14
23102.0	1.0	13.0000	2.0	2.0	15
23112.0	3.0	13.0000	2.0	2.0	15
23122.0	1.0	13.0000	2.0	1.0	43
23132.0	3.0	13.0000	2.0	1.0	43
23142.0	1.0	13.0000	2.0	3.0	11
23152.0	3.0	13.0000	2.0	3.0	11
23162.0	1.0	13.0000	2.0	3.0	16
23172.0	3.0	13.0000	2.0	3.0	16
2318explain select * from v_decimal as v,t2_decimal as t where
2319(((v.a>4) or (v.a=2)) or (v.b>3)) and (v.avg_c=13);
2320id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
23211	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	9	Using where
23221	PRIMARY	t	ALL	NULL	NULL	NULL	NULL	9	Using join buffer (flat, BNL join)
23232	DERIVED	t1_decimal	ALL	NULL	NULL	NULL	NULL	9	Using where; Using temporary; Using filesort
2324explain format=json select * from v_decimal as v,t2_decimal as t where
2325(((v.a>4) or (v.a=2)) or (v.b>3)) and (v.avg_c=13);
2326EXPLAIN
2327{
2328  "query_block": {
2329    "select_id": 1,
2330    "table": {
2331      "table_name": "<derived2>",
2332      "access_type": "ALL",
2333      "rows": 9,
2334      "filtered": 100,
2335      "attached_condition": "(v.a > 4 or v.a = 2 or v.b > 3) and v.avg_c = 13",
2336      "materialized": {
2337        "query_block": {
2338          "select_id": 2,
2339          "having_condition": "avg_c > 12 and avg_c = 13",
2340          "filesort": {
2341            "sort_key": "t1_decimal.a, t1_decimal.b",
2342            "temporary_table": {
2343              "table": {
2344                "table_name": "t1_decimal",
2345                "access_type": "ALL",
2346                "rows": 9,
2347                "filtered": 100,
2348                "attached_condition": "t1_decimal.a > 4 or t1_decimal.a = 2 or t1_decimal.b > 3"
2349              }
2350            }
2351          }
2352        }
2353      }
2354    },
2355    "block-nl-join": {
2356      "table": {
2357        "table_name": "t",
2358        "access_type": "ALL",
2359        "rows": 9,
2360        "filtered": 100
2361      },
2362      "buffer_type": "flat",
2363      "buffer_size": "162",
2364      "join_type": "BNL"
2365    }
2366  }
2367}
2368set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from
2369(select a, b, max(c) as max_c, avg(c) as avg_c from t1
2370where t1.a>5 group by a,b having max_c < 707) v1,
2371t2 where (v1.a=t2.a) and (v1.max_c>300) and (v1.a=v1.b);
2372a	b	max_c	avg_c	a	b	c	d
2373select * from
2374(select a, b, max(c) as max_c, avg(c) as avg_c from t1
2375where t1.a>5 group by a,b having max_c < 707) v1,
2376t2 where (v1.a=t2.a) and (v1.max_c>300) and (v1.a=v1.b);
2377a	b	max_c	avg_c	a	b	c	d
2378explain select * from
2379(select a, b, max(c) as max_c, avg(c) as avg_c from t1
2380where t1.a>5 group by a,b having max_c < 707) v1,
2381t2 where (v1.a=t2.a) and (v1.max_c>300) and (v1.a=v1.b);
2382id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
23831	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
23841	PRIMARY	<derived2>	ref	key0	key0	10	test.t2.a,test.t2.a	2	Using where
23852	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
2386explain format=json select * from
2387(select a, b, max(c) as max_c, avg(c) as avg_c from t1
2388where t1.a>5 group by a,b having max_c < 707) v1,
2389t2 where (v1.a=t2.a) and (v1.max_c>300) and (v1.a=v1.b);
2390EXPLAIN
2391{
2392  "query_block": {
2393    "select_id": 1,
2394    "table": {
2395      "table_name": "t2",
2396      "access_type": "ALL",
2397      "rows": 9,
2398      "filtered": 100,
2399      "attached_condition": "t2.a is not null and t2.a is not null"
2400    },
2401    "table": {
2402      "table_name": "<derived2>",
2403      "access_type": "ref",
2404      "possible_keys": ["key0"],
2405      "key": "key0",
2406      "key_length": "10",
2407      "used_key_parts": ["a", "b"],
2408      "ref": ["test.t2.a", "test.t2.a"],
2409      "rows": 2,
2410      "filtered": 100,
2411      "attached_condition": "v1.max_c > 300",
2412      "materialized": {
2413        "query_block": {
2414          "select_id": 2,
2415          "having_condition": "max_c < 707 and max_c > 300",
2416          "filesort": {
2417            "sort_key": "t1.a, t1.b",
2418            "temporary_table": {
2419              "table": {
2420                "table_name": "t1",
2421                "access_type": "ALL",
2422                "rows": 20,
2423                "filtered": 100,
2424                "attached_condition": "t1.b = t1.a and t1.a > 5"
2425              }
2426            }
2427          }
2428        }
2429      }
2430    }
2431  }
2432}
2433# nothing to push
2434set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (t2.a<2) and (t2.c>900);
2435a	b	max_c	avg_c	a	b	c	d
24361	19	107	107.0000	1	21	909	12
24371	21	500	234.6000	1	21	909	12
24385	16	207	207.0000	1	21	909	12
24395	27	132	132.0000	1	21	909	12
24406	20	315	279.3333	1	21	909	12
24418	33	404	213.6667	1	21	909	12
2442select * from v1,t2 where (t2.a<2) and (t2.c>900);
2443a	b	max_c	avg_c	a	b	c	d
24441	19	107	107.0000	1	21	909	12
24451	21	500	234.6000	1	21	909	12
24465	16	207	207.0000	1	21	909	12
24475	27	132	132.0000	1	21	909	12
24486	20	315	279.3333	1	21	909	12
24498	33	404	213.6667	1	21	909	12
2450explain select * from v1,t2 where (t2.a<2) and (t2.c>900);
2451id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
24521	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
24531	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using join buffer (flat, BNL join)
24542	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using temporary; Using filesort
2455explain format=json select * from v1,t2 where (t2.a<2) and (t2.c>900);
2456EXPLAIN
2457{
2458  "query_block": {
2459    "select_id": 1,
2460    "table": {
2461      "table_name": "t2",
2462      "access_type": "ALL",
2463      "rows": 9,
2464      "filtered": 100,
2465      "attached_condition": "t2.a < 2 and t2.c > 900"
2466    },
2467    "block-nl-join": {
2468      "table": {
2469        "table_name": "<derived2>",
2470        "access_type": "ALL",
2471        "rows": 20,
2472        "filtered": 100
2473      },
2474      "buffer_type": "flat",
2475      "buffer_size": "238",
2476      "join_type": "BNL",
2477      "materialized": {
2478        "query_block": {
2479          "select_id": 2,
2480          "having_condition": "max_c < 707",
2481          "filesort": {
2482            "sort_key": "t1.a, t1.b",
2483            "temporary_table": {
2484              "table": {
2485                "table_name": "t1",
2486                "access_type": "ALL",
2487                "rows": 20,
2488                "filtered": 100
2489              }
2490            }
2491          }
2492        }
2493      }
2494    }
2495  }
2496}
2497set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.a=t2.a) and (v1.b=t2.b);
2498a	b	max_c	avg_c	a	b	c	d
24991	21	500	234.6000	1	21	909	12
25006	20	315	279.3333	6	20	315	279
25011	19	107	107.0000	1	19	203	107
2502select * from v1,t2 where (v1.a=t2.a) and (v1.b=t2.b);
2503a	b	max_c	avg_c	a	b	c	d
25041	21	500	234.6000	1	21	909	12
25056	20	315	279.3333	6	20	315	279
25061	19	107	107.0000	1	19	203	107
2507explain select * from v1,t2 where (v1.a=t2.a) and (v1.b=t2.b);
2508id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
25091	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
25101	PRIMARY	<derived2>	ref	key0	key0	10	test.t2.a,test.t2.b	2
25112	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using temporary; Using filesort
2512explain format=json select * from v1,t2 where (v1.a=t2.a) and (v1.b=t2.b);
2513EXPLAIN
2514{
2515  "query_block": {
2516    "select_id": 1,
2517    "table": {
2518      "table_name": "t2",
2519      "access_type": "ALL",
2520      "rows": 9,
2521      "filtered": 100,
2522      "attached_condition": "t2.a is not null and t2.b is not null"
2523    },
2524    "table": {
2525      "table_name": "<derived2>",
2526      "access_type": "ref",
2527      "possible_keys": ["key0"],
2528      "key": "key0",
2529      "key_length": "10",
2530      "used_key_parts": ["a", "b"],
2531      "ref": ["test.t2.a", "test.t2.b"],
2532      "rows": 2,
2533      "filtered": 100,
2534      "materialized": {
2535        "query_block": {
2536          "select_id": 2,
2537          "having_condition": "max_c < 707",
2538          "filesort": {
2539            "sort_key": "t1.a, t1.b",
2540            "temporary_table": {
2541              "table": {
2542                "table_name": "t1",
2543                "access_type": "ALL",
2544                "rows": 20,
2545                "filtered": 100
2546              }
2547            }
2548          }
2549        }
2550      }
2551    }
2552  }
2553}
2554set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where
2555(t2.a=v1.a) or (v1.b=t2.b) and ((v1.a=1) or (v1.a=6));
2556a	b	max_c	avg_c	a	b	c	d
25571	19	107	107.0000	1	21	909	12
25581	19	107	107.0000	1	19	203	107
25591	21	500	234.6000	1	21	909	12
25601	21	500	234.6000	1	19	203	107
25616	20	315	279.3333	6	20	315	279
25626	20	315	279.3333	6	23	303	909
25638	33	404	213.6667	8	64	248	107
25648	33	404	213.6667	8	80	800	314
2565select * from v1,t2 where
2566(t2.a=v1.a) or (v1.b=t2.b) and ((v1.a=1) or (v1.a=6));
2567a	b	max_c	avg_c	a	b	c	d
25681	19	107	107.0000	1	21	909	12
25691	19	107	107.0000	1	19	203	107
25701	21	500	234.6000	1	21	909	12
25711	21	500	234.6000	1	19	203	107
25726	20	315	279.3333	6	20	315	279
25736	20	315	279.3333	6	23	303	909
25748	33	404	213.6667	8	64	248	107
25758	33	404	213.6667	8	80	800	314
2576explain select * from v1,t2 where
2577(t2.a=v1.a) or (v1.b=t2.b) and ((v1.a=1) or (v1.a=6));
2578id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
25791	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9
25801	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
25812	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using temporary; Using filesort
2582explain format=json select * from v1,t2 where
2583(t2.a=v1.a) or (v1.b=t2.b) and ((v1.a=1) or (v1.a=6));
2584EXPLAIN
2585{
2586  "query_block": {
2587    "select_id": 1,
2588    "table": {
2589      "table_name": "t2",
2590      "access_type": "ALL",
2591      "rows": 9,
2592      "filtered": 100
2593    },
2594    "block-nl-join": {
2595      "table": {
2596        "table_name": "<derived2>",
2597        "access_type": "ALL",
2598        "rows": 20,
2599        "filtered": 100
2600      },
2601      "buffer_type": "flat",
2602      "buffer_size": "238",
2603      "join_type": "BNL",
2604      "attached_condition": "v1.a = t2.a or v1.b = t2.b and (v1.a = 1 or v1.a = 6)",
2605      "materialized": {
2606        "query_block": {
2607          "select_id": 2,
2608          "having_condition": "max_c < 707",
2609          "filesort": {
2610            "sort_key": "t1.a, t1.b",
2611            "temporary_table": {
2612              "table": {
2613                "table_name": "t1",
2614                "access_type": "ALL",
2615                "rows": 20,
2616                "filtered": 100
2617              }
2618            }
2619          }
2620        }
2621      }
2622    }
2623  }
2624}
2625set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.a=1) or (v1.b=21) or (t2.a=2);
2626a	b	max_c	avg_c	a	b	c	d
26271	19	107	107.0000	2	3	207	207
26281	19	107	107.0000	1	21	909	12
26291	19	107	107.0000	7	13	312	406
26301	19	107	107.0000	8	64	248	107
26311	19	107	107.0000	6	20	315	279
26321	19	107	107.0000	1	19	203	107
26331	19	107	107.0000	8	80	800	314
26341	19	107	107.0000	3	12	231	190
26351	19	107	107.0000	6	23	303	909
26361	21	500	234.6000	2	3	207	207
26371	21	500	234.6000	1	21	909	12
26381	21	500	234.6000	7	13	312	406
26391	21	500	234.6000	8	64	248	107
26401	21	500	234.6000	6	20	315	279
26411	21	500	234.6000	1	19	203	107
26421	21	500	234.6000	8	80	800	314
26431	21	500	234.6000	3	12	231	190
26441	21	500	234.6000	6	23	303	909
26455	16	207	207.0000	2	3	207	207
26465	27	132	132.0000	2	3	207	207
26476	20	315	279.3333	2	3	207	207
26488	33	404	213.6667	2	3	207	207
2649select * from v1,t2 where (v1.a=1) or (v1.b=21) or (t2.a=2);
2650a	b	max_c	avg_c	a	b	c	d
26511	19	107	107.0000	2	3	207	207
26521	19	107	107.0000	1	21	909	12
26531	19	107	107.0000	7	13	312	406
26541	19	107	107.0000	8	64	248	107
26551	19	107	107.0000	6	20	315	279
26561	19	107	107.0000	1	19	203	107
26571	19	107	107.0000	8	80	800	314
26581	19	107	107.0000	3	12	231	190
26591	19	107	107.0000	6	23	303	909
26601	21	500	234.6000	2	3	207	207
26611	21	500	234.6000	1	21	909	12
26621	21	500	234.6000	7	13	312	406
26631	21	500	234.6000	8	64	248	107
26641	21	500	234.6000	6	20	315	279
26651	21	500	234.6000	1	19	203	107
26661	21	500	234.6000	8	80	800	314
26671	21	500	234.6000	3	12	231	190
26681	21	500	234.6000	6	23	303	909
26695	16	207	207.0000	2	3	207	207
26705	27	132	132.0000	2	3	207	207
26716	20	315	279.3333	2	3	207	207
26728	33	404	213.6667	2	3	207	207
2673explain select * from v1,t2 where (v1.a=1) or (v1.b=21) or (t2.a=2);
2674id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
26751	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9
26761	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
26772	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using temporary; Using filesort
2678explain format=json select * from v1,t2 where (v1.a=1) or (v1.b=21) or (t2.a=2);
2679EXPLAIN
2680{
2681  "query_block": {
2682    "select_id": 1,
2683    "table": {
2684      "table_name": "t2",
2685      "access_type": "ALL",
2686      "rows": 9,
2687      "filtered": 100
2688    },
2689    "block-nl-join": {
2690      "table": {
2691        "table_name": "<derived2>",
2692        "access_type": "ALL",
2693        "rows": 20,
2694        "filtered": 100
2695      },
2696      "buffer_type": "flat",
2697      "buffer_size": "238",
2698      "join_type": "BNL",
2699      "attached_condition": "v1.a = 1 or v1.b = 21 or t2.a = 2",
2700      "materialized": {
2701        "query_block": {
2702          "select_id": 2,
2703          "having_condition": "max_c < 707",
2704          "filesort": {
2705            "sort_key": "t1.a, t1.b",
2706            "temporary_table": {
2707              "table": {
2708                "table_name": "t1",
2709                "access_type": "ALL",
2710                "rows": 20,
2711                "filtered": 100
2712              }
2713            }
2714          }
2715        }
2716      }
2717    }
2718  }
2719}
2720set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where
2721(t2.a<2) and (t2.c>900) and ((v1.a<t2.a) or (t2.a<11));
2722a	b	max_c	avg_c	a	b	c	d
27231	19	107	107.0000	1	21	909	12
27241	21	500	234.6000	1	21	909	12
27255	16	207	207.0000	1	21	909	12
27265	27	132	132.0000	1	21	909	12
27276	20	315	279.3333	1	21	909	12
27288	33	404	213.6667	1	21	909	12
2729select * from v1,t2 where
2730(t2.a<2) and (t2.c>900) and ((v1.a<t2.a) or (t2.a<11));
2731a	b	max_c	avg_c	a	b	c	d
27321	19	107	107.0000	1	21	909	12
27331	21	500	234.6000	1	21	909	12
27345	16	207	207.0000	1	21	909	12
27355	27	132	132.0000	1	21	909	12
27366	20	315	279.3333	1	21	909	12
27378	33	404	213.6667	1	21	909	12
2738explain select * from v1,t2 where
2739(t2.a<2) and (t2.c>900) and ((v1.a<t2.a) or (t2.a<11));
2740id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
27411	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
27421	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
27432	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using temporary; Using filesort
2744explain format=json select * from v1,t2 where
2745(t2.a<2) and (t2.c>900) and ((v1.a<t2.a) or (t2.a<11));
2746EXPLAIN
2747{
2748  "query_block": {
2749    "select_id": 1,
2750    "table": {
2751      "table_name": "t2",
2752      "access_type": "ALL",
2753      "rows": 9,
2754      "filtered": 100,
2755      "attached_condition": "t2.a < 2 and t2.c > 900"
2756    },
2757    "block-nl-join": {
2758      "table": {
2759        "table_name": "<derived2>",
2760        "access_type": "ALL",
2761        "rows": 20,
2762        "filtered": 100
2763      },
2764      "buffer_type": "flat",
2765      "buffer_size": "238",
2766      "join_type": "BNL",
2767      "attached_condition": "v1.a < t2.a or t2.a < 11",
2768      "materialized": {
2769        "query_block": {
2770          "select_id": 2,
2771          "having_condition": "max_c < 707",
2772          "filesort": {
2773            "sort_key": "t1.a, t1.b",
2774            "temporary_table": {
2775              "table": {
2776                "table_name": "t1",
2777                "access_type": "ALL",
2778                "rows": 20,
2779                "filtered": 100
2780              }
2781            }
2782          }
2783        }
2784      }
2785    }
2786  }
2787}
2788# using several derived tables : nothing to push
2789set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,v2,t2 where
2790(v1.a=v2.a) and (v1.a=t2.a) and (v2.b<50);
2791a	b	max_c	avg_c	a	b	max_c	avg_c	a	b	c	d
27928	33	404	213.6667	8	33	404	213.6667	8	64	248	107
27936	20	315	279.3333	6	20	315	279.3333	6	20	315	279
27948	33	404	213.6667	8	33	404	213.6667	8	80	800	314
27956	20	315	279.3333	6	20	315	279.3333	6	23	303	909
2796select * from v1,v2,t2 where
2797(v1.a=v2.a) and (v1.a=t2.a) and (v2.b<50);
2798a	b	max_c	avg_c	a	b	max_c	avg_c	a	b	c	d
27998	33	404	213.6667	8	33	404	213.6667	8	64	248	107
28006	20	315	279.3333	6	20	315	279.3333	6	20	315	279
28018	33	404	213.6667	8	33	404	213.6667	8	80	800	314
28026	20	315	279.3333	6	20	315	279.3333	6	23	303	909
2803explain select * from v1,v2,t2 where
2804(v1.a=v2.a) and (v1.a=t2.a) and (v2.b<50);
2805id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
28061	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
28071	PRIMARY	<derived2>	ref	key1	key1	5	test.t2.a	2
28081	PRIMARY	<derived3>	ref	key0	key0	5	test.t2.a	2	Using where
28093	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
28102	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using temporary; Using filesort
2811explain format=json select * from v1,v2,t2 where
2812(v1.a=v2.a) and (v1.a=t2.a) and (v2.b<50);
2813EXPLAIN
2814{
2815  "query_block": {
2816    "select_id": 1,
2817    "table": {
2818      "table_name": "t2",
2819      "access_type": "ALL",
2820      "rows": 9,
2821      "filtered": 100,
2822      "attached_condition": "t2.a is not null and t2.a is not null"
2823    },
2824    "table": {
2825      "table_name": "<derived2>",
2826      "access_type": "ref",
2827      "possible_keys": ["key1"],
2828      "key": "key1",
2829      "key_length": "5",
2830      "used_key_parts": ["a"],
2831      "ref": ["test.t2.a"],
2832      "rows": 2,
2833      "filtered": 100,
2834      "materialized": {
2835        "query_block": {
2836          "select_id": 2,
2837          "having_condition": "max_c < 707",
2838          "filesort": {
2839            "sort_key": "t1.a, t1.b",
2840            "temporary_table": {
2841              "table": {
2842                "table_name": "t1",
2843                "access_type": "ALL",
2844                "rows": 20,
2845                "filtered": 100
2846              }
2847            }
2848          }
2849        }
2850      }
2851    },
2852    "table": {
2853      "table_name": "<derived3>",
2854      "access_type": "ref",
2855      "possible_keys": ["key0"],
2856      "key": "key0",
2857      "key_length": "5",
2858      "used_key_parts": ["a"],
2859      "ref": ["test.t2.a"],
2860      "rows": 2,
2861      "filtered": 100,
2862      "attached_condition": "v2.b < 50",
2863      "materialized": {
2864        "query_block": {
2865          "select_id": 3,
2866          "having_condition": "max_c < 707",
2867          "filesort": {
2868            "sort_key": "t1.a, t1.b",
2869            "temporary_table": {
2870              "table": {
2871                "table_name": "t1",
2872                "access_type": "ALL",
2873                "rows": 20,
2874                "filtered": 100,
2875                "attached_condition": "t1.a > 5 and t1.b < 50"
2876              }
2877            }
2878          }
2879        }
2880      }
2881    }
2882  }
2883}
2884set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,v2,t2 where
2885((v1.a=v2.a) or (v1.a=t2.a)) and (t2.b<50) and (v1.b=v2.b);
2886a	b	max_c	avg_c	a	b	max_c	avg_c	a	b	c	d
28876	20	315	279.3333	6	20	315	279.3333	2	3	207	207
28886	20	315	279.3333	6	20	315	279.3333	1	21	909	12
28896	20	315	279.3333	6	20	315	279.3333	7	13	312	406
28906	20	315	279.3333	6	20	315	279.3333	6	20	315	279
28916	20	315	279.3333	6	20	315	279.3333	1	19	203	107
28926	20	315	279.3333	6	20	315	279.3333	3	12	231	190
28936	20	315	279.3333	6	20	315	279.3333	6	23	303	909
28948	33	404	213.6667	8	33	404	213.6667	2	3	207	207
28958	33	404	213.6667	8	33	404	213.6667	1	21	909	12
28968	33	404	213.6667	8	33	404	213.6667	7	13	312	406
28978	33	404	213.6667	8	33	404	213.6667	6	20	315	279
28988	33	404	213.6667	8	33	404	213.6667	1	19	203	107
28998	33	404	213.6667	8	33	404	213.6667	3	12	231	190
29008	33	404	213.6667	8	33	404	213.6667	6	23	303	909
2901select * from v1,v2,t2 where
2902((v1.a=v2.a) or (v1.a=t2.a)) and (t2.b<50) and (v1.b=v2.b);
2903a	b	max_c	avg_c	a	b	max_c	avg_c	a	b	c	d
29046	20	315	279.3333	6	20	315	279.3333	2	3	207	207
29056	20	315	279.3333	6	20	315	279.3333	1	21	909	12
29066	20	315	279.3333	6	20	315	279.3333	7	13	312	406
29076	20	315	279.3333	6	20	315	279.3333	6	20	315	279
29086	20	315	279.3333	6	20	315	279.3333	1	19	203	107
29096	20	315	279.3333	6	20	315	279.3333	3	12	231	190
29106	20	315	279.3333	6	20	315	279.3333	6	23	303	909
29118	33	404	213.6667	8	33	404	213.6667	2	3	207	207
29128	33	404	213.6667	8	33	404	213.6667	1	21	909	12
29138	33	404	213.6667	8	33	404	213.6667	7	13	312	406
29148	33	404	213.6667	8	33	404	213.6667	6	20	315	279
29158	33	404	213.6667	8	33	404	213.6667	1	19	203	107
29168	33	404	213.6667	8	33	404	213.6667	3	12	231	190
29178	33	404	213.6667	8	33	404	213.6667	6	23	303	909
2918explain select * from v1,v2,t2 where
2919((v1.a=v2.a) or (v1.a=t2.a)) and (t2.b<50) and (v1.b=v2.b);
2920id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
29211	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
29221	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
29231	PRIMARY	<derived3>	ref	key0	key0	5	v1.b	2	Using where
29243	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
29252	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using temporary; Using filesort
2926explain format=json select * from v1,v2,t2 where
2927((v1.a=v2.a) or (v1.a=t2.a)) and (t2.b<50) and (v1.b=v2.b);
2928EXPLAIN
2929{
2930  "query_block": {
2931    "select_id": 1,
2932    "table": {
2933      "table_name": "t2",
2934      "access_type": "ALL",
2935      "rows": 9,
2936      "filtered": 100,
2937      "attached_condition": "t2.b < 50"
2938    },
2939    "block-nl-join": {
2940      "table": {
2941        "table_name": "<derived2>",
2942        "access_type": "ALL",
2943        "rows": 20,
2944        "filtered": 100
2945      },
2946      "buffer_type": "flat",
2947      "buffer_size": "238",
2948      "join_type": "BNL",
2949      "attached_condition": "v1.b is not null",
2950      "materialized": {
2951        "query_block": {
2952          "select_id": 2,
2953          "having_condition": "max_c < 707",
2954          "filesort": {
2955            "sort_key": "t1.a, t1.b",
2956            "temporary_table": {
2957              "table": {
2958                "table_name": "t1",
2959                "access_type": "ALL",
2960                "rows": 20,
2961                "filtered": 100
2962              }
2963            }
2964          }
2965        }
2966      }
2967    },
2968    "table": {
2969      "table_name": "<derived3>",
2970      "access_type": "ref",
2971      "possible_keys": ["key0"],
2972      "key": "key0",
2973      "key_length": "5",
2974      "used_key_parts": ["b"],
2975      "ref": ["v1.b"],
2976      "rows": 2,
2977      "filtered": 100,
2978      "attached_condition": "v2.a = v1.a or v1.a = t2.a",
2979      "materialized": {
2980        "query_block": {
2981          "select_id": 3,
2982          "having_condition": "max_c < 707",
2983          "filesort": {
2984            "sort_key": "t1.a, t1.b",
2985            "temporary_table": {
2986              "table": {
2987                "table_name": "t1",
2988                "access_type": "ALL",
2989                "rows": 20,
2990                "filtered": 100,
2991                "attached_condition": "t1.a > 5"
2992              }
2993            }
2994          }
2995        }
2996      }
2997    }
2998  }
2999}
3000set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,v2,t2 where
3001((v1.a=v2.a) and (v1.a=t2.a)) or ((v2.b>13) and (t2.c<115));
3002a	b	max_c	avg_c	a	b	max_c	avg_c	a	b	c	d
30036	20	315	279.3333	6	20	315	279.3333	6	20	315	279
30046	20	315	279.3333	6	20	315	279.3333	6	23	303	909
30058	33	404	213.6667	8	33	404	213.6667	8	64	248	107
30068	33	404	213.6667	8	33	404	213.6667	8	80	800	314
3007select * from v1,v2,t2 where
3008((v1.a=v2.a) and (v1.a=t2.a)) or ((v2.b>13) and (t2.c<115));
3009a	b	max_c	avg_c	a	b	max_c	avg_c	a	b	c	d
30106	20	315	279.3333	6	20	315	279.3333	6	20	315	279
30116	20	315	279.3333	6	20	315	279.3333	6	23	303	909
30128	33	404	213.6667	8	33	404	213.6667	8	64	248	107
30138	33	404	213.6667	8	33	404	213.6667	8	80	800	314
3014explain select * from v1,v2,t2 where
3015((v1.a=v2.a) and (v1.a=t2.a)) or ((v2.b>13) and (t2.c<115));
3016id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
30171	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9
30181	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
30191	PRIMARY	<derived3>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (incremental, BNL join)
30203	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
30212	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using temporary; Using filesort
3022explain format=json select * from v1,v2,t2 where
3023((v1.a=v2.a) and (v1.a=t2.a)) or ((v2.b>13) and (t2.c<115));
3024EXPLAIN
3025{
3026  "query_block": {
3027    "select_id": 1,
3028    "table": {
3029      "table_name": "t2",
3030      "access_type": "ALL",
3031      "rows": 9,
3032      "filtered": 100
3033    },
3034    "block-nl-join": {
3035      "table": {
3036        "table_name": "<derived2>",
3037        "access_type": "ALL",
3038        "rows": 20,
3039        "filtered": 100
3040      },
3041      "buffer_type": "flat",
3042      "buffer_size": "238",
3043      "join_type": "BNL",
3044      "attached_condition": "v1.a = t2.a or t2.c < 115",
3045      "materialized": {
3046        "query_block": {
3047          "select_id": 2,
3048          "having_condition": "max_c < 707",
3049          "filesort": {
3050            "sort_key": "t1.a, t1.b",
3051            "temporary_table": {
3052              "table": {
3053                "table_name": "t1",
3054                "access_type": "ALL",
3055                "rows": 20,
3056                "filtered": 100
3057              }
3058            }
3059          }
3060        }
3061      }
3062    },
3063    "block-nl-join": {
3064      "table": {
3065        "table_name": "<derived3>",
3066        "access_type": "ALL",
3067        "rows": 20,
3068        "filtered": 100
3069      },
3070      "buffer_type": "incremental",
3071      "buffer_size": "4Kb",
3072      "join_type": "BNL",
3073      "attached_condition": "v1.a = t2.a and v2.a = t2.a or v2.b > 13 and t2.c < 115",
3074      "materialized": {
3075        "query_block": {
3076          "select_id": 3,
3077          "having_condition": "max_c < 707",
3078          "filesort": {
3079            "sort_key": "t1.a, t1.b",
3080            "temporary_table": {
3081              "table": {
3082                "table_name": "t1",
3083                "access_type": "ALL",
3084                "rows": 20,
3085                "filtered": 100,
3086                "attached_condition": "t1.a > 5"
3087              }
3088            }
3089          }
3090        }
3091      }
3092    }
3093  }
3094}
3095# using several derived tables : pushing in all tables
3096# conjunctive subformula : pushing into HAVING
3097# extracted or formula : pushing into WHERE
3098# pushing equalities
3099set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,v2,t2 where ((v1.a=v2.a) or (v1.a=t2.a)) and
3100((v2.b<50) or (v2.b=19)) and (v1.max_c<300);
3101a	b	max_c	avg_c	a	b	max_c	avg_c	a	b	c	d
31021	19	107	107.0000	6	20	315	279.3333	1	21	909	12
31031	19	107	107.0000	6	20	315	279.3333	1	19	203	107
31041	19	107	107.0000	8	33	404	213.6667	1	21	909	12
31051	19	107	107.0000	8	33	404	213.6667	1	19	203	107
3106select * from v1,v2,t2 where ((v1.a=v2.a) or (v1.a=t2.a)) and
3107((v2.b<50) or (v2.b=19)) and (v1.max_c<300);
3108a	b	max_c	avg_c	a	b	max_c	avg_c	a	b	c	d
31091	19	107	107.0000	6	20	315	279.3333	1	21	909	12
31101	19	107	107.0000	6	20	315	279.3333	1	19	203	107
31111	19	107	107.0000	8	33	404	213.6667	1	21	909	12
31121	19	107	107.0000	8	33	404	213.6667	1	19	203	107
3113explain select * from v1,v2,t2 where ((v1.a=v2.a) or (v1.a=t2.a)) and
3114((v2.b<50) or (v2.b=19)) and (v1.max_c<300);
3115id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
31161	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9
31171	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
31181	PRIMARY	<derived3>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (incremental, BNL join)
31193	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
31202	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using temporary; Using filesort
3121explain format=json select * from v1,v2,t2 where ((v1.a=v2.a) or (v1.a=t2.a)) and
3122((v2.b<50) or (v2.b=19)) and (v1.max_c<300);
3123EXPLAIN
3124{
3125  "query_block": {
3126    "select_id": 1,
3127    "table": {
3128      "table_name": "t2",
3129      "access_type": "ALL",
3130      "rows": 9,
3131      "filtered": 100
3132    },
3133    "block-nl-join": {
3134      "table": {
3135        "table_name": "<derived2>",
3136        "access_type": "ALL",
3137        "rows": 20,
3138        "filtered": 100,
3139        "attached_condition": "v1.max_c < 300"
3140      },
3141      "buffer_type": "flat",
3142      "buffer_size": "238",
3143      "join_type": "BNL",
3144      "materialized": {
3145        "query_block": {
3146          "select_id": 2,
3147          "having_condition": "max_c < 707 and max_c < 300",
3148          "filesort": {
3149            "sort_key": "t1.a, t1.b",
3150            "temporary_table": {
3151              "table": {
3152                "table_name": "t1",
3153                "access_type": "ALL",
3154                "rows": 20,
3155                "filtered": 100
3156              }
3157            }
3158          }
3159        }
3160      }
3161    },
3162    "block-nl-join": {
3163      "table": {
3164        "table_name": "<derived3>",
3165        "access_type": "ALL",
3166        "rows": 20,
3167        "filtered": 100,
3168        "attached_condition": "v2.b < 50 or v2.b = 19"
3169      },
3170      "buffer_type": "incremental",
3171      "buffer_size": "4Kb",
3172      "join_type": "BNL",
3173      "attached_condition": "(v2.a = v1.a or v1.a = t2.a) and (v2.b < 50 or v2.b = 19)",
3174      "materialized": {
3175        "query_block": {
3176          "select_id": 3,
3177          "having_condition": "max_c < 707",
3178          "filesort": {
3179            "sort_key": "t1.a, t1.b",
3180            "temporary_table": {
3181              "table": {
3182                "table_name": "t1",
3183                "access_type": "ALL",
3184                "rows": 20,
3185                "filtered": 100,
3186                "attached_condition": "t1.a > 5 and (t1.b < 50 or t1.b = 19)"
3187              }
3188            }
3189          }
3190        }
3191      }
3192    }
3193  }
3194}
3195# using several derived tables : pushing only in one table
3196# conjunctive subformula : pushing into WHERE
3197# pushing equalities
3198set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,v2,t2 where
3199(v1.a=t2.a) and (v1.a=v1.b) and (v1.a=v2.a) and (v2.max_c<300);
3200a	b	max_c	avg_c	a	b	max_c	avg_c	a	b	c	d
3201select * from v1,v2,t2 where
3202(v1.a=t2.a) and (v1.a=v1.b) and (v1.a=v2.a) and (v2.max_c<300);
3203a	b	max_c	avg_c	a	b	max_c	avg_c	a	b	c	d
3204explain select * from v1,v2,t2 where
3205(v1.a=t2.a) and (v1.a=v1.b) and (v1.a=v2.a) and (v2.max_c<300);
3206id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
32071	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
32081	PRIMARY	<derived2>	ref	key1	key1	10	test.t2.a,test.t2.a	2
32091	PRIMARY	<derived3>	ref	key0	key0	5	test.t2.a	2	Using where
32103	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
32112	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
3212explain format=json select * from v1,v2,t2 where
3213(v1.a=t2.a) and (v1.a=v1.b) and (v1.a=v2.a) and (v2.max_c<300);
3214EXPLAIN
3215{
3216  "query_block": {
3217    "select_id": 1,
3218    "table": {
3219      "table_name": "t2",
3220      "access_type": "ALL",
3221      "rows": 9,
3222      "filtered": 100,
3223      "attached_condition": "t2.a is not null and t2.a is not null and t2.a is not null"
3224    },
3225    "table": {
3226      "table_name": "<derived2>",
3227      "access_type": "ref",
3228      "possible_keys": ["key1"],
3229      "key": "key1",
3230      "key_length": "10",
3231      "used_key_parts": ["a", "b"],
3232      "ref": ["test.t2.a", "test.t2.a"],
3233      "rows": 2,
3234      "filtered": 100,
3235      "materialized": {
3236        "query_block": {
3237          "select_id": 2,
3238          "having_condition": "max_c < 707",
3239          "filesort": {
3240            "sort_key": "t1.a, t1.b",
3241            "temporary_table": {
3242              "table": {
3243                "table_name": "t1",
3244                "access_type": "ALL",
3245                "rows": 20,
3246                "filtered": 100,
3247                "attached_condition": "t1.b = t1.a"
3248              }
3249            }
3250          }
3251        }
3252      }
3253    },
3254    "table": {
3255      "table_name": "<derived3>",
3256      "access_type": "ref",
3257      "possible_keys": ["key0"],
3258      "key": "key0",
3259      "key_length": "5",
3260      "used_key_parts": ["a"],
3261      "ref": ["test.t2.a"],
3262      "rows": 2,
3263      "filtered": 100,
3264      "attached_condition": "v2.max_c < 300",
3265      "materialized": {
3266        "query_block": {
3267          "select_id": 3,
3268          "having_condition": "max_c < 707 and max_c < 300",
3269          "filesort": {
3270            "sort_key": "t1.a, t1.b",
3271            "temporary_table": {
3272              "table": {
3273                "table_name": "t1",
3274                "access_type": "ALL",
3275                "rows": 20,
3276                "filtered": 100,
3277                "attached_condition": "t1.a > 5"
3278              }
3279            }
3280          }
3281        }
3282      }
3283    }
3284  }
3285}
3286# using several derived tables : pushing only in one table
3287# extracted and formula : pushing into WHERE
3288# conjunctive subformula : pushing into WHERE using equalities
3289# pushing equalities
3290set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,v2,t2 where (v1.a=1) and (v1.b>10) and (v1.b=v2.b);
3291a	b	max_c	avg_c	a	b	max_c	avg_c	a	b	c	d
3292select * from v1,v2,t2 where (v1.a=1) and (v1.b>10) and (v1.b=v2.b);
3293a	b	max_c	avg_c	a	b	max_c	avg_c	a	b	c	d
3294explain select * from v1,v2,t2 where (v1.a=1) and (v1.b>10) and (v1.b=v2.b);
3295id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
32961	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9
32971	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
32981	PRIMARY	<derived3>	ref	key0	key0	5	v1.b	2
32993	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
33002	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
3301explain format=json select * from v1,v2,t2 where (v1.a=1) and (v1.b>10) and (v1.b=v2.b);
3302EXPLAIN
3303{
3304  "query_block": {
3305    "select_id": 1,
3306    "table": {
3307      "table_name": "t2",
3308      "access_type": "ALL",
3309      "rows": 9,
3310      "filtered": 100
3311    },
3312    "block-nl-join": {
3313      "table": {
3314        "table_name": "<derived2>",
3315        "access_type": "ALL",
3316        "rows": 20,
3317        "filtered": 100,
3318        "attached_condition": "v1.a = 1 and v1.b > 10"
3319      },
3320      "buffer_type": "flat",
3321      "buffer_size": "238",
3322      "join_type": "BNL",
3323      "attached_condition": "v1.b is not null",
3324      "materialized": {
3325        "query_block": {
3326          "select_id": 2,
3327          "having_condition": "max_c < 707",
3328          "filesort": {
3329            "sort_key": "t1.b",
3330            "temporary_table": {
3331              "table": {
3332                "table_name": "t1",
3333                "access_type": "ALL",
3334                "rows": 20,
3335                "filtered": 100,
3336                "attached_condition": "t1.a = 1 and t1.b > 10"
3337              }
3338            }
3339          }
3340        }
3341      }
3342    },
3343    "table": {
3344      "table_name": "<derived3>",
3345      "access_type": "ref",
3346      "possible_keys": ["key0"],
3347      "key": "key0",
3348      "key_length": "5",
3349      "used_key_parts": ["b"],
3350      "ref": ["v1.b"],
3351      "rows": 2,
3352      "filtered": 100,
3353      "materialized": {
3354        "query_block": {
3355          "select_id": 3,
3356          "having_condition": "max_c < 707",
3357          "filesort": {
3358            "sort_key": "t1.a, t1.b",
3359            "temporary_table": {
3360              "table": {
3361                "table_name": "t1",
3362                "access_type": "ALL",
3363                "rows": 20,
3364                "filtered": 100,
3365                "attached_condition": "t1.a > 5 and t1.b > 10"
3366              }
3367            }
3368          }
3369        }
3370      }
3371    }
3372  }
3373}
3374# extracted or formula : pushing into WHERE
3375# conjunctive subformula : pushing into WHERE using equalities
3376# pushing equalities
3377set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v_char as v,t2_char as t where
3378(v.a=t.a) and (t.a='b') and ((v.b='Vika') or (v.b='Ali'));
3379a	b	max_c	a	b	c
3380b	Vika	2	b	Ivan	1
3381b	Vika	2	b	Ali	6
3382b	Vika	2	b	Hermes	3
3383b	Vika	2	b	Ivan	11
3384b	Vika	2	b	Harry	4
3385select * from v_char as v,t2_char as t where
3386(v.a=t.a) and (t.a='b') and ((v.b='Vika') or (v.b='Ali'));
3387a	b	max_c	a	b	c
3388b	Vika	2	b	Ivan	1
3389b	Vika	2	b	Ali	6
3390b	Vika	2	b	Hermes	3
3391b	Vika	2	b	Ivan	11
3392b	Vika	2	b	Harry	4
3393explain select * from v_char as v,t2_char as t where
3394(v.a=t.a) and (t.a='b') and ((v.b='Vika') or (v.b='Ali'));
3395id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
33961	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	12	Using where
33971	PRIMARY	t	ALL	NULL	NULL	NULL	NULL	12	Using where; Using join buffer (flat, BNL join)
33982	DERIVED	t1_char	ALL	NULL	NULL	NULL	NULL	12	Using where; Using temporary; Using filesort
3399explain format=json select * from v_char as v,t2_char as t where
3400(v.a=t.a) and (t.a='b') and ((v.b='Vika') or (v.b='Ali'));
3401EXPLAIN
3402{
3403  "query_block": {
3404    "select_id": 1,
3405    "table": {
3406      "table_name": "<derived2>",
3407      "access_type": "ALL",
3408      "rows": 12,
3409      "filtered": 100,
3410      "attached_condition": "v.a = 'b' and (v.b = 'Vika' or v.b = 'Ali')",
3411      "materialized": {
3412        "query_block": {
3413          "select_id": 2,
3414          "having_condition": "max_c < 9",
3415          "filesort": {
3416            "sort_key": "t1_char.b",
3417            "temporary_table": {
3418              "table": {
3419                "table_name": "t1_char",
3420                "access_type": "ALL",
3421                "rows": 12,
3422                "filtered": 100,
3423                "attached_condition": "t1_char.a = 'b' and (t1_char.b = 'Vika' or t1_char.b = 'Ali')"
3424              }
3425            }
3426          }
3427        }
3428      }
3429    },
3430    "block-nl-join": {
3431      "table": {
3432        "table_name": "t",
3433        "access_type": "ALL",
3434        "rows": 12,
3435        "filtered": 100,
3436        "attached_condition": "t.a = 'b'"
3437      },
3438      "buffer_type": "flat",
3439      "buffer_size": "220",
3440      "join_type": "BNL"
3441    }
3442  }
3443}
3444# using several derived tables : pushing in all tables
3445# extracted or formula : pushing into WHERE
3446# conjunctive subformulas : pushing into HAVING
3447# pushing equalities
3448set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,v2,v3,t2 where
3449((v1.a=v2.a) or (v1.a=t2.a)) and ((v3.b<50) or (v3.b=33))
3450and (v1.max_c<500) and (v3.a=t2.a) and (v2.max_c>300);
3451a	b	max_c	avg_c	a	b	max_c	avg_c	a	b	min_c	a	b	c	d
34526	20	315	279.3333	6	20	315	279.3333	7	11	708	7	13	312	406
34536	20	315	279.3333	6	20	315	279.3333	8	33	114	8	64	248	107
34546	20	315	279.3333	6	20	315	279.3333	6	20	214	6	20	315	279
34556	20	315	279.3333	6	20	315	279.3333	8	33	114	8	80	800	314
34566	20	315	279.3333	6	20	315	279.3333	6	20	214	6	23	303	909
34576	20	315	279.3333	8	33	404	213.6667	6	20	214	6	20	315	279
34586	20	315	279.3333	8	33	404	213.6667	6	20	214	6	23	303	909
34598	33	404	213.6667	6	20	315	279.3333	8	33	114	8	64	248	107
34608	33	404	213.6667	6	20	315	279.3333	8	33	114	8	80	800	314
34618	33	404	213.6667	8	33	404	213.6667	7	11	708	7	13	312	406
34628	33	404	213.6667	8	33	404	213.6667	8	33	114	8	64	248	107
34638	33	404	213.6667	8	33	404	213.6667	6	20	214	6	20	315	279
34648	33	404	213.6667	8	33	404	213.6667	8	33	114	8	80	800	314
34658	33	404	213.6667	8	33	404	213.6667	6	20	214	6	23	303	909
3466select * from v1,v2,v3,t2 where
3467((v1.a=v2.a) or (v1.a=t2.a)) and ((v3.b<50) or (v3.b=33))
3468and (v1.max_c<500) and (v3.a=t2.a) and (v2.max_c>300);
3469a	b	max_c	avg_c	a	b	max_c	avg_c	a	b	min_c	a	b	c	d
34706	20	315	279.3333	6	20	315	279.3333	7	11	708	7	13	312	406
34716	20	315	279.3333	6	20	315	279.3333	8	33	114	8	64	248	107
34726	20	315	279.3333	6	20	315	279.3333	6	20	214	6	20	315	279
34736	20	315	279.3333	6	20	315	279.3333	8	33	114	8	80	800	314
34746	20	315	279.3333	6	20	315	279.3333	6	20	214	6	23	303	909
34756	20	315	279.3333	8	33	404	213.6667	6	20	214	6	20	315	279
34766	20	315	279.3333	8	33	404	213.6667	6	20	214	6	23	303	909
34778	33	404	213.6667	6	20	315	279.3333	8	33	114	8	64	248	107
34788	33	404	213.6667	6	20	315	279.3333	8	33	114	8	80	800	314
34798	33	404	213.6667	8	33	404	213.6667	7	11	708	7	13	312	406
34808	33	404	213.6667	8	33	404	213.6667	8	33	114	8	64	248	107
34818	33	404	213.6667	8	33	404	213.6667	6	20	214	6	20	315	279
34828	33	404	213.6667	8	33	404	213.6667	8	33	114	8	80	800	314
34838	33	404	213.6667	8	33	404	213.6667	6	20	214	6	23	303	909
3484explain select * from v1,v2,v3,t2 where
3485((v1.a=v2.a) or (v1.a=t2.a)) and ((v3.b<50) or (v3.b=33))
3486and (v1.max_c<500) and (v3.a=t2.a) and (v2.max_c>300);
3487id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
34881	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
34891	PRIMARY	<derived4>	ref	key0	key0	5	test.t2.a	2	Using where
34901	PRIMARY	<derived3>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
34911	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (incremental, BNL join)
34924	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
34933	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
34942	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using temporary; Using filesort
3495explain format=json select * from v1,v2,v3,t2 where
3496((v1.a=v2.a) or (v1.a=t2.a)) and ((v3.b<50) or (v3.b=33))
3497and (v1.max_c<500) and (v3.a=t2.a) and (v2.max_c>300);
3498EXPLAIN
3499{
3500  "query_block": {
3501    "select_id": 1,
3502    "table": {
3503      "table_name": "t2",
3504      "access_type": "ALL",
3505      "rows": 9,
3506      "filtered": 100,
3507      "attached_condition": "t2.a is not null"
3508    },
3509    "table": {
3510      "table_name": "<derived4>",
3511      "access_type": "ref",
3512      "possible_keys": ["key0"],
3513      "key": "key0",
3514      "key_length": "5",
3515      "used_key_parts": ["a"],
3516      "ref": ["test.t2.a"],
3517      "rows": 2,
3518      "filtered": 100,
3519      "attached_condition": "v3.b < 50 or v3.b = 33",
3520      "materialized": {
3521        "query_block": {
3522          "select_id": 4,
3523          "having_condition": "min_c > 109",
3524          "filesort": {
3525            "sort_key": "t1.a, t1.b",
3526            "temporary_table": {
3527              "table": {
3528                "table_name": "t1",
3529                "access_type": "ALL",
3530                "rows": 20,
3531                "filtered": 100,
3532                "attached_condition": "t1.a < 10 and (t1.b < 50 or t1.b = 33)"
3533              }
3534            }
3535          }
3536        }
3537      }
3538    },
3539    "block-nl-join": {
3540      "table": {
3541        "table_name": "<derived3>",
3542        "access_type": "ALL",
3543        "rows": 20,
3544        "filtered": 100,
3545        "attached_condition": "v2.max_c > 300"
3546      },
3547      "buffer_type": "flat",
3548      "buffer_size": "715",
3549      "join_type": "BNL",
3550      "materialized": {
3551        "query_block": {
3552          "select_id": 3,
3553          "having_condition": "max_c < 707 and max_c > 300",
3554          "filesort": {
3555            "sort_key": "t1.a, t1.b",
3556            "temporary_table": {
3557              "table": {
3558                "table_name": "t1",
3559                "access_type": "ALL",
3560                "rows": 20,
3561                "filtered": 100,
3562                "attached_condition": "t1.a > 5"
3563              }
3564            }
3565          }
3566        }
3567      }
3568    },
3569    "block-nl-join": {
3570      "table": {
3571        "table_name": "<derived2>",
3572        "access_type": "ALL",
3573        "rows": 20,
3574        "filtered": 100,
3575        "attached_condition": "v1.max_c < 500"
3576      },
3577      "buffer_type": "incremental",
3578      "buffer_size": "9Kb",
3579      "join_type": "BNL",
3580      "attached_condition": "v1.a = v2.a or v1.a = t2.a",
3581      "materialized": {
3582        "query_block": {
3583          "select_id": 2,
3584          "having_condition": "max_c < 707 and max_c < 500",
3585          "filesort": {
3586            "sort_key": "t1.a, t1.b",
3587            "temporary_table": {
3588              "table": {
3589                "table_name": "t1",
3590                "access_type": "ALL",
3591                "rows": 20,
3592                "filtered": 100
3593              }
3594            }
3595          }
3596        }
3597      }
3598    }
3599  }
3600}
3601# using several derived tables : pushing in all tables
3602# conjunctive subformulas : pushing into HAVING
3603set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from
3604(select a, b, max(c) as max_c, avg(c) as avg_c from t1
3605where t1.a>5 group by a,b having max_c < 707) v1,
3606(select a, b, min(c) as min_c from t1
3607where t1.a>5 group by a,b having min_c < 707) v2,
3608t2 where (v1.a=v2.a) and (v1.b=t2.b) and (v1.max_c>130) and (v2.min_c<130);
3609a	b	max_c	avg_c	a	b	min_c	a	b	c	d
3610select * from
3611(select a, b, max(c) as max_c, avg(c) as avg_c from t1
3612where t1.a>5 group by a,b having max_c < 707) v1,
3613(select a, b, min(c) as min_c from t1
3614where t1.a>5 group by a,b having min_c < 707) v2,
3615t2 where (v1.a=v2.a) and (v1.b=t2.b) and (v1.max_c>130) and (v2.min_c<130);
3616a	b	max_c	avg_c	a	b	min_c	a	b	c	d
3617explain select * from
3618(select a, b, max(c) as max_c, avg(c) as avg_c from t1
3619where t1.a>5 group by a,b having max_c < 707) v1,
3620(select a, b, min(c) as min_c from t1
3621where t1.a>5 group by a,b having min_c < 707) v2,
3622t2 where (v1.a=v2.a) and (v1.b=t2.b) and (v1.max_c>130) and (v2.min_c<130);
3623id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
36241	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
36251	PRIMARY	<derived2>	ref	key1	key1	5	test.t2.b	2	Using where
36261	PRIMARY	<derived3>	ref	key0	key0	5	v1.a	2	Using where
36273	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
36282	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
3629explain format=json select * from
3630(select a, b, max(c) as max_c, avg(c) as avg_c from t1
3631where t1.a>5 group by a,b having max_c < 707) v1,
3632(select a, b, min(c) as min_c from t1
3633where t1.a>5 group by a,b having min_c < 707) v2,
3634t2 where (v1.a=v2.a) and (v1.b=t2.b) and (v1.max_c>130) and (v2.min_c<130);
3635EXPLAIN
3636{
3637  "query_block": {
3638    "select_id": 1,
3639    "table": {
3640      "table_name": "t2",
3641      "access_type": "ALL",
3642      "rows": 9,
3643      "filtered": 100,
3644      "attached_condition": "t2.b is not null"
3645    },
3646    "table": {
3647      "table_name": "<derived2>",
3648      "access_type": "ref",
3649      "possible_keys": ["key1"],
3650      "key": "key1",
3651      "key_length": "5",
3652      "used_key_parts": ["b"],
3653      "ref": ["test.t2.b"],
3654      "rows": 2,
3655      "filtered": 100,
3656      "attached_condition": "v1.max_c > 130 and v1.a is not null",
3657      "materialized": {
3658        "query_block": {
3659          "select_id": 2,
3660          "having_condition": "max_c < 707 and max_c > 130",
3661          "filesort": {
3662            "sort_key": "t1.a, t1.b",
3663            "temporary_table": {
3664              "table": {
3665                "table_name": "t1",
3666                "access_type": "ALL",
3667                "rows": 20,
3668                "filtered": 100,
3669                "attached_condition": "t1.a > 5"
3670              }
3671            }
3672          }
3673        }
3674      }
3675    },
3676    "table": {
3677      "table_name": "<derived3>",
3678      "access_type": "ref",
3679      "possible_keys": ["key0"],
3680      "key": "key0",
3681      "key_length": "5",
3682      "used_key_parts": ["a"],
3683      "ref": ["v1.a"],
3684      "rows": 2,
3685      "filtered": 100,
3686      "attached_condition": "v2.min_c < 130",
3687      "materialized": {
3688        "query_block": {
3689          "select_id": 3,
3690          "having_condition": "min_c < 707 and min_c < 130",
3691          "filesort": {
3692            "sort_key": "t1.a, t1.b",
3693            "temporary_table": {
3694              "table": {
3695                "table_name": "t1",
3696                "access_type": "ALL",
3697                "rows": 20,
3698                "filtered": 100,
3699                "attached_condition": "t1.a > 5"
3700              }
3701            }
3702          }
3703        }
3704      }
3705    }
3706  }
3707}
3708# using several derived tables : pushing in all tables
3709# extracted or formulas : pushing into HAVING
3710# conjunctive subformula : pushing into HAVING
3711set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from
3712(select a, b, max(c) as max_c, avg(c) as avg_c from t1
3713where t1.a>5 group by a,b having max_c < 707) v1,
3714(select a, b, min(c) as min_c from t1
3715where t1.a>5 group by a,b having min_c < 707) v2,
3716(select a, b, avg(c) as avg_c from t1
3717where t1.a<8 group by a,b) v3,
3718t2 where (v1.a=v2.a) and (v1.b=v3.b) and ((v3.avg_c>170) or (v3.a<5))
3719and ((v1.avg_c<400) or (v1.a>1)) and (v2.min_c<200);
3720a	b	max_c	avg_c	a	b	min_c	a	b	avg_c	a	b	c	d
37218	33	404	213.6667	8	33	114	1	33	497.5000	2	3	207	207
37228	33	404	213.6667	8	33	114	1	33	497.5000	1	21	909	12
37238	33	404	213.6667	8	33	114	1	33	497.5000	7	13	312	406
37248	33	404	213.6667	8	33	114	1	33	497.5000	8	64	248	107
37258	33	404	213.6667	8	33	114	1	33	497.5000	6	20	315	279
37268	33	404	213.6667	8	33	114	1	33	497.5000	1	19	203	107
37278	33	404	213.6667	8	33	114	1	33	497.5000	8	80	800	314
37288	33	404	213.6667	8	33	114	1	33	497.5000	3	12	231	190
37298	33	404	213.6667	8	33	114	1	33	497.5000	6	23	303	909
3730select * from
3731(select a, b, max(c) as max_c, avg(c) as avg_c from t1
3732where t1.a>5 group by a,b having max_c < 707) v1,
3733(select a, b, min(c) as min_c from t1
3734where t1.a>5 group by a,b having min_c < 707) v2,
3735(select a, b, avg(c) as avg_c from t1
3736where t1.a<8 group by a,b) v3,
3737t2 where (v1.a=v2.a) and (v1.b=v3.b) and ((v3.avg_c>170) or (v3.a<5))
3738and ((v1.avg_c<400) or (v1.a>1)) and (v2.min_c<200);
3739a	b	max_c	avg_c	a	b	min_c	a	b	avg_c	a	b	c	d
37408	33	404	213.6667	8	33	114	1	33	497.5000	2	3	207	207
37418	33	404	213.6667	8	33	114	1	33	497.5000	1	21	909	12
37428	33	404	213.6667	8	33	114	1	33	497.5000	7	13	312	406
37438	33	404	213.6667	8	33	114	1	33	497.5000	8	64	248	107
37448	33	404	213.6667	8	33	114	1	33	497.5000	6	20	315	279
37458	33	404	213.6667	8	33	114	1	33	497.5000	1	19	203	107
37468	33	404	213.6667	8	33	114	1	33	497.5000	8	80	800	314
37478	33	404	213.6667	8	33	114	1	33	497.5000	3	12	231	190
37488	33	404	213.6667	8	33	114	1	33	497.5000	6	23	303	909
3749explain select * from
3750(select a, b, max(c) as max_c, avg(c) as avg_c from t1
3751where t1.a>5 group by a,b having max_c < 707) v1,
3752(select a, b, min(c) as min_c from t1
3753where t1.a>5 group by a,b having min_c < 707) v2,
3754(select a, b, avg(c) as avg_c from t1
3755where t1.a<8 group by a,b) v3,
3756t2 where (v1.a=v2.a) and (v1.b=v3.b) and ((v3.avg_c>170) or (v3.a<5))
3757and ((v1.avg_c<400) or (v1.a>1)) and (v2.min_c<200);
3758id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
37591	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9
37601	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
37611	PRIMARY	<derived3>	ref	key0	key0	5	v1.a	2	Using where
37621	PRIMARY	<derived4>	ref	key0	key0	5	v1.b	2	Using where
37634	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
37643	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
37652	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
3766explain format=json select * from
3767(select a, b, max(c) as max_c, avg(c) as avg_c from t1
3768where t1.a>5 group by a,b having max_c < 707) v1,
3769(select a, b, min(c) as min_c from t1
3770where t1.a>5 group by a,b having min_c < 707) v2,
3771(select a, b, avg(c) as avg_c from t1
3772where t1.a<8 group by a,b) v3,
3773t2 where (v1.a=v2.a) and (v1.b=v3.b) and ((v3.avg_c>170) or (v3.a<5))
3774and ((v1.avg_c<400) or (v1.a>1)) and (v2.min_c<200);
3775EXPLAIN
3776{
3777  "query_block": {
3778    "select_id": 1,
3779    "table": {
3780      "table_name": "t2",
3781      "access_type": "ALL",
3782      "rows": 9,
3783      "filtered": 100
3784    },
3785    "block-nl-join": {
3786      "table": {
3787        "table_name": "<derived2>",
3788        "access_type": "ALL",
3789        "rows": 20,
3790        "filtered": 100,
3791        "attached_condition": "v1.avg_c < 400 or v1.a > 1"
3792      },
3793      "buffer_type": "flat",
3794      "buffer_size": "238",
3795      "join_type": "BNL",
3796      "attached_condition": "(v1.avg_c < 400 or v1.a > 1) and v1.a is not null and v1.b is not null",
3797      "materialized": {
3798        "query_block": {
3799          "select_id": 2,
3800          "having_condition": "max_c < 707 and (avg_c < 400 or t1.a > 1)",
3801          "filesort": {
3802            "sort_key": "t1.a, t1.b",
3803            "temporary_table": {
3804              "table": {
3805                "table_name": "t1",
3806                "access_type": "ALL",
3807                "rows": 20,
3808                "filtered": 100,
3809                "attached_condition": "t1.a > 5"
3810              }
3811            }
3812          }
3813        }
3814      }
3815    },
3816    "table": {
3817      "table_name": "<derived3>",
3818      "access_type": "ref",
3819      "possible_keys": ["key0"],
3820      "key": "key0",
3821      "key_length": "5",
3822      "used_key_parts": ["a"],
3823      "ref": ["v1.a"],
3824      "rows": 2,
3825      "filtered": 100,
3826      "attached_condition": "v2.min_c < 200",
3827      "materialized": {
3828        "query_block": {
3829          "select_id": 3,
3830          "having_condition": "min_c < 707 and min_c < 200",
3831          "filesort": {
3832            "sort_key": "t1.a, t1.b",
3833            "temporary_table": {
3834              "table": {
3835                "table_name": "t1",
3836                "access_type": "ALL",
3837                "rows": 20,
3838                "filtered": 100,
3839                "attached_condition": "t1.a > 5"
3840              }
3841            }
3842          }
3843        }
3844      }
3845    },
3846    "table": {
3847      "table_name": "<derived4>",
3848      "access_type": "ref",
3849      "possible_keys": ["key0"],
3850      "key": "key0",
3851      "key_length": "5",
3852      "used_key_parts": ["b"],
3853      "ref": ["v1.b"],
3854      "rows": 2,
3855      "filtered": 100,
3856      "attached_condition": "v3.avg_c > 170 or v3.a < 5",
3857      "materialized": {
3858        "query_block": {
3859          "select_id": 4,
3860          "having_condition": "avg_c > 170 or t1.a < 5",
3861          "filesort": {
3862            "sort_key": "t1.a, t1.b",
3863            "temporary_table": {
3864              "table": {
3865                "table_name": "t1",
3866                "access_type": "ALL",
3867                "rows": 20,
3868                "filtered": 100,
3869                "attached_condition": "t1.a < 8"
3870              }
3871            }
3872          }
3873        }
3874      }
3875    }
3876  }
3877}
3878# extracted or formula : pushing into HAVING
3879# conjunctive subformula : pushing into WHERE
3880set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from
3881(select a, b, max(c) as max_c, avg(c) as avg_c from t1
3882group by a,b having max_c < 707) v1,
3883t2 where ((v1.a=1) or (v1.max_c<300)) and (v1.b>25);
3884a	b	max_c	avg_c	a	b	c	d
38855	27	132	132.0000	2	3	207	207
38865	27	132	132.0000	1	21	909	12
38875	27	132	132.0000	7	13	312	406
38885	27	132	132.0000	8	64	248	107
38895	27	132	132.0000	6	20	315	279
38905	27	132	132.0000	1	19	203	107
38915	27	132	132.0000	8	80	800	314
38925	27	132	132.0000	3	12	231	190
38935	27	132	132.0000	6	23	303	909
3894select * from
3895(select a, b, max(c) as max_c, avg(c) as avg_c from t1
3896group by a,b having max_c < 707) v1,
3897t2 where ((v1.a=1) or (v1.max_c<300)) and (v1.b>25);
3898a	b	max_c	avg_c	a	b	c	d
38995	27	132	132.0000	2	3	207	207
39005	27	132	132.0000	1	21	909	12
39015	27	132	132.0000	7	13	312	406
39025	27	132	132.0000	8	64	248	107
39035	27	132	132.0000	6	20	315	279
39045	27	132	132.0000	1	19	203	107
39055	27	132	132.0000	8	80	800	314
39065	27	132	132.0000	3	12	231	190
39075	27	132	132.0000	6	23	303	909
3908explain select * from
3909(select a, b, max(c) as max_c, avg(c) as avg_c from t1
3910group by a,b having max_c < 707) v1,
3911t2 where ((v1.a=1) or (v1.max_c<300)) and (v1.b>25);
3912id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
39131	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9
39141	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
39152	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
3916explain format=json select * from
3917(select a, b, max(c) as max_c, avg(c) as avg_c from t1
3918group by a,b having max_c < 707) v1,
3919t2 where ((v1.a=1) or (v1.max_c<300)) and (v1.b>25);
3920EXPLAIN
3921{
3922  "query_block": {
3923    "select_id": 1,
3924    "table": {
3925      "table_name": "t2",
3926      "access_type": "ALL",
3927      "rows": 9,
3928      "filtered": 100
3929    },
3930    "block-nl-join": {
3931      "table": {
3932        "table_name": "<derived2>",
3933        "access_type": "ALL",
3934        "rows": 20,
3935        "filtered": 100,
3936        "attached_condition": "(v1.a = 1 or v1.max_c < 300) and v1.b > 25"
3937      },
3938      "buffer_type": "flat",
3939      "buffer_size": "238",
3940      "join_type": "BNL",
3941      "attached_condition": "v1.a = 1 or v1.max_c < 300",
3942      "materialized": {
3943        "query_block": {
3944          "select_id": 2,
3945          "having_condition": "max_c < 707 and (t1.a = 1 or max_c < 300)",
3946          "filesort": {
3947            "sort_key": "t1.a, t1.b",
3948            "temporary_table": {
3949              "table": {
3950                "table_name": "t1",
3951                "access_type": "ALL",
3952                "rows": 20,
3953                "filtered": 100,
3954                "attached_condition": "t1.b > 25"
3955              }
3956            }
3957          }
3958        }
3959      }
3960    }
3961  }
3962}
3963# extracted and formula : pushing into WHERE
3964# conjunctive subformula : pushing into HAVING
3965set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from
3966(select a, b, max(c) as max_c, avg(c) as avg_c from t1
3967where t1.a>5 group by a,b having max_c < 707) v1,
3968t2 where (v1.a=t2.a) and (v1.max_c>300) and (v1.b<30);
3969a	b	max_c	avg_c	a	b	c	d
39706	20	315	279.3333	6	20	315	279
39716	20	315	279.3333	6	23	303	909
3972select * from
3973(select a, b, max(c) as max_c, avg(c) as avg_c from t1
3974where t1.a>5 group by a,b having max_c < 707) v1,
3975t2 where (v1.a=t2.a) and (v1.max_c>300) and (v1.b<30);
3976a	b	max_c	avg_c	a	b	c	d
39776	20	315	279.3333	6	20	315	279
39786	20	315	279.3333	6	23	303	909
3979explain select * from
3980(select a, b, max(c) as max_c, avg(c) as avg_c from t1
3981where t1.a>5 group by a,b having max_c < 707) v1,
3982t2 where (v1.a=t2.a) and (v1.max_c>300) and (v1.b<30);
3983id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
39841	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
39851	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.a	2	Using where
39862	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
3987explain format=json select * from
3988(select a, b, max(c) as max_c, avg(c) as avg_c from t1
3989where t1.a>5 group by a,b having max_c < 707) v1,
3990t2 where (v1.a=t2.a) and (v1.max_c>300) and (v1.b<30);
3991EXPLAIN
3992{
3993  "query_block": {
3994    "select_id": 1,
3995    "table": {
3996      "table_name": "t2",
3997      "access_type": "ALL",
3998      "rows": 9,
3999      "filtered": 100,
4000      "attached_condition": "t2.a is not null"
4001    },
4002    "table": {
4003      "table_name": "<derived2>",
4004      "access_type": "ref",
4005      "possible_keys": ["key0"],
4006      "key": "key0",
4007      "key_length": "5",
4008      "used_key_parts": ["a"],
4009      "ref": ["test.t2.a"],
4010      "rows": 2,
4011      "filtered": 100,
4012      "attached_condition": "v1.max_c > 300 and v1.b < 30",
4013      "materialized": {
4014        "query_block": {
4015          "select_id": 2,
4016          "having_condition": "max_c < 707 and max_c > 300",
4017          "filesort": {
4018            "sort_key": "t1.a, t1.b",
4019            "temporary_table": {
4020              "table": {
4021                "table_name": "t1",
4022                "access_type": "ALL",
4023                "rows": 20,
4024                "filtered": 100,
4025                "attached_condition": "t1.a > 5 and t1.b < 30"
4026              }
4027            }
4028          }
4029        }
4030      }
4031    }
4032  }
4033}
4034# using query with union
4035# conjunctive subformula : pushing into WHERE
4036# conjunctive subformulas : pushing into HAVING and WHERE
4037set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.a<5) and (v1.b=t2.b) and (t2.c>800)
4038union
4039select * from v1,t2 where (v1.max_c>100) and (v1.a>7) and (t2.d>800);
4040a	b	max_c	avg_c	a	b	c	d
40411	21	500	234.6000	1	21	909	12
40428	33	404	213.6667	6	23	303	909
4043select * from v1,t2 where (v1.a<5) and (v1.b=t2.b) and (t2.c>800)
4044union
4045select * from v1,t2 where (v1.max_c>100) and (v1.a>7) and (t2.d>800);
4046a	b	max_c	avg_c	a	b	c	d
40471	21	500	234.6000	1	21	909	12
40488	33	404	213.6667	6	23	303	909
4049explain select * from v1,t2 where (v1.a<5) and (v1.b=t2.b) and (t2.c>800)
4050union
4051select * from v1,t2 where (v1.max_c>100) and (v1.a>7) and (t2.d>800);
4052id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
40531	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
40541	PRIMARY	<derived3>	ref	key0	key0	5	test.t2.b	2	Using where
40553	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
40562	UNION	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
40572	UNION	<derived4>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
40584	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
4059NULL	UNION RESULT	<union1,2>	ALL	NULL	NULL	NULL	NULL	NULL
4060explain format=json select * from v1,t2 where (v1.a<5) and (v1.b=t2.b) and (t2.c>800)
4061union
4062select * from v1,t2 where (v1.max_c>100) and (v1.a>7) and (t2.d>800);
4063EXPLAIN
4064{
4065  "query_block": {
4066    "union_result": {
4067      "table_name": "<union1,2>",
4068      "access_type": "ALL",
4069      "query_specifications": [
4070        {
4071          "query_block": {
4072            "select_id": 1,
4073            "table": {
4074              "table_name": "t2",
4075              "access_type": "ALL",
4076              "rows": 9,
4077              "filtered": 100,
4078              "attached_condition": "t2.c > 800 and t2.b is not null"
4079            },
4080            "table": {
4081              "table_name": "<derived3>",
4082              "access_type": "ref",
4083              "possible_keys": ["key0"],
4084              "key": "key0",
4085              "key_length": "5",
4086              "used_key_parts": ["b"],
4087              "ref": ["test.t2.b"],
4088              "rows": 2,
4089              "filtered": 100,
4090              "attached_condition": "v1.a < 5",
4091              "materialized": {
4092                "query_block": {
4093                  "select_id": 3,
4094                  "having_condition": "max_c < 707",
4095                  "filesort": {
4096                    "sort_key": "t1.a, t1.b",
4097                    "temporary_table": {
4098                      "table": {
4099                        "table_name": "t1",
4100                        "access_type": "ALL",
4101                        "rows": 20,
4102                        "filtered": 100,
4103                        "attached_condition": "t1.a < 5"
4104                      }
4105                    }
4106                  }
4107                }
4108              }
4109            }
4110          }
4111        },
4112        {
4113          "query_block": {
4114            "select_id": 2,
4115            "operation": "UNION",
4116            "table": {
4117              "table_name": "t2",
4118              "access_type": "ALL",
4119              "rows": 9,
4120              "filtered": 100,
4121              "attached_condition": "t2.d > 800"
4122            },
4123            "block-nl-join": {
4124              "table": {
4125                "table_name": "<derived4>",
4126                "access_type": "ALL",
4127                "rows": 20,
4128                "filtered": 100,
4129                "attached_condition": "v1.max_c > 100 and v1.a > 7"
4130              },
4131              "buffer_type": "flat",
4132              "buffer_size": "238",
4133              "join_type": "BNL",
4134              "materialized": {
4135                "query_block": {
4136                  "select_id": 4,
4137                  "having_condition": "max_c < 707 and max_c > 100",
4138                  "filesort": {
4139                    "sort_key": "t1.a, t1.b",
4140                    "temporary_table": {
4141                      "table": {
4142                        "table_name": "t1",
4143                        "access_type": "ALL",
4144                        "rows": 20,
4145                        "filtered": 100,
4146                        "attached_condition": "t1.a > 7"
4147                      }
4148                    }
4149                  }
4150                }
4151              }
4152            }
4153          }
4154        }
4155      ]
4156    }
4157  }
4158}
4159# using query with union
4160# extracted and formula : pushing into WHERE
4161# extracted or formula : pushing into HAVING
4162# pushing equalities
4163set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.a<5) and (v1.b=t2.b) and (v1.b=19)
4164union
4165select * from v1,t2 where ((v1.max_c>400) or (v1.avg_c>270)) and (v1.a<t2.a);
4166a	b	max_c	avg_c	a	b	c	d
41671	19	107	107.0000	1	19	203	107
41681	21	500	234.6000	2	3	207	207
41691	21	500	234.6000	7	13	312	406
41701	21	500	234.6000	8	64	248	107
41711	21	500	234.6000	6	20	315	279
41721	21	500	234.6000	8	80	800	314
41731	21	500	234.6000	3	12	231	190
41741	21	500	234.6000	6	23	303	909
41756	20	315	279.3333	7	13	312	406
41766	20	315	279.3333	8	64	248	107
41776	20	315	279.3333	8	80	800	314
4178select * from v1,t2 where (v1.a<5) and (v1.b=t2.b) and (v1.b=19)
4179union
4180select * from v1,t2 where ((v1.max_c>400) or (v1.avg_c>270)) and (v1.a<t2.a);
4181a	b	max_c	avg_c	a	b	c	d
41821	19	107	107.0000	1	19	203	107
41831	21	500	234.6000	2	3	207	207
41841	21	500	234.6000	7	13	312	406
41851	21	500	234.6000	8	64	248	107
41861	21	500	234.6000	6	20	315	279
41871	21	500	234.6000	8	80	800	314
41881	21	500	234.6000	3	12	231	190
41891	21	500	234.6000	6	23	303	909
41906	20	315	279.3333	7	13	312	406
41916	20	315	279.3333	8	64	248	107
41926	20	315	279.3333	8	80	800	314
4193explain select * from v1,t2 where (v1.a<5) and (v1.b=t2.b) and (v1.b=19)
4194union
4195select * from v1,t2 where ((v1.max_c>400) or (v1.avg_c>270)) and (v1.a<t2.a);
4196id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
41971	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
41981	PRIMARY	<derived3>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
41993	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
42002	UNION	t2	ALL	NULL	NULL	NULL	NULL	9
42012	UNION	<derived4>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
42024	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using temporary; Using filesort
4203NULL	UNION RESULT	<union1,2>	ALL	NULL	NULL	NULL	NULL	NULL
4204explain format=json select * from v1,t2 where (v1.a<5) and (v1.b=t2.b) and (v1.b=19)
4205union
4206select * from v1,t2 where ((v1.max_c>400) or (v1.avg_c>270)) and (v1.a<t2.a);
4207EXPLAIN
4208{
4209  "query_block": {
4210    "union_result": {
4211      "table_name": "<union1,2>",
4212      "access_type": "ALL",
4213      "query_specifications": [
4214        {
4215          "query_block": {
4216            "select_id": 1,
4217            "table": {
4218              "table_name": "t2",
4219              "access_type": "ALL",
4220              "rows": 9,
4221              "filtered": 100,
4222              "attached_condition": "t2.b = 19"
4223            },
4224            "block-nl-join": {
4225              "table": {
4226                "table_name": "<derived3>",
4227                "access_type": "ALL",
4228                "rows": 20,
4229                "filtered": 100,
4230                "attached_condition": "v1.b = 19 and v1.a < 5"
4231              },
4232              "buffer_type": "flat",
4233              "buffer_size": "238",
4234              "join_type": "BNL",
4235              "materialized": {
4236                "query_block": {
4237                  "select_id": 3,
4238                  "having_condition": "max_c < 707",
4239                  "filesort": {
4240                    "sort_key": "t1.a",
4241                    "temporary_table": {
4242                      "table": {
4243                        "table_name": "t1",
4244                        "access_type": "ALL",
4245                        "rows": 20,
4246                        "filtered": 100,
4247                        "attached_condition": "t1.b = 19 and t1.a < 5"
4248                      }
4249                    }
4250                  }
4251                }
4252              }
4253            }
4254          }
4255        },
4256        {
4257          "query_block": {
4258            "select_id": 2,
4259            "operation": "UNION",
4260            "table": {
4261              "table_name": "t2",
4262              "access_type": "ALL",
4263              "rows": 9,
4264              "filtered": 100
4265            },
4266            "block-nl-join": {
4267              "table": {
4268                "table_name": "<derived4>",
4269                "access_type": "ALL",
4270                "rows": 20,
4271                "filtered": 100,
4272                "attached_condition": "v1.max_c > 400 or v1.avg_c > 270"
4273              },
4274              "buffer_type": "flat",
4275              "buffer_size": "238",
4276              "join_type": "BNL",
4277              "attached_condition": "(v1.max_c > 400 or v1.avg_c > 270) and v1.a < t2.a",
4278              "materialized": {
4279                "query_block": {
4280                  "select_id": 4,
4281                  "having_condition": "max_c < 707 and (max_c > 400 or avg_c > 270)",
4282                  "filesort": {
4283                    "sort_key": "t1.a, t1.b",
4284                    "temporary_table": {
4285                      "table": {
4286                        "table_name": "t1",
4287                        "access_type": "ALL",
4288                        "rows": 20,
4289                        "filtered": 100
4290                      }
4291                    }
4292                  }
4293                }
4294              }
4295            }
4296          }
4297        }
4298      ]
4299    }
4300  }
4301}
4302# using query with union
4303# extracted or formula : pushing into HAVING
4304# extracted or formula : pushing into WHERE
4305# pushing equalities
4306set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where
4307((t2.a=v1.a) or (v1.b=t2.b)) and ((v1.a=1) or (v1.a=6))
4308union
4309select * from v1,t2 where ((v1.a>3) and (v1.b>27)) or (v1.max_c>550);
4310a	b	max_c	avg_c	a	b	c	d
43111	19	107	107.0000	1	21	909	12
43121	19	107	107.0000	1	19	203	107
43131	21	500	234.6000	1	21	909	12
43141	21	500	234.6000	1	19	203	107
43156	20	315	279.3333	6	20	315	279
43166	20	315	279.3333	6	23	303	909
43178	33	404	213.6667	2	3	207	207
43188	33	404	213.6667	1	21	909	12
43198	33	404	213.6667	7	13	312	406
43208	33	404	213.6667	8	64	248	107
43218	33	404	213.6667	6	20	315	279
43228	33	404	213.6667	1	19	203	107
43238	33	404	213.6667	8	80	800	314
43248	33	404	213.6667	3	12	231	190
43258	33	404	213.6667	6	23	303	909
4326select * from v1,t2 where
4327((t2.a=v1.a) or (v1.b=t2.b)) and ((v1.a=1) or (v1.a=6))
4328union
4329select * from v1,t2 where ((v1.a>3) and (v1.b>27)) or (v1.max_c>550);
4330a	b	max_c	avg_c	a	b	c	d
43311	19	107	107.0000	1	21	909	12
43321	19	107	107.0000	1	19	203	107
43331	21	500	234.6000	1	21	909	12
43341	21	500	234.6000	1	19	203	107
43356	20	315	279.3333	6	20	315	279
43366	20	315	279.3333	6	23	303	909
43378	33	404	213.6667	2	3	207	207
43388	33	404	213.6667	1	21	909	12
43398	33	404	213.6667	7	13	312	406
43408	33	404	213.6667	8	64	248	107
43418	33	404	213.6667	6	20	315	279
43428	33	404	213.6667	1	19	203	107
43438	33	404	213.6667	8	80	800	314
43448	33	404	213.6667	3	12	231	190
43458	33	404	213.6667	6	23	303	909
4346explain select * from v1,t2 where
4347((t2.a=v1.a) or (v1.b=t2.b)) and ((v1.a=1) or (v1.a=6))
4348union
4349select * from v1,t2 where ((v1.a>3) and (v1.b>27)) or (v1.max_c>550);
4350id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
43511	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9
43521	PRIMARY	<derived3>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
43533	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
43542	UNION	t2	ALL	NULL	NULL	NULL	NULL	9
43552	UNION	<derived4>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
43564	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using temporary; Using filesort
4357NULL	UNION RESULT	<union1,2>	ALL	NULL	NULL	NULL	NULL	NULL
4358explain format=json select * from v1,t2 where
4359((t2.a=v1.a) or (v1.b=t2.b)) and ((v1.a=1) or (v1.a=6))
4360union
4361select * from v1,t2 where ((v1.a>3) and (v1.b>27)) or (v1.max_c>550);
4362EXPLAIN
4363{
4364  "query_block": {
4365    "union_result": {
4366      "table_name": "<union1,2>",
4367      "access_type": "ALL",
4368      "query_specifications": [
4369        {
4370          "query_block": {
4371            "select_id": 1,
4372            "table": {
4373              "table_name": "t2",
4374              "access_type": "ALL",
4375              "rows": 9,
4376              "filtered": 100
4377            },
4378            "block-nl-join": {
4379              "table": {
4380                "table_name": "<derived3>",
4381                "access_type": "ALL",
4382                "rows": 20,
4383                "filtered": 100,
4384                "attached_condition": "v1.a = 1 or v1.a = 6"
4385              },
4386              "buffer_type": "flat",
4387              "buffer_size": "238",
4388              "join_type": "BNL",
4389              "attached_condition": "(v1.a = t2.a or v1.b = t2.b) and (v1.a = 1 or v1.a = 6)",
4390              "materialized": {
4391                "query_block": {
4392                  "select_id": 3,
4393                  "having_condition": "max_c < 707",
4394                  "filesort": {
4395                    "sort_key": "t1.a, t1.b",
4396                    "temporary_table": {
4397                      "table": {
4398                        "table_name": "t1",
4399                        "access_type": "ALL",
4400                        "rows": 20,
4401                        "filtered": 100,
4402                        "attached_condition": "t1.a = 1 or t1.a = 6"
4403                      }
4404                    }
4405                  }
4406                }
4407              }
4408            }
4409          }
4410        },
4411        {
4412          "query_block": {
4413            "select_id": 2,
4414            "operation": "UNION",
4415            "table": {
4416              "table_name": "t2",
4417              "access_type": "ALL",
4418              "rows": 9,
4419              "filtered": 100
4420            },
4421            "block-nl-join": {
4422              "table": {
4423                "table_name": "<derived4>",
4424                "access_type": "ALL",
4425                "rows": 20,
4426                "filtered": 100,
4427                "attached_condition": "v1.a > 3 and v1.b > 27 or v1.max_c > 550"
4428              },
4429              "buffer_type": "flat",
4430              "buffer_size": "238",
4431              "join_type": "BNL",
4432              "attached_condition": "v1.a > 3 and v1.b > 27 or v1.max_c > 550",
4433              "materialized": {
4434                "query_block": {
4435                  "select_id": 4,
4436                  "having_condition": "max_c < 707 and (t1.a > 3 and t1.b > 27 or max_c > 550)",
4437                  "filesort": {
4438                    "sort_key": "t1.a, t1.b",
4439                    "temporary_table": {
4440                      "table": {
4441                        "table_name": "t1",
4442                        "access_type": "ALL",
4443                        "rows": 20,
4444                        "filtered": 100
4445                      }
4446                    }
4447                  }
4448                }
4449              }
4450            }
4451          }
4452        }
4453      ]
4454    }
4455  }
4456}
4457# using query with union
4458# extracted or formula : pushing into HAVING
4459# conjunctive subformulas : pushing into WHERE
4460# pushing equalities
4461set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where
4462((v1.a=1) and (v1.a=t2.a)) and ((v1.max_c<500) or (v1.avg_c>500))
4463union
4464select * from v2,t2 where
4465((v2.a<t2.b) or (v2.max_c>200)) and (v2.b>10) and (t2.a<2)
4466union
4467select * from v2,t2 where
4468(v2.max_c=t2.c) and (v2.b<10);
4469a	b	max_c	avg_c	a	b	c	d
44701	19	107	107.0000	1	21	909	12
44711	19	107	107.0000	1	19	203	107
44726	20	315	279.3333	1	21	909	12
44736	20	315	279.3333	1	19	203	107
44748	33	404	213.6667	1	21	909	12
44758	33	404	213.6667	1	19	203	107
4476select * from v1,t2 where
4477((v1.a=1) and (v1.a=t2.a)) and ((v1.max_c<500) or (v1.avg_c>500))
4478union
4479select * from v2,t2 where
4480((v2.a<t2.b) or (v2.max_c>200)) and (v2.b>10) and (t2.a<2)
4481union
4482select * from v2,t2 where
4483(v2.max_c=t2.c) and (v2.b<10);
4484a	b	max_c	avg_c	a	b	c	d
44851	19	107	107.0000	1	21	909	12
44861	19	107	107.0000	1	19	203	107
44876	20	315	279.3333	1	21	909	12
44886	20	315	279.3333	1	19	203	107
44898	33	404	213.6667	1	21	909	12
44908	33	404	213.6667	1	19	203	107
4491explain select * from v1,t2 where
4492((v1.a=1) and (v1.a=t2.a)) and ((v1.max_c<500) or (v1.avg_c>500))
4493union
4494select * from v2,t2 where
4495((v2.a<t2.b) or (v2.max_c>200)) and (v2.b>10) and (t2.a<2)
4496union
4497select * from v2,t2 where
4498(v2.max_c=t2.c) and (v2.b<10);
4499id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
45001	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
45011	PRIMARY	<derived4>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
45024	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
45032	UNION	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
45042	UNION	<derived5>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
45055	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
45063	UNION	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
45073	UNION	<derived6>	ref	key0	key0	5	test.t2.c	2	Using where
45086	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
4509NULL	UNION RESULT	<union1,2,3>	ALL	NULL	NULL	NULL	NULL	NULL
4510explain format=json select * from v1,t2 where
4511((v1.a=1) and (v1.a=t2.a)) and ((v1.max_c<500) or (v1.avg_c>500))
4512union
4513select * from v2,t2 where
4514((v2.a<t2.b) or (v2.max_c>200)) and (v2.b>10) and (t2.a<2)
4515union
4516select * from v2,t2 where
4517(v2.max_c=t2.c) and (v2.b<10);
4518EXPLAIN
4519{
4520  "query_block": {
4521    "union_result": {
4522      "table_name": "<union1,2,3>",
4523      "access_type": "ALL",
4524      "query_specifications": [
4525        {
4526          "query_block": {
4527            "select_id": 1,
4528            "table": {
4529              "table_name": "t2",
4530              "access_type": "ALL",
4531              "rows": 9,
4532              "filtered": 100,
4533              "attached_condition": "t2.a = 1"
4534            },
4535            "block-nl-join": {
4536              "table": {
4537                "table_name": "<derived4>",
4538                "access_type": "ALL",
4539                "rows": 20,
4540                "filtered": 100,
4541                "attached_condition": "v1.a = 1 and (v1.max_c < 500 or v1.avg_c > 500)"
4542              },
4543              "buffer_type": "flat",
4544              "buffer_size": "238",
4545              "join_type": "BNL",
4546              "attached_condition": "v1.max_c < 500 or v1.avg_c > 500",
4547              "materialized": {
4548                "query_block": {
4549                  "select_id": 4,
4550                  "having_condition": "max_c < 707 and (max_c < 500 or avg_c > 500)",
4551                  "filesort": {
4552                    "sort_key": "t1.b",
4553                    "temporary_table": {
4554                      "table": {
4555                        "table_name": "t1",
4556                        "access_type": "ALL",
4557                        "rows": 20,
4558                        "filtered": 100,
4559                        "attached_condition": "t1.a = 1"
4560                      }
4561                    }
4562                  }
4563                }
4564              }
4565            }
4566          }
4567        },
4568        {
4569          "query_block": {
4570            "select_id": 2,
4571            "operation": "UNION",
4572            "table": {
4573              "table_name": "t2",
4574              "access_type": "ALL",
4575              "rows": 9,
4576              "filtered": 100,
4577              "attached_condition": "t2.a < 2"
4578            },
4579            "block-nl-join": {
4580              "table": {
4581                "table_name": "<derived5>",
4582                "access_type": "ALL",
4583                "rows": 20,
4584                "filtered": 100,
4585                "attached_condition": "v2.b > 10"
4586              },
4587              "buffer_type": "flat",
4588              "buffer_size": "238",
4589              "join_type": "BNL",
4590              "attached_condition": "v2.a < t2.b or v2.max_c > 200",
4591              "materialized": {
4592                "query_block": {
4593                  "select_id": 5,
4594                  "having_condition": "max_c < 707",
4595                  "filesort": {
4596                    "sort_key": "t1.a, t1.b",
4597                    "temporary_table": {
4598                      "table": {
4599                        "table_name": "t1",
4600                        "access_type": "ALL",
4601                        "rows": 20,
4602                        "filtered": 100,
4603                        "attached_condition": "t1.a > 5 and t1.b > 10"
4604                      }
4605                    }
4606                  }
4607                }
4608              }
4609            }
4610          }
4611        },
4612        {
4613          "query_block": {
4614            "select_id": 3,
4615            "operation": "UNION",
4616            "table": {
4617              "table_name": "t2",
4618              "access_type": "ALL",
4619              "rows": 9,
4620              "filtered": 100,
4621              "attached_condition": "t2.c is not null"
4622            },
4623            "table": {
4624              "table_name": "<derived6>",
4625              "access_type": "ref",
4626              "possible_keys": ["key0"],
4627              "key": "key0",
4628              "key_length": "5",
4629              "used_key_parts": ["max_c"],
4630              "ref": ["test.t2.c"],
4631              "rows": 2,
4632              "filtered": 100,
4633              "attached_condition": "v2.b < 10",
4634              "materialized": {
4635                "query_block": {
4636                  "select_id": 6,
4637                  "having_condition": "max_c < 707",
4638                  "filesort": {
4639                    "sort_key": "t1.a, t1.b",
4640                    "temporary_table": {
4641                      "table": {
4642                        "table_name": "t1",
4643                        "access_type": "ALL",
4644                        "rows": 20,
4645                        "filtered": 100,
4646                        "attached_condition": "t1.a > 5 and t1.b < 10"
4647                      }
4648                    }
4649                  }
4650                }
4651              }
4652            }
4653          }
4654        }
4655      ]
4656    }
4657  }
4658}
4659# using derived table with union
4660# conjunctive subformulas : pushing into WHERE and HAVING
4661set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v_union,t2 where (v_union.a<3) and (v_union.c>100);
4662a	b	c	a	b	c	d
46631	19	107	2	3	207	207
46641	19	107	1	21	909	12
46651	19	107	7	13	312	406
46661	19	107	8	64	248	107
46671	19	107	6	20	315	279
46681	19	107	1	19	203	107
46691	19	107	8	80	800	314
46701	19	107	3	12	231	190
46711	19	107	6	23	303	909
4672select * from v_union,t2 where (v_union.a<3) and (v_union.c>100);
4673a	b	c	a	b	c	d
46741	19	107	2	3	207	207
46751	19	107	1	21	909	12
46761	19	107	7	13	312	406
46771	19	107	8	64	248	107
46781	19	107	6	20	315	279
46791	19	107	1	19	203	107
46801	19	107	8	80	800	314
46811	19	107	3	12	231	190
46821	19	107	6	23	303	909
4683explain select * from v_union,t2 where (v_union.a<3) and (v_union.c>100);
4684id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
46851	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9
46861	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	40	Using where; Using join buffer (flat, BNL join)
46872	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
46883	UNION	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
4689NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL
4690explain format=json select * from v_union,t2 where (v_union.a<3) and (v_union.c>100);
4691EXPLAIN
4692{
4693  "query_block": {
4694    "select_id": 1,
4695    "table": {
4696      "table_name": "t2",
4697      "access_type": "ALL",
4698      "rows": 9,
4699      "filtered": 100
4700    },
4701    "block-nl-join": {
4702      "table": {
4703        "table_name": "<derived2>",
4704        "access_type": "ALL",
4705        "rows": 40,
4706        "filtered": 100,
4707        "attached_condition": "v_union.a < 3 and v_union.c > 100"
4708      },
4709      "buffer_type": "flat",
4710      "buffer_size": "238",
4711      "join_type": "BNL",
4712      "materialized": {
4713        "query_block": {
4714          "union_result": {
4715            "table_name": "<union2,3>",
4716            "access_type": "ALL",
4717            "query_specifications": [
4718              {
4719                "query_block": {
4720                  "select_id": 2,
4721                  "having_condition": "c > 109 and c > 100",
4722                  "filesort": {
4723                    "sort_key": "t1.a, t1.b",
4724                    "temporary_table": {
4725                      "table": {
4726                        "table_name": "t1",
4727                        "access_type": "ALL",
4728                        "rows": 20,
4729                        "filtered": 100,
4730                        "attached_condition": "t1.a < 10 and t1.a < 3"
4731                      }
4732                    }
4733                  }
4734                }
4735              },
4736              {
4737                "query_block": {
4738                  "select_id": 3,
4739                  "operation": "UNION",
4740                  "having_condition": "c < 300 and c > 100",
4741                  "filesort": {
4742                    "sort_key": "t1.a, t1.b",
4743                    "temporary_table": {
4744                      "table": {
4745                        "table_name": "t1",
4746                        "access_type": "ALL",
4747                        "rows": 20,
4748                        "filtered": 100,
4749                        "attached_condition": "t1.b > 10 and t1.a < 3"
4750                      }
4751                    }
4752                  }
4753                }
4754              }
4755            ]
4756          }
4757        }
4758      }
4759    }
4760  }
4761}
4762# using derived table with union
4763# conjunctive subformula : pushing into WHERE
4764# extracted or formula : pushing into HAVING
4765set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v_union,t2 where
4766((v_union.a<2) or (v_union.c>800)) and (v_union.b>12);
4767a	b	c	a	b	c	d
47681	19	107	2	3	207	207
47691	19	107	1	21	909	12
47701	19	107	7	13	312	406
47711	19	107	8	64	248	107
47721	19	107	6	20	315	279
47731	19	107	1	19	203	107
47741	19	107	8	80	800	314
47751	19	107	3	12	231	190
47761	19	107	6	23	303	909
4777select * from v_union,t2 where
4778((v_union.a<2) or (v_union.c>800)) and (v_union.b>12);
4779a	b	c	a	b	c	d
47801	19	107	2	3	207	207
47811	19	107	1	21	909	12
47821	19	107	7	13	312	406
47831	19	107	8	64	248	107
47841	19	107	6	20	315	279
47851	19	107	1	19	203	107
47861	19	107	8	80	800	314
47871	19	107	3	12	231	190
47881	19	107	6	23	303	909
4789explain select * from v_union,t2 where
4790((v_union.a<2) or (v_union.c>800)) and (v_union.b>12);
4791id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
47921	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9
47931	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	40	Using where; Using join buffer (flat, BNL join)
47942	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
47953	UNION	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
4796NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL
4797explain format=json select * from v_union,t2 where
4798((v_union.a<2) or (v_union.c>800)) and (v_union.b>12);
4799EXPLAIN
4800{
4801  "query_block": {
4802    "select_id": 1,
4803    "table": {
4804      "table_name": "t2",
4805      "access_type": "ALL",
4806      "rows": 9,
4807      "filtered": 100
4808    },
4809    "block-nl-join": {
4810      "table": {
4811        "table_name": "<derived2>",
4812        "access_type": "ALL",
4813        "rows": 40,
4814        "filtered": 100,
4815        "attached_condition": "(v_union.a < 2 or v_union.c > 800) and v_union.b > 12"
4816      },
4817      "buffer_type": "flat",
4818      "buffer_size": "238",
4819      "join_type": "BNL",
4820      "attached_condition": "v_union.a < 2 or v_union.c > 800",
4821      "materialized": {
4822        "query_block": {
4823          "union_result": {
4824            "table_name": "<union2,3>",
4825            "access_type": "ALL",
4826            "query_specifications": [
4827              {
4828                "query_block": {
4829                  "select_id": 2,
4830                  "having_condition": "c > 109 and (t1.a < 2 or c > 800)",
4831                  "filesort": {
4832                    "sort_key": "t1.a, t1.b",
4833                    "temporary_table": {
4834                      "table": {
4835                        "table_name": "t1",
4836                        "access_type": "ALL",
4837                        "rows": 20,
4838                        "filtered": 100,
4839                        "attached_condition": "t1.a < 10 and t1.b > 12"
4840                      }
4841                    }
4842                  }
4843                }
4844              },
4845              {
4846                "query_block": {
4847                  "select_id": 3,
4848                  "operation": "UNION",
4849                  "having_condition": "c < 300 and (t1.a < 2 or c > 800)",
4850                  "filesort": {
4851                    "sort_key": "t1.a, t1.b",
4852                    "temporary_table": {
4853                      "table": {
4854                        "table_name": "t1",
4855                        "access_type": "ALL",
4856                        "rows": 20,
4857                        "filtered": 100,
4858                        "attached_condition": "t1.b > 10 and t1.b > 12"
4859                      }
4860                    }
4861                  }
4862                }
4863              }
4864            ]
4865          }
4866        }
4867      }
4868    }
4869  }
4870}
4871# using derived table with union
4872# conjunctive subformula : pushing into HAVING
4873# conjunctive subformula : pushing into WHERE
4874# pushing equalities
4875set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v_union,t2 where
4876(v_union.a=1) and (v_union.a=t2.a) and (v_union.c<200);
4877a	b	c	a	b	c	d
48781	19	107	1	21	909	12
48791	19	107	1	19	203	107
4880select * from v_union,t2 where
4881(v_union.a=1) and (v_union.a=t2.a) and (v_union.c<200);
4882a	b	c	a	b	c	d
48831	19	107	1	21	909	12
48841	19	107	1	19	203	107
4885explain select * from v_union,t2 where
4886(v_union.a=1) and (v_union.a=t2.a) and (v_union.c<200);
4887id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
48881	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
48891	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	40	Using where; Using join buffer (flat, BNL join)
48902	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
48913	UNION	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
4892NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL
4893explain format=json select * from v_union,t2 where
4894(v_union.a=1) and (v_union.a=t2.a) and (v_union.c<200);
4895EXPLAIN
4896{
4897  "query_block": {
4898    "select_id": 1,
4899    "table": {
4900      "table_name": "t2",
4901      "access_type": "ALL",
4902      "rows": 9,
4903      "filtered": 100,
4904      "attached_condition": "t2.a = 1"
4905    },
4906    "block-nl-join": {
4907      "table": {
4908        "table_name": "<derived2>",
4909        "access_type": "ALL",
4910        "rows": 40,
4911        "filtered": 100,
4912        "attached_condition": "v_union.a = 1 and v_union.c < 200"
4913      },
4914      "buffer_type": "flat",
4915      "buffer_size": "238",
4916      "join_type": "BNL",
4917      "materialized": {
4918        "query_block": {
4919          "union_result": {
4920            "table_name": "<union2,3>",
4921            "access_type": "ALL",
4922            "query_specifications": [
4923              {
4924                "query_block": {
4925                  "select_id": 2,
4926                  "having_condition": "c > 109 and c < 200",
4927                  "filesort": {
4928                    "sort_key": "t1.b",
4929                    "temporary_table": {
4930                      "table": {
4931                        "table_name": "t1",
4932                        "access_type": "ALL",
4933                        "rows": 20,
4934                        "filtered": 100,
4935                        "attached_condition": "t1.a = 1"
4936                      }
4937                    }
4938                  }
4939                }
4940              },
4941              {
4942                "query_block": {
4943                  "select_id": 3,
4944                  "operation": "UNION",
4945                  "having_condition": "c < 300 and c < 200",
4946                  "filesort": {
4947                    "sort_key": "t1.b",
4948                    "temporary_table": {
4949                      "table": {
4950                        "table_name": "t1",
4951                        "access_type": "ALL",
4952                        "rows": 20,
4953                        "filtered": 100,
4954                        "attached_condition": "t1.a = 1 and t1.b > 10"
4955                      }
4956                    }
4957                  }
4958                }
4959              }
4960            ]
4961          }
4962        }
4963      }
4964    }
4965  }
4966}
4967set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v_char as v,t2_char as t where
4968(v.a=t.a) and (v.b='Vika') and (v.max_c>2);
4969a	b	max_c	a	b	c
4970c	Vika	7	c	Vinny	3
4971c	Vika	7	c	Inga	9
4972c	Vika	7	c	Ivan	2
4973c	Vika	7	c	Inga	2
4974select * from v_char as v,t2_char as t where
4975(v.a=t.a) and (v.b='Vika') and (v.max_c>2);
4976a	b	max_c	a	b	c
4977c	Vika	7	c	Vinny	3
4978c	Vika	7	c	Inga	9
4979c	Vika	7	c	Ivan	2
4980c	Vika	7	c	Inga	2
4981explain select * from v_char as v,t2_char as t where
4982(v.a=t.a) and (v.b='Vika') and (v.max_c>2);
4983id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
49841	PRIMARY	t	ALL	NULL	NULL	NULL	NULL	12	Using where
49851	PRIMARY	<derived2>	ref	key0	key0	2	test.t.a	2	Using where
49862	DERIVED	t1_char	ALL	NULL	NULL	NULL	NULL	12	Using where; Using temporary; Using filesort
4987explain format=json select * from v_char as v,t2_char as t where
4988(v.a=t.a) and (v.b='Vika') and (v.max_c>2);
4989EXPLAIN
4990{
4991  "query_block": {
4992    "select_id": 1,
4993    "table": {
4994      "table_name": "t",
4995      "access_type": "ALL",
4996      "rows": 12,
4997      "filtered": 100,
4998      "attached_condition": "t.a is not null"
4999    },
5000    "table": {
5001      "table_name": "<derived2>",
5002      "access_type": "ref",
5003      "possible_keys": ["key0"],
5004      "key": "key0",
5005      "key_length": "2",
5006      "used_key_parts": ["a"],
5007      "ref": ["test.t.a"],
5008      "rows": 2,
5009      "filtered": 100,
5010      "attached_condition": "v.b = 'Vika' and v.max_c > 2",
5011      "materialized": {
5012        "query_block": {
5013          "select_id": 2,
5014          "having_condition": "max_c < 9 and max_c > 2",
5015          "filesort": {
5016            "sort_key": "t1_char.a",
5017            "temporary_table": {
5018              "table": {
5019                "table_name": "t1_char",
5020                "access_type": "ALL",
5021                "rows": 12,
5022                "filtered": 100,
5023                "attached_condition": "t1_char.b = 'Vika'"
5024              }
5025            }
5026          }
5027        }
5028      }
5029    }
5030  }
5031}
5032# using derived table with union
5033# using several derived tables : pushing in all tables
5034# conjunctive subformula : pushing into WHERE using equalities
5035# pushing equalities
5036set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v_union,v1,t2 where
5037(v_union.a=v1.a) and (v1.a=t2.a) and (t2.a=1)
5038and ((v_union.c>800) or (v1.max_c>200));
5039a	b	c	a	b	max_c	avg_c	a	b	c	d
50401	19	107	1	21	500	234.6000	1	21	909	12
50411	19	107	1	21	500	234.6000	1	19	203	107
5042select * from v_union,v1,t2 where
5043(v_union.a=v1.a) and (v1.a=t2.a) and (t2.a=1)
5044and ((v_union.c>800) or (v1.max_c>200));
5045a	b	c	a	b	max_c	avg_c	a	b	c	d
50461	19	107	1	21	500	234.6000	1	21	909	12
50471	19	107	1	21	500	234.6000	1	19	203	107
5048explain select * from v_union,v1,t2 where
5049(v_union.a=v1.a) and (v1.a=t2.a) and (t2.a=1)
5050and ((v_union.c>800) or (v1.max_c>200));
5051id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
50521	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
50531	PRIMARY	<derived4>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
50541	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	40	Using where; Using join buffer (incremental, BNL join)
50554	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
50562	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
50573	UNION	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
5058NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL
5059explain format=json select * from v_union,v1,t2 where
5060(v_union.a=v1.a) and (v1.a=t2.a) and (t2.a=1)
5061and ((v_union.c>800) or (v1.max_c>200));
5062EXPLAIN
5063{
5064  "query_block": {
5065    "select_id": 1,
5066    "table": {
5067      "table_name": "t2",
5068      "access_type": "ALL",
5069      "rows": 9,
5070      "filtered": 100,
5071      "attached_condition": "t2.a = 1"
5072    },
5073    "block-nl-join": {
5074      "table": {
5075        "table_name": "<derived4>",
5076        "access_type": "ALL",
5077        "rows": 20,
5078        "filtered": 100,
5079        "attached_condition": "v1.a = 1"
5080      },
5081      "buffer_type": "flat",
5082      "buffer_size": "238",
5083      "join_type": "BNL",
5084      "materialized": {
5085        "query_block": {
5086          "select_id": 4,
5087          "having_condition": "max_c < 707",
5088          "filesort": {
5089            "sort_key": "t1.b",
5090            "temporary_table": {
5091              "table": {
5092                "table_name": "t1",
5093                "access_type": "ALL",
5094                "rows": 20,
5095                "filtered": 100,
5096                "attached_condition": "t1.a = 1"
5097              }
5098            }
5099          }
5100        }
5101      }
5102    },
5103    "block-nl-join": {
5104      "table": {
5105        "table_name": "<derived2>",
5106        "access_type": "ALL",
5107        "rows": 40,
5108        "filtered": 100,
5109        "attached_condition": "v_union.a = 1"
5110      },
5111      "buffer_type": "incremental",
5112      "buffer_size": "4Kb",
5113      "join_type": "BNL",
5114      "attached_condition": "v_union.c > 800 or v1.max_c > 200",
5115      "materialized": {
5116        "query_block": {
5117          "union_result": {
5118            "table_name": "<union2,3>",
5119            "access_type": "ALL",
5120            "query_specifications": [
5121              {
5122                "query_block": {
5123                  "select_id": 2,
5124                  "having_condition": "c > 109",
5125                  "filesort": {
5126                    "sort_key": "t1.b",
5127                    "temporary_table": {
5128                      "table": {
5129                        "table_name": "t1",
5130                        "access_type": "ALL",
5131                        "rows": 20,
5132                        "filtered": 100,
5133                        "attached_condition": "t1.a = 1"
5134                      }
5135                    }
5136                  }
5137                }
5138              },
5139              {
5140                "query_block": {
5141                  "select_id": 3,
5142                  "operation": "UNION",
5143                  "having_condition": "c < 300",
5144                  "filesort": {
5145                    "sort_key": "t1.b",
5146                    "temporary_table": {
5147                      "table": {
5148                        "table_name": "t1",
5149                        "access_type": "ALL",
5150                        "rows": 20,
5151                        "filtered": 100,
5152                        "attached_condition": "t1.a = 1 and t1.b > 10"
5153                      }
5154                    }
5155                  }
5156                }
5157              }
5158            ]
5159          }
5160        }
5161      }
5162    }
5163  }
5164}
5165# using derived table with union
5166# extracted or formula : pushing into WHERE
5167# conjunctive subformula : pushing into HAVING
5168# pushing equalities
5169set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v2_union as v,t2 where
5170((v.a=6) or (v.a=8)) and (v.c>200) and (v.a=t2.a);
5171a	b	c	a	b	c	d
51728	33	404.0000	8	64	248	107
51736	20	312.0000	6	20	315	279
51746	20	214.0000	6	20	315	279
51758	33	404.0000	8	80	800	314
51766	20	312.0000	6	23	303	909
51776	20	214.0000	6	23	303	909
5178select * from v2_union as v,t2 where
5179((v.a=6) or (v.a=8)) and (v.c>200) and (v.a=t2.a);
5180a	b	c	a	b	c	d
51818	33	404.0000	8	64	248	107
51826	20	312.0000	6	20	315	279
51836	20	214.0000	6	20	315	279
51848	33	404.0000	8	80	800	314
51856	20	312.0000	6	23	303	909
51866	20	214.0000	6	23	303	909
5187explain select * from v2_union as v,t2 where
5188((v.a=6) or (v.a=8)) and (v.c>200) and (v.a=t2.a);
5189id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
51901	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
51911	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.a	6	Using where
51922	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
51933	UNION	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
51944	UNION	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
5195NULL	UNION RESULT	<union2,3,4>	ALL	NULL	NULL	NULL	NULL	NULL
5196explain format=json select * from v2_union as v,t2 where
5197((v.a=6) or (v.a=8)) and (v.c>200) and (v.a=t2.a);
5198EXPLAIN
5199{
5200  "query_block": {
5201    "select_id": 1,
5202    "table": {
5203      "table_name": "t2",
5204      "access_type": "ALL",
5205      "rows": 9,
5206      "filtered": 100,
5207      "attached_condition": "(t2.a = 6 or t2.a = 8) and t2.a is not null"
5208    },
5209    "table": {
5210      "table_name": "<derived2>",
5211      "access_type": "ref",
5212      "possible_keys": ["key0"],
5213      "key": "key0",
5214      "key_length": "5",
5215      "used_key_parts": ["a"],
5216      "ref": ["test.t2.a"],
5217      "rows": 6,
5218      "filtered": 100,
5219      "attached_condition": "v.c > 200",
5220      "materialized": {
5221        "query_block": {
5222          "union_result": {
5223            "table_name": "<union2,3,4>",
5224            "access_type": "ALL",
5225            "query_specifications": [
5226              {
5227                "query_block": {
5228                  "select_id": 2,
5229                  "having_condition": "c > 109 and c > 200",
5230                  "filesort": {
5231                    "sort_key": "t1.a, t1.b",
5232                    "temporary_table": {
5233                      "table": {
5234                        "table_name": "t1",
5235                        "access_type": "ALL",
5236                        "rows": 20,
5237                        "filtered": 100,
5238                        "attached_condition": "t1.a < 10 and (t1.a = 6 or t1.a = 8)"
5239                      }
5240                    }
5241                  }
5242                }
5243              },
5244              {
5245                "query_block": {
5246                  "select_id": 3,
5247                  "operation": "UNION",
5248                  "having_condition": "c < 300 and c > 200",
5249                  "filesort": {
5250                    "sort_key": "t1.a, t1.b",
5251                    "temporary_table": {
5252                      "table": {
5253                        "table_name": "t1",
5254                        "access_type": "ALL",
5255                        "rows": 20,
5256                        "filtered": 100,
5257                        "attached_condition": "t1.b > 10 and (t1.a = 6 or t1.a = 8)"
5258                      }
5259                    }
5260                  }
5261                }
5262              },
5263              {
5264                "query_block": {
5265                  "select_id": 4,
5266                  "operation": "UNION",
5267                  "having_condition": "c < 707 and c > 200",
5268                  "filesort": {
5269                    "sort_key": "t1.a, t1.b",
5270                    "temporary_table": {
5271                      "table": {
5272                        "table_name": "t1",
5273                        "access_type": "ALL",
5274                        "rows": 20,
5275                        "filtered": 100,
5276                        "attached_condition": "t1.c > 300 and (t1.a = 6 or t1.a = 8)"
5277                      }
5278                    }
5279                  }
5280                }
5281              }
5282            ]
5283          }
5284        }
5285      }
5286    }
5287  }
5288}
5289# using derived table with union of selects without aggregation
5290# extracted conjunctive predicate: pushing in WHERE of both selects
5291set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v3_union as v,t2 where (v.a=t2.a) and (v.c>6);
5292a	b	c	a	b	c	d
52931	21	123	1	21	909	12
52941	21	101	1	21	909	12
52951	21	104	1	21	909	12
52961	33	988	1	21	909	12
52971	19	107	1	21	909	12
52981	21	500	1	21	909	12
52991	21	345	1	21	909	12
53007	11	708	7	13	312	406
53017	11	8	7	13	312	406
53028	33	404	8	64	248	107
53038	33	123	8	64	248	107
53048	33	114	8	64	248	107
53058	33	9	8	64	248	107
53066	20	214	6	20	315	279
53076	20	315	6	20	315	279
53086	20	309	6	20	315	279
53096	20	7	6	20	315	279
53101	21	123	1	19	203	107
53111	21	101	1	19	203	107
53121	21	104	1	19	203	107
53131	33	988	1	19	203	107
53141	19	107	1	19	203	107
53151	21	500	1	19	203	107
53161	21	345	1	19	203	107
53178	33	404	8	80	800	314
53188	33	123	8	80	800	314
53198	33	114	8	80	800	314
53208	33	9	8	80	800	314
53216	20	214	6	23	303	909
53226	20	315	6	23	303	909
53236	20	309	6	23	303	909
53246	20	7	6	23	303	909
5325select * from v3_union as v,t2 where (v.a=t2.a) and (v.c>6);
5326a	b	c	a	b	c	d
53271	21	123	1	21	909	12
53281	21	101	1	21	909	12
53291	21	104	1	21	909	12
53301	33	988	1	21	909	12
53311	19	107	1	21	909	12
53321	21	500	1	21	909	12
53331	21	345	1	21	909	12
53347	11	708	7	13	312	406
53357	11	8	7	13	312	406
53368	33	404	8	64	248	107
53378	33	123	8	64	248	107
53388	33	114	8	64	248	107
53398	33	9	8	64	248	107
53406	20	214	6	20	315	279
53416	20	315	6	20	315	279
53426	20	309	6	20	315	279
53436	20	7	6	20	315	279
53441	21	123	1	19	203	107
53451	21	101	1	19	203	107
53461	21	104	1	19	203	107
53471	33	988	1	19	203	107
53481	19	107	1	19	203	107
53491	21	500	1	19	203	107
53501	21	345	1	19	203	107
53518	33	404	8	80	800	314
53528	33	123	8	80	800	314
53538	33	114	8	80	800	314
53548	33	9	8	80	800	314
53556	20	214	6	23	303	909
53566	20	315	6	23	303	909
53576	20	309	6	23	303	909
53586	20	7	6	23	303	909
5359explain select * from v3_union as v,t2 where (v.a=t2.a) and (v.c>6);
5360id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
53611	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
53621	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.a	4	Using where
53632	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where
53643	UNION	t1	ALL	NULL	NULL	NULL	NULL	20	Using where
5365NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL
5366explain format=json select * from v3_union as v,t2 where (v.a=t2.a) and (v.c>6);
5367EXPLAIN
5368{
5369  "query_block": {
5370    "select_id": 1,
5371    "table": {
5372      "table_name": "t2",
5373      "access_type": "ALL",
5374      "rows": 9,
5375      "filtered": 100,
5376      "attached_condition": "t2.a is not null"
5377    },
5378    "table": {
5379      "table_name": "<derived2>",
5380      "access_type": "ref",
5381      "possible_keys": ["key0"],
5382      "key": "key0",
5383      "key_length": "5",
5384      "used_key_parts": ["a"],
5385      "ref": ["test.t2.a"],
5386      "rows": 4,
5387      "filtered": 100,
5388      "attached_condition": "v.c > 6",
5389      "materialized": {
5390        "query_block": {
5391          "union_result": {
5392            "table_name": "<union2,3>",
5393            "access_type": "ALL",
5394            "query_specifications": [
5395              {
5396                "query_block": {
5397                  "select_id": 2,
5398                  "table": {
5399                    "table_name": "t1",
5400                    "access_type": "ALL",
5401                    "rows": 20,
5402                    "filtered": 100,
5403                    "attached_condition": "t1.a < 10 and t1.a + 1 > 6"
5404                  }
5405                }
5406              },
5407              {
5408                "query_block": {
5409                  "select_id": 3,
5410                  "operation": "UNION",
5411                  "table": {
5412                    "table_name": "t1",
5413                    "access_type": "ALL",
5414                    "rows": 20,
5415                    "filtered": 100,
5416                    "attached_condition": "t1.b > 10 and t1.c > 100 and t1.c > 6"
5417                  }
5418                }
5419              }
5420            ]
5421          }
5422        }
5423      }
5424    }
5425  }
5426}
5427# using derived table with union of selects without aggregation
5428# extracted conjunctive OR subformula: pushing in WHERE using equalities
5429set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v3_union as v,t2 where (v.a=t2.a) and ((t2.a>1) or (v.b<20));
5430a	b	c	a	b	c	d
54311	19	107	1	21	909	12
54321	19	2	1	21	909	12
54337	11	708	7	13	312	406
54347	11	8	7	13	312	406
54358	33	404	8	64	248	107
54368	33	123	8	64	248	107
54378	33	114	8	64	248	107
54388	33	9	8	64	248	107
54396	20	214	6	20	315	279
54406	20	315	6	20	315	279
54416	20	309	6	20	315	279
54426	20	7	6	20	315	279
54431	19	107	1	19	203	107
54441	19	2	1	19	203	107
54458	33	404	8	80	800	314
54468	33	123	8	80	800	314
54478	33	114	8	80	800	314
54488	33	9	8	80	800	314
54496	20	214	6	23	303	909
54506	20	315	6	23	303	909
54516	20	309	6	23	303	909
54526	20	7	6	23	303	909
5453select * from v3_union as v,t2 where (v.a=t2.a) and ((t2.a>1) or (v.b<20));
5454a	b	c	a	b	c	d
54551	19	107	1	21	909	12
54561	19	2	1	21	909	12
54577	11	708	7	13	312	406
54587	11	8	7	13	312	406
54598	33	404	8	64	248	107
54608	33	123	8	64	248	107
54618	33	114	8	64	248	107
54628	33	9	8	64	248	107
54636	20	214	6	20	315	279
54646	20	315	6	20	315	279
54656	20	309	6	20	315	279
54666	20	7	6	20	315	279
54671	19	107	1	19	203	107
54681	19	2	1	19	203	107
54698	33	404	8	80	800	314
54708	33	123	8	80	800	314
54718	33	114	8	80	800	314
54728	33	9	8	80	800	314
54736	20	214	6	23	303	909
54746	20	315	6	23	303	909
54756	20	309	6	23	303	909
54766	20	7	6	23	303	909
5477explain select * from v3_union as v,t2 where (v.a=t2.a) and ((t2.a>1) or (v.b<20));
5478id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
54791	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
54801	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.a	4	Using where
54812	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where
54823	UNION	t1	ALL	NULL	NULL	NULL	NULL	20	Using where
5483NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL
5484explain format=json select * from v3_union as v,t2 where (v.a=t2.a) and ((t2.a>1) or (v.b<20));
5485EXPLAIN
5486{
5487  "query_block": {
5488    "select_id": 1,
5489    "table": {
5490      "table_name": "t2",
5491      "access_type": "ALL",
5492      "rows": 9,
5493      "filtered": 100,
5494      "attached_condition": "t2.a is not null"
5495    },
5496    "table": {
5497      "table_name": "<derived2>",
5498      "access_type": "ref",
5499      "possible_keys": ["key0"],
5500      "key": "key0",
5501      "key_length": "5",
5502      "used_key_parts": ["a"],
5503      "ref": ["test.t2.a"],
5504      "rows": 4,
5505      "filtered": 100,
5506      "attached_condition": "t2.a > 1 or v.b < 20",
5507      "materialized": {
5508        "query_block": {
5509          "union_result": {
5510            "table_name": "<union2,3>",
5511            "access_type": "ALL",
5512            "query_specifications": [
5513              {
5514                "query_block": {
5515                  "select_id": 2,
5516                  "table": {
5517                    "table_name": "t1",
5518                    "access_type": "ALL",
5519                    "rows": 20,
5520                    "filtered": 100,
5521                    "attached_condition": "t1.a < 10 and (t1.a > 1 or t1.b < 20)"
5522                  }
5523                }
5524              },
5525              {
5526                "query_block": {
5527                  "select_id": 3,
5528                  "operation": "UNION",
5529                  "table": {
5530                    "table_name": "t1",
5531                    "access_type": "ALL",
5532                    "rows": 20,
5533                    "filtered": 100,
5534                    "attached_condition": "t1.b > 10 and t1.c > 100 and (t1.a > 1 or t1.b < 20)"
5535                  }
5536                }
5537              }
5538            ]
5539          }
5540        }
5541      }
5542    }
5543  }
5544}
5545# using derived table with union of selects without aggregation
5546# extracted the whole condition: in WHERE of both selects
5547set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v3_union as v,t2 where
5548(v.a=t2.a) and ((v.b=19) or (v.b=21)) and ((v.c<3) or (v.c>600));
5549a	b	c	a	b	c	d
55501	19	2	1	21	909	12
55511	21	2	1	21	909	12
55521	19	2	1	19	203	107
55531	21	2	1	19	203	107
5554select * from v3_union as v,t2 where
5555(v.a=t2.a) and ((v.b=19) or (v.b=21)) and ((v.c<3) or (v.c>600));
5556a	b	c	a	b	c	d
55571	19	2	1	21	909	12
55581	21	2	1	21	909	12
55591	19	2	1	19	203	107
55601	21	2	1	19	203	107
5561explain select * from v3_union as v,t2 where
5562(v.a=t2.a) and ((v.b=19) or (v.b=21)) and ((v.c<3) or (v.c>600));
5563id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
55641	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
55651	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.a	4	Using where
55662	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where
55673	UNION	t1	ALL	NULL	NULL	NULL	NULL	20	Using where
5568NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL
5569explain format=json select * from v3_union as v,t2 where
5570(v.a=t2.a) and ((v.b=19) or (v.b=21)) and ((v.c<3) or (v.c>600));
5571EXPLAIN
5572{
5573  "query_block": {
5574    "select_id": 1,
5575    "table": {
5576      "table_name": "t2",
5577      "access_type": "ALL",
5578      "rows": 9,
5579      "filtered": 100,
5580      "attached_condition": "t2.a is not null"
5581    },
5582    "table": {
5583      "table_name": "<derived2>",
5584      "access_type": "ref",
5585      "possible_keys": ["key0"],
5586      "key": "key0",
5587      "key_length": "5",
5588      "used_key_parts": ["a"],
5589      "ref": ["test.t2.a"],
5590      "rows": 4,
5591      "filtered": 100,
5592      "attached_condition": "(v.b = 19 or v.b = 21) and (v.c < 3 or v.c > 600)",
5593      "materialized": {
5594        "query_block": {
5595          "union_result": {
5596            "table_name": "<union2,3>",
5597            "access_type": "ALL",
5598            "query_specifications": [
5599              {
5600                "query_block": {
5601                  "select_id": 2,
5602                  "table": {
5603                    "table_name": "t1",
5604                    "access_type": "ALL",
5605                    "rows": 20,
5606                    "filtered": 100,
5607                    "attached_condition": "t1.a < 10 and (t1.b = 19 or t1.b = 21) and (t1.a + 1 < 3 or t1.a + 1 > 600)"
5608                  }
5609                }
5610              },
5611              {
5612                "query_block": {
5613                  "select_id": 3,
5614                  "operation": "UNION",
5615                  "table": {
5616                    "table_name": "t1",
5617                    "access_type": "ALL",
5618                    "rows": 20,
5619                    "filtered": 100,
5620                    "attached_condition": "t1.b > 10 and t1.c > 100 and (t1.b = 19 or t1.b = 21) and (t1.c < 3 or t1.c > 600)"
5621                  }
5622                }
5623              }
5624            ]
5625          }
5626        }
5627      }
5628    }
5629  }
5630}
5631# using derived table with union of
5632# a select without aggregation and a select with aggregation
5633# extracted conjunctive predicate: pushing in WHERE of both selects
5634set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v4_union as v,t2 where (v.a=t2.a) and (v.b<20);
5635a	b	c	a	b	c	d
56361	19	207	1	21	909	12
56377	11	808	7	13	312	406
56387	11	608	7	13	312	406
56391	19	207	1	19	203	107
5640select * from v4_union as v,t2 where (v.a=t2.a) and (v.b<20);
5641a	b	c	a	b	c	d
56421	19	207	1	21	909	12
56437	11	808	7	13	312	406
56447	11	608	7	13	312	406
56451	19	207	1	19	203	107
5646explain select * from v4_union as v,t2 where (v.a=t2.a) and (v.b<20);
5647id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
56481	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
56491	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.a	4	Using where
56502	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
56513	UNION	t1	ALL	NULL	NULL	NULL	NULL	20	Using where
5652NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL
5653explain format=json select * from v4_union as v,t2 where (v.a=t2.a) and (v.b<20);
5654EXPLAIN
5655{
5656  "query_block": {
5657    "select_id": 1,
5658    "table": {
5659      "table_name": "t2",
5660      "access_type": "ALL",
5661      "rows": 9,
5662      "filtered": 100,
5663      "attached_condition": "t2.a is not null"
5664    },
5665    "table": {
5666      "table_name": "<derived2>",
5667      "access_type": "ref",
5668      "possible_keys": ["key0"],
5669      "key": "key0",
5670      "key_length": "5",
5671      "used_key_parts": ["a"],
5672      "ref": ["test.t2.a"],
5673      "rows": 4,
5674      "filtered": 100,
5675      "attached_condition": "v.b < 20",
5676      "materialized": {
5677        "query_block": {
5678          "union_result": {
5679            "table_name": "<union2,3>",
5680            "access_type": "ALL",
5681            "query_specifications": [
5682              {
5683                "query_block": {
5684                  "select_id": 2,
5685                  "having_condition": "c > 109",
5686                  "filesort": {
5687                    "sort_key": "t1.a, t1.b",
5688                    "temporary_table": {
5689                      "table": {
5690                        "table_name": "t1",
5691                        "access_type": "ALL",
5692                        "rows": 20,
5693                        "filtered": 100,
5694                        "attached_condition": "t1.a < 10 and t1.b < 20"
5695                      }
5696                    }
5697                  }
5698                }
5699              },
5700              {
5701                "query_block": {
5702                  "select_id": 3,
5703                  "operation": "UNION",
5704                  "table": {
5705                    "table_name": "t1",
5706                    "access_type": "ALL",
5707                    "rows": 20,
5708                    "filtered": 100,
5709                    "attached_condition": "t1.b > 10 and t1.b < 20"
5710                  }
5711                }
5712              }
5713            ]
5714          }
5715        }
5716      }
5717    }
5718  }
5719}
5720# using derived table with union of
5721# a select without aggregation and a select with aggregation
5722# extracted subformula: pushing in WHERE of one select
5723# extracted subformula: pushing in HAVING of the other select
5724# extracted sub-subformula: pushing in WHERE of the other select
5725# using an equality in all pushdowns
5726set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v4_union as v,t2 where
5727(v.a=t2.a) and ((t2.a<3) or (v.b<40)) and (v.c>500);
5728a	b	c	a	b	c	d
57291	33	1088	1	21	909	12
57301	21	600	1	21	909	12
57311	33	888	1	21	909	12
57327	11	808	7	13	312	406
57337	11	608	7	13	312	406
57348	33	504	8	64	248	107
57351	33	1088	1	19	203	107
57361	21	600	1	19	203	107
57371	33	888	1	19	203	107
57388	33	504	8	80	800	314
5739select * from v4_union as v,t2 where
5740(v.a=t2.a) and ((t2.a<3) or (v.b<40)) and (v.c>500);
5741a	b	c	a	b	c	d
57421	33	1088	1	21	909	12
57431	21	600	1	21	909	12
57441	33	888	1	21	909	12
57457	11	808	7	13	312	406
57467	11	608	7	13	312	406
57478	33	504	8	64	248	107
57481	33	1088	1	19	203	107
57491	21	600	1	19	203	107
57501	33	888	1	19	203	107
57518	33	504	8	80	800	314
5752explain select * from v4_union as v,t2 where
5753(v.a=t2.a) and ((t2.a<3) or (v.b<40)) and (v.c>500);
5754id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
57551	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
57561	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.a	4	Using where
57572	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
57583	UNION	t1	ALL	NULL	NULL	NULL	NULL	20	Using where
5759NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL
5760explain format=json select * from v4_union as v,t2 where
5761(v.a=t2.a) and ((t2.a<3) or (v.b<40)) and (v.c>500);
5762EXPLAIN
5763{
5764  "query_block": {
5765    "select_id": 1,
5766    "table": {
5767      "table_name": "t2",
5768      "access_type": "ALL",
5769      "rows": 9,
5770      "filtered": 100,
5771      "attached_condition": "t2.a is not null"
5772    },
5773    "table": {
5774      "table_name": "<derived2>",
5775      "access_type": "ref",
5776      "possible_keys": ["key0"],
5777      "key": "key0",
5778      "key_length": "5",
5779      "used_key_parts": ["a"],
5780      "ref": ["test.t2.a"],
5781      "rows": 4,
5782      "filtered": 100,
5783      "attached_condition": "(t2.a < 3 or v.b < 40) and v.c > 500",
5784      "materialized": {
5785        "query_block": {
5786          "union_result": {
5787            "table_name": "<union2,3>",
5788            "access_type": "ALL",
5789            "query_specifications": [
5790              {
5791                "query_block": {
5792                  "select_id": 2,
5793                  "having_condition": "c > 109 and c > 500",
5794                  "filesort": {
5795                    "sort_key": "t1.a, t1.b",
5796                    "temporary_table": {
5797                      "table": {
5798                        "table_name": "t1",
5799                        "access_type": "ALL",
5800                        "rows": 20,
5801                        "filtered": 100,
5802                        "attached_condition": "t1.a < 10 and (t1.a < 3 or t1.b < 40)"
5803                      }
5804                    }
5805                  }
5806                }
5807              },
5808              {
5809                "query_block": {
5810                  "select_id": 3,
5811                  "operation": "UNION",
5812                  "table": {
5813                    "table_name": "t1",
5814                    "access_type": "ALL",
5815                    "rows": 20,
5816                    "filtered": 100,
5817                    "attached_condition": "t1.b > 10 and (t1.a < 3 or t1.b < 40) and t1.c + 100 > 500"
5818                  }
5819                }
5820              }
5821            ]
5822          }
5823        }
5824      }
5825    }
5826  }
5827}
5828# using embedded derived table : pushing the same conditions
5829# using several derived tables : pushing in all tables
5830# conjunctive subformula : pushing into WHERE
5831# extracted and formula : pushing into WHERE
5832set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v4,v1 where
5833(v4.a<13) and (v1.a>5) and (v1.b>12);
5834a	b	min_c	a	b	max_c	avg_c
58351	19	107	6	20	315	279.3333
58361	21	500	6	20	315	279.3333
58375	16	207	6	20	315	279.3333
58385	27	132	6	20	315	279.3333
58396	20	315	6	20	315	279.3333
58408	33	404	6	20	315	279.3333
58411	19	107	8	33	404	213.6667
58421	21	500	8	33	404	213.6667
58435	16	207	8	33	404	213.6667
58445	27	132	8	33	404	213.6667
58456	20	315	8	33	404	213.6667
58468	33	404	8	33	404	213.6667
5847select * from v4,v1 where
5848(v4.a<13) and (v1.a>5) and (v1.b>12);
5849a	b	min_c	a	b	max_c	avg_c
58501	19	107	6	20	315	279.3333
58511	21	500	6	20	315	279.3333
58525	16	207	6	20	315	279.3333
58535	27	132	6	20	315	279.3333
58546	20	315	6	20	315	279.3333
58558	33	404	6	20	315	279.3333
58561	19	107	8	33	404	213.6667
58571	21	500	8	33	404	213.6667
58585	16	207	8	33	404	213.6667
58595	27	132	8	33	404	213.6667
58606	20	315	8	33	404	213.6667
58618	33	404	8	33	404	213.6667
5862explain select * from v4,v1 where
5863(v4.a<13) and (v1.a>5) and (v1.b>12);
5864id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
58651	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where
58661	PRIMARY	<derived4>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
58674	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
58682	DERIVED	<derived3>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
58693	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
5870explain format=json select * from v4,v1 where
5871(v4.a<13) and (v1.a>5) and (v1.b>12);
5872EXPLAIN
5873{
5874  "query_block": {
5875    "select_id": 1,
5876    "table": {
5877      "table_name": "<derived2>",
5878      "access_type": "ALL",
5879      "rows": 20,
5880      "filtered": 100,
5881      "attached_condition": "v4.a < 13",
5882      "materialized": {
5883        "query_block": {
5884          "select_id": 2,
5885          "filesort": {
5886            "sort_key": "v1.a, v1.b",
5887            "temporary_table": {
5888              "table": {
5889                "table_name": "<derived3>",
5890                "access_type": "ALL",
5891                "rows": 20,
5892                "filtered": 100,
5893                "attached_condition": "v1.a < 15 and v1.a < 13",
5894                "materialized": {
5895                  "query_block": {
5896                    "select_id": 3,
5897                    "having_condition": "max_c < 707",
5898                    "filesort": {
5899                      "sort_key": "t1.a, t1.b",
5900                      "temporary_table": {
5901                        "table": {
5902                          "table_name": "t1",
5903                          "access_type": "ALL",
5904                          "rows": 20,
5905                          "filtered": 100,
5906                          "attached_condition": "t1.a < 15 and t1.a < 13"
5907                        }
5908                      }
5909                    }
5910                  }
5911                }
5912              }
5913            }
5914          }
5915        }
5916      }
5917    },
5918    "block-nl-join": {
5919      "table": {
5920        "table_name": "<derived4>",
5921        "access_type": "ALL",
5922        "rows": 20,
5923        "filtered": 100,
5924        "attached_condition": "v1.a > 5 and v1.b > 12"
5925      },
5926      "buffer_type": "flat",
5927      "buffer_size": "333",
5928      "join_type": "BNL",
5929      "materialized": {
5930        "query_block": {
5931          "select_id": 4,
5932          "having_condition": "max_c < 707",
5933          "filesort": {
5934            "sort_key": "t1.a, t1.b",
5935            "temporary_table": {
5936              "table": {
5937                "table_name": "t1",
5938                "access_type": "ALL",
5939                "rows": 20,
5940                "filtered": 100,
5941                "attached_condition": "t1.a > 5 and t1.b > 12"
5942              }
5943            }
5944          }
5945        }
5946      }
5947    }
5948  }
5949}
5950# using embedded view : nothing to push
5951# using several derived tables : pushing only in one table
5952# conjunctive subformula : pushing into WHERE
5953set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v4,v1,t2 where
5954(v4.a=t2.a) and (v4.a=v1.a) and (v1.b>30);
5955a	b	min_c	a	b	max_c	avg_c	a	b	c	d
59568	33	404	8	33	404	213.6667	8	64	248	107
59578	33	404	8	33	404	213.6667	8	80	800	314
5958select * from v4,v1,t2 where
5959(v4.a=t2.a) and (v4.a=v1.a) and (v1.b>30);
5960a	b	min_c	a	b	max_c	avg_c	a	b	c	d
59618	33	404	8	33	404	213.6667	8	64	248	107
59628	33	404	8	33	404	213.6667	8	80	800	314
5963explain select * from v4,v1,t2 where
5964(v4.a=t2.a) and (v4.a=v1.a) and (v1.b>30);
5965id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
59661	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
59671	PRIMARY	<derived2>	ref	key1	key1	5	test.t2.a	2
59681	PRIMARY	<derived4>	ref	key0	key0	5	test.t2.a	2	Using where
59694	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
59702	DERIVED	<derived3>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
59713	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
5972explain format=json select * from v4,v1,t2 where
5973(v4.a=t2.a) and (v4.a=v1.a) and (v1.b>30);
5974EXPLAIN
5975{
5976  "query_block": {
5977    "select_id": 1,
5978    "table": {
5979      "table_name": "t2",
5980      "access_type": "ALL",
5981      "rows": 9,
5982      "filtered": 100,
5983      "attached_condition": "t2.a is not null and t2.a is not null"
5984    },
5985    "table": {
5986      "table_name": "<derived2>",
5987      "access_type": "ref",
5988      "possible_keys": ["key1"],
5989      "key": "key1",
5990      "key_length": "5",
5991      "used_key_parts": ["a"],
5992      "ref": ["test.t2.a"],
5993      "rows": 2,
5994      "filtered": 100,
5995      "materialized": {
5996        "query_block": {
5997          "select_id": 2,
5998          "filesort": {
5999            "sort_key": "v1.a, v1.b",
6000            "temporary_table": {
6001              "table": {
6002                "table_name": "<derived3>",
6003                "access_type": "ALL",
6004                "rows": 20,
6005                "filtered": 100,
6006                "attached_condition": "v1.a < 15",
6007                "materialized": {
6008                  "query_block": {
6009                    "select_id": 3,
6010                    "having_condition": "max_c < 707",
6011                    "filesort": {
6012                      "sort_key": "t1.a, t1.b",
6013                      "temporary_table": {
6014                        "table": {
6015                          "table_name": "t1",
6016                          "access_type": "ALL",
6017                          "rows": 20,
6018                          "filtered": 100,
6019                          "attached_condition": "t1.a < 15"
6020                        }
6021                      }
6022                    }
6023                  }
6024                }
6025              }
6026            }
6027          }
6028        }
6029      }
6030    },
6031    "table": {
6032      "table_name": "<derived4>",
6033      "access_type": "ref",
6034      "possible_keys": ["key0"],
6035      "key": "key0",
6036      "key_length": "5",
6037      "used_key_parts": ["a"],
6038      "ref": ["test.t2.a"],
6039      "rows": 2,
6040      "filtered": 100,
6041      "attached_condition": "v1.b > 30",
6042      "materialized": {
6043        "query_block": {
6044          "select_id": 4,
6045          "having_condition": "max_c < 707",
6046          "filesort": {
6047            "sort_key": "t1.a, t1.b",
6048            "temporary_table": {
6049              "table": {
6050                "table_name": "t1",
6051                "access_type": "ALL",
6052                "rows": 20,
6053                "filtered": 100,
6054                "attached_condition": "t1.b > 30"
6055              }
6056            }
6057          }
6058        }
6059      }
6060    }
6061  }
6062}
6063# using embedded view : pushing different conditions
6064# using several derived tables : pushing in all tables
6065# conjunctive subformula : pushing into WHERE using equalities
6066# extracted and formula : pushing into WHERE using equalities
6067# conjunctive subformula : pushing into HAVING
6068set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v4,v1,t2 where
6069(v4.a=t2.a) and (v4.a>1) and (v4.a=v1.a) and (v4.min_c>100) and (v1.b<30);
6070a	b	min_c	a	b	max_c	avg_c	a	b	c	d
60716	20	315	6	20	315	279.3333	6	20	315	279
60726	20	315	6	20	315	279.3333	6	23	303	909
6073select * from v4,v1,t2 where
6074(v4.a=t2.a) and (v4.a>1) and (v4.a=v1.a) and (v4.min_c>100) and (v1.b<30);
6075a	b	min_c	a	b	max_c	avg_c	a	b	c	d
60766	20	315	6	20	315	279.3333	6	20	315	279
60776	20	315	6	20	315	279.3333	6	23	303	909
6078explain select * from v4,v1,t2 where
6079(v4.a=t2.a) and (v4.a>1) and (v4.a=v1.a) and (v4.min_c>100) and (v1.b<30);
6080id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
60811	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
60821	PRIMARY	<derived2>	ref	key1	key1	5	test.t2.a	2	Using where
60831	PRIMARY	<derived4>	ref	key0	key0	5	test.t2.a	2	Using where
60844	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
60852	DERIVED	<derived3>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
60863	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
6087explain format=json select * from v4,v1,t2 where
6088(v4.a=t2.a) and (v4.a>1) and (v4.a=v1.a) and (v4.min_c>100) and (v1.b<30);
6089EXPLAIN
6090{
6091  "query_block": {
6092    "select_id": 1,
6093    "table": {
6094      "table_name": "t2",
6095      "access_type": "ALL",
6096      "rows": 9,
6097      "filtered": 100,
6098      "attached_condition": "t2.a > 1 and t2.a is not null and t2.a is not null"
6099    },
6100    "table": {
6101      "table_name": "<derived2>",
6102      "access_type": "ref",
6103      "possible_keys": ["key1"],
6104      "key": "key1",
6105      "key_length": "5",
6106      "used_key_parts": ["a"],
6107      "ref": ["test.t2.a"],
6108      "rows": 2,
6109      "filtered": 100,
6110      "attached_condition": "v4.min_c > 100",
6111      "materialized": {
6112        "query_block": {
6113          "select_id": 2,
6114          "having_condition": "min_c > 100",
6115          "filesort": {
6116            "sort_key": "v1.a, v1.b",
6117            "temporary_table": {
6118              "table": {
6119                "table_name": "<derived3>",
6120                "access_type": "ALL",
6121                "rows": 20,
6122                "filtered": 100,
6123                "attached_condition": "v1.a < 15 and v1.a > 1",
6124                "materialized": {
6125                  "query_block": {
6126                    "select_id": 3,
6127                    "having_condition": "max_c < 707",
6128                    "filesort": {
6129                      "sort_key": "t1.a, t1.b",
6130                      "temporary_table": {
6131                        "table": {
6132                          "table_name": "t1",
6133                          "access_type": "ALL",
6134                          "rows": 20,
6135                          "filtered": 100,
6136                          "attached_condition": "t1.a < 15 and t1.a > 1"
6137                        }
6138                      }
6139                    }
6140                  }
6141                }
6142              }
6143            }
6144          }
6145        }
6146      }
6147    },
6148    "table": {
6149      "table_name": "<derived4>",
6150      "access_type": "ref",
6151      "possible_keys": ["key0"],
6152      "key": "key0",
6153      "key_length": "5",
6154      "used_key_parts": ["a"],
6155      "ref": ["test.t2.a"],
6156      "rows": 2,
6157      "filtered": 100,
6158      "attached_condition": "v1.b < 30",
6159      "materialized": {
6160        "query_block": {
6161          "select_id": 4,
6162          "having_condition": "max_c < 707",
6163          "filesort": {
6164            "sort_key": "t1.a, t1.b",
6165            "temporary_table": {
6166              "table": {
6167                "table_name": "t1",
6168                "access_type": "ALL",
6169                "rows": 20,
6170                "filtered": 100,
6171                "attached_condition": "t1.a > 1 and t1.b < 30"
6172              }
6173            }
6174          }
6175        }
6176      }
6177    }
6178  }
6179}
6180# using embedded view : pushing different conditions
6181# using several derived tables : pushing in all tables
6182# extracted or formula : pushing into WHERE
6183# conjunctive subformula : pushing into HAVING
6184set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v4,v1,t2 where
6185(((v4.b>10) and (v4.a>1)) or (v4.b<20)) and (v1.max_c>200) and (v1.a=v4.a);
6186a	b	min_c	a	b	max_c	avg_c	a	b	c	d
61871	19	107	1	21	500	234.6000	2	3	207	207
61881	19	107	1	21	500	234.6000	1	21	909	12
61891	19	107	1	21	500	234.6000	7	13	312	406
61901	19	107	1	21	500	234.6000	8	64	248	107
61911	19	107	1	21	500	234.6000	6	20	315	279
61921	19	107	1	21	500	234.6000	1	19	203	107
61931	19	107	1	21	500	234.6000	8	80	800	314
61941	19	107	1	21	500	234.6000	3	12	231	190
61951	19	107	1	21	500	234.6000	6	23	303	909
61965	16	207	5	16	207	207.0000	2	3	207	207
61975	16	207	5	16	207	207.0000	1	21	909	12
61985	16	207	5	16	207	207.0000	7	13	312	406
61995	16	207	5	16	207	207.0000	8	64	248	107
62005	16	207	5	16	207	207.0000	6	20	315	279
62015	16	207	5	16	207	207.0000	1	19	203	107
62025	16	207	5	16	207	207.0000	8	80	800	314
62035	16	207	5	16	207	207.0000	3	12	231	190
62045	16	207	5	16	207	207.0000	6	23	303	909
62055	27	132	5	16	207	207.0000	2	3	207	207
62065	27	132	5	16	207	207.0000	1	21	909	12
62075	27	132	5	16	207	207.0000	7	13	312	406
62085	27	132	5	16	207	207.0000	8	64	248	107
62095	27	132	5	16	207	207.0000	6	20	315	279
62105	27	132	5	16	207	207.0000	1	19	203	107
62115	27	132	5	16	207	207.0000	8	80	800	314
62125	27	132	5	16	207	207.0000	3	12	231	190
62135	27	132	5	16	207	207.0000	6	23	303	909
62146	20	315	6	20	315	279.3333	2	3	207	207
62156	20	315	6	20	315	279.3333	1	21	909	12
62166	20	315	6	20	315	279.3333	7	13	312	406
62176	20	315	6	20	315	279.3333	8	64	248	107
62186	20	315	6	20	315	279.3333	6	20	315	279
62196	20	315	6	20	315	279.3333	1	19	203	107
62206	20	315	6	20	315	279.3333	8	80	800	314
62216	20	315	6	20	315	279.3333	3	12	231	190
62226	20	315	6	20	315	279.3333	6	23	303	909
62238	33	404	8	33	404	213.6667	2	3	207	207
62248	33	404	8	33	404	213.6667	1	21	909	12
62258	33	404	8	33	404	213.6667	7	13	312	406
62268	33	404	8	33	404	213.6667	8	64	248	107
62278	33	404	8	33	404	213.6667	6	20	315	279
62288	33	404	8	33	404	213.6667	1	19	203	107
62298	33	404	8	33	404	213.6667	8	80	800	314
62308	33	404	8	33	404	213.6667	3	12	231	190
62318	33	404	8	33	404	213.6667	6	23	303	909
6232select * from v4,v1,t2 where
6233(((v4.b>10) and (v4.a>1)) or (v4.b<20)) and (v1.max_c>200) and (v1.a=v4.a);
6234a	b	min_c	a	b	max_c	avg_c	a	b	c	d
62351	19	107	1	21	500	234.6000	2	3	207	207
62361	19	107	1	21	500	234.6000	1	21	909	12
62371	19	107	1	21	500	234.6000	7	13	312	406
62381	19	107	1	21	500	234.6000	8	64	248	107
62391	19	107	1	21	500	234.6000	6	20	315	279
62401	19	107	1	21	500	234.6000	1	19	203	107
62411	19	107	1	21	500	234.6000	8	80	800	314
62421	19	107	1	21	500	234.6000	3	12	231	190
62431	19	107	1	21	500	234.6000	6	23	303	909
62445	16	207	5	16	207	207.0000	2	3	207	207
62455	16	207	5	16	207	207.0000	1	21	909	12
62465	16	207	5	16	207	207.0000	7	13	312	406
62475	16	207	5	16	207	207.0000	8	64	248	107
62485	16	207	5	16	207	207.0000	6	20	315	279
62495	16	207	5	16	207	207.0000	1	19	203	107
62505	16	207	5	16	207	207.0000	8	80	800	314
62515	16	207	5	16	207	207.0000	3	12	231	190
62525	16	207	5	16	207	207.0000	6	23	303	909
62535	27	132	5	16	207	207.0000	2	3	207	207
62545	27	132	5	16	207	207.0000	1	21	909	12
62555	27	132	5	16	207	207.0000	7	13	312	406
62565	27	132	5	16	207	207.0000	8	64	248	107
62575	27	132	5	16	207	207.0000	6	20	315	279
62585	27	132	5	16	207	207.0000	1	19	203	107
62595	27	132	5	16	207	207.0000	8	80	800	314
62605	27	132	5	16	207	207.0000	3	12	231	190
62615	27	132	5	16	207	207.0000	6	23	303	909
62626	20	315	6	20	315	279.3333	2	3	207	207
62636	20	315	6	20	315	279.3333	1	21	909	12
62646	20	315	6	20	315	279.3333	7	13	312	406
62656	20	315	6	20	315	279.3333	8	64	248	107
62666	20	315	6	20	315	279.3333	6	20	315	279
62676	20	315	6	20	315	279.3333	1	19	203	107
62686	20	315	6	20	315	279.3333	8	80	800	314
62696	20	315	6	20	315	279.3333	3	12	231	190
62706	20	315	6	20	315	279.3333	6	23	303	909
62718	33	404	8	33	404	213.6667	2	3	207	207
62728	33	404	8	33	404	213.6667	1	21	909	12
62738	33	404	8	33	404	213.6667	7	13	312	406
62748	33	404	8	33	404	213.6667	8	64	248	107
62758	33	404	8	33	404	213.6667	6	20	315	279
62768	33	404	8	33	404	213.6667	1	19	203	107
62778	33	404	8	33	404	213.6667	8	80	800	314
62788	33	404	8	33	404	213.6667	3	12	231	190
62798	33	404	8	33	404	213.6667	6	23	303	909
6280explain select * from v4,v1,t2 where
6281(((v4.b>10) and (v4.a>1)) or (v4.b<20)) and (v1.max_c>200) and (v1.a=v4.a);
6282id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
62831	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9
62841	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
62851	PRIMARY	<derived4>	ref	key0	key0	5	v4.a	2	Using where
62864	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using temporary; Using filesort
62872	DERIVED	<derived3>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
62883	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
6289explain format=json select * from v4,v1,t2 where
6290(((v4.b>10) and (v4.a>1)) or (v4.b<20)) and (v1.max_c>200) and (v1.a=v4.a);
6291EXPLAIN
6292{
6293  "query_block": {
6294    "select_id": 1,
6295    "table": {
6296      "table_name": "t2",
6297      "access_type": "ALL",
6298      "rows": 9,
6299      "filtered": 100
6300    },
6301    "block-nl-join": {
6302      "table": {
6303        "table_name": "<derived2>",
6304        "access_type": "ALL",
6305        "rows": 20,
6306        "filtered": 100,
6307        "attached_condition": "v4.b > 10 and v4.a > 1 or v4.b < 20"
6308      },
6309      "buffer_type": "flat",
6310      "buffer_size": "238",
6311      "join_type": "BNL",
6312      "attached_condition": "(v4.b > 10 and v4.a > 1 or v4.b < 20) and v4.a is not null",
6313      "materialized": {
6314        "query_block": {
6315          "select_id": 2,
6316          "filesort": {
6317            "sort_key": "v1.a, v1.b",
6318            "temporary_table": {
6319              "table": {
6320                "table_name": "<derived3>",
6321                "access_type": "ALL",
6322                "rows": 20,
6323                "filtered": 100,
6324                "attached_condition": "v1.a < 15 and (v1.b > 10 and v1.a > 1 or v1.b < 20)",
6325                "materialized": {
6326                  "query_block": {
6327                    "select_id": 3,
6328                    "having_condition": "max_c < 707",
6329                    "filesort": {
6330                      "sort_key": "t1.a, t1.b",
6331                      "temporary_table": {
6332                        "table": {
6333                          "table_name": "t1",
6334                          "access_type": "ALL",
6335                          "rows": 20,
6336                          "filtered": 100,
6337                          "attached_condition": "t1.a < 15 and (t1.b > 10 and t1.a > 1 or t1.b < 20)"
6338                        }
6339                      }
6340                    }
6341                  }
6342                }
6343              }
6344            }
6345          }
6346        }
6347      }
6348    },
6349    "table": {
6350      "table_name": "<derived4>",
6351      "access_type": "ref",
6352      "possible_keys": ["key0"],
6353      "key": "key0",
6354      "key_length": "5",
6355      "used_key_parts": ["a"],
6356      "ref": ["v4.a"],
6357      "rows": 2,
6358      "filtered": 100,
6359      "attached_condition": "v1.max_c > 200",
6360      "materialized": {
6361        "query_block": {
6362          "select_id": 4,
6363          "having_condition": "max_c < 707 and max_c > 200",
6364          "filesort": {
6365            "sort_key": "t1.a, t1.b",
6366            "temporary_table": {
6367              "table": {
6368                "table_name": "t1",
6369                "access_type": "ALL",
6370                "rows": 20,
6371                "filtered": 100
6372              }
6373            }
6374          }
6375        }
6376      }
6377    }
6378  }
6379}
6380# using embedded view : pushing different conditions
6381# using several derived tables : pushing only in one table
6382# extracted or formula : pushing into WHERE
6383# extracted or formula : pushing into HAVING
6384set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v4,v2 where
6385((v4.a>12) and (v4.min_c<300) and (v4.b>13)) or (v4.a<1);
6386a	b	min_c	a	b	max_c	avg_c
6387select * from v4,v2 where
6388((v4.a>12) and (v4.min_c<300) and (v4.b>13)) or (v4.a<1);
6389a	b	min_c	a	b	max_c	avg_c
6390explain select * from v4,v2 where
6391((v4.a>12) and (v4.min_c<300) and (v4.b>13)) or (v4.a<1);
6392id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
63931	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where
63941	PRIMARY	<derived4>	ALL	NULL	NULL	NULL	NULL	20	Using join buffer (flat, BNL join)
63954	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
63962	DERIVED	<derived3>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
63973	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
6398explain format=json select * from v4,v2 where
6399((v4.a>12) and (v4.min_c<300) and (v4.b>13)) or (v4.a<1);
6400EXPLAIN
6401{
6402  "query_block": {
6403    "select_id": 1,
6404    "table": {
6405      "table_name": "<derived2>",
6406      "access_type": "ALL",
6407      "rows": 20,
6408      "filtered": 100,
6409      "attached_condition": "v4.a > 12 and v4.min_c < 300 and v4.b > 13 or v4.a < 1",
6410      "materialized": {
6411        "query_block": {
6412          "select_id": 2,
6413          "having_condition": "v1.a > 12 and min_c < 300 and v1.b > 13 or v1.a < 1",
6414          "filesort": {
6415            "sort_key": "v1.a, v1.b",
6416            "temporary_table": {
6417              "table": {
6418                "table_name": "<derived3>",
6419                "access_type": "ALL",
6420                "rows": 20,
6421                "filtered": 100,
6422                "attached_condition": "v1.a < 15 and (v1.a > 12 and v1.b > 13 or v1.a < 1)",
6423                "materialized": {
6424                  "query_block": {
6425                    "select_id": 3,
6426                    "having_condition": "max_c < 707",
6427                    "filesort": {
6428                      "sort_key": "t1.a, t1.b",
6429                      "temporary_table": {
6430                        "table": {
6431                          "table_name": "t1",
6432                          "access_type": "ALL",
6433                          "rows": 20,
6434                          "filtered": 100,
6435                          "attached_condition": "t1.a < 15 and (t1.a > 12 and t1.b > 13 or t1.a < 1)"
6436                        }
6437                      }
6438                    }
6439                  }
6440                }
6441              }
6442            }
6443          }
6444        }
6445      }
6446    },
6447    "block-nl-join": {
6448      "table": {
6449        "table_name": "<derived4>",
6450        "access_type": "ALL",
6451        "rows": 20,
6452        "filtered": 100
6453      },
6454      "buffer_type": "flat",
6455      "buffer_size": "333",
6456      "join_type": "BNL",
6457      "materialized": {
6458        "query_block": {
6459          "select_id": 4,
6460          "having_condition": "max_c < 707",
6461          "filesort": {
6462            "sort_key": "t1.a, t1.b",
6463            "temporary_table": {
6464              "table": {
6465                "table_name": "t1",
6466                "access_type": "ALL",
6467                "rows": 20,
6468                "filtered": 100,
6469                "attached_condition": "t1.a > 5"
6470              }
6471            }
6472          }
6473        }
6474      }
6475    }
6476  }
6477}
6478# using embedded view : pushing different conditions
6479# using several derived tables : pushing only in one table
6480# conjunctive subformula : pushing into WHERE
6481# conjunctive subformula : pushing into HAVING
6482# pushing equalities
6483set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v4,v2 where
6484(v4.a=v2.b) and (v4.a=v4.b) and (v4.min_c<100);
6485a	b	min_c	a	b	max_c	avg_c
6486select * from v4,v2 where
6487(v4.a=v2.b) and (v4.a=v4.b) and (v4.min_c<100);
6488a	b	min_c	a	b	max_c	avg_c
6489explain select * from v4,v2 where
6490(v4.a=v2.b) and (v4.a=v4.b) and (v4.min_c<100);
6491id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
64921	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where
64931	PRIMARY	<derived4>	ref	key0	key0	5	v4.a	2
64944	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
64952	DERIVED	<derived3>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
64963	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
6497explain format=json select * from v4,v2 where
6498(v4.a=v2.b) and (v4.a=v4.b) and (v4.min_c<100);
6499EXPLAIN
6500{
6501  "query_block": {
6502    "select_id": 1,
6503    "table": {
6504      "table_name": "<derived2>",
6505      "access_type": "ALL",
6506      "rows": 20,
6507      "filtered": 100,
6508      "attached_condition": "v4.b = v4.a and v4.min_c < 100 and v4.a is not null",
6509      "materialized": {
6510        "query_block": {
6511          "select_id": 2,
6512          "having_condition": "min_c < 100",
6513          "filesort": {
6514            "sort_key": "v1.a, v1.b",
6515            "temporary_table": {
6516              "table": {
6517                "table_name": "<derived3>",
6518                "access_type": "ALL",
6519                "rows": 20,
6520                "filtered": 100,
6521                "attached_condition": "v1.b = v1.a and v1.a < 15",
6522                "materialized": {
6523                  "query_block": {
6524                    "select_id": 3,
6525                    "having_condition": "max_c < 707",
6526                    "filesort": {
6527                      "sort_key": "t1.a, t1.b",
6528                      "temporary_table": {
6529                        "table": {
6530                          "table_name": "t1",
6531                          "access_type": "ALL",
6532                          "rows": 20,
6533                          "filtered": 100,
6534                          "attached_condition": "t1.b = t1.a and t1.a < 15"
6535                        }
6536                      }
6537                    }
6538                  }
6539                }
6540              }
6541            }
6542          }
6543        }
6544      }
6545    },
6546    "table": {
6547      "table_name": "<derived4>",
6548      "access_type": "ref",
6549      "possible_keys": ["key0"],
6550      "key": "key0",
6551      "key_length": "5",
6552      "used_key_parts": ["b"],
6553      "ref": ["v4.a"],
6554      "rows": 2,
6555      "filtered": 100,
6556      "materialized": {
6557        "query_block": {
6558          "select_id": 4,
6559          "having_condition": "max_c < 707",
6560          "filesort": {
6561            "sort_key": "t1.a, t1.b",
6562            "temporary_table": {
6563              "table": {
6564                "table_name": "t1",
6565                "access_type": "ALL",
6566                "rows": 20,
6567                "filtered": 100,
6568                "attached_condition": "t1.a > 5"
6569              }
6570            }
6571          }
6572        }
6573      }
6574    }
6575  }
6576}
6577# using embedded view : pushing the same conditions
6578# using several derived tables : pushing in all tables
6579# extracted and formula : pushing into WHERE using equalities
6580# conjunctive subformula : pushing into WHERE
6581# pushing equalities
6582set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v4,v2 where
6583(v4.a=v2.b) and (v4.a=v4.b) and (v2.b<30);
6584a	b	min_c	a	b	max_c	avg_c
6585select * from v4,v2 where
6586(v4.a=v2.b) and (v4.a=v4.b) and (v2.b<30);
6587a	b	min_c	a	b	max_c	avg_c
6588explain select * from v4,v2 where
6589(v4.a=v2.b) and (v4.a=v4.b) and (v2.b<30);
6590id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
65911	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where
65921	PRIMARY	<derived4>	ref	key0	key0	5	v4.a	2
65934	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
65942	DERIVED	<derived3>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
65953	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
6596explain format=json select * from v4,v2 where
6597(v4.a=v2.b) and (v4.a=v4.b) and (v2.b<30);
6598EXPLAIN
6599{
6600  "query_block": {
6601    "select_id": 1,
6602    "table": {
6603      "table_name": "<derived2>",
6604      "access_type": "ALL",
6605      "rows": 20,
6606      "filtered": 100,
6607      "attached_condition": "v4.b = v4.a and v4.a < 30 and v4.a is not null",
6608      "materialized": {
6609        "query_block": {
6610          "select_id": 2,
6611          "filesort": {
6612            "sort_key": "v1.a, v1.b",
6613            "temporary_table": {
6614              "table": {
6615                "table_name": "<derived3>",
6616                "access_type": "ALL",
6617                "rows": 20,
6618                "filtered": 100,
6619                "attached_condition": "v1.b = v1.a and v1.a < 15 and v1.a < 30",
6620                "materialized": {
6621                  "query_block": {
6622                    "select_id": 3,
6623                    "having_condition": "max_c < 707",
6624                    "filesort": {
6625                      "sort_key": "t1.a, t1.b",
6626                      "temporary_table": {
6627                        "table": {
6628                          "table_name": "t1",
6629                          "access_type": "ALL",
6630                          "rows": 20,
6631                          "filtered": 100,
6632                          "attached_condition": "t1.b = t1.a and t1.a < 15 and t1.a < 30"
6633                        }
6634                      }
6635                    }
6636                  }
6637                }
6638              }
6639            }
6640          }
6641        }
6642      }
6643    },
6644    "table": {
6645      "table_name": "<derived4>",
6646      "access_type": "ref",
6647      "possible_keys": ["key0"],
6648      "key": "key0",
6649      "key_length": "5",
6650      "used_key_parts": ["b"],
6651      "ref": ["v4.a"],
6652      "rows": 2,
6653      "filtered": 100,
6654      "materialized": {
6655        "query_block": {
6656          "select_id": 4,
6657          "having_condition": "max_c < 707",
6658          "filesort": {
6659            "sort_key": "t1.a, t1.b",
6660            "temporary_table": {
6661              "table": {
6662                "table_name": "t1",
6663                "access_type": "ALL",
6664                "rows": 20,
6665                "filtered": 100,
6666                "attached_condition": "t1.a > 5 and t1.b < 30"
6667              }
6668            }
6669          }
6670        }
6671      }
6672    }
6673  }
6674}
6675# using embedded view : pushing the same conditions
6676# using several derived tables : pushing in all tables
6677# extracted or formula : pushing into WHERE using equalities
6678# extracted and formula : pushing into WHERE using equalities
6679# pushing equalities
6680set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v4,v2 where
6681(v4.a=v2.b) and (v4.a=v4.b) and ((v2.b<30) or (v4.a>2));
6682a	b	min_c	a	b	max_c	avg_c
6683select * from v4,v2 where
6684(v4.a=v2.b) and (v4.a=v4.b) and ((v2.b<30) or (v4.a>2));
6685a	b	min_c	a	b	max_c	avg_c
6686explain select * from v4,v2 where
6687(v4.a=v2.b) and (v4.a=v4.b) and ((v2.b<30) or (v4.a>2));
6688id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
66891	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where
66901	PRIMARY	<derived4>	ref	key0	key0	5	v4.a	2
66914	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
66922	DERIVED	<derived3>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
66933	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
6694explain format=json select * from v4,v2 where
6695(v4.a=v2.b) and (v4.a=v4.b) and ((v2.b<30) or (v4.a>2));
6696EXPLAIN
6697{
6698  "query_block": {
6699    "select_id": 1,
6700    "table": {
6701      "table_name": "<derived2>",
6702      "access_type": "ALL",
6703      "rows": 20,
6704      "filtered": 100,
6705      "attached_condition": "v4.b = v4.a and (v4.a < 30 or v4.a > 2) and v4.a is not null",
6706      "materialized": {
6707        "query_block": {
6708          "select_id": 2,
6709          "filesort": {
6710            "sort_key": "v1.a, v1.b",
6711            "temporary_table": {
6712              "table": {
6713                "table_name": "<derived3>",
6714                "access_type": "ALL",
6715                "rows": 20,
6716                "filtered": 100,
6717                "attached_condition": "v1.b = v1.a and v1.a < 15 and (v1.a < 30 or v1.a > 2)",
6718                "materialized": {
6719                  "query_block": {
6720                    "select_id": 3,
6721                    "having_condition": "max_c < 707",
6722                    "filesort": {
6723                      "sort_key": "t1.a, t1.b",
6724                      "temporary_table": {
6725                        "table": {
6726                          "table_name": "t1",
6727                          "access_type": "ALL",
6728                          "rows": 20,
6729                          "filtered": 100,
6730                          "attached_condition": "t1.b = t1.a and t1.a < 15 and (t1.a < 30 or t1.a > 2)"
6731                        }
6732                      }
6733                    }
6734                  }
6735                }
6736              }
6737            }
6738          }
6739        }
6740      }
6741    },
6742    "table": {
6743      "table_name": "<derived4>",
6744      "access_type": "ref",
6745      "possible_keys": ["key0"],
6746      "key": "key0",
6747      "key_length": "5",
6748      "used_key_parts": ["b"],
6749      "ref": ["v4.a"],
6750      "rows": 2,
6751      "filtered": 100,
6752      "materialized": {
6753        "query_block": {
6754          "select_id": 4,
6755          "having_condition": "max_c < 707",
6756          "filesort": {
6757            "sort_key": "t1.a, t1.b",
6758            "temporary_table": {
6759              "table": {
6760                "table_name": "t1",
6761                "access_type": "ALL",
6762                "rows": 20,
6763                "filtered": 100,
6764                "attached_condition": "t1.a > 5 and (t1.b < 30 or t1.b > 2)"
6765              }
6766            }
6767          }
6768        }
6769      }
6770    }
6771  }
6772}
6773# using embedded view : pushing the same conditions
6774# using several derived tables : pushing in all tables
6775# extracted or formula : pushing into WHERE
6776# conjunctive subformula : pushing into WHERE
6777# pushing equalities
6778set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v4,v2 where
6779(((v4.a<12) and (v4.b>13)) or (v4.a>10)) and
6780(v4.min_c=v2.max_c) and (v4.min_c>100);
6781a	b	min_c	a	b	max_c	avg_c
67826	20	315	6	20	315	279.3333
67838	33	404	8	33	404	213.6667
6784select * from v4,v2 where
6785(((v4.a<12) and (v4.b>13)) or (v4.a>10)) and
6786(v4.min_c=v2.max_c) and (v4.min_c>100);
6787a	b	min_c	a	b	max_c	avg_c
67886	20	315	6	20	315	279.3333
67898	33	404	8	33	404	213.6667
6790explain select * from v4,v2 where
6791(((v4.a<12) and (v4.b>13)) or (v4.a>10)) and
6792(v4.min_c=v2.max_c) and (v4.min_c>100);
6793id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
67941	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where
67951	PRIMARY	<derived4>	ref	key0	key0	5	v4.min_c	2
67964	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
67972	DERIVED	<derived3>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
67983	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
6799explain format=json select * from v4,v2 where
6800(((v4.a<12) and (v4.b>13)) or (v4.a>10)) and
6801(v4.min_c=v2.max_c) and (v4.min_c>100);
6802EXPLAIN
6803{
6804  "query_block": {
6805    "select_id": 1,
6806    "table": {
6807      "table_name": "<derived2>",
6808      "access_type": "ALL",
6809      "rows": 20,
6810      "filtered": 100,
6811      "attached_condition": "(v4.a < 12 and v4.b > 13 or v4.a > 10) and v4.min_c > 100 and v4.min_c is not null",
6812      "materialized": {
6813        "query_block": {
6814          "select_id": 2,
6815          "having_condition": "min_c > 100",
6816          "filesort": {
6817            "sort_key": "v1.a, v1.b",
6818            "temporary_table": {
6819              "table": {
6820                "table_name": "<derived3>",
6821                "access_type": "ALL",
6822                "rows": 20,
6823                "filtered": 100,
6824                "attached_condition": "v1.a < 15 and (v1.a < 12 and v1.b > 13 or v1.a > 10)",
6825                "materialized": {
6826                  "query_block": {
6827                    "select_id": 3,
6828                    "having_condition": "max_c < 707",
6829                    "filesort": {
6830                      "sort_key": "t1.a, t1.b",
6831                      "temporary_table": {
6832                        "table": {
6833                          "table_name": "t1",
6834                          "access_type": "ALL",
6835                          "rows": 20,
6836                          "filtered": 100,
6837                          "attached_condition": "t1.a < 15 and (t1.a < 12 and t1.b > 13 or t1.a > 10)"
6838                        }
6839                      }
6840                    }
6841                  }
6842                }
6843              }
6844            }
6845          }
6846        }
6847      }
6848    },
6849    "table": {
6850      "table_name": "<derived4>",
6851      "access_type": "ref",
6852      "possible_keys": ["key0"],
6853      "key": "key0",
6854      "key_length": "5",
6855      "used_key_parts": ["max_c"],
6856      "ref": ["v4.min_c"],
6857      "rows": 2,
6858      "filtered": 100,
6859      "materialized": {
6860        "query_block": {
6861          "select_id": 4,
6862          "having_condition": "max_c < 707 and max_c > 100",
6863          "filesort": {
6864            "sort_key": "t1.a, t1.b",
6865            "temporary_table": {
6866              "table": {
6867                "table_name": "t1",
6868                "access_type": "ALL",
6869                "rows": 20,
6870                "filtered": 100,
6871                "attached_condition": "t1.a > 5"
6872              }
6873            }
6874          }
6875        }
6876      }
6877    }
6878  }
6879}
6880# using embedded view : pushing the same conditions
6881# using several derived tables : pushing only in one table
6882# extracted or formula : pushing into WHERE
6883set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v4,v2,t2 where
6884(((v4.a<12) and (t2.b>13)) or (v4.a>10)) and
6885(v4.min_c=t2.c) and (t2.c>100);
6886a	b	min_c	a	b	max_c	avg_c	a	b	c	d
68876	20	315	6	20	315	279.3333	6	20	315	279
68886	20	315	8	33	404	213.6667	6	20	315	279
6889select * from v4,v2,t2 where
6890(((v4.a<12) and (t2.b>13)) or (v4.a>10)) and
6891(v4.min_c=t2.c) and (t2.c>100);
6892a	b	min_c	a	b	max_c	avg_c	a	b	c	d
68936	20	315	6	20	315	279.3333	6	20	315	279
68946	20	315	8	33	404	213.6667	6	20	315	279
6895explain select * from v4,v2,t2 where
6896(((v4.a<12) and (t2.b>13)) or (v4.a>10)) and
6897(v4.min_c=t2.c) and (t2.c>100);
6898id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
68991	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
69001	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.c	2	Using where
69011	PRIMARY	<derived4>	ALL	NULL	NULL	NULL	NULL	20	Using join buffer (flat, BNL join)
69024	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
69032	DERIVED	<derived3>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
69043	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
6905explain format=json select * from v4,v2,t2 where
6906(((v4.a<12) and (t2.b>13)) or (v4.a>10)) and
6907(v4.min_c=t2.c) and (t2.c>100);
6908EXPLAIN
6909{
6910  "query_block": {
6911    "select_id": 1,
6912    "table": {
6913      "table_name": "t2",
6914      "access_type": "ALL",
6915      "rows": 9,
6916      "filtered": 100,
6917      "attached_condition": "t2.c > 100 and t2.c is not null"
6918    },
6919    "table": {
6920      "table_name": "<derived2>",
6921      "access_type": "ref",
6922      "possible_keys": ["key0"],
6923      "key": "key0",
6924      "key_length": "5",
6925      "used_key_parts": ["min_c"],
6926      "ref": ["test.t2.c"],
6927      "rows": 2,
6928      "filtered": 100,
6929      "attached_condition": "v4.a < 12 and t2.b > 13 or v4.a > 10",
6930      "materialized": {
6931        "query_block": {
6932          "select_id": 2,
6933          "having_condition": "min_c > 100",
6934          "filesort": {
6935            "sort_key": "v1.a, v1.b",
6936            "temporary_table": {
6937              "table": {
6938                "table_name": "<derived3>",
6939                "access_type": "ALL",
6940                "rows": 20,
6941                "filtered": 100,
6942                "attached_condition": "v1.a < 15 and (v1.a < 12 or v1.a > 10)",
6943                "materialized": {
6944                  "query_block": {
6945                    "select_id": 3,
6946                    "having_condition": "max_c < 707",
6947                    "filesort": {
6948                      "sort_key": "t1.a, t1.b",
6949                      "temporary_table": {
6950                        "table": {
6951                          "table_name": "t1",
6952                          "access_type": "ALL",
6953                          "rows": 20,
6954                          "filtered": 100,
6955                          "attached_condition": "t1.a < 15 and (t1.a < 12 or t1.a > 10)"
6956                        }
6957                      }
6958                    }
6959                  }
6960                }
6961              }
6962            }
6963          }
6964        }
6965      }
6966    },
6967    "block-nl-join": {
6968      "table": {
6969        "table_name": "<derived4>",
6970        "access_type": "ALL",
6971        "rows": 20,
6972        "filtered": 100
6973      },
6974      "buffer_type": "flat",
6975      "buffer_size": "715",
6976      "join_type": "BNL",
6977      "materialized": {
6978        "query_block": {
6979          "select_id": 4,
6980          "having_condition": "max_c < 707",
6981          "filesort": {
6982            "sort_key": "t1.a, t1.b",
6983            "temporary_table": {
6984              "table": {
6985                "table_name": "t1",
6986                "access_type": "ALL",
6987                "rows": 20,
6988                "filtered": 100,
6989                "attached_condition": "t1.a > 5"
6990              }
6991            }
6992          }
6993        }
6994      }
6995    }
6996  }
6997}
6998drop view v1,v2,v3,v4;
6999drop view v_union,v2_union,v3_union,v4_union;
7000drop view v_double,v_char,v_decimal;
7001drop table t1,t2,t1_double,t2_double,t1_char,t2_char,t1_decimal,t2_decimal;
7002#
7003# MDEV-10782: condition extracted from a multiple equality
7004#             pushed into HAVING
7005#
7006CREATE TABLE t1 (i int);
7007INSERT INTO t1 VALUES (1),(2);
7008EXPLAIN EXTENDED
7009SELECT *
7010FROM ( SELECT * FROM ( SELECT MIN(i) as f FROM t1 ) sq1 ) AS sq2
7011WHERE f = 8;
7012id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
70131	PRIMARY	<derived3>	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where
70143	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	2	100.00
7015Warnings:
7016Note	1003	/* select#1 */ select `sq1`.`f` AS `f` from (/* select#3 */ select min(`test`.`t1`.`i`) AS `f` from `test`.`t1` having `f` = 8) `sq1` where `sq1`.`f` = 8
7017SELECT *
7018FROM ( SELECT * FROM ( SELECT MIN(i) as f FROM t1 ) sq1 ) AS sq2
7019WHERE f = 8;
7020f
7021SELECT *
7022FROM ( SELECT * FROM ( SELECT MIN(i) as f FROM t1 ) sq1 ) AS sq2
7023WHERE f = 1;
7024f
70251
7026DROP TABLE t1;
7027#
7028# MDEV-10783: pushdown into constant view
7029#
7030CREATE TABLE t1 (i int) ENGINE=MyISAM;
7031CREATE VIEW v AS SELECT 5;
7032SELECT * FROM t1 WHERE 1 IN ( SELECT * FROM v );
7033i
7034DROP VIEW v;
7035DROP TABLE t1;
7036#
7037# MDEV-10785: second execution of a query with condition
7038#             pushed into view
7039#
7040CREATE TABLE t1 (i int);
7041CREATE VIEW v1 AS SELECT i FROM t1 WHERE i < 5;
7042CREATE FUNCTION f (in1 int) RETURNS int RETURN in1;
7043CREATE VIEW v2 AS SELECT * FROM v1 GROUP BY i;
7044PREPARE stmt FROM "SELECT * FROM v2 WHERE f(0) <> 2";
7045EXECUTE stmt;
7046i
7047EXECUTE stmt;
7048i
7049DROP FUNCTION f;
7050DROP VIEW v2,v1;
7051DROP TABLE t1;
7052#
7053# MDEV-10884: condition pushdown into derived specified by
7054#             1. unit with  SELECT containing ORDER BY ... LIMIT
7055#             2. unit containing global ORDER BY ... LIMIT
7056#
7057create table t1(a int);
7058insert into t1 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
7059select a from t1 order by a limit 5;
7060a
70610
70621
70632
70643
70654
7066set statement optimizer_switch='condition_pushdown_for_derived=off' for
7067select * from (select a from t1 order by a limit 5) t where t.a not in (1,2,3);
7068a
70690
70704
7071set statement optimizer_switch='condition_pushdown_for_derived=on' for
7072select * from (select a from t1 order by a limit 5) t where t.a not in (1,2,3);
7073a
70740
70754
7076select a from t1 where a < 4 union select a from t1 where a > 5
7077order by a limit 5;
7078a
70790
70801
70812
70823
70836
7084set statement optimizer_switch='condition_pushdown_for_derived=off' for
7085select * from
7086(select a from t1 where a < 4 union select a from t1 where a > 5
7087order by a limit 5) t where t.a not in (2,9);
7088a
70890
70901
70913
70926
7093set statement optimizer_switch='condition_pushdown_for_derived=on' for
7094select * from
7095(select a from t1 where a < 4 union select a from t1 where a > 5
7096order by a limit 5) t where t.a not in (2,9);
7097a
70980
70991
71003
71016
7102drop table t1;
7103#
7104# MDEV-11072: pushdown of the condition obtained
7105#             after constant row substitution
7106#
7107CREATE TABLE t1 (a INT) ENGINE=MyISAM;
7108CREATE TABLE t2 (b INT) ENGINE=MyISAM;
7109CREATE OR REPLACE VIEW v2 AS SELECT * FROM t2;
7110CREATE TABLE t3 (c INT) ENGINE=MyISAM;
7111CREATE OR REPLACE ALGORITHM=TEMPTABLE VIEW v3 AS SELECT * FROM t3;
7112SELECT * FROM t1 WHERE a IN (
7113SELECT b FROM v2 WHERE b < a OR b IN (
7114SELECT c FROM v3 WHERE c = a
7115)
7116);
7117a
7118INSERT INTO t1 VALUES (2);
7119INSERT INTO t2 VALUES (3), (2);
7120INSERT INTO t3 VALUES (4), (1), (2), (7);
7121SELECT * FROM t1 WHERE a IN (
7122SELECT b FROM v2 WHERE b < a OR b IN (
7123SELECT c FROM v3 WHERE c = a
7124)
7125);
7126a
71272
7128EXPLAIN FORMAT=JSON
7129SELECT * FROM t1 WHERE a IN (
7130SELECT b FROM v2 WHERE b < a OR b IN (
7131SELECT c FROM v3 WHERE c = a
7132)
7133);
7134EXPLAIN
7135{
7136  "query_block": {
7137    "select_id": 1,
7138    "const_condition": "0 or <in_optimizer>(2,<exists>(subquery#3))",
7139    "table": {
7140      "table_name": "t1",
7141      "access_type": "system",
7142      "rows": 1,
7143      "filtered": 100
7144    },
7145    "table": {
7146      "table_name": "t2",
7147      "access_type": "ALL",
7148      "rows": 2,
7149      "filtered": 100,
7150      "attached_condition": "t2.b = 2",
7151      "first_match": "t1"
7152    },
7153    "subqueries": [
7154      {
7155        "query_block": {
7156          "select_id": 3,
7157          "table": {
7158            "table_name": "<derived5>",
7159            "access_type": "index_subquery",
7160            "possible_keys": ["key0"],
7161            "key": "key0",
7162            "key_length": "5",
7163            "used_key_parts": ["c"],
7164            "ref": ["func"],
7165            "rows": 2,
7166            "filtered": 100,
7167            "materialized": {
7168              "query_block": {
7169                "select_id": 5,
7170                "table": {
7171                  "table_name": "t3",
7172                  "access_type": "ALL",
7173                  "rows": 4,
7174                  "filtered": 100,
7175                  "attached_condition": "t3.c = 2"
7176                }
7177              }
7178            }
7179          }
7180        }
7181      }
7182    ]
7183  }
7184}
7185CREATE TABLE t4 (d INT, e INT) ENGINE=MyISAM;
7186INSERT INTO t4 VALUES (1,10),(3,11),(2,10),(2,20),(3,21);
7187CREATE OR REPLACE VIEW v4 AS
7188SELECT d, sum(e) AS s FROM t4 GROUP BY d;
7189set statement optimizer_switch='condition_pushdown_for_derived=off' for SELECT * FROM t1 WHERE a IN (
7190SELECT b FROM v2 WHERE b < a OR b IN (
7191SELECT d FROM v4 WHERE s > a
7192)
7193);
7194a
71952
7196SELECT * FROM t1 WHERE a IN (
7197SELECT b FROM v2 WHERE b < a OR b IN (
7198SELECT d FROM v4 WHERE s > a
7199)
7200);
7201a
72022
7203explain SELECT * FROM t1 WHERE a IN (
7204SELECT b FROM v2 WHERE b < a OR b IN (
7205SELECT d FROM v4 WHERE s > a
7206)
7207);
7208id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
72091	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1
72101	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; FirstMatch(t1)
72113	DEPENDENT SUBQUERY	<derived5>	index_subquery	key0	key0	5	func	2	Using where
72125	DERIVED	t4	ALL	NULL	NULL	NULL	NULL	5	Using temporary; Using filesort
7213explain format=json SELECT * FROM t1 WHERE a IN (
7214SELECT b FROM v2 WHERE b < a OR b IN (
7215SELECT d FROM v4 WHERE s > a
7216)
7217);
7218EXPLAIN
7219{
7220  "query_block": {
7221    "select_id": 1,
7222    "const_condition": "0 or <in_optimizer>(2,<exists>(subquery#3))",
7223    "table": {
7224      "table_name": "t1",
7225      "access_type": "system",
7226      "rows": 1,
7227      "filtered": 100
7228    },
7229    "table": {
7230      "table_name": "t2",
7231      "access_type": "ALL",
7232      "rows": 2,
7233      "filtered": 100,
7234      "attached_condition": "t2.b = 2",
7235      "first_match": "t1"
7236    },
7237    "subqueries": [
7238      {
7239        "query_block": {
7240          "select_id": 3,
7241          "table": {
7242            "table_name": "<derived5>",
7243            "access_type": "index_subquery",
7244            "possible_keys": ["key0"],
7245            "key": "key0",
7246            "key_length": "5",
7247            "used_key_parts": ["d"],
7248            "ref": ["func"],
7249            "rows": 2,
7250            "filtered": 100,
7251            "materialized": {
7252              "query_block": {
7253                "select_id": 5,
7254                "having_condition": "s > 2",
7255                "filesort": {
7256                  "sort_key": "t4.d",
7257                  "temporary_table": {
7258                    "table": {
7259                      "table_name": "t4",
7260                      "access_type": "ALL",
7261                      "rows": 5,
7262                      "filtered": 100
7263                    }
7264                  }
7265                }
7266              }
7267            }
7268          }
7269        }
7270      }
7271    ]
7272  }
7273}
7274DROP VIEW v2,v3,v4;
7275DROP TABLE t1,t2,t3,t4;
7276#
7277# MDEV-10800: pushdown of the condition obtained
7278#             after constant row substitution
7279#
7280CREATE TABLE t1 (a INT) ENGINE=MyISAM;
7281INSERT INTO t1 VALUES (1);
7282CREATE TABLE t2 (b INT) ENGINE=MyISAM;
7283INSERT INTO t2 VALUES (3),(4);
7284CREATE OR REPLACE ALGORITHM=TEMPTABLE VIEW v2 AS SELECT * FROM t2;
7285SELECT * FROM
7286( SELECT * FROM t1
7287WHERE EXISTS ( SELECT * FROM v2 WHERE b = a ) ) AS sq;
7288a
7289EXPLAIN FORMAT=JSON
7290SELECT * FROM
7291( SELECT * FROM t1
7292WHERE EXISTS ( SELECT * FROM v2 WHERE b = a ) ) AS sq;
7293EXPLAIN
7294{
7295  "query_block": {
7296    "select_id": 1,
7297    "const_condition": "<in_optimizer>(1,exists(subquery#3))",
7298    "table": {
7299      "table_name": "t1",
7300      "access_type": "system",
7301      "rows": 1,
7302      "filtered": 100
7303    },
7304    "subqueries": [
7305      {
7306        "query_block": {
7307          "select_id": 3,
7308          "table": {
7309            "table_name": "<derived4>",
7310            "access_type": "ALL",
7311            "rows": 2,
7312            "filtered": 100,
7313            "attached_condition": "v2.b = 1",
7314            "materialized": {
7315              "query_block": {
7316                "select_id": 4,
7317                "table": {
7318                  "table_name": "t2",
7319                  "access_type": "ALL",
7320                  "rows": 2,
7321                  "filtered": 100,
7322                  "attached_condition": "t2.b = 1"
7323                }
7324              }
7325            }
7326          }
7327        }
7328      }
7329    ]
7330  }
7331}
7332DROP VIEW v2;
7333DROP TABLE t1,t2;
7334#
7335# MDEV-11102: condition pushdown into materialized inner table
7336#              of outer join is not applied as not being valid
7337#
7338CREATE TABLE t1 (a INT);
7339INSERT INTO t1 VALUES (0),(2);
7340CREATE TABLE t2 (b INT);
7341INSERT INTO t2 VALUES (1),(2);
7342CREATE OR REPLACE ALGORITHM=TEMPTABLE VIEW v2 AS SELECT * FROM t2;
7343SELECT * FROM t1 LEFT JOIN t2 ON a = b WHERE b IS NULL;
7344a	b
73450	NULL
7346SELECT * FROM t1 LEFT JOIN v2 ON a = b WHERE b IS NULL;
7347a	b
73480	NULL
7349EXPLAIN FORMAT=JSON
7350SELECT * FROM t1 LEFT JOIN v2 ON a = b WHERE b IS NULL;
7351EXPLAIN
7352{
7353  "query_block": {
7354    "select_id": 1,
7355    "table": {
7356      "table_name": "t1",
7357      "access_type": "ALL",
7358      "rows": 2,
7359      "filtered": 100
7360    },
7361    "table": {
7362      "table_name": "<derived2>",
7363      "access_type": "ref",
7364      "possible_keys": ["key0"],
7365      "key": "key0",
7366      "key_length": "5",
7367      "used_key_parts": ["b"],
7368      "ref": ["test.t1.a"],
7369      "rows": 2,
7370      "filtered": 100,
7371      "attached_condition": "trigcond(v2.b is null) and trigcond(trigcond(t1.a is not null))",
7372      "materialized": {
7373        "query_block": {
7374          "select_id": 2,
7375          "table": {
7376            "table_name": "t2",
7377            "access_type": "ALL",
7378            "rows": 2,
7379            "filtered": 100
7380          }
7381        }
7382      }
7383    }
7384  }
7385}
7386DROP VIEW v2;
7387DROP TABLE t1,t2;
7388#
7389# MDEV-11103: pushdown condition with ANY subquery
7390#
7391CREATE TABLE t1 (i INT);
7392CREATE OR REPLACE ALGORITHM=TEMPTABLE VIEW v1 AS SELECT * FROM t1;
7393INSERT INTO t1 VALUES (1),(2);
7394EXPLAIN FORMAT=JSON
7395SELECT * FROM v1 WHERE i <= ANY ( SELECT 3 );
7396EXPLAIN
7397{
7398  "query_block": {
7399    "select_id": 1,
7400    "table": {
7401      "table_name": "<derived3>",
7402      "access_type": "ALL",
7403      "rows": 2,
7404      "filtered": 100,
7405      "attached_condition": "<nop>(v1.i <= 3)",
7406      "materialized": {
7407        "query_block": {
7408          "select_id": 3,
7409          "table": {
7410            "table_name": "t1",
7411            "access_type": "ALL",
7412            "rows": 2,
7413            "filtered": 100,
7414            "attached_condition": "<nop>(t1.i <= 3)"
7415          }
7416        }
7417      }
7418    }
7419  }
7420}
7421Warnings:
7422Note	1249	Select 2 was reduced during optimization
7423SELECT * FROM v1 WHERE i <= ANY ( SELECT 3 );
7424i
74251
74262
7427DROP VIEW v1;
7428DROP TABLE t1;
7429#
7430# MDEV-11315: condition with outer reference to mergeable derived
7431#
7432CREATE TABLE t1 (pk1 INT PRIMARY KEY, a INT, b INT) ENGINE=MyISAM;
7433INSERT INTO t1 VALUES (10,7,1),(11,0,2);
7434CREATE TABLE t2 (pk2 INT PRIMARY KEY, c INT, d DATETIME) ENGINE=MyISAM;
7435INSERT INTO t2 VALUES
7436(1,4,'2008-09-27 00:34:58'),
7437(2,5,'2007-05-28 00:00:00'),
7438(3,6,'2009-07-25 09:21:20');
7439CREATE VIEW v1 AS SELECT * FROM t1;
7440CREATE ALGORITHM=TEMPTABLE VIEW v2 AS SELECT * FROM t2;
7441SELECT * FROM v1 AS sq
7442WHERE b IN ( SELECT pk2 FROM v2 WHERE c > sq.b ) OR b = 100;
7443pk1	a	b
744410	7	1
744511	0	2
7446EXPLAIN FORMAT=JSON
7447SELECT * FROM v1 AS sq
7448WHERE b IN ( SELECT pk2 FROM v2 WHERE c > sq.b ) OR b = 100;
7449EXPLAIN
7450{
7451  "query_block": {
7452    "select_id": 1,
7453    "table": {
7454      "table_name": "t1",
7455      "access_type": "ALL",
7456      "rows": 2,
7457      "filtered": 100,
7458      "attached_condition": "<in_optimizer>(t1.b,<exists>(subquery#2)) or t1.b = 100"
7459    },
7460    "subqueries": [
7461      {
7462        "query_block": {
7463          "select_id": 2,
7464          "table": {
7465            "table_name": "<derived4>",
7466            "access_type": "index_subquery",
7467            "possible_keys": ["key0"],
7468            "key": "key0",
7469            "key_length": "4",
7470            "used_key_parts": ["pk2"],
7471            "ref": ["func"],
7472            "rows": 2,
7473            "filtered": 100,
7474            "materialized": {
7475              "query_block": {
7476                "select_id": 4,
7477                "table": {
7478                  "table_name": "t2",
7479                  "access_type": "ALL",
7480                  "rows": 3,
7481                  "filtered": 100
7482                }
7483              }
7484            }
7485          }
7486        }
7487      }
7488    ]
7489  }
7490}
7491SELECT * FROM ( SELECT * FROM t1 ) AS sq
7492WHERE b IN ( SELECT pk2 FROM v2 WHERE c > sq.b ) OR b = 100;
7493pk1	a	b
749410	7	1
749511	0	2
7496EXPLAIN FORMAT=JSON
7497SELECT * FROM ( SELECT * FROM t1 ) AS sq
7498WHERE b IN ( SELECT pk2 FROM v2 WHERE c > sq.b ) OR b = 100;
7499EXPLAIN
7500{
7501  "query_block": {
7502    "select_id": 1,
7503    "table": {
7504      "table_name": "t1",
7505      "access_type": "ALL",
7506      "rows": 2,
7507      "filtered": 100,
7508      "attached_condition": "<in_optimizer>(t1.b,<exists>(subquery#3)) or t1.b = 100"
7509    },
7510    "subqueries": [
7511      {
7512        "query_block": {
7513          "select_id": 3,
7514          "table": {
7515            "table_name": "<derived4>",
7516            "access_type": "index_subquery",
7517            "possible_keys": ["key0"],
7518            "key": "key0",
7519            "key_length": "4",
7520            "used_key_parts": ["pk2"],
7521            "ref": ["func"],
7522            "rows": 2,
7523            "filtered": 100,
7524            "materialized": {
7525              "query_block": {
7526                "select_id": 4,
7527                "table": {
7528                  "table_name": "t2",
7529                  "access_type": "ALL",
7530                  "rows": 3,
7531                  "filtered": 100
7532                }
7533              }
7534            }
7535          }
7536        }
7537      }
7538    ]
7539  }
7540}
7541DROP VIEW v1,v2;
7542DROP TABLE t1,t2;
7543#
7544# MDEV-11313: pushdown of the condition obtained
7545#             after constant row substitution
7546#
7547CREATE TABLE t1 (a INT) ENGINE=MyISAM;
7548INSERT INTO t1 VALUES (1),(2);
7549CREATE TABLE t2 (b INT) ENGINE=MyISAM;
7550INSERT INTO t2 VALUES (50);
7551CREATE ALGORITHM=TEMPTABLE VIEW v1 AS SELECT * FROM t1;
7552SELECT ( SELECT COUNT(*) FROM v1 WHERE a = t2.b ) AS f FROM t2 GROUP BY f;
7553f
75540
7555EXPLAIN FORMAT=JSON
7556SELECT ( SELECT COUNT(*) FROM v1 WHERE a = t2.b ) AS f FROM t2 GROUP BY f;
7557EXPLAIN
7558{
7559  "query_block": {
7560    "select_id": 1,
7561    "table": {
7562      "table_name": "t2",
7563      "access_type": "system",
7564      "rows": 1,
7565      "filtered": 100
7566    },
7567    "subqueries": [
7568      {
7569        "query_block": {
7570          "select_id": 2,
7571          "table": {
7572            "table_name": "<derived3>",
7573            "access_type": "ALL",
7574            "rows": 2,
7575            "filtered": 100,
7576            "attached_condition": "v1.a = 50",
7577            "materialized": {
7578              "query_block": {
7579                "select_id": 3,
7580                "table": {
7581                  "table_name": "t1",
7582                  "access_type": "ALL",
7583                  "rows": 2,
7584                  "filtered": 100,
7585                  "attached_condition": "t1.a = 50"
7586                }
7587              }
7588            }
7589          }
7590        }
7591      }
7592    ]
7593  }
7594}
7595CREATE TABLE t3 (a INT, b INT) ENGINE=MYISAM;
7596INSERT INTO t3 VALUES (1,10),(3,11),(2,10),(2,20),(3,21);
7597CREATE VIEW v2 AS SELECT a, sum(b) AS s FROM t3 GROUP BY a ;
7598SELECT ( SELECT COUNT(*) FROM v2 WHERE s < t2.b ) AS f FROM t2 GROUP BY f;
7599f
76003
7601EXPLAIN FORMAT=JSON
7602SELECT ( SELECT COUNT(*) FROM v2 WHERE s < t2.b ) AS f FROM t2 GROUP BY f;
7603EXPLAIN
7604{
7605  "query_block": {
7606    "select_id": 1,
7607    "table": {
7608      "table_name": "t2",
7609      "access_type": "system",
7610      "rows": 1,
7611      "filtered": 100
7612    },
7613    "subqueries": [
7614      {
7615        "query_block": {
7616          "select_id": 2,
7617          "table": {
7618            "table_name": "<derived3>",
7619            "access_type": "ALL",
7620            "rows": 5,
7621            "filtered": 100,
7622            "attached_condition": "v2.s < 50",
7623            "materialized": {
7624              "query_block": {
7625                "select_id": 3,
7626                "having_condition": "s < 50",
7627                "filesort": {
7628                  "sort_key": "t3.a",
7629                  "temporary_table": {
7630                    "table": {
7631                      "table_name": "t3",
7632                      "access_type": "ALL",
7633                      "rows": 5,
7634                      "filtered": 100
7635                    }
7636                  }
7637                }
7638              }
7639            }
7640          }
7641        }
7642      }
7643    ]
7644  }
7645}
7646DROP VIEW v1,v2;
7647DROP TABLE t1,t2,t3;
7648#
7649# MDEV-10882: pushdown of the predicate with cached value
7650#
7651CREATE TABLE t1 (a INT NOT NULL, b INT NOT NULL) ENGINE=MyISAM;
7652CREATE OR REPLACE ALGORITHM=TEMPTABLE VIEW v1 AS SELECT * FROM t1;
7653INSERT INTO t1 VALUES (1,2),(3,4);
7654CREATE TABLE t2 (c INT NOT NULL) ENGINE=MyISAM;
7655INSERT INTO t2 VALUES (5),(6);
7656SELECT a, GROUP_CONCAT(b) FROM v1
7657WHERE b IN ( SELECT COUNT(c) FROM t2 ) GROUP BY a;
7658a	GROUP_CONCAT(b)
76591	2
7660EXPLAIN FORMAT=JSON
7661SELECT a, GROUP_CONCAT(b) FROM v1
7662WHERE b IN ( SELECT COUNT(c) FROM t2 ) GROUP BY a;
7663EXPLAIN
7664{
7665  "query_block": {
7666    "select_id": 1,
7667    "table": {
7668      "table_name": "<subquery2>",
7669      "access_type": "system",
7670      "rows": 1,
7671      "filtered": 100,
7672      "materialized": {
7673        "unique": 1,
7674        "query_block": {
7675          "select_id": 2,
7676          "table": {
7677            "message": "Select tables optimized away"
7678          }
7679        }
7680      }
7681    },
7682    "read_sorted_file": {
7683      "filesort": {
7684        "sort_key": "v1.a",
7685        "table": {
7686          "table_name": "<derived3>",
7687          "access_type": "ALL",
7688          "rows": 2,
7689          "filtered": 100,
7690          "attached_condition": "v1.b = 2",
7691          "materialized": {
7692            "query_block": {
7693              "select_id": 3,
7694              "table": {
7695                "table_name": "t1",
7696                "access_type": "ALL",
7697                "rows": 2,
7698                "filtered": 100,
7699                "attached_condition": "t1.b = 2"
7700              }
7701            }
7702          }
7703        }
7704      }
7705    }
7706  }
7707}
7708DROP VIEW v1;
7709DROP TABLE t1,t2;
7710#
7711# MDEV-10836: pushdown of the predicate with cached value
7712#
7713CREATE TABLE t (pk INT PRIMARY KEY, f INT) ENGINE=MyISAM;
7714CREATE ALGORITHM=TEMPTABLE VIEW v AS SELECT * FROM t;
7715INSERT INTO t VALUES (1,1),(3,2);
7716SELECT * FROM v AS v1, v AS v2
7717WHERE v2.pk > v1.f AND v1.f IN ( SELECT COUNT(pk) FROM t );
7718pk	f	pk	f
77193	2	3	2
7720EXPLAIN FORMAT=JSON
7721SELECT * FROM v AS v1, v AS v2
7722WHERE v2.pk > v1.f AND v1.f IN ( SELECT COUNT(pk) FROM t );
7723EXPLAIN
7724{
7725  "query_block": {
7726    "select_id": 1,
7727    "table": {
7728      "table_name": "<subquery2>",
7729      "access_type": "system",
7730      "rows": 1,
7731      "filtered": 100,
7732      "materialized": {
7733        "unique": 1,
7734        "query_block": {
7735          "select_id": 2,
7736          "table": {
7737            "message": "Select tables optimized away"
7738          }
7739        }
7740      }
7741    },
7742    "table": {
7743      "table_name": "<derived3>",
7744      "access_type": "ALL",
7745      "rows": 2,
7746      "filtered": 100,
7747      "attached_condition": "v1.f = 2",
7748      "materialized": {
7749        "query_block": {
7750          "select_id": 3,
7751          "table": {
7752            "table_name": "t",
7753            "access_type": "ALL",
7754            "rows": 2,
7755            "filtered": 100,
7756            "attached_condition": "t.f = 2"
7757          }
7758        }
7759      }
7760    },
7761    "block-nl-join": {
7762      "table": {
7763        "table_name": "<derived4>",
7764        "access_type": "ALL",
7765        "rows": 2,
7766        "filtered": 100,
7767        "attached_condition": "v2.pk > 2"
7768      },
7769      "buffer_type": "flat",
7770      "buffer_size": "119",
7771      "join_type": "BNL",
7772      "materialized": {
7773        "query_block": {
7774          "select_id": 4,
7775          "table": {
7776            "table_name": "t",
7777            "access_type": "range",
7778            "possible_keys": ["PRIMARY"],
7779            "key": "PRIMARY",
7780            "key_length": "4",
7781            "used_key_parts": ["pk"],
7782            "rows": 1,
7783            "filtered": 100,
7784            "index_condition": "t.pk > 2"
7785          }
7786        }
7787      }
7788    }
7789  }
7790}
7791DROP VIEW v;
7792DROP TABLE t;
7793#
7794# MDEV-11488: pushdown of the predicate with cached value
7795#
7796CREATE TABLE t1 (i INT) ENGINE=MyISAM;
7797INSERT INTO t1 VALUES (1),(3),(2);
7798CREATE TABLE t2 (j INT, KEY(j)) ENGINE=MyISAM;
7799INSERT INTO t2 VALUES (3),(4);
7800SELECT * FROM ( SELECT DISTINCT * FROM t1 ) AS sq
7801WHERE i IN ( SELECT MIN(j) FROM t2 );
7802i
78033
7804EXPLAIN FORMAT=JSON
7805SELECT * FROM ( SELECT DISTINCT * FROM t1 ) AS sq
7806WHERE i IN ( SELECT MIN(j) FROM t2 );
7807EXPLAIN
7808{
7809  "query_block": {
7810    "select_id": 1,
7811    "table": {
7812      "table_name": "<subquery3>",
7813      "access_type": "system",
7814      "rows": 1,
7815      "filtered": 100,
7816      "materialized": {
7817        "unique": 1,
7818        "query_block": {
7819          "select_id": 3,
7820          "table": {
7821            "message": "Select tables optimized away"
7822          }
7823        }
7824      }
7825    },
7826    "table": {
7827      "table_name": "<derived2>",
7828      "access_type": "ALL",
7829      "rows": 3,
7830      "filtered": 100,
7831      "attached_condition": "sq.i = 3",
7832      "materialized": {
7833        "query_block": {
7834          "select_id": 2,
7835          "table": {
7836            "table_name": "t1",
7837            "access_type": "ALL",
7838            "rows": 3,
7839            "filtered": 100,
7840            "attached_condition": "t1.i = 3"
7841          }
7842        }
7843      }
7844    }
7845  }
7846}
7847UPDATE t2 SET j = 2 WHERE j = 3;
7848SELECT * FROM ( SELECT DISTINCT * FROM t1 ) AS sq
7849WHERE i IN ( SELECT MIN(j) FROM t2 );
7850i
78512
7852DROP TABLE t1,t2;
7853CREATE TABLE t1 (i FLOAT) ENGINE=MyISAM;
7854INSERT INTO t1 VALUES (1.5),(3.2),(2.71);
7855CREATE TABLE t2 (j FLOAT, KEY(j)) ENGINE=MyISAM;
7856INSERT INTO t2 VALUES (3.2),(2.71);
7857SELECT * FROM ( SELECT DISTINCT * FROM t1 ) AS sq
7858WHERE i IN ( SELECT MIN(j) FROM t2 );
7859i
78602.71
7861EXPLAIN FORMAT=JSON
7862SELECT * FROM ( SELECT DISTINCT * FROM t1 ) AS sq
7863WHERE i IN ( SELECT MIN(j) FROM t2 );
7864EXPLAIN
7865{
7866  "query_block": {
7867    "select_id": 1,
7868    "table": {
7869      "table_name": "<subquery3>",
7870      "access_type": "system",
7871      "rows": 1,
7872      "filtered": 100,
7873      "materialized": {
7874        "unique": 1,
7875        "query_block": {
7876          "select_id": 3,
7877          "table": {
7878            "message": "Select tables optimized away"
7879          }
7880        }
7881      }
7882    },
7883    "table": {
7884      "table_name": "<derived2>",
7885      "access_type": "ALL",
7886      "rows": 3,
7887      "filtered": 100,
7888      "attached_condition": "sq.i = 2.71",
7889      "materialized": {
7890        "query_block": {
7891          "select_id": 2,
7892          "table": {
7893            "table_name": "t1",
7894            "access_type": "ALL",
7895            "rows": 3,
7896            "filtered": 100,
7897            "attached_condition": "t1.i = 2.7100000381469727"
7898          }
7899        }
7900      }
7901    }
7902  }
7903}
7904DROP TABLE t1,t2;
7905CREATE TABLE t1 (i DECIMAL(10,2)) ENGINE=MyISAM;
7906INSERT INTO t1 VALUES (1.5),(3.21),(2.47);
7907CREATE TABLE t2 (j DECIMAL(10,2), KEY(j)) ENGINE=MyISAM;
7908INSERT INTO t2 VALUES (3.21),(4.55);
7909SELECT * FROM ( SELECT DISTINCT * FROM t1 ) AS sq
7910WHERE i IN ( SELECT MIN(j) FROM t2 );
7911i
79123.21
7913EXPLAIN FORMAT=JSON
7914SELECT * FROM ( SELECT DISTINCT * FROM t1 ) AS sq
7915WHERE i IN ( SELECT MIN(j) FROM t2 );
7916EXPLAIN
7917{
7918  "query_block": {
7919    "select_id": 1,
7920    "table": {
7921      "table_name": "<subquery3>",
7922      "access_type": "system",
7923      "rows": 1,
7924      "filtered": 100,
7925      "materialized": {
7926        "unique": 1,
7927        "query_block": {
7928          "select_id": 3,
7929          "table": {
7930            "message": "Select tables optimized away"
7931          }
7932        }
7933      }
7934    },
7935    "table": {
7936      "table_name": "<derived2>",
7937      "access_type": "ALL",
7938      "rows": 3,
7939      "filtered": 100,
7940      "attached_condition": "sq.i = 3.21",
7941      "materialized": {
7942        "query_block": {
7943          "select_id": 2,
7944          "table": {
7945            "table_name": "t1",
7946            "access_type": "ALL",
7947            "rows": 3,
7948            "filtered": 100,
7949            "attached_condition": "t1.i = 3.21"
7950          }
7951        }
7952      }
7953    }
7954  }
7955}
7956DROP TABLE t1,t2;
7957CREATE TABLE t1 (i VARCHAR(32)) ENGINE=MyISAM;
7958INSERT INTO t1 VALUES ('cc'),('aa'),('ddd');
7959CREATE TABLE t2 (j VARCHAR(16), KEY(j)) ENGINE=MyISAM;
7960INSERT INTO t2 VALUES ('bbb'),('aa');
7961SELECT * FROM ( SELECT DISTINCT * FROM t1 ) AS sq
7962WHERE i IN ( SELECT MIN(j) FROM t2 );
7963i
7964aa
7965EXPLAIN FORMAT=JSON
7966SELECT * FROM ( SELECT DISTINCT * FROM t1 ) AS sq
7967WHERE i IN ( SELECT MIN(j) FROM t2 );
7968EXPLAIN
7969{
7970  "query_block": {
7971    "select_id": 1,
7972    "table": {
7973      "table_name": "<subquery3>",
7974      "access_type": "system",
7975      "rows": 1,
7976      "filtered": 100,
7977      "materialized": {
7978        "unique": 1,
7979        "query_block": {
7980          "select_id": 3,
7981          "table": {
7982            "message": "Select tables optimized away"
7983          }
7984        }
7985      }
7986    },
7987    "table": {
7988      "table_name": "<derived2>",
7989      "access_type": "ALL",
7990      "rows": 3,
7991      "filtered": 100,
7992      "attached_condition": "sq.i = 'aa'",
7993      "materialized": {
7994        "query_block": {
7995          "select_id": 2,
7996          "table": {
7997            "table_name": "t1",
7998            "access_type": "ALL",
7999            "rows": 3,
8000            "filtered": 100,
8001            "attached_condition": "t1.i = 'aa'"
8002          }
8003        }
8004      }
8005    }
8006  }
8007}
8008DROP TABLE t1,t2;
8009CREATE TABLE t1 (i DATETIME) ENGINE=MyISAM;
8010INSERT INTO t1 VALUES
8011('2008-09-27 00:34:58'),('2007-05-28 00:00:00'), ('2009-07-25 09:21:20');
8012CREATE TABLE t2 (j DATETIME, KEY(j)) ENGINE=MyISAM;
8013INSERT INTO t2 VALUES
8014('2007-05-28 00:00:00'), ('2010-08-25 00:00:00');
8015SELECT * FROM ( SELECT DISTINCT * FROM t1 ) AS sq
8016WHERE i IN ( SELECT MIN(j) FROM t2 );
8017i
80182007-05-28 00:00:00
8019EXPLAIN FORMAT=JSON
8020SELECT * FROM ( SELECT DISTINCT * FROM t1 ) AS sq
8021WHERE i IN ( SELECT MIN(j) FROM t2 );
8022EXPLAIN
8023{
8024  "query_block": {
8025    "select_id": 1,
8026    "table": {
8027      "table_name": "<subquery3>",
8028      "access_type": "system",
8029      "rows": 1,
8030      "filtered": 100,
8031      "materialized": {
8032        "unique": 1,
8033        "query_block": {
8034          "select_id": 3,
8035          "table": {
8036            "message": "Select tables optimized away"
8037          }
8038        }
8039      }
8040    },
8041    "table": {
8042      "table_name": "<derived2>",
8043      "access_type": "ALL",
8044      "rows": 3,
8045      "filtered": 100,
8046      "attached_condition": "sq.i = '2007-05-28 00:00:00'",
8047      "materialized": {
8048        "query_block": {
8049          "select_id": 2,
8050          "table": {
8051            "table_name": "t1",
8052            "access_type": "ALL",
8053            "rows": 3,
8054            "filtered": 100,
8055            "attached_condition": "t1.i = TIMESTAMP'2007-05-28 00:00:00'"
8056          }
8057        }
8058      }
8059    }
8060  }
8061}
8062DROP TABLE t1,t2;
8063CREATE TABLE t1 (i DATE) ENGINE=MyISAM;
8064INSERT INTO t1 VALUES ('2008-09-27'),('2007-05-28'), ('2009-07-25');
8065CREATE TABLE t2 (j DATE, KEY(j)) ENGINE=MyISAM;
8066INSERT INTO t2 VALUES ('2007-05-28'), ('2010-08-25');
8067SELECT * FROM ( SELECT DISTINCT * FROM t1 ) AS sq
8068WHERE i IN ( SELECT MIN(j) FROM t2 );
8069i
80702007-05-28
8071EXPLAIN FORMAT=JSON
8072SELECT * FROM ( SELECT DISTINCT * FROM t1 ) AS sq
8073WHERE i IN ( SELECT MIN(j) FROM t2 );
8074EXPLAIN
8075{
8076  "query_block": {
8077    "select_id": 1,
8078    "table": {
8079      "table_name": "<subquery3>",
8080      "access_type": "system",
8081      "rows": 1,
8082      "filtered": 100,
8083      "materialized": {
8084        "unique": 1,
8085        "query_block": {
8086          "select_id": 3,
8087          "table": {
8088            "message": "Select tables optimized away"
8089          }
8090        }
8091      }
8092    },
8093    "table": {
8094      "table_name": "<derived2>",
8095      "access_type": "ALL",
8096      "rows": 3,
8097      "filtered": 100,
8098      "attached_condition": "sq.i = '2007-05-28'",
8099      "materialized": {
8100        "query_block": {
8101          "select_id": 2,
8102          "table": {
8103            "table_name": "t1",
8104            "access_type": "ALL",
8105            "rows": 3,
8106            "filtered": 100,
8107            "attached_condition": "t1.i = DATE'2007-05-28'"
8108          }
8109        }
8110      }
8111    }
8112  }
8113}
8114DROP TABLE t1,t2;
8115CREATE TABLE t1 (i TIME) ENGINE=MyISAM;
8116INSERT INTO t1 VALUES ('00:34:58'),('10:00:02'), ('09:21:20');
8117CREATE TABLE t2 (j TIME, KEY(j)) ENGINE=MyISAM;
8118INSERT INTO t2 VALUES ('10:00:02'), ('11:00:10');
8119SELECT * FROM ( SELECT DISTINCT * FROM t1 ) AS sq
8120WHERE i IN ( SELECT MIN(j) FROM t2 );
8121i
812210:00:02
8123EXPLAIN FORMAT=JSON
8124SELECT * FROM ( SELECT DISTINCT * FROM t1 ) AS sq
8125WHERE i IN ( SELECT MIN(j) FROM t2 );
8126EXPLAIN
8127{
8128  "query_block": {
8129    "select_id": 1,
8130    "table": {
8131      "table_name": "<subquery3>",
8132      "access_type": "system",
8133      "rows": 1,
8134      "filtered": 100,
8135      "materialized": {
8136        "unique": 1,
8137        "query_block": {
8138          "select_id": 3,
8139          "table": {
8140            "message": "Select tables optimized away"
8141          }
8142        }
8143      }
8144    },
8145    "table": {
8146      "table_name": "<derived2>",
8147      "access_type": "ALL",
8148      "rows": 3,
8149      "filtered": 100,
8150      "attached_condition": "sq.i = '10:00:02'",
8151      "materialized": {
8152        "query_block": {
8153          "select_id": 2,
8154          "table": {
8155            "table_name": "t1",
8156            "access_type": "ALL",
8157            "rows": 3,
8158            "filtered": 100,
8159            "attached_condition": "t1.i = TIME'10:00:02'"
8160          }
8161        }
8162      }
8163    }
8164  }
8165}
8166DROP TABLE t1,t2;
8167#
8168# MDEV-11593: pushdown of condition with NULLIF
8169#
8170CREATE TABLE t1 (i INT);
8171CREATE OR REPLACE ALGORITHM=TEMPTABLE VIEW v1 AS SELECT * FROM t1;
8172INSERT INTO t1 VALUES (2), (1);
8173SELECT * FROM v1 WHERE NULLIF(1, i);
8174i
81752
8176EXPLAIN FORMAT=JSON
8177SELECT * FROM v1 WHERE NULLIF(1, i);
8178EXPLAIN
8179{
8180  "query_block": {
8181    "select_id": 1,
8182    "table": {
8183      "table_name": "<derived2>",
8184      "access_type": "ALL",
8185      "rows": 2,
8186      "filtered": 100,
8187      "attached_condition": "nullif(1,v1.i)",
8188      "materialized": {
8189        "query_block": {
8190          "select_id": 2,
8191          "table": {
8192            "table_name": "t1",
8193            "access_type": "ALL",
8194            "rows": 2,
8195            "filtered": 100,
8196            "attached_condition": "nullif(1,t1.i)"
8197          }
8198        }
8199      }
8200    }
8201  }
8202}
8203DROP VIEW v1;
8204DROP TABLE t1;
8205#
8206# MDEV-11608: pushdown of the predicate with cached null value
8207#
8208CREATE TABLE t1 (c VARCHAR(3));
8209CREATE ALGORITHM=TEMPTABLE VIEW v1 AS SELECT * FROM t1;
8210INSERT INTO t1 VALUES ('foo'),('bar');
8211CREATE TABLE t2 (c VARCHAR(3));
8212INSERT INTO t2 VALUES ('foo'),('xyz');
8213SELECT * FROM v1 WHERE v1.c IN ( SELECT MIN(c) FROM t2 WHERE 0 );
8214c
8215EXPLAIN FORMAT=JSON
8216SELECT * FROM v1 WHERE v1.c IN ( SELECT MIN(c) FROM t2 WHERE 0 );
8217EXPLAIN
8218{
8219  "query_block": {
8220    "select_id": 1,
8221    "table": {
8222      "message": "Impossible WHERE"
8223    },
8224    "subqueries": [
8225      {
8226        "query_block": {
8227          "select_id": 2,
8228          "table": {
8229            "message": "Impossible WHERE"
8230          }
8231        }
8232      }
8233    ]
8234  }
8235}
8236DROP VIEW v1;
8237DROP TABLE t1,t2;
8238CREATE TABLE t1 (d DECIMAL(10,2));
8239CREATE ALGORITHM=TEMPTABLE VIEW v1 AS SELECT * FROM t1;
8240INSERT INTO t1 VALUES (5.37),(1.1);
8241CREATE TABLE t2 (d DECIMAL(10,2));
8242INSERT INTO t2 VALUES ('1.1'),('2.23');
8243SELECT * FROM v1 WHERE v1.d IN ( SELECT MIN(d) FROM t2 WHERE 0 );
8244d
8245DROP VIEW v1;
8246DROP TABLE t1,t2;
8247#
8248# MDEV-11820: second execution of PS for query
8249#             with false subquery predicate in WHERE
8250#
8251CREATE TABLE t1 (c VARCHAR(3)) ENGINE=MyISAM;
8252INSERT INTO t1 VALUES ('foo'),('bar');
8253CREATE ALGORITHM=TEMPTABLE VIEW v1 AS SELECT * FROM t1;
8254CREATE TABLE t2 (a INT);
8255INSERT INTO t2 VALUES (3), (4);
8256PREPARE stmt1 FROM
8257" SELECT * FROM v1 WHERE 1 IN (SELECT a FROM t2) OR c = 'foo'";
8258PREPARE stmt2 FROM
8259"EXPLAIN FORMAT=JSON
8260 SELECT * FROM v1 WHERE 1 IN (SELECT a FROM t2) OR c = 'foo'";
8261EXECUTE stmt1;
8262c
8263foo
8264EXECUTE stmt2;
8265EXPLAIN
8266{
8267  "query_block": {
8268    "select_id": 1,
8269    "table": {
8270      "table_name": "<derived3>",
8271      "access_type": "ALL",
8272      "rows": 2,
8273      "filtered": 100,
8274      "attached_condition": "v1.c = 'foo'",
8275      "materialized": {
8276        "query_block": {
8277          "select_id": 3,
8278          "table": {
8279            "table_name": "t1",
8280            "access_type": "ALL",
8281            "rows": 2,
8282            "filtered": 100,
8283            "attached_condition": "t1.c = 'foo'"
8284          }
8285        }
8286      }
8287    },
8288    "subqueries": [
8289      {
8290        "query_block": {
8291          "select_id": 2,
8292          "table": {
8293            "table_name": "t2",
8294            "access_type": "ALL",
8295            "rows": 2,
8296            "filtered": 100,
8297            "attached_condition": "1 = t2.a"
8298          }
8299        }
8300      }
8301    ]
8302  }
8303}
8304INSERT INTO t2 SELECT a+1 FROM t2;
8305INSERT INTO t2 SELECT a+1 FROM t2;
8306INSERT INTO t2 SELECT a+1 FROM t2;
8307INSERT INTO t2 SELECT a+1 FROM t2;
8308INSERT INTO t2 SELECT a+1 FROM t2;
8309INSERT INTO t2 SELECT a+1 FROM t2;
8310EXECUTE stmt1;
8311c
8312foo
8313EXECUTE stmt2;
8314EXPLAIN
8315{
8316  "query_block": {
8317    "select_id": 1,
8318    "table": {
8319      "table_name": "<derived3>",
8320      "access_type": "ALL",
8321      "rows": 2,
8322      "filtered": 100,
8323      "attached_condition": "<cache>(<in_optimizer>(1,<exists>(subquery#2))) or v1.c = 'foo'",
8324      "materialized": {
8325        "query_block": {
8326          "select_id": 3,
8327          "table": {
8328            "table_name": "t1",
8329            "access_type": "ALL",
8330            "rows": 2,
8331            "filtered": 100
8332          }
8333        }
8334      }
8335    },
8336    "subqueries": [
8337      {
8338        "query_block": {
8339          "select_id": 2,
8340          "table": {
8341            "table_name": "t2",
8342            "access_type": "ALL",
8343            "rows": 128,
8344            "filtered": 100,
8345            "attached_condition": "1 = t2.a"
8346          }
8347        }
8348      }
8349    ]
8350  }
8351}
8352DEALLOCATE PREPARE stmt1;
8353DEALLOCATE PREPARE stmt2;
8354DROP VIEW v1;
8355DROP TABLE t1,t2;
8356#
8357# MDEV-12373: pushdown into derived with side effects is prohibited
8358#
8359CREATE TABLE sales_documents (
8360id           int NOT NULL AUTO_INCREMENT,
8361sale_id      int NULL DEFAULT NULL,
8362type         tinyint unsigned NULL DEFAULT NULL,
8363data         text NULL DEFAULT NULL COLLATE 'utf8_unicode_ci',
8364date         date NULL DEFAULT NULL,
8365order_number int unsigned NULL DEFAULT NULL,
8366created_at   int NULL DEFAULT NULL,
8367updated_at   int NULL DEFAULT NULL,
8368generated    tinyint NOT NULL DEFAULT '0',
8369synced_at    int NOT NULL DEFAULT '0',
8370sum          decimal(13,2) NOT NULL DEFAULT '0',
8371PRIMARY KEY (id)
8372);
8373INSERT INTO sales_documents
8374(id, sale_id, type, order_number, data, created_at,
8375updated_at, date, generated, synced_at, sum)
8376VALUES
8377(555, 165, 3, 5, '{}', 1486538300, 1486722835, '2017-02-17', 0, 1486538313, 2320.00),
8378(556, 165, 2, 3, '{}', 1486538304, 1486563125, '2017-02-08', 1, 1486538302, 2320.00),
8379(557, 158, 2, 2, '{}', 1486538661, 1486538661, '2017-02-08', 0, 1486538660, 2320.00),
8380(558, 171, 1, 3, '{}', 1486539104, 1488203405, '2017-02-08', 1, 1486539102, 23230.00),
8381(559, 171, 2, 5, '{}', 1486549233, 1487146010, '2017-02-08', 1, 1486549225, 37690.00),
8382(560, 172, 1, 1, '{}', 1486658260, 1488203409, '2017-02-09', 1, 1486658256, 40312.00),
8383(561, 172, 2, 1, '{}', 1486711997, 1486711997, '2017-02-10', 1, 1486711996, 40312.00),
8384(562, 172, 3, 1, '{}', 1486712104, 1486721395, '2017-02-10', 1, 1486712101, 40312.00),
8385(563, 171, 3, 2, '{}', 1486712953, 1486720244, '2017-02-10', 1, 1486712910, 23230.00),
8386(564, 170, 1, 2, '{}', 1486715948, 1488203410, '2017-02-10', 1, 1486715930, 28873.00),
8387(565, 170, 3, 3, '{}', 1486716782, 1486717426, '2017-02-10', 1, 1486716779, 61948.00),
8388(566, 166, 3, 4, '{}', 1486720947, 1486720947, '2017-02-10', 1, 1486720945, 4640.00),
8389(567, 167, 3, 5, '{}', 1486722741, 1486722783, '2017-02-26', 0, 1486722738, 14755.00),
8390(568, 165, 1, 4, '{}', 1486722849, 1486722849, '2017-02-10', 0, 1486722846, 2320.00),
8391(569, 173, 2, 2, '{}', 1486723073, 1487071275, '2017-02-10', 1, 1486723071, 14282.00),
8392(570, 173, 1, 4, '{}', 1486723100, 1488203412, '2017-02-10', 1, 1486723099, 14282.00),
8393(571, 167, 2, 4, '{}', 1486730859, 1486730859, '2017-02-10', 1, 1486730856, 18655.00),
8394(572, 167, 1, 5, '{}', 1486730883, 1488203412, '2017-02-10', 1, 1486730877, 18655.00),
8395(573, 174, 2, 51, '{}', 1486731622, 1487060259, '2017-02-10', 1, 1486731620, 7140.00),
8396(574, 174, 3, 5, '{}', 1486993472, 1486993472, '2017-02-13', 1, 1488216147, 28020.00),
8397(575, 174, 1, 6, '{}', 1486993530, 1488203412, '2017-02-13', 1, 1486993505, 7140.00),
8398(576, 173, 3, 6, '{}', 1487071425, 1487071425, '2017-02-14', 0, 1487071422, 14282.00),
8399(577, 178, 2, 6, '{}', 1487327372, 1487327372, '2017-02-17', 1, 1487327370, 12321.00),
8400(578, 177, 2, 7, '{}', 1487327394, 1487327394, '2017-02-17', 0, 1487327391, 4270.00),
8401(579, 182, 3, 6, '{}', 1487750589, 1487751693, '2017-02-22', 1, 1487751688, 4270.00),
8402(580, 182, 2, 7, '{}', 1487750601, 1487750663, '2017-02-22', 1, 1487750598, 4270.00),
8403(581, 182, 1, 7, '{}', 1487750694, 1488203412, '2017-02-22', 1, 1487750692, 4270.00),
8404(582, 185, 3, 7, '{}', 1487774051, 1487774051, '2017-02-22', 0, 1487774043, 8913.00),
8405(583, 184, 3, 7, '{}', 1487774071, 1487774235, '2017-02-22', 0, 1487774093, 3285.00),
8406(584, 184, 2, 8, '{}', 1487774074, 1487774074, '2017-02-22', 0, 1487774073, 3285.00),
8407(585, 184, 1, 8, '{}', 1487774081, 1487774081, '2017-02-22', 0, 1487774075, 3285.00),
8408(586, 193, 2, 8, '{}', 1487955294, 1487955318, '2017-02-24', 0, 1487955311, 4270.00),
8409(587, 193, 1, 8, '{}', 1487955324, 1487955324, '2017-02-24', 0, 1487955320, 4270.00),
8410(588, 193, 3, 7, '{}', 1487955341, 1487955341, '2017-02-24', 0, 1487955325, 4270.00),
8411(589, 186, 1, 8, '{}', 1487957291, 1487957464, '2017-02-24', 0, 1487957459, 6960.00),
8412(590, 186, 2, 8, '{}', 1487957308, 1487957468, '2017-02-24', 0, 1487957465, 6960.00),
8413(591, 186, 3, 7, '{}', 1487957312, 1487957473, '2017-02-24', 0, 1487957469, 6960.00),
8414(592, 194, 1, 8, '{}', 1488193293, 1488203412, '2017-02-27', 1, 1488193280, 2320.00),
8415(593, 194, 2, 8, '{}', 1488193304, 1488193304, '2017-02-27', 1, 1488193303, 2320.00),
8416(594, 210, 1, 9, '{}', 1488198896, 1488198896, '2017-02-27', 0, 1488198885, 4270.00),
8417(595, 210, 2, 12, '{}', 1488198901, 1488198901, '2017-02-27', 1, 1488532585, 4270.00),
8418(596, 210, 3, 10, '{}', 1488198904, 1488198904, '2017-02-27', 1, 1488532565, 4270.00),
8419(597, 209, 2, 9, '{}', 1488200016, 1488450772, '2017-02-27', 1, 1488450449, 4270.00),
8420(598, 209, 1, 9, '{}', 1488200020, 1488200063, '2017-02-27', 1, 1488200017, 4271.00),
8421(599, 209, 3, 7, '{}', 1488200053, 1488200053, '2017-02-27', 0, 1488200021, 4271.00),
8422(600, 211, 2, 10, '{}', 1488216265, 1489402027, '2017-02-27', 1, 1488216264, 2320.00),
8423(601, 211, 3, 7, '{}', 1488216281, 1488216281, '2017-02-27', 1, 1488216276, 2320.00),
8424(602, 211, 1, 10, '{}', 1488216283, 1488216283, '2017-02-27', 1, 1488216282, 2320.00),
8425(603, 198, 2, 11, '{}', 1488280125, 1488280125, '2017-02-28', 0, 1488280095, 4270.00),
8426(604, 198, 1, 11, '{}', 1488280160, 1488280160, '2017-02-28', 0, 1488280126, 4270.00),
8427(605, 198, 3, 8, '{}', 1488280440, 1488280440, '2017-02-28', 0, 1488280435, 4270.00),
8428(606, 212, 1, 12, '{}', 1488286301, 1489402168, '2017-02-28', 1, 1488286295, 13825.00),
8429(607, 212, 3, 8, '{}', 1488289644, 1488289690, '2017-02-28', 1, 1488289642, 25295.00),
8430(608, 212, 2, 13, '{}', 1488290350, 1488290431, '2017-02-28', 1, 1488290347, 13133.75),
8431(609, 213, 1, 11, '{}', 1488529470, 1488529470, '2017-03-03', 1, 1488529461, 5660.00),
8432(610, 213, 2, 11, '{}', 1488529484, 1488529484, '2017-03-03', 1, 1488529479, 5660.00),
8433(611, 213, 3, 9, '{}', 1488529493, 1488529493, '2017-03-03', 1, 1488529489, 5660.00),
8434(612, 197, 2, 13, '{}', 1489400715, 1489400715, '2017-03-13', 0, 1489398959, 4270.00),
8435(613, 219, 3, 11, '{}', 1490084337, 1490181958, '2017-03-21', 1, 1490084334, 73526.00),
8436(614, 216, 3, 11, '{}', 1490085757, 1490086717, '2017-03-21', 0, 1490085755, 5377.00);
8437SELECT * FROM
8438(SELECT @row := @row + 1 as row, a.* from (
8439SELECT t.order_number
8440FROM sales_documents t
8441WHERE
8442t.type = 2 AND
8443t.date >= '2017-01-01' AND
8444t.date <= '2017-12-31' AND
8445t.order_number IS NOT NULL AND
8446t.generated = 1
8447GROUP BY t.order_number
8448) a, (SELECT @row := 0) r) t
8449WHERE row <> order_number;
8450row	order_number
845114	51
8452DROP TABLE sales_documents;
8453#
8454# MDEV-12845: pushdown from merged derived using equalities
8455#
8456create table t1 (a int);
8457insert into t1 values
8458(4), (8), (5), (3), (10), (2), (7);
8459create table t2 (b int, c int);
8460insert into t2 values
8461(2,1), (5,2), (2,2), (4,1), (4,3),
8462(5,3), (2,4), (4,6), (2,1);
8463create view v1 as
8464select b, sum(c) as s from t2 group by b;
8465create view v2 as
8466select distinct b, c from t2;
8467create view v3 as
8468select b, max(c) as m from t2 group by b;
8469select b
8470from ( select t1.a, v1.b, v1.s from t1, v1 where t1.a = v1.b ) as t
8471where b > 2;
8472b
84734
84745
8475explain format=json select b
8476from ( select t1.a, v1.b, v1.s from t1, v1 where t1.a = v1.b ) as t
8477where b > 2;
8478EXPLAIN
8479{
8480  "query_block": {
8481    "select_id": 1,
8482    "table": {
8483      "table_name": "t1",
8484      "access_type": "ALL",
8485      "rows": 7,
8486      "filtered": 100,
8487      "attached_condition": "t1.a > 2 and t1.a is not null"
8488    },
8489    "table": {
8490      "table_name": "<derived3>",
8491      "access_type": "ref",
8492      "possible_keys": ["key0"],
8493      "key": "key0",
8494      "key_length": "5",
8495      "used_key_parts": ["b"],
8496      "ref": ["test.t1.a"],
8497      "rows": 2,
8498      "filtered": 100,
8499      "materialized": {
8500        "query_block": {
8501          "select_id": 3,
8502          "filesort": {
8503            "sort_key": "t2.b",
8504            "temporary_table": {
8505              "table": {
8506                "table_name": "t2",
8507                "access_type": "ALL",
8508                "rows": 9,
8509                "filtered": 100,
8510                "attached_condition": "t2.b > 2"
8511              }
8512            }
8513          }
8514        }
8515      }
8516    }
8517  }
8518}
8519select a
8520from ( select t1.a, v1.b, v1.s from t1, v1 where t1.a = v1.b ) as t
8521where a > 2;
8522a
85234
85245
8525explain format=json select a
8526from ( select t1.a, v1.b, v1.s from t1, v1 where t1.a = v1.b ) as t
8527where a > 2;
8528EXPLAIN
8529{
8530  "query_block": {
8531    "select_id": 1,
8532    "table": {
8533      "table_name": "t1",
8534      "access_type": "ALL",
8535      "rows": 7,
8536      "filtered": 100,
8537      "attached_condition": "t1.a > 2 and t1.a is not null"
8538    },
8539    "table": {
8540      "table_name": "<derived3>",
8541      "access_type": "ref",
8542      "possible_keys": ["key0"],
8543      "key": "key0",
8544      "key_length": "5",
8545      "used_key_parts": ["b"],
8546      "ref": ["test.t1.a"],
8547      "rows": 2,
8548      "filtered": 100,
8549      "materialized": {
8550        "query_block": {
8551          "select_id": 3,
8552          "filesort": {
8553            "sort_key": "t2.b",
8554            "temporary_table": {
8555              "table": {
8556                "table_name": "t2",
8557                "access_type": "ALL",
8558                "rows": 9,
8559                "filtered": 100,
8560                "attached_condition": "t2.b > 2"
8561              }
8562            }
8563          }
8564        }
8565      }
8566    }
8567  }
8568}
8569select a
8570from  ( select t1.a, v2.b, v2.c from t1, v2 where t1.a = v2.b ) as t
8571where a > 2;
8572a
85734
85744
85754
85765
85775
8578explain format=json select a
8579from  ( select t1.a, v2.b, v2.c from t1, v2 where t1.a = v2.b ) as t
8580where a > 2;
8581EXPLAIN
8582{
8583  "query_block": {
8584    "select_id": 1,
8585    "table": {
8586      "table_name": "t1",
8587      "access_type": "ALL",
8588      "rows": 7,
8589      "filtered": 100,
8590      "attached_condition": "t1.a > 2 and t1.a is not null"
8591    },
8592    "table": {
8593      "table_name": "<derived3>",
8594      "access_type": "ref",
8595      "possible_keys": ["key0"],
8596      "key": "key0",
8597      "key_length": "5",
8598      "used_key_parts": ["b"],
8599      "ref": ["test.t1.a"],
8600      "rows": 2,
8601      "filtered": 100,
8602      "materialized": {
8603        "query_block": {
8604          "select_id": 3,
8605          "temporary_table": {
8606            "table": {
8607              "table_name": "t2",
8608              "access_type": "ALL",
8609              "rows": 9,
8610              "filtered": 100,
8611              "attached_condition": "t2.b > 2"
8612            }
8613          }
8614        }
8615      }
8616    }
8617  }
8618}
8619select a
8620from  ( select t1.a, v3.b, v3.m from t1, v3 where t1.a = v3.m ) as t
8621where a > 2;
8622a
86234
86243
8625explain format=json select a
8626from  ( select t1.a, v3.b, v3.m from t1, v3 where t1.a = v3.m ) as t
8627where a > 2;
8628EXPLAIN
8629{
8630  "query_block": {
8631    "select_id": 1,
8632    "table": {
8633      "table_name": "t1",
8634      "access_type": "ALL",
8635      "rows": 7,
8636      "filtered": 100,
8637      "attached_condition": "t1.a > 2 and t1.a is not null"
8638    },
8639    "table": {
8640      "table_name": "<derived3>",
8641      "access_type": "ref",
8642      "possible_keys": ["key0"],
8643      "key": "key0",
8644      "key_length": "5",
8645      "used_key_parts": ["m"],
8646      "ref": ["test.t1.a"],
8647      "rows": 2,
8648      "filtered": 100,
8649      "materialized": {
8650        "query_block": {
8651          "select_id": 3,
8652          "having_condition": "m > 2",
8653          "filesort": {
8654            "sort_key": "t2.b",
8655            "temporary_table": {
8656              "table": {
8657                "table_name": "t2",
8658                "access_type": "ALL",
8659                "rows": 9,
8660                "filtered": 100
8661              }
8662            }
8663          }
8664        }
8665      }
8666    }
8667  }
8668}
8669drop view v1,v2,v3;
8670drop table t1,t2;
8671#
8672# MDEV-13166: pushdown from merged derived
8673#
8674CREATE TABLE t1 (i int) ENGINE=MyISAM;
8675INSERT INTO t1 VALUES (1),(2);
8676CREATE VIEW v1 AS SELECT MAX(i) AS f FROM t1;
8677SELECT * FROM ( SELECT * FROM v1 ) AS sq WHERE f > 0;
8678f
86792
8680explain format=json SELECT * FROM ( SELECT * FROM v1 ) AS sq WHERE f > 0;
8681EXPLAIN
8682{
8683  "query_block": {
8684    "select_id": 1,
8685    "table": {
8686      "table_name": "<derived3>",
8687      "access_type": "ALL",
8688      "rows": 2,
8689      "filtered": 100,
8690      "attached_condition": "v1.f > 0",
8691      "materialized": {
8692        "query_block": {
8693          "select_id": 3,
8694          "having_condition": "f > 0",
8695          "table": {
8696            "table_name": "t1",
8697            "access_type": "ALL",
8698            "rows": 2,
8699            "filtered": 100
8700          }
8701        }
8702      }
8703    }
8704  }
8705}
8706DROP VIEW v1;
8707DROP TABLE t1;
8708#
8709# MDEV-13193: pushdown of equality extracted from multiple equality
8710#
8711CREATE TABLE t1 (i1 int, KEY(i1)) ENGINE=MyISAM;
8712INSERT INTO t1 VALUES (1),(2);
8713CREATE TABLE t2 (i2 int) ENGINE=MyISAM;
8714INSERT INTO t2 VALUES (2),(4);
8715CREATE ALGORITHM=TEMPTABLE VIEW v2 AS SELECT * FROM t2;
8716SELECT * FROM t1, ( SELECT * FROM v2 ) AS sq
8717WHERE i1 = 1 AND ( i1 = i2 OR i1 = 2 );
8718i1	i2
8719explain format=json SELECT * FROM t1, ( SELECT * FROM v2 ) AS sq
8720WHERE i1 = 1 AND ( i1 = i2 OR i1 = 2 );
8721EXPLAIN
8722{
8723  "query_block": {
8724    "select_id": 1,
8725    "table": {
8726      "table_name": "t1",
8727      "access_type": "ref",
8728      "possible_keys": ["i1"],
8729      "key": "i1",
8730      "key_length": "5",
8731      "used_key_parts": ["i1"],
8732      "ref": ["const"],
8733      "rows": 1,
8734      "filtered": 100,
8735      "using_index": true
8736    },
8737    "block-nl-join": {
8738      "table": {
8739        "table_name": "<derived3>",
8740        "access_type": "ALL",
8741        "rows": 2,
8742        "filtered": 100,
8743        "attached_condition": "v2.i2 = 1"
8744      },
8745      "buffer_type": "flat",
8746      "buffer_size": "65",
8747      "join_type": "BNL",
8748      "materialized": {
8749        "query_block": {
8750          "select_id": 3,
8751          "table": {
8752            "table_name": "t2",
8753            "access_type": "ALL",
8754            "rows": 2,
8755            "filtered": 100,
8756            "attached_condition": "t2.i2 = 1"
8757          }
8758        }
8759      }
8760    }
8761  }
8762}
8763DROP VIEW v2;
8764DROP TABLE t1,t2;
8765#
8766# MDEV-14237: derived with regexp_substr() in select list
8767#
8768create table t1 (a char(8));
8769insert into t1 values ('b'), ('a'), ('xx');
8770select *
8771from ( select distinct regexp_substr(t1.a,'^[A-Za-z]+') as f from t1) as t
8772where t.f = 'a' or t.f = 'b';
8773f
8774b
8775a
8776explain format=json select *
8777from ( select distinct regexp_substr(t1.a,'^[A-Za-z]+') as f from t1) as t
8778where t.f = 'a' or t.f = 'b';
8779EXPLAIN
8780{
8781  "query_block": {
8782    "select_id": 1,
8783    "table": {
8784      "table_name": "<derived2>",
8785      "access_type": "ALL",
8786      "rows": 3,
8787      "filtered": 100,
8788      "attached_condition": "t.f = 'a' or t.f = 'b'",
8789      "materialized": {
8790        "query_block": {
8791          "select_id": 2,
8792          "temporary_table": {
8793            "table": {
8794              "table_name": "t1",
8795              "access_type": "ALL",
8796              "rows": 3,
8797              "filtered": 100
8798            }
8799          }
8800        }
8801      }
8802    }
8803  }
8804}
8805drop table t1;
8806#
8807# MDEV-13454: consequence of mdev-14368 fixed for 5.5
8808#
8809SET  sql_mode = 'ONLY_FULL_GROUP_BY';
8810create table t1 (id int, id2 int);
8811insert into t1 values (1,1),(2,3),(3,4),(7,2);
8812create table t2(id2 int);
8813insert  t2 values (1),(2),(3);
8814SELECT * FROM t1
8815LEFT OUTER JOIN
8816(SELECT id2, COUNT(*) as ct FROM t2 GROUP BY id2) vc USING (id2)
8817WHERE (vc.ct>0);
8818id2	id	ct
88191	1	1
88203	2	1
88212	7	1
8822EXPLAIN FORMAT=JSON SELECT * FROM t1
8823LEFT OUTER JOIN
8824(SELECT id2, COUNT(*) as ct FROM t2 GROUP BY id2) vc USING (id2)
8825WHERE (vc.ct>0);
8826EXPLAIN
8827{
8828  "query_block": {
8829    "select_id": 1,
8830    "table": {
8831      "table_name": "t1",
8832      "access_type": "ALL",
8833      "rows": 4,
8834      "filtered": 100,
8835      "attached_condition": "t1.id2 is not null"
8836    },
8837    "table": {
8838      "table_name": "<derived2>",
8839      "access_type": "ref",
8840      "possible_keys": ["key0"],
8841      "key": "key0",
8842      "key_length": "5",
8843      "used_key_parts": ["id2"],
8844      "ref": ["test.t1.id2"],
8845      "rows": 2,
8846      "filtered": 100,
8847      "attached_condition": "vc.ct > 0",
8848      "materialized": {
8849        "query_block": {
8850          "select_id": 2,
8851          "having_condition": "ct > 0",
8852          "filesort": {
8853            "sort_key": "t2.id2",
8854            "temporary_table": {
8855              "table": {
8856                "table_name": "t2",
8857                "access_type": "ALL",
8858                "rows": 3,
8859                "filtered": 100
8860              }
8861            }
8862          }
8863        }
8864      }
8865    }
8866  }
8867}
8868DROP TABLE t1,t2;
8869SET  sql_mode = DEFAULT;
8870#
8871# MDEV-15579: incorrect removal of sub-formulas to be pushed
8872#             into WHERE of materialized derived with GROUP BY
8873#
8874CREATE TABLE t1 (a INT, b INT, c INT, d INT);
8875CREATE TABLE t2 (x INT, y INT, z INT);
8876INSERT INTO t1 VALUES (1,1,66,1), (1,1,56,2), (3,2,42,3);
8877INSERT INTO t2 VALUES (1,1,66), (1,12,32);
8878SELECT *
8879FROM t2,
8880(
8881SELECT a, b, max(c) AS max_c
8882FROM t1
8883GROUP BY a
8884HAVING max_c > 37
8885) AS v1
8886WHERE (v1.a=1) AND (v1.b=v1.a) AND
8887(v1.a=t2.x) AND (v1.max_c>30);
8888x	y	z	a	b	max_c
88891	1	66	1	1	66
88901	12	32	1	1	66
8891EXPLAIN SELECT *
8892FROM t2,
8893(
8894SELECT a, b, max(c) AS max_c
8895FROM t1
8896GROUP BY a
8897HAVING max_c > 37
8898) AS v1
8899WHERE (v1.a=1) AND (v1.b=v1.a) AND
8900(v1.a=t2.x) AND (v1.max_c>30);
8901id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
89021	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where
89031	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	3	Using where; Using join buffer (flat, BNL join)
89042	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	3	Using where
8905EXPLAIN FORMAT=JSON SELECT *
8906FROM t2,
8907(
8908SELECT a, b, max(c) AS max_c
8909FROM t1
8910GROUP BY a
8911HAVING max_c > 37
8912) AS v1
8913WHERE (v1.a=1) AND (v1.b=v1.a) AND
8914(v1.a=t2.x) AND (v1.max_c>30);
8915EXPLAIN
8916{
8917  "query_block": {
8918    "select_id": 1,
8919    "table": {
8920      "table_name": "t2",
8921      "access_type": "ALL",
8922      "rows": 2,
8923      "filtered": 100,
8924      "attached_condition": "t2.x = 1"
8925    },
8926    "block-nl-join": {
8927      "table": {
8928        "table_name": "<derived2>",
8929        "access_type": "ALL",
8930        "rows": 3,
8931        "filtered": 100,
8932        "attached_condition": "v1.a = 1 and v1.b = 1 and v1.max_c > 30"
8933      },
8934      "buffer_type": "flat",
8935      "buffer_size": "173",
8936      "join_type": "BNL",
8937      "materialized": {
8938        "query_block": {
8939          "select_id": 2,
8940          "having_condition": "max_c > 37 and max_c > 30",
8941          "table": {
8942            "table_name": "t1",
8943            "access_type": "ALL",
8944            "rows": 3,
8945            "filtered": 100,
8946            "attached_condition": "t1.a = 1 and t1.b = 1"
8947          }
8948        }
8949      }
8950    }
8951  }
8952}
8953SELECT *
8954FROM t2,
8955(
8956SELECT a, b, d, max(c) AS max_c
8957FROM t1
8958GROUP BY a,d
8959HAVING max_c > 37
8960) AS v1
8961WHERE (v1.a=1) AND (v1.b=v1.a) AND (v1.b=v1.d) AND
8962(v1.a=t2.x) AND (v1.max_c>30);
8963x	y	z	a	b	d	max_c
89641	1	66	1	1	1	66
89651	12	32	1	1	1	66
8966EXPLAIN SELECT *
8967FROM t2,
8968(
8969SELECT a, b, d, max(c) AS max_c
8970FROM t1
8971GROUP BY a,d
8972HAVING max_c > 37
8973) AS v1
8974WHERE (v1.a=1) AND (v1.b=v1.a) AND (v1.b=v1.d) AND
8975(v1.a=t2.x) AND (v1.max_c>30);
8976id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
89771	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where
89781	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	3	Using where; Using join buffer (flat, BNL join)
89792	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	3	Using where
8980EXPLAIN FORMAT=JSON SELECT *
8981FROM t2,
8982(
8983SELECT a, b, d, max(c) AS max_c
8984FROM t1
8985GROUP BY a,d
8986HAVING max_c > 37
8987) AS v1
8988WHERE (v1.a=1) AND (v1.b=v1.a) AND (v1.b=v1.d) AND
8989(v1.a=t2.x) AND (v1.max_c>30);
8990EXPLAIN
8991{
8992  "query_block": {
8993    "select_id": 1,
8994    "table": {
8995      "table_name": "t2",
8996      "access_type": "ALL",
8997      "rows": 2,
8998      "filtered": 100,
8999      "attached_condition": "t2.x = 1"
9000    },
9001    "block-nl-join": {
9002      "table": {
9003        "table_name": "<derived2>",
9004        "access_type": "ALL",
9005        "rows": 3,
9006        "filtered": 100,
9007        "attached_condition": "v1.a = 1 and v1.b = 1 and v1.d = 1 and v1.max_c > 30"
9008      },
9009      "buffer_type": "flat",
9010      "buffer_size": "173",
9011      "join_type": "BNL",
9012      "materialized": {
9013        "query_block": {
9014          "select_id": 2,
9015          "having_condition": "max_c > 37 and max_c > 30",
9016          "table": {
9017            "table_name": "t1",
9018            "access_type": "ALL",
9019            "rows": 3,
9020            "filtered": 100,
9021            "attached_condition": "t1.a = 1 and t1.b = 1 and t1.d = 1"
9022          }
9023        }
9024      }
9025    }
9026  }
9027}
9028DROP TABLE t1,t2;
9029#
9030# MDEV-15765: pushing condition with temporal constants
9031#             into constant tables
9032#
9033select * from (select  date('2018-01-01') as d
9034union all
9035select  date('2018-01-01') as d) as t
9036where t.d between date ('2017-01-01') and date ('2019-01-01');
9037d
90382018-01-01
90392018-01-01
9040select * from (select  date('2018-01-01') as d) as t
9041where t.d between date ('2017-01-01') and date ('2019-01-01');
9042d
90432018-01-01
9044#
9045# MDEV-16088: pushdown into derived defined in the IN subquery
9046#
9047CREATE TABLE t1 (a INT, b INT);
9048CREATE TABLE t2 (e INT, f INT, g INT);
9049INSERT INTO t1 VALUES (1,14),(2,13),(1,19),(2,32),(3,24);
9050INSERT INTO t2 VALUES (1,19,2),(3,24,1),(1,12,2),(3,11,3),(2,32,1);
9051SELECT * FROM t1
9052WHERE (t1.a,t1.b) IN
9053(
9054SELECT d_tab.e,d_tab.max_f
9055FROM (
9056SELECT t2.e, MAX(t2.f) AS max_f
9057FROM t2
9058GROUP BY t2.e
9059HAVING max_f>18
9060) as d_tab
9061WHERE d_tab.e>1
9062)
9063;
9064a	b
90652	32
90663	24
9067EXPLAIN SELECT * FROM t1
9068WHERE (t1.a,t1.b) IN
9069(
9070SELECT d_tab.e,d_tab.max_f
9071FROM (
9072SELECT t2.e, MAX(t2.f) AS max_f
9073FROM t2
9074GROUP BY t2.e
9075HAVING max_f>18
9076) as d_tab
9077WHERE d_tab.e>1
9078)
9079;
9080id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
90811	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	5	Using where
90821	PRIMARY	<derived3>	ref	key0	key0	10	test.t1.a,test.t1.b	2	FirstMatch(t1)
90833	DERIVED	t2	ALL	NULL	NULL	NULL	NULL	5	Using where; Using temporary; Using filesort
9084EXPLAIN FORMAT=JSON SELECT * FROM t1
9085WHERE (t1.a,t1.b) IN
9086(
9087SELECT d_tab.e,d_tab.max_f
9088FROM (
9089SELECT t2.e, MAX(t2.f) AS max_f
9090FROM t2
9091GROUP BY t2.e
9092HAVING max_f>18
9093) as d_tab
9094WHERE d_tab.e>1
9095)
9096;
9097EXPLAIN
9098{
9099  "query_block": {
9100    "select_id": 1,
9101    "table": {
9102      "table_name": "t1",
9103      "access_type": "ALL",
9104      "rows": 5,
9105      "filtered": 100,
9106      "attached_condition": "t1.a > 1 and t1.a is not null and t1.b is not null"
9107    },
9108    "table": {
9109      "table_name": "<derived3>",
9110      "access_type": "ref",
9111      "possible_keys": ["key0"],
9112      "key": "key0",
9113      "key_length": "10",
9114      "used_key_parts": ["e", "max_f"],
9115      "ref": ["test.t1.a", "test.t1.b"],
9116      "rows": 2,
9117      "filtered": 100,
9118      "first_match": "t1",
9119      "materialized": {
9120        "query_block": {
9121          "select_id": 3,
9122          "having_condition": "max_f > 18",
9123          "filesort": {
9124            "sort_key": "t2.e",
9125            "temporary_table": {
9126              "table": {
9127                "table_name": "t2",
9128                "access_type": "ALL",
9129                "rows": 5,
9130                "filtered": 100,
9131                "attached_condition": "t2.e > 1"
9132              }
9133            }
9134          }
9135        }
9136      }
9137    }
9138  }
9139}
9140SELECT * FROM t1
9141WHERE (t1.a,t1.b) IN
9142(
9143SELECT d_tab.e,d_tab.max_f
9144FROM (
9145SELECT t2.e, MAX(t2.f) AS max_f
9146FROM t2
9147GROUP BY t2.e
9148HAVING max_f>18
9149) as d_tab
9150WHERE d_tab.max_f<25
9151)
9152;
9153a	b
91541	19
91553	24
9156EXPLAIN SELECT * FROM t1
9157WHERE (t1.a,t1.b) IN
9158(
9159SELECT d_tab.e,d_tab.max_f
9160FROM (
9161SELECT t2.e, MAX(t2.f) AS max_f
9162FROM t2
9163GROUP BY t2.e
9164HAVING max_f>18
9165) as d_tab
9166WHERE d_tab.max_f<25
9167)
9168;
9169id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
91701	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	5	Using where
91711	PRIMARY	<derived3>	ref	key0	key0	10	test.t1.a,test.t1.b	2	FirstMatch(t1)
91723	DERIVED	t2	ALL	NULL	NULL	NULL	NULL	5	Using temporary; Using filesort
9173EXPLAIN FORMAT=JSON SELECT * FROM t1
9174WHERE (t1.a,t1.b) IN
9175(
9176SELECT d_tab.e,d_tab.max_f
9177FROM (
9178SELECT t2.e, MAX(t2.f) AS max_f
9179FROM t2
9180GROUP BY t2.e
9181HAVING max_f>18
9182) as d_tab
9183WHERE d_tab.max_f<25
9184)
9185;
9186EXPLAIN
9187{
9188  "query_block": {
9189    "select_id": 1,
9190    "table": {
9191      "table_name": "t1",
9192      "access_type": "ALL",
9193      "rows": 5,
9194      "filtered": 100,
9195      "attached_condition": "t1.b < 25 and t1.a is not null and t1.b is not null"
9196    },
9197    "table": {
9198      "table_name": "<derived3>",
9199      "access_type": "ref",
9200      "possible_keys": ["key0"],
9201      "key": "key0",
9202      "key_length": "10",
9203      "used_key_parts": ["e", "max_f"],
9204      "ref": ["test.t1.a", "test.t1.b"],
9205      "rows": 2,
9206      "filtered": 100,
9207      "first_match": "t1",
9208      "materialized": {
9209        "query_block": {
9210          "select_id": 3,
9211          "having_condition": "max_f > 18 and max_f < 25",
9212          "filesort": {
9213            "sort_key": "t2.e",
9214            "temporary_table": {
9215              "table": {
9216                "table_name": "t2",
9217                "access_type": "ALL",
9218                "rows": 5,
9219                "filtered": 100
9220              }
9221            }
9222          }
9223        }
9224      }
9225    }
9226  }
9227}
9228SELECT * FROM t1
9229WHERE (t1.a,t1.b) IN
9230(
9231SELECT d_tab.e, MAX(d_tab.max_f) AS max_f
9232FROM (
9233SELECT t2.e, MAX(t2.f) as max_f, t2.g
9234FROM t2
9235GROUP BY t2.e
9236) as d_tab
9237WHERE d_tab.e>1
9238GROUP BY d_tab.g
9239)
9240;
9241a	b
92422	32
9243EXPLAIN SELECT * FROM t1
9244WHERE (t1.a,t1.b) IN
9245(
9246SELECT d_tab.e, MAX(d_tab.max_f) AS max_f
9247FROM (
9248SELECT t2.e, MAX(t2.f) as max_f, t2.g
9249FROM t2
9250GROUP BY t2.e
9251) as d_tab
9252WHERE d_tab.e>1
9253GROUP BY d_tab.g
9254)
9255;
9256id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
92571	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	5	Using where
92581	PRIMARY	<subquery2>	eq_ref	distinct_key	distinct_key	8	test.t1.a,test.t1.b	1
92592	MATERIALIZED	<derived3>	ALL	NULL	NULL	NULL	NULL	5	Using where; Using temporary
92603	DERIVED	t2	ALL	NULL	NULL	NULL	NULL	5	Using where; Using temporary; Using filesort
9261EXPLAIN FORMAT=JSON SELECT * FROM t1
9262WHERE (t1.a,t1.b) IN
9263(
9264SELECT d_tab.e, MAX(d_tab.max_f) AS max_f
9265FROM (
9266SELECT t2.e, MAX(t2.f) as max_f, t2.g
9267FROM t2
9268GROUP BY t2.e
9269) as d_tab
9270WHERE d_tab.e>1
9271GROUP BY d_tab.g
9272)
9273;
9274EXPLAIN
9275{
9276  "query_block": {
9277    "select_id": 1,
9278    "table": {
9279      "table_name": "t1",
9280      "access_type": "ALL",
9281      "rows": 5,
9282      "filtered": 100,
9283      "attached_condition": "t1.a is not null and t1.b is not null"
9284    },
9285    "table": {
9286      "table_name": "<subquery2>",
9287      "access_type": "eq_ref",
9288      "possible_keys": ["distinct_key"],
9289      "key": "distinct_key",
9290      "key_length": "8",
9291      "used_key_parts": ["e", "max_f"],
9292      "ref": ["test.t1.a", "test.t1.b"],
9293      "rows": 1,
9294      "filtered": 100,
9295      "materialized": {
9296        "unique": 1,
9297        "query_block": {
9298          "select_id": 2,
9299          "temporary_table": {
9300            "table": {
9301              "table_name": "<derived3>",
9302              "access_type": "ALL",
9303              "rows": 5,
9304              "filtered": 100,
9305              "attached_condition": "d_tab.e > 1",
9306              "materialized": {
9307                "query_block": {
9308                  "select_id": 3,
9309                  "filesort": {
9310                    "sort_key": "t2.e",
9311                    "temporary_table": {
9312                      "table": {
9313                        "table_name": "t2",
9314                        "access_type": "ALL",
9315                        "rows": 5,
9316                        "filtered": 100,
9317                        "attached_condition": "t2.e > 1"
9318                      }
9319                    }
9320                  }
9321                }
9322              }
9323            }
9324          }
9325        }
9326      }
9327    }
9328  }
9329}
9330SELECT * FROM t1
9331WHERE (t1.a,t1.b) IN
9332(
9333SELECT d_tab.e, MAX(d_tab.max_f) AS max_f
9334FROM (
9335SELECT t2.e, MAX(t2.f) as max_f, t2.g
9336FROM t2
9337GROUP BY t2.e
9338) as d_tab
9339WHERE d_tab.max_f>20
9340GROUP BY d_tab.g
9341)
9342;
9343a	b
93442	32
9345EXPLAIN SELECT * FROM t1
9346WHERE (t1.a,t1.b) IN
9347(
9348SELECT d_tab.e, MAX(d_tab.max_f) AS max_f
9349FROM (
9350SELECT t2.e, MAX(t2.f) as max_f, t2.g
9351FROM t2
9352GROUP BY t2.e
9353) as d_tab
9354WHERE d_tab.max_f>20
9355GROUP BY d_tab.g
9356)
9357;
9358id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
93591	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	5	Using where
93601	PRIMARY	<subquery2>	eq_ref	distinct_key	distinct_key	8	test.t1.a,test.t1.b	1
93612	MATERIALIZED	<derived3>	ALL	NULL	NULL	NULL	NULL	5	Using where; Using temporary
93623	DERIVED	t2	ALL	NULL	NULL	NULL	NULL	5	Using temporary; Using filesort
9363EXPLAIN FORMAT=JSON SELECT * FROM t1
9364WHERE (t1.a,t1.b) IN
9365(
9366SELECT d_tab.e, MAX(d_tab.max_f) AS max_f
9367FROM (
9368SELECT t2.e, MAX(t2.f) as max_f, t2.g
9369FROM t2
9370GROUP BY t2.e
9371) as d_tab
9372WHERE d_tab.max_f>20
9373GROUP BY d_tab.g
9374)
9375;
9376EXPLAIN
9377{
9378  "query_block": {
9379    "select_id": 1,
9380    "table": {
9381      "table_name": "t1",
9382      "access_type": "ALL",
9383      "rows": 5,
9384      "filtered": 100,
9385      "attached_condition": "t1.a is not null and t1.b is not null"
9386    },
9387    "table": {
9388      "table_name": "<subquery2>",
9389      "access_type": "eq_ref",
9390      "possible_keys": ["distinct_key"],
9391      "key": "distinct_key",
9392      "key_length": "8",
9393      "used_key_parts": ["e", "max_f"],
9394      "ref": ["test.t1.a", "test.t1.b"],
9395      "rows": 1,
9396      "filtered": 100,
9397      "materialized": {
9398        "unique": 1,
9399        "query_block": {
9400          "select_id": 2,
9401          "temporary_table": {
9402            "table": {
9403              "table_name": "<derived3>",
9404              "access_type": "ALL",
9405              "rows": 5,
9406              "filtered": 100,
9407              "attached_condition": "d_tab.max_f > 20",
9408              "materialized": {
9409                "query_block": {
9410                  "select_id": 3,
9411                  "having_condition": "max_f > 20",
9412                  "filesort": {
9413                    "sort_key": "t2.e",
9414                    "temporary_table": {
9415                      "table": {
9416                        "table_name": "t2",
9417                        "access_type": "ALL",
9418                        "rows": 5,
9419                        "filtered": 100
9420                      }
9421                    }
9422                  }
9423                }
9424              }
9425            }
9426          }
9427        }
9428      }
9429    }
9430  }
9431}
9432DROP TABLE t1,t2;
9433#
9434# MDEV-15765: pushing condition with IN subquery defined with constants
9435#             using substitution
9436#
9437CREATE TABLE t1 (a INT);
9438INSERT INTO t1 VALUES (1),(2);
9439SELECT * FROM
9440(
9441SELECT DISTINCT * FROM t1
9442) der_tab
9443WHERE (a>0 AND a<2 OR a IN (2,3)) AND
9444(a=2 OR 0);
9445a
94462
9447DROP TABLE t1;
9448#
9449# MDEV-16386: pushing condition into the HAVING clause when ambiguous
9450#             fields warning appears
9451#
9452CREATE TABLE t1 (a INT, b INT);
9453INSERT INTO t1 VALUES (1,2),(2,3),(3,4);
9454SELECT * FROM
9455(
9456SELECT t1.b AS a
9457FROM t1
9458GROUP BY t1.a
9459) dt
9460WHERE (dt.a=2);
9461a
94622
9463EXPLAIN FORMAT=JSON SELECT * FROM
9464(
9465SELECT t1.b AS a
9466FROM t1
9467GROUP BY t1.a
9468) dt
9469WHERE (dt.a=2);
9470EXPLAIN
9471{
9472  "query_block": {
9473    "select_id": 1,
9474    "table": {
9475      "table_name": "<derived2>",
9476      "access_type": "ALL",
9477      "rows": 3,
9478      "filtered": 100,
9479      "attached_condition": "dt.a = 2",
9480      "materialized": {
9481        "query_block": {
9482          "select_id": 2,
9483          "having_condition": "a = 2",
9484          "filesort": {
9485            "sort_key": "t1.a",
9486            "temporary_table": {
9487              "table": {
9488                "table_name": "t1",
9489                "access_type": "ALL",
9490                "rows": 3,
9491                "filtered": 100
9492              }
9493            }
9494          }
9495        }
9496      }
9497    }
9498  }
9499}
9500SELECT * FROM
9501(
9502SELECT t1.b AS a
9503FROM t1
9504GROUP BY t1.a
9505HAVING (t1.a<3)
9506) dt
9507WHERE (dt.a>1);
9508a
95092
95103
9511EXPLAIN FORMAT=JSON SELECT * FROM
9512(
9513SELECT t1.b AS a
9514FROM t1
9515GROUP BY t1.a
9516HAVING (t1.a<3)
9517) dt
9518WHERE (dt.a>1);
9519EXPLAIN
9520{
9521  "query_block": {
9522    "select_id": 1,
9523    "table": {
9524      "table_name": "<derived2>",
9525      "access_type": "ALL",
9526      "rows": 3,
9527      "filtered": 100,
9528      "attached_condition": "dt.a > 1",
9529      "materialized": {
9530        "query_block": {
9531          "select_id": 2,
9532          "having_condition": "a > 1",
9533          "filesort": {
9534            "sort_key": "t1.a",
9535            "temporary_table": {
9536              "table": {
9537                "table_name": "t1",
9538                "access_type": "ALL",
9539                "rows": 3,
9540                "filtered": 100,
9541                "attached_condition": "t1.a < 3"
9542              }
9543            }
9544          }
9545        }
9546      }
9547    }
9548  }
9549}
9550SELECT * FROM
9551(
9552SELECT 'ab' AS a
9553FROM t1
9554GROUP BY t1.a
9555) dt
9556WHERE (dt.a='ab');
9557a
9558ab
9559ab
9560ab
9561EXPLAIN FORMAT=JSON SELECT * FROM
9562(
9563SELECT 'ab' AS a
9564FROM t1
9565GROUP BY t1.a
9566) dt
9567WHERE (dt.a='ab');
9568EXPLAIN
9569{
9570  "query_block": {
9571    "select_id": 1,
9572    "table": {
9573      "table_name": "<derived2>",
9574      "access_type": "ALL",
9575      "rows": 3,
9576      "filtered": 100,
9577      "attached_condition": "dt.a = 'ab'",
9578      "materialized": {
9579        "query_block": {
9580          "select_id": 2,
9581          "filesort": {
9582            "sort_key": "t1.a",
9583            "temporary_table": {
9584              "table": {
9585                "table_name": "t1",
9586                "access_type": "ALL",
9587                "rows": 3,
9588                "filtered": 100
9589              }
9590            }
9591          }
9592        }
9593      }
9594    }
9595  }
9596}
9597SELECT * FROM
9598(
9599SELECT 1 AS a
9600FROM t1
9601GROUP BY t1.a
9602) dt
9603WHERE (dt.a=1);
9604a
96051
96061
96071
9608EXPLAIN FORMAT=JSON SELECT * FROM
9609(
9610SELECT 1 AS a
9611FROM t1
9612GROUP BY t1.a
9613) dt
9614WHERE (dt.a=1);
9615EXPLAIN
9616{
9617  "query_block": {
9618    "select_id": 1,
9619    "table": {
9620      "table_name": "<derived2>",
9621      "access_type": "ALL",
9622      "rows": 3,
9623      "filtered": 100,
9624      "attached_condition": "dt.a = 1",
9625      "materialized": {
9626        "query_block": {
9627          "select_id": 2,
9628          "filesort": {
9629            "sort_key": "t1.a",
9630            "temporary_table": {
9631              "table": {
9632                "table_name": "t1",
9633                "access_type": "ALL",
9634                "rows": 3,
9635                "filtered": 100
9636              }
9637            }
9638          }
9639        }
9640      }
9641    }
9642  }
9643}
9644DROP TABLE t1;
9645#
9646# MDEV-16517: pushdown condition with the IN predicate defined
9647#             with non-constant values
9648#
9649CREATE TABLE t1 (a INT, b INT);
9650INSERT INTO t1 VALUES (1,2),(1,3);
9651SELECT * FROM
9652(
9653SELECT t1.a
9654FROM t1
9655WHERE 1 IN (0,t1.a)
9656GROUP BY t1.a
9657) AS dt1
9658JOIN
9659(
9660SELECT t1.a
9661FROM t1
9662WHERE 1 IN (0,t1.a)
9663) AS dt2
9664ON dt1.a = dt2.a;
9665a	a
96661	1
96671	1
9668EXPLAIN FORMAT=JSON SELECT * FROM
9669(
9670SELECT t1.a
9671FROM t1
9672WHERE 1 IN (0,t1.a)
9673GROUP BY t1.a
9674) AS dt1
9675JOIN
9676(
9677SELECT t1.a
9678FROM t1
9679WHERE 1 IN (0,t1.a)
9680) AS dt2
9681ON dt1.a = dt2.a;
9682EXPLAIN
9683{
9684  "query_block": {
9685    "select_id": 1,
9686    "table": {
9687      "table_name": "<derived2>",
9688      "access_type": "ALL",
9689      "rows": 2,
9690      "filtered": 100,
9691      "attached_condition": "1 in (0,dt1.a)",
9692      "materialized": {
9693        "query_block": {
9694          "select_id": 2,
9695          "filesort": {
9696            "sort_key": "t1.a",
9697            "temporary_table": {
9698              "table": {
9699                "table_name": "t1",
9700                "access_type": "ALL",
9701                "rows": 2,
9702                "filtered": 100,
9703                "attached_condition": "1 in (0,t1.a) and 1 in (0,t1.a)"
9704              }
9705            }
9706          }
9707        }
9708      }
9709    },
9710    "block-nl-join": {
9711      "table": {
9712        "table_name": "t1",
9713        "access_type": "ALL",
9714        "rows": 2,
9715        "filtered": 100
9716      },
9717      "buffer_type": "flat",
9718      "buffer_size": "65",
9719      "join_type": "BNL",
9720      "attached_condition": "t1.a = dt1.a"
9721    }
9722  }
9723}
9724SELECT * FROM
9725(
9726SELECT t1.a,MAX(t1.b)
9727FROM t1
9728GROUP BY t1.a
9729) AS dt, t1
9730WHERE dt.a=t1.a AND dt.a IN (1,t1.a);
9731a	MAX(t1.b)	a	b
97321	3	1	2
97331	3	1	3
9734EXPLAIN FORMAT=JSON SELECT * FROM
9735(
9736SELECT t1.a,MAX(t1.b)
9737FROM t1
9738GROUP BY t1.a
9739) AS dt, t1
9740WHERE dt.a=t1.a AND dt.a IN (1,t1.a);
9741EXPLAIN
9742{
9743  "query_block": {
9744    "select_id": 1,
9745    "table": {
9746      "table_name": "<derived2>",
9747      "access_type": "ALL",
9748      "rows": 2,
9749      "filtered": 100,
9750      "attached_condition": "dt.a in (1,dt.a)",
9751      "materialized": {
9752        "query_block": {
9753          "select_id": 2,
9754          "filesort": {
9755            "sort_key": "t1.a",
9756            "temporary_table": {
9757              "table": {
9758                "table_name": "t1",
9759                "access_type": "ALL",
9760                "rows": 2,
9761                "filtered": 100,
9762                "attached_condition": "t1.a in (1,t1.a)"
9763              }
9764            }
9765          }
9766        }
9767      }
9768    },
9769    "block-nl-join": {
9770      "table": {
9771        "table_name": "t1",
9772        "access_type": "ALL",
9773        "rows": 2,
9774        "filtered": 100
9775      },
9776      "buffer_type": "flat",
9777      "buffer_size": "119",
9778      "join_type": "BNL",
9779      "attached_condition": "t1.a = dt.a"
9780    }
9781  }
9782}
9783DROP TABLE t1;
9784#
9785# MDEV-15087: error from inexpensive subquery before check
9786#                   for condition pushdown into derived
9787#
9788CREATE TABLE t1 (i1 int, v1 varchar(1));
9789INSERT INTO t1 VALUES (7,'x');
9790CREATE TABLE t2 (i1 int);
9791INSERT INTO t2 VALUES (8);
9792CREATE TABLE t3 (i1 int ,v1 varchar(1), v2 varchar(1));
9793INSERT INTO t3 VALUES (4, 'v','v'),(62,'v','k'),(7, 'n', NULL);
9794SELECT 1
9795FROM (t1 AS a1
9796JOIN (((SELECT  DISTINCT t3.*
9797FROM t3) AS a2
9798JOIN t1 ON (t1.v1 = a2.v2))) ON (t1.v1 = a2.v1))
9799WHERE (SELECT  BIT_COUNT(t2.i1)
9800FROM (t2 JOIN t3)) IS NULL;
9801ERROR 21000: Subquery returns more than 1 row
9802DROP TABLE t1, t2, t3;
9803#
9804# MDEV-16614 signal 7 after calling stored procedure, that uses regexp
9805#
9806CREATE PROCEDURE p1(m1 varchar(5), m2 varchar(5))
9807BEGIN
9808SELECT a FROM
9809(SELECT "aa" a) t
9810JOIN (SELECT "aa" b) t1 on t.a=t1.b
9811WHERE t.a regexp m1 and t1.b regexp m2
9812GROUP BY a;
9813END$$
9814CALL p1('a','a');
9815a
9816aa
9817DROP PROCEDURE p1;
9818CREATE PROCEDURE p1(m1 varchar(5))
9819BEGIN
9820SELECT a FROM (SELECT "aa" a) t WHERE t.a regexp m1;
9821END$$
9822CALL p1('a');
9823a
9824aa
9825DROP PROCEDURE p1;
9826SELECT a FROM (SELECT "aa" a) t WHERE REGEXP_INSTR(t.a, (SELECT MAX('aa') FROM DUAL LIMIT 1));
9827a
9828aa
9829CREATE OR REPLACE FUNCTION f1(a VARCHAR(10), b VARCHAR(10)) RETURNS INT
9830BEGIN
9831RETURN 1;
9832END;$$
9833CREATE OR REPLACE PROCEDURE p1(m1 varchar(5))
9834BEGIN
9835SELECT a FROM (SELECT "aa" a) t WHERE f1(t.a, m1);
9836END$$
9837CALL p1('a');
9838a
9839aa
9840DROP PROCEDURE p1;
9841DROP FUNCTION f1;
9842CREATE OR REPLACE FUNCTION f1(a VARCHAR(10), b VARCHAR(10)) RETURNS INT
9843BEGIN
9844RETURN 1;
9845END;$$
9846SELECT a FROM (SELECT "aa" a) t WHERE f1(t.a, (SELECT MAX('aa') FROM DUAL LIMIT 1));
9847a
9848aa
9849DROP FUNCTION f1;
9850#
9851# MDEV-17011: condition pushdown into materialized derived used
9852#             in INSERT SELECT, multi-table UPDATE and DELETE
9853#
9854CREATE TABLE t1 (a int ,b int) ENGINE=MyISAM;
9855INSERT INTO t1 VALUES
9856(1, 1), (1, 2), (2, 1), (2, 2), (3,1), (3,3), (4,2);
9857CREATE TABLE t2 (a int) ENGINE MYISAM;
9858INSERT INTO t2 VALUES
9859(3), (7), (1), (4), (1);
9860CREATE TABLE t3 (a int, b int) ENGINE MYISAM;
9861EXPLAIN FORMAT=JSON INSERT INTO t3
9862SELECT * FROM (SELECT a, count(*) as c FROM t1 GROUP BY a) t WHERE a<=2;
9863EXPLAIN
9864{
9865  "query_block": {
9866    "select_id": 1,
9867    "table": {
9868      "table_name": "<derived2>",
9869      "access_type": "ALL",
9870      "rows": 7,
9871      "filtered": 100,
9872      "attached_condition": "t.a <= 2",
9873      "materialized": {
9874        "query_block": {
9875          "select_id": 2,
9876          "filesort": {
9877            "sort_key": "t1.a",
9878            "temporary_table": {
9879              "table": {
9880                "table_name": "t1",
9881                "access_type": "ALL",
9882                "rows": 7,
9883                "filtered": 100,
9884                "attached_condition": "t1.a <= 2"
9885              }
9886            }
9887          }
9888        }
9889      }
9890    }
9891  }
9892}
9893INSERT INTO t3
9894SELECT * FROM (SELECT a, count(*) as c FROM t1 GROUP BY a) t WHERE a<=2;
9895SELECT * FROM t3;
9896a	b
98971	2
98982	2
9899EXPLAIN FORMAT=JSON UPDATE t2, (SELECT a, count(*) as c FROM t1 GROUP BY a) t SET t2.a=t.c+10
9900WHERE t2.a= t.c and t.a>=3;
9901EXPLAIN
9902{
9903  "query_block": {
9904    "select_id": 1,
9905    "table": {
9906      "table_name": "t2",
9907      "access_type": "ALL",
9908      "rows": 5,
9909      "filtered": 100,
9910      "attached_condition": "t2.a is not null"
9911    },
9912    "table": {
9913      "table_name": "<derived2>",
9914      "access_type": "ref",
9915      "possible_keys": ["key0"],
9916      "key": "key0",
9917      "key_length": "8",
9918      "used_key_parts": ["c"],
9919      "ref": ["test.t2.a"],
9920      "rows": 2,
9921      "filtered": 100,
9922      "attached_condition": "t2.a = t.c and t.a >= 3",
9923      "materialized": {
9924        "query_block": {
9925          "select_id": 2,
9926          "filesort": {
9927            "sort_key": "t1.a",
9928            "temporary_table": {
9929              "table": {
9930                "table_name": "t1",
9931                "access_type": "ALL",
9932                "rows": 7,
9933                "filtered": 100,
9934                "attached_condition": "t1.a >= 3"
9935              }
9936            }
9937          }
9938        }
9939      }
9940    }
9941  }
9942}
9943UPDATE t2, (SELECT a, count(*) as c FROM t1 GROUP BY a) t SET t2.a=t.c+10
9944WHERE t2.a= t.c and t.a>=3;
9945SELECT * FROM t2;
9946a
99473
99487
994911
99504
995111
9952EXPLAIN FORMAT=JSON DELETE t2 FROM t2, (SELECT a, count(*) as c FROM t1 GROUP BY a) t
9953WHERE t2.a= t.c+9 and t.a=2;
9954EXPLAIN
9955{
9956  "query_block": {
9957    "select_id": 1,
9958    "table": {
9959      "table_name": "t2",
9960      "access_type": "ALL",
9961      "rows": 5,
9962      "filtered": 100
9963    },
9964    "table": {
9965      "table_name": "<derived2>",
9966      "access_type": "ALL",
9967      "rows": 7,
9968      "filtered": 100,
9969      "attached_condition": "t.a = 2 and t2.a = t.c + 9",
9970      "materialized": {
9971        "query_block": {
9972          "select_id": 2,
9973          "table": {
9974            "table_name": "t1",
9975            "access_type": "ALL",
9976            "rows": 7,
9977            "filtered": 100,
9978            "attached_condition": "t1.a = 2"
9979          }
9980        }
9981      }
9982    }
9983  }
9984}
9985DELETE t2 FROM t2, (SELECT a, count(*) as c FROM t1 GROUP BY a) t
9986WHERE t2.a= t.c+9 and t.a=2;
9987SELECT * FROM t2;
9988a
99893
99907
99914
9992DROP TABLE t1,t2,t3;
9993#
9994# MDEV-16765: pushdown condition with the CASE structure
9995#             defined with Item_cond item
9996#
9997CREATE TABLE t1(a INT, b INT);
9998INSERT INTO t1 VALUES (1,2), (3,4), (2,3);
9999SELECT *
10000FROM
10001(
10002SELECT CASE WHEN ((tab2.max_a=1) OR (tab2.max_a=2))
10003THEN 1 ELSE 0 END AS max_a,b
10004FROM (SELECT MAX(a) as max_a,b FROM t1 GROUP BY t1.b) AS tab2
10005) AS tab1
10006WHERE (tab1.max_a=1);
10007max_a	b
100081	2
100091	3
10010EXPLAIN FORMAT=JSON SELECT *
10011FROM
10012(
10013SELECT CASE WHEN ((tab2.max_a=1) OR (tab2.max_a=2))
10014THEN 1 ELSE 0 END AS max_a,b
10015FROM (SELECT MAX(a) as max_a,b FROM t1 GROUP BY t1.b) AS tab2
10016) AS tab1
10017WHERE (tab1.max_a=1);
10018EXPLAIN
10019{
10020  "query_block": {
10021    "select_id": 1,
10022    "table": {
10023      "table_name": "<derived3>",
10024      "access_type": "ALL",
10025      "rows": 3,
10026      "filtered": 100,
10027      "attached_condition": "case when tab2.max_a = 1 or tab2.max_a = 2 then 1 else 0 end = 1",
10028      "materialized": {
10029        "query_block": {
10030          "select_id": 3,
10031          "having_condition": "case when max_a = 1 or max_a = 2 then 1 else 0 end = 1",
10032          "filesort": {
10033            "sort_key": "t1.b",
10034            "temporary_table": {
10035              "table": {
10036                "table_name": "t1",
10037                "access_type": "ALL",
10038                "rows": 3,
10039                "filtered": 100
10040              }
10041            }
10042          }
10043        }
10044      }
10045    }
10046  }
10047}
10048SELECT *
10049FROM
10050(
10051SELECT CASE WHEN ((tab2.max_a=1) OR ((tab2.max_a>2) AND (tab2.max_a<4)))
10052THEN 1 ELSE 0 END AS max_a,b
10053FROM (SELECT MAX(a) as max_a,b FROM t1 GROUP BY t1.b) AS tab2
10054) AS tab1
10055WHERE (tab1.max_a=1);
10056max_a	b
100571	2
100581	4
10059EXPLAIN FORMAT=JSON SELECT *
10060FROM
10061(
10062SELECT CASE WHEN ((tab2.max_a=1) OR ((tab2.max_a>2) AND (tab2.max_a<4)))
10063THEN 1 ELSE 0 END AS max_a,b
10064FROM (SELECT MAX(a) as max_a,b FROM t1 GROUP BY t1.b) AS tab2
10065) AS tab1
10066WHERE (tab1.max_a=1);
10067EXPLAIN
10068{
10069  "query_block": {
10070    "select_id": 1,
10071    "table": {
10072      "table_name": "<derived3>",
10073      "access_type": "ALL",
10074      "rows": 3,
10075      "filtered": 100,
10076      "attached_condition": "case when tab2.max_a = 1 or tab2.max_a > 2 and tab2.max_a < 4 then 1 else 0 end = 1",
10077      "materialized": {
10078        "query_block": {
10079          "select_id": 3,
10080          "having_condition": "case when max_a = 1 or max_a > 2 and max_a < 4 then 1 else 0 end = 1",
10081          "filesort": {
10082            "sort_key": "t1.b",
10083            "temporary_table": {
10084              "table": {
10085                "table_name": "t1",
10086                "access_type": "ALL",
10087                "rows": 3,
10088                "filtered": 100
10089              }
10090            }
10091          }
10092        }
10093      }
10094    }
10095  }
10096}
10097SELECT *
10098FROM
10099(
10100SELECT CASE WHEN ((tab2.max_a>1) AND ((tab2.max_a=2) OR (tab2.max_a>2)))
10101THEN 1 ELSE 0 END AS max_a,b
10102FROM (SELECT MAX(a) as max_a,b FROM t1 GROUP BY t1.b) AS tab2
10103) AS tab1
10104WHERE (tab1.max_a=1);
10105max_a	b
101061	3
101071	4
10108EXPLAIN FORMAT=JSON SELECT *
10109FROM
10110(
10111SELECT CASE WHEN ((tab2.max_a>1) AND ((tab2.max_a=2) OR (tab2.max_a>2)))
10112THEN 1 ELSE 0 END AS max_a,b
10113FROM (SELECT MAX(a) as max_a,b FROM t1 GROUP BY t1.b) AS tab2
10114) AS tab1
10115WHERE (tab1.max_a=1);
10116EXPLAIN
10117{
10118  "query_block": {
10119    "select_id": 1,
10120    "table": {
10121      "table_name": "<derived3>",
10122      "access_type": "ALL",
10123      "rows": 3,
10124      "filtered": 100,
10125      "attached_condition": "case when tab2.max_a > 1 and (tab2.max_a = 2 or tab2.max_a > 2) then 1 else 0 end = 1",
10126      "materialized": {
10127        "query_block": {
10128          "select_id": 3,
10129          "having_condition": "case when max_a > 1 and (max_a = 2 or max_a > 2) then 1 else 0 end = 1",
10130          "filesort": {
10131            "sort_key": "t1.b",
10132            "temporary_table": {
10133              "table": {
10134                "table_name": "t1",
10135                "access_type": "ALL",
10136                "rows": 3,
10137                "filtered": 100
10138              }
10139            }
10140          }
10141        }
10142      }
10143    }
10144  }
10145}
10146SELECT *
10147FROM
10148(
10149SELECT CASE WHEN ((tab2.b=2) OR (tab2.b=4))
10150THEN 1 ELSE 0 END AS max_a,b
10151FROM (SELECT MAX(a) as max_a,b FROM t1 GROUP BY t1.b) AS tab2
10152) AS tab1
10153WHERE (tab1.max_a=1);
10154max_a	b
101551	2
101561	4
10157EXPLAIN FORMAT=JSON SELECT *
10158FROM
10159(
10160SELECT CASE WHEN ((tab2.b=2) OR (tab2.b=4))
10161THEN 1 ELSE 0 END AS max_a,b
10162FROM (SELECT MAX(a) as max_a,b FROM t1 GROUP BY t1.b) AS tab2
10163) AS tab1
10164WHERE (tab1.max_a=1);
10165EXPLAIN
10166{
10167  "query_block": {
10168    "select_id": 1,
10169    "table": {
10170      "table_name": "<derived3>",
10171      "access_type": "ALL",
10172      "rows": 3,
10173      "filtered": 100,
10174      "attached_condition": "case when tab2.b = 2 or tab2.b = 4 then 1 else 0 end = 1",
10175      "materialized": {
10176        "query_block": {
10177          "select_id": 3,
10178          "filesort": {
10179            "sort_key": "t1.b",
10180            "temporary_table": {
10181              "table": {
10182                "table_name": "t1",
10183                "access_type": "ALL",
10184                "rows": 3,
10185                "filtered": 100,
10186                "attached_condition": "case when t1.b = 2 or t1.b = 4 then 1 else 0 end = 1"
10187              }
10188            }
10189          }
10190        }
10191      }
10192    }
10193  }
10194}
10195DROP TABLE t1;
10196#
10197# MDEV-16803: pushdown condition with IN predicate in the derived table
10198#             defined with several SELECT statements
10199#
10200CREATE TABLE t1 (a INT, b INT);
10201INSERT INTO t1 VALUES (1,2),(3,2),(1,1);
10202SELECT * FROM
10203(
10204SELECT a,b,1 as c
10205FROM t1
10206UNION ALL
10207SELECT a,b,2 as c
10208FROM t1
10209) AS tab
10210WHERE ((a,b) IN ((1,2),(3,2)));
10211a	b	c
102121	2	1
102133	2	1
102141	2	2
102153	2	2
10216DROP TABLE t1;
10217#
10218# MDEV-17354: INSERT SELECT with condition pushdown into derived
10219#
10220CREATE TABLE t1 (f INT NOT NULL);
10221INSERT INTO t1 VALUES (3), (7), (3);
10222CREATE ALGORITHM= TEMPTABLE VIEW v1 AS SELECT * FROM ( SELECT * FROM t1 ) AS sq;
10223INSERT INTO t1
10224SELECT * FROM ( SELECT t1.f FROM v1 JOIN t1 ) AS t WHERE f IS NOT NULL;
10225EXPLAIN INSERT INTO t1
10226SELECT * FROM ( SELECT t1.f FROM v1 JOIN t1 ) AS t WHERE f IS NOT NULL;
10227id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
102281	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	144	Using where
102292	DERIVED	<derived4>	ALL	NULL	NULL	NULL	NULL	12
102302	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	12	Using where; Using join buffer (flat, BNL join)
102314	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	12
10232EXPLAIN FORMAT=JSON INSERT INTO t1
10233SELECT * FROM ( SELECT t1.f FROM v1 JOIN t1 ) AS t WHERE f IS NOT NULL;
10234EXPLAIN
10235{
10236  "query_block": {
10237    "select_id": 1,
10238    "table": {
10239      "table_name": "<derived2>",
10240      "access_type": "ALL",
10241      "rows": 144,
10242      "filtered": 100,
10243      "attached_condition": "t.f is not null",
10244      "materialized": {
10245        "query_block": {
10246          "select_id": 2,
10247          "table": {
10248            "table_name": "<derived4>",
10249            "access_type": "ALL",
10250            "rows": 12,
10251            "filtered": 100,
10252            "materialized": {
10253              "query_block": {
10254                "select_id": 4,
10255                "table": {
10256                  "table_name": "t1",
10257                  "access_type": "ALL",
10258                  "rows": 12,
10259                  "filtered": 100
10260                }
10261              }
10262            }
10263          },
10264          "block-nl-join": {
10265            "table": {
10266              "table_name": "t1",
10267              "access_type": "ALL",
10268              "rows": 12,
10269              "filtered": 100,
10270              "attached_condition": "t1.f is not null"
10271            },
10272            "buffer_type": "flat",
10273            "buffer_size": "64",
10274            "join_type": "BNL"
10275          }
10276        }
10277      }
10278    }
10279  }
10280}
10281SELECT * FROM t1;
10282f
102833
102847
102853
102863
102873
102883
102897
102907
102917
102923
102933
102943
10295DELETE FROM t1;
10296INSERT INTO t1 VALUES (3), (7), (3);
10297INSERT INTO t1
10298SELECT * FROM ( SELECT t1.f FROM v1 JOIN t1 ON v1.f=t1.f) AS t
10299WHERE f IS NOT NULL;
10300EXPLAIN FORMAT=JSON INSERT INTO t1
10301SELECT * FROM ( SELECT t1.f FROM v1 JOIN t1 ON v1.f=t1.f) AS t
10302WHERE f IS NOT NULL;
10303EXPLAIN
10304{
10305  "query_block": {
10306    "select_id": 1,
10307    "table": {
10308      "table_name": "<derived2>",
10309      "access_type": "ALL",
10310      "rows": 16,
10311      "filtered": 100,
10312      "attached_condition": "t.f is not null",
10313      "materialized": {
10314        "query_block": {
10315          "select_id": 2,
10316          "table": {
10317            "table_name": "t1",
10318            "access_type": "ALL",
10319            "rows": 8,
10320            "filtered": 100,
10321            "attached_condition": "t1.f is not null"
10322          },
10323          "table": {
10324            "table_name": "<derived4>",
10325            "access_type": "ref",
10326            "possible_keys": ["key0"],
10327            "key": "key0",
10328            "key_length": "4",
10329            "used_key_parts": ["f"],
10330            "ref": ["test.t1.f"],
10331            "rows": 2,
10332            "filtered": 100,
10333            "materialized": {
10334              "query_block": {
10335                "select_id": 4,
10336                "table": {
10337                  "table_name": "t1",
10338                  "access_type": "ALL",
10339                  "rows": 8,
10340                  "filtered": 100,
10341                  "attached_condition": "t1.f is not null"
10342                }
10343              }
10344            }
10345          }
10346        }
10347      }
10348    }
10349  }
10350}
10351SELECT * FROM t1;
10352f
103533
103547
103553
103563
103573
103587
103593
103603
10361DROP VIEW v1;
10362DROP TABLE t1;
10363#
10364# MDEV-17574: pushdown into derived from mergeable view
10365#             used in multi-table UPDATE
10366#             pushdown into materialized derived from mergeable view
10367#             used in SELECT
10368#
10369CREATE TABLE t1 (f1 text, f2 int);
10370INSERT INTO t1 VALUES ('x',1), ('y',2);
10371CREATE VIEW v1 AS SELECT f2 FROM ( SELECT f2 FROM t1 ) AS t;
10372UPDATE v1, t1 SET t1.f1 = 'z' WHERE v1.f2 < 2 AND t1.f2 = v1.f2;
10373EXPLAIN FORMAT=JSON UPDATE v1, t1 SET t1.f1 = 'z' WHERE v1.f2 < 2 AND t1.f2 = v1.f2;
10374EXPLAIN
10375{
10376  "query_block": {
10377    "select_id": 1,
10378    "table": {
10379      "table_name": "<derived3>",
10380      "access_type": "ALL",
10381      "rows": 2,
10382      "filtered": 100,
10383      "attached_condition": "t.f2 < 2",
10384      "materialized": {
10385        "query_block": {
10386          "select_id": 3,
10387          "table": {
10388            "table_name": "t1",
10389            "access_type": "ALL",
10390            "rows": 2,
10391            "filtered": 100,
10392            "attached_condition": "t1.f2 < 2"
10393          }
10394        }
10395      }
10396    },
10397    "table": {
10398      "table_name": "t1",
10399      "access_type": "ALL",
10400      "rows": 2,
10401      "filtered": 100,
10402      "attached_condition": "t1.f2 = t.f2"
10403    }
10404  }
10405}
10406SELECT * FROM t1;
10407f1	f2
10408z	1
10409y	2
10410CREATE VIEW v2 AS SELECT f2 FROM ( SELECT DISTINCT f2 FROM t1 ) AS t;
10411SELECT * FROM v2, t1 WHERE v2.f2 < 2 AND t1.f2 = v2.f2;
10412f2	f1	f2
104131	z	1
10414EXPLAIN FORMAT=JSON SELECT * FROM v2, t1 WHERE v2.f2 < 2 AND t1.f2 = v2.f2;
10415EXPLAIN
10416{
10417  "query_block": {
10418    "select_id": 1,
10419    "table": {
10420      "table_name": "<derived3>",
10421      "access_type": "ALL",
10422      "rows": 2,
10423      "filtered": 100,
10424      "attached_condition": "t.f2 < 2",
10425      "materialized": {
10426        "query_block": {
10427          "select_id": 3,
10428          "temporary_table": {
10429            "table": {
10430              "table_name": "t1",
10431              "access_type": "ALL",
10432              "rows": 2,
10433              "filtered": 100,
10434              "attached_condition": "t1.f2 < 2"
10435            }
10436          }
10437        }
10438      }
10439    },
10440    "block-nl-join": {
10441      "table": {
10442        "table_name": "t1",
10443        "access_type": "ALL",
10444        "rows": 2,
10445        "filtered": 100
10446      },
10447      "buffer_type": "flat",
10448      "buffer_size": "65",
10449      "join_type": "BNL",
10450      "attached_condition": "t1.f2 = t.f2"
10451    }
10452  }
10453}
10454DROP VIEW v1,v2;
10455DROP TABLE t1;
10456#
10457# MDEV-18383: pushdown condition with the IF structure
10458#             defined with Item_cond item
10459#
10460CREATE TABLE t1(a INT, b INT);
10461CREATE TABLE t2(c INT, d INT);
10462INSERT INTO t1 VALUES (1,2),(3,4),(5,6);
10463INSERT INTO t2 VALUES (1,3),(3,7),(5,1);
10464SELECT *
10465FROM t1,
10466(
10467SELECT MAX(d) AS max_d,c
10468FROM t2
10469GROUP BY c
10470) AS tab
10471WHERE t1.a=tab.c AND
10472IF(2,t1.a=1 OR t1.b>5,1=1);
10473a	b	max_d	c
104741	2	3	1
104755	6	1	5
10476DROP TABLE t1,t2;
10477#
10478# MDEV-19139: pushdown condition with Item_func_set_user_var
10479#
10480CREATE TABLE t1 (a INT, b INT);
10481CREATE VIEW v1 AS SELECT a, MAX(b) FROM t1 GROUP BY a;
10482SELECT * FROM (SELECT 1 FROM v1 UNION (SELECT 1 FROM v1 WHERE @a := uuid())) dt;
104831
10484EXPLAIN FORMAT=JSON
10485SELECT * FROM (SELECT 1 FROM v1 UNION (SELECT 1 FROM v1 WHERE @a := uuid())) dt;
10486EXPLAIN
10487{
10488  "query_block": {
10489    "select_id": 1,
10490    "table": {
10491      "table_name": "<derived2>",
10492      "access_type": "ALL",
10493      "rows": 2,
10494      "filtered": 100,
10495      "materialized": {
10496        "query_block": {
10497          "union_result": {
10498            "table_name": "<union2,3>",
10499            "access_type": "ALL",
10500            "query_specifications": [
10501              {
10502                "query_block": {
10503                  "select_id": 2,
10504                  "table": {
10505                    "message": "no matching row in const table"
10506                  }
10507                }
10508              },
10509              {
10510                "query_block": {
10511                  "select_id": 3,
10512                  "operation": "UNION",
10513                  "table": {
10514                    "message": "no matching row in const table"
10515                  }
10516                }
10517              }
10518            ]
10519          }
10520        }
10521      }
10522    }
10523  }
10524}
10525DROP TABLE t1;
10526DROP VIEW v1;
10527#
10528# MDEV-21388 Wrong result of DAYNAME()=xxx in combination with condition_pushdown_for_derived=on
10529#
10530CREATE TABLE t1 (a INT, b DATE, c INT);
10531INSERT INTO t1 VALUES
10532(1,'2001-01-21',345),
10533(6,'2001-01-20',315),
10534(6,'2001-01-20',214);
10535CREATE TABLE t2 (a INT, b INT);
10536INSERT INTO t2 VALUES (2,19), (7,20);
10537CREATE VIEW v1 AS SELECT a, b, max(c) AS max_c FROM t1
10538GROUP BY a,b HAVING max_c < 707;
10539SELECT *, dayname(v1.b) FROM v1,t2 WHERE (v1.max_c>214) AND (t2.a>v1.a);
10540a	b	max_c	a	b	dayname(v1.b)
105411	2001-01-21	345	2	19	Sunday
105421	2001-01-21	345	7	20	Sunday
105436	2001-01-20	315	7	20	Saturday
10544SET optimizer_switch='condition_pushdown_for_derived=off';
10545SELECT *, dayname(v1.b) FROM v1,t2 WHERE (v1.max_c>214) AND (t2.a>v1.a) AND dayname(v1.b)='Sunday';
10546a	b	max_c	a	b	dayname(v1.b)
105471	2001-01-21	345	2	19	Sunday
105481	2001-01-21	345	7	20	Sunday
10549SET optimizer_switch='condition_pushdown_for_derived=on';
10550SELECT *, dayname(v1.b) FROM v1,t2 WHERE (v1.max_c>214) AND (t2.a>v1.a) AND dayname(v1.b)='Sunday';
10551a	b	max_c	a	b	dayname(v1.b)
105521	2001-01-21	345	2	19	Sunday
105531	2001-01-21	345	7	20	Sunday
10554DROP VIEW v1;
10555DROP TABLE t1, t2;
10556SET optimizer_switch=DEFAULT;
10557#
10558# MDEV-17177: an attempt to push down IN predicate when one of
10559#             the arguments is too complex to be cloned
10560#
10561CREATE TABLE t1 (a VARCHAR(8));
10562INSERT INTO t1 VALUES ('abc'),('def');
10563CREATE VIEW v1 AS SELECT * FROM t1 GROUP BY a;
10564SELECT * FROM v1 WHERE IF( a REGEXP 'def', 'foo', a ) IN ('abc', 'foobar');
10565a
10566abc
10567DROP VIEW v1;
10568DROP TABLE t1;
10569#
10570# MDEV-19179: pushdown into UNION of aggregation selects whose
10571#             corresponding columns have different names
10572#
10573create table t1 (a int);
10574insert into t1 values (3), (7), (1);
10575select *
10576from (select min(a) as x from t1 union all select max(a) as y from t1) t
10577where x>0;
10578x
105791
105807
10581explain extended select *
10582from (select min(a) as x from t1 union all select max(a) as y from t1) t
10583where x>0;
10584id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
105851	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	6	100.00	Using where
105862	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	3	100.00
105873	UNION	t1	ALL	NULL	NULL	NULL	NULL	3	100.00
10588Warnings:
10589Note	1003	/* select#1 */ select `t`.`x` AS `x` from (/* select#2 */ select min(`test`.`t1`.`a`) AS `x` from `test`.`t1` having `x` > 0 union all /* select#3 */ select max(`test`.`t1`.`a`) AS `x` from `test`.`t1` having `x` > 0) `t` where `t`.`x` > 0
10590prepare stmt from "select *
10591from (select min(a) as x from t1 union all select max(a) as y from t1) t
10592where x>0";
10593execute stmt;
10594x
105951
105967
10597execute stmt;
10598x
105991
106007
10601deallocate prepare stmt;
10602create view v1(m) as
10603select min(a) as x from t1 union all select max(a) as y from t1;
10604select * from v1 where m > 0;
10605m
106061
106077
10608drop view v1;
10609drop table t1;
10610#
10611# MDEV-25635: pushdown into grouping view using aggregate functions
10612#             with constant arguments via a mergeable derived table
10613#
10614create table t1 (a int);
10615insert into t1 values (3), (7), (1), (3), (7), (7), (3);
10616create view v1 as select a, sum(1) as f, sum(1) as g from t1 group by a;
10617select * from v1;
10618a	f	g
106191	1	1
106203	3	3
106217	3	3
10622select * from (select * from v1) as dt where a=f and a=g;
10623a	f	g
106241	1	1
106253	3	3
10626explain extended select * from (select * from v1) as dt where a=f and a=g;
10627id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
106281	PRIMARY	<derived3>	ALL	NULL	NULL	NULL	NULL	7	100.00	Using where
106293	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	7	100.00	Using temporary; Using filesort
10630Warnings:
10631Note	1003	/* select#1 */ select `v1`.`a` AS `a`,`v1`.`f` AS `f`,`v1`.`g` AS `g` from `test`.`v1` where `v1`.`a` = `v1`.`f` and `v1`.`a` = `v1`.`g`
10632create view v2 as select a, min(1) as f, min(1) as g from t1 group by a;
10633select * from v2;
10634a	f	g
106351	1	1
106363	1	1
106377	1	1
10638select * from (select * from v2) as dt where a=f and a=g;
10639a	f	g
106401	1	1
10641explain extended select * from (select * from v2) as dt where a=f and a=g;
10642id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
106431	PRIMARY	<derived3>	ALL	NULL	NULL	NULL	NULL	7	100.00	Using where
106443	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	7	100.00	Using temporary; Using filesort
10645Warnings:
10646Note	1003	/* select#1 */ select `v2`.`a` AS `a`,`v2`.`f` AS `f`,`v2`.`g` AS `g` from `test`.`v2` where `v2`.`a` = `v2`.`f` and `v2`.`a` = `v2`.`g`
10647drop view v1,v2;
10648drop table t1;
10649#
10650# MDEV-25969: Condition pushdown into derived table doesn't work if select list uses SP
10651#
10652create function f1(a int) returns int DETERMINISTIC return (a+1);
10653create table t1 (
10654pk int primary key,
10655a int,
10656b int,
10657key(a)
10658);
10659create table t2(a int);
10660insert into t2 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
10661create table t3(a int);
10662insert into t3 select A.a + B.a* 10 + C.a * 100 from t2 A, t2 B, t2 C;
10663insert into t1 select a,a,a from t3;
10664create view v1 as
10665select
10666t1.a as col1,
10667f1(t1.b) as col2
10668from
10669t1;
10670create view v2 as
10671select
10672t1.a as col1,
10673f1(t1.b) as col2
10674from
10675t1;
10676create view v3 as
10677select col2, col1 from v1
10678union all
10679select col2, col1 from v2;
10680explain select * from v3 where col1=123;
10681id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
106821	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	2	Using where
106832	DERIVED	t1	ref	a	a	5	const	1
106843	UNION	t1	ref	a	a	5	const	1
10685# This must use ref accesses for reading table t1, not full scans:
10686explain format=json
10687select * from v3 where col1=123 and col2=321;
10688EXPLAIN
10689{
10690  "query_block": {
10691    "select_id": 1,
10692    "table": {
10693      "table_name": "<derived2>",
10694      "access_type": "ALL",
10695      "rows": 2,
10696      "filtered": 100,
10697      "attached_condition": "v3.col1 = 123 and v3.col2 = 321",
10698      "materialized": {
10699        "query_block": {
10700          "union_result": {
10701            "table_name": "<union2,3>",
10702            "access_type": "ALL",
10703            "query_specifications": [
10704              {
10705                "query_block": {
10706                  "select_id": 2,
10707                  "table": {
10708                    "table_name": "t1",
10709                    "access_type": "ref",
10710                    "possible_keys": ["a"],
10711                    "key": "a",
10712                    "key_length": "5",
10713                    "used_key_parts": ["a"],
10714                    "ref": ["const"],
10715                    "rows": 1,
10716                    "filtered": 100
10717                  }
10718                }
10719              },
10720              {
10721                "query_block": {
10722                  "select_id": 3,
10723                  "operation": "UNION",
10724                  "table": {
10725                    "table_name": "t1",
10726                    "access_type": "ref",
10727                    "possible_keys": ["a"],
10728                    "key": "a",
10729                    "key_length": "5",
10730                    "used_key_parts": ["a"],
10731                    "ref": ["const"],
10732                    "rows": 1,
10733                    "filtered": 100
10734                  }
10735                }
10736              }
10737            ]
10738          }
10739        }
10740      }
10741    }
10742  }
10743}
10744drop function f1;
10745drop view v1,v2,v3;
10746drop table t1, t2,t3;
10747#
10748# Another testcase, with pushdown through GROUP BY
10749#
10750create table t1 (a int, b  int);
10751insert into t1 values (1,1),(2,2),(3,3);
10752create function f1(a int) returns int DETERMINISTIC return (a+1);
10753create view v2(a, a2, s) as
10754select a, f1(a), sum(b) from t1 group by a, f1(a);
10755# Here,
10756#   "(s+1) > 10" will be pushed into HAVING
10757#   "a > 1" will be pushed all the way to the table scan on t1
10758#   "a2>123" will be pushed into HAVING (as it refers to an SP call which
10759#                                       prevents pushing it to the WHERE)
10760explain format=json
10761select * from v2 where (s+1) > 10 AND a > 1 and a2>123;
10762EXPLAIN
10763{
10764  "query_block": {
10765    "select_id": 1,
10766    "table": {
10767      "table_name": "<derived2>",
10768      "access_type": "ALL",
10769      "rows": 3,
10770      "filtered": 100,
10771      "attached_condition": "v2.s + 1 > 10 and v2.a > 1 and v2.a2 > 123",
10772      "materialized": {
10773        "query_block": {
10774          "select_id": 2,
10775          "having_condition": "s + 1 > 10 and a2 > 123",
10776          "filesort": {
10777            "sort_key": "t1.a, f1(t1.a)",
10778            "temporary_table": {
10779              "table": {
10780                "table_name": "t1",
10781                "access_type": "ALL",
10782                "rows": 3,
10783                "filtered": 100,
10784                "attached_condition": "t1.a > 1"
10785              }
10786            }
10787          }
10788        }
10789      }
10790    }
10791  }
10792}
10793# Extra test for 10.4+: Check that this works for pushdown into IN
10794# subqueries:
10795create table t4 (a int, b int, c decimal);
10796insert into t4 select a,a,a from t1;
10797# The subquery must be materialized and must have
10798#  "attached_condition": "t1.a + 1 > 10",
10799#  "having_condition": "`f1(a)` > 1 and `sum(b)` > 123",
10800explain format=json
10801select *
10802from t4
10803where
10804(a,b,c) in (select a, f1(a), sum(b) from t1 group by a, f1(a))
10805and
10806(a+1) > 10 AND b > 1 and c>123;
10807EXPLAIN
10808{
10809  "query_block": {
10810    "select_id": 1,
10811    "table": {
10812      "table_name": "t4",
10813      "access_type": "ALL",
10814      "rows": 3,
10815      "filtered": 100,
10816      "attached_condition": "t4.a + 1 > 10 and t4.b > 1 and t4.c > 123 and t4.a is not null and t4.b is not null and t4.c is not null"
10817    },
10818    "table": {
10819      "table_name": "<subquery2>",
10820      "access_type": "eq_ref",
10821      "possible_keys": ["distinct_key"],
10822      "key": "distinct_key",
10823      "key_length": "23",
10824      "used_key_parts": ["a", "f1(a)", "sum(b)"],
10825      "ref": ["test.t4.a", "test.t4.b", "test.t4.c"],
10826      "rows": 1,
10827      "filtered": 100,
10828      "attached_condition": "t4.c = `<subquery2>`.`sum(b)`",
10829      "materialized": {
10830        "unique": 1,
10831        "query_block": {
10832          "select_id": 2,
10833          "having_condition": "`f1(a)` > 1 and `sum(b)` > 123",
10834          "temporary_table": {
10835            "table": {
10836              "table_name": "t1",
10837              "access_type": "ALL",
10838              "rows": 3,
10839              "filtered": 100,
10840              "attached_condition": "t1.a + 1 > 10"
10841            }
10842          }
10843        }
10844      }
10845    }
10846  }
10847}
10848drop view v2;
10849drop function f1;
10850drop table t1, t4;
10851# End of 10.2 tests
10852#
10853# MDEV-14579: pushdown conditions into materialized views/derived tables
10854# that are defined with EXIST or/and INTERSECT
10855#
10856create table t1 (a int, b int, c int);
10857create table t2 (a int, b int, c int);
10858insert into t1 values
10859(1,21,345), (1,33,7), (8,33,114), (1,21,500), (1,19,117), (5,14,787),
10860(8,33,123), (9,10,211), (5,16,207), (1,33,988), (5,27,132), (1,21,104),
10861(6,20,309), (6,20,315), (1,21,101), (4,33,404), (9,10,800), (1,21,123);
10862insert into t2 values
10863(2,3,207), (1,16,909), (5,14,312),
10864(5,33,207), (6,20,211), (1,19,132),
10865(8,33,117), (3,21,231), (6,23,303);
10866create view v1 as
10867select a, b, min(c) as c from t1
10868where t1.a<9 group by a,b having c < 300
10869intersect
10870select a, b, min(c) as c from t1
10871where t1.b>10 group by a,b having c > 100;
10872# using intersect in view definition
10873# conjunctive subformulas : pushing into WHERE
10874set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.a=t2.a) and (v1.a<5);
10875a	b	c	a	b	c
108761	21	101	1	16	909
108771	19	117	1	16	909
108781	21	101	1	19	132
108791	19	117	1	19	132
10880select * from v1,t2 where (v1.a=t2.a) and (v1.a<5);
10881a	b	c	a	b	c
108821	21	101	1	16	909
108831	19	117	1	16	909
108841	21	101	1	19	132
108851	19	117	1	19	132
10886explain select * from v1,t2 where (v1.a=t2.a) and (v1.a<5);
10887id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
108881	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
108891	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.a	2
108902	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
108913	INTERSECT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
10892NULL	INTERSECT RESULT	<intersect2,3>	ALL	NULL	NULL	NULL	NULL	NULL
10893explain format=json select * from v1,t2 where (v1.a=t2.a) and (v1.a<5);
10894EXPLAIN
10895{
10896  "query_block": {
10897    "select_id": 1,
10898    "table": {
10899      "table_name": "t2",
10900      "access_type": "ALL",
10901      "rows": 9,
10902      "filtered": 100,
10903      "attached_condition": "t2.a < 5 and t2.a is not null"
10904    },
10905    "table": {
10906      "table_name": "<derived2>",
10907      "access_type": "ref",
10908      "possible_keys": ["key0"],
10909      "key": "key0",
10910      "key_length": "5",
10911      "used_key_parts": ["a"],
10912      "ref": ["test.t2.a"],
10913      "rows": 2,
10914      "filtered": 100,
10915      "materialized": {
10916        "query_block": {
10917          "union_result": {
10918            "table_name": "<intersect2,3>",
10919            "access_type": "ALL",
10920            "query_specifications": [
10921              {
10922                "query_block": {
10923                  "select_id": 2,
10924                  "having_condition": "c < 300",
10925                  "filesort": {
10926                    "sort_key": "t1.a, t1.b",
10927                    "temporary_table": {
10928                      "table": {
10929                        "table_name": "t1",
10930                        "access_type": "ALL",
10931                        "rows": 18,
10932                        "filtered": 100,
10933                        "attached_condition": "t1.a < 9 and t1.a < 5"
10934                      }
10935                    }
10936                  }
10937                }
10938              },
10939              {
10940                "query_block": {
10941                  "select_id": 3,
10942                  "operation": "INTERSECT",
10943                  "having_condition": "c > 100",
10944                  "filesort": {
10945                    "sort_key": "t1.a, t1.b",
10946                    "temporary_table": {
10947                      "table": {
10948                        "table_name": "t1",
10949                        "access_type": "ALL",
10950                        "rows": 18,
10951                        "filtered": 100,
10952                        "attached_condition": "t1.b > 10 and t1.a < 5"
10953                      }
10954                    }
10955                  }
10956                }
10957              }
10958            ]
10959          }
10960        }
10961      }
10962    }
10963  }
10964}
10965# using intersect in view definition
10966# conjunctive subformulas : pushing into WHERE
10967# pushing equalities
10968set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.a=t2.a) and (v1.a=8);
10969a	b	c	a	b	c
109708	33	114	8	33	117
10971select * from v1,t2 where (v1.a=t2.a) and (v1.a=8);
10972a	b	c	a	b	c
109738	33	114	8	33	117
10974explain select * from v1,t2 where (v1.a=t2.a) and (v1.a=8);
10975id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
109761	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
109771	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	18	Using where; Using join buffer (flat, BNL join)
109782	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
109793	INTERSECT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
10980NULL	INTERSECT RESULT	<intersect2,3>	ALL	NULL	NULL	NULL	NULL	NULL
10981explain format=json select * from v1,t2 where (v1.a=t2.a) and (v1.a=8);
10982EXPLAIN
10983{
10984  "query_block": {
10985    "select_id": 1,
10986    "table": {
10987      "table_name": "t2",
10988      "access_type": "ALL",
10989      "rows": 9,
10990      "filtered": 100,
10991      "attached_condition": "t2.a = 8"
10992    },
10993    "block-nl-join": {
10994      "table": {
10995        "table_name": "<derived2>",
10996        "access_type": "ALL",
10997        "rows": 18,
10998        "filtered": 100,
10999        "attached_condition": "v1.a = 8"
11000      },
11001      "buffer_type": "flat",
11002      "buffer_size": "173",
11003      "join_type": "BNL",
11004      "materialized": {
11005        "query_block": {
11006          "union_result": {
11007            "table_name": "<intersect2,3>",
11008            "access_type": "ALL",
11009            "query_specifications": [
11010              {
11011                "query_block": {
11012                  "select_id": 2,
11013                  "having_condition": "c < 300",
11014                  "filesort": {
11015                    "sort_key": "t1.b",
11016                    "temporary_table": {
11017                      "table": {
11018                        "table_name": "t1",
11019                        "access_type": "ALL",
11020                        "rows": 18,
11021                        "filtered": 100,
11022                        "attached_condition": "t1.a = 8"
11023                      }
11024                    }
11025                  }
11026                }
11027              },
11028              {
11029                "query_block": {
11030                  "select_id": 3,
11031                  "operation": "INTERSECT",
11032                  "having_condition": "c > 100",
11033                  "filesort": {
11034                    "sort_key": "t1.b",
11035                    "temporary_table": {
11036                      "table": {
11037                        "table_name": "t1",
11038                        "access_type": "ALL",
11039                        "rows": 18,
11040                        "filtered": 100,
11041                        "attached_condition": "t1.a = 8 and t1.b > 10"
11042                      }
11043                    }
11044                  }
11045                }
11046              }
11047            ]
11048          }
11049        }
11050      }
11051    }
11052  }
11053}
11054# using intersect in view definition
11055# conjunctive subformulas : pushing into WHERE using equalities
11056set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.a=t2.a) and (t2.a=8);
11057a	b	c	a	b	c
110588	33	114	8	33	117
11059select * from v1,t2 where (v1.a=t2.a) and (t2.a=8);
11060a	b	c	a	b	c
110618	33	114	8	33	117
11062explain select * from v1,t2 where (v1.a=t2.a) and (t2.a=8);
11063id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
110641	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
110651	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	18	Using where; Using join buffer (flat, BNL join)
110662	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
110673	INTERSECT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
11068NULL	INTERSECT RESULT	<intersect2,3>	ALL	NULL	NULL	NULL	NULL	NULL
11069explain format=json select * from v1,t2 where (v1.a=t2.a) and (t2.a=8);
11070EXPLAIN
11071{
11072  "query_block": {
11073    "select_id": 1,
11074    "table": {
11075      "table_name": "t2",
11076      "access_type": "ALL",
11077      "rows": 9,
11078      "filtered": 100,
11079      "attached_condition": "t2.a = 8"
11080    },
11081    "block-nl-join": {
11082      "table": {
11083        "table_name": "<derived2>",
11084        "access_type": "ALL",
11085        "rows": 18,
11086        "filtered": 100,
11087        "attached_condition": "v1.a = 8"
11088      },
11089      "buffer_type": "flat",
11090      "buffer_size": "173",
11091      "join_type": "BNL",
11092      "materialized": {
11093        "query_block": {
11094          "union_result": {
11095            "table_name": "<intersect2,3>",
11096            "access_type": "ALL",
11097            "query_specifications": [
11098              {
11099                "query_block": {
11100                  "select_id": 2,
11101                  "having_condition": "c < 300",
11102                  "filesort": {
11103                    "sort_key": "t1.b",
11104                    "temporary_table": {
11105                      "table": {
11106                        "table_name": "t1",
11107                        "access_type": "ALL",
11108                        "rows": 18,
11109                        "filtered": 100,
11110                        "attached_condition": "t1.a = 8"
11111                      }
11112                    }
11113                  }
11114                }
11115              },
11116              {
11117                "query_block": {
11118                  "select_id": 3,
11119                  "operation": "INTERSECT",
11120                  "having_condition": "c > 100",
11121                  "filesort": {
11122                    "sort_key": "t1.b",
11123                    "temporary_table": {
11124                      "table": {
11125                        "table_name": "t1",
11126                        "access_type": "ALL",
11127                        "rows": 18,
11128                        "filtered": 100,
11129                        "attached_condition": "t1.a = 8 and t1.b > 10"
11130                      }
11131                    }
11132                  }
11133                }
11134              }
11135            ]
11136          }
11137        }
11138      }
11139    }
11140  }
11141}
11142# using intersect in view definition
11143# conjunctive subformulas : pushing into HAVING
11144set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.a=t2.a) and (v1.c>200);
11145a	b	c	a	b	c
111465	16	207	5	14	312
111475	16	207	5	33	207
11148select * from v1,t2 where (v1.a=t2.a) and (v1.c>200);
11149a	b	c	a	b	c
111505	16	207	5	14	312
111515	16	207	5	33	207
11152explain select * from v1,t2 where (v1.a=t2.a) and (v1.c>200);
11153id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
111541	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
111551	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.a	2	Using where
111562	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
111573	INTERSECT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
11158NULL	INTERSECT RESULT	<intersect2,3>	ALL	NULL	NULL	NULL	NULL	NULL
11159explain format=json select * from v1,t2 where (v1.a=t2.a) and (v1.c>200);
11160EXPLAIN
11161{
11162  "query_block": {
11163    "select_id": 1,
11164    "table": {
11165      "table_name": "t2",
11166      "access_type": "ALL",
11167      "rows": 9,
11168      "filtered": 100,
11169      "attached_condition": "t2.a is not null"
11170    },
11171    "table": {
11172      "table_name": "<derived2>",
11173      "access_type": "ref",
11174      "possible_keys": ["key0"],
11175      "key": "key0",
11176      "key_length": "5",
11177      "used_key_parts": ["a"],
11178      "ref": ["test.t2.a"],
11179      "rows": 2,
11180      "filtered": 100,
11181      "attached_condition": "v1.c > 200",
11182      "materialized": {
11183        "query_block": {
11184          "union_result": {
11185            "table_name": "<intersect2,3>",
11186            "access_type": "ALL",
11187            "query_specifications": [
11188              {
11189                "query_block": {
11190                  "select_id": 2,
11191                  "having_condition": "c < 300 and c > 200",
11192                  "filesort": {
11193                    "sort_key": "t1.a, t1.b",
11194                    "temporary_table": {
11195                      "table": {
11196                        "table_name": "t1",
11197                        "access_type": "ALL",
11198                        "rows": 18,
11199                        "filtered": 100,
11200                        "attached_condition": "t1.a < 9"
11201                      }
11202                    }
11203                  }
11204                }
11205              },
11206              {
11207                "query_block": {
11208                  "select_id": 3,
11209                  "operation": "INTERSECT",
11210                  "having_condition": "c > 100 and c > 200",
11211                  "filesort": {
11212                    "sort_key": "t1.a, t1.b",
11213                    "temporary_table": {
11214                      "table": {
11215                        "table_name": "t1",
11216                        "access_type": "ALL",
11217                        "rows": 18,
11218                        "filtered": 100,
11219                        "attached_condition": "t1.b > 10"
11220                      }
11221                    }
11222                  }
11223                }
11224              }
11225            ]
11226          }
11227        }
11228      }
11229    }
11230  }
11231}
11232# using intersect in view definition
11233# conjunctive subformulas : pushing into WHERE
11234# conjunctive subformulas : pushing into HAVING
11235set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.a=t2.a) and (v1.a<5) and (v1.c>110);
11236a	b	c	a	b	c
112371	19	117	1	16	909
112381	19	117	1	19	132
11239select * from v1,t2 where (v1.a=t2.a) and (v1.a<5) and (v1.c>110);
11240a	b	c	a	b	c
112411	19	117	1	16	909
112421	19	117	1	19	132
11243explain select * from v1,t2 where (v1.a=t2.a) and (v1.a<5) and (v1.c>110);
11244id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
112451	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
112461	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.a	2	Using where
112472	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
112483	INTERSECT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
11249NULL	INTERSECT RESULT	<intersect2,3>	ALL	NULL	NULL	NULL	NULL	NULL
11250explain format=json select * from v1,t2 where (v1.a=t2.a) and (v1.a<5) and (v1.c>110);
11251EXPLAIN
11252{
11253  "query_block": {
11254    "select_id": 1,
11255    "table": {
11256      "table_name": "t2",
11257      "access_type": "ALL",
11258      "rows": 9,
11259      "filtered": 100,
11260      "attached_condition": "t2.a < 5 and t2.a is not null"
11261    },
11262    "table": {
11263      "table_name": "<derived2>",
11264      "access_type": "ref",
11265      "possible_keys": ["key0"],
11266      "key": "key0",
11267      "key_length": "5",
11268      "used_key_parts": ["a"],
11269      "ref": ["test.t2.a"],
11270      "rows": 2,
11271      "filtered": 100,
11272      "attached_condition": "v1.c > 110",
11273      "materialized": {
11274        "query_block": {
11275          "union_result": {
11276            "table_name": "<intersect2,3>",
11277            "access_type": "ALL",
11278            "query_specifications": [
11279              {
11280                "query_block": {
11281                  "select_id": 2,
11282                  "having_condition": "c < 300 and c > 110",
11283                  "filesort": {
11284                    "sort_key": "t1.a, t1.b",
11285                    "temporary_table": {
11286                      "table": {
11287                        "table_name": "t1",
11288                        "access_type": "ALL",
11289                        "rows": 18,
11290                        "filtered": 100,
11291                        "attached_condition": "t1.a < 9 and t1.a < 5"
11292                      }
11293                    }
11294                  }
11295                }
11296              },
11297              {
11298                "query_block": {
11299                  "select_id": 3,
11300                  "operation": "INTERSECT",
11301                  "having_condition": "c > 100 and c > 110",
11302                  "filesort": {
11303                    "sort_key": "t1.a, t1.b",
11304                    "temporary_table": {
11305                      "table": {
11306                        "table_name": "t1",
11307                        "access_type": "ALL",
11308                        "rows": 18,
11309                        "filtered": 100,
11310                        "attached_condition": "t1.b > 10 and t1.a < 5"
11311                      }
11312                    }
11313                  }
11314                }
11315              }
11316            ]
11317          }
11318        }
11319      }
11320    }
11321  }
11322}
11323# using intersect in view definition
11324# extracted or formula : pushing into WHERE
11325set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.a=t2.a) and ((v1.b>27) or (v1.b<19));
11326a	b	c	a	b	c
113275	16	207	5	14	312
113285	16	207	5	33	207
113298	33	114	8	33	117
11330select * from v1,t2 where (v1.a=t2.a) and ((v1.b>27) or (v1.b<19));
11331a	b	c	a	b	c
113325	16	207	5	14	312
113335	16	207	5	33	207
113348	33	114	8	33	117
11335explain select * from v1,t2 where (v1.a=t2.a) and ((v1.b>27) or (v1.b<19));
11336id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
113371	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
113381	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.a	2	Using where
113392	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
113403	INTERSECT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
11341NULL	INTERSECT RESULT	<intersect2,3>	ALL	NULL	NULL	NULL	NULL	NULL
11342explain format=json select * from v1,t2 where (v1.a=t2.a) and ((v1.b>27) or (v1.b<19));
11343EXPLAIN
11344{
11345  "query_block": {
11346    "select_id": 1,
11347    "table": {
11348      "table_name": "t2",
11349      "access_type": "ALL",
11350      "rows": 9,
11351      "filtered": 100,
11352      "attached_condition": "t2.a is not null"
11353    },
11354    "table": {
11355      "table_name": "<derived2>",
11356      "access_type": "ref",
11357      "possible_keys": ["key0"],
11358      "key": "key0",
11359      "key_length": "5",
11360      "used_key_parts": ["a"],
11361      "ref": ["test.t2.a"],
11362      "rows": 2,
11363      "filtered": 100,
11364      "attached_condition": "v1.b > 27 or v1.b < 19",
11365      "materialized": {
11366        "query_block": {
11367          "union_result": {
11368            "table_name": "<intersect2,3>",
11369            "access_type": "ALL",
11370            "query_specifications": [
11371              {
11372                "query_block": {
11373                  "select_id": 2,
11374                  "having_condition": "c < 300",
11375                  "filesort": {
11376                    "sort_key": "t1.a, t1.b",
11377                    "temporary_table": {
11378                      "table": {
11379                        "table_name": "t1",
11380                        "access_type": "ALL",
11381                        "rows": 18,
11382                        "filtered": 100,
11383                        "attached_condition": "t1.a < 9 and (t1.b > 27 or t1.b < 19)"
11384                      }
11385                    }
11386                  }
11387                }
11388              },
11389              {
11390                "query_block": {
11391                  "select_id": 3,
11392                  "operation": "INTERSECT",
11393                  "having_condition": "c > 100",
11394                  "filesort": {
11395                    "sort_key": "t1.a, t1.b",
11396                    "temporary_table": {
11397                      "table": {
11398                        "table_name": "t1",
11399                        "access_type": "ALL",
11400                        "rows": 18,
11401                        "filtered": 100,
11402                        "attached_condition": "t1.b > 10 and (t1.b > 27 or t1.b < 19)"
11403                      }
11404                    }
11405                  }
11406                }
11407              }
11408            ]
11409          }
11410        }
11411      }
11412    }
11413  }
11414}
11415# using intersect in view definition
11416# extracted or formula : pushing into HAVING
11417set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where
11418(v1.a=t2.a) and ((v1.c>200) or (v1.c<105));
11419a	b	c	a	b	c
114201	21	101	1	16	909
114215	16	207	5	14	312
114225	16	207	5	33	207
114231	21	101	1	19	132
11424select * from v1,t2 where
11425(v1.a=t2.a) and ((v1.c>200) or (v1.c<105));
11426a	b	c	a	b	c
114271	21	101	1	16	909
114285	16	207	5	14	312
114295	16	207	5	33	207
114301	21	101	1	19	132
11431explain select * from v1,t2 where
11432(v1.a=t2.a) and ((v1.c>200) or (v1.c<105));
11433id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
114341	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
114351	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.a	2	Using where
114362	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
114373	INTERSECT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
11438NULL	INTERSECT RESULT	<intersect2,3>	ALL	NULL	NULL	NULL	NULL	NULL
11439explain format=json select * from v1,t2 where
11440(v1.a=t2.a) and ((v1.c>200) or (v1.c<105));
11441EXPLAIN
11442{
11443  "query_block": {
11444    "select_id": 1,
11445    "table": {
11446      "table_name": "t2",
11447      "access_type": "ALL",
11448      "rows": 9,
11449      "filtered": 100,
11450      "attached_condition": "t2.a is not null"
11451    },
11452    "table": {
11453      "table_name": "<derived2>",
11454      "access_type": "ref",
11455      "possible_keys": ["key0"],
11456      "key": "key0",
11457      "key_length": "5",
11458      "used_key_parts": ["a"],
11459      "ref": ["test.t2.a"],
11460      "rows": 2,
11461      "filtered": 100,
11462      "attached_condition": "v1.c > 200 or v1.c < 105",
11463      "materialized": {
11464        "query_block": {
11465          "union_result": {
11466            "table_name": "<intersect2,3>",
11467            "access_type": "ALL",
11468            "query_specifications": [
11469              {
11470                "query_block": {
11471                  "select_id": 2,
11472                  "having_condition": "c < 300 and (c > 200 or c < 105)",
11473                  "filesort": {
11474                    "sort_key": "t1.a, t1.b",
11475                    "temporary_table": {
11476                      "table": {
11477                        "table_name": "t1",
11478                        "access_type": "ALL",
11479                        "rows": 18,
11480                        "filtered": 100,
11481                        "attached_condition": "t1.a < 9"
11482                      }
11483                    }
11484                  }
11485                }
11486              },
11487              {
11488                "query_block": {
11489                  "select_id": 3,
11490                  "operation": "INTERSECT",
11491                  "having_condition": "c > 100 and (c > 200 or c < 105)",
11492                  "filesort": {
11493                    "sort_key": "t1.a, t1.b",
11494                    "temporary_table": {
11495                      "table": {
11496                        "table_name": "t1",
11497                        "access_type": "ALL",
11498                        "rows": 18,
11499                        "filtered": 100,
11500                        "attached_condition": "t1.b > 10"
11501                      }
11502                    }
11503                  }
11504                }
11505              }
11506            ]
11507          }
11508        }
11509      }
11510    }
11511  }
11512}
11513# using intersect in view definition
11514# extracted or formula : pushing into WHERE
11515# extracted or formula : pushing into HAVING using equalities
11516# pushing equalities
11517set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where
11518((v1.a>3) and (t2.c>110) and (v1.c=t2.c)) or
11519((v1.a=1) and (v1.c<110));
11520a	b	c	a	b	c
115211	21	101	2	3	207
115221	21	101	1	16	909
115231	21	101	5	14	312
115241	21	101	5	33	207
115251	21	101	6	20	211
115261	21	101	1	19	132
115271	21	101	8	33	117
115281	21	101	3	21	231
115291	21	101	6	23	303
115305	16	207	2	3	207
115315	16	207	5	33	207
115325	27	132	1	19	132
11533select * from v1,t2 where
11534((v1.a>3) and (t2.c>110) and (v1.c=t2.c)) or
11535((v1.a=1) and (v1.c<110));
11536a	b	c	a	b	c
115371	21	101	2	3	207
115381	21	101	1	16	909
115391	21	101	5	14	312
115401	21	101	5	33	207
115411	21	101	6	20	211
115421	21	101	1	19	132
115431	21	101	8	33	117
115441	21	101	3	21	231
115451	21	101	6	23	303
115465	16	207	2	3	207
115475	16	207	5	33	207
115485	27	132	1	19	132
11549explain select * from v1,t2 where
11550((v1.a>3) and (t2.c>110) and (v1.c=t2.c)) or
11551((v1.a=1) and (v1.c<110));
11552id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
115531	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9
115541	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	18	Using where; Using join buffer (flat, BNL join)
115552	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
115563	INTERSECT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
11557NULL	INTERSECT RESULT	<intersect2,3>	ALL	NULL	NULL	NULL	NULL	NULL
11558explain format=json select * from v1,t2 where
11559((v1.a>3) and (t2.c>110) and (v1.c=t2.c)) or
11560((v1.a=1) and (v1.c<110));
11561EXPLAIN
11562{
11563  "query_block": {
11564    "select_id": 1,
11565    "table": {
11566      "table_name": "t2",
11567      "access_type": "ALL",
11568      "rows": 9,
11569      "filtered": 100
11570    },
11571    "block-nl-join": {
11572      "table": {
11573        "table_name": "<derived2>",
11574        "access_type": "ALL",
11575        "rows": 18,
11576        "filtered": 100,
11577        "attached_condition": "v1.a > 3 or v1.a = 1 and v1.c < 110"
11578      },
11579      "buffer_type": "flat",
11580      "buffer_size": "173",
11581      "join_type": "BNL",
11582      "attached_condition": "v1.c = t2.c and v1.a > 3 and t2.c > 110 or v1.a = 1 and v1.c < 110",
11583      "materialized": {
11584        "query_block": {
11585          "union_result": {
11586            "table_name": "<intersect2,3>",
11587            "access_type": "ALL",
11588            "query_specifications": [
11589              {
11590                "query_block": {
11591                  "select_id": 2,
11592                  "having_condition": "c < 300 and (t1.a > 3 and c > 110 or t1.a = 1 and c < 110)",
11593                  "filesort": {
11594                    "sort_key": "t1.a, t1.b",
11595                    "temporary_table": {
11596                      "table": {
11597                        "table_name": "t1",
11598                        "access_type": "ALL",
11599                        "rows": 18,
11600                        "filtered": 100,
11601                        "attached_condition": "t1.a < 9 and (t1.a > 3 or t1.a = 1)"
11602                      }
11603                    }
11604                  }
11605                }
11606              },
11607              {
11608                "query_block": {
11609                  "select_id": 3,
11610                  "operation": "INTERSECT",
11611                  "having_condition": "c > 100 and (t1.a > 3 and c > 110 or t1.a = 1 and c < 110)",
11612                  "filesort": {
11613                    "sort_key": "t1.a, t1.b",
11614                    "temporary_table": {
11615                      "table": {
11616                        "table_name": "t1",
11617                        "access_type": "ALL",
11618                        "rows": 18,
11619                        "filtered": 100,
11620                        "attached_condition": "t1.b > 10 and (t1.a > 3 or t1.a = 1)"
11621                      }
11622                    }
11623                  }
11624                }
11625              }
11626            ]
11627          }
11628        }
11629      }
11630    }
11631  }
11632}
11633# using intersect in view definition
11634# prepare of a query
11635# conjunctive subformulas : pushing into WHERE
11636# conjunctive subformulas : pushing into HAVING
11637prepare stmt from "select * from v1,t2
11638                   where (v1.a=t2.a) and (v1.a<5) and (v1.c>110);";
11639execute stmt;
11640a	b	c	a	b	c
116411	19	117	1	16	909
116421	19	117	1	19	132
11643execute stmt;
11644a	b	c	a	b	c
116451	19	117	1	16	909
116461	19	117	1	19	132
11647deallocate prepare stmt;
11648# using intersect in derived table definition
11649# extracted or formula : pushing into WHERE using equalities
11650# extracted or formula : pushing into HAVING
11651# pushing equalities
11652set statement optimizer_switch='condition_pushdown_for_derived=off' for select *
11653from t2,
11654(select a, b, min(c) as c from t1
11655where t1.a<9 group by a,b having c < 300
11656intersect
11657select a, b, min(c) as c from t1
11658where t1.b>10 group by a,b having c > 100) as d1
11659where
11660(d1.b=t2.b) and
11661(((t2.b>13) and (t2.c=909)) or
11662((d1.a<4) and (d1.c<200)));
11663a	b	c	a	b	c
116641	16	909	5	16	207
116651	19	132	1	19	117
116663	21	231	1	21	101
11667select *
11668from t2,
11669(select a, b, min(c) as c from t1
11670where t1.a<9 group by a,b having c < 300
11671intersect
11672select a, b, min(c) as c from t1
11673where t1.b>10 group by a,b having c > 100) as d1
11674where
11675(d1.b=t2.b) and
11676(((t2.b>13) and (t2.c=909)) or
11677((d1.a<4) and (d1.c<200)));
11678a	b	c	a	b	c
116791	16	909	5	16	207
116801	19	132	1	19	117
116813	21	231	1	21	101
11682explain select *
11683from t2,
11684(select a, b, min(c) as c from t1
11685where t1.a<9 group by a,b having c < 300
11686intersect
11687select a, b, min(c) as c from t1
11688where t1.b>10 group by a,b having c > 100) as d1
11689where
11690(d1.b=t2.b) and
11691(((t2.b>13) and (t2.c=909)) or
11692((d1.a<4) and (d1.c<200)));
11693id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
116941	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
116951	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.b	2	Using where
116962	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
116973	INTERSECT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
11698NULL	INTERSECT RESULT	<intersect2,3>	ALL	NULL	NULL	NULL	NULL	NULL
11699explain format=json select *
11700from t2,
11701(select a, b, min(c) as c from t1
11702where t1.a<9 group by a,b having c < 300
11703intersect
11704select a, b, min(c) as c from t1
11705where t1.b>10 group by a,b having c > 100) as d1
11706where
11707(d1.b=t2.b) and
11708(((t2.b>13) and (t2.c=909)) or
11709((d1.a<4) and (d1.c<200)));
11710EXPLAIN
11711{
11712  "query_block": {
11713    "select_id": 1,
11714    "table": {
11715      "table_name": "t2",
11716      "access_type": "ALL",
11717      "rows": 9,
11718      "filtered": 100,
11719      "attached_condition": "t2.b is not null"
11720    },
11721    "table": {
11722      "table_name": "<derived2>",
11723      "access_type": "ref",
11724      "possible_keys": ["key0"],
11725      "key": "key0",
11726      "key_length": "5",
11727      "used_key_parts": ["b"],
11728      "ref": ["test.t2.b"],
11729      "rows": 2,
11730      "filtered": 100,
11731      "attached_condition": "t2.c = 909 and t2.b > 13 or d1.a < 4 and d1.c < 200",
11732      "materialized": {
11733        "query_block": {
11734          "union_result": {
11735            "table_name": "<intersect2,3>",
11736            "access_type": "ALL",
11737            "query_specifications": [
11738              {
11739                "query_block": {
11740                  "select_id": 2,
11741                  "having_condition": "c < 300 and (t1.b > 13 or t1.a < 4 and c < 200)",
11742                  "filesort": {
11743                    "sort_key": "t1.a, t1.b",
11744                    "temporary_table": {
11745                      "table": {
11746                        "table_name": "t1",
11747                        "access_type": "ALL",
11748                        "rows": 18,
11749                        "filtered": 100,
11750                        "attached_condition": "t1.a < 9 and (t1.b > 13 or t1.a < 4)"
11751                      }
11752                    }
11753                  }
11754                }
11755              },
11756              {
11757                "query_block": {
11758                  "select_id": 3,
11759                  "operation": "INTERSECT",
11760                  "having_condition": "c > 100 and (t1.b > 13 or t1.a < 4 and c < 200)",
11761                  "filesort": {
11762                    "sort_key": "t1.a, t1.b",
11763                    "temporary_table": {
11764                      "table": {
11765                        "table_name": "t1",
11766                        "access_type": "ALL",
11767                        "rows": 18,
11768                        "filtered": 100,
11769                        "attached_condition": "t1.b > 10 and (t1.b > 13 or t1.a < 4)"
11770                      }
11771                    }
11772                  }
11773                }
11774              }
11775            ]
11776          }
11777        }
11778      }
11779    }
11780  }
11781}
11782drop view v1;
11783create view v1 as
11784select a, b, max(c) as c from t1
11785where t1.a<9 group by a,b having c > 200
11786except
11787select a, b, max(c) as c from t1
11788where t1.b>10 group by a,b having c < 300;
11789# using except in view definition
11790# conjunctive subformulas : pushing into WHERE
11791set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.a=t2.a) and (v1.a<5);
11792a	b	c	a	b	c
117931	33	988	1	16	909
117941	21	500	1	16	909
117951	33	988	1	19	132
117961	21	500	1	19	132
11797select * from v1,t2 where (v1.a=t2.a) and (v1.a<5);
11798a	b	c	a	b	c
117991	33	988	1	16	909
118001	21	500	1	16	909
118011	33	988	1	19	132
118021	21	500	1	19	132
11803explain select * from v1,t2 where (v1.a=t2.a) and (v1.a<5);
11804id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
118051	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
118061	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.a	2
118072	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
118083	EXCEPT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
11809NULL	EXCEPT RESULT	<except2,3>	ALL	NULL	NULL	NULL	NULL	NULL
11810explain format=json select * from v1,t2 where (v1.a=t2.a) and (v1.a<5);
11811EXPLAIN
11812{
11813  "query_block": {
11814    "select_id": 1,
11815    "table": {
11816      "table_name": "t2",
11817      "access_type": "ALL",
11818      "rows": 9,
11819      "filtered": 100,
11820      "attached_condition": "t2.a < 5 and t2.a is not null"
11821    },
11822    "table": {
11823      "table_name": "<derived2>",
11824      "access_type": "ref",
11825      "possible_keys": ["key0"],
11826      "key": "key0",
11827      "key_length": "5",
11828      "used_key_parts": ["a"],
11829      "ref": ["test.t2.a"],
11830      "rows": 2,
11831      "filtered": 100,
11832      "materialized": {
11833        "query_block": {
11834          "union_result": {
11835            "table_name": "<except2,3>",
11836            "access_type": "ALL",
11837            "query_specifications": [
11838              {
11839                "query_block": {
11840                  "select_id": 2,
11841                  "having_condition": "c > 200",
11842                  "filesort": {
11843                    "sort_key": "t1.a, t1.b",
11844                    "temporary_table": {
11845                      "table": {
11846                        "table_name": "t1",
11847                        "access_type": "ALL",
11848                        "rows": 18,
11849                        "filtered": 100,
11850                        "attached_condition": "t1.a < 9 and t1.a < 5"
11851                      }
11852                    }
11853                  }
11854                }
11855              },
11856              {
11857                "query_block": {
11858                  "select_id": 3,
11859                  "operation": "EXCEPT",
11860                  "having_condition": "c < 300",
11861                  "filesort": {
11862                    "sort_key": "t1.a, t1.b",
11863                    "temporary_table": {
11864                      "table": {
11865                        "table_name": "t1",
11866                        "access_type": "ALL",
11867                        "rows": 18,
11868                        "filtered": 100,
11869                        "attached_condition": "t1.b > 10 and t1.a < 5"
11870                      }
11871                    }
11872                  }
11873                }
11874              }
11875            ]
11876          }
11877        }
11878      }
11879    }
11880  }
11881}
11882# using except in view definition
11883# conjunctive subformulas : pushing into WHERE
11884# pushing equalities
11885set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.a=t2.a) and (v1.a=6);
11886a	b	c	a	b	c
118876	20	315	6	20	211
118886	20	315	6	23	303
11889select * from v1,t2 where (v1.a=t2.a) and (v1.a=6);
11890a	b	c	a	b	c
118916	20	315	6	20	211
118926	20	315	6	23	303
11893explain select * from v1,t2 where (v1.a=t2.a) and (v1.a=6);
11894id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
118951	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
118961	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	18	Using where; Using join buffer (flat, BNL join)
118972	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
118983	EXCEPT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
11899NULL	EXCEPT RESULT	<except2,3>	ALL	NULL	NULL	NULL	NULL	NULL
11900explain format=json select * from v1,t2 where (v1.a=t2.a) and (v1.a=6);
11901EXPLAIN
11902{
11903  "query_block": {
11904    "select_id": 1,
11905    "table": {
11906      "table_name": "t2",
11907      "access_type": "ALL",
11908      "rows": 9,
11909      "filtered": 100,
11910      "attached_condition": "t2.a = 6"
11911    },
11912    "block-nl-join": {
11913      "table": {
11914        "table_name": "<derived2>",
11915        "access_type": "ALL",
11916        "rows": 18,
11917        "filtered": 100,
11918        "attached_condition": "v1.a = 6"
11919      },
11920      "buffer_type": "flat",
11921      "buffer_size": "173",
11922      "join_type": "BNL",
11923      "materialized": {
11924        "query_block": {
11925          "union_result": {
11926            "table_name": "<except2,3>",
11927            "access_type": "ALL",
11928            "query_specifications": [
11929              {
11930                "query_block": {
11931                  "select_id": 2,
11932                  "having_condition": "c > 200",
11933                  "filesort": {
11934                    "sort_key": "t1.b",
11935                    "temporary_table": {
11936                      "table": {
11937                        "table_name": "t1",
11938                        "access_type": "ALL",
11939                        "rows": 18,
11940                        "filtered": 100,
11941                        "attached_condition": "t1.a = 6"
11942                      }
11943                    }
11944                  }
11945                }
11946              },
11947              {
11948                "query_block": {
11949                  "select_id": 3,
11950                  "operation": "EXCEPT",
11951                  "having_condition": "c < 300",
11952                  "filesort": {
11953                    "sort_key": "t1.b",
11954                    "temporary_table": {
11955                      "table": {
11956                        "table_name": "t1",
11957                        "access_type": "ALL",
11958                        "rows": 18,
11959                        "filtered": 100,
11960                        "attached_condition": "t1.a = 6 and t1.b > 10"
11961                      }
11962                    }
11963                  }
11964                }
11965              }
11966            ]
11967          }
11968        }
11969      }
11970    }
11971  }
11972}
11973# using except in view definition
11974# conjunctive subformulas : pushing into WHERE using equalities
11975set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.a=t2.a) and (t2.a=6);
11976a	b	c	a	b	c
119776	20	315	6	20	211
119786	20	315	6	23	303
11979select * from v1,t2 where (v1.a=t2.a) and (t2.a=6);
11980a	b	c	a	b	c
119816	20	315	6	20	211
119826	20	315	6	23	303
11983explain select * from v1,t2 where (v1.a=t2.a) and (t2.a=6);
11984id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
119851	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
119861	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	18	Using where; Using join buffer (flat, BNL join)
119872	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
119883	EXCEPT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
11989NULL	EXCEPT RESULT	<except2,3>	ALL	NULL	NULL	NULL	NULL	NULL
11990explain format=json select * from v1,t2 where (v1.a=t2.a) and (t2.a=6);
11991EXPLAIN
11992{
11993  "query_block": {
11994    "select_id": 1,
11995    "table": {
11996      "table_name": "t2",
11997      "access_type": "ALL",
11998      "rows": 9,
11999      "filtered": 100,
12000      "attached_condition": "t2.a = 6"
12001    },
12002    "block-nl-join": {
12003      "table": {
12004        "table_name": "<derived2>",
12005        "access_type": "ALL",
12006        "rows": 18,
12007        "filtered": 100,
12008        "attached_condition": "v1.a = 6"
12009      },
12010      "buffer_type": "flat",
12011      "buffer_size": "173",
12012      "join_type": "BNL",
12013      "materialized": {
12014        "query_block": {
12015          "union_result": {
12016            "table_name": "<except2,3>",
12017            "access_type": "ALL",
12018            "query_specifications": [
12019              {
12020                "query_block": {
12021                  "select_id": 2,
12022                  "having_condition": "c > 200",
12023                  "filesort": {
12024                    "sort_key": "t1.b",
12025                    "temporary_table": {
12026                      "table": {
12027                        "table_name": "t1",
12028                        "access_type": "ALL",
12029                        "rows": 18,
12030                        "filtered": 100,
12031                        "attached_condition": "t1.a = 6"
12032                      }
12033                    }
12034                  }
12035                }
12036              },
12037              {
12038                "query_block": {
12039                  "select_id": 3,
12040                  "operation": "EXCEPT",
12041                  "having_condition": "c < 300",
12042                  "filesort": {
12043                    "sort_key": "t1.b",
12044                    "temporary_table": {
12045                      "table": {
12046                        "table_name": "t1",
12047                        "access_type": "ALL",
12048                        "rows": 18,
12049                        "filtered": 100,
12050                        "attached_condition": "t1.a = 6 and t1.b > 10"
12051                      }
12052                    }
12053                  }
12054                }
12055              }
12056            ]
12057          }
12058        }
12059      }
12060    }
12061  }
12062}
12063# using except in view definition
12064# conjunctive subformulas : pushing into HAVING
12065set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.a=t2.a) and (v1.c>500);
12066a	b	c	a	b	c
120671	33	988	1	16	909
120685	14	787	5	14	312
120695	14	787	5	33	207
120701	33	988	1	19	132
12071select * from v1,t2 where (v1.a=t2.a) and (v1.c>500);
12072a	b	c	a	b	c
120731	33	988	1	16	909
120745	14	787	5	14	312
120755	14	787	5	33	207
120761	33	988	1	19	132
12077explain select * from v1,t2 where (v1.a=t2.a) and (v1.c>500);
12078id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
120791	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
120801	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.a	2	Using where
120812	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
120823	EXCEPT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
12083NULL	EXCEPT RESULT	<except2,3>	ALL	NULL	NULL	NULL	NULL	NULL
12084explain format=json select * from v1,t2 where (v1.a=t2.a) and (v1.c>500);
12085EXPLAIN
12086{
12087  "query_block": {
12088    "select_id": 1,
12089    "table": {
12090      "table_name": "t2",
12091      "access_type": "ALL",
12092      "rows": 9,
12093      "filtered": 100,
12094      "attached_condition": "t2.a is not null"
12095    },
12096    "table": {
12097      "table_name": "<derived2>",
12098      "access_type": "ref",
12099      "possible_keys": ["key0"],
12100      "key": "key0",
12101      "key_length": "5",
12102      "used_key_parts": ["a"],
12103      "ref": ["test.t2.a"],
12104      "rows": 2,
12105      "filtered": 100,
12106      "attached_condition": "v1.c > 500",
12107      "materialized": {
12108        "query_block": {
12109          "union_result": {
12110            "table_name": "<except2,3>",
12111            "access_type": "ALL",
12112            "query_specifications": [
12113              {
12114                "query_block": {
12115                  "select_id": 2,
12116                  "having_condition": "c > 200 and c > 500",
12117                  "filesort": {
12118                    "sort_key": "t1.a, t1.b",
12119                    "temporary_table": {
12120                      "table": {
12121                        "table_name": "t1",
12122                        "access_type": "ALL",
12123                        "rows": 18,
12124                        "filtered": 100,
12125                        "attached_condition": "t1.a < 9"
12126                      }
12127                    }
12128                  }
12129                }
12130              },
12131              {
12132                "query_block": {
12133                  "select_id": 3,
12134                  "operation": "EXCEPT",
12135                  "having_condition": "c < 300 and c > 500",
12136                  "filesort": {
12137                    "sort_key": "t1.a, t1.b",
12138                    "temporary_table": {
12139                      "table": {
12140                        "table_name": "t1",
12141                        "access_type": "ALL",
12142                        "rows": 18,
12143                        "filtered": 100,
12144                        "attached_condition": "t1.b > 10"
12145                      }
12146                    }
12147                  }
12148                }
12149              }
12150            ]
12151          }
12152        }
12153      }
12154    }
12155  }
12156}
12157# using except in view definition
12158# conjunctive subformulas : pushing into WHERE
12159# conjunctive subformulas : pushing into HAVING
12160set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.a=t2.a) and (v1.a<5) and (v1.c>500);
12161a	b	c	a	b	c
121621	33	988	1	16	909
121631	33	988	1	19	132
12164select * from v1,t2 where (v1.a=t2.a) and (v1.a<5) and (v1.c>500);
12165a	b	c	a	b	c
121661	33	988	1	16	909
121671	33	988	1	19	132
12168explain select * from v1,t2 where (v1.a=t2.a) and (v1.a<5) and (v1.c>500);
12169id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
121701	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
121711	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.a	2	Using where
121722	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
121733	EXCEPT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
12174NULL	EXCEPT RESULT	<except2,3>	ALL	NULL	NULL	NULL	NULL	NULL
12175explain format=json select * from v1,t2 where (v1.a=t2.a) and (v1.a<5) and (v1.c>500);
12176EXPLAIN
12177{
12178  "query_block": {
12179    "select_id": 1,
12180    "table": {
12181      "table_name": "t2",
12182      "access_type": "ALL",
12183      "rows": 9,
12184      "filtered": 100,
12185      "attached_condition": "t2.a < 5 and t2.a is not null"
12186    },
12187    "table": {
12188      "table_name": "<derived2>",
12189      "access_type": "ref",
12190      "possible_keys": ["key0"],
12191      "key": "key0",
12192      "key_length": "5",
12193      "used_key_parts": ["a"],
12194      "ref": ["test.t2.a"],
12195      "rows": 2,
12196      "filtered": 100,
12197      "attached_condition": "v1.c > 500",
12198      "materialized": {
12199        "query_block": {
12200          "union_result": {
12201            "table_name": "<except2,3>",
12202            "access_type": "ALL",
12203            "query_specifications": [
12204              {
12205                "query_block": {
12206                  "select_id": 2,
12207                  "having_condition": "c > 200 and c > 500",
12208                  "filesort": {
12209                    "sort_key": "t1.a, t1.b",
12210                    "temporary_table": {
12211                      "table": {
12212                        "table_name": "t1",
12213                        "access_type": "ALL",
12214                        "rows": 18,
12215                        "filtered": 100,
12216                        "attached_condition": "t1.a < 9 and t1.a < 5"
12217                      }
12218                    }
12219                  }
12220                }
12221              },
12222              {
12223                "query_block": {
12224                  "select_id": 3,
12225                  "operation": "EXCEPT",
12226                  "having_condition": "c < 300 and c > 500",
12227                  "filesort": {
12228                    "sort_key": "t1.a, t1.b",
12229                    "temporary_table": {
12230                      "table": {
12231                        "table_name": "t1",
12232                        "access_type": "ALL",
12233                        "rows": 18,
12234                        "filtered": 100,
12235                        "attached_condition": "t1.b > 10 and t1.a < 5"
12236                      }
12237                    }
12238                  }
12239                }
12240              }
12241            ]
12242          }
12243        }
12244      }
12245    }
12246  }
12247}
12248# using except in view definition
12249# extracted or formula : pushing into WHERE
12250set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.a=t2.a) and ((v1.b>27) or (v1.b<19));
12251a	b	c	a	b	c
122521	33	988	1	16	909
122535	14	787	5	14	312
122545	14	787	5	33	207
122551	33	988	1	19	132
12256select * from v1,t2 where (v1.a=t2.a) and ((v1.b>27) or (v1.b<19));
12257a	b	c	a	b	c
122581	33	988	1	16	909
122595	14	787	5	14	312
122605	14	787	5	33	207
122611	33	988	1	19	132
12262explain select * from v1,t2 where (v1.a=t2.a) and ((v1.b>27) or (v1.b<19));
12263id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
122641	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
122651	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.a	2	Using where
122662	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
122673	EXCEPT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
12268NULL	EXCEPT RESULT	<except2,3>	ALL	NULL	NULL	NULL	NULL	NULL
12269explain format=json select * from v1,t2 where (v1.a=t2.a) and ((v1.b>27) or (v1.b<19));
12270EXPLAIN
12271{
12272  "query_block": {
12273    "select_id": 1,
12274    "table": {
12275      "table_name": "t2",
12276      "access_type": "ALL",
12277      "rows": 9,
12278      "filtered": 100,
12279      "attached_condition": "t2.a is not null"
12280    },
12281    "table": {
12282      "table_name": "<derived2>",
12283      "access_type": "ref",
12284      "possible_keys": ["key0"],
12285      "key": "key0",
12286      "key_length": "5",
12287      "used_key_parts": ["a"],
12288      "ref": ["test.t2.a"],
12289      "rows": 2,
12290      "filtered": 100,
12291      "attached_condition": "v1.b > 27 or v1.b < 19",
12292      "materialized": {
12293        "query_block": {
12294          "union_result": {
12295            "table_name": "<except2,3>",
12296            "access_type": "ALL",
12297            "query_specifications": [
12298              {
12299                "query_block": {
12300                  "select_id": 2,
12301                  "having_condition": "c > 200",
12302                  "filesort": {
12303                    "sort_key": "t1.a, t1.b",
12304                    "temporary_table": {
12305                      "table": {
12306                        "table_name": "t1",
12307                        "access_type": "ALL",
12308                        "rows": 18,
12309                        "filtered": 100,
12310                        "attached_condition": "t1.a < 9 and (t1.b > 27 or t1.b < 19)"
12311                      }
12312                    }
12313                  }
12314                }
12315              },
12316              {
12317                "query_block": {
12318                  "select_id": 3,
12319                  "operation": "EXCEPT",
12320                  "having_condition": "c < 300",
12321                  "filesort": {
12322                    "sort_key": "t1.a, t1.b",
12323                    "temporary_table": {
12324                      "table": {
12325                        "table_name": "t1",
12326                        "access_type": "ALL",
12327                        "rows": 18,
12328                        "filtered": 100,
12329                        "attached_condition": "t1.b > 10 and (t1.b > 27 or t1.b < 19)"
12330                      }
12331                    }
12332                  }
12333                }
12334              }
12335            ]
12336          }
12337        }
12338      }
12339    }
12340  }
12341}
12342# using except in view definition
12343# extracted or formula : pushing into HAVING
12344set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where
12345(v1.a=t2.a) and ((v1.c<400) or (v1.c>800));
12346a	b	c	a	b	c
123471	33	988	1	16	909
123486	20	315	6	20	211
123491	33	988	1	19	132
123506	20	315	6	23	303
12351select * from v1,t2 where
12352(v1.a=t2.a) and ((v1.c<400) or (v1.c>800));
12353a	b	c	a	b	c
123541	33	988	1	16	909
123556	20	315	6	20	211
123561	33	988	1	19	132
123576	20	315	6	23	303
12358explain select * from v1,t2 where
12359(v1.a=t2.a) and ((v1.c<400) or (v1.c>800));
12360id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
123611	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
123621	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.a	2	Using where
123632	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
123643	EXCEPT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
12365NULL	EXCEPT RESULT	<except2,3>	ALL	NULL	NULL	NULL	NULL	NULL
12366explain format=json select * from v1,t2 where
12367(v1.a=t2.a) and ((v1.c<400) or (v1.c>800));
12368EXPLAIN
12369{
12370  "query_block": {
12371    "select_id": 1,
12372    "table": {
12373      "table_name": "t2",
12374      "access_type": "ALL",
12375      "rows": 9,
12376      "filtered": 100,
12377      "attached_condition": "t2.a is not null"
12378    },
12379    "table": {
12380      "table_name": "<derived2>",
12381      "access_type": "ref",
12382      "possible_keys": ["key0"],
12383      "key": "key0",
12384      "key_length": "5",
12385      "used_key_parts": ["a"],
12386      "ref": ["test.t2.a"],
12387      "rows": 2,
12388      "filtered": 100,
12389      "attached_condition": "v1.c < 400 or v1.c > 800",
12390      "materialized": {
12391        "query_block": {
12392          "union_result": {
12393            "table_name": "<except2,3>",
12394            "access_type": "ALL",
12395            "query_specifications": [
12396              {
12397                "query_block": {
12398                  "select_id": 2,
12399                  "having_condition": "c > 200 and (c < 400 or c > 800)",
12400                  "filesort": {
12401                    "sort_key": "t1.a, t1.b",
12402                    "temporary_table": {
12403                      "table": {
12404                        "table_name": "t1",
12405                        "access_type": "ALL",
12406                        "rows": 18,
12407                        "filtered": 100,
12408                        "attached_condition": "t1.a < 9"
12409                      }
12410                    }
12411                  }
12412                }
12413              },
12414              {
12415                "query_block": {
12416                  "select_id": 3,
12417                  "operation": "EXCEPT",
12418                  "having_condition": "c < 300 and (c < 400 or c > 800)",
12419                  "filesort": {
12420                    "sort_key": "t1.a, t1.b",
12421                    "temporary_table": {
12422                      "table": {
12423                        "table_name": "t1",
12424                        "access_type": "ALL",
12425                        "rows": 18,
12426                        "filtered": 100,
12427                        "attached_condition": "t1.b > 10"
12428                      }
12429                    }
12430                  }
12431                }
12432              }
12433            ]
12434          }
12435        }
12436      }
12437    }
12438  }
12439}
12440# using except in view definition
12441# extracted or formula : pushing into WHERE
12442# extracted or formula : pushing into HAVING using equalities
12443# pushing equalities
12444set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where
12445(v1.c=t2.c) and
12446((v1.a>1) and (t2.c<500)) or
12447((v1.a=1) and (v1.c>500));
12448a	b	c	a	b	c
124491	33	988	2	3	207
124501	33	988	1	16	909
124511	33	988	5	14	312
124521	33	988	5	33	207
124531	33	988	6	20	211
124541	33	988	1	19	132
124551	33	988	8	33	117
124561	33	988	3	21	231
124571	33	988	6	23	303
12458select * from v1,t2 where
12459(v1.c=t2.c) and
12460((v1.a>1) and (t2.c<500)) or
12461((v1.a=1) and (v1.c>500));
12462a	b	c	a	b	c
124631	33	988	2	3	207
124641	33	988	1	16	909
124651	33	988	5	14	312
124661	33	988	5	33	207
124671	33	988	6	20	211
124681	33	988	1	19	132
124691	33	988	8	33	117
124701	33	988	3	21	231
124711	33	988	6	23	303
12472explain select * from v1,t2 where
12473(v1.c=t2.c) and
12474((v1.a>1) and (t2.c<500)) or
12475((v1.a=1) and (v1.c>500));
12476id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
124771	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9
124781	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	18	Using where; Using join buffer (flat, BNL join)
124792	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
124803	EXCEPT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
12481NULL	EXCEPT RESULT	<except2,3>	ALL	NULL	NULL	NULL	NULL	NULL
12482explain format=json select * from v1,t2 where
12483(v1.c=t2.c) and
12484((v1.a>1) and (t2.c<500)) or
12485((v1.a=1) and (v1.c>500));
12486EXPLAIN
12487{
12488  "query_block": {
12489    "select_id": 1,
12490    "table": {
12491      "table_name": "t2",
12492      "access_type": "ALL",
12493      "rows": 9,
12494      "filtered": 100
12495    },
12496    "block-nl-join": {
12497      "table": {
12498        "table_name": "<derived2>",
12499        "access_type": "ALL",
12500        "rows": 18,
12501        "filtered": 100,
12502        "attached_condition": "v1.a > 1 or v1.a = 1 and v1.c > 500"
12503      },
12504      "buffer_type": "flat",
12505      "buffer_size": "173",
12506      "join_type": "BNL",
12507      "attached_condition": "v1.c = t2.c and v1.a > 1 and t2.c < 500 or v1.a = 1 and v1.c > 500",
12508      "materialized": {
12509        "query_block": {
12510          "union_result": {
12511            "table_name": "<except2,3>",
12512            "access_type": "ALL",
12513            "query_specifications": [
12514              {
12515                "query_block": {
12516                  "select_id": 2,
12517                  "having_condition": "c > 200 and (t1.a > 1 and c < 500 or t1.a = 1 and c > 500)",
12518                  "filesort": {
12519                    "sort_key": "t1.a, t1.b",
12520                    "temporary_table": {
12521                      "table": {
12522                        "table_name": "t1",
12523                        "access_type": "ALL",
12524                        "rows": 18,
12525                        "filtered": 100,
12526                        "attached_condition": "t1.a < 9 and (t1.a > 1 or t1.a = 1)"
12527                      }
12528                    }
12529                  }
12530                }
12531              },
12532              {
12533                "query_block": {
12534                  "select_id": 3,
12535                  "operation": "EXCEPT",
12536                  "having_condition": "c < 300 and (t1.a > 1 and c < 500 or t1.a = 1 and c > 500)",
12537                  "filesort": {
12538                    "sort_key": "t1.a, t1.b",
12539                    "temporary_table": {
12540                      "table": {
12541                        "table_name": "t1",
12542                        "access_type": "ALL",
12543                        "rows": 18,
12544                        "filtered": 100,
12545                        "attached_condition": "t1.b > 10 and (t1.a > 1 or t1.a = 1)"
12546                      }
12547                    }
12548                  }
12549                }
12550              }
12551            ]
12552          }
12553        }
12554      }
12555    }
12556  }
12557}
12558# using except in view definition
12559# prepare of a query
12560# conjunctive subformulas : pushing into WHERE
12561# conjunctive subformulas : pushing into HAVING
12562prepare stmt from "select * from v1,t2
12563                   where (v1.a=t2.a) and (v1.a<5) and (v1.c>500);";
12564execute stmt;
12565a	b	c	a	b	c
125661	33	988	1	16	909
125671	33	988	1	19	132
12568execute stmt;
12569a	b	c	a	b	c
125701	33	988	1	16	909
125711	33	988	1	19	132
12572deallocate prepare stmt;
12573# using except in view definition
12574# extracted or formula : pushing into WHERE using equalities
12575# extracted or formula : pushing into HAVING
12576# pushing equalities
12577set statement optimizer_switch='condition_pushdown_for_derived=off' for select *
12578from t2,
12579(select a, b, max(c) as c from t1
12580where t1.a<9 group by a,b having c > 200
12581except
12582select a, b, max(c) as c from t1
12583where t1.b>10 group by a,b having c < 300) as d1
12584where
12585(d1.b=t2.b) and
12586(((t2.b>13) and (t2.c=988)) or
12587((d1.a>4) and (d1.c>500)));
12588a	b	c	a	b	c
125895	14	312	5	14	787
12590select *
12591from t2,
12592(select a, b, max(c) as c from t1
12593where t1.a<9 group by a,b having c > 200
12594except
12595select a, b, max(c) as c from t1
12596where t1.b>10 group by a,b having c < 300) as d1
12597where
12598(d1.b=t2.b) and
12599(((t2.b>13) and (t2.c=988)) or
12600((d1.a>4) and (d1.c>500)));
12601a	b	c	a	b	c
126025	14	312	5	14	787
12603explain select *
12604from t2,
12605(select a, b, max(c) as c from t1
12606where t1.a<9 group by a,b having c > 200
12607except
12608select a, b, max(c) as c from t1
12609where t1.b>10 group by a,b having c < 300) as d1
12610where
12611(d1.b=t2.b) and
12612(((t2.b>13) and (t2.c=988)) or
12613((d1.a>4) and (d1.c>500)));
12614id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
126151	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
126161	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.b	2	Using where
126172	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
126183	EXCEPT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
12619NULL	EXCEPT RESULT	<except2,3>	ALL	NULL	NULL	NULL	NULL	NULL
12620explain format=json select *
12621from t2,
12622(select a, b, max(c) as c from t1
12623where t1.a<9 group by a,b having c > 200
12624except
12625select a, b, max(c) as c from t1
12626where t1.b>10 group by a,b having c < 300) as d1
12627where
12628(d1.b=t2.b) and
12629(((t2.b>13) and (t2.c=988)) or
12630((d1.a>4) and (d1.c>500)));
12631EXPLAIN
12632{
12633  "query_block": {
12634    "select_id": 1,
12635    "table": {
12636      "table_name": "t2",
12637      "access_type": "ALL",
12638      "rows": 9,
12639      "filtered": 100,
12640      "attached_condition": "t2.b is not null"
12641    },
12642    "table": {
12643      "table_name": "<derived2>",
12644      "access_type": "ref",
12645      "possible_keys": ["key0"],
12646      "key": "key0",
12647      "key_length": "5",
12648      "used_key_parts": ["b"],
12649      "ref": ["test.t2.b"],
12650      "rows": 2,
12651      "filtered": 100,
12652      "attached_condition": "t2.c = 988 and t2.b > 13 or d1.a > 4 and d1.c > 500",
12653      "materialized": {
12654        "query_block": {
12655          "union_result": {
12656            "table_name": "<except2,3>",
12657            "access_type": "ALL",
12658            "query_specifications": [
12659              {
12660                "query_block": {
12661                  "select_id": 2,
12662                  "having_condition": "c > 200 and (t1.b > 13 or t1.a > 4 and c > 500)",
12663                  "filesort": {
12664                    "sort_key": "t1.a, t1.b",
12665                    "temporary_table": {
12666                      "table": {
12667                        "table_name": "t1",
12668                        "access_type": "ALL",
12669                        "rows": 18,
12670                        "filtered": 100,
12671                        "attached_condition": "t1.a < 9 and (t1.b > 13 or t1.a > 4)"
12672                      }
12673                    }
12674                  }
12675                }
12676              },
12677              {
12678                "query_block": {
12679                  "select_id": 3,
12680                  "operation": "EXCEPT",
12681                  "having_condition": "c < 300 and (t1.b > 13 or t1.a > 4 and c > 500)",
12682                  "filesort": {
12683                    "sort_key": "t1.a, t1.b",
12684                    "temporary_table": {
12685                      "table": {
12686                        "table_name": "t1",
12687                        "access_type": "ALL",
12688                        "rows": 18,
12689                        "filtered": 100,
12690                        "attached_condition": "t1.b > 10 and (t1.b > 13 or t1.a > 4)"
12691                      }
12692                    }
12693                  }
12694                }
12695              }
12696            ]
12697          }
12698        }
12699      }
12700    }
12701  }
12702}
12703drop view v1;
12704# using union and intersect in view definition
12705# conjunctive subformulas : pushing into WHERE and HAVING
12706create view v1 as
12707select a, b, min(c) as c from t1
12708where t1.a<9 group by a,b having c > 200
12709union
12710select a, b, max(c) as c from t1
12711where t1.b>10 group by a,b having c < 300
12712intersect
12713select a, b, max(c) as c from t1
12714where t1.a>3 group by a,b having c < 530;
12715set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.a=t2.a) and (v1.a>5) and (v1.c>200);
12716a	b	c	a	b	c
127176	20	309	6	20	211
127186	20	309	6	23	303
12719select * from v1,t2 where (v1.a=t2.a) and (v1.a>5) and (v1.c>200);
12720a	b	c	a	b	c
127216	20	309	6	20	211
127226	20	309	6	23	303
12723explain select * from v1,t2 where (v1.a=t2.a) and (v1.a>5) and (v1.c>200);
12724id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
127251	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
127261	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.a	3	Using where
127272	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
127283	UNION	<derived4>	ALL	NULL	NULL	NULL	NULL	18	Using where
127294	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
127305	INTERSECT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
12731NULL	INTERSECT RESULT	<intersect4,5>	ALL	NULL	NULL	NULL	NULL	NULL
12732NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL
12733explain format=json select * from v1,t2 where (v1.a=t2.a) and (v1.a>5) and (v1.c>200);
12734EXPLAIN
12735{
12736  "query_block": {
12737    "select_id": 1,
12738    "table": {
12739      "table_name": "t2",
12740      "access_type": "ALL",
12741      "rows": 9,
12742      "filtered": 100,
12743      "attached_condition": "t2.a > 5 and t2.a is not null"
12744    },
12745    "table": {
12746      "table_name": "<derived2>",
12747      "access_type": "ref",
12748      "possible_keys": ["key0"],
12749      "key": "key0",
12750      "key_length": "5",
12751      "used_key_parts": ["a"],
12752      "ref": ["test.t2.a"],
12753      "rows": 3,
12754      "filtered": 100,
12755      "attached_condition": "v1.c > 200",
12756      "materialized": {
12757        "query_block": {
12758          "union_result": {
12759            "table_name": "<union2,3>",
12760            "access_type": "ALL",
12761            "query_specifications": [
12762              {
12763                "query_block": {
12764                  "select_id": 2,
12765                  "having_condition": "c > 200 and c > 200",
12766                  "filesort": {
12767                    "sort_key": "t1.a, t1.b",
12768                    "temporary_table": {
12769                      "table": {
12770                        "table_name": "t1",
12771                        "access_type": "ALL",
12772                        "rows": 18,
12773                        "filtered": 100,
12774                        "attached_condition": "t1.a < 9 and t1.a > 5"
12775                      }
12776                    }
12777                  }
12778                }
12779              },
12780              {
12781                "query_block": {
12782                  "select_id": 3,
12783                  "operation": "UNION",
12784                  "table": {
12785                    "table_name": "<derived4>",
12786                    "access_type": "ALL",
12787                    "rows": 18,
12788                    "filtered": 100,
12789                    "attached_condition": "__5.a > 5 and __5.c > 200",
12790                    "materialized": {
12791                      "query_block": {
12792                        "union_result": {
12793                          "table_name": "<intersect4,5>",
12794                          "access_type": "ALL",
12795                          "query_specifications": [
12796                            {
12797                              "query_block": {
12798                                "select_id": 4,
12799                                "having_condition": "c < 300 and c > 200",
12800                                "filesort": {
12801                                  "sort_key": "t1.a, t1.b",
12802                                  "temporary_table": {
12803                                    "table": {
12804                                      "table_name": "t1",
12805                                      "access_type": "ALL",
12806                                      "rows": 18,
12807                                      "filtered": 100,
12808                                      "attached_condition": "t1.b > 10 and t1.a > 5"
12809                                    }
12810                                  }
12811                                }
12812                              }
12813                            },
12814                            {
12815                              "query_block": {
12816                                "select_id": 5,
12817                                "operation": "INTERSECT",
12818                                "having_condition": "c < 530 and c > 200",
12819                                "filesort": {
12820                                  "sort_key": "t1.a, t1.b",
12821                                  "temporary_table": {
12822                                    "table": {
12823                                      "table_name": "t1",
12824                                      "access_type": "ALL",
12825                                      "rows": 18,
12826                                      "filtered": 100,
12827                                      "attached_condition": "t1.a > 3 and t1.a > 5"
12828                                    }
12829                                  }
12830                                }
12831                              }
12832                            }
12833                          ]
12834                        }
12835                      }
12836                    }
12837                  }
12838                }
12839              }
12840            ]
12841          }
12842        }
12843      }
12844    }
12845  }
12846}
12847drop view v1;
12848# using union and intersect in view definition
12849# conjunctive subformulas : pushing into WHERE and HAVING
12850create view v1 as
12851select a, b, min(c) as c from t1
12852where t1.a<9 group by a,b having c > 200
12853intersect
12854select a, b, max(c) as c from t1
12855where t1.a>3 group by a,b having c < 500
12856union
12857select a, b, max(c) as c from t1
12858where t1.b>10 group by a,b having c < 300;
12859set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.a=t2.a) and (v1.a>4) and (v1.c<200);
12860a	b	c	a	b	c
128615	27	132	5	14	312
128625	27	132	5	33	207
128638	33	123	8	33	117
12864select * from v1,t2 where (v1.a=t2.a) and (v1.a>4) and (v1.c<200);
12865a	b	c	a	b	c
128665	27	132	5	14	312
128675	27	132	5	33	207
128688	33	123	8	33	117
12869explain select * from v1,t2 where (v1.a=t2.a) and (v1.a>4) and (v1.c<200);
12870id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
128711	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
128721	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.a	3	Using where
128732	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
128743	INTERSECT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
128754	UNION	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
12876NULL	UNIT RESULT	<unit2,3,4>	ALL	NULL	NULL	NULL	NULL	NULL
12877explain format=json select * from v1,t2 where (v1.a=t2.a) and (v1.a>4) and (v1.c<200);
12878EXPLAIN
12879{
12880  "query_block": {
12881    "select_id": 1,
12882    "table": {
12883      "table_name": "t2",
12884      "access_type": "ALL",
12885      "rows": 9,
12886      "filtered": 100,
12887      "attached_condition": "t2.a > 4 and t2.a is not null"
12888    },
12889    "table": {
12890      "table_name": "<derived2>",
12891      "access_type": "ref",
12892      "possible_keys": ["key0"],
12893      "key": "key0",
12894      "key_length": "5",
12895      "used_key_parts": ["a"],
12896      "ref": ["test.t2.a"],
12897      "rows": 3,
12898      "filtered": 100,
12899      "attached_condition": "v1.c < 200",
12900      "materialized": {
12901        "query_block": {
12902          "union_result": {
12903            "table_name": "<unit2,3,4>",
12904            "access_type": "ALL",
12905            "query_specifications": [
12906              {
12907                "query_block": {
12908                  "select_id": 2,
12909                  "having_condition": "c > 200 and c < 200",
12910                  "filesort": {
12911                    "sort_key": "t1.a, t1.b",
12912                    "temporary_table": {
12913                      "table": {
12914                        "table_name": "t1",
12915                        "access_type": "ALL",
12916                        "rows": 18,
12917                        "filtered": 100,
12918                        "attached_condition": "t1.a < 9 and t1.a > 4"
12919                      }
12920                    }
12921                  }
12922                }
12923              },
12924              {
12925                "query_block": {
12926                  "select_id": 3,
12927                  "operation": "INTERSECT",
12928                  "having_condition": "c < 500 and c < 200",
12929                  "filesort": {
12930                    "sort_key": "t1.a, t1.b",
12931                    "temporary_table": {
12932                      "table": {
12933                        "table_name": "t1",
12934                        "access_type": "ALL",
12935                        "rows": 18,
12936                        "filtered": 100,
12937                        "attached_condition": "t1.a > 3 and t1.a > 4"
12938                      }
12939                    }
12940                  }
12941                }
12942              },
12943              {
12944                "query_block": {
12945                  "select_id": 4,
12946                  "operation": "UNION",
12947                  "having_condition": "c < 300 and c < 200",
12948                  "filesort": {
12949                    "sort_key": "t1.a, t1.b",
12950                    "temporary_table": {
12951                      "table": {
12952                        "table_name": "t1",
12953                        "access_type": "ALL",
12954                        "rows": 18,
12955                        "filtered": 100,
12956                        "attached_condition": "t1.b > 10 and t1.a > 4"
12957                      }
12958                    }
12959                  }
12960                }
12961              }
12962            ]
12963          }
12964        }
12965      }
12966    }
12967  }
12968}
12969drop view v1;
12970# using union and except in view definition
12971# conjunctive subformulas : pushing into WHERE and HAVING
12972create view v1 as
12973select a, b, min(c) as c from t1
12974where t1.a<9 group by a,b having c > 200
12975union
12976select a, b, max(c) as c from t1
12977where t1.b>10 group by a,b having c < 300
12978except
12979select a, b, max(c) as c from t1
12980where t1.a>3 group by a,b having c < 530;
12981set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.a=t2.a) and (v1.a>5) and (v1.c>200);
12982a	b	c	a	b	c
129836	20	309	6	20	211
129846	20	309	6	23	303
12985select * from v1,t2 where (v1.a=t2.a) and (v1.a>5) and (v1.c>200);
12986a	b	c	a	b	c
129876	20	309	6	20	211
129886	20	309	6	23	303
12989explain select * from v1,t2 where (v1.a=t2.a) and (v1.a>5) and (v1.c>200);
12990id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
129911	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
129921	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.a	3	Using where
129932	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
129943	UNION	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
129954	EXCEPT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
12996NULL	UNIT RESULT	<unit2,3,4>	ALL	NULL	NULL	NULL	NULL	NULL
12997explain format=json select * from v1,t2 where (v1.a=t2.a) and (v1.a>5) and (v1.c>200);
12998EXPLAIN
12999{
13000  "query_block": {
13001    "select_id": 1,
13002    "table": {
13003      "table_name": "t2",
13004      "access_type": "ALL",
13005      "rows": 9,
13006      "filtered": 100,
13007      "attached_condition": "t2.a > 5 and t2.a is not null"
13008    },
13009    "table": {
13010      "table_name": "<derived2>",
13011      "access_type": "ref",
13012      "possible_keys": ["key0"],
13013      "key": "key0",
13014      "key_length": "5",
13015      "used_key_parts": ["a"],
13016      "ref": ["test.t2.a"],
13017      "rows": 3,
13018      "filtered": 100,
13019      "attached_condition": "v1.c > 200",
13020      "materialized": {
13021        "query_block": {
13022          "union_result": {
13023            "table_name": "<unit2,3,4>",
13024            "access_type": "ALL",
13025            "query_specifications": [
13026              {
13027                "query_block": {
13028                  "select_id": 2,
13029                  "having_condition": "c > 200 and c > 200",
13030                  "filesort": {
13031                    "sort_key": "t1.a, t1.b",
13032                    "temporary_table": {
13033                      "table": {
13034                        "table_name": "t1",
13035                        "access_type": "ALL",
13036                        "rows": 18,
13037                        "filtered": 100,
13038                        "attached_condition": "t1.a < 9 and t1.a > 5"
13039                      }
13040                    }
13041                  }
13042                }
13043              },
13044              {
13045                "query_block": {
13046                  "select_id": 3,
13047                  "operation": "UNION",
13048                  "having_condition": "c < 300 and c > 200",
13049                  "filesort": {
13050                    "sort_key": "t1.a, t1.b",
13051                    "temporary_table": {
13052                      "table": {
13053                        "table_name": "t1",
13054                        "access_type": "ALL",
13055                        "rows": 18,
13056                        "filtered": 100,
13057                        "attached_condition": "t1.b > 10 and t1.a > 5"
13058                      }
13059                    }
13060                  }
13061                }
13062              },
13063              {
13064                "query_block": {
13065                  "select_id": 4,
13066                  "operation": "EXCEPT",
13067                  "having_condition": "c < 530 and c > 200",
13068                  "filesort": {
13069                    "sort_key": "t1.a, t1.b",
13070                    "temporary_table": {
13071                      "table": {
13072                        "table_name": "t1",
13073                        "access_type": "ALL",
13074                        "rows": 18,
13075                        "filtered": 100,
13076                        "attached_condition": "t1.a > 3 and t1.a > 5"
13077                      }
13078                    }
13079                  }
13080                }
13081              }
13082            ]
13083          }
13084        }
13085      }
13086    }
13087  }
13088}
13089drop view v1;
13090# using union and except in view definition
13091# conjunctive subformulas : pushing into WHERE and HAVING
13092create view v1 as
13093select a, b, min(c) as c from t1
13094where t1.a<9 group by a,b having c > 200
13095except
13096select a, b, max(c) as c from t1
13097where t1.a>3 group by a,b having c < 500
13098union
13099select a, b, max(c) as c from t1
13100where t1.b>10 group by a,b having c < 300;
13101set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.a=t2.a) and (v1.a>4) and (v1.c<200);
13102a	b	c	a	b	c
131035	27	132	5	14	312
131045	27	132	5	33	207
131058	33	123	8	33	117
13106select * from v1,t2 where (v1.a=t2.a) and (v1.a>4) and (v1.c<200);
13107a	b	c	a	b	c
131085	27	132	5	14	312
131095	27	132	5	33	207
131108	33	123	8	33	117
13111explain select * from v1,t2 where (v1.a=t2.a) and (v1.a>4) and (v1.c<200);
13112id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
131131	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
131141	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.a	3	Using where
131152	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
131163	EXCEPT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
131174	UNION	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
13118NULL	UNIT RESULT	<unit2,3,4>	ALL	NULL	NULL	NULL	NULL	NULL
13119explain format=json select * from v1,t2 where (v1.a=t2.a) and (v1.a>4) and (v1.c<200);
13120EXPLAIN
13121{
13122  "query_block": {
13123    "select_id": 1,
13124    "table": {
13125      "table_name": "t2",
13126      "access_type": "ALL",
13127      "rows": 9,
13128      "filtered": 100,
13129      "attached_condition": "t2.a > 4 and t2.a is not null"
13130    },
13131    "table": {
13132      "table_name": "<derived2>",
13133      "access_type": "ref",
13134      "possible_keys": ["key0"],
13135      "key": "key0",
13136      "key_length": "5",
13137      "used_key_parts": ["a"],
13138      "ref": ["test.t2.a"],
13139      "rows": 3,
13140      "filtered": 100,
13141      "attached_condition": "v1.c < 200",
13142      "materialized": {
13143        "query_block": {
13144          "union_result": {
13145            "table_name": "<unit2,3,4>",
13146            "access_type": "ALL",
13147            "query_specifications": [
13148              {
13149                "query_block": {
13150                  "select_id": 2,
13151                  "having_condition": "c > 200 and c < 200",
13152                  "filesort": {
13153                    "sort_key": "t1.a, t1.b",
13154                    "temporary_table": {
13155                      "table": {
13156                        "table_name": "t1",
13157                        "access_type": "ALL",
13158                        "rows": 18,
13159                        "filtered": 100,
13160                        "attached_condition": "t1.a < 9 and t1.a > 4"
13161                      }
13162                    }
13163                  }
13164                }
13165              },
13166              {
13167                "query_block": {
13168                  "select_id": 3,
13169                  "operation": "EXCEPT",
13170                  "having_condition": "c < 500 and c < 200",
13171                  "filesort": {
13172                    "sort_key": "t1.a, t1.b",
13173                    "temporary_table": {
13174                      "table": {
13175                        "table_name": "t1",
13176                        "access_type": "ALL",
13177                        "rows": 18,
13178                        "filtered": 100,
13179                        "attached_condition": "t1.a > 3 and t1.a > 4"
13180                      }
13181                    }
13182                  }
13183                }
13184              },
13185              {
13186                "query_block": {
13187                  "select_id": 4,
13188                  "operation": "UNION",
13189                  "having_condition": "c < 300 and c < 200",
13190                  "filesort": {
13191                    "sort_key": "t1.a, t1.b",
13192                    "temporary_table": {
13193                      "table": {
13194                        "table_name": "t1",
13195                        "access_type": "ALL",
13196                        "rows": 18,
13197                        "filtered": 100,
13198                        "attached_condition": "t1.b > 10 and t1.a > 4"
13199                      }
13200                    }
13201                  }
13202                }
13203              }
13204            ]
13205          }
13206        }
13207      }
13208    }
13209  }
13210}
13211drop view v1;
13212# using except and intersect in view definition
13213# conjunctive subformulas : pushing into WHERE and HAVING
13214create view v1 as
13215select a, b, max(c) as c from t1
13216where t1.b>10 group by a,b having c < 300
13217intersect
13218select a, b, max(c) as c from t1
13219where t1.a<7 group by a,b having c < 500
13220except
13221select a, b, max(c) as c from t1
13222where t1.a<9 group by a,b having c > 150;
13223set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.a=t2.a) and (v1.a>4) and (v1.c<150);
13224a	b	c	a	b	c
132255	27	132	5	14	312
132265	27	132	5	33	207
13227select * from v1,t2 where (v1.a=t2.a) and (v1.a>4) and (v1.c<150);
13228a	b	c	a	b	c
132295	27	132	5	14	312
132305	27	132	5	33	207
13231explain select * from v1,t2 where (v1.a=t2.a) and (v1.a>4) and (v1.c<150);
13232id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
132331	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
132341	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.a	2	Using where
132352	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
132363	INTERSECT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
132374	EXCEPT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
13238NULL	UNIT RESULT	<unit2,3,4>	ALL	NULL	NULL	NULL	NULL	NULL
13239explain format=json select * from v1,t2 where (v1.a=t2.a) and (v1.a>4) and (v1.c<150);
13240EXPLAIN
13241{
13242  "query_block": {
13243    "select_id": 1,
13244    "table": {
13245      "table_name": "t2",
13246      "access_type": "ALL",
13247      "rows": 9,
13248      "filtered": 100,
13249      "attached_condition": "t2.a > 4 and t2.a is not null"
13250    },
13251    "table": {
13252      "table_name": "<derived2>",
13253      "access_type": "ref",
13254      "possible_keys": ["key0"],
13255      "key": "key0",
13256      "key_length": "5",
13257      "used_key_parts": ["a"],
13258      "ref": ["test.t2.a"],
13259      "rows": 2,
13260      "filtered": 100,
13261      "attached_condition": "v1.c < 150",
13262      "materialized": {
13263        "query_block": {
13264          "union_result": {
13265            "table_name": "<unit2,3,4>",
13266            "access_type": "ALL",
13267            "query_specifications": [
13268              {
13269                "query_block": {
13270                  "select_id": 2,
13271                  "having_condition": "c < 300 and c < 150",
13272                  "filesort": {
13273                    "sort_key": "t1.a, t1.b",
13274                    "temporary_table": {
13275                      "table": {
13276                        "table_name": "t1",
13277                        "access_type": "ALL",
13278                        "rows": 18,
13279                        "filtered": 100,
13280                        "attached_condition": "t1.b > 10 and t1.a > 4"
13281                      }
13282                    }
13283                  }
13284                }
13285              },
13286              {
13287                "query_block": {
13288                  "select_id": 3,
13289                  "operation": "INTERSECT",
13290                  "having_condition": "c < 500 and c < 150",
13291                  "filesort": {
13292                    "sort_key": "t1.a, t1.b",
13293                    "temporary_table": {
13294                      "table": {
13295                        "table_name": "t1",
13296                        "access_type": "ALL",
13297                        "rows": 18,
13298                        "filtered": 100,
13299                        "attached_condition": "t1.a < 7 and t1.a > 4"
13300                      }
13301                    }
13302                  }
13303                }
13304              },
13305              {
13306                "query_block": {
13307                  "select_id": 4,
13308                  "operation": "EXCEPT",
13309                  "having_condition": "c > 150 and c < 150",
13310                  "filesort": {
13311                    "sort_key": "t1.a, t1.b",
13312                    "temporary_table": {
13313                      "table": {
13314                        "table_name": "t1",
13315                        "access_type": "ALL",
13316                        "rows": 18,
13317                        "filtered": 100,
13318                        "attached_condition": "t1.a < 9 and t1.a > 4"
13319                      }
13320                    }
13321                  }
13322                }
13323              }
13324            ]
13325          }
13326        }
13327      }
13328    }
13329  }
13330}
13331drop view v1;
13332# using except and intersect in view definition
13333# conjunctive subformulas : pushing into WHERE and HAVING
13334create view v1 as
13335select a, b, max(c) as c from t1
13336where t1.b>10 group by a,b having c < 300
13337except
13338select a, b, max(c) as c from t1
13339where t1.a<9 group by a,b having c > 150
13340intersect
13341select a, b, max(c) as c from t1
13342where t1.a<7 group by a,b having c < 500;
13343set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.a=t2.a) and (v1.a>4) and (v1.c<130);
13344a	b	c	a	b	c
133458	33	123	8	33	117
13346select * from v1,t2 where (v1.a=t2.a) and (v1.a>4) and (v1.c<130);
13347a	b	c	a	b	c
133488	33	123	8	33	117
13349explain select * from v1,t2 where (v1.a=t2.a) and (v1.a>4) and (v1.c<130);
13350id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
133511	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
133521	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.a	2	Using where
133532	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
133543	EXCEPT	<derived4>	ALL	NULL	NULL	NULL	NULL	18	Using where
133554	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
133565	INTERSECT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
13357NULL	INTERSECT RESULT	<intersect4,5>	ALL	NULL	NULL	NULL	NULL	NULL
13358NULL	EXCEPT RESULT	<except2,3>	ALL	NULL	NULL	NULL	NULL	NULL
13359explain format=json select * from v1,t2 where (v1.a=t2.a) and (v1.a>4) and (v1.c<130);
13360EXPLAIN
13361{
13362  "query_block": {
13363    "select_id": 1,
13364    "table": {
13365      "table_name": "t2",
13366      "access_type": "ALL",
13367      "rows": 9,
13368      "filtered": 100,
13369      "attached_condition": "t2.a > 4 and t2.a is not null"
13370    },
13371    "table": {
13372      "table_name": "<derived2>",
13373      "access_type": "ref",
13374      "possible_keys": ["key0"],
13375      "key": "key0",
13376      "key_length": "5",
13377      "used_key_parts": ["a"],
13378      "ref": ["test.t2.a"],
13379      "rows": 2,
13380      "filtered": 100,
13381      "attached_condition": "v1.c < 130",
13382      "materialized": {
13383        "query_block": {
13384          "union_result": {
13385            "table_name": "<except2,3>",
13386            "access_type": "ALL",
13387            "query_specifications": [
13388              {
13389                "query_block": {
13390                  "select_id": 2,
13391                  "having_condition": "c < 300 and c < 130",
13392                  "filesort": {
13393                    "sort_key": "t1.a, t1.b",
13394                    "temporary_table": {
13395                      "table": {
13396                        "table_name": "t1",
13397                        "access_type": "ALL",
13398                        "rows": 18,
13399                        "filtered": 100,
13400                        "attached_condition": "t1.b > 10 and t1.a > 4"
13401                      }
13402                    }
13403                  }
13404                }
13405              },
13406              {
13407                "query_block": {
13408                  "select_id": 3,
13409                  "operation": "EXCEPT",
13410                  "table": {
13411                    "table_name": "<derived4>",
13412                    "access_type": "ALL",
13413                    "rows": 18,
13414                    "filtered": 100,
13415                    "attached_condition": "__5.a > 4 and __5.c < 130",
13416                    "materialized": {
13417                      "query_block": {
13418                        "union_result": {
13419                          "table_name": "<intersect4,5>",
13420                          "access_type": "ALL",
13421                          "query_specifications": [
13422                            {
13423                              "query_block": {
13424                                "select_id": 4,
13425                                "having_condition": "c > 150 and c < 130",
13426                                "filesort": {
13427                                  "sort_key": "t1.a, t1.b",
13428                                  "temporary_table": {
13429                                    "table": {
13430                                      "table_name": "t1",
13431                                      "access_type": "ALL",
13432                                      "rows": 18,
13433                                      "filtered": 100,
13434                                      "attached_condition": "t1.a < 9 and t1.a > 4"
13435                                    }
13436                                  }
13437                                }
13438                              }
13439                            },
13440                            {
13441                              "query_block": {
13442                                "select_id": 5,
13443                                "operation": "INTERSECT",
13444                                "having_condition": "c < 500 and c < 130",
13445                                "filesort": {
13446                                  "sort_key": "t1.a, t1.b",
13447                                  "temporary_table": {
13448                                    "table": {
13449                                      "table_name": "t1",
13450                                      "access_type": "ALL",
13451                                      "rows": 18,
13452                                      "filtered": 100,
13453                                      "attached_condition": "t1.a < 7 and t1.a > 4"
13454                                    }
13455                                  }
13456                                }
13457                              }
13458                            }
13459                          ]
13460                        }
13461                      }
13462                    }
13463                  }
13464                }
13465              }
13466            ]
13467          }
13468        }
13469      }
13470    }
13471  }
13472}
13473drop view v1;
13474# using except, intersect and union in view definition
13475# conjunctive subformulas : pushing into WHERE and HAVING
13476create view v1 as
13477select a, b, max(c) as c from t1
13478where t1.b>10 group by a,b having c < 300
13479except
13480select a, b, max(c) as c from t1
13481where t1.a<9 group by a,b having c > 150
13482intersect
13483select a, b, max(c) as c from t1
13484where t1.a<7 group by a,b having c < 500
13485union
13486select a, b, max(c) as c from t1
13487where t1.a<7 group by a,b having c < 120;
13488set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.a=t2.a) and (v1.a>4) and (v1.c<130);
13489a	b	c	a	b	c
134908	33	123	8	33	117
13491select * from v1,t2 where (v1.a=t2.a) and (v1.a>4) and (v1.c<130);
13492a	b	c	a	b	c
134938	33	123	8	33	117
13494explain select * from v1,t2 where (v1.a=t2.a) and (v1.a>4) and (v1.c<130);
13495id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
134961	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
134971	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.a	3	Using where
134982	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
134993	EXCEPT	<derived4>	ALL	NULL	NULL	NULL	NULL	18	Using where
135004	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
135015	INTERSECT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
13502NULL	INTERSECT RESULT	<intersect4,5>	ALL	NULL	NULL	NULL	NULL	NULL
135036	UNION	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
13504NULL	UNIT RESULT	<unit2,3,6>	ALL	NULL	NULL	NULL	NULL	NULL
13505explain format=json select * from v1,t2 where (v1.a=t2.a) and (v1.a>4) and (v1.c<130);
13506EXPLAIN
13507{
13508  "query_block": {
13509    "select_id": 1,
13510    "table": {
13511      "table_name": "t2",
13512      "access_type": "ALL",
13513      "rows": 9,
13514      "filtered": 100,
13515      "attached_condition": "t2.a > 4 and t2.a is not null"
13516    },
13517    "table": {
13518      "table_name": "<derived2>",
13519      "access_type": "ref",
13520      "possible_keys": ["key0"],
13521      "key": "key0",
13522      "key_length": "5",
13523      "used_key_parts": ["a"],
13524      "ref": ["test.t2.a"],
13525      "rows": 3,
13526      "filtered": 100,
13527      "attached_condition": "v1.c < 130",
13528      "materialized": {
13529        "query_block": {
13530          "union_result": {
13531            "table_name": "<unit2,3,6>",
13532            "access_type": "ALL",
13533            "query_specifications": [
13534              {
13535                "query_block": {
13536                  "select_id": 2,
13537                  "having_condition": "c < 300 and c < 130",
13538                  "filesort": {
13539                    "sort_key": "t1.a, t1.b",
13540                    "temporary_table": {
13541                      "table": {
13542                        "table_name": "t1",
13543                        "access_type": "ALL",
13544                        "rows": 18,
13545                        "filtered": 100,
13546                        "attached_condition": "t1.b > 10 and t1.a > 4"
13547                      }
13548                    }
13549                  }
13550                }
13551              },
13552              {
13553                "query_block": {
13554                  "select_id": 3,
13555                  "operation": "EXCEPT",
13556                  "table": {
13557                    "table_name": "<derived4>",
13558                    "access_type": "ALL",
13559                    "rows": 18,
13560                    "filtered": 100,
13561                    "attached_condition": "__6.a > 4 and __6.c < 130",
13562                    "materialized": {
13563                      "query_block": {
13564                        "union_result": {
13565                          "table_name": "<intersect4,5>",
13566                          "access_type": "ALL",
13567                          "query_specifications": [
13568                            {
13569                              "query_block": {
13570                                "select_id": 4,
13571                                "having_condition": "c > 150 and c < 130",
13572                                "filesort": {
13573                                  "sort_key": "t1.a, t1.b",
13574                                  "temporary_table": {
13575                                    "table": {
13576                                      "table_name": "t1",
13577                                      "access_type": "ALL",
13578                                      "rows": 18,
13579                                      "filtered": 100,
13580                                      "attached_condition": "t1.a < 9 and t1.a > 4"
13581                                    }
13582                                  }
13583                                }
13584                              }
13585                            },
13586                            {
13587                              "query_block": {
13588                                "select_id": 5,
13589                                "operation": "INTERSECT",
13590                                "having_condition": "c < 500 and c < 130",
13591                                "filesort": {
13592                                  "sort_key": "t1.a, t1.b",
13593                                  "temporary_table": {
13594                                    "table": {
13595                                      "table_name": "t1",
13596                                      "access_type": "ALL",
13597                                      "rows": 18,
13598                                      "filtered": 100,
13599                                      "attached_condition": "t1.a < 7 and t1.a > 4"
13600                                    }
13601                                  }
13602                                }
13603                              }
13604                            }
13605                          ]
13606                        }
13607                      }
13608                    }
13609                  }
13610                }
13611              },
13612              {
13613                "query_block": {
13614                  "select_id": 6,
13615                  "operation": "UNION",
13616                  "having_condition": "c < 120 and c < 130",
13617                  "filesort": {
13618                    "sort_key": "t1.a, t1.b",
13619                    "temporary_table": {
13620                      "table": {
13621                        "table_name": "t1",
13622                        "access_type": "ALL",
13623                        "rows": 18,
13624                        "filtered": 100,
13625                        "attached_condition": "t1.a < 7 and t1.a > 4"
13626                      }
13627                    }
13628                  }
13629                }
13630              }
13631            ]
13632          }
13633        }
13634      }
13635    }
13636  }
13637}
13638drop view v1;
13639# using intersect in view definition
13640# using embedded view
13641# conjunctive subformulas : pushing into WHERE and HAVING
13642create view v1 as
13643select a, b, max(c) as c from t1
13644where t1.b>10 group by a,b having c < 300
13645intersect
13646select a, b, max(c) as c from t1
13647where t1.a<9 group by a,b having c > 120;
13648create view v2 as
13649select a, b, max(c) as c from v1
13650where v1.a<7 group by a,b;
13651set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v2,t2 where (v2.a=t2.a) and (v2.a>4) and (v2.c<150);
13652a	b	c	a	b	c
136535	27	132	5	14	312
136545	27	132	5	33	207
13655select * from v2,t2 where (v2.a=t2.a) and (v2.a>4) and (v2.c<150);
13656a	b	c	a	b	c
136575	27	132	5	14	312
136585	27	132	5	33	207
13659explain select * from v2,t2 where (v2.a=t2.a) and (v2.a>4) and (v2.c<150);
13660id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
136611	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
136621	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.a	2	Using where
136632	DERIVED	<derived3>	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
136643	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
136654	INTERSECT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
13666NULL	INTERSECT RESULT	<intersect3,4>	ALL	NULL	NULL	NULL	NULL	NULL
13667explain format=json select * from v2,t2 where (v2.a=t2.a) and (v2.a>4) and (v2.c<150);
13668EXPLAIN
13669{
13670  "query_block": {
13671    "select_id": 1,
13672    "table": {
13673      "table_name": "t2",
13674      "access_type": "ALL",
13675      "rows": 9,
13676      "filtered": 100,
13677      "attached_condition": "t2.a > 4 and t2.a is not null"
13678    },
13679    "table": {
13680      "table_name": "<derived2>",
13681      "access_type": "ref",
13682      "possible_keys": ["key0"],
13683      "key": "key0",
13684      "key_length": "5",
13685      "used_key_parts": ["a"],
13686      "ref": ["test.t2.a"],
13687      "rows": 2,
13688      "filtered": 100,
13689      "attached_condition": "v2.c < 150",
13690      "materialized": {
13691        "query_block": {
13692          "select_id": 2,
13693          "having_condition": "c < 150",
13694          "filesort": {
13695            "sort_key": "v1.a, v1.b",
13696            "temporary_table": {
13697              "table": {
13698                "table_name": "<derived3>",
13699                "access_type": "ALL",
13700                "rows": 18,
13701                "filtered": 100,
13702                "attached_condition": "v1.a < 7 and v1.a > 4",
13703                "materialized": {
13704                  "query_block": {
13705                    "union_result": {
13706                      "table_name": "<intersect3,4>",
13707                      "access_type": "ALL",
13708                      "query_specifications": [
13709                        {
13710                          "query_block": {
13711                            "select_id": 3,
13712                            "having_condition": "c < 300",
13713                            "filesort": {
13714                              "sort_key": "t1.a, t1.b",
13715                              "temporary_table": {
13716                                "table": {
13717                                  "table_name": "t1",
13718                                  "access_type": "ALL",
13719                                  "rows": 18,
13720                                  "filtered": 100,
13721                                  "attached_condition": "t1.b > 10 and t1.a < 7 and t1.a > 4"
13722                                }
13723                              }
13724                            }
13725                          }
13726                        },
13727                        {
13728                          "query_block": {
13729                            "select_id": 4,
13730                            "operation": "INTERSECT",
13731                            "having_condition": "c > 120",
13732                            "filesort": {
13733                              "sort_key": "t1.a, t1.b",
13734                              "temporary_table": {
13735                                "table": {
13736                                  "table_name": "t1",
13737                                  "access_type": "ALL",
13738                                  "rows": 18,
13739                                  "filtered": 100,
13740                                  "attached_condition": "t1.a < 9 and t1.a < 7 and t1.a > 4"
13741                                }
13742                              }
13743                            }
13744                          }
13745                        }
13746                      ]
13747                    }
13748                  }
13749                }
13750              }
13751            }
13752          }
13753        }
13754      }
13755    }
13756  }
13757}
13758drop view v1,v2;
13759# using except in view definition
13760# using embedded view
13761# conjunctive subformulas : pushing into WHERE and HAVING
13762create view v1 as
13763select a, b, max(c) as c from t1
13764where t1.b>10 group by a,b having c < 300
13765except
13766select a, b, max(c) as c from t1
13767where t1.a<9 group by a,b having c > 150;
13768create view v2 as
13769select a, b, max(c) as c from v1
13770where v1.a<7 group by a,b;
13771set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v2,t2 where (v2.a=t2.a) and (v2.a>4) and (v2.c<150);
13772a	b	c	a	b	c
137735	27	132	5	14	312
137745	27	132	5	33	207
13775select * from v2,t2 where (v2.a=t2.a) and (v2.a>4) and (v2.c<150);
13776a	b	c	a	b	c
137775	27	132	5	14	312
137785	27	132	5	33	207
13779explain select * from v2,t2 where (v2.a=t2.a) and (v2.a>4) and (v2.c<150);
13780id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
137811	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
137821	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.a	2	Using where
137832	DERIVED	<derived3>	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
137843	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
137854	EXCEPT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
13786NULL	EXCEPT RESULT	<except3,4>	ALL	NULL	NULL	NULL	NULL	NULL
13787explain format=json select * from v2,t2 where (v2.a=t2.a) and (v2.a>4) and (v2.c<150);
13788EXPLAIN
13789{
13790  "query_block": {
13791    "select_id": 1,
13792    "table": {
13793      "table_name": "t2",
13794      "access_type": "ALL",
13795      "rows": 9,
13796      "filtered": 100,
13797      "attached_condition": "t2.a > 4 and t2.a is not null"
13798    },
13799    "table": {
13800      "table_name": "<derived2>",
13801      "access_type": "ref",
13802      "possible_keys": ["key0"],
13803      "key": "key0",
13804      "key_length": "5",
13805      "used_key_parts": ["a"],
13806      "ref": ["test.t2.a"],
13807      "rows": 2,
13808      "filtered": 100,
13809      "attached_condition": "v2.c < 150",
13810      "materialized": {
13811        "query_block": {
13812          "select_id": 2,
13813          "having_condition": "c < 150",
13814          "filesort": {
13815            "sort_key": "v1.a, v1.b",
13816            "temporary_table": {
13817              "table": {
13818                "table_name": "<derived3>",
13819                "access_type": "ALL",
13820                "rows": 18,
13821                "filtered": 100,
13822                "attached_condition": "v1.a < 7 and v1.a > 4",
13823                "materialized": {
13824                  "query_block": {
13825                    "union_result": {
13826                      "table_name": "<except3,4>",
13827                      "access_type": "ALL",
13828                      "query_specifications": [
13829                        {
13830                          "query_block": {
13831                            "select_id": 3,
13832                            "having_condition": "c < 300",
13833                            "filesort": {
13834                              "sort_key": "t1.a, t1.b",
13835                              "temporary_table": {
13836                                "table": {
13837                                  "table_name": "t1",
13838                                  "access_type": "ALL",
13839                                  "rows": 18,
13840                                  "filtered": 100,
13841                                  "attached_condition": "t1.b > 10 and t1.a < 7 and t1.a > 4"
13842                                }
13843                              }
13844                            }
13845                          }
13846                        },
13847                        {
13848                          "query_block": {
13849                            "select_id": 4,
13850                            "operation": "EXCEPT",
13851                            "having_condition": "c > 150",
13852                            "filesort": {
13853                              "sort_key": "t1.a, t1.b",
13854                              "temporary_table": {
13855                                "table": {
13856                                  "table_name": "t1",
13857                                  "access_type": "ALL",
13858                                  "rows": 18,
13859                                  "filtered": 100,
13860                                  "attached_condition": "t1.a < 9 and t1.a < 7 and t1.a > 4"
13861                                }
13862                              }
13863                            }
13864                          }
13865                        }
13866                      ]
13867                    }
13868                  }
13869                }
13870              }
13871            }
13872          }
13873        }
13874      }
13875    }
13876  }
13877}
13878drop view v1,v2;
13879# using intersect in view definition
13880# conditions are pushed in different parts of selects
13881# conjunctive subformulas : pushing into WHERE and HAVING
13882create view v1 as
13883select a, b, max(c) as c from t1
13884where t1.a<9 group by a having c > 300
13885intersect
13886select a, b, max(c) as c from t1
13887where t1.b<21 group by b having c > 200;
13888set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.a=t2.a) and (v1.a>4) and (v1.b>12) and (v1.c<450);
13889a	b	c	a	b	c
138906	20	315	6	20	211
138916	20	315	6	23	303
13892select * from v1,t2 where (v1.a=t2.a) and (v1.a>4) and (v1.b>12) and (v1.c<450);
13893a	b	c	a	b	c
138946	20	315	6	20	211
138956	20	315	6	23	303
13896explain select * from v1,t2 where (v1.a=t2.a) and (v1.a>4) and (v1.b>12) and (v1.c<450);
13897id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
138981	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
138991	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.a	2	Using where
139002	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
139013	INTERSECT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
13902NULL	INTERSECT RESULT	<intersect2,3>	ALL	NULL	NULL	NULL	NULL	NULL
13903explain format=json select * from v1,t2 where (v1.a=t2.a) and (v1.a>4) and (v1.b>12) and (v1.c<450);
13904EXPLAIN
13905{
13906  "query_block": {
13907    "select_id": 1,
13908    "table": {
13909      "table_name": "t2",
13910      "access_type": "ALL",
13911      "rows": 9,
13912      "filtered": 100,
13913      "attached_condition": "t2.a > 4 and t2.a is not null"
13914    },
13915    "table": {
13916      "table_name": "<derived2>",
13917      "access_type": "ref",
13918      "possible_keys": ["key0"],
13919      "key": "key0",
13920      "key_length": "5",
13921      "used_key_parts": ["a"],
13922      "ref": ["test.t2.a"],
13923      "rows": 2,
13924      "filtered": 100,
13925      "attached_condition": "v1.b > 12 and v1.c < 450",
13926      "materialized": {
13927        "query_block": {
13928          "union_result": {
13929            "table_name": "<intersect2,3>",
13930            "access_type": "ALL",
13931            "query_specifications": [
13932              {
13933                "query_block": {
13934                  "select_id": 2,
13935                  "having_condition": "c > 300 and t1.b > 12 and c < 450",
13936                  "filesort": {
13937                    "sort_key": "t1.a",
13938                    "temporary_table": {
13939                      "table": {
13940                        "table_name": "t1",
13941                        "access_type": "ALL",
13942                        "rows": 18,
13943                        "filtered": 100,
13944                        "attached_condition": "t1.a < 9 and t1.a > 4"
13945                      }
13946                    }
13947                  }
13948                }
13949              },
13950              {
13951                "query_block": {
13952                  "select_id": 3,
13953                  "operation": "INTERSECT",
13954                  "having_condition": "c > 200 and t1.a > 4 and c < 450",
13955                  "filesort": {
13956                    "sort_key": "t1.b",
13957                    "temporary_table": {
13958                      "table": {
13959                        "table_name": "t1",
13960                        "access_type": "ALL",
13961                        "rows": 18,
13962                        "filtered": 100,
13963                        "attached_condition": "t1.b < 21 and t1.b > 12"
13964                      }
13965                    }
13966                  }
13967                }
13968              }
13969            ]
13970          }
13971        }
13972      }
13973    }
13974  }
13975}
13976drop view v1;
13977# using except in view definition
13978# conditions are pushed in different parts of selects
13979# conjunctive subformulas : pushing into WHERE and HAVING
13980create view v1 as
13981select a, b, max(c) as c from t1
13982where t1.b>20 group by a having c > 300
13983except
13984select a, b, max(c) as c from t1
13985where t1.a<7 group by b having c > 150;
13986set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.a=t2.a) and (v1.a<2) and (v1.b<30) and (v1.c>450);
13987a	b	c	a	b	c
139881	21	988	1	16	909
139891	21	988	1	19	132
13990select * from v1,t2 where (v1.a=t2.a) and (v1.a<2) and (v1.b<30) and (v1.c>450);
13991a	b	c	a	b	c
139921	21	988	1	16	909
139931	21	988	1	19	132
13994explain select * from v1,t2 where (v1.a=t2.a) and (v1.a<2) and (v1.b<30) and (v1.c>450);
13995id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
139961	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
139971	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.a	2	Using where
139982	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
139993	EXCEPT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
14000NULL	EXCEPT RESULT	<except2,3>	ALL	NULL	NULL	NULL	NULL	NULL
14001explain format=json select * from v1,t2 where (v1.a=t2.a) and (v1.a<2) and (v1.b<30) and (v1.c>450);
14002EXPLAIN
14003{
14004  "query_block": {
14005    "select_id": 1,
14006    "table": {
14007      "table_name": "t2",
14008      "access_type": "ALL",
14009      "rows": 9,
14010      "filtered": 100,
14011      "attached_condition": "t2.a < 2 and t2.a is not null"
14012    },
14013    "table": {
14014      "table_name": "<derived2>",
14015      "access_type": "ref",
14016      "possible_keys": ["key0"],
14017      "key": "key0",
14018      "key_length": "5",
14019      "used_key_parts": ["a"],
14020      "ref": ["test.t2.a"],
14021      "rows": 2,
14022      "filtered": 100,
14023      "attached_condition": "v1.b < 30 and v1.c > 450",
14024      "materialized": {
14025        "query_block": {
14026          "union_result": {
14027            "table_name": "<except2,3>",
14028            "access_type": "ALL",
14029            "query_specifications": [
14030              {
14031                "query_block": {
14032                  "select_id": 2,
14033                  "having_condition": "c > 300 and t1.b < 30 and c > 450",
14034                  "filesort": {
14035                    "sort_key": "t1.a",
14036                    "temporary_table": {
14037                      "table": {
14038                        "table_name": "t1",
14039                        "access_type": "ALL",
14040                        "rows": 18,
14041                        "filtered": 100,
14042                        "attached_condition": "t1.b > 20 and t1.a < 2"
14043                      }
14044                    }
14045                  }
14046                }
14047              },
14048              {
14049                "query_block": {
14050                  "select_id": 3,
14051                  "operation": "EXCEPT",
14052                  "having_condition": "c > 150 and t1.a < 2 and c > 450",
14053                  "filesort": {
14054                    "sort_key": "t1.b",
14055                    "temporary_table": {
14056                      "table": {
14057                        "table_name": "t1",
14058                        "access_type": "ALL",
14059                        "rows": 18,
14060                        "filtered": 100,
14061                        "attached_condition": "t1.a < 7 and t1.b < 30"
14062                      }
14063                    }
14064                  }
14065                }
14066              }
14067            ]
14068          }
14069        }
14070      }
14071    }
14072  }
14073}
14074drop view v1;
14075# using except and union in view definition
14076# conditions are pushed in different parts of selects
14077# conjunctive subformulas : pushing into HAVING
14078# extracted or formula : pushing into WHERE
14079# extracted or formula : pushing into HAVING
14080create view v1 as
14081select a, b, max(c) as c from t1
14082where t1.b>20 group by a having c > 300
14083except
14084select a, b, max(c) as c from t1
14085where t1.a<7 group by b having c > 150;
14086set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.a=t2.a) and ((v1.a<2) or (v1.a<5)) and (v1.c>450);
14087a	b	c	a	b	c
140881	21	988	1	16	909
140891	21	988	1	19	132
14090select * from v1,t2 where (v1.a=t2.a) and ((v1.a<2) or (v1.a<5)) and (v1.c>450);
14091a	b	c	a	b	c
140921	21	988	1	16	909
140931	21	988	1	19	132
14094explain select * from v1,t2 where (v1.a=t2.a) and ((v1.a<2) or (v1.a<5)) and (v1.c>450);
14095id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
140961	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
140971	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.a	2	Using where
140982	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
140993	EXCEPT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
14100NULL	EXCEPT RESULT	<except2,3>	ALL	NULL	NULL	NULL	NULL	NULL
14101explain format=json select * from v1,t2 where (v1.a=t2.a) and ((v1.a<2) or (v1.a<5)) and (v1.c>450);
14102EXPLAIN
14103{
14104  "query_block": {
14105    "select_id": 1,
14106    "table": {
14107      "table_name": "t2",
14108      "access_type": "ALL",
14109      "rows": 9,
14110      "filtered": 100,
14111      "attached_condition": "(t2.a < 2 or t2.a < 5) and t2.a is not null"
14112    },
14113    "table": {
14114      "table_name": "<derived2>",
14115      "access_type": "ref",
14116      "possible_keys": ["key0"],
14117      "key": "key0",
14118      "key_length": "5",
14119      "used_key_parts": ["a"],
14120      "ref": ["test.t2.a"],
14121      "rows": 2,
14122      "filtered": 100,
14123      "attached_condition": "v1.c > 450",
14124      "materialized": {
14125        "query_block": {
14126          "union_result": {
14127            "table_name": "<except2,3>",
14128            "access_type": "ALL",
14129            "query_specifications": [
14130              {
14131                "query_block": {
14132                  "select_id": 2,
14133                  "having_condition": "c > 300 and c > 450",
14134                  "filesort": {
14135                    "sort_key": "t1.a",
14136                    "temporary_table": {
14137                      "table": {
14138                        "table_name": "t1",
14139                        "access_type": "ALL",
14140                        "rows": 18,
14141                        "filtered": 100,
14142                        "attached_condition": "t1.b > 20 and (t1.a < 2 or t1.a < 5)"
14143                      }
14144                    }
14145                  }
14146                }
14147              },
14148              {
14149                "query_block": {
14150                  "select_id": 3,
14151                  "operation": "EXCEPT",
14152                  "having_condition": "c > 150 and (t1.a < 2 or t1.a < 5) and c > 450",
14153                  "filesort": {
14154                    "sort_key": "t1.b",
14155                    "temporary_table": {
14156                      "table": {
14157                        "table_name": "t1",
14158                        "access_type": "ALL",
14159                        "rows": 18,
14160                        "filtered": 100,
14161                        "attached_condition": "t1.a < 7"
14162                      }
14163                    }
14164                  }
14165                }
14166              }
14167            ]
14168          }
14169        }
14170      }
14171    }
14172  }
14173}
14174drop view v1;
14175# using union and intersect in view definition
14176# conditions are pushed in different parts of selects
14177# conjunctive subformulas : pushing into WHERE and HAVING
14178create view v1 as
14179select a, b, max(c) as c from t1
14180where t1.a<9 group by a having c > 100
14181intersect
14182select a, b, max(c) as c from t1
14183where t1.a>3 group by b having c < 800
14184union
14185select a, b, max(c) as c from t1
14186where t1.b>10 group by a,b having c > 300;
14187set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.a=t2.a) and (v1.a>1) and (v1.b > 12) and (v1.c>400);
14188a	b	c	a	b	c
141895	14	787	5	14	312
141905	14	787	5	33	207
14191select * from v1,t2 where (v1.a=t2.a) and (v1.a>1) and (v1.b > 12) and (v1.c>400);
14192a	b	c	a	b	c
141935	14	787	5	14	312
141945	14	787	5	33	207
14195explain select * from v1,t2 where (v1.a=t2.a) and (v1.a>1) and (v1.b > 12) and (v1.c>400);
14196id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
141971	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
141981	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.a	3	Using where
141992	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
142003	INTERSECT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
142014	UNION	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
14202NULL	UNIT RESULT	<unit2,3,4>	ALL	NULL	NULL	NULL	NULL	NULL
14203explain format=json select * from v1,t2 where (v1.a=t2.a) and (v1.a>1) and (v1.b > 12) and (v1.c>400);
14204EXPLAIN
14205{
14206  "query_block": {
14207    "select_id": 1,
14208    "table": {
14209      "table_name": "t2",
14210      "access_type": "ALL",
14211      "rows": 9,
14212      "filtered": 100,
14213      "attached_condition": "t2.a > 1 and t2.a is not null"
14214    },
14215    "table": {
14216      "table_name": "<derived2>",
14217      "access_type": "ref",
14218      "possible_keys": ["key0"],
14219      "key": "key0",
14220      "key_length": "5",
14221      "used_key_parts": ["a"],
14222      "ref": ["test.t2.a"],
14223      "rows": 3,
14224      "filtered": 100,
14225      "attached_condition": "v1.b > 12 and v1.c > 400",
14226      "materialized": {
14227        "query_block": {
14228          "union_result": {
14229            "table_name": "<unit2,3,4>",
14230            "access_type": "ALL",
14231            "query_specifications": [
14232              {
14233                "query_block": {
14234                  "select_id": 2,
14235                  "having_condition": "c > 100 and t1.b > 12 and c > 400",
14236                  "filesort": {
14237                    "sort_key": "t1.a",
14238                    "temporary_table": {
14239                      "table": {
14240                        "table_name": "t1",
14241                        "access_type": "ALL",
14242                        "rows": 18,
14243                        "filtered": 100,
14244                        "attached_condition": "t1.a < 9 and t1.a > 1"
14245                      }
14246                    }
14247                  }
14248                }
14249              },
14250              {
14251                "query_block": {
14252                  "select_id": 3,
14253                  "operation": "INTERSECT",
14254                  "having_condition": "c < 800 and t1.a > 1 and c > 400",
14255                  "filesort": {
14256                    "sort_key": "t1.b",
14257                    "temporary_table": {
14258                      "table": {
14259                        "table_name": "t1",
14260                        "access_type": "ALL",
14261                        "rows": 18,
14262                        "filtered": 100,
14263                        "attached_condition": "t1.a > 3 and t1.b > 12"
14264                      }
14265                    }
14266                  }
14267                }
14268              },
14269              {
14270                "query_block": {
14271                  "select_id": 4,
14272                  "operation": "UNION",
14273                  "having_condition": "c > 300 and c > 400",
14274                  "filesort": {
14275                    "sort_key": "t1.a, t1.b",
14276                    "temporary_table": {
14277                      "table": {
14278                        "table_name": "t1",
14279                        "access_type": "ALL",
14280                        "rows": 18,
14281                        "filtered": 100,
14282                        "attached_condition": "t1.b > 10 and t1.a > 1 and t1.b > 12"
14283                      }
14284                    }
14285                  }
14286                }
14287              }
14288            ]
14289          }
14290        }
14291      }
14292    }
14293  }
14294}
14295drop view v1;
14296create table t3 (a int, b int, c int);
14297insert into t3 values
14298(1,21,345), (2,33,7), (8,33,114), (3,21,500), (1,19,107), (5,14,787),
14299(4,33,123), (9,10,211), (11,16,207), (10,33,988), (5,27,132), (12,21,104),
14300(6,20,309), (16,20,315), (16,21,101), (18,33,404), (19,10,800), (10,21,123),
14301(17,11,708), (6,20,214);
14302create index i1 on t3(a);
14303# conjunctive subformulas : pushing into WHERE
14304# pushed condition gives range access
14305create view v1 as
14306select a, b, max(c) as max_c from t3
14307where a>0 group by a;
14308set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.b=t2.b) and (v1.a<5);
14309a	b	max_c	a	b	c
143104	33	123	5	33	207
143112	33	7	5	33	207
143124	33	123	8	33	117
143132	33	7	8	33	117
143143	21	500	3	21	231
143151	21	345	3	21	231
14316select * from v1,t2 where (v1.b=t2.b) and (v1.a<5);
14317a	b	max_c	a	b	c
143184	33	123	5	33	207
143192	33	7	5	33	207
143204	33	123	8	33	117
143212	33	7	8	33	117
143223	21	500	3	21	231
143231	21	345	3	21	231
14324explain select * from v1,t2 where (v1.b=t2.b) and (v1.a<5);
14325id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
143261	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
143271	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.b	2	Using where
143282	DERIVED	t3	range	i1	i1	5	NULL	5	Using index condition
14329explain format=json select * from v1,t2 where (v1.b=t2.b) and (v1.a<5);
14330EXPLAIN
14331{
14332  "query_block": {
14333    "select_id": 1,
14334    "table": {
14335      "table_name": "t2",
14336      "access_type": "ALL",
14337      "rows": 9,
14338      "filtered": 100,
14339      "attached_condition": "t2.b is not null"
14340    },
14341    "table": {
14342      "table_name": "<derived2>",
14343      "access_type": "ref",
14344      "possible_keys": ["key0"],
14345      "key": "key0",
14346      "key_length": "5",
14347      "used_key_parts": ["b"],
14348      "ref": ["test.t2.b"],
14349      "rows": 2,
14350      "filtered": 100,
14351      "attached_condition": "v1.a < 5",
14352      "materialized": {
14353        "query_block": {
14354          "select_id": 2,
14355          "table": {
14356            "table_name": "t3",
14357            "access_type": "range",
14358            "possible_keys": ["i1"],
14359            "key": "i1",
14360            "key_length": "5",
14361            "used_key_parts": ["a"],
14362            "rows": 5,
14363            "filtered": 100,
14364            "index_condition": "t3.a > 0 and t3.a < 5"
14365          }
14366        }
14367      }
14368    }
14369  }
14370}
14371drop view v1;
14372# using union in view definition
14373# conjunctive subformulas : pushing into WHERE
14374# pushed condition gives range access
14375create view v1 as
14376select a, b, max(c) as c from t3
14377where t3.a>1 group by a
14378union
14379select a, b, max(c) as c from t3
14380where t3.a>2 group by a;
14381set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.b=t2.b) and (v1.a<4);
14382a	b	c	a	b	c
143832	33	7	5	33	207
143842	33	7	8	33	117
143853	21	500	3	21	231
14386select * from v1,t2 where (v1.b=t2.b) and (v1.a<4);
14387a	b	c	a	b	c
143882	33	7	5	33	207
143892	33	7	8	33	117
143903	21	500	3	21	231
14391explain select * from v1,t2 where (v1.b=t2.b) and (v1.a<4);
14392id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
143931	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	3	Using where
143941	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where; Using join buffer (flat, BNL join)
143952	DERIVED	t3	range	i1	i1	5	NULL	2	Using index condition
143963	UNION	t3	range	i1	i1	5	NULL	1	Using index condition
14397NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL
14398explain format=json select * from v1,t2 where (v1.b=t2.b) and (v1.a<4);
14399EXPLAIN
14400{
14401  "query_block": {
14402    "select_id": 1,
14403    "table": {
14404      "table_name": "<derived2>",
14405      "access_type": "ALL",
14406      "rows": 3,
14407      "filtered": 100,
14408      "attached_condition": "v1.a < 4",
14409      "materialized": {
14410        "query_block": {
14411          "union_result": {
14412            "table_name": "<union2,3>",
14413            "access_type": "ALL",
14414            "query_specifications": [
14415              {
14416                "query_block": {
14417                  "select_id": 2,
14418                  "table": {
14419                    "table_name": "t3",
14420                    "access_type": "range",
14421                    "possible_keys": ["i1"],
14422                    "key": "i1",
14423                    "key_length": "5",
14424                    "used_key_parts": ["a"],
14425                    "rows": 2,
14426                    "filtered": 100,
14427                    "index_condition": "t3.a > 1 and t3.a < 4"
14428                  }
14429                }
14430              },
14431              {
14432                "query_block": {
14433                  "select_id": 3,
14434                  "operation": "UNION",
14435                  "table": {
14436                    "table_name": "t3",
14437                    "access_type": "range",
14438                    "possible_keys": ["i1"],
14439                    "key": "i1",
14440                    "key_length": "5",
14441                    "used_key_parts": ["a"],
14442                    "rows": 1,
14443                    "filtered": 100,
14444                    "index_condition": "t3.a > 2 and t3.a < 4"
14445                  }
14446                }
14447              }
14448            ]
14449          }
14450        }
14451      }
14452    },
14453    "block-nl-join": {
14454      "table": {
14455        "table_name": "t2",
14456        "access_type": "ALL",
14457        "rows": 9,
14458        "filtered": 100
14459      },
14460      "buffer_type": "flat",
14461      "buffer_size": "173",
14462      "join_type": "BNL",
14463      "attached_condition": "t2.b = v1.b"
14464    }
14465  }
14466}
14467drop view v1;
14468# using union in view definition
14469# conjunctive subformulas : pushing into WHERE
14470# pushed condition gives range access in one of the selects
14471create view v1 as
14472select a, b, max(c) as c from t3
14473where t3.a>1 group by a
14474union
14475select a, b, max(c) as c from t3
14476where t3.b<21 group by b;
14477set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.b=t2.b) and (v1.a<3);
14478a	b	c	a	b	c
144792	33	7	5	33	207
144801	19	107	1	19	132
144812	33	7	8	33	117
14482select * from v1,t2 where (v1.b=t2.b) and (v1.a<3);
14483a	b	c	a	b	c
144842	33	7	5	33	207
144851	19	107	1	19	132
144862	33	7	8	33	117
14487explain select * from v1,t2 where (v1.b=t2.b) and (v1.a<3);
14488id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
144891	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
144901	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.b	2	Using where
144912	DERIVED	t3	range	i1	i1	5	NULL	1	Using index condition
144923	UNION	t3	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
14493NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL
14494explain format=json select * from v1,t2 where (v1.b=t2.b) and (v1.a<3);
14495EXPLAIN
14496{
14497  "query_block": {
14498    "select_id": 1,
14499    "table": {
14500      "table_name": "t2",
14501      "access_type": "ALL",
14502      "rows": 9,
14503      "filtered": 100,
14504      "attached_condition": "t2.b is not null"
14505    },
14506    "table": {
14507      "table_name": "<derived2>",
14508      "access_type": "ref",
14509      "possible_keys": ["key0"],
14510      "key": "key0",
14511      "key_length": "5",
14512      "used_key_parts": ["b"],
14513      "ref": ["test.t2.b"],
14514      "rows": 2,
14515      "filtered": 100,
14516      "attached_condition": "v1.a < 3",
14517      "materialized": {
14518        "query_block": {
14519          "union_result": {
14520            "table_name": "<union2,3>",
14521            "access_type": "ALL",
14522            "query_specifications": [
14523              {
14524                "query_block": {
14525                  "select_id": 2,
14526                  "table": {
14527                    "table_name": "t3",
14528                    "access_type": "range",
14529                    "possible_keys": ["i1"],
14530                    "key": "i1",
14531                    "key_length": "5",
14532                    "used_key_parts": ["a"],
14533                    "rows": 1,
14534                    "filtered": 100,
14535                    "index_condition": "t3.a > 1 and t3.a < 3"
14536                  }
14537                }
14538              },
14539              {
14540                "query_block": {
14541                  "select_id": 3,
14542                  "operation": "UNION",
14543                  "having_condition": "t3.a < 3",
14544                  "filesort": {
14545                    "sort_key": "t3.b",
14546                    "temporary_table": {
14547                      "table": {
14548                        "table_name": "t3",
14549                        "access_type": "ALL",
14550                        "rows": 20,
14551                        "filtered": 100,
14552                        "attached_condition": "t3.b < 21"
14553                      }
14554                    }
14555                  }
14556                }
14557              }
14558            ]
14559          }
14560        }
14561      }
14562    }
14563  }
14564}
14565drop view v1;
14566alter table t3 drop index i1;
14567drop table t1,t2,t3;
14568#
14569# MDEV-10855: Pushdown into derived with window functions
14570#
14571set @save_optimizer_switch= @@optimizer_switch;
14572set optimizer_switch='split_materialized=off';
14573create table t1 (a int, c varchar(16));
14574insert into t1 values
14575(8,'aa'), (5,'cc'), (1,'bb'), (2,'aa'), (9,'cc'),
14576(7,'aa'), (2,'aa'), (7,'bb');
14577create table t2 (a int, b int, c varchar(16), index idx(a,c));
14578insert into t2 values
14579(7,10,'cc'), (1,20,'aa'), (2,23,'bb'), (7,18,'cc'), (1,30,'bb'),
14580(4,71,'xx'), (3,15,'aa'), (7,82,'bb'), (8,12,'dd'), (4,15,'aa'),
14581(11,33,'yy'), (10,42,'zz'), (4,53,'xx'), (10,17,'yy'), (7,12,'bb'),
14582(8,20,'dd'), (7,32,'bb'), (1,50,'aa'), (3,40,'bb'), (3,77,'aa');
14583set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from (select a, c, sum(b) over (partition by a,c) from t2) as t
14584where t.a > 2 and t.c in ('aa','bb','cc');
14585a	c	sum(b) over (partition by a,c)
145863	aa	92
145873	aa	92
145883	bb	40
145894	aa	15
145907	bb	126
145917	bb	126
145927	bb	126
145937	cc	28
145947	cc	28
14595select * from (select a, c, sum(b) over (partition by a,c) from t2) as t
14596where t.a > 2 and t.c in ('aa','bb','cc');
14597a	c	sum(b) over (partition by a,c)
145983	aa	92
145993	aa	92
146003	bb	40
146014	aa	15
146027	bb	126
146037	bb	126
146047	bb	126
146057	cc	28
146067	cc	28
14607explain select * from (select a, c, sum(b) over (partition by a,c) from t2) as t
14608where t.a > 2 and t.c in ('aa','bb','cc');
14609id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
146101	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	16	Using where
146112	DERIVED	t2	ALL	idx	NULL	NULL	NULL	20	Using where; Using temporary
14612explain format=json select * from (select a, c, sum(b) over (partition by a,c) from t2) as t
14613where t.a > 2 and t.c in ('aa','bb','cc');
14614EXPLAIN
14615{
14616  "query_block": {
14617    "select_id": 1,
14618    "table": {
14619      "table_name": "<derived2>",
14620      "access_type": "ALL",
14621      "rows": 16,
14622      "filtered": 100,
14623      "attached_condition": "t.a > 2 and t.c in ('aa','bb','cc')",
14624      "materialized": {
14625        "query_block": {
14626          "select_id": 2,
14627          "window_functions_computation": {
14628            "sorts": {
14629              "filesort": {
14630                "sort_key": "t2.a, t2.c"
14631              }
14632            },
14633            "temporary_table": {
14634              "table": {
14635                "table_name": "t2",
14636                "access_type": "ALL",
14637                "possible_keys": ["idx"],
14638                "rows": 20,
14639                "filtered": 80,
14640                "attached_condition": "t2.a > 2 and t2.c in ('aa','bb','cc')"
14641              }
14642            }
14643          }
14644        }
14645      }
14646    }
14647  }
14648}
14649set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from
14650(
14651select 1 as n, a, c, sum(b) over (partition by a,c) as s from t2
14652union all
14653select 2 as n, a, c, sum(b) over (partition by a) as s from t2
14654) as t
14655where t.a > 2 and t.c in ('aa','bb','cc');
14656n	a	c	s
146571	3	aa	92
146581	3	aa	92
146591	3	bb	40
146601	4	aa	15
146611	7	bb	126
146621	7	bb	126
146631	7	bb	126
146641	7	cc	28
146651	7	cc	28
146662	3	aa	132
146672	3	aa	132
146682	3	bb	132
146692	4	aa	139
146702	7	bb	154
146712	7	bb	154
146722	7	bb	154
146732	7	cc	154
146742	7	cc	154
14675select * from
14676(
14677select 1 as n, a, c, sum(b) over (partition by a,c) as s from t2
14678union all
14679select 2 as n, a, c, sum(b) over (partition by a) as s from t2
14680) as t
14681where t.a > 2 and t.c in ('aa','bb','cc');
14682n	a	c	s
146831	3	aa	92
146841	3	aa	92
146851	3	bb	40
146861	4	aa	15
146871	7	bb	126
146881	7	bb	126
146891	7	bb	126
146901	7	cc	28
146911	7	cc	28
146922	3	aa	132
146932	3	aa	132
146942	3	bb	132
146952	4	aa	139
146962	7	bb	154
146972	7	bb	154
146982	7	bb	154
146992	7	cc	154
147002	7	cc	154
14701explain select * from
14702(
14703select 1 as n, a, c, sum(b) over (partition by a,c) as s from t2
14704union all
14705select 2 as n, a, c, sum(b) over (partition by a) as s from t2
14706) as t
14707where t.a > 2 and t.c in ('aa','bb','cc');
14708id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
147091	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	32	Using where
147102	DERIVED	t2	ALL	idx	NULL	NULL	NULL	20	Using where; Using temporary
147113	UNION	t2	ALL	idx	NULL	NULL	NULL	20	Using where; Using temporary
14712explain format=json select * from
14713(
14714select 1 as n, a, c, sum(b) over (partition by a,c) as s from t2
14715union all
14716select 2 as n, a, c, sum(b) over (partition by a) as s from t2
14717) as t
14718where t.a > 2 and t.c in ('aa','bb','cc');
14719EXPLAIN
14720{
14721  "query_block": {
14722    "select_id": 1,
14723    "table": {
14724      "table_name": "<derived2>",
14725      "access_type": "ALL",
14726      "rows": 32,
14727      "filtered": 100,
14728      "attached_condition": "t.a > 2 and t.c in ('aa','bb','cc')",
14729      "materialized": {
14730        "query_block": {
14731          "union_result": {
14732            "table_name": "<union2,3>",
14733            "access_type": "ALL",
14734            "query_specifications": [
14735              {
14736                "query_block": {
14737                  "select_id": 2,
14738                  "window_functions_computation": {
14739                    "sorts": {
14740                      "filesort": {
14741                        "sort_key": "t2.a, t2.c"
14742                      }
14743                    },
14744                    "temporary_table": {
14745                      "table": {
14746                        "table_name": "t2",
14747                        "access_type": "ALL",
14748                        "possible_keys": ["idx"],
14749                        "rows": 20,
14750                        "filtered": 80,
14751                        "attached_condition": "t2.a > 2 and t2.c in ('aa','bb','cc')"
14752                      }
14753                    }
14754                  }
14755                }
14756              },
14757              {
14758                "query_block": {
14759                  "select_id": 3,
14760                  "operation": "UNION",
14761                  "window_functions_computation": {
14762                    "sorts": {
14763                      "filesort": {
14764                        "sort_key": "t2.a"
14765                      }
14766                    },
14767                    "temporary_table": {
14768                      "table": {
14769                        "table_name": "t2",
14770                        "access_type": "ALL",
14771                        "possible_keys": ["idx"],
14772                        "rows": 20,
14773                        "filtered": 80,
14774                        "attached_condition": "t2.a > 2"
14775                      }
14776                    }
14777                  }
14778                }
14779              }
14780            ]
14781          }
14782        }
14783      }
14784    }
14785  }
14786}
14787set statement optimizer_switch='condition_pushdown_for_derived=off' for select *
14788from (select a, c, sum(b) over (partition by a,c) as s from t2) as t, t1
14789where t1.a=t.a and t1.c=t.c and t1.c in ('aa','bb','cc');
14790a	c	s	a	c
147911	bb	30	1	bb
147927	bb	126	7	bb
147937	bb	126	7	bb
147947	bb	126	7	bb
14795select *
14796from (select a, c, sum(b) over (partition by a,c) as s from t2) as t, t1
14797where t1.a=t.a and t1.c=t.c and t1.c in ('aa','bb','cc');
14798a	c	s	a	c
147991	bb	30	1	bb
148007	bb	126	7	bb
148017	bb	126	7	bb
148027	bb	126	7	bb
14803explain select *
14804from (select a, c, sum(b) over (partition by a,c) as s from t2) as t, t1
14805where t1.a=t.a and t1.c=t.c and t1.c in ('aa','bb','cc');
14806id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
148071	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	8	Using where
148081	PRIMARY	<derived2>	ref	key0	key0	24	test.t1.a,test.t1.c	2
148092	DERIVED	t2	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary
14810explain format=json select *
14811from (select a, c, sum(b) over (partition by a,c) as s from t2) as t, t1
14812where t1.a=t.a and t1.c=t.c and t1.c in ('aa','bb','cc');
14813EXPLAIN
14814{
14815  "query_block": {
14816    "select_id": 1,
14817    "table": {
14818      "table_name": "t1",
14819      "access_type": "ALL",
14820      "rows": 8,
14821      "filtered": 100,
14822      "attached_condition": "t1.c in ('aa','bb','cc') and t1.a is not null and t1.c is not null"
14823    },
14824    "table": {
14825      "table_name": "<derived2>",
14826      "access_type": "ref",
14827      "possible_keys": ["key0"],
14828      "key": "key0",
14829      "key_length": "24",
14830      "used_key_parts": ["a", "c"],
14831      "ref": ["test.t1.a", "test.t1.c"],
14832      "rows": 2,
14833      "filtered": 100,
14834      "materialized": {
14835        "query_block": {
14836          "select_id": 2,
14837          "window_functions_computation": {
14838            "sorts": {
14839              "filesort": {
14840                "sort_key": "t2.a, t2.c"
14841              }
14842            },
14843            "temporary_table": {
14844              "table": {
14845                "table_name": "t2",
14846                "access_type": "ALL",
14847                "rows": 20,
14848                "filtered": 100,
14849                "attached_condition": "t2.c in ('aa','bb','cc')"
14850              }
14851            }
14852          }
14853        }
14854      }
14855    }
14856  }
14857}
14858set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from
14859(
14860select 1 as n, a, c, sum(b) over (partition by a,c) as s from t2
14861union all
14862select 2 as n, a, c, sum(b) over (partition by a) as s from t2
14863union all
14864select 3 as n, a, c, sum(b) as s from t2 group by a
14865) as t
14866where t.a > 2 and t.c in ('aa','bb','cc');
14867n	a	c	s
148681	3	aa	92
148691	3	aa	92
148701	3	bb	40
148711	4	aa	15
148721	7	bb	126
148731	7	bb	126
148741	7	bb	126
148751	7	cc	28
148761	7	cc	28
148772	3	aa	132
148782	3	aa	132
148792	3	bb	132
148802	4	aa	139
148812	7	bb	154
148822	7	bb	154
148832	7	bb	154
148842	7	cc	154
148852	7	cc	154
148863	3	aa	132
148873	7	cc	154
14888select * from
14889(
14890select 1 as n, a, c, sum(b) over (partition by a,c) as s from t2
14891union all
14892select 2 as n, a, c, sum(b) over (partition by a) as s from t2
14893union all
14894select 3 as n, a, c, sum(b) as s from t2 group by a
14895) as t
14896where t.a > 2 and t.c in ('aa','bb','cc');
14897n	a	c	s
148981	3	aa	92
148991	3	aa	92
149001	3	bb	40
149011	4	aa	15
149021	7	bb	126
149031	7	bb	126
149041	7	bb	126
149051	7	cc	28
149061	7	cc	28
149072	3	aa	132
149082	3	aa	132
149092	3	bb	132
149102	4	aa	139
149112	7	bb	154
149122	7	bb	154
149132	7	bb	154
149142	7	cc	154
149152	7	cc	154
149163	3	aa	132
149173	7	cc	154
14918explain select * from
14919(
14920select 1 as n, a, c, sum(b) over (partition by a,c) as s from t2
14921union all
14922select 2 as n, a, c, sum(b) over (partition by a) as s from t2
14923union all
14924select 3 as n, a, c, sum(b) as s from t2 group by a
14925) as t
14926where t.a > 2 and t.c in ('aa','bb','cc');
14927id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
149281	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	48	Using where
149292	DERIVED	t2	ALL	idx	NULL	NULL	NULL	20	Using where; Using temporary
149303	UNION	t2	ALL	idx	NULL	NULL	NULL	20	Using where; Using temporary
149314	UNION	t2	ALL	idx	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
14932explain format=json select * from
14933(
14934select 1 as n, a, c, sum(b) over (partition by a,c) as s from t2
14935union all
14936select 2 as n, a, c, sum(b) over (partition by a) as s from t2
14937union all
14938select 3 as n, a, c, sum(b) as s from t2 group by a
14939) as t
14940where t.a > 2 and t.c in ('aa','bb','cc');
14941EXPLAIN
14942{
14943  "query_block": {
14944    "select_id": 1,
14945    "table": {
14946      "table_name": "<derived2>",
14947      "access_type": "ALL",
14948      "rows": 48,
14949      "filtered": 100,
14950      "attached_condition": "t.a > 2 and t.c in ('aa','bb','cc')",
14951      "materialized": {
14952        "query_block": {
14953          "union_result": {
14954            "table_name": "<union2,3,4>",
14955            "access_type": "ALL",
14956            "query_specifications": [
14957              {
14958                "query_block": {
14959                  "select_id": 2,
14960                  "window_functions_computation": {
14961                    "sorts": {
14962                      "filesort": {
14963                        "sort_key": "t2.a, t2.c"
14964                      }
14965                    },
14966                    "temporary_table": {
14967                      "table": {
14968                        "table_name": "t2",
14969                        "access_type": "ALL",
14970                        "possible_keys": ["idx"],
14971                        "rows": 20,
14972                        "filtered": 80,
14973                        "attached_condition": "t2.a > 2 and t2.c in ('aa','bb','cc')"
14974                      }
14975                    }
14976                  }
14977                }
14978              },
14979              {
14980                "query_block": {
14981                  "select_id": 3,
14982                  "operation": "UNION",
14983                  "window_functions_computation": {
14984                    "sorts": {
14985                      "filesort": {
14986                        "sort_key": "t2.a"
14987                      }
14988                    },
14989                    "temporary_table": {
14990                      "table": {
14991                        "table_name": "t2",
14992                        "access_type": "ALL",
14993                        "possible_keys": ["idx"],
14994                        "rows": 20,
14995                        "filtered": 80,
14996                        "attached_condition": "t2.a > 2"
14997                      }
14998                    }
14999                  }
15000                }
15001              },
15002              {
15003                "query_block": {
15004                  "select_id": 4,
15005                  "operation": "UNION",
15006                  "having_condition": "t2.c in ('aa','bb','cc')",
15007                  "filesort": {
15008                    "sort_key": "t2.a",
15009                    "temporary_table": {
15010                      "table": {
15011                        "table_name": "t2",
15012                        "access_type": "ALL",
15013                        "possible_keys": ["idx"],
15014                        "rows": 20,
15015                        "filtered": 80,
15016                        "attached_condition": "t2.a > 2"
15017                      }
15018                    }
15019                  }
15020                }
15021              }
15022            ]
15023          }
15024        }
15025      }
15026    }
15027  }
15028}
15029set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from (select a, c,
15030sum(b) over (partition by a,c) as sum_b,
15031avg(b) over (partition by a,c) as avg_b
15032from t2 ) as t
15033where t.a > 2 and t.c in ('aa','bb','cc');
15034a	c	sum_b	avg_b
150353	aa	92	46.0000
150363	aa	92	46.0000
150373	bb	40	40.0000
150384	aa	15	15.0000
150397	bb	126	42.0000
150407	bb	126	42.0000
150417	bb	126	42.0000
150427	cc	28	14.0000
150437	cc	28	14.0000
15044select * from (select a, c,
15045sum(b) over (partition by a,c) as sum_b,
15046avg(b) over (partition by a,c) as avg_b
15047from t2 ) as t
15048where t.a > 2 and t.c in ('aa','bb','cc');
15049a	c	sum_b	avg_b
150503	aa	92	46.0000
150513	aa	92	46.0000
150523	bb	40	40.0000
150534	aa	15	15.0000
150547	bb	126	42.0000
150557	bb	126	42.0000
150567	bb	126	42.0000
150577	cc	28	14.0000
150587	cc	28	14.0000
15059explain select * from (select a, c,
15060sum(b) over (partition by a,c) as sum_b,
15061avg(b) over (partition by a,c) as avg_b
15062from t2 ) as t
15063where t.a > 2 and t.c in ('aa','bb','cc');
15064id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
150651	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	16	Using where
150662	DERIVED	t2	ALL	idx	NULL	NULL	NULL	20	Using where; Using temporary
15067explain format=json select * from (select a, c,
15068sum(b) over (partition by a,c) as sum_b,
15069avg(b) over (partition by a,c) as avg_b
15070from t2 ) as t
15071where t.a > 2 and t.c in ('aa','bb','cc');
15072EXPLAIN
15073{
15074  "query_block": {
15075    "select_id": 1,
15076    "table": {
15077      "table_name": "<derived2>",
15078      "access_type": "ALL",
15079      "rows": 16,
15080      "filtered": 100,
15081      "attached_condition": "t.a > 2 and t.c in ('aa','bb','cc')",
15082      "materialized": {
15083        "query_block": {
15084          "select_id": 2,
15085          "window_functions_computation": {
15086            "sorts": {
15087              "filesort": {
15088                "sort_key": "t2.a, t2.c"
15089              }
15090            },
15091            "temporary_table": {
15092              "table": {
15093                "table_name": "t2",
15094                "access_type": "ALL",
15095                "possible_keys": ["idx"],
15096                "rows": 20,
15097                "filtered": 80,
15098                "attached_condition": "t2.a > 2 and t2.c in ('aa','bb','cc')"
15099              }
15100            }
15101          }
15102        }
15103      }
15104    }
15105  }
15106}
15107set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from (select a, c,
15108sum(b) over (partition by a,c) as sum_b,
15109avg(b) over (partition by a) as avg_b
15110from t2 ) as t
15111where t.a > 2 and t.c in ('aa','bb','cc');
15112a	c	sum_b	avg_b
151133	aa	92	44.0000
151143	aa	92	44.0000
151153	bb	40	44.0000
151164	aa	15	46.3333
151177	bb	126	30.8000
151187	bb	126	30.8000
151197	bb	126	30.8000
151207	cc	28	30.8000
151217	cc	28	30.8000
15122select * from (select a, c,
15123sum(b) over (partition by a,c) as sum_b,
15124avg(b) over (partition by a) as avg_b
15125from t2 ) as t
15126where t.a > 2 and t.c in ('aa','bb','cc');
15127a	c	sum_b	avg_b
151283	aa	92	44.0000
151293	aa	92	44.0000
151303	bb	40	44.0000
151314	aa	15	46.3333
151327	bb	126	30.8000
151337	bb	126	30.8000
151347	bb	126	30.8000
151357	cc	28	30.8000
151367	cc	28	30.8000
15137explain select * from (select a, c,
15138sum(b) over (partition by a,c) as sum_b,
15139avg(b) over (partition by a) as avg_b
15140from t2 ) as t
15141where t.a > 2 and t.c in ('aa','bb','cc');
15142id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
151431	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	16	Using where
151442	DERIVED	t2	ALL	idx	NULL	NULL	NULL	20	Using where; Using temporary
15145explain format=json select * from (select a, c,
15146sum(b) over (partition by a,c) as sum_b,
15147avg(b) over (partition by a) as avg_b
15148from t2 ) as t
15149where t.a > 2 and t.c in ('aa','bb','cc');
15150EXPLAIN
15151{
15152  "query_block": {
15153    "select_id": 1,
15154    "table": {
15155      "table_name": "<derived2>",
15156      "access_type": "ALL",
15157      "rows": 16,
15158      "filtered": 100,
15159      "attached_condition": "t.a > 2 and t.c in ('aa','bb','cc')",
15160      "materialized": {
15161        "query_block": {
15162          "select_id": 2,
15163          "window_functions_computation": {
15164            "sorts": {
15165              "filesort": {
15166                "sort_key": "t2.a, t2.c"
15167              }
15168            },
15169            "temporary_table": {
15170              "table": {
15171                "table_name": "t2",
15172                "access_type": "ALL",
15173                "possible_keys": ["idx"],
15174                "rows": 20,
15175                "filtered": 80,
15176                "attached_condition": "t2.a > 2"
15177              }
15178            }
15179          }
15180        }
15181      }
15182    }
15183  }
15184}
15185set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from (select a, c,
15186sum(b) over (partition by a,c) as sum_b,
15187avg(b) over (partition by c) as avg_b
15188from t2 ) as t
15189where t.a > 2 and t.c in ('aa','bb','cc');
15190a	c	sum_b	avg_b
151913	aa	92	35.4000
151923	aa	92	35.4000
151933	bb	40	36.5000
151944	aa	15	35.4000
151957	bb	126	36.5000
151967	bb	126	36.5000
151977	bb	126	36.5000
151987	cc	28	14.0000
151997	cc	28	14.0000
15200select * from (select a, c,
15201sum(b) over (partition by a,c) as sum_b,
15202avg(b) over (partition by c) as avg_b
15203from t2 ) as t
15204where t.a > 2 and t.c in ('aa','bb','cc');
15205a	c	sum_b	avg_b
152063	aa	92	35.4000
152073	aa	92	35.4000
152083	bb	40	36.5000
152094	aa	15	35.4000
152107	bb	126	36.5000
152117	bb	126	36.5000
152127	bb	126	36.5000
152137	cc	28	14.0000
152147	cc	28	14.0000
15215explain select * from (select a, c,
15216sum(b) over (partition by a,c) as sum_b,
15217avg(b) over (partition by c) as avg_b
15218from t2 ) as t
15219where t.a > 2 and t.c in ('aa','bb','cc');
15220id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
152211	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where
152222	DERIVED	t2	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary
15223explain format=json select * from (select a, c,
15224sum(b) over (partition by a,c) as sum_b,
15225avg(b) over (partition by c) as avg_b
15226from t2 ) as t
15227where t.a > 2 and t.c in ('aa','bb','cc');
15228EXPLAIN
15229{
15230  "query_block": {
15231    "select_id": 1,
15232    "table": {
15233      "table_name": "<derived2>",
15234      "access_type": "ALL",
15235      "rows": 20,
15236      "filtered": 100,
15237      "attached_condition": "t.a > 2 and t.c in ('aa','bb','cc')",
15238      "materialized": {
15239        "query_block": {
15240          "select_id": 2,
15241          "window_functions_computation": {
15242            "sorts": {
15243              "filesort": {
15244                "sort_key": "t2.a, t2.c"
15245              },
15246              "filesort": {
15247                "sort_key": "t2.c"
15248              }
15249            },
15250            "temporary_table": {
15251              "table": {
15252                "table_name": "t2",
15253                "access_type": "ALL",
15254                "rows": 20,
15255                "filtered": 100,
15256                "attached_condition": "t2.c in ('aa','bb','cc')"
15257              }
15258            }
15259          }
15260        }
15261      }
15262    }
15263  }
15264}
15265drop table t1,t2;
15266set optimizer_switch= @save_optimizer_switch;
15267#
15268# MDEV-13369: Optimization for equi-joins of grouping derived tables
15269#             (Splitting derived tables / views with GROUP BY)
15270# MDEV-13389: Optimization for equi-joins of derived tables with WF
15271#             (Splitting derived tables / views with window functions)
15272#
15273create table t1 (a int, b int, index idx_b(b)) engine=myisam;
15274insert into t1 values
15275(8,3), (5,7), (1,2), (2,1), (9,7), (7,5), (2,2), (7,3),
15276(9,3), (8,1), (4,5), (2,3);
15277create table t2 (a int, b int, c char(127), index idx_a(a)) engine=myisam;
15278insert into t2 values
15279(7,10,'x'), (1,20,'a'), (2,23,'b'), (7,18,'z'), (1,30,'c'),
15280(4,71,'d'), (3,15,'x'), (7,82,'y'), (8,12,'t'), (4,15,'b'),
15281(11,33,'a'), (10,42,'u'), (4,53,'p'), (10,17,'r'), (2,90,'x'),
15282(17,10,'s'), (11,20,'v'), (12,23,'y'), (17,18,'a'), (11,30,'d'),
15283(24,71,'h'), (23,15,'i'), (27,82,'k'), (28,12,'p'), (24,15,'q'),
15284(31,33,'f'), (30,42,'h'), (40,53,'m'), (30,17,'o'), (21,90,'b'),
15285(37,10,'e'), (31,20,'g'), (32,23,'f'), (37,18,'n'), (41,30,'l'),
15286(54,71,'j'), (53,15,'w'), (57,82,'z'), (58,12,'k'), (54,15,'p'),
15287(61,33,'c'), (60,42,'a'), (62,53,'x'), (67,17,'g'), (64,90,'v');
15288insert into t2 select a+10, b+10, concat(c,'f') from t2;
15289analyze table t1,t2;
15290Table	Op	Msg_type	Msg_text
15291test.t1	analyze	status	Engine-independent statistics collected
15292test.t1	analyze	status	OK
15293test.t2	analyze	status	Engine-independent statistics collected
15294test.t2	analyze	status	OK
15295set statement optimizer_switch='split_materialized=off' for select t1.a,t.s,t.m
15296from t1 join
15297(select a, sum(t2.b) as s, min(t2.c) as m from t2 group by t2.a) t
15298on t1.a=t.a
15299where t1.b < 3;
15300a	s	m
153012	113	b
153028	12	t
153031	50	a
153042	113	b
15305select t1.a,t.s,t.m
15306from t1 join
15307(select a, sum(t2.b) as s, min(t2.c) as m from t2 group by t2.a) t
15308on t1.a=t.a
15309where t1.b < 3;
15310a	s	m
153112	113	b
153128	12	t
153131	50	a
153142	113	b
15315explain extended select t1.a,t.s,t.m
15316from t1 join
15317(select a, sum(t2.b) as s, min(t2.c) as m from t2 group by t2.a) t
15318on t1.a=t.a
15319where t1.b < 3;
15320id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
153211	PRIMARY	t1	range	idx_b	idx_b	5	NULL	4	100.00	Using index condition; Using where
153221	PRIMARY	<derived2>	ref	key0	key0	5	test.t1.a	2	100.00
153232	LATERAL DERIVED	t2	ref	idx_a	idx_a	5	test.t1.a	1	100.00
15324Warnings:
15325Note	1003	/* select#1 */ select `test`.`t1`.`a` AS `a`,`t`.`s` AS `s`,`t`.`m` AS `m` from `test`.`t1` join (/* select#2 */ select `test`.`t2`.`a` AS `a`,sum(`test`.`t2`.`b`) AS `s`,min(`test`.`t2`.`c`) AS `m` from `test`.`t2` where `test`.`t2`.`a` = `test`.`t1`.`a` group by `test`.`t2`.`a`) `t` where `t`.`a` = `test`.`t1`.`a` and `test`.`t1`.`b` < 3
15326explain format=json select t1.a,t.s,t.m
15327from t1 join
15328(select a, sum(t2.b) as s, min(t2.c) as m from t2 group by t2.a) t
15329on t1.a=t.a
15330where t1.b < 3;
15331EXPLAIN
15332{
15333  "query_block": {
15334    "select_id": 1,
15335    "table": {
15336      "table_name": "t1",
15337      "access_type": "range",
15338      "possible_keys": ["idx_b"],
15339      "key": "idx_b",
15340      "key_length": "5",
15341      "used_key_parts": ["b"],
15342      "rows": 4,
15343      "filtered": 100,
15344      "index_condition": "t1.b < 3",
15345      "attached_condition": "t1.a is not null"
15346    },
15347    "table": {
15348      "table_name": "<derived2>",
15349      "access_type": "ref",
15350      "possible_keys": ["key0"],
15351      "key": "key0",
15352      "key_length": "5",
15353      "used_key_parts": ["a"],
15354      "ref": ["test.t1.a"],
15355      "rows": 2,
15356      "filtered": 100,
15357      "materialized": {
15358        "lateral": 1,
15359        "query_block": {
15360          "select_id": 2,
15361          "outer_ref_condition": "t1.a is not null",
15362          "table": {
15363            "table_name": "t2",
15364            "access_type": "ref",
15365            "possible_keys": ["idx_a"],
15366            "key": "idx_a",
15367            "key_length": "5",
15368            "used_key_parts": ["a"],
15369            "ref": ["test.t1.a"],
15370            "rows": 1,
15371            "filtered": 100
15372          }
15373        }
15374      }
15375    }
15376  }
15377}
15378prepare stmt from "select t1.a,t.s,t.m
15379from t1 join
15380(select a, sum(t2.b) as s, min(t2.c) as m from t2 group by t2.a) t
15381on t1.a=t.a
15382where t1.b < 3";
15383execute stmt;
15384a	s	m
153852	113	b
153868	12	t
153871	50	a
153882	113	b
15389execute stmt;
15390a	s	m
153912	113	b
153928	12	t
153931	50	a
153942	113	b
15395deallocate prepare stmt;
15396set statement optimizer_switch='split_materialized=off' for select t1.a,t.s,t.m
15397from t1 join
15398(select a, sum(t2.b) as s, min(t2.b) as m from t2 group by t2.a) t
15399on t1.a=t.a
15400where t1.b <= 5;
15401a	s	m
154028	12	12
154031	50	20
154042	113	23
154057	110	10
154062	113	23
154077	110	10
154088	12	12
154094	139	15
154102	113	23
15411select t1.a,t.s,t.m
15412from t1 join
15413(select a, sum(t2.b) as s, min(t2.b) as m from t2 group by t2.a) t
15414on t1.a=t.a
15415where t1.b <= 5;
15416a	s	m
154178	12	12
154181	50	20
154192	113	23
154207	110	10
154212	113	23
154227	110	10
154238	12	12
154244	139	15
154252	113	23
15426explain extended select t1.a,t.s,t.m
15427from t1 join
15428(select a, sum(t2.b) as s, min(t2.b) as m from t2 group by t2.a) t
15429on t1.a=t.a
15430where t1.b <= 5;
15431id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
154321	PRIMARY	t1	ALL	idx_b	NULL	NULL	NULL	12	83.33	Using where
154331	PRIMARY	<derived2>	ref	key0	key0	5	test.t1.a	2	100.00
154342	LATERAL DERIVED	t2	ref	idx_a	idx_a	5	test.t1.a	1	100.00
15435Warnings:
15436Note	1003	/* select#1 */ select `test`.`t1`.`a` AS `a`,`t`.`s` AS `s`,`t`.`m` AS `m` from `test`.`t1` join (/* select#2 */ select `test`.`t2`.`a` AS `a`,sum(`test`.`t2`.`b`) AS `s`,min(`test`.`t2`.`b`) AS `m` from `test`.`t2` where `test`.`t2`.`a` = `test`.`t1`.`a` group by `test`.`t2`.`a`) `t` where `t`.`a` = `test`.`t1`.`a` and `test`.`t1`.`b` <= 5
15437explain format=json select t1.a,t.s,t.m
15438from t1 join
15439(select a, sum(t2.b) as s, min(t2.b) as m from t2 group by t2.a) t
15440on t1.a=t.a
15441where t1.b <= 5;
15442EXPLAIN
15443{
15444  "query_block": {
15445    "select_id": 1,
15446    "table": {
15447      "table_name": "t1",
15448      "access_type": "ALL",
15449      "possible_keys": ["idx_b"],
15450      "rows": 12,
15451      "filtered": 83.33333588,
15452      "attached_condition": "t1.b <= 5 and t1.a is not null"
15453    },
15454    "table": {
15455      "table_name": "<derived2>",
15456      "access_type": "ref",
15457      "possible_keys": ["key0"],
15458      "key": "key0",
15459      "key_length": "5",
15460      "used_key_parts": ["a"],
15461      "ref": ["test.t1.a"],
15462      "rows": 2,
15463      "filtered": 100,
15464      "materialized": {
15465        "lateral": 1,
15466        "query_block": {
15467          "select_id": 2,
15468          "outer_ref_condition": "t1.a is not null",
15469          "table": {
15470            "table_name": "t2",
15471            "access_type": "ref",
15472            "possible_keys": ["idx_a"],
15473            "key": "idx_a",
15474            "key_length": "5",
15475            "used_key_parts": ["a"],
15476            "ref": ["test.t1.a"],
15477            "rows": 1,
15478            "filtered": 100
15479          }
15480        }
15481      }
15482    }
15483  }
15484}
15485prepare stmt from "select t1.a,t.s,t.m
15486from t1 join
15487(select a, sum(t2.b) as s, min(t2.b) as m from t2 group by t2.a) t
15488on t1.a=t.a
15489where t1.b <= 5";
15490execute stmt;
15491a	s	m
154928	12	12
154931	50	20
154942	113	23
154957	110	10
154962	113	23
154977	110	10
154988	12	12
154994	139	15
155002	113	23
15501execute stmt;
15502a	s	m
155038	12	12
155041	50	20
155052	113	23
155067	110	10
155072	113	23
155087	110	10
155098	12	12
155104	139	15
155112	113	23
15512deallocate prepare stmt;
15513delete from t1 where t1.b between 2 and 5;
15514set statement optimizer_switch='split_materialized=off' for select t1.a,t.max,t.min
15515from t1 left join
15516(select a, max(t2.b) max, min(t2.b) min from t2 group by t2.a) t
15517on t1.a=t.a;
15518a	max	min
155195	NULL	NULL
155202	90	23
155219	NULL	NULL
155228	12	12
15523select t1.a,t.max,t.min
15524from t1 left join
15525(select a, max(t2.b) max, min(t2.b) min from t2 group by t2.a) t
15526on t1.a=t.a;
15527a	max	min
155285	NULL	NULL
155292	90	23
155309	NULL	NULL
155318	12	12
15532explain extended select t1.a,t.max,t.min
15533from t1 left join
15534(select a, max(t2.b) max, min(t2.b) min from t2 group by t2.a) t
15535on t1.a=t.a;
15536id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
155371	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	12	100.00
155381	PRIMARY	<derived2>	ref	key0	key0	5	test.t1.a	9	100.00	Using where
155392	DERIVED	t2	ALL	idx_a	NULL	NULL	NULL	90	100.00	Using temporary; Using filesort
15540Warnings:
15541Note	1003	/* select#1 */ select `test`.`t1`.`a` AS `a`,`t`.`max` AS `max`,`t`.`min` AS `min` from `test`.`t1` left join (/* select#2 */ select `test`.`t2`.`a` AS `a`,max(`test`.`t2`.`b`) AS `max`,min(`test`.`t2`.`b`) AS `min` from `test`.`t2` group by `test`.`t2`.`a`) `t` on(`t`.`a` = `test`.`t1`.`a` and `test`.`t1`.`a` is not null) where 1
15542explain format=json select t1.a,t.max,t.min
15543from t1 left join
15544(select a, max(t2.b) max, min(t2.b) min from t2 group by t2.a) t
15545on t1.a=t.a;
15546EXPLAIN
15547{
15548  "query_block": {
15549    "select_id": 1,
15550    "const_condition": "1",
15551    "table": {
15552      "table_name": "t1",
15553      "access_type": "ALL",
15554      "rows": 12,
15555      "filtered": 100
15556    },
15557    "table": {
15558      "table_name": "<derived2>",
15559      "access_type": "ref",
15560      "possible_keys": ["key0"],
15561      "key": "key0",
15562      "key_length": "5",
15563      "used_key_parts": ["a"],
15564      "ref": ["test.t1.a"],
15565      "rows": 9,
15566      "filtered": 100,
15567      "attached_condition": "trigcond(trigcond(t1.a is not null))",
15568      "materialized": {
15569        "query_block": {
15570          "select_id": 2,
15571          "filesort": {
15572            "sort_key": "t2.a",
15573            "temporary_table": {
15574              "table": {
15575                "table_name": "t2",
15576                "access_type": "ALL",
15577                "possible_keys": ["idx_a"],
15578                "rows": 90,
15579                "filtered": 100
15580              }
15581            }
15582          }
15583        }
15584      }
15585    }
15586  }
15587}
15588create table t3 (a int, b int, c char(127), index idx_b(b)) engine=myisam;
15589insert into t3 values
15590(8,11,'aa'), (5,15,'cc'), (1,14,'bb'), (2,12,'aa'), (7,17,'cc'),
15591(7,18,'aa'), (2,11,'aa'), (7,10,'bb'), (3,11,'dd'), (4,12,'ee'),
15592(5,14,'dd'), (9,12,'ee');
15593create table t4 (a int, b int, c char(127), index idx(a,c)) engine=myisam;
15594insert into t4 values
15595(7,10,'cc'), (1,20,'aa'), (2,23,'bb'), (7,18,'cc'), (1,30,'bb'),
15596(4,71,'xx'), (3,15,'aa'), (7,82,'aa'), (8,12,'dd'), (4,15,'aa'),
15597(11,33,'yy'), (10,42,'zz'), (4,53,'xx'), (10,17,'yy'), (7,12,'cc'),
15598(8,20,'dd'), (7,32,'bb'), (1,50,'aa'), (3,40,'bb'), (3,77,'aa');
15599insert into t4 select a+10, b+10, concat(c,'f') from t4;
15600analyze table t3,t4;
15601Table	Op	Msg_type	Msg_text
15602test.t3	analyze	status	Engine-independent statistics collected
15603test.t3	analyze	status	OK
15604test.t4	analyze	status	Engine-independent statistics collected
15605test.t4	analyze	status	OK
15606set statement optimizer_switch='split_materialized=off' for select t3.a,t3.c,t.max,t.min
15607from t3 join
15608(select a, c, max(b) max, min(b) min from t4 group by a,c) t
15609on t3.a=t.a and t3.c=t.c
15610where t3.b > 15;
15611a	c	max	min
156127	cc	18	10
156137	aa	82	82
15614select t3.a,t3.c,t.max,t.min
15615from t3 join
15616(select a, c, max(b) max, min(b) min from t4 group by a,c) t
15617on t3.a=t.a and t3.c=t.c
15618where t3.b > 15;
15619a	c	max	min
156207	cc	18	10
156217	aa	82	82
15622explain extended select t3.a,t3.c,t.max,t.min
15623from t3 join
15624(select a, c, max(b) max, min(b) min from t4 group by a,c) t
15625on t3.a=t.a and t3.c=t.c
15626where t3.b > 15;
15627id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
156281	PRIMARY	t3	range	idx_b	idx_b	5	NULL	2	100.00	Using index condition; Using where
156291	PRIMARY	<derived2>	ref	key0	key0	133	test.t3.a,test.t3.c	2	100.00
156302	LATERAL DERIVED	t4	ref	idx	idx	133	test.t3.a,test.t3.c	1	100.00
15631Warnings:
15632Note	1003	/* select#1 */ select `test`.`t3`.`a` AS `a`,`test`.`t3`.`c` AS `c`,`t`.`max` AS `max`,`t`.`min` AS `min` from `test`.`t3` join (/* select#2 */ select `test`.`t4`.`a` AS `a`,`test`.`t4`.`c` AS `c`,max(`test`.`t4`.`b`) AS `max`,min(`test`.`t4`.`b`) AS `min` from `test`.`t4` where `test`.`t4`.`a` = `test`.`t3`.`a` and `test`.`t4`.`c` = `test`.`t3`.`c` group by `test`.`t4`.`a`,`test`.`t4`.`c`) `t` where `t`.`a` = `test`.`t3`.`a` and `t`.`c` = `test`.`t3`.`c` and `test`.`t3`.`b` > 15
15633explain format=json select t3.a,t3.c,t.max,t.min
15634from t3 join
15635(select a, c, max(b) max, min(b) min from t4 group by a,c) t
15636on t3.a=t.a and t3.c=t.c
15637where t3.b > 15;
15638EXPLAIN
15639{
15640  "query_block": {
15641    "select_id": 1,
15642    "table": {
15643      "table_name": "t3",
15644      "access_type": "range",
15645      "possible_keys": ["idx_b"],
15646      "key": "idx_b",
15647      "key_length": "5",
15648      "used_key_parts": ["b"],
15649      "rows": 2,
15650      "filtered": 100,
15651      "index_condition": "t3.b > 15",
15652      "attached_condition": "t3.a is not null and t3.c is not null"
15653    },
15654    "table": {
15655      "table_name": "<derived2>",
15656      "access_type": "ref",
15657      "possible_keys": ["key0"],
15658      "key": "key0",
15659      "key_length": "133",
15660      "used_key_parts": ["a", "c"],
15661      "ref": ["test.t3.a", "test.t3.c"],
15662      "rows": 2,
15663      "filtered": 100,
15664      "materialized": {
15665        "lateral": 1,
15666        "query_block": {
15667          "select_id": 2,
15668          "outer_ref_condition": "t3.a is not null and t3.c is not null",
15669          "table": {
15670            "table_name": "t4",
15671            "access_type": "ref",
15672            "possible_keys": ["idx"],
15673            "key": "idx",
15674            "key_length": "133",
15675            "used_key_parts": ["a", "c"],
15676            "ref": ["test.t3.a", "test.t3.c"],
15677            "rows": 1,
15678            "filtered": 100
15679          }
15680        }
15681      }
15682    }
15683  }
15684}
15685set statement optimizer_switch='split_materialized=off' for select t3.a,t3.c,t.max,t.min
15686from t3 join
15687(select a, c, max(b) max, min(b) min from t4 group by a,c) t
15688on t3.a=t.a and t3.c=t.c
15689where t3.b <= 15;
15690a	c	max	min
156911	bb	30	30
156927	bb	32	32
15693select t3.a,t3.c,t.max,t.min
15694from t3 join
15695(select a, c, max(b) max, min(b) min from t4 group by a,c) t
15696on t3.a=t.a and t3.c=t.c
15697where t3.b <= 15;
15698a	c	max	min
156991	bb	30	30
157007	bb	32	32
15701explain extended select t3.a,t3.c,t.max,t.min
15702from t3 join
15703(select a, c, max(b) max, min(b) min from t4 group by a,c) t
15704on t3.a=t.a and t3.c=t.c
15705where t3.b <= 15;
15706id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
157071	PRIMARY	t3	ALL	idx_b	NULL	NULL	NULL	12	83.33	Using where
157081	PRIMARY	<derived2>	ref	key0	key0	133	test.t3.a,test.t3.c	4	100.00
157092	DERIVED	t4	ALL	idx	NULL	NULL	NULL	40	100.00	Using temporary; Using filesort
15710Warnings:
15711Note	1003	/* select#1 */ select `test`.`t3`.`a` AS `a`,`test`.`t3`.`c` AS `c`,`t`.`max` AS `max`,`t`.`min` AS `min` from `test`.`t3` join (/* select#2 */ select `test`.`t4`.`a` AS `a`,`test`.`t4`.`c` AS `c`,max(`test`.`t4`.`b`) AS `max`,min(`test`.`t4`.`b`) AS `min` from `test`.`t4` group by `test`.`t4`.`a`,`test`.`t4`.`c`) `t` where `t`.`a` = `test`.`t3`.`a` and `t`.`c` = `test`.`t3`.`c` and `test`.`t3`.`b` <= 15
15712explain format=json select t3.a,t3.c,t.max,t.min
15713from t3 join
15714(select a, c, max(b) max, min(b) min from t4 group by a,c) t
15715on t3.a=t.a and t3.c=t.c
15716where t3.b <= 15;
15717EXPLAIN
15718{
15719  "query_block": {
15720    "select_id": 1,
15721    "table": {
15722      "table_name": "t3",
15723      "access_type": "ALL",
15724      "possible_keys": ["idx_b"],
15725      "rows": 12,
15726      "filtered": 83.33333588,
15727      "attached_condition": "t3.b <= 15 and t3.a is not null and t3.c is not null"
15728    },
15729    "table": {
15730      "table_name": "<derived2>",
15731      "access_type": "ref",
15732      "possible_keys": ["key0"],
15733      "key": "key0",
15734      "key_length": "133",
15735      "used_key_parts": ["a", "c"],
15736      "ref": ["test.t3.a", "test.t3.c"],
15737      "rows": 4,
15738      "filtered": 100,
15739      "materialized": {
15740        "query_block": {
15741          "select_id": 2,
15742          "filesort": {
15743            "sort_key": "t4.a, t4.c",
15744            "temporary_table": {
15745              "table": {
15746                "table_name": "t4",
15747                "access_type": "ALL",
15748                "possible_keys": ["idx"],
15749                "rows": 40,
15750                "filtered": 100
15751              }
15752            }
15753          }
15754        }
15755      }
15756    }
15757  }
15758}
15759set statement optimizer_switch='split_materialized=off' for select t3.a,t3.c,t.max,t.min
15760from t3 join
15761(select a, c, max(b) max, min(b) min from t4 group by c,a) t
15762on t3.a=t.a and t3.c=t.c
15763where t3.b > 15;
15764a	c	max	min
157657	cc	18	10
157667	aa	82	82
15767select t3.a,t3.c,t.max,t.min
15768from t3 join
15769(select a, c, max(b) max, min(b) min from t4 group by c,a) t
15770on t3.a=t.a and t3.c=t.c
15771where t3.b > 15;
15772a	c	max	min
157737	cc	18	10
157747	aa	82	82
15775explain extended select t3.a,t3.c,t.max,t.min
15776from t3 join
15777(select a, c, max(b) max, min(b) min from t4 group by c,a) t
15778on t3.a=t.a and t3.c=t.c
15779where t3.b > 15;
15780id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
157811	PRIMARY	t3	range	idx_b	idx_b	5	NULL	2	100.00	Using index condition; Using where
157821	PRIMARY	<derived2>	ref	key0	key0	133	test.t3.a,test.t3.c	2	100.00
157832	LATERAL DERIVED	t4	ref	idx	idx	133	test.t3.a,test.t3.c	1	100.00
15784Warnings:
15785Note	1003	/* select#1 */ select `test`.`t3`.`a` AS `a`,`test`.`t3`.`c` AS `c`,`t`.`max` AS `max`,`t`.`min` AS `min` from `test`.`t3` join (/* select#2 */ select `test`.`t4`.`a` AS `a`,`test`.`t4`.`c` AS `c`,max(`test`.`t4`.`b`) AS `max`,min(`test`.`t4`.`b`) AS `min` from `test`.`t4` where `test`.`t4`.`a` = `test`.`t3`.`a` and `test`.`t4`.`c` = `test`.`t3`.`c` group by `test`.`t4`.`c`,`test`.`t4`.`a`) `t` where `t`.`a` = `test`.`t3`.`a` and `t`.`c` = `test`.`t3`.`c` and `test`.`t3`.`b` > 15
15786explain format=json select t3.a,t3.c,t.max,t.min
15787from t3 join
15788(select a, c, max(b) max, min(b) min from t4 group by c,a) t
15789on t3.a=t.a and t3.c=t.c
15790where t3.b > 15;
15791EXPLAIN
15792{
15793  "query_block": {
15794    "select_id": 1,
15795    "table": {
15796      "table_name": "t3",
15797      "access_type": "range",
15798      "possible_keys": ["idx_b"],
15799      "key": "idx_b",
15800      "key_length": "5",
15801      "used_key_parts": ["b"],
15802      "rows": 2,
15803      "filtered": 100,
15804      "index_condition": "t3.b > 15",
15805      "attached_condition": "t3.a is not null and t3.c is not null"
15806    },
15807    "table": {
15808      "table_name": "<derived2>",
15809      "access_type": "ref",
15810      "possible_keys": ["key0"],
15811      "key": "key0",
15812      "key_length": "133",
15813      "used_key_parts": ["a", "c"],
15814      "ref": ["test.t3.a", "test.t3.c"],
15815      "rows": 2,
15816      "filtered": 100,
15817      "materialized": {
15818        "lateral": 1,
15819        "query_block": {
15820          "select_id": 2,
15821          "outer_ref_condition": "t3.a is not null and t3.c is not null",
15822          "table": {
15823            "table_name": "t4",
15824            "access_type": "ref",
15825            "possible_keys": ["idx"],
15826            "key": "idx",
15827            "key_length": "133",
15828            "used_key_parts": ["a", "c"],
15829            "ref": ["test.t3.a", "test.t3.c"],
15830            "rows": 1,
15831            "filtered": 100
15832          }
15833        }
15834      }
15835    }
15836  }
15837}
15838set statement optimizer_switch='split_materialized=off' for select t3.a,t3.c,t.max,t.min
15839from t3 join
15840(select a, c, max(b) max, min(b) min from t4 group by c,a) t
15841on t3.a=t.a and t3.c=t.c
15842where t3.b <= 15;
15843a	c	max	min
158441	bb	30	30
158457	bb	32	32
15846select t3.a,t3.c,t.max,t.min
15847from t3 join
15848(select a, c, max(b) max, min(b) min from t4 group by c,a) t
15849on t3.a=t.a and t3.c=t.c
15850where t3.b <= 15;
15851a	c	max	min
158521	bb	30	30
158537	bb	32	32
15854explain extended select t3.a,t3.c,t.max,t.min
15855from t3 join
15856(select a, c, max(b) max, min(b) min from t4 group by c,a) t
15857on t3.a=t.a and t3.c=t.c
15858where t3.b <= 15;
15859id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
158601	PRIMARY	t3	ALL	idx_b	NULL	NULL	NULL	12	83.33	Using where
158611	PRIMARY	<derived2>	ref	key0	key0	133	test.t3.a,test.t3.c	4	100.00
158622	DERIVED	t4	ALL	idx	NULL	NULL	NULL	40	100.00	Using temporary; Using filesort
15863Warnings:
15864Note	1003	/* select#1 */ select `test`.`t3`.`a` AS `a`,`test`.`t3`.`c` AS `c`,`t`.`max` AS `max`,`t`.`min` AS `min` from `test`.`t3` join (/* select#2 */ select `test`.`t4`.`a` AS `a`,`test`.`t4`.`c` AS `c`,max(`test`.`t4`.`b`) AS `max`,min(`test`.`t4`.`b`) AS `min` from `test`.`t4` group by `test`.`t4`.`c`,`test`.`t4`.`a`) `t` where `t`.`a` = `test`.`t3`.`a` and `t`.`c` = `test`.`t3`.`c` and `test`.`t3`.`b` <= 15
15865explain format=json select t3.a,t3.c,t.max,t.min
15866from t3 join
15867(select a, c, max(b) max, min(b) min from t4 group by c,a) t
15868on t3.a=t.a and t3.c=t.c
15869where t3.b <= 15;
15870EXPLAIN
15871{
15872  "query_block": {
15873    "select_id": 1,
15874    "table": {
15875      "table_name": "t3",
15876      "access_type": "ALL",
15877      "possible_keys": ["idx_b"],
15878      "rows": 12,
15879      "filtered": 83.33333588,
15880      "attached_condition": "t3.b <= 15 and t3.a is not null and t3.c is not null"
15881    },
15882    "table": {
15883      "table_name": "<derived2>",
15884      "access_type": "ref",
15885      "possible_keys": ["key0"],
15886      "key": "key0",
15887      "key_length": "133",
15888      "used_key_parts": ["a", "c"],
15889      "ref": ["test.t3.a", "test.t3.c"],
15890      "rows": 4,
15891      "filtered": 100,
15892      "materialized": {
15893        "query_block": {
15894          "select_id": 2,
15895          "filesort": {
15896            "sort_key": "t4.c, t4.a",
15897            "temporary_table": {
15898              "table": {
15899                "table_name": "t4",
15900                "access_type": "ALL",
15901                "possible_keys": ["idx"],
15902                "rows": 40,
15903                "filtered": 100
15904              }
15905            }
15906          }
15907        }
15908      }
15909    }
15910  }
15911}
15912drop index idx_a on t2;
15913create index idx on t2(c,b);
15914create index idx_a on t3(a);
15915create index idx_c on t4(c);
15916insert into t3 select a+10, b+10, concat(c,'f') from t3;
15917insert into t3 select a+100, b+100, concat(c,'g')  from t3;
15918insert into t4 select a+100, b+100, concat(c,'g') from t4;
15919insert into t4 select a+1000, b+1000, concat(c,'h') from t4;
15920analyze table t2,t3,t4;
15921Table	Op	Msg_type	Msg_text
15922test.t2	analyze	status	Engine-independent statistics collected
15923test.t2	analyze	status	OK
15924test.t3	analyze	status	Engine-independent statistics collected
15925test.t3	analyze	status	OK
15926test.t4	analyze	status	Engine-independent statistics collected
15927test.t4	analyze	status	OK
15928set statement optimizer_switch='split_materialized=off' for select t2.a,t2.b,t2.c,t.c as t_c,t.max,t.min
15929from t2, t3, (select c, max(b) max, min(b) min from t4 group by c) t
15930where t2.b between 80 and 85 and t2.c in ('y','z') and t2.a=t3.a and t3.c=t.c;
15931a	b	c	t_c	max	min
159327	82	y	aa	82	15
159337	82	y	bb	40	23
159347	82	y	cc	18	10
15935select t2.a,t2.b,t2.c,t.c as t_c,t.max,t.min
15936from t2, t3, (select c, max(b) max, min(b) min from t4 group by c) t
15937where t2.b between 80 and 85 and t2.c in ('y','z') and t2.a=t3.a and t3.c=t.c;
15938a	b	c	t_c	max	min
159397	82	y	aa	82	15
159407	82	y	bb	40	23
159417	82	y	cc	18	10
15942explain extended select t2.a,t2.b,t2.c,t.c as t_c,t.max,t.min
15943from t2, t3, (select c, max(b) max, min(b) min from t4 group by c) t
15944where t2.b between 80 and 85 and t2.c in ('y','z') and t2.a=t3.a and t3.c=t.c;
15945id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
159461	PRIMARY	t2	range	idx	idx	133	NULL	2	100.00	Using index condition; Using where
159471	PRIMARY	t3	ref	idx_a	idx_a	5	test.t2.a	1	100.00	Using where
159481	PRIMARY	<derived2>	ref	key0	key0	128	test.t3.c	2	100.00
159492	LATERAL DERIVED	t4	ref	idx_c	idx_c	128	test.t3.c	2	100.00
15950Warnings:
15951Note	1003	/* select#1 */ select `test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b`,`test`.`t2`.`c` AS `c`,`t`.`c` AS `t_c`,`t`.`max` AS `max`,`t`.`min` AS `min` from `test`.`t2` join `test`.`t3` join (/* select#2 */ select `test`.`t4`.`c` AS `c`,max(`test`.`t4`.`b`) AS `max`,min(`test`.`t4`.`b`) AS `min` from `test`.`t4` where `test`.`t4`.`c` = `test`.`t3`.`c` group by `test`.`t4`.`c`) `t` where `test`.`t3`.`a` = `test`.`t2`.`a` and `t`.`c` = `test`.`t3`.`c` and `test`.`t2`.`b` between 80 and 85 and `test`.`t2`.`c` in ('y','z')
15952explain format=json select t2.a,t2.b,t2.c,t.c as t_c,t.max,t.min
15953from t2, t3, (select c, max(b) max, min(b) min from t4 group by c) t
15954where t2.b between 80 and 85 and t2.c in ('y','z') and t2.a=t3.a and t3.c=t.c;
15955EXPLAIN
15956{
15957  "query_block": {
15958    "select_id": 1,
15959    "table": {
15960      "table_name": "t2",
15961      "access_type": "range",
15962      "possible_keys": ["idx"],
15963      "key": "idx",
15964      "key_length": "133",
15965      "used_key_parts": ["c", "b"],
15966      "rows": 2,
15967      "filtered": 100,
15968      "index_condition": "t2.b between 80 and 85 and t2.c in ('y','z')",
15969      "attached_condition": "t2.a is not null"
15970    },
15971    "table": {
15972      "table_name": "t3",
15973      "access_type": "ref",
15974      "possible_keys": ["idx_a"],
15975      "key": "idx_a",
15976      "key_length": "5",
15977      "used_key_parts": ["a"],
15978      "ref": ["test.t2.a"],
15979      "rows": 1,
15980      "filtered": 100,
15981      "attached_condition": "t3.c is not null"
15982    },
15983    "table": {
15984      "table_name": "<derived2>",
15985      "access_type": "ref",
15986      "possible_keys": ["key0"],
15987      "key": "key0",
15988      "key_length": "128",
15989      "used_key_parts": ["c"],
15990      "ref": ["test.t3.c"],
15991      "rows": 2,
15992      "filtered": 100,
15993      "materialized": {
15994        "lateral": 1,
15995        "query_block": {
15996          "select_id": 2,
15997          "outer_ref_condition": "t3.c is not null",
15998          "table": {
15999            "table_name": "t4",
16000            "access_type": "ref",
16001            "possible_keys": ["idx_c"],
16002            "key": "idx_c",
16003            "key_length": "128",
16004            "used_key_parts": ["c"],
16005            "ref": ["test.t3.c"],
16006            "rows": 2,
16007            "filtered": 100
16008          }
16009        }
16010      }
16011    }
16012  }
16013}
16014set statement optimizer_switch='split_materialized=off' for select t2.a,t2.b,t2.c,t.c as t_c,t.max,t.min
16015from t2, t3, (select c, max(b) max, min(b) min from t4 group by c) t
16016where t2.b < 40 and t2.a=t3.a and t3.c=t.c;
16017a	b	c	t_c	max	min
160187	10	x	cc	18	10
160197	10	x	aa	82	15
160207	10	x	bb	40	23
160211	20	a	bb	40	23
160222	23	b	aa	82	15
160232	23	b	aa	82	15
160247	18	z	cc	18	10
160257	18	z	aa	82	15
160267	18	z	bb	40	23
160271	30	c	bb	40	23
160283	15	x	dd	20	12
160298	12	t	aa	82	15
1603011	33	a	bbf	50	33
1603117	10	s	ccf	28	20
1603217	10	s	aaf	92	25
1603317	10	s	bbf	50	33
1603411	20	v	bbf	50	33
1603512	23	y	aaf	92	25
1603612	23	y	aaf	92	25
1603717	18	a	ccf	28	20
1603817	18	a	aaf	92	25
1603917	18	a	bbf	50	33
1604011	30	d	bbf	50	33
1604117	20	xf	ccf	28	20
1604217	20	xf	aaf	92	25
1604317	20	xf	bbf	50	33
1604411	30	af	bbf	50	33
1604512	33	bf	aaf	92	25
1604612	33	bf	aaf	92	25
1604717	28	zf	ccf	28	20
1604817	28	zf	aaf	92	25
1604917	28	zf	bbf	50	33
1605013	25	xf	ddf	30	22
1605118	22	tf	aaf	92	25
16052select t2.a,t2.b,t2.c,t.c as t_c,t.max,t.min
16053from t2, t3, (select c, max(b) max, min(b) min from t4 group by c) t
16054where t2.b < 40 and t2.a=t3.a and t3.c=t.c;
16055a	b	c	t_c	max	min
160567	10	x	cc	18	10
160577	10	x	aa	82	15
160587	10	x	bb	40	23
160591	20	a	bb	40	23
160602	23	b	aa	82	15
160612	23	b	aa	82	15
160627	18	z	cc	18	10
160637	18	z	aa	82	15
160647	18	z	bb	40	23
160651	30	c	bb	40	23
160663	15	x	dd	20	12
160678	12	t	aa	82	15
1606811	33	a	bbf	50	33
1606917	10	s	ccf	28	20
1607017	10	s	aaf	92	25
1607117	10	s	bbf	50	33
1607211	20	v	bbf	50	33
1607312	23	y	aaf	92	25
1607412	23	y	aaf	92	25
1607517	18	a	ccf	28	20
1607617	18	a	aaf	92	25
1607717	18	a	bbf	50	33
1607811	30	d	bbf	50	33
1607917	20	xf	ccf	28	20
1608017	20	xf	aaf	92	25
1608117	20	xf	bbf	50	33
1608211	30	af	bbf	50	33
1608312	33	bf	aaf	92	25
1608412	33	bf	aaf	92	25
1608517	28	zf	ccf	28	20
1608617	28	zf	aaf	92	25
1608717	28	zf	bbf	50	33
1608813	25	xf	ddf	30	22
1608918	22	tf	aaf	92	25
16090explain extended select t2.a,t2.b,t2.c,t.c as t_c,t.max,t.min
16091from t2, t3, (select c, max(b) max, min(b) min from t4 group by c) t
16092where t2.b < 40 and t2.a=t3.a and t3.c=t.c;
16093id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
160941	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	90	63.28	Using where
160951	PRIMARY	t3	ref	idx_a	idx_a	5	test.t2.a	1	100.00	Using where
160961	PRIMARY	<derived2>	ref	key0	key0	128	test.t3.c	10	100.00
160972	DERIVED	t4	ALL	idx_c	NULL	NULL	NULL	160	100.00	Using temporary; Using filesort
16098Warnings:
16099Note	1003	/* select#1 */ select `test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b`,`test`.`t2`.`c` AS `c`,`t`.`c` AS `t_c`,`t`.`max` AS `max`,`t`.`min` AS `min` from `test`.`t2` join `test`.`t3` join (/* select#2 */ select `test`.`t4`.`c` AS `c`,max(`test`.`t4`.`b`) AS `max`,min(`test`.`t4`.`b`) AS `min` from `test`.`t4` group by `test`.`t4`.`c`) `t` where `test`.`t3`.`a` = `test`.`t2`.`a` and `t`.`c` = `test`.`t3`.`c` and `test`.`t2`.`b` < 40
16100explain format=json select t2.a,t2.b,t2.c,t.c as t_c,t.max,t.min
16101from t2, t3, (select c, max(b) max, min(b) min from t4 group by c) t
16102where t2.b < 40 and t2.a=t3.a and t3.c=t.c;
16103EXPLAIN
16104{
16105  "query_block": {
16106    "select_id": 1,
16107    "table": {
16108      "table_name": "t2",
16109      "access_type": "ALL",
16110      "rows": 90,
16111      "filtered": 63.28125,
16112      "attached_condition": "t2.b < 40 and t2.a is not null"
16113    },
16114    "table": {
16115      "table_name": "t3",
16116      "access_type": "ref",
16117      "possible_keys": ["idx_a"],
16118      "key": "idx_a",
16119      "key_length": "5",
16120      "used_key_parts": ["a"],
16121      "ref": ["test.t2.a"],
16122      "rows": 1,
16123      "filtered": 100,
16124      "attached_condition": "t3.c is not null"
16125    },
16126    "table": {
16127      "table_name": "<derived2>",
16128      "access_type": "ref",
16129      "possible_keys": ["key0"],
16130      "key": "key0",
16131      "key_length": "128",
16132      "used_key_parts": ["c"],
16133      "ref": ["test.t3.c"],
16134      "rows": 10,
16135      "filtered": 100,
16136      "materialized": {
16137        "query_block": {
16138          "select_id": 2,
16139          "filesort": {
16140            "sort_key": "t4.c",
16141            "temporary_table": {
16142              "table": {
16143                "table_name": "t4",
16144                "access_type": "ALL",
16145                "possible_keys": ["idx_c"],
16146                "rows": 160,
16147                "filtered": 100
16148              }
16149            }
16150          }
16151        }
16152      }
16153    }
16154  }
16155}
16156set statement optimizer_switch='split_materialized=off' for select *
16157from t2, t3, (select c, b, sum(b) over (partition by c) from t4 ) t
16158where t2.b between 80 and 85 and t2.c in ('y','z') and t2.a=t3.a and t3.c=t.c;
16159a	b	c	a	b	c	c	b	sum(b) over (partition by c)
161607	82	y	7	10	bb	bb	23	125
161617	82	y	7	10	bb	bb	30	125
161627	82	y	7	10	bb	bb	32	125
161637	82	y	7	10	bb	bb	40	125
161647	82	y	7	17	cc	cc	10	40
161657	82	y	7	17	cc	cc	12	40
161667	82	y	7	17	cc	cc	18	40
161677	82	y	7	18	aa	aa	15	259
161687	82	y	7	18	aa	aa	15	259
161697	82	y	7	18	aa	aa	20	259
161707	82	y	7	18	aa	aa	50	259
161717	82	y	7	18	aa	aa	77	259
161727	82	y	7	18	aa	aa	82	259
16173select *
16174from t2, t3, (select c, b, sum(b) over (partition by c) from t4 ) t
16175where t2.b between 80 and 85 and t2.c in ('y','z') and t2.a=t3.a and t3.c=t.c;
16176a	b	c	a	b	c	c	b	sum(b) over (partition by c)
161777	82	y	7	10	bb	bb	23	125
161787	82	y	7	10	bb	bb	30	125
161797	82	y	7	10	bb	bb	32	125
161807	82	y	7	10	bb	bb	40	125
161817	82	y	7	17	cc	cc	10	40
161827	82	y	7	17	cc	cc	12	40
161837	82	y	7	17	cc	cc	18	40
161847	82	y	7	18	aa	aa	15	259
161857	82	y	7	18	aa	aa	15	259
161867	82	y	7	18	aa	aa	20	259
161877	82	y	7	18	aa	aa	50	259
161887	82	y	7	18	aa	aa	77	259
161897	82	y	7	18	aa	aa	82	259
16190explain extended select *
16191from t2, t3, (select c, b, sum(b) over (partition by c) from t4 ) t
16192where t2.b between 80 and 85 and t2.c in ('y','z') and t2.a=t3.a and t3.c=t.c;
16193id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
161941	PRIMARY	t2	range	idx	idx	133	NULL	2	100.00	Using index condition; Using where
161951	PRIMARY	t3	ref	idx_a	idx_a	5	test.t2.a	1	100.00	Using where
161961	PRIMARY	<derived2>	ref	key0	key0	128	test.t3.c	2	100.00
161972	LATERAL DERIVED	t4	ref	idx_c	idx_c	128	test.t3.c	2	100.00	Using temporary
16198Warnings:
16199Note	1003	/* select#1 */ select `test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b`,`test`.`t2`.`c` AS `c`,`test`.`t3`.`a` AS `a`,`test`.`t3`.`b` AS `b`,`test`.`t3`.`c` AS `c`,`t`.`c` AS `c`,`t`.`b` AS `b`,`t`.`sum(b) over (partition by c)` AS `sum(b) over (partition by c)` from `test`.`t2` join `test`.`t3` join (/* select#2 */ select `test`.`t4`.`c` AS `c`,`test`.`t4`.`b` AS `b`,sum(`test`.`t4`.`b`) over ( partition by `test`.`t4`.`c`) AS `sum(b) over (partition by c)` from `test`.`t4` where `test`.`t4`.`c` = `test`.`t3`.`c`) `t` where `test`.`t3`.`a` = `test`.`t2`.`a` and `t`.`c` = `test`.`t3`.`c` and `test`.`t2`.`b` between 80 and 85 and `test`.`t2`.`c` in ('y','z')
16200explain format=json select *
16201from t2, t3, (select c, b, sum(b) over (partition by c) from t4 ) t
16202where t2.b between 80 and 85 and t2.c in ('y','z') and t2.a=t3.a and t3.c=t.c;
16203EXPLAIN
16204{
16205  "query_block": {
16206    "select_id": 1,
16207    "table": {
16208      "table_name": "t2",
16209      "access_type": "range",
16210      "possible_keys": ["idx"],
16211      "key": "idx",
16212      "key_length": "133",
16213      "used_key_parts": ["c", "b"],
16214      "rows": 2,
16215      "filtered": 100,
16216      "index_condition": "t2.b between 80 and 85 and t2.c in ('y','z')",
16217      "attached_condition": "t2.a is not null"
16218    },
16219    "table": {
16220      "table_name": "t3",
16221      "access_type": "ref",
16222      "possible_keys": ["idx_a"],
16223      "key": "idx_a",
16224      "key_length": "5",
16225      "used_key_parts": ["a"],
16226      "ref": ["test.t2.a"],
16227      "rows": 1,
16228      "filtered": 100,
16229      "attached_condition": "t3.c is not null"
16230    },
16231    "table": {
16232      "table_name": "<derived2>",
16233      "access_type": "ref",
16234      "possible_keys": ["key0"],
16235      "key": "key0",
16236      "key_length": "128",
16237      "used_key_parts": ["c"],
16238      "ref": ["test.t3.c"],
16239      "rows": 2,
16240      "filtered": 100,
16241      "materialized": {
16242        "lateral": 1,
16243        "query_block": {
16244          "select_id": 2,
16245          "outer_ref_condition": "t3.c is not null",
16246          "window_functions_computation": {
16247            "sorts": {
16248              "filesort": {
16249                "sort_key": "t4.c"
16250              }
16251            },
16252            "temporary_table": {
16253              "table": {
16254                "table_name": "t4",
16255                "access_type": "ref",
16256                "possible_keys": ["idx_c"],
16257                "key": "idx_c",
16258                "key_length": "128",
16259                "used_key_parts": ["c"],
16260                "ref": ["test.t3.c"],
16261                "rows": 2,
16262                "filtered": 100
16263              }
16264            }
16265          }
16266        }
16267      }
16268    }
16269  }
16270}
16271set statement optimizer_switch='split_materialized=off' for select *
16272from t2, t3, (select c, b, sum(b) over (partition by c) from t4 ) t
16273where t2.b < 40 and t2.a=t3.a and t3.c=t.c;
16274a	b	c	a	b	c	c	b	sum(b) over (partition by c)
162751	20	a	1	14	bb	bb	23	125
162761	20	a	1	14	bb	bb	30	125
162771	20	a	1	14	bb	bb	32	125
162781	20	a	1	14	bb	bb	40	125
162791	30	c	1	14	bb	bb	23	125
162801	30	c	1	14	bb	bb	30	125
162811	30	c	1	14	bb	bb	32	125
162821	30	c	1	14	bb	bb	40	125
1628311	20	v	11	24	bbf	bbf	33	165
1628411	20	v	11	24	bbf	bbf	40	165
1628511	20	v	11	24	bbf	bbf	42	165
1628611	20	v	11	24	bbf	bbf	50	165
1628711	30	af	11	24	bbf	bbf	33	165
1628811	30	af	11	24	bbf	bbf	40	165
1628911	30	af	11	24	bbf	bbf	42	165
1629011	30	af	11	24	bbf	bbf	50	165
1629111	30	d	11	24	bbf	bbf	33	165
1629211	30	d	11	24	bbf	bbf	40	165
1629311	30	d	11	24	bbf	bbf	42	165
1629411	30	d	11	24	bbf	bbf	50	165
1629511	33	a	11	24	bbf	bbf	33	165
1629611	33	a	11	24	bbf	bbf	40	165
1629711	33	a	11	24	bbf	bbf	42	165
1629811	33	a	11	24	bbf	bbf	50	165
1629912	23	y	12	21	aaf	aaf	25	319
1630012	23	y	12	21	aaf	aaf	25	319
1630112	23	y	12	21	aaf	aaf	30	319
1630212	23	y	12	21	aaf	aaf	60	319
1630312	23	y	12	21	aaf	aaf	87	319
1630412	23	y	12	21	aaf	aaf	92	319
1630512	23	y	12	22	aaf	aaf	25	319
1630612	23	y	12	22	aaf	aaf	25	319
1630712	23	y	12	22	aaf	aaf	30	319
1630812	23	y	12	22	aaf	aaf	60	319
1630912	23	y	12	22	aaf	aaf	87	319
1631012	23	y	12	22	aaf	aaf	92	319
1631112	33	bf	12	21	aaf	aaf	25	319
1631212	33	bf	12	21	aaf	aaf	25	319
1631312	33	bf	12	21	aaf	aaf	30	319
1631412	33	bf	12	21	aaf	aaf	60	319
1631512	33	bf	12	21	aaf	aaf	87	319
1631612	33	bf	12	21	aaf	aaf	92	319
1631712	33	bf	12	22	aaf	aaf	25	319
1631812	33	bf	12	22	aaf	aaf	25	319
1631912	33	bf	12	22	aaf	aaf	30	319
1632012	33	bf	12	22	aaf	aaf	60	319
1632112	33	bf	12	22	aaf	aaf	87	319
1632212	33	bf	12	22	aaf	aaf	92	319
1632313	25	xf	13	21	ddf	ddf	22	52
1632413	25	xf	13	21	ddf	ddf	30	52
1632517	10	s	17	20	bbf	bbf	33	165
1632617	10	s	17	20	bbf	bbf	40	165
1632717	10	s	17	20	bbf	bbf	42	165
1632817	10	s	17	20	bbf	bbf	50	165
1632917	10	s	17	27	ccf	ccf	20	70
1633017	10	s	17	27	ccf	ccf	22	70
1633117	10	s	17	27	ccf	ccf	28	70
1633217	10	s	17	28	aaf	aaf	25	319
1633317	10	s	17	28	aaf	aaf	25	319
1633417	10	s	17	28	aaf	aaf	30	319
1633517	10	s	17	28	aaf	aaf	60	319
1633617	10	s	17	28	aaf	aaf	87	319
1633717	10	s	17	28	aaf	aaf	92	319
1633817	18	a	17	20	bbf	bbf	33	165
1633917	18	a	17	20	bbf	bbf	40	165
1634017	18	a	17	20	bbf	bbf	42	165
1634117	18	a	17	20	bbf	bbf	50	165
1634217	18	a	17	27	ccf	ccf	20	70
1634317	18	a	17	27	ccf	ccf	22	70
1634417	18	a	17	27	ccf	ccf	28	70
1634517	18	a	17	28	aaf	aaf	25	319
1634617	18	a	17	28	aaf	aaf	25	319
1634717	18	a	17	28	aaf	aaf	30	319
1634817	18	a	17	28	aaf	aaf	60	319
1634917	18	a	17	28	aaf	aaf	87	319
1635017	18	a	17	28	aaf	aaf	92	319
1635117	20	xf	17	20	bbf	bbf	33	165
1635217	20	xf	17	20	bbf	bbf	40	165
1635317	20	xf	17	20	bbf	bbf	42	165
1635417	20	xf	17	20	bbf	bbf	50	165
1635517	20	xf	17	27	ccf	ccf	20	70
1635617	20	xf	17	27	ccf	ccf	22	70
1635717	20	xf	17	27	ccf	ccf	28	70
1635817	20	xf	17	28	aaf	aaf	25	319
1635917	20	xf	17	28	aaf	aaf	25	319
1636017	20	xf	17	28	aaf	aaf	30	319
1636117	20	xf	17	28	aaf	aaf	60	319
1636217	20	xf	17	28	aaf	aaf	87	319
1636317	20	xf	17	28	aaf	aaf	92	319
1636417	28	zf	17	20	bbf	bbf	33	165
1636517	28	zf	17	20	bbf	bbf	40	165
1636617	28	zf	17	20	bbf	bbf	42	165
1636717	28	zf	17	20	bbf	bbf	50	165
1636817	28	zf	17	27	ccf	ccf	20	70
1636917	28	zf	17	27	ccf	ccf	22	70
1637017	28	zf	17	27	ccf	ccf	28	70
1637117	28	zf	17	28	aaf	aaf	25	319
1637217	28	zf	17	28	aaf	aaf	25	319
1637317	28	zf	17	28	aaf	aaf	30	319
1637417	28	zf	17	28	aaf	aaf	60	319
1637517	28	zf	17	28	aaf	aaf	87	319
1637617	28	zf	17	28	aaf	aaf	92	319
1637718	22	tf	18	21	aaf	aaf	25	319
1637818	22	tf	18	21	aaf	aaf	25	319
1637918	22	tf	18	21	aaf	aaf	30	319
1638018	22	tf	18	21	aaf	aaf	60	319
1638118	22	tf	18	21	aaf	aaf	87	319
1638218	22	tf	18	21	aaf	aaf	92	319
163832	23	b	2	11	aa	aa	15	259
163842	23	b	2	11	aa	aa	15	259
163852	23	b	2	11	aa	aa	20	259
163862	23	b	2	11	aa	aa	50	259
163872	23	b	2	11	aa	aa	77	259
163882	23	b	2	11	aa	aa	82	259
163892	23	b	2	12	aa	aa	15	259
163902	23	b	2	12	aa	aa	15	259
163912	23	b	2	12	aa	aa	20	259
163922	23	b	2	12	aa	aa	50	259
163932	23	b	2	12	aa	aa	77	259
163942	23	b	2	12	aa	aa	82	259
163953	15	x	3	11	dd	dd	12	32
163963	15	x	3	11	dd	dd	20	32
163977	10	x	7	10	bb	bb	23	125
163987	10	x	7	10	bb	bb	30	125
163997	10	x	7	10	bb	bb	32	125
164007	10	x	7	10	bb	bb	40	125
164017	10	x	7	17	cc	cc	10	40
164027	10	x	7	17	cc	cc	12	40
164037	10	x	7	17	cc	cc	18	40
164047	10	x	7	18	aa	aa	15	259
164057	10	x	7	18	aa	aa	15	259
164067	10	x	7	18	aa	aa	20	259
164077	10	x	7	18	aa	aa	50	259
164087	10	x	7	18	aa	aa	77	259
164097	10	x	7	18	aa	aa	82	259
164107	18	z	7	10	bb	bb	23	125
164117	18	z	7	10	bb	bb	30	125
164127	18	z	7	10	bb	bb	32	125
164137	18	z	7	10	bb	bb	40	125
164147	18	z	7	17	cc	cc	10	40
164157	18	z	7	17	cc	cc	12	40
164167	18	z	7	17	cc	cc	18	40
164177	18	z	7	18	aa	aa	15	259
164187	18	z	7	18	aa	aa	15	259
164197	18	z	7	18	aa	aa	20	259
164207	18	z	7	18	aa	aa	50	259
164217	18	z	7	18	aa	aa	77	259
164227	18	z	7	18	aa	aa	82	259
164238	12	t	8	11	aa	aa	15	259
164248	12	t	8	11	aa	aa	15	259
164258	12	t	8	11	aa	aa	20	259
164268	12	t	8	11	aa	aa	50	259
164278	12	t	8	11	aa	aa	77	259
164288	12	t	8	11	aa	aa	82	259
16429select *
16430from t2, t3, (select c, b, sum(b) over (partition by c) from t4 ) t
16431where t2.b < 40 and t2.a=t3.a and t3.c=t.c;
16432a	b	c	a	b	c	c	b	sum(b) over (partition by c)
164331	20	a	1	14	bb	bb	23	125
164341	20	a	1	14	bb	bb	30	125
164351	20	a	1	14	bb	bb	32	125
164361	20	a	1	14	bb	bb	40	125
164371	30	c	1	14	bb	bb	23	125
164381	30	c	1	14	bb	bb	30	125
164391	30	c	1	14	bb	bb	32	125
164401	30	c	1	14	bb	bb	40	125
1644111	20	v	11	24	bbf	bbf	33	165
1644211	20	v	11	24	bbf	bbf	40	165
1644311	20	v	11	24	bbf	bbf	42	165
1644411	20	v	11	24	bbf	bbf	50	165
1644511	30	af	11	24	bbf	bbf	33	165
1644611	30	af	11	24	bbf	bbf	40	165
1644711	30	af	11	24	bbf	bbf	42	165
1644811	30	af	11	24	bbf	bbf	50	165
1644911	30	d	11	24	bbf	bbf	33	165
1645011	30	d	11	24	bbf	bbf	40	165
1645111	30	d	11	24	bbf	bbf	42	165
1645211	30	d	11	24	bbf	bbf	50	165
1645311	33	a	11	24	bbf	bbf	33	165
1645411	33	a	11	24	bbf	bbf	40	165
1645511	33	a	11	24	bbf	bbf	42	165
1645611	33	a	11	24	bbf	bbf	50	165
1645712	23	y	12	21	aaf	aaf	25	319
1645812	23	y	12	21	aaf	aaf	25	319
1645912	23	y	12	21	aaf	aaf	30	319
1646012	23	y	12	21	aaf	aaf	60	319
1646112	23	y	12	21	aaf	aaf	87	319
1646212	23	y	12	21	aaf	aaf	92	319
1646312	23	y	12	22	aaf	aaf	25	319
1646412	23	y	12	22	aaf	aaf	25	319
1646512	23	y	12	22	aaf	aaf	30	319
1646612	23	y	12	22	aaf	aaf	60	319
1646712	23	y	12	22	aaf	aaf	87	319
1646812	23	y	12	22	aaf	aaf	92	319
1646912	33	bf	12	21	aaf	aaf	25	319
1647012	33	bf	12	21	aaf	aaf	25	319
1647112	33	bf	12	21	aaf	aaf	30	319
1647212	33	bf	12	21	aaf	aaf	60	319
1647312	33	bf	12	21	aaf	aaf	87	319
1647412	33	bf	12	21	aaf	aaf	92	319
1647512	33	bf	12	22	aaf	aaf	25	319
1647612	33	bf	12	22	aaf	aaf	25	319
1647712	33	bf	12	22	aaf	aaf	30	319
1647812	33	bf	12	22	aaf	aaf	60	319
1647912	33	bf	12	22	aaf	aaf	87	319
1648012	33	bf	12	22	aaf	aaf	92	319
1648113	25	xf	13	21	ddf	ddf	22	52
1648213	25	xf	13	21	ddf	ddf	30	52
1648317	10	s	17	20	bbf	bbf	33	165
1648417	10	s	17	20	bbf	bbf	40	165
1648517	10	s	17	20	bbf	bbf	42	165
1648617	10	s	17	20	bbf	bbf	50	165
1648717	10	s	17	27	ccf	ccf	20	70
1648817	10	s	17	27	ccf	ccf	22	70
1648917	10	s	17	27	ccf	ccf	28	70
1649017	10	s	17	28	aaf	aaf	25	319
1649117	10	s	17	28	aaf	aaf	25	319
1649217	10	s	17	28	aaf	aaf	30	319
1649317	10	s	17	28	aaf	aaf	60	319
1649417	10	s	17	28	aaf	aaf	87	319
1649517	10	s	17	28	aaf	aaf	92	319
1649617	18	a	17	20	bbf	bbf	33	165
1649717	18	a	17	20	bbf	bbf	40	165
1649817	18	a	17	20	bbf	bbf	42	165
1649917	18	a	17	20	bbf	bbf	50	165
1650017	18	a	17	27	ccf	ccf	20	70
1650117	18	a	17	27	ccf	ccf	22	70
1650217	18	a	17	27	ccf	ccf	28	70
1650317	18	a	17	28	aaf	aaf	25	319
1650417	18	a	17	28	aaf	aaf	25	319
1650517	18	a	17	28	aaf	aaf	30	319
1650617	18	a	17	28	aaf	aaf	60	319
1650717	18	a	17	28	aaf	aaf	87	319
1650817	18	a	17	28	aaf	aaf	92	319
1650917	20	xf	17	20	bbf	bbf	33	165
1651017	20	xf	17	20	bbf	bbf	40	165
1651117	20	xf	17	20	bbf	bbf	42	165
1651217	20	xf	17	20	bbf	bbf	50	165
1651317	20	xf	17	27	ccf	ccf	20	70
1651417	20	xf	17	27	ccf	ccf	22	70
1651517	20	xf	17	27	ccf	ccf	28	70
1651617	20	xf	17	28	aaf	aaf	25	319
1651717	20	xf	17	28	aaf	aaf	25	319
1651817	20	xf	17	28	aaf	aaf	30	319
1651917	20	xf	17	28	aaf	aaf	60	319
1652017	20	xf	17	28	aaf	aaf	87	319
1652117	20	xf	17	28	aaf	aaf	92	319
1652217	28	zf	17	20	bbf	bbf	33	165
1652317	28	zf	17	20	bbf	bbf	40	165
1652417	28	zf	17	20	bbf	bbf	42	165
1652517	28	zf	17	20	bbf	bbf	50	165
1652617	28	zf	17	27	ccf	ccf	20	70
1652717	28	zf	17	27	ccf	ccf	22	70
1652817	28	zf	17	27	ccf	ccf	28	70
1652917	28	zf	17	28	aaf	aaf	25	319
1653017	28	zf	17	28	aaf	aaf	25	319
1653117	28	zf	17	28	aaf	aaf	30	319
1653217	28	zf	17	28	aaf	aaf	60	319
1653317	28	zf	17	28	aaf	aaf	87	319
1653417	28	zf	17	28	aaf	aaf	92	319
1653518	22	tf	18	21	aaf	aaf	25	319
1653618	22	tf	18	21	aaf	aaf	25	319
1653718	22	tf	18	21	aaf	aaf	30	319
1653818	22	tf	18	21	aaf	aaf	60	319
1653918	22	tf	18	21	aaf	aaf	87	319
1654018	22	tf	18	21	aaf	aaf	92	319
165412	23	b	2	11	aa	aa	15	259
165422	23	b	2	11	aa	aa	15	259
165432	23	b	2	11	aa	aa	20	259
165442	23	b	2	11	aa	aa	50	259
165452	23	b	2	11	aa	aa	77	259
165462	23	b	2	11	aa	aa	82	259
165472	23	b	2	12	aa	aa	15	259
165482	23	b	2	12	aa	aa	15	259
165492	23	b	2	12	aa	aa	20	259
165502	23	b	2	12	aa	aa	50	259
165512	23	b	2	12	aa	aa	77	259
165522	23	b	2	12	aa	aa	82	259
165533	15	x	3	11	dd	dd	12	32
165543	15	x	3	11	dd	dd	20	32
165557	10	x	7	10	bb	bb	23	125
165567	10	x	7	10	bb	bb	30	125
165577	10	x	7	10	bb	bb	32	125
165587	10	x	7	10	bb	bb	40	125
165597	10	x	7	17	cc	cc	10	40
165607	10	x	7	17	cc	cc	12	40
165617	10	x	7	17	cc	cc	18	40
165627	10	x	7	18	aa	aa	15	259
165637	10	x	7	18	aa	aa	15	259
165647	10	x	7	18	aa	aa	20	259
165657	10	x	7	18	aa	aa	50	259
165667	10	x	7	18	aa	aa	77	259
165677	10	x	7	18	aa	aa	82	259
165687	18	z	7	10	bb	bb	23	125
165697	18	z	7	10	bb	bb	30	125
165707	18	z	7	10	bb	bb	32	125
165717	18	z	7	10	bb	bb	40	125
165727	18	z	7	17	cc	cc	10	40
165737	18	z	7	17	cc	cc	12	40
165747	18	z	7	17	cc	cc	18	40
165757	18	z	7	18	aa	aa	15	259
165767	18	z	7	18	aa	aa	15	259
165777	18	z	7	18	aa	aa	20	259
165787	18	z	7	18	aa	aa	50	259
165797	18	z	7	18	aa	aa	77	259
165807	18	z	7	18	aa	aa	82	259
165818	12	t	8	11	aa	aa	15	259
165828	12	t	8	11	aa	aa	15	259
165838	12	t	8	11	aa	aa	20	259
165848	12	t	8	11	aa	aa	50	259
165858	12	t	8	11	aa	aa	77	259
165868	12	t	8	11	aa	aa	82	259
16587explain extended select *
16588from t2, t3, (select c, b, sum(b) over (partition by c) from t4 ) t
16589where t2.b < 40 and t2.a=t3.a and t3.c=t.c;
16590id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
165911	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	90	63.28	Using where
165921	PRIMARY	t3	ref	idx_a	idx_a	5	test.t2.a	1	100.00	Using where
165931	PRIMARY	<derived2>	ref	key0	key0	128	test.t3.c	10	100.00
165942	DERIVED	t4	ALL	idx_c	NULL	NULL	NULL	160	100.00	Using temporary
16595Warnings:
16596Note	1003	/* select#1 */ select `test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b`,`test`.`t2`.`c` AS `c`,`test`.`t3`.`a` AS `a`,`test`.`t3`.`b` AS `b`,`test`.`t3`.`c` AS `c`,`t`.`c` AS `c`,`t`.`b` AS `b`,`t`.`sum(b) over (partition by c)` AS `sum(b) over (partition by c)` from `test`.`t2` join `test`.`t3` join (/* select#2 */ select `test`.`t4`.`c` AS `c`,`test`.`t4`.`b` AS `b`,sum(`test`.`t4`.`b`) over ( partition by `test`.`t4`.`c`) AS `sum(b) over (partition by c)` from `test`.`t4`) `t` where `test`.`t3`.`a` = `test`.`t2`.`a` and `t`.`c` = `test`.`t3`.`c` and `test`.`t2`.`b` < 40
16597explain format=json select *
16598from t2, t3, (select c, b, sum(b) over (partition by c) from t4 ) t
16599where t2.b < 40 and t2.a=t3.a and t3.c=t.c;
16600EXPLAIN
16601{
16602  "query_block": {
16603    "select_id": 1,
16604    "table": {
16605      "table_name": "t2",
16606      "access_type": "ALL",
16607      "rows": 90,
16608      "filtered": 63.28125,
16609      "attached_condition": "t2.b < 40 and t2.a is not null"
16610    },
16611    "table": {
16612      "table_name": "t3",
16613      "access_type": "ref",
16614      "possible_keys": ["idx_a"],
16615      "key": "idx_a",
16616      "key_length": "5",
16617      "used_key_parts": ["a"],
16618      "ref": ["test.t2.a"],
16619      "rows": 1,
16620      "filtered": 100,
16621      "attached_condition": "t3.c is not null"
16622    },
16623    "table": {
16624      "table_name": "<derived2>",
16625      "access_type": "ref",
16626      "possible_keys": ["key0"],
16627      "key": "key0",
16628      "key_length": "128",
16629      "used_key_parts": ["c"],
16630      "ref": ["test.t3.c"],
16631      "rows": 10,
16632      "filtered": 100,
16633      "materialized": {
16634        "query_block": {
16635          "select_id": 2,
16636          "window_functions_computation": {
16637            "sorts": {
16638              "filesort": {
16639                "sort_key": "t4.c"
16640              }
16641            },
16642            "temporary_table": {
16643              "table": {
16644                "table_name": "t4",
16645                "access_type": "ALL",
16646                "possible_keys": ["idx_c"],
16647                "rows": 160,
16648                "filtered": 100
16649              }
16650            }
16651          }
16652        }
16653      }
16654    }
16655  }
16656}
16657drop table t1,t2,t3,t4;
16658#
16659# MDEV-13709: Optimization for semi-joins of grouping derived tables
16660#             (Splitting derived tables / views with GROUP BY)
16661#
16662CREATE TABLE t1 (i int);
16663INSERT INTO t1 VALUES (1),(9),(3);
16664CREATE TABLE t2 (a int, i int);
16665INSERT INTO t2 VALUES (1,9),(2,3),(3,7),(4,1);
16666CREATE TABLE t3 (a int, c char(127), index(c));
16667INSERT INTO t3 VALUES (1,'foo'),(3,'bar'),(4,'foo'),(2,'bar');
16668INSERT INTO t3 SELECT a, concat(c,'a') FROM t3;
16669CREATE TABLE t4 (a int, c char(127), index(a));
16670INSERT INTO t4 VALUES
16671(3,'abc'),(1,'foo'),(4,'def'),(8,'xxx'),(3,'yyy'),
16672(5,'zzz'),(9,'xyz'),(2,'yxz'),(5,'zxy'),(7,'zyx') ;
16673ANALYZE TABLE t1,t2,t3,t4;
16674Table	Op	Msg_type	Msg_text
16675test.t1	analyze	status	Engine-independent statistics collected
16676test.t1	analyze	status	OK
16677test.t2	analyze	status	Engine-independent statistics collected
16678test.t2	analyze	status	OK
16679test.t3	analyze	status	Engine-independent statistics collected
16680test.t3	analyze	status	OK
16681test.t4	analyze	status	Engine-independent statistics collected
16682test.t4	analyze	status	OK
16683CREATE VIEW v1 AS
16684SELECT c FROM t3
16685WHERE a IN ( SELECT t2.a FROM t1 JOIN t2 WHERE t1.i = t2.i ) GROUP BY c ;
16686set statement optimizer_switch='split_materialized=off' for SELECT * FROM t4 WHERE c IN ( SELECT c FROM v1 ) and a < 2;
16687a	c
166881	foo
16689SELECT * FROM t4 WHERE c IN ( SELECT c FROM v1 ) and a < 2;
16690a	c
166911	foo
16692explain extended SELECT * FROM t4 WHERE c IN ( SELECT c FROM v1 ) and a < 2;
16693id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
166941	PRIMARY	t4	range	a	a	5	NULL	1	100.00	Using index condition; Using where
166951	PRIMARY	<derived3>	ref	key0	key0	128	test.t4.c	2	100.00	FirstMatch(t4)
166963	LATERAL DERIVED	t3	ref	c	c	128	test.t4.c	2	100.00
166973	LATERAL DERIVED	<subquery4>	eq_ref	distinct_key	distinct_key	4	func	1	100.00
166984	MATERIALIZED	t1	ALL	NULL	NULL	NULL	NULL	3	100.00
166994	MATERIALIZED	t2	ALL	NULL	NULL	NULL	NULL	4	100.00	Using where; Using join buffer (flat, BNL join)
16700Warnings:
16701Note	1003	/* select#1 */ select `test`.`t4`.`a` AS `a`,`test`.`t4`.`c` AS `c` from `test`.`t4` semi join (`test`.`v1`) where `v1`.`c` = `test`.`t4`.`c` and `test`.`t4`.`a` < 2
16702explain format=json SELECT * FROM t4 WHERE c IN ( SELECT c FROM v1 ) and a < 2;
16703EXPLAIN
16704{
16705  "query_block": {
16706    "select_id": 1,
16707    "table": {
16708      "table_name": "t4",
16709      "access_type": "range",
16710      "possible_keys": ["a"],
16711      "key": "a",
16712      "key_length": "5",
16713      "used_key_parts": ["a"],
16714      "rows": 1,
16715      "filtered": 100,
16716      "index_condition": "t4.a < 2",
16717      "attached_condition": "t4.c is not null"
16718    },
16719    "table": {
16720      "table_name": "<derived3>",
16721      "access_type": "ref",
16722      "possible_keys": ["key0"],
16723      "key": "key0",
16724      "key_length": "128",
16725      "used_key_parts": ["c"],
16726      "ref": ["test.t4.c"],
16727      "rows": 2,
16728      "filtered": 100,
16729      "first_match": "t4",
16730      "materialized": {
16731        "lateral": 1,
16732        "query_block": {
16733          "select_id": 3,
16734          "const_condition": "1",
16735          "outer_ref_condition": "t4.c is not null",
16736          "table": {
16737            "table_name": "t3",
16738            "access_type": "ref",
16739            "possible_keys": ["c"],
16740            "key": "c",
16741            "key_length": "128",
16742            "used_key_parts": ["c"],
16743            "ref": ["test.t4.c"],
16744            "rows": 2,
16745            "filtered": 100
16746          },
16747          "table": {
16748            "table_name": "<subquery4>",
16749            "access_type": "eq_ref",
16750            "possible_keys": ["distinct_key"],
16751            "key": "distinct_key",
16752            "key_length": "4",
16753            "used_key_parts": ["a"],
16754            "ref": ["func"],
16755            "rows": 1,
16756            "filtered": 100,
16757            "materialized": {
16758              "unique": 1,
16759              "query_block": {
16760                "select_id": 4,
16761                "table": {
16762                  "table_name": "t1",
16763                  "access_type": "ALL",
16764                  "rows": 3,
16765                  "filtered": 100
16766                },
16767                "block-nl-join": {
16768                  "table": {
16769                    "table_name": "t2",
16770                    "access_type": "ALL",
16771                    "rows": 4,
16772                    "filtered": 100
16773                  },
16774                  "buffer_type": "flat",
16775                  "buffer_size": "65",
16776                  "join_type": "BNL",
16777                  "attached_condition": "t2.i = t1.i and t2.i = t1.i"
16778                }
16779              }
16780            }
16781          }
16782        }
16783      }
16784    }
16785  }
16786}
16787DROP VIEW v1;
16788DROP TABLE t1,t2,t3,t4;
16789#
16790# MDEV-13710: Optimization for equi-joins of grouping derived tables
16791#             (Splitting derived tables / views with GROUP BY) :
16792#             FROM list of the derived table contains constant tables
16793#
16794CREATE TABLE t1 (a int, INDEX(a)) ENGINE=MyISAM;
16795INSERT INTO t1 VALUES (9),(5),(1);
16796CREATE TABLE t2 (b int) ENGINE=MyISAM;
16797CREATE TABLE t3 (c varchar(8), d int) ENGINE=MyISAM;
16798INSERT INTO t3 VALUES ('foo',2),('bar',6);
16799CREATE VIEW v1 AS SELECT a FROM t1, t2 GROUP BY a;
16800SELECT * FROM t3
16801WHERE d IN ( SELECT * FROM v1 ) AND c LIKE 'z%' OR c IS NULL;
16802c	d
16803DROP VIEW v1;
16804DROP TABLE t1,t2,t3;
16805#
16806# MDEV-13734: Optimization for equi-joins of grouping derived tables
16807#             (Splitting derived tables / views with GROUP BY) :
16808#             derived table / view is empty
16809#
16810CREATE TABLE t1 (a int, b int, INDEX(a)) ENGINE=MyISAM;
16811CREATE TABLE t2 (c int) ENGINE=MyISAM;
16812CREATE VIEW v1 AS SELECT a, b FROM t1 STRAIGHT_JOIN t2;
16813CREATE VIEW v2 AS SELECT a, max(b) as bmax FROM v1 GROUP BY a;
16814CREATE VIEW v3 AS SELECT v2.* FROM t1 JOIN v2 ON t1.b = v2.bmax ;
16815SELECT * FROM v3 JOIN t1 ON (bmax = b);
16816a	bmax	a	b
16817DROP VIEW v1,v2,v3;
16818DROP TABLE t1,t2;
16819#
16820# MDEV-14845: Impossible where for derived with GROUP BY
16821#
16822CREATE TABLE t1 (pk INT PRIMARY KEY);
16823INSERT INTO t1 VALUES (1),(2);
16824WITH cte AS ( SELECT pk FROM t1 WHERE pk IS NULL GROUP BY pk )
16825SELECT * FROM cte;
16826pk
16827EXPLAIN EXTENDED WITH cte AS ( SELECT pk FROM t1 WHERE pk IS NULL GROUP BY pk )
16828SELECT * FROM cte;
16829id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
168301	PRIMARY	<derived2>	system	NULL	NULL	NULL	NULL	0	0.00	Const row not found
168312	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
16832Warnings:
16833Note	1003	with cte as (/* select#2 */ select `test`.`t1`.`pk` AS `pk` from `test`.`t1` where 0 group by `test`.`t1`.`pk`)/* select#1 */ select NULL AS `pk` from `cte`
16834DROP TABLE t1;
16835#
16836# MDEV-14880: assertion failure in optimizer when splitting is applied
16837#
16838CREATE TABLE t1 (pk1 INT PRIMARY KEY, f INT) ENGINE=Aria;
16839INSERT INTO t1 VALUES (1,0),(2,0);
16840CREATE TABLE t2 (pk2 INT PRIMARY KEY) ENGINE=Aria;
16841INSERT INTO t2 VALUES (1),(2),(3);
16842CREATE VIEW v2 AS SELECT pk2, COUNT(*) AS cnt FROM t2 GROUP BY pk2;
16843SELECT * FROM t1 INNER JOIN v2 ON pk1 = pk2 WHERE f <> 5;
16844pk1	f	pk2	cnt
168451	0	1	1
168462	0	2	1
16847EXPLAIN EXTENDED SELECT * FROM t1 INNER JOIN v2 ON pk1 = pk2 WHERE f <> 5;
16848id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
168491	PRIMARY	t1	ALL	PRIMARY	NULL	NULL	NULL	2	100.00	Using where
168501	PRIMARY	<derived2>	ref	key0	key0	4	test.t1.pk1	2	100.00
168512	LATERAL DERIVED	t2	eq_ref	PRIMARY	PRIMARY	4	test.t1.pk1	1	100.00	Using index
16852Warnings:
16853Note	1003	/* select#1 */ select `test`.`t1`.`pk1` AS `pk1`,`test`.`t1`.`f` AS `f`,`v2`.`pk2` AS `pk2`,`v2`.`cnt` AS `cnt` from `test`.`t1` join `test`.`v2` where `v2`.`pk2` = `test`.`t1`.`pk1` and `test`.`t1`.`f` <> 5
16854EXPLAIN FORMAT=JSON SELECT * FROM t1 INNER JOIN v2 ON pk1 = pk2 WHERE f <> 5;
16855EXPLAIN
16856{
16857  "query_block": {
16858    "select_id": 1,
16859    "table": {
16860      "table_name": "t1",
16861      "access_type": "ALL",
16862      "possible_keys": ["PRIMARY"],
16863      "rows": 2,
16864      "filtered": 100,
16865      "attached_condition": "t1.f <> 5"
16866    },
16867    "table": {
16868      "table_name": "<derived2>",
16869      "access_type": "ref",
16870      "possible_keys": ["key0"],
16871      "key": "key0",
16872      "key_length": "4",
16873      "used_key_parts": ["pk2"],
16874      "ref": ["test.t1.pk1"],
16875      "rows": 2,
16876      "filtered": 100,
16877      "materialized": {
16878        "lateral": 1,
16879        "query_block": {
16880          "select_id": 2,
16881          "table": {
16882            "table_name": "t2",
16883            "access_type": "eq_ref",
16884            "possible_keys": ["PRIMARY"],
16885            "key": "PRIMARY",
16886            "key_length": "4",
16887            "used_key_parts": ["pk2"],
16888            "ref": ["test.t1.pk1"],
16889            "rows": 1,
16890            "filtered": 100,
16891            "using_index": true
16892          }
16893        }
16894      }
16895    }
16896  }
16897}
16898DROP VIEW v2;
16899DROP TABLE t1,t2;
16900#
16901# MDEV-15017: splittable table is constant table
16902#
16903CREATE TABLE t1 (a INT) ENGINE=MyISAM;
16904CREATE TABLE t2 (pk INT, b INT, PRIMARY KEY (pk)) ENGINE=MyISAM;
16905INSERT INTO t2 VALUES (1,2),(3,4);
16906CREATE VIEW v2 AS SELECT pk, MIN(b) FROM t2 GROUP BY pk;
16907SELECT * FROM t1 LEFT JOIN v2 ON (a = pk);
16908a	pk	MIN(b)
16909DROP VIEW v2;
16910DROP TABLE t1,t2;
16911#
16912# MDEV-14994: splittable table with no rows
16913#
16914CREATE TABLE t1 (f INT PRIMARY KEY) ENGINE=MyISAM;
16915CREATE VIEW v1 AS SELECT a.* FROM t1 AS a STRAIGHT_JOIN t1 AS b;
16916CREATE VIEW v2 AS SELECT f FROM v1 GROUP BY f;
16917SELECT * FROM v1 JOIN v2 ON v1.f = v2.f;
16918f	f
16919EXPLAIN EXTENDED
16920SELECT * FROM v1 JOIN v2 ON v1.f = v2.f;
16921id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
169221	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE noticed after reading const tables
169233	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
16924Warnings:
16925Note	1003	/* select#1 */ select NULL AS `f`,`v2`.`f` AS `f` from `test`.`t1` `a` straight_join `test`.`t1` `b` join `test`.`v2` where 0
16926DROP VIEW v1,v2;
16927DROP TABLE t1;
16928#
16929# MDEV-15899: derived with WF without any key access
16930#
16931create table t1 (f1 int, f2 int, f4 int);
16932insert into t1 values
16933(3,1,1), (3,0,9), (0,1,8), (9,0,0), (3,0,9);
16934with
16935cte as (select  median(f2) over (partition by f1) as k1 from t1 order by f1),
16936cte1 as (select median(f4) over (partition by f1) as k2 from t1)
16937select k1,k2 from cte1, cte;
16938k1	k2
169390.0000000000	0.0000000000
169400.0000000000	0.0000000000
169410.0000000000	0.0000000000
169420.0000000000	0.0000000000
169430.0000000000	8.0000000000
169440.0000000000	8.0000000000
169450.0000000000	8.0000000000
169460.0000000000	8.0000000000
169470.0000000000	9.0000000000
169480.0000000000	9.0000000000
169490.0000000000	9.0000000000
169500.0000000000	9.0000000000
169510.0000000000	9.0000000000
169520.0000000000	9.0000000000
169530.0000000000	9.0000000000
169540.0000000000	9.0000000000
169550.0000000000	9.0000000000
169560.0000000000	9.0000000000
169570.0000000000	9.0000000000
169580.0000000000	9.0000000000
169591.0000000000	0.0000000000
169601.0000000000	8.0000000000
169611.0000000000	9.0000000000
169621.0000000000	9.0000000000
169631.0000000000	9.0000000000
16964explain with
16965cte as (select  median(f2) over (partition by f1) as k1 from t1 order by f1),
16966cte1 as (select median(f4) over (partition by f1) as k2 from t1)
16967select k1,k2 from cte1, cte;
16968id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
169691	PRIMARY	<derived3>	ALL	NULL	NULL	NULL	NULL	5
169701	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	5	Using join buffer (flat, BNL join)
169713	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	5	Using temporary
169722	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	5	Using temporary; Using filesort
16973drop table t1;
16974#
16975# MDEV-16104: embedded splittable materialized derived/views
16976#
16977CREATE TABLE t1 (f int PRIMARY KEY) ENGINE=MyISAM;
16978INSERT INTO t1
16979VALUES (3), (7), (1), (4), (8), (5), (9);
16980CREATE ALGORITHM=MERGE VIEW v1 AS
16981SELECT a2.*
16982FROM
16983( SELECT f, COUNT(*) as c FROM t1 GROUP BY f ) AS a1
16984JOIN
16985t1 AS a2
16986USING (f);
16987EXPLAIN EXTENDED
16988SELECT * FROM ( SELECT STRAIGHT_JOIN f, COUNT(*) as c FROM v1 GROUP BY f ) AS s;
16989id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
169901	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	7	100.00
169912	DERIVED	<derived4>	ALL	NULL	NULL	NULL	NULL	7	100.00	Using temporary; Using filesort
169922	DERIVED	a2	eq_ref	PRIMARY	PRIMARY	4	a1.f	1	100.00	Using index
169934	DERIVED	t1	index	PRIMARY	PRIMARY	4	NULL	7	100.00	Using index; Using temporary; Using filesort
16994Warnings:
16995Note	1003	/* select#1 */ select `s`.`f` AS `f`,`s`.`c` AS `c` from (/* select#2 */ select straight_join `a2`.`f` AS `f`,count(0) AS `c` from (/* select#4 */ select `test`.`t1`.`f` AS `f`,count(0) AS `c` from `test`.`t1` group by `test`.`t1`.`f`) `a1` join `test`.`t1` `a2` where `a2`.`f` = `a1`.`f` group by `a2`.`f`) `s`
16996SELECT * FROM ( SELECT STRAIGHT_JOIN f, COUNT(*) as c FROM v1 GROUP BY f ) AS s;
16997f	c
169981	1
169993	1
170004	1
170015	1
170027	1
170038	1
170049	1
17005DROP VIEW v1;
17006DROP TABLE t1;
17007#
17008# MDEV-16801: splittable materialized derived/views with
17009#             one grouping field from table without keys
17010#
17011CREATE TABLE t1 (a int, b int, INDEX idx_a(a), INDEX idx_b(b)) ENGINE=MYISAM;
17012CREATE TABLE t2 (c int) ENGINE=MYISAM;
17013CREATE TABLE t3 (d int) ENGINE=MYISAM;
17014INSERT INTO t1 VALUES
17015(77,7), (11,1), (33,3), (44,4), (8,88),
17016(78,7), (98,9), (38,3), (28,2), (79,7),
17017(58,5), (42,4), (71,7), (27,2), (91,9);
17018INSERT INTO t1 SELECT a+100, b+10 FROM t1;
17019INSERT INTO t2 VALUES
17020(100), (700), (200), (100), (200);
17021INSERT INTO t3 VALUES
17022(3), (4), (1), (8), (3);
17023ANALYZE tables t1,t2,t3;
17024Table	Op	Msg_type	Msg_text
17025test.t1	analyze	status	Engine-independent statistics collected
17026test.t1	analyze	status	OK
17027test.t2	analyze	status	Engine-independent statistics collected
17028test.t2	analyze	status	OK
17029test.t3	analyze	status	Engine-independent statistics collected
17030test.t3	analyze	status	OK
17031SELECT *
17032FROM t3,
17033(SELECT t1.b, t2.c
17034FROM  t1, t2
17035GROUP BY t1.b,t2.c) dt
17036WHERE t3.d = dt.b;
17037d	b	c
170383	3	700
170393	3	200
170403	3	100
170414	4	700
170424	4	200
170434	4	100
170441	1	700
170451	1	200
170461	1	100
170473	3	700
170483	3	200
170493	3	100
17050EXPLAIN EXTENDED SELECT *
17051FROM t3,
17052(SELECT t1.b, t2.c
17053FROM  t1, t2
17054GROUP BY t1.b,t2.c) dt
17055WHERE t3.d = dt.b;
17056id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
170571	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	5	100.00	Using where
170581	PRIMARY	<derived2>	ref	key0	key0	5	test.t3.d	2	100.00
170592	LATERAL DERIVED	t1	ref	idx_b	idx_b	5	test.t3.d	1	100.00	Using index; Using temporary; Using filesort
170602	LATERAL DERIVED	t2	ALL	NULL	NULL	NULL	NULL	5	100.00	Using join buffer (flat, BNL join)
17061Warnings:
17062Note	1003	/* select#1 */ select `test`.`t3`.`d` AS `d`,`dt`.`b` AS `b`,`dt`.`c` AS `c` from `test`.`t3` join (/* select#2 */ select `test`.`t1`.`b` AS `b`,`test`.`t2`.`c` AS `c` from `test`.`t1` join `test`.`t2` where `test`.`t1`.`b` = `test`.`t3`.`d` group by `test`.`t1`.`b`,`test`.`t2`.`c`) `dt` where `dt`.`b` = `test`.`t3`.`d`
17063DROP TABLE t1,t2,t3;
17064#
17065# MDEV-17419: splittable materialized derived/view
17066#             when join_cache_level = 4
17067#
17068set join_cache_level = 4;
17069CREATE TABLE t1 (
17070id INT UNSIGNED NOT NULL AUTO_INCREMENT,
17071username VARCHAR(50) NULL DEFAULT '0',
17072PRIMARY KEY (id)
17073) COLLATE='utf8_general_ci';
17074CREATE TABLE t2 (
17075id INT UNSIGNED NOT NULL AUTO_INCREMENT,
17076userid INT UNSIGNED NOT NULL,
17077logindate DATETIME NOT NULL,
17078PRIMARY KEY (id)
17079) COLLATE='utf8_general_ci';
17080INSERT INTO t1 (id, username) VALUES
17081(1,"user1"), (2, "user2");
17082INSERT INTO t2 (id, userid, logindate) VALUES
17083(1,1,"2015-06-19 12:17:02.828"),
17084(2,1,"2016-06-19 12:17:02.828"),
17085(3,2,"2017-06-19 12:17:02.828"),
17086(4,2,"2018-06-19 12:17:02.828");
17087EXPLAIN select * from t1 as u
17088left join
17089(select * from t2 as au group by au.userid) as auditlastlogin
17090on u.id=auditlastlogin.userid;
17091id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
170921	PRIMARY	u	ALL	NULL	NULL	NULL	NULL	2
170931	PRIMARY	<derived2>	ref	key0	key0	5	test.u.id	2
170942	DERIVED	au	ALL	NULL	NULL	NULL	NULL	4	Using temporary; Using filesort
17095select * from t1 as u
17096left join
17097(select * from t2 as au group by au.userid) as auditlastlogin
17098on u.id=auditlastlogin.userid;
17099id	username	id	userid	logindate
171001	user1	1	1	2015-06-19 12:17:02
171012	user2	3	2	2017-06-19 12:17:02
17102set join_cache_level=default;
17103DROP TABLE t1,t2;
17104#
17105# MDEV-21614: potentially splittable materialized derived/view
17106#             within materialized semi-join
17107#
17108create table t1 (
17109id int not null auto_increment primary key,
17110a int not null
17111) engine=myisam;
17112create table t2 (
17113id int not null auto_increment primary key,
17114ro_id int not null,
17115flag int not null, key (ro_id)
17116) engine=myisam;
17117insert into t1(a) select seq+100 from seq_1_to_20;
17118insert into t2(ro_id,flag) select seq, 1 from seq_1_to_20;
17119insert into t2(ro_id,flag) select seq, 0 from seq_1_to_20;
17120create view v1 as
17121select t1.* from t1 left join t2 on (t1.id = t2.ro_id AND t2.flag = 1)
17122group by t1.id;
17123select id, a from t1 where  id in (select id from v1);
17124id	a
171251	101
171262	102
171273	103
171284	104
171295	105
171306	106
171317	107
171328	108
171339	109
1713410	110
1713511	111
1713612	112
1713713	113
1713814	114
1713915	115
1714016	116
1714117	117
1714218	118
1714319	119
1714420	120
17145explain extended select id, a from t1 where  id in (select id from v1);
17146id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
171471	PRIMARY	t1	ALL	PRIMARY	NULL	NULL	NULL	20	100.00
171481	PRIMARY	<derived3>	ref	key0	key0	4	test.t1.id	2	100.00	FirstMatch(t1)
171493	LATERAL DERIVED	t1	eq_ref	PRIMARY	PRIMARY	4	test.t1.id	1	100.00
171503	LATERAL DERIVED	t2	ref	ro_id	ro_id	4	test.t1.id	1	100.00	Using where
17151Warnings:
17152Note	1003	/* select#1 */ select `test`.`t1`.`id` AS `id`,`test`.`t1`.`a` AS `a` from `test`.`t1` semi join (`test`.`v1`) where `v1`.`id` = `test`.`t1`.`id`
17153select id, a from t1
17154where id in (select id
17155from (select t1.* from t1 left join t2
17156on (t1.id = t2.ro_id AND t2.flag = 1)
17157group by t1.id) dt);
17158id	a
171591	101
171602	102
171613	103
171624	104
171635	105
171646	106
171657	107
171668	108
171679	109
1716810	110
1716911	111
1717012	112
1717113	113
1717214	114
1717315	115
1717416	116
1717517	117
1717618	118
1717719	119
1717820	120
17179explain extended select id, a from t1
17180where id in (select id
17181from (select t1.* from t1 left join t2
17182on (t1.id = t2.ro_id AND t2.flag = 1)
17183group by t1.id) dt);
17184id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
171851	PRIMARY	t1	ALL	PRIMARY	NULL	NULL	NULL	20	100.00
171861	PRIMARY	<derived3>	ref	key0	key0	4	test.t1.id	2	100.00	FirstMatch(t1)
171873	LATERAL DERIVED	t1	eq_ref	PRIMARY	PRIMARY	4	test.t1.id	1	100.00
171883	LATERAL DERIVED	t2	ref	ro_id	ro_id	4	test.t1.id	1	100.00	Using where
17189Warnings:
17190Note	1003	/* select#1 */ select `test`.`t1`.`id` AS `id`,`test`.`t1`.`a` AS `a` from `test`.`t1` semi join ((/* select#3 */ select `test`.`t1`.`id` AS `id`,`test`.`t1`.`a` AS `a` from `test`.`t1` left join `test`.`t2` on(`test`.`t2`.`ro_id` = `test`.`t1`.`id` and `test`.`t2`.`flag` = 1) where `test`.`t1`.`id` = `test`.`t1`.`id` group by `test`.`t1`.`id`) `dt`) where `dt`.`id` = `test`.`t1`.`id`
17191drop view v1;
17192drop table t1,t2;
17193#
17194# MDEV-21883: potentially splittable materialized derived
17195#             that uses a join of 32 tables
17196#
17197CREATE TABLE t (id INT NOT NULL PRIMARY KEY);
17198INSERT INTO t values (1),(2),(3);
17199set statement optimizer_switch='split_materialized=off' for SELECT t.id FROM t
17200LEFT JOIN (
17201SELECT t0.id FROM t AS t0
17202LEFT JOIN t AS t1 ON 0=1
17203LEFT JOIN t AS t2 ON 0=1
17204LEFT JOIN t AS t3 ON 0=1
17205LEFT JOIN t AS t4 ON 0=1
17206LEFT JOIN t AS t5 ON 0=1
17207LEFT JOIN t AS t6 ON 0=1
17208LEFT JOIN t AS t7 ON 0=1
17209LEFT JOIN t AS t8 ON 0=1
17210LEFT JOIN t AS t9 ON 0=1
17211LEFT JOIN t AS t10 ON 0=1
17212LEFT JOIN t AS t11 ON 0=1
17213LEFT JOIN t AS t12 ON 0=1
17214LEFT JOIN t AS t13 ON 0=1
17215LEFT JOIN t AS t14 ON 0=1
17216LEFT JOIN t AS t15 ON 0=1
17217LEFT JOIN t AS t16 ON 0=1
17218LEFT JOIN t AS t17 ON 0=1
17219LEFT JOIN t AS t18 ON 0=1
17220LEFT JOIN t AS t19 ON 0=1
17221LEFT JOIN t AS t20 ON 0=1
17222LEFT JOIN t AS t21 ON 0=1
17223LEFT JOIN t AS t22 ON 0=1
17224LEFT JOIN t AS t23 ON 0=1
17225LEFT JOIN t AS t24 ON 0=1
17226LEFT JOIN t AS t25 ON 0=1
17227LEFT JOIN t AS t26 ON 0=1
17228LEFT JOIN t AS t27 ON 0=1
17229LEFT JOIN t AS t28 ON 0=1
17230LEFT JOIN t AS t29 ON 0=1
17231LEFT JOIN t AS t30 ON 0=1
17232LEFT JOIN t AS t31 ON 0=1
17233GROUP BY t0.id) AS dt ON dt.id = t.id;
17234id
172351
172362
172373
17238set statement optimizer_switch='split_materialized=on' for SELECT t.id FROM t
17239LEFT JOIN (
17240SELECT t0.id FROM t AS t0
17241LEFT JOIN t AS t1 ON 0=1
17242LEFT JOIN t AS t2 ON 0=1
17243LEFT JOIN t AS t3 ON 0=1
17244LEFT JOIN t AS t4 ON 0=1
17245LEFT JOIN t AS t5 ON 0=1
17246LEFT JOIN t AS t6 ON 0=1
17247LEFT JOIN t AS t7 ON 0=1
17248LEFT JOIN t AS t8 ON 0=1
17249LEFT JOIN t AS t9 ON 0=1
17250LEFT JOIN t AS t10 ON 0=1
17251LEFT JOIN t AS t11 ON 0=1
17252LEFT JOIN t AS t12 ON 0=1
17253LEFT JOIN t AS t13 ON 0=1
17254LEFT JOIN t AS t14 ON 0=1
17255LEFT JOIN t AS t15 ON 0=1
17256LEFT JOIN t AS t16 ON 0=1
17257LEFT JOIN t AS t17 ON 0=1
17258LEFT JOIN t AS t18 ON 0=1
17259LEFT JOIN t AS t19 ON 0=1
17260LEFT JOIN t AS t20 ON 0=1
17261LEFT JOIN t AS t21 ON 0=1
17262LEFT JOIN t AS t22 ON 0=1
17263LEFT JOIN t AS t23 ON 0=1
17264LEFT JOIN t AS t24 ON 0=1
17265LEFT JOIN t AS t25 ON 0=1
17266LEFT JOIN t AS t26 ON 0=1
17267LEFT JOIN t AS t27 ON 0=1
17268LEFT JOIN t AS t28 ON 0=1
17269LEFT JOIN t AS t29 ON 0=1
17270LEFT JOIN t AS t30 ON 0=1
17271LEFT JOIN t AS t31 ON 0=1
17272GROUP BY t0.id) AS dt ON dt.id = t.id;
17273id
172741
172752
172763
17277DROP TABLE t;
17278#
17279# MDEV-23804: Server crashes in st_select_lex::collect_grouping_fields_for_derived
17280#
17281CREATE TABLE t1 (a INT);
17282INSERT INTO t1 VALUES (3),(4);
17283CREATE VIEW v1 AS SELECT a FROM t1 UNION VALUES (3),(4);
17284ANALYZE FORMAT=JSON SELECT * from v1 WHERE a=3;
17285ANALYZE
17286{
17287  "query_block": {
17288    "select_id": 1,
17289    "r_loops": 1,
17290    "r_total_time_ms": "REPLACED",
17291    "table": {
17292      "table_name": "<derived2>",
17293      "access_type": "ALL",
17294      "r_loops": 1,
17295      "rows": 4,
17296      "r_rows": 2,
17297      "r_table_time_ms": "REPLACED",
17298      "r_other_time_ms": "REPLACED",
17299      "filtered": 100,
17300      "r_filtered": 50,
17301      "attached_condition": "v1.a = 3",
17302      "materialized": {
17303        "query_block": {
17304          "union_result": {
17305            "table_name": "<union2,3>",
17306            "access_type": "ALL",
17307            "r_loops": 1,
17308            "r_rows": 2,
17309            "query_specifications": [
17310              {
17311                "query_block": {
17312                  "select_id": 2,
17313                  "r_loops": 1,
17314                  "r_total_time_ms": "REPLACED",
17315                  "table": {
17316                    "table_name": "t1",
17317                    "access_type": "ALL",
17318                    "r_loops": 1,
17319                    "rows": 2,
17320                    "r_rows": 2,
17321                    "r_table_time_ms": "REPLACED",
17322                    "r_other_time_ms": "REPLACED",
17323                    "filtered": 100,
17324                    "r_filtered": 50,
17325                    "attached_condition": "t1.a = 3"
17326                  }
17327                }
17328              },
17329              {
17330                "query_block": {
17331                  "select_id": 3,
17332                  "operation": "UNION",
17333                  "table": {
17334                    "message": "No tables used"
17335                  }
17336                }
17337              }
17338            ]
17339          }
17340        }
17341      }
17342    }
17343  }
17344}
17345SELECT * from v1 WHERE a=3;
17346a
173473
17348DROP VIEW v1;
17349DROP TABLE t1;
17350#
17351# MDEV-25128: Split optimization for join with materialized semi-join
17352#
17353create table t1 (id int, a int, index (a), index (id, a)) engine=myisam;
17354insert into t1 values
17355(17,1),(17,3010),(17,3013),(17,3053),(21,2446),(21,2467),(21,2);
17356create table t2 (a int) engine=myisam;
17357insert into t2 values (1),(2),(3);
17358create  table t3 (id int) engine=myisam;
17359insert into t3 values (1),(2);
17360analyze table t1,t2,t3;
17361Table	Op	Msg_type	Msg_text
17362test.t1	analyze	status	Engine-independent statistics collected
17363test.t1	analyze	status	OK
17364test.t2	analyze	status	Engine-independent statistics collected
17365test.t2	analyze	status	OK
17366test.t3	analyze	status	Engine-independent statistics collected
17367test.t3	analyze	status	OK
17368set optimizer_switch="split_materialized=off";
17369select * from t1, (select a from t1 cp2 group by a) dt, t3
17370where dt.a = t1.a and t1.a = t3.id and t1.a in (select a from t2);
17371id	a	a	id
1737217	1	1	1
1737321	2	2	2
17374explain select * from t1, (select a from t1 cp2 group by a) dt, t3
17375where dt.a = t1.a and t1.a = t3.id and t1.a in (select a from t2);
17376id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
173771	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where
173781	PRIMARY	t1	ref	a	a	5	test.t3.id	1
173791	PRIMARY	<subquery3>	eq_ref	distinct_key	distinct_key	4	func	1
173801	PRIMARY	<derived2>	ref	key0	key0	5	test.t3.id	2
173813	MATERIALIZED	t2	ALL	NULL	NULL	NULL	NULL	3
173822	DERIVED	cp2	range	NULL	a	5	NULL	8	Using index for group-by
17383explain format=json select * from t1, (select a from t1 cp2 group by a) dt, t3
17384where dt.a = t1.a and t1.a = t3.id and t1.a in (select a from t2);
17385EXPLAIN
17386{
17387  "query_block": {
17388    "select_id": 1,
17389    "table": {
17390      "table_name": "t3",
17391      "access_type": "ALL",
17392      "rows": 2,
17393      "filtered": 100,
17394      "attached_condition": "t3.`id` is not null and t3.`id` is not null"
17395    },
17396    "table": {
17397      "table_name": "t1",
17398      "access_type": "ref",
17399      "possible_keys": ["a"],
17400      "key": "a",
17401      "key_length": "5",
17402      "used_key_parts": ["a"],
17403      "ref": ["test.t3.id"],
17404      "rows": 1,
17405      "filtered": 100
17406    },
17407    "table": {
17408      "table_name": "<subquery3>",
17409      "access_type": "eq_ref",
17410      "possible_keys": ["distinct_key"],
17411      "key": "distinct_key",
17412      "key_length": "4",
17413      "used_key_parts": ["a"],
17414      "ref": ["func"],
17415      "rows": 1,
17416      "filtered": 100,
17417      "materialized": {
17418        "unique": 1,
17419        "query_block": {
17420          "select_id": 3,
17421          "table": {
17422            "table_name": "t2",
17423            "access_type": "ALL",
17424            "rows": 3,
17425            "filtered": 100
17426          }
17427        }
17428      }
17429    },
17430    "table": {
17431      "table_name": "<derived2>",
17432      "access_type": "ref",
17433      "possible_keys": ["key0"],
17434      "key": "key0",
17435      "key_length": "5",
17436      "used_key_parts": ["a"],
17437      "ref": ["test.t3.id"],
17438      "rows": 2,
17439      "filtered": 100,
17440      "materialized": {
17441        "query_block": {
17442          "select_id": 2,
17443          "table": {
17444            "table_name": "cp2",
17445            "access_type": "range",
17446            "key": "a",
17447            "key_length": "5",
17448            "used_key_parts": ["a"],
17449            "rows": 8,
17450            "filtered": 100,
17451            "using_index_for_group_by": true
17452          }
17453        }
17454      }
17455    }
17456  }
17457}
17458set optimizer_switch="split_materialized=default";
17459select * from t1, (select a from t1 cp2 group by a) dt, t3
17460where dt.a = t1.a and t1.a = t3.id and t1.a in (select a from t2);
17461id	a	a	id
1746217	1	1	1
1746321	2	2	2
17464explain select * from t1, (select a from t1 cp2 group by a) dt, t3
17465where dt.a = t1.a and t1.a = t3.id and t1.a in (select a from t2);
17466id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
174671	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where
174681	PRIMARY	t1	ref	a	a	5	test.t3.id	1
174691	PRIMARY	<subquery3>	eq_ref	distinct_key	distinct_key	4	func	1
174701	PRIMARY	<derived2>	ref	key0	key0	5	test.t3.id	2
174713	MATERIALIZED	t2	ALL	NULL	NULL	NULL	NULL	3
174722	LATERAL DERIVED	cp2	ref	a	a	5	test.t1.a	1	Using where; Using index
17473explain format=json select * from t1, (select a from t1 cp2 group by a) dt, t3
17474where dt.a = t1.a and t1.a = t3.id and t1.a in (select a from t2);
17475EXPLAIN
17476{
17477  "query_block": {
17478    "select_id": 1,
17479    "table": {
17480      "table_name": "t3",
17481      "access_type": "ALL",
17482      "rows": 2,
17483      "filtered": 100,
17484      "attached_condition": "t3.`id` is not null and t3.`id` is not null"
17485    },
17486    "table": {
17487      "table_name": "t1",
17488      "access_type": "ref",
17489      "possible_keys": ["a"],
17490      "key": "a",
17491      "key_length": "5",
17492      "used_key_parts": ["a"],
17493      "ref": ["test.t3.id"],
17494      "rows": 1,
17495      "filtered": 100
17496    },
17497    "table": {
17498      "table_name": "<subquery3>",
17499      "access_type": "eq_ref",
17500      "possible_keys": ["distinct_key"],
17501      "key": "distinct_key",
17502      "key_length": "4",
17503      "used_key_parts": ["a"],
17504      "ref": ["func"],
17505      "rows": 1,
17506      "filtered": 100,
17507      "materialized": {
17508        "unique": 1,
17509        "query_block": {
17510          "select_id": 3,
17511          "table": {
17512            "table_name": "t2",
17513            "access_type": "ALL",
17514            "rows": 3,
17515            "filtered": 100
17516          }
17517        }
17518      }
17519    },
17520    "table": {
17521      "table_name": "<derived2>",
17522      "access_type": "ref",
17523      "possible_keys": ["key0"],
17524      "key": "key0",
17525      "key_length": "5",
17526      "used_key_parts": ["a"],
17527      "ref": ["test.t3.id"],
17528      "rows": 2,
17529      "filtered": 100,
17530      "materialized": {
17531        "lateral": 1,
17532        "query_block": {
17533          "select_id": 2,
17534          "outer_ref_condition": "t1.a is not null",
17535          "table": {
17536            "table_name": "cp2",
17537            "access_type": "ref",
17538            "possible_keys": ["a"],
17539            "key": "a",
17540            "key_length": "5",
17541            "used_key_parts": ["a"],
17542            "ref": ["test.t1.a"],
17543            "rows": 1,
17544            "filtered": 100,
17545            "attached_condition": "cp2.a = t3.`id`",
17546            "using_index": true
17547          }
17548        }
17549      }
17550    }
17551  }
17552}
17553prepare stmt from "select * from t1, (select a from t1 cp2 group by a) dt, t3
17554where dt.a = t1.a and t1.a = t3.id and t1.a in (select a from t2)";
17555execute stmt;
17556id	a	a	id
1755717	1	1	1
1755821	2	2	2
17559execute stmt;
17560id	a	a	id
1756117	1	1	1
1756221	2	2	2
17563deallocate prepare stmt;
17564drop table t1,t2,t3;
17565#
17566# MDEV-MDEV-27132: Splittable derived with equality in WHERE
17567#
17568CREATE TABLE t1 (
17569id int PRIMARY KEY
17570) ENGINE=MyISAM;
17571INSERT INTO t1 VALUES
17572(-1),(2070),(4826),(4827),(4828),(4829),(4830),(4831),(4832),(4833),(4834),
17573(4835),(4836),(4837),(4838),(4839),(4840),(4841),(4842),(4843),(4844),
17574(4845),(4846),(4847),(4848),(4849),(4850),(4851),(4852),(4853),(4854),
17575(4855),(4856),(4857),(4858),(4859),(4860),(4861),(4862),(4863),(4864),
17576(4865),(4866),(4867),(4868),(4869),(4870),(4871),(4872),(4873),(4874),
17577(4875),(4876);
17578CREATE TABLE t2 (
17579id int PRIMARY KEY AUTO_INCREMENT,
17580deleted int(1),
17581t1_id int,
17582email varchar(255),
17583reporting_person int(1),
17584KEY t1_id (t1_id)
17585) ENGINE=MyISAM;
17586INSERT INTO t2 VALUES
17587(1,0,2064,'1test@test.ee',1),(2,1626095588,2066,'2test@test.ee',1),
17588(3,0,2066,'3test@test.ee',1),(4,0,2068,'4test@test.ee',1),
17589(5,0,2068,'5test@test.ee',1),(6,0,2069,'6test@test.ee',1),(7,0,2070,'',0),
17590(8,0,2070,'',0),(9,0,2071,'',0),(10,0,2071,'',0),(11,0,2072,'',0),
17591(12,0,2072,'',0),(13,0,2072,'13test@test.ee',1),(14,0,2073,'14test@test.ee',1),
17592(15,0,2074,'15test@test.ee',1),(16,0,2075,'16test@test.ee',1),(17,0,2075,'',0),
17593(18,0,2075,'',0),(19,0,2076,'19test@test.ee',1),(20,0,2077,'',0),
17594(21,0,2078,'21test@test.ee',1),(22,0,2078,'22test@test.ee',1);
17595INSERT INTO t2(deleted, t1_id, email, reporting_person)
17596SELECT deleted, t1_id, email, reporting_person FROM t2;
17597INSERT INTO t2(deleted, t1_id, email, reporting_person)
17598SELECT deleted, t1_id+10000, email, reporting_person FROM t2;
17599INSERT INTO t2(deleted, t1_id, email, reporting_person)
17600SELECT deleted, t1_id+20000, email, reporting_person FROM t2;
17601INSERT INTO t2(deleted, t1_id, email, reporting_person)
17602SELECT deleted, t1_id+40000, email, reporting_person FROM t2;
17603INSERT INTO t2(deleted, t1_id, email, reporting_person)
17604SELECT deleted, t1_id+80000, email, reporting_person FROM t2;
17605INSERT INTO t2(deleted, t1_id, email, reporting_person)
17606SELECT deleted, t1_id+160000, email, reporting_person FROM t2;
17607CREATE TABLE t3 (
17608id int PRIMARY KEY,
17609deleted int,
17610t1_id int,
17611YEAR int(4),
17612quarter int(1),
17613KEY t1_id (t1_id,year,quarter)
17614) ENGINE=MyISAM;
17615INSERT INTO t3 VALUES
17616(1,0,3885,2020,1),(2,0,2064,2020,1),(3,1611670734,2225,2020,1),
17617(4,0,2070,2020,1),(5,1611055981,2095,2020,1),(6,1610970096,2102,2020,1),
17618(7,0,3974,2020,1),(153,1609851928,3892,2020,2),(154,0,3885,2020,2),
17619(155,0,2064,2020,2),(156,1611670717,2225,2020,2),(157,0,2070,2020,2),
17620(317,0,2257,2020,2),(318,0,3885,2020,3),(319,0,2064,2020,3),
17621(320,1611670709,2225,2020,3),(321,0,2070,2020,3),(322,0,2095,2020,3),
17622(323,0,2102,2020,3),(324,0,3974,2020,3),(325,0,3886,2020,3),
17623(326,1609939963,2104,2020,3),(327,0,3887,2020,3),(328,0,3888,2020,3),
17624(329,0,2148,2020,3),(330,0,3889,2020,3),(331,0,3890,2020,3),
17625(332,0,2179,2020,3),(333,0,2115,2020,3),(334,0,2193,2020,3),
17626(335,0,2213,2020,3),(336,0,3891,2020,3),(337,1609851955,3892,2020,3),
17627(338,1610447706,2232,2020,3),(339,0,2235,2020,3),(340,0,2237,2020,3),
17628(341,0,3972,2020,3),(342,1610449357,2242,2020,3),(343,0,3893,2020,3),
17629(344,0,2257,2020,3),(345,0,3951,2020,3),(346,0,3894,2020,3),
17630(347,0,3912,2020,3),(348,0,3895,2020,3),(349,0,2301,2020,3),
17631(350,0,2304,2020,3),(351,0,3896,2020,3);
17632ANALYZE TABLE t1,t2,t3;
17633Table	Op	Msg_type	Msg_text
17634test.t1	analyze	status	Engine-independent statistics collected
17635test.t1	analyze	status	OK
17636test.t2	analyze	status	Engine-independent statistics collected
17637test.t2	analyze	status	OK
17638test.t3	analyze	status	Engine-independent statistics collected
17639test.t3	analyze	status	OK
17640set  optimizer_switch='split_materialized=on';
17641SELECT t1.id
17642FROM t1
17643JOIN t3
17644ON t3.t1_id = t1.id
17645JOIN (SELECT t1_id FROM t2 WHERE reporting_person = 1 GROUP BY t1_id) tx
17646ON tx.t1_id = t1.id
17647WHERE t1.id BETWEEN 200 AND 100000;
17648id
17649EXPLAIN SELECT t1.id
17650FROM t1
17651JOIN t3
17652ON t3.t1_id = t1.id
17653JOIN (SELECT t1_id FROM t2 WHERE reporting_person = 1 GROUP BY t1_id) tx
17654ON tx.t1_id = t1.id
17655WHERE t1.id BETWEEN 200 AND 100000;
17656id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
176571	PRIMARY	t3	range	t1_id	t1_id	5	NULL	47	Using where; Using index
176581	PRIMARY	t1	eq_ref	PRIMARY	PRIMARY	4	test.t3.t1_id	1	Using index
176591	PRIMARY	<derived2>	ref	key0	key0	5	test.t3.t1_id	2
176602	LATERAL DERIVED	t2	ref	t1_id	t1_id	5	test.t1.id	3	Using index condition; Using where
17661EXPLAIN FORMAT=JSON SELECT t1.id
17662FROM t1
17663JOIN t3
17664ON t3.t1_id = t1.id
17665JOIN (SELECT t1_id FROM t2 WHERE reporting_person = 1 GROUP BY t1_id) tx
17666ON tx.t1_id = t1.id
17667WHERE t1.id BETWEEN 200 AND 100000;
17668EXPLAIN
17669{
17670  "query_block": {
17671    "select_id": 1,
17672    "table": {
17673      "table_name": "t3",
17674      "access_type": "range",
17675      "possible_keys": ["t1_id"],
17676      "key": "t1_id",
17677      "key_length": "5",
17678      "used_key_parts": ["t1_id"],
17679      "rows": 47,
17680      "filtered": 100,
17681      "attached_condition": "t3.t1_id between 200 and 100000 and t3.t1_id is not null",
17682      "using_index": true
17683    },
17684    "table": {
17685      "table_name": "t1",
17686      "access_type": "eq_ref",
17687      "possible_keys": ["PRIMARY"],
17688      "key": "PRIMARY",
17689      "key_length": "4",
17690      "used_key_parts": ["id"],
17691      "ref": ["test.t3.t1_id"],
17692      "rows": 1,
17693      "filtered": 100,
17694      "using_index": true
17695    },
17696    "table": {
17697      "table_name": "<derived2>",
17698      "access_type": "ref",
17699      "possible_keys": ["key0"],
17700      "key": "key0",
17701      "key_length": "5",
17702      "used_key_parts": ["t1_id"],
17703      "ref": ["test.t3.t1_id"],
17704      "rows": 2,
17705      "filtered": 100,
17706      "materialized": {
17707        "lateral": 1,
17708        "query_block": {
17709          "select_id": 2,
17710          "table": {
17711            "table_name": "t2",
17712            "access_type": "ref",
17713            "possible_keys": ["t1_id"],
17714            "key": "t1_id",
17715            "key_length": "5",
17716            "used_key_parts": ["t1_id"],
17717            "ref": ["test.t1.id"],
17718            "rows": 3,
17719            "filtered": 58.59375,
17720            "index_condition": "t2.t1_id between 200 and 100000 and t2.t1_id = t3.t1_id",
17721            "attached_condition": "t2.reporting_person = 1"
17722          }
17723        }
17724      }
17725    }
17726  }
17727}
17728set  optimizer_switch='split_materialized=off';
17729SELECT t1.id
17730FROM t1
17731JOIN t3
17732ON t3.t1_id = t1.id
17733JOIN (SELECT t1_id FROM t2 WHERE reporting_person = 1 GROUP BY t1_id) tx
17734ON tx.t1_id = t1.id
17735WHERE t1.id BETWEEN 200 AND 100000;
17736id
17737set  optimizer_switch='split_materialized=default';
17738DROP TABLE t1,t2,t3;
17739#
17740# MDEV-27510: Splittable derived with grouping over two tables
17741#
17742CREATE TABLE ledgers (
17743id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
17744name VARCHAR(32)
17745) ENGINE=MyISAM;
17746CREATE TABLE charges (
17747id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
17748from_ledger_id BIGINT UNSIGNED NOT NULL,
17749to_ledger_id BIGINT UNSIGNED NOT NULL,
17750amount INT NOT NULL,
17751KEY fk_charge_from_ledger (from_ledger_id),
17752KEY fk_charge_to_ledger (to_ledger_id)
17753) ENGINE=MyISAM;
17754CREATE TABLE transactions (
17755id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
17756ledger_id BIGINT UNSIGNED NOT NULL,
17757KEY fk_transactions_ledger (ledger_id)
17758) ENGINE=MyISAM;
17759CREATE TABLE transaction_items (
17760id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
17761transaction_id BIGINT UNSIGNED NOT NULL,
17762charge_id BIGINT UNSIGNED,
17763amount INT NOT NULL,
17764KEY fk_items_transaction (transaction_id),
17765KEY fk_items_charge (charge_id)
17766) ENGINE=MyISAM;
17767INSERT INTO ledgers (id, name) VALUES
17768(1, 'Anna'), (2, 'John'), (3, 'Fred');
17769INSERT INTO charges (id, from_ledger_id, to_ledger_id, amount) VALUES
17770(1, 2, 1, 200), (2, 1, 2, 330), (3, 1, 2, 640), (4, 3, 1, 640), (5, 3, 2, 1000),
17771(6, 3, 1, 660), (7, 2, 3, 650), (8, 3, 2, 160), (9, 2, 1, 740), (10, 3, 2, 310),
17772(11, 2, 1, 640), (12, 3, 2, 240), (13, 3, 2, 340), (14, 2, 1, 720),
17773(15, 2, 3, 100),
17774(16, 2, 3, 980), (17, 2, 1, 80), (18, 1, 2, 760), (19, 2, 3, 740),
17775(20, 2, 1, 990);
17776INSERT INTO transactions (id, ledger_id) VALUES
17777(2, 1), (3, 1), (5, 1), (8, 1), (12, 1), (18, 1), (22, 1), (28, 1),
17778(34, 1), (35, 1),
17779(40, 1), (1, 2), (4, 2), (6, 2), (10, 2), (13, 2), (16, 2), (17, 2),
17780(20, 2), (21, 2),
17781(24, 2), (26, 2), (27, 2), (29, 2), (31, 2), (33, 2), (36, 2), (37, 2),
17782(39, 2), (7, 3),
17783(9, 3), (11, 3), (14, 3), (15, 3), (19, 3), (23, 3), (25, 3), (30, 3),
17784(32, 3), (38, 3);
17785INSERT INTO transaction_items (id, transaction_id, charge_id, amount) VALUES
17786(1, 1, 1, -200), (2, 2, 1, 200), (3, 3, 2, -330), (4, 4, 2, 330),
17787(5, 5, 3, -640),
17788(6, 6, 3, 640), (7, 7, 4, -640), (8, 8, 4, 640), (9, 9, 5, -1000),
17789(10, 10, 5, 1000),
17790(11, 11, 6, -660), (12, 12, 6, 660), (13, 13, 7, -650), (14, 14, 7, 650),
17791(15, 15, 8, -160),
17792(16, 16, 8, 160), (17, 17, 9, -740), (18, 18, 9, 740), (19, 19, 10, -310),
17793(20, 20, 10, 310),
17794(21, 21, 11, -640), (22, 22, 11, 640), (23, 23, 12, -240), (24, 24, 12, 240),
17795(25, 25, 13, -340),
17796(26, 26, 13, 340), (27, 27, 14, -720), (28, 28, 14, 720), (29, 29, 15, -100),
17797(30, 30, 15, 100),
17798(31, 31, 16, -980), (32, 32, 16, 980), (33, 33, 17, -80), (34, 34, 17, 80),
17799(35, 35, 18, -760),
17800(36, 36, 18, 760), (37, 37, 19, -740), (38, 38, 19, 740), (39, 39, 20, -990),
17801(40, 40, 20, 990);
17802ANALYZE TABLE ledgers, charges, transactions, transaction_items;
17803Table	Op	Msg_type	Msg_text
17804test.ledgers	analyze	status	Engine-independent statistics collected
17805test.ledgers	analyze	status	OK
17806test.charges	analyze	status	Engine-independent statistics collected
17807test.charges	analyze	status	OK
17808test.transactions	analyze	status	Engine-independent statistics collected
17809test.transactions	analyze	status	OK
17810test.transaction_items	analyze	status	Engine-independent statistics collected
17811test.transaction_items	analyze	status	OK
17812set optimizer_switch='split_materialized=on';
17813SELECT
17814charges.id,
17815charges.from_ledger_id,
17816charges.to_ledger_id,
17817from_agg_items.num_rows AS from_num_rows
17818FROM charges
17819INNER JOIN (
17820SELECT
17821transactions.ledger_id,
17822transaction_items.charge_id,
17823count(*) as num_rows
17824FROM transaction_items
17825INNER JOIN transactions ON transaction_items.transaction_id = transactions.id
17826GROUP BY transactions.ledger_id, transaction_items.charge_id
17827) AS from_agg_items
17828ON from_agg_items.charge_id = charges.id AND
17829from_agg_items.ledger_id = charges.from_ledger_id
17830WHERE charges.to_ledger_id = 2;
17831id	from_ledger_id	to_ledger_id	from_num_rows
178322	1	2	1
178333	1	2	1
178345	3	2	1
178358	3	2	1
1783610	3	2	1
1783712	3	2	1
1783813	3	2	1
1783918	1	2	1
17840EXPLAIN SELECT
17841charges.id,
17842charges.from_ledger_id,
17843charges.to_ledger_id,
17844from_agg_items.num_rows AS from_num_rows
17845FROM charges
17846INNER JOIN (
17847SELECT
17848transactions.ledger_id,
17849transaction_items.charge_id,
17850count(*) as num_rows
17851FROM transaction_items
17852INNER JOIN transactions ON transaction_items.transaction_id = transactions.id
17853GROUP BY transactions.ledger_id, transaction_items.charge_id
17854) AS from_agg_items
17855ON from_agg_items.charge_id = charges.id AND
17856from_agg_items.ledger_id = charges.from_ledger_id
17857WHERE charges.to_ledger_id = 2;
17858id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
178591	PRIMARY	charges	ALL	PRIMARY,fk_charge_from_ledger,fk_charge_to_ledger	NULL	NULL	NULL	20	Using where
178601	PRIMARY	<derived2>	ref	key0	key0	17	test.charges.from_ledger_id,test.charges.id	2
178612	LATERAL DERIVED	transaction_items	ref	fk_items_transaction,fk_items_charge	fk_items_charge	9	test.charges.id	2
178622	LATERAL DERIVED	transactions	eq_ref	PRIMARY,fk_transactions_ledger	PRIMARY	8	test.transaction_items.transaction_id	1	Using where
17863EXPLAIN FORMAT=JSON SELECT
17864charges.id,
17865charges.from_ledger_id,
17866charges.to_ledger_id,
17867from_agg_items.num_rows AS from_num_rows
17868FROM charges
17869INNER JOIN (
17870SELECT
17871transactions.ledger_id,
17872transaction_items.charge_id,
17873count(*) as num_rows
17874FROM transaction_items
17875INNER JOIN transactions ON transaction_items.transaction_id = transactions.id
17876GROUP BY transactions.ledger_id, transaction_items.charge_id
17877) AS from_agg_items
17878ON from_agg_items.charge_id = charges.id AND
17879from_agg_items.ledger_id = charges.from_ledger_id
17880WHERE charges.to_ledger_id = 2;
17881EXPLAIN
17882{
17883  "query_block": {
17884    "select_id": 1,
17885    "table": {
17886      "table_name": "charges",
17887      "access_type": "ALL",
17888      "possible_keys": ["PRIMARY", "fk_charge_from_ledger", "fk_charge_to_ledger"],
17889      "rows": 20,
17890      "filtered": 40,
17891      "attached_condition": "charges.to_ledger_id = 2"
17892    },
17893    "table": {
17894      "table_name": "<derived2>",
17895      "access_type": "ref",
17896      "possible_keys": ["key0"],
17897      "key": "key0",
17898      "key_length": "17",
17899      "used_key_parts": ["ledger_id", "charge_id"],
17900      "ref": ["test.charges.from_ledger_id", "test.charges.id"],
17901      "rows": 2,
17902      "filtered": 100,
17903      "materialized": {
17904        "lateral": 1,
17905        "query_block": {
17906          "select_id": 2,
17907          "table": {
17908            "table_name": "transaction_items",
17909            "access_type": "ref",
17910            "possible_keys": ["fk_items_transaction", "fk_items_charge"],
17911            "key": "fk_items_charge",
17912            "key_length": "9",
17913            "used_key_parts": ["charge_id"],
17914            "ref": ["test.charges.id"],
17915            "rows": 2,
17916            "filtered": 100
17917          },
17918          "table": {
17919            "table_name": "transactions",
17920            "access_type": "eq_ref",
17921            "possible_keys": ["PRIMARY", "fk_transactions_ledger"],
17922            "key": "PRIMARY",
17923            "key_length": "8",
17924            "used_key_parts": ["id"],
17925            "ref": ["test.transaction_items.transaction_id"],
17926            "rows": 1,
17927            "filtered": 100,
17928            "attached_condition": "transactions.ledger_id = charges.from_ledger_id"
17929          }
17930        }
17931      }
17932    }
17933  }
17934}
17935set optimizer_switch='split_materialized=off';
17936SELECT
17937charges.id,
17938charges.from_ledger_id,
17939charges.to_ledger_id,
17940from_agg_items.num_rows AS from_num_rows
17941FROM charges
17942INNER JOIN (
17943SELECT
17944transactions.ledger_id,
17945transaction_items.charge_id,
17946count(*) as num_rows
17947FROM transaction_items
17948INNER JOIN transactions ON transaction_items.transaction_id = transactions.id
17949GROUP BY transactions.ledger_id, transaction_items.charge_id
17950) AS from_agg_items
17951ON from_agg_items.charge_id = charges.id AND
17952from_agg_items.ledger_id = charges.from_ledger_id
17953WHERE charges.to_ledger_id = 2;
17954id	from_ledger_id	to_ledger_id	from_num_rows
179552	1	2	1
179563	1	2	1
179575	3	2	1
179588	3	2	1
1795910	3	2	1
1796012	3	2	1
1796113	3	2	1
1796218	1	2	1
17963EXPLAIN SELECT
17964charges.id,
17965charges.from_ledger_id,
17966charges.to_ledger_id,
17967from_agg_items.num_rows AS from_num_rows
17968FROM charges
17969INNER JOIN (
17970SELECT
17971transactions.ledger_id,
17972transaction_items.charge_id,
17973count(*) as num_rows
17974FROM transaction_items
17975INNER JOIN transactions ON transaction_items.transaction_id = transactions.id
17976GROUP BY transactions.ledger_id, transaction_items.charge_id
17977) AS from_agg_items
17978ON from_agg_items.charge_id = charges.id AND
17979from_agg_items.ledger_id = charges.from_ledger_id
17980WHERE charges.to_ledger_id = 2;
17981id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
179821	PRIMARY	charges	ALL	PRIMARY,fk_charge_from_ledger,fk_charge_to_ledger	NULL	NULL	NULL	20	Using where
179831	PRIMARY	<derived2>	ref	key0	key0	17	test.charges.from_ledger_id,test.charges.id	4
179842	DERIVED	transaction_items	ALL	fk_items_transaction	NULL	NULL	NULL	40	Using temporary; Using filesort
179852	DERIVED	transactions	eq_ref	PRIMARY	PRIMARY	8	test.transaction_items.transaction_id	1
17986INSERT INTO charges (id, from_ledger_id, to_ledger_id, amount) VALUES
17987(101, 4, 2, 100), (102, 7, 2, 200);
17988set optimizer_switch='split_materialized=on';
17989SELECT
17990charges.id,
17991charges.from_ledger_id,
17992charges.to_ledger_id,
17993from_agg_items.num_rows AS from_num_rows
17994FROM charges
17995LEFT JOIN (
17996SELECT
17997transactions.ledger_id,
17998transaction_items.charge_id,
17999count(*) as num_rows
18000FROM transaction_items
18001INNER JOIN transactions ON transaction_items.transaction_id = transactions.id
18002GROUP BY transactions.ledger_id, transaction_items.charge_id
18003) AS from_agg_items
18004ON from_agg_items.charge_id = charges.id AND
18005from_agg_items.ledger_id = charges.from_ledger_id
18006WHERE charges.to_ledger_id = 2;
18007id	from_ledger_id	to_ledger_id	from_num_rows
180082	1	2	1
180093	1	2	1
180105	3	2	1
180118	3	2	1
1801210	3	2	1
1801312	3	2	1
1801413	3	2	1
1801518	1	2	1
18016101	4	2	NULL
18017102	7	2	NULL
18018EXPLAIN SELECT
18019charges.id,
18020charges.from_ledger_id,
18021charges.to_ledger_id,
18022from_agg_items.num_rows AS from_num_rows
18023FROM charges
18024LEFT JOIN (
18025SELECT
18026transactions.ledger_id,
18027transaction_items.charge_id,
18028count(*) as num_rows
18029FROM transaction_items
18030INNER JOIN transactions ON transaction_items.transaction_id = transactions.id
18031GROUP BY transactions.ledger_id, transaction_items.charge_id
18032) AS from_agg_items
18033ON from_agg_items.charge_id = charges.id AND
18034from_agg_items.ledger_id = charges.from_ledger_id
18035WHERE charges.to_ledger_id = 2;
18036id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
180371	PRIMARY	charges	ALL	fk_charge_to_ledger	NULL	NULL	NULL	20	Using where
180381	PRIMARY	<derived2>	ref	key0	key0	18	test.charges.from_ledger_id,test.charges.id	2
180392	LATERAL DERIVED	transaction_items	ref	fk_items_transaction,fk_items_charge	fk_items_charge	9	test.charges.id	2
180402	LATERAL DERIVED	transactions	eq_ref	PRIMARY,fk_transactions_ledger	PRIMARY	8	test.transaction_items.transaction_id	1	Using where
18041EXPLAIN FORMAT=JSON SELECT
18042charges.id,
18043charges.from_ledger_id,
18044charges.to_ledger_id,
18045from_agg_items.num_rows AS from_num_rows
18046FROM charges
18047LEFT JOIN (
18048SELECT
18049transactions.ledger_id,
18050transaction_items.charge_id,
18051count(*) as num_rows
18052FROM transaction_items
18053INNER JOIN transactions ON transaction_items.transaction_id = transactions.id
18054GROUP BY transactions.ledger_id, transaction_items.charge_id
18055) AS from_agg_items
18056ON from_agg_items.charge_id = charges.id AND
18057from_agg_items.ledger_id = charges.from_ledger_id
18058WHERE charges.to_ledger_id = 2;
18059EXPLAIN
18060{
18061  "query_block": {
18062    "select_id": 1,
18063    "table": {
18064      "table_name": "charges",
18065      "access_type": "ALL",
18066      "possible_keys": ["fk_charge_to_ledger"],
18067      "rows": 20,
18068      "filtered": 50,
18069      "attached_condition": "charges.to_ledger_id = 2"
18070    },
18071    "table": {
18072      "table_name": "<derived2>",
18073      "access_type": "ref",
18074      "possible_keys": ["key0"],
18075      "key": "key0",
18076      "key_length": "18",
18077      "used_key_parts": ["ledger_id", "charge_id"],
18078      "ref": ["test.charges.from_ledger_id", "test.charges.id"],
18079      "rows": 2,
18080      "filtered": 100,
18081      "materialized": {
18082        "lateral": 1,
18083        "query_block": {
18084          "select_id": 2,
18085          "table": {
18086            "table_name": "transaction_items",
18087            "access_type": "ref",
18088            "possible_keys": ["fk_items_transaction", "fk_items_charge"],
18089            "key": "fk_items_charge",
18090            "key_length": "9",
18091            "used_key_parts": ["charge_id"],
18092            "ref": ["test.charges.id"],
18093            "rows": 2,
18094            "filtered": 100
18095          },
18096          "table": {
18097            "table_name": "transactions",
18098            "access_type": "eq_ref",
18099            "possible_keys": ["PRIMARY", "fk_transactions_ledger"],
18100            "key": "PRIMARY",
18101            "key_length": "8",
18102            "used_key_parts": ["id"],
18103            "ref": ["test.transaction_items.transaction_id"],
18104            "rows": 1,
18105            "filtered": 100,
18106            "attached_condition": "transactions.ledger_id = charges.from_ledger_id"
18107          }
18108        }
18109      }
18110    }
18111  }
18112}
18113set optimizer_switch='split_materialized=off';
18114SELECT
18115charges.id,
18116charges.from_ledger_id,
18117charges.to_ledger_id,
18118from_agg_items.num_rows AS from_num_rows
18119FROM charges
18120LEFT JOIN (
18121SELECT
18122transactions.ledger_id,
18123transaction_items.charge_id,
18124count(*) as num_rows
18125FROM transaction_items
18126INNER JOIN transactions ON transaction_items.transaction_id = transactions.id
18127GROUP BY transactions.ledger_id, transaction_items.charge_id
18128) AS from_agg_items
18129ON from_agg_items.charge_id = charges.id AND
18130from_agg_items.ledger_id = charges.from_ledger_id
18131WHERE charges.to_ledger_id = 2;
18132id	from_ledger_id	to_ledger_id	from_num_rows
181332	1	2	1
181343	1	2	1
181355	3	2	1
181368	3	2	1
1813710	3	2	1
1813812	3	2	1
1813913	3	2	1
1814018	1	2	1
18141101	4	2	NULL
18142102	7	2	NULL
18143EXPLAIN SELECT
18144charges.id,
18145charges.from_ledger_id,
18146charges.to_ledger_id,
18147from_agg_items.num_rows AS from_num_rows
18148FROM charges
18149LEFT JOIN (
18150SELECT
18151transactions.ledger_id,
18152transaction_items.charge_id,
18153count(*) as num_rows
18154FROM transaction_items
18155INNER JOIN transactions ON transaction_items.transaction_id = transactions.id
18156GROUP BY transactions.ledger_id, transaction_items.charge_id
18157) AS from_agg_items
18158ON from_agg_items.charge_id = charges.id AND
18159from_agg_items.ledger_id = charges.from_ledger_id
18160WHERE charges.to_ledger_id = 2;
18161id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
181621	PRIMARY	charges	ALL	fk_charge_to_ledger	NULL	NULL	NULL	20	Using where
181631	PRIMARY	<derived2>	ref	key0	key0	18	test.charges.from_ledger_id,test.charges.id	4
181642	DERIVED	transaction_items	ALL	fk_items_transaction	NULL	NULL	NULL	40	Using temporary; Using filesort
181652	DERIVED	transactions	eq_ref	PRIMARY	PRIMARY	8	test.transaction_items.transaction_id	1
18166set optimizer_switch='split_materialized=default';
18167DROP TABLE transaction_items;
18168DROP TABLE transactions;
18169DROP TABLE charges;
18170DROP TABLE ledgers;
18171# End of 10.3 tests
18172#
18173# MDEV-18679: materialized view with SELECT S containing materialized
18174#             derived when impossible WHERE has been detected for S
18175#
18176create table t1 (pk int, f varchar(1));
18177insert into t1 values
18178(3,'y'), (1,'x'), (7,'z');
18179create view v1 as
18180select t1.f
18181from t1, (select distinct * from t1) t
18182where t.f = t1.f and 1 = 0
18183group by t1.f;
18184select * from v1;
18185f
18186explain select * from v1;
18187id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
181881	PRIMARY	<derived2>	system	NULL	NULL	NULL	NULL	0	Const row not found
181892	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
181903	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	3	Using temporary
18191drop view v1;
18192drop table t1;
18193# End of 10.4 tests
18194