set @@join_buffer_size=256*1024;
create table t1 (a int, b int, c int);
create table t2 (a int, b int, c int, d decimal);
insert into t1 values
(1,21,345), (1,33,7), (8,33,114), (1,21,500), (1,19,107), (5,14,787),
(8,33,123), (9,10,211), (5,16,207), (1,33,988), (5,27,132), (1,21,104),
(6,20,309), (6,20,315), (1,21,101), (8,33,404), (9,10,800), (1,21,123),
(7,11,708), (6,20,214);
insert into t2 values
(2,3,207,207.0000), (1,21,909,12.0000), (7,13,312,406.0000),
(8,64,248,107.0000), (6,20,315,279.3333), (1,19,203,107.0000),
(8,80,800,314.0000), (3,12,231,190.0000), (6,23,303,909.0000);
Warnings:
Note	1265	Data truncated for column 'd' at row 5
create table t1_double(a int, b double, c double);
insert into t1_double values
(1,23.4,14.3333), (1,12.5,18.9), (3,12.5,18.9),
(4,33.4,14.3333), (4,14.3333,13.65), (5,17.89,7.22),
(6,33.4,14.3), (10,33.4,13.65), (11,33.4,13.65);
create table t2_double(a int, b double, c double);
insert into t2_double values
(1,22.4,14.3333), (1,12.5,18.9), (2,22.4,18.9),
(4,33.4,14.3333), (5,22.4,13.65), (7,17.89,18.9),
(6,33.4,14.3333), (10,31.4,13.65), (12,33.4,13.65);
create table t1_char(a char, b char(8), c int);
insert into t1_char values
('a','Ivan',1), ('b','Vika',2), ('b','Inga',6), ('c','Vika',7),
('b','Ivan',7), ('a','Alex',6), ('b','Inga',5), ('d','Ron',9),
('d','Harry',2), ('d','Hermione',3), ('c','Ivan',3), ('c','Harry',4);
create table t2_char(a char, b char(8), c int);
insert into t2_char values
('b','Ivan',1), ('c','Vinny',3), ('c','Inga',9), ('a','Vika',1),
('c','Ivan',2), ('b','Ali',6), ('c','Inga',2), ('a','Ron',9),
('d','Harry',1), ('b','Hermes',3), ('b','Ivan',11), ('b','Harry',4);
create table t1_decimal (a decimal(3,1), b decimal(3,1), c int);
insert into t1_decimal values
(1,1,23),(2,2,11),(3,3,16),
(1,1,12),(1,1,14),(2,3,15),
(2,1,13),(2,3,11),(3,3,16);
create table t2_decimal (a decimal(3,1), b decimal(3,1), c int);
insert into t2_decimal values
(2,1,13),(2,2,11),(3,3,16),
(1,3,22),(1,3,14),(2,2,15),
(2,1,43),(2,3,11),(2,3,16);
create view v1 as select a, b, max(c) as max_c, avg(c) as avg_c from t1
group by a,b having max_c < 707;
create view v2 as select a, b, max(c) as max_c, avg(c) as avg_c from t1
where t1.a>5 group by a,b having max_c < 707;
create view v3 as select a, b, min(c) as min_c from t1
where t1.a<10 group by a,b having min_c > 109;
create view v4 as
select a, b, min(max_c) as min_c from v1
where (v1.a<15) group by a,b;
create view v_union as
select a, b, min(c) as c from t1
where t1.a<10 group by a,b having c > 109
union
select a, b, max(c) as c from t1
where t1.b>10 group by a,b having c < 300;
create view v2_union as
select a, b, min(c) as c from t1
where t1.a<10 group by a,b having c > 109
union
select a, b, max(c) as c from t1
where t1.b>10 group by a,b having c < 300
union
select a, b, avg(c) as c from t1
where t1.c>300 group by a,b having c < 707;
create view v3_union as
select a, b, (a+1) as c from t1
where t1.a<10
union
select a, b, c from t1
where t1.b>10 and t1.c>100;
create view v4_union as
select a, b, max(c)-100 as c from t1
where t1.a<10 group by a,b having c > 109
union
select a, b, (c+100) as c from t1
where t1.b>10;
create view v_double as
select a, avg(a/4) as avg_a, b, c from t1_double
where (b>12.2) group by b,c having (avg_a<22.333);
create view v_char as
select a, b, max(c) as max_c from t1_char
group by a,b having max_c < 9;
create view v_decimal as
select a, b, avg(c) as avg_c from t1_decimal
group by a,b having (avg_c>12);
# conjunctive subformula : pushing into HAVING
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.max_c>214) and (t2.a>v1.a);
a	b	max_c	avg_c	a	b	c	d
1	21	500	234.6000	2	3	207	207
1	21	500	234.6000	7	13	312	406
1	21	500	234.6000	8	64	248	107
1	21	500	234.6000	6	20	315	279
1	21	500	234.6000	8	80	800	314
1	21	500	234.6000	3	12	231	190
1	21	500	234.6000	6	23	303	909
6	20	315	279.3333	7	13	312	406
6	20	315	279.3333	8	64	248	107
6	20	315	279.3333	8	80	800	314
select * from v1,t2 where (v1.max_c>214) and (t2.a>v1.a);
a	b	max_c	avg_c	a	b	c	d
1	21	500	234.6000	2	3	207	207
1	21	500	234.6000	7	13	312	406
1	21	500	234.6000	8	64	248	107
1	21	500	234.6000	6	20	315	279
1	21	500	234.6000	8	80	800	314
1	21	500	234.6000	3	12	231	190
1	21	500	234.6000	6	23	303	909
6	20	315	279.3333	7	13	312	406
6	20	315	279.3333	8	64	248	107
6	20	315	279.3333	8	80	800	314
explain select * from v1,t2 where (v1.max_c>214) and (t2.a>v1.a);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using temporary; Using filesort
explain format=json select * from v1,t2 where (v1.max_c>214) and (t2.a>v1.a);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived2>",
            "access_type": "ALL",
            "loops": 9,
            "rows": 20,
            "cost": "COST_REPLACED",
            "filtered": 100,
            "attached_condition": "v1.max_c > 214"
          },
          "buffer_type": "flat",
          "buffer_size": "238",
          "join_type": "BNL",
          "attached_condition": "t2.a > v1.a",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707 and max_c > 214",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from
(select a, b, max(c) as max_c, avg(c) as avg_c from t1
group by a,b having max_c < 707) v1,
t2 where (v1.a=t2.a) and (v1.max_c>300);
a	b	max_c	avg_c	a	b	c	d
1	21	500	234.6000	1	21	909	12
8	33	404	213.6667	8	64	248	107
6	20	315	279.3333	6	20	315	279
1	21	500	234.6000	1	19	203	107
8	33	404	213.6667	8	80	800	314
6	20	315	279.3333	6	23	303	909
select * from
(select a, b, max(c) as max_c, avg(c) as avg_c from t1
group by a,b having max_c < 707) v1,
t2 where (v1.a=t2.a) and (v1.max_c>300);
a	b	max_c	avg_c	a	b	c	d
1	21	500	234.6000	1	21	909	12
8	33	404	213.6667	8	64	248	107
6	20	315	279.3333	6	20	315	279
1	21	500	234.6000	1	19	203	107
8	33	404	213.6667	8	80	800	314
6	20	315	279.3333	6	23	303	909
explain select * from
(select a, b, max(c) as max_c, avg(c) as avg_c from t1
group by a,b having max_c < 707) v1,
t2 where (v1.a=t2.a) and (v1.max_c>300);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.a	2	Using where
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using temporary; Using filesort
explain format=json select * from
(select a, b, max(c) as max_c, avg(c) as avg_c from t1
group by a,b having max_c < 707) v1,
t2 where (v1.a=t2.a) and (v1.max_c>300);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t2.a"],
          "loops": 9,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v1.max_c > 300",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707 and max_c > 300",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
# extracted or formula : pushing into HAVING
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where
((v1.max_c>400) and (t2.a>v1.a)) or ((v1.max_c<135) and (t2.a<v1.a));
a	b	max_c	avg_c	a	b	c	d
1	21	500	234.6000	2	3	207	207
1	21	500	234.6000	7	13	312	406
1	21	500	234.6000	8	64	248	107
1	21	500	234.6000	6	20	315	279
1	21	500	234.6000	8	80	800	314
1	21	500	234.6000	3	12	231	190
1	21	500	234.6000	6	23	303	909
5	27	132	132.0000	2	3	207	207
5	27	132	132.0000	1	21	909	12
5	27	132	132.0000	1	19	203	107
5	27	132	132.0000	3	12	231	190
select * from v1,t2 where
((v1.max_c>400) and (t2.a>v1.a)) or ((v1.max_c<135) and (t2.a<v1.a));
a	b	max_c	avg_c	a	b	c	d
1	21	500	234.6000	2	3	207	207
1	21	500	234.6000	7	13	312	406
1	21	500	234.6000	8	64	248	107
1	21	500	234.6000	6	20	315	279
1	21	500	234.6000	8	80	800	314
1	21	500	234.6000	3	12	231	190
1	21	500	234.6000	6	23	303	909
5	27	132	132.0000	2	3	207	207
5	27	132	132.0000	1	21	909	12
5	27	132	132.0000	1	19	203	107
5	27	132	132.0000	3	12	231	190
explain select * from v1,t2 where
((v1.max_c>400) and (t2.a>v1.a)) or ((v1.max_c<135) and (t2.a<v1.a));
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using temporary; Using filesort
explain format=json select * from v1,t2 where
((v1.max_c>400) and (t2.a>v1.a)) or ((v1.max_c<135) and (t2.a<v1.a));
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived2>",
            "access_type": "ALL",
            "loops": 9,
            "rows": 20,
            "cost": "COST_REPLACED",
            "filtered": 100,
            "attached_condition": "v1.max_c > 400 or v1.max_c < 135"
          },
          "buffer_type": "flat",
          "buffer_size": "238",
          "join_type": "BNL",
          "attached_condition": "v1.max_c > 400 and t2.a > v1.a or v1.max_c < 135 and t2.a < v1.a",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707 and (max_c > 400 or max_c < 135)",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where
((v1.max_c>300) and (v1.avg_c>t2.d) and (v1.b=t2.b)) or
((v1.max_c<135) and (v1.max_c<t2.c) and (v1.a=t2.a));
a	b	max_c	avg_c	a	b	c	d
1	19	107	107.0000	1	21	909	12
1	19	107	107.0000	1	19	203	107
1	21	500	234.6000	1	21	909	12
6	20	315	279.3333	6	20	315	279
select * from v1,t2 where
((v1.max_c>300) and (v1.avg_c>t2.d) and (v1.b=t2.b)) or
((v1.max_c<135) and (v1.max_c<t2.c) and (v1.a=t2.a));
a	b	max_c	avg_c	a	b	c	d
1	19	107	107.0000	1	21	909	12
1	19	107	107.0000	1	19	203	107
1	21	500	234.6000	1	21	909	12
6	20	315	279.3333	6	20	315	279
explain select * from v1,t2 where
((v1.max_c>300) and (v1.avg_c>t2.d) and (v1.b=t2.b)) or
((v1.max_c<135) and (v1.max_c<t2.c) and (v1.a=t2.a));
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using temporary; Using filesort
explain format=json select * from v1,t2 where
((v1.max_c>300) and (v1.avg_c>t2.d) and (v1.b=t2.b)) or
((v1.max_c<135) and (v1.max_c<t2.c) and (v1.a=t2.a));
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived2>",
            "access_type": "ALL",
            "loops": 9,
            "rows": 20,
            "cost": "COST_REPLACED",
            "filtered": 100,
            "attached_condition": "v1.max_c > 300 or v1.max_c < 135"
          },
          "buffer_type": "flat",
          "buffer_size": "238",
          "join_type": "BNL",
          "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",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707 and (max_c > 300 or max_c < 135)",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
# conjunctive subformula : pushing into WHERE
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.a>6) and (t2.b>v1.b);
a	b	max_c	avg_c	a	b	c	d
8	33	404	213.6667	8	64	248	107
8	33	404	213.6667	8	80	800	314
select * from v1,t2 where (v1.a>6) and (t2.b>v1.b);
a	b	max_c	avg_c	a	b	c	d
8	33	404	213.6667	8	64	248	107
8	33	404	213.6667	8	80	800	314
explain select * from v1,t2 where (v1.a>6) and (t2.b>v1.b);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
explain format=json select * from v1,t2 where (v1.a>6) and (t2.b>v1.b);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived2>",
            "access_type": "ALL",
            "loops": 9,
            "rows": 20,
            "cost": "COST_REPLACED",
            "filtered": 100,
            "attached_condition": "v1.a > 6"
          },
          "buffer_type": "flat",
          "buffer_size": "238",
          "join_type": "BNL",
          "attached_condition": "t2.b > v1.b",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.a > 6"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v2,t2 where (v2.b>25) and (t2.a<v2.a);
a	b	max_c	avg_c	a	b	c	d
8	33	404	213.6667	2	3	207	207
8	33	404	213.6667	1	21	909	12
8	33	404	213.6667	7	13	312	406
8	33	404	213.6667	6	20	315	279
8	33	404	213.6667	1	19	203	107
8	33	404	213.6667	3	12	231	190
8	33	404	213.6667	6	23	303	909
select * from v2,t2 where (v2.b>25) and (t2.a<v2.a);
a	b	max_c	avg_c	a	b	c	d
8	33	404	213.6667	2	3	207	207
8	33	404	213.6667	1	21	909	12
8	33	404	213.6667	7	13	312	406
8	33	404	213.6667	6	20	315	279
8	33	404	213.6667	1	19	203	107
8	33	404	213.6667	3	12	231	190
8	33	404	213.6667	6	23	303	909
explain select * from v2,t2 where (v2.b>25) and (t2.a<v2.a);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
explain format=json select * from v2,t2 where (v2.b>25) and (t2.a<v2.a);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived2>",
            "access_type": "ALL",
            "loops": 9,
            "rows": 20,
            "cost": "COST_REPLACED",
            "filtered": 100,
            "attached_condition": "v2.b > 25"
          },
          "buffer_type": "flat",
          "buffer_size": "238",
          "join_type": "BNL",
          "attached_condition": "t2.a < v2.a",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.a > 5 and t1.b > 25"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
# extracted or formula : pushing into WHERE
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where
((v1.a>7) and (t2.c<v1.max_c)) or ((v1.a<2) and (t2.b<v1.b));
a	b	max_c	avg_c	a	b	c	d
1	19	107	107.0000	2	3	207	207
1	19	107	107.0000	7	13	312	406
1	19	107	107.0000	3	12	231	190
1	21	500	234.6000	2	3	207	207
1	21	500	234.6000	7	13	312	406
1	21	500	234.6000	6	20	315	279
1	21	500	234.6000	1	19	203	107
1	21	500	234.6000	3	12	231	190
8	33	404	213.6667	2	3	207	207
8	33	404	213.6667	7	13	312	406
8	33	404	213.6667	8	64	248	107
8	33	404	213.6667	6	20	315	279
8	33	404	213.6667	1	19	203	107
8	33	404	213.6667	3	12	231	190
8	33	404	213.6667	6	23	303	909
select * from v1,t2 where
((v1.a>7) and (t2.c<v1.max_c)) or ((v1.a<2) and (t2.b<v1.b));
a	b	max_c	avg_c	a	b	c	d
1	19	107	107.0000	2	3	207	207
1	19	107	107.0000	7	13	312	406
1	19	107	107.0000	3	12	231	190
1	21	500	234.6000	2	3	207	207
1	21	500	234.6000	7	13	312	406
1	21	500	234.6000	6	20	315	279
1	21	500	234.6000	1	19	203	107
1	21	500	234.6000	3	12	231	190
8	33	404	213.6667	2	3	207	207
8	33	404	213.6667	7	13	312	406
8	33	404	213.6667	8	64	248	107
8	33	404	213.6667	6	20	315	279
8	33	404	213.6667	1	19	203	107
8	33	404	213.6667	3	12	231	190
8	33	404	213.6667	6	23	303	909
explain select * from v1,t2 where
((v1.a>7) and (t2.c<v1.max_c)) or ((v1.a<2) and (t2.b<v1.b));
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
explain format=json select * from v1,t2 where
((v1.a>7) and (t2.c<v1.max_c)) or ((v1.a<2) and (t2.b<v1.b));
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived2>",
            "access_type": "ALL",
            "loops": 9,
            "rows": 20,
            "cost": "COST_REPLACED",
            "filtered": 100,
            "attached_condition": "v1.a > 7 or v1.a < 2"
          },
          "buffer_type": "flat",
          "buffer_size": "238",
          "join_type": "BNL",
          "attached_condition": "v1.a > 7 and t2.c < v1.max_c or v1.a < 2 and t2.b < v1.b",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.a > 7 or t1.a < 2"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v2,t2 where
((v2.a>7) and (t2.c<v2.max_c)) or ((v2.a>5) and (t2.b<v2.b));
a	b	max_c	avg_c	a	b	c	d
6	20	315	279.3333	2	3	207	207
6	20	315	279.3333	7	13	312	406
6	20	315	279.3333	1	19	203	107
6	20	315	279.3333	3	12	231	190
8	33	404	213.6667	2	3	207	207
8	33	404	213.6667	1	21	909	12
8	33	404	213.6667	7	13	312	406
8	33	404	213.6667	8	64	248	107
8	33	404	213.6667	6	20	315	279
8	33	404	213.6667	1	19	203	107
8	33	404	213.6667	3	12	231	190
8	33	404	213.6667	6	23	303	909
select * from v2,t2 where
((v2.a>7) and (t2.c<v2.max_c)) or ((v2.a>5) and (t2.b<v2.b));
a	b	max_c	avg_c	a	b	c	d
6	20	315	279.3333	2	3	207	207
6	20	315	279.3333	7	13	312	406
6	20	315	279.3333	1	19	203	107
6	20	315	279.3333	3	12	231	190
8	33	404	213.6667	2	3	207	207
8	33	404	213.6667	1	21	909	12
8	33	404	213.6667	7	13	312	406
8	33	404	213.6667	8	64	248	107
8	33	404	213.6667	6	20	315	279
8	33	404	213.6667	1	19	203	107
8	33	404	213.6667	3	12	231	190
8	33	404	213.6667	6	23	303	909
explain select * from v2,t2 where
((v2.a>7) and (t2.c<v2.max_c)) or ((v2.a>5) and (t2.b<v2.b));
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
explain format=json select * from v2,t2 where
((v2.a>7) and (t2.c<v2.max_c)) or ((v2.a>5) and (t2.b<v2.b));
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived2>",
            "access_type": "ALL",
            "loops": 9,
            "rows": 20,
            "cost": "COST_REPLACED",
            "filtered": 100,
            "attached_condition": "v2.a > 7 or v2.a > 5"
          },
          "buffer_type": "flat",
          "buffer_size": "238",
          "join_type": "BNL",
          "attached_condition": "v2.a > 7 and t2.c < v2.max_c or v2.a > 5 and t2.b < v2.b",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.a > 5 and (t1.a > 7 or t1.a > 5)"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where
((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));
a	b	max_c	avg_c	a	b	c	d
1	19	107	107.0000	8	64	248	107
1	19	107	107.0000	1	19	203	107
5	16	207	207.0000	2	3	207	207
select * from v1,t2 where
((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));
a	b	max_c	avg_c	a	b	c	d
1	19	107	107.0000	8	64	248	107
1	19	107	107.0000	1	19	203	107
5	16	207	207.0000	2	3	207	207
explain select * from v1,t2 where
((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));
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
explain format=json select * from v1,t2 where
((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));
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived2>",
            "access_type": "ALL",
            "loops": 9,
            "rows": 20,
            "cost": "COST_REPLACED",
            "filtered": 100,
            "attached_condition": "v1.a > 4 or v1.a < 2"
          },
          "buffer_type": "flat",
          "buffer_size": "238",
          "join_type": "BNL",
          "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",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.a > 4 or t1.a < 2"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
# conjunctive subformulas : pushing into HAVING and WHERE
set 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);
a	b	max_c	avg_c	a	b	c	d
1	21	500	234.6000	8	64	248	107
1	21	500	234.6000	8	80	800	314
1	21	500	234.6000	6	23	303	909
select * from v1,t2 where (v1.a<2) and (v1.max_c>400) and (t2.b>v1.b);
a	b	max_c	avg_c	a	b	c	d
1	21	500	234.6000	8	64	248	107
1	21	500	234.6000	8	80	800	314
1	21	500	234.6000	6	23	303	909
explain select * from v1,t2 where (v1.a<2) and (v1.max_c>400) and (t2.b>v1.b);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
explain format=json select * from v1,t2 where (v1.a<2) and (v1.max_c>400) and (t2.b>v1.b);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived2>",
            "access_type": "ALL",
            "loops": 9,
            "rows": 20,
            "cost": "COST_REPLACED",
            "filtered": 100,
            "attached_condition": "v1.a < 2 and v1.max_c > 400"
          },
          "buffer_type": "flat",
          "buffer_size": "238",
          "join_type": "BNL",
          "attached_condition": "t2.b > v1.b",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707 and max_c > 400",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.a < 2"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v_double as v,t2_double as t where
(v.a=t.a) and (v.avg_a>0.45) and (v.b>10);
a	avg_a	b	c	a	b	c
1	0.50000000	12.5	18.9	1	22.4	14.3333
1	0.50000000	12.5	18.9	1	12.5	18.9
4	1.00000000	33.4	14.3333	4	33.4	14.3333
4	1.00000000	14.3333	13.65	4	33.4	14.3333
5	1.25000000	17.89	7.22	5	22.4	13.65
6	1.50000000	33.4	14.3	6	33.4	14.3333
10	2.62500000	33.4	13.65	10	31.4	13.65
select * from v_double as v,t2_double as t where
(v.a=t.a) and (v.avg_a>0.45) and (v.b>10);
a	avg_a	b	c	a	b	c
1	0.50000000	12.5	18.9	1	22.4	14.3333
1	0.50000000	12.5	18.9	1	12.5	18.9
4	1.00000000	33.4	14.3333	4	33.4	14.3333
4	1.00000000	14.3333	13.65	4	33.4	14.3333
5	1.25000000	17.89	7.22	5	22.4	13.65
6	1.50000000	33.4	14.3	6	33.4	14.3333
10	2.62500000	33.4	13.65	10	31.4	13.65
explain select * from v_double as v,t2_double as t where
(v.a=t.a) and (v.avg_a>0.45) and (v.b>10);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key0	key0	5	test.t.a	1	Using where
2	DERIVED	t1_double	ALL	NULL	NULL	NULL	NULL	9	Using where; Using temporary; Using filesort
explain format=json select * from v_double as v,t2_double as t where
(v.a=t.a) and (v.avg_a>0.45) and (v.b>10);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t.a"],
          "loops": 9,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v.avg_a > 0.45 and v.b > 10",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "avg_a < 22.333 and avg_a > 0.45",
              "filesort": {
                "sort_key": "t1_double.b, t1_double.c",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1_double",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 9,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1_double.b > 12.2 and t1_double.b > 10"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v_decimal as v,t2_decimal as t where
(v.a=t.a) and (v.avg_c>15) and (v.b>1);
a	b	avg_c	a	b	c
3.0	3.0	16.0000	3.0	3.0	16
select * from v_decimal as v,t2_decimal as t where
(v.a=t.a) and (v.avg_c>15) and (v.b>1);
a	b	avg_c	a	b	c
3.0	3.0	16.0000	3.0	3.0	16
explain select * from v_decimal as v,t2_decimal as t where
(v.a=t.a) and (v.avg_c>15) and (v.b>1);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key0	key0	3	test.t.a	1	Using where
2	DERIVED	t1_decimal	ALL	NULL	NULL	NULL	NULL	9	Using where; Using temporary; Using filesort
explain format=json select * from v_decimal as v,t2_decimal as t where
(v.a=t.a) and (v.avg_c>15) and (v.b>1);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "3",
          "used_key_parts": ["a"],
          "ref": ["test.t.a"],
          "loops": 9,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v.avg_c > 15 and v.b > 1",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "avg_c > 12 and avg_c > 15",
              "filesort": {
                "sort_key": "t1_decimal.a, t1_decimal.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1_decimal",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 9,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1_decimal.b > 1"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
# extracted or formula : pushing into HAVING and WHERE
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where
((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));
a	b	max_c	avg_c	a	b	c	d
1	19	107	107.0000	2	3	207	207
1	19	107	107.0000	7	13	312	406
1	19	107	107.0000	3	12	231	190
8	33	404	213.6667	2	3	207	207
8	33	404	213.6667	7	13	312	406
8	33	404	213.6667	8	64	248	107
8	33	404	213.6667	6	20	315	279
8	33	404	213.6667	1	19	203	107
8	33	404	213.6667	3	12	231	190
8	33	404	213.6667	6	23	303	909
select * from v1,t2 where
((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));
a	b	max_c	avg_c	a	b	c	d
1	19	107	107.0000	2	3	207	207
1	19	107	107.0000	7	13	312	406
1	19	107	107.0000	3	12	231	190
8	33	404	213.6667	2	3	207	207
8	33	404	213.6667	7	13	312	406
8	33	404	213.6667	8	64	248	107
8	33	404	213.6667	6	20	315	279
8	33	404	213.6667	1	19	203	107
8	33	404	213.6667	3	12	231	190
8	33	404	213.6667	6	23	303	909
explain select * from v1,t2 where
((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));
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
explain format=json select * from v1,t2 where
((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));
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived2>",
            "access_type": "ALL",
            "loops": 9,
            "rows": 20,
            "cost": "COST_REPLACED",
            "filtered": 100,
            "attached_condition": "v1.a > 7 and v1.max_c > 300 or v1.a < 4 and v1.max_c < 500"
          },
          "buffer_type": "flat",
          "buffer_size": "238",
          "join_type": "BNL",
          "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",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707 and (t1.a > 7 and max_c > 300 or t1.a < 4 and max_c < 500)",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.a > 7 or t1.a < 4"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
set 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);
a	b	max_c	avg_c	a	b	c	d
1	21	500	234.6000	2	3	207	207
1	21	500	234.6000	1	21	909	12
1	21	500	234.6000	7	13	312	406
1	21	500	234.6000	8	64	248	107
1	21	500	234.6000	6	20	315	279
1	21	500	234.6000	1	19	203	107
1	21	500	234.6000	8	80	800	314
1	21	500	234.6000	3	12	231	190
1	21	500	234.6000	6	23	303	909
8	33	404	213.6667	2	3	207	207
8	33	404	213.6667	1	21	909	12
8	33	404	213.6667	7	13	312	406
8	33	404	213.6667	8	64	248	107
8	33	404	213.6667	6	20	315	279
8	33	404	213.6667	1	19	203	107
8	33	404	213.6667	8	80	800	314
8	33	404	213.6667	3	12	231	190
8	33	404	213.6667	6	23	303	909
select * from v1,t2 where ((v1.a<2) and (v1.max_c>120)) or (v1.a>7);
a	b	max_c	avg_c	a	b	c	d
1	21	500	234.6000	2	3	207	207
1	21	500	234.6000	1	21	909	12
1	21	500	234.6000	7	13	312	406
1	21	500	234.6000	8	64	248	107
1	21	500	234.6000	6	20	315	279
1	21	500	234.6000	1	19	203	107
1	21	500	234.6000	8	80	800	314
1	21	500	234.6000	3	12	231	190
1	21	500	234.6000	6	23	303	909
8	33	404	213.6667	2	3	207	207
8	33	404	213.6667	1	21	909	12
8	33	404	213.6667	7	13	312	406
8	33	404	213.6667	8	64	248	107
8	33	404	213.6667	6	20	315	279
8	33	404	213.6667	1	19	203	107
8	33	404	213.6667	8	80	800	314
8	33	404	213.6667	3	12	231	190
8	33	404	213.6667	6	23	303	909
explain select * from v1,t2 where ((v1.a<2) and (v1.max_c>120)) or (v1.a>7);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
explain format=json select * from v1,t2 where ((v1.a<2) and (v1.max_c>120)) or (v1.a>7);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived2>",
            "access_type": "ALL",
            "loops": 9,
            "rows": 20,
            "cost": "COST_REPLACED",
            "filtered": 100,
            "attached_condition": "v1.a < 2 and v1.max_c > 120 or v1.a > 7"
          },
          "buffer_type": "flat",
          "buffer_size": "238",
          "join_type": "BNL",
          "attached_condition": "v1.a < 2 and v1.max_c > 120 or v1.a > 7",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707 and (t1.a < 2 and max_c > 120 or t1.a > 7)",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.a < 2 or t1.a > 7"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
# extracted or formulas : pushing into WHERE and HAVING
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where
((v1.a<2) and (v1.max_c>120) and (v1.b=t2.b)) or (v1.a>7);
a	b	max_c	avg_c	a	b	c	d
1	21	500	234.6000	1	21	909	12
8	33	404	213.6667	2	3	207	207
8	33	404	213.6667	1	21	909	12
8	33	404	213.6667	7	13	312	406
8	33	404	213.6667	8	64	248	107
8	33	404	213.6667	6	20	315	279
8	33	404	213.6667	1	19	203	107
8	33	404	213.6667	8	80	800	314
8	33	404	213.6667	3	12	231	190
8	33	404	213.6667	6	23	303	909
select * from v1,t2 where
((v1.a<2) and (v1.max_c>120) and (v1.b=t2.b)) or (v1.a>7);
a	b	max_c	avg_c	a	b	c	d
1	21	500	234.6000	1	21	909	12
8	33	404	213.6667	2	3	207	207
8	33	404	213.6667	1	21	909	12
8	33	404	213.6667	7	13	312	406
8	33	404	213.6667	8	64	248	107
8	33	404	213.6667	6	20	315	279
8	33	404	213.6667	1	19	203	107
8	33	404	213.6667	8	80	800	314
8	33	404	213.6667	3	12	231	190
8	33	404	213.6667	6	23	303	909
explain select * from v1,t2 where
((v1.a<2) and (v1.max_c>120) and (v1.b=t2.b)) or (v1.a>7);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
explain format=json select * from v1,t2 where
((v1.a<2) and (v1.max_c>120) and (v1.b=t2.b)) or (v1.a>7);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived2>",
            "access_type": "ALL",
            "loops": 9,
            "rows": 20,
            "cost": "COST_REPLACED",
            "filtered": 100,
            "attached_condition": "v1.a < 2 and v1.max_c > 120 or v1.a > 7"
          },
          "buffer_type": "flat",
          "buffer_size": "238",
          "join_type": "BNL",
          "attached_condition": "v1.b = t2.b and v1.a < 2 and v1.max_c > 120 or v1.a > 7",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707 and (t1.a < 2 and max_c > 120 or t1.a > 7)",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.a < 2 or t1.a > 7"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where
((v1.a<2) and (v1.max_c<200) and (t2.c>v1.max_c) and (v1.max_c=t2.d)) or
((v1.a>4) and (v1.max_c<500) and (t2.b<v1.b) and (v1.max_c=t2.c));
a	b	max_c	avg_c	a	b	c	d
1	19	107	107.0000	8	64	248	107
1	19	107	107.0000	1	19	203	107
5	16	207	207.0000	2	3	207	207
select * from v1,t2 where
((v1.a<2) and (v1.max_c<200) and (t2.c>v1.max_c) and (v1.max_c=t2.d)) or
((v1.a>4) and (v1.max_c<500) and (t2.b<v1.b) and (v1.max_c=t2.c));
a	b	max_c	avg_c	a	b	c	d
1	19	107	107.0000	8	64	248	107
1	19	107	107.0000	1	19	203	107
5	16	207	207.0000	2	3	207	207
explain select * from v1,t2 where
((v1.a<2) and (v1.max_c<200) and (t2.c>v1.max_c) and (v1.max_c=t2.d)) or
((v1.a>4) and (v1.max_c<500) and (t2.b<v1.b) and (v1.max_c=t2.c));
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
explain format=json select * from v1,t2 where
((v1.a<2) and (v1.max_c<200) and (t2.c>v1.max_c) and (v1.max_c=t2.d)) or
((v1.a>4) and (v1.max_c<500) and (t2.b<v1.b) and (v1.max_c=t2.c));
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived2>",
            "access_type": "ALL",
            "loops": 9,
            "rows": 20,
            "cost": "COST_REPLACED",
            "filtered": 100,
            "attached_condition": "v1.a < 2 and v1.max_c < 200 or v1.a > 4"
          },
          "buffer_type": "flat",
          "buffer_size": "238",
          "join_type": "BNL",
          "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",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707 and (t1.a < 2 and max_c < 200 or t1.a > 4 and max_c < 500)",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.a < 2 or t1.a > 4"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
# prepare of a query containing extracted or formula
prepare stmt from "select * from v1,t2 where
  ((v1.max_c>400) and (t2.a>v1.a)) or ((v1.max_c<135) and (t2.a<v1.a));";
execute stmt;
a	b	max_c	avg_c	a	b	c	d
1	21	500	234.6000	2	3	207	207
1	21	500	234.6000	7	13	312	406
1	21	500	234.6000	8	64	248	107
1	21	500	234.6000	6	20	315	279
1	21	500	234.6000	8	80	800	314
1	21	500	234.6000	3	12	231	190
1	21	500	234.6000	6	23	303	909
5	27	132	132.0000	2	3	207	207
5	27	132	132.0000	1	21	909	12
5	27	132	132.0000	1	19	203	107
5	27	132	132.0000	3	12	231	190
execute stmt;
a	b	max_c	avg_c	a	b	c	d
1	21	500	234.6000	2	3	207	207
1	21	500	234.6000	7	13	312	406
1	21	500	234.6000	8	64	248	107
1	21	500	234.6000	6	20	315	279
1	21	500	234.6000	8	80	800	314
1	21	500	234.6000	3	12	231	190
1	21	500	234.6000	6	23	303	909
5	27	132	132.0000	2	3	207	207
5	27	132	132.0000	1	21	909	12
5	27	132	132.0000	1	19	203	107
5	27	132	132.0000	3	12	231	190
deallocate prepare stmt;
prepare stmt from
"explain format=json select * from v1,t2 where
    ((v1.max_c>400) and (t2.a>v1.a)) or ((v1.max_c<135) and (t2.a<v1.a));";
execute stmt;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived2>",
            "access_type": "ALL",
            "loops": 9,
            "rows": 20,
            "cost": "COST_REPLACED",
            "filtered": 100,
            "attached_condition": "v1.max_c > 400 or v1.max_c < 135"
          },
          "buffer_type": "flat",
          "buffer_size": "238",
          "join_type": "BNL",
          "attached_condition": "v1.max_c > 400 and t2.a > v1.a or v1.max_c < 135 and t2.a < v1.a",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707 and (max_c > 400 or max_c < 135)",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
execute stmt;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived2>",
            "access_type": "ALL",
            "loops": 9,
            "rows": 20,
            "cost": "COST_REPLACED",
            "filtered": 100,
            "attached_condition": "v1.max_c > 400 or v1.max_c < 135"
          },
          "buffer_type": "flat",
          "buffer_size": "238",
          "join_type": "BNL",
          "attached_condition": "v1.max_c > 400 and t2.a > v1.a or v1.max_c < 135 and t2.a < v1.a",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707 and (max_c > 400 or max_c < 135)",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
deallocate prepare stmt;
# conjunctive subformula : pushing into WHERE
# pushing equalities
set 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);
a	b	max_c	avg_c	a	b	c	d
1	21	500	234.6000	1	21	909	12
1	19	107	107.0000	1	19	203	107
select * from v1,t2 where (t2.a=v1.a) and (v1.b=t2.b) and (v1.a=1);
a	b	max_c	avg_c	a	b	c	d
1	21	500	234.6000	1	21	909	12
1	19	107	107.0000	1	19	203	107
explain select * from v1,t2 where (t2.a=v1.a) and (v1.b=t2.b) and (v1.a=1);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.b	2	Using where
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
explain format=json select * from v1,t2 where (t2.a=v1.a) and (v1.b=t2.b) and (v1.a=1);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a = 1 and t2.b is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "5",
          "used_key_parts": ["b"],
          "ref": ["test.t2.b"],
          "loops": 9,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v1.a = 1",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707",
              "filesort": {
                "sort_key": "t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.a = 1"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.a=5) and (v1.max_c=t2.d);
a	b	max_c	avg_c	a	b	c	d
5	16	207	207.0000	2	3	207	207
select * from v1,t2 where (v1.a=5) and (v1.max_c=t2.d);
a	b	max_c	avg_c	a	b	c	d
5	16	207	207.0000	2	3	207	207
explain select * from v1,t2 where (v1.a=5) and (v1.max_c=t2.d);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.d	2	Using where
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
explain format=json select * from v1,t2 where (v1.a=5) and (v1.max_c=t2.d);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.d is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "5",
          "used_key_parts": ["max_c"],
          "ref": ["test.t2.d"],
          "loops": 9,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v1.a = 5 and v1.max_c = t2.d",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707",
              "filesort": {
                "sort_key": "t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.a = 5"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
# conjunctive subformula : pushing into WHERE using equalities
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (t2.a<5) and (v1.a=t2.a);
a	b	max_c	avg_c	a	b	c	d
1	21	500	234.6000	1	21	909	12
1	19	107	107.0000	1	21	909	12
1	21	500	234.6000	1	19	203	107
1	19	107	107.0000	1	19	203	107
select * from v1,t2 where (t2.a<5) and (v1.a=t2.a);
a	b	max_c	avg_c	a	b	c	d
1	21	500	234.6000	1	21	909	12
1	19	107	107.0000	1	21	909	12
1	21	500	234.6000	1	19	203	107
1	19	107	107.0000	1	19	203	107
explain select * from v1,t2 where (t2.a<5) and (v1.a=t2.a);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.a	2	
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
explain format=json select * from v1,t2 where (t2.a<5) and (v1.a=t2.a);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a < 5 and t2.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t2.a"],
          "loops": 9,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.a < 5"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.a=v1.b) and (v1.a=t2.a);
a	b	max_c	avg_c	a	b	c	d
select * from v1,t2 where (v1.a=v1.b) and (v1.a=t2.a);
a	b	max_c	avg_c	a	b	c	d
explain select * from v1,t2 where (v1.a=v1.b) and (v1.a=t2.a);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key0	key0	10	test.t2.a,test.t2.a	2	
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
explain format=json select * from v1,t2 where (v1.a=v1.b) and (v1.a=t2.a);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a is not null and t2.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "10",
          "used_key_parts": ["a", "b"],
          "ref": ["test.t2.a", "test.t2.a"],
          "loops": 9,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.b = t1.a"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
# conjunctive subformula : pushing into HAVING using equalities
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (t2.c>150) and (v1.max_c=t2.c);
a	b	max_c	avg_c	a	b	c	d
5	16	207	207.0000	2	3	207	207
6	20	315	279.3333	6	20	315	279
select * from v1,t2 where (t2.c>150) and (v1.max_c=t2.c);
a	b	max_c	avg_c	a	b	c	d
5	16	207	207.0000	2	3	207	207
6	20	315	279.3333	6	20	315	279
explain select * from v1,t2 where (t2.c>150) and (v1.max_c=t2.c);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.c	2	
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using temporary; Using filesort
explain format=json select * from v1,t2 where (t2.c>150) and (v1.max_c=t2.c);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.c > 150 and t2.c is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "5",
          "used_key_parts": ["max_c"],
          "ref": ["test.t2.c"],
          "loops": 9,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707 and max_c > 150",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
# extracted and formula : pushing into WHERE
# pushing equalities
set 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);
a	b	max_c	avg_c	a	b	c	d
select * from v1,t2 where (v1.a=v1.b) and (v1.a=t2.a) and (v1.a=3);
a	b	max_c	avg_c	a	b	c	d
explain select * from v1,t2 where (v1.a=v1.b) and (v1.a=t2.a) and (v1.a=3);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where
explain format=json select * from v1,t2 where (v1.a=v1.b) and (v1.a=t2.a) and (v1.a=3);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a = 3"
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived2>",
            "access_type": "ALL",
            "loops": 9,
            "rows": 20,
            "cost": "COST_REPLACED",
            "filtered": 100,
            "attached_condition": "v1.a = 3 and v1.b = 3"
          },
          "buffer_type": "flat",
          "buffer_size": "238",
          "join_type": "BNL",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707",
              "nested_loop": [
                {
                  "table": {
                    "table_name": "t1",
                    "access_type": "ALL",
                    "loops": 1,
                    "rows": 20,
                    "cost": "COST_REPLACED",
                    "filtered": 100,
                    "attached_condition": "t1.a = 3 and t1.b = 3"
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}
set 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);
a	b	max_c	avg_c	a	b	c	d
1	21	500	234.6000	2	3	207	207
select * from v1,t2 where (v1.a=1) and (v1.b=21) and (t2.a=2);
a	b	max_c	avg_c	a	b	c	d
1	21	500	234.6000	2	3	207	207
explain select * from v1,t2 where (v1.a=1) and (v1.b=21) and (t2.a=2);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where
explain format=json select * from v1,t2 where (v1.a=1) and (v1.b=21) and (t2.a=2);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a = 2"
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived2>",
            "access_type": "ALL",
            "loops": 9,
            "rows": 20,
            "cost": "COST_REPLACED",
            "filtered": 100,
            "attached_condition": "v1.a = 1 and v1.b = 21"
          },
          "buffer_type": "flat",
          "buffer_size": "238",
          "join_type": "BNL",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707",
              "nested_loop": [
                {
                  "table": {
                    "table_name": "t1",
                    "access_type": "ALL",
                    "loops": 1,
                    "rows": 20,
                    "cost": "COST_REPLACED",
                    "filtered": 100,
                    "attached_condition": "t1.a = 1 and t1.b = 21"
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v_char as v,t2_char as t where
(v.a='c') and (v.b<'Hermes') and ((v.b=t.b) or (v.max_c>20));
a	b	max_c	a	b	c
c	Harry	4	d	Harry	1
c	Harry	4	b	Harry	4
select * from v_char as v,t2_char as t where
(v.a='c') and (v.b<'Hermes') and ((v.b=t.b) or (v.max_c>20));
a	b	max_c	a	b	c
c	Harry	4	d	Harry	1
c	Harry	4	b	Harry	4
explain select * from v_char as v,t2_char as t where
(v.a='c') and (v.b<'Hermes') and ((v.b=t.b) or (v.max_c>20));
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	12	Using where
1	PRIMARY	t	ALL	NULL	NULL	NULL	NULL	12	Using where; Using join buffer (flat, BNL join)
2	DERIVED	t1_char	ALL	NULL	NULL	NULL	NULL	12	Using where; Using temporary; Using filesort
explain format=json select * from v_char as v,t2_char as t where
(v.a='c') and (v.b<'Hermes') and ((v.b=t.b) or (v.max_c>20));
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 12,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v.a = 'c' and v.b < 'Hermes'",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 9",
              "filesort": {
                "sort_key": "t1_char.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1_char",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 12,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1_char.a = 'c' and t1_char.b < 'Hermes'"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "t",
            "access_type": "ALL",
            "loops": 12,
            "rows": 12,
            "cost": "COST_REPLACED",
            "filtered": 100
          },
          "buffer_type": "flat",
          "buffer_size": "220",
          "join_type": "BNL",
          "attached_condition": "t.b = v.b or v.max_c > 20"
        }
      }
    ]
  }
}
# extracted and formula : pushing into WHERE using equalities
# pushing equalities
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v_decimal as v,t2_decimal as t where
(v.a=v.b) and (v.b=t.b) and ((t.b>1) or (v.a=1));
a	b	avg_c	a	b	c
1.0	1.0	16.3333	2.0	1.0	13
3.0	3.0	16.0000	3.0	3.0	16
3.0	3.0	16.0000	1.0	3.0	22
3.0	3.0	16.0000	1.0	3.0	14
1.0	1.0	16.3333	2.0	1.0	43
3.0	3.0	16.0000	2.0	3.0	11
3.0	3.0	16.0000	2.0	3.0	16
select * from v_decimal as v,t2_decimal as t where
(v.a=v.b) and (v.b=t.b) and ((t.b>1) or (v.a=1));
a	b	avg_c	a	b	c
1.0	1.0	16.3333	2.0	1.0	13
3.0	3.0	16.0000	3.0	3.0	16
3.0	3.0	16.0000	1.0	3.0	22
3.0	3.0	16.0000	1.0	3.0	14
1.0	1.0	16.3333	2.0	1.0	43
3.0	3.0	16.0000	2.0	3.0	11
3.0	3.0	16.0000	2.0	3.0	16
explain select * from v_decimal as v,t2_decimal as t where
(v.a=v.b) and (v.b=t.b) and ((t.b>1) or (v.a=1));
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key0	key0	6	test.t.b,test.t.b	1	
2	DERIVED	t1_decimal	ALL	NULL	NULL	NULL	NULL	9	Using where; Using temporary; Using filesort
explain format=json select * from v_decimal as v,t2_decimal as t where
(v.a=v.b) and (v.b=t.b) and ((t.b>1) or (v.a=1));
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "(t.b > 1 or t.b = 1) and t.b is not null and t.b is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "6",
          "used_key_parts": ["a", "b"],
          "ref": ["test.t.b", "test.t.b"],
          "loops": 9,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "avg_c > 12",
              "filesort": {
                "sort_key": "t1_decimal.a, t1_decimal.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1_decimal",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 9,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1_decimal.b = t1_decimal.a and (t1_decimal.a > 1 or t1_decimal.a = 1)"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
# extracted or formula : pushing into HAVING using equalities
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2
where ((t2.a<4) and (v1.a=t2.a)) or ((t2.c>150) and (v1.max_c=t2.c));
a	b	max_c	avg_c	a	b	c	d
1	19	107	107.0000	1	21	909	12
1	19	107	107.0000	1	19	203	107
1	21	500	234.6000	1	21	909	12
1	21	500	234.6000	1	19	203	107
5	16	207	207.0000	2	3	207	207
6	20	315	279.3333	6	20	315	279
select * from v1,t2
where ((t2.a<4) and (v1.a=t2.a)) or ((t2.c>150) and (v1.max_c=t2.c));
a	b	max_c	avg_c	a	b	c	d
1	19	107	107.0000	1	21	909	12
1	19	107	107.0000	1	19	203	107
1	21	500	234.6000	1	21	909	12
1	21	500	234.6000	1	19	203	107
5	16	207	207.0000	2	3	207	207
6	20	315	279.3333	6	20	315	279
explain select * from v1,t2
where ((t2.a<4) and (v1.a=t2.a)) or ((t2.c>150) and (v1.max_c=t2.c));
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using temporary; Using filesort
explain format=json select * from v1,t2
where ((t2.a<4) and (v1.a=t2.a)) or ((t2.c>150) and (v1.max_c=t2.c));
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a < 4 or t2.c > 150"
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived2>",
            "access_type": "ALL",
            "loops": 9,
            "rows": 20,
            "cost": "COST_REPLACED",
            "filtered": 100
          },
          "buffer_type": "flat",
          "buffer_size": "238",
          "join_type": "BNL",
          "attached_condition": "v1.a = t2.a and t2.a < 4 or v1.max_c = t2.c and t2.c > 150",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707 and (t1.a < 4 or max_c > 150)",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
# conjunctive subformulas : pushing into WHERE and HAVING using equalities
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2
where ((t2.a>5) and (v1.a=t2.a)) and ((t2.c>250) and (v1.max_c=t2.c));
a	b	max_c	avg_c	a	b	c	d
6	20	315	279.3333	6	20	315	279
select * from v1,t2
where ((t2.a>5) and (v1.a=t2.a)) and ((t2.c>250) and (v1.max_c=t2.c));
a	b	max_c	avg_c	a	b	c	d
6	20	315	279.3333	6	20	315	279
explain select * from v1,t2
where ((t2.a>5) and (v1.a=t2.a)) and ((t2.c>250) and (v1.max_c=t2.c));
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key0	key0	10	test.t2.a,test.t2.c	2	
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
explain format=json select * from v1,t2
where ((t2.a>5) and (v1.a=t2.a)) and ((t2.c>250) and (v1.max_c=t2.c));
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a > 5 and t2.c > 250 and t2.a is not null and t2.c is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "10",
          "used_key_parts": ["a", "max_c"],
          "ref": ["test.t2.a", "test.t2.c"],
          "loops": 9,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707 and max_c > 250",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.a > 5"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
# conjunctive subformulas : pushing into WHERE and HAVING
# pushing equalities
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from
(select a, b, max(c) as max_c, avg(c) as avg_c from t1
group by a,b having max_c < 707) v1,
t2 where (v1.a=8) and (v1.a=t2.a) and (v1.max_c=404);
a	b	max_c	avg_c	a	b	c	d
8	33	404	213.6667	8	64	248	107
8	33	404	213.6667	8	80	800	314
select * from
(select a, b, max(c) as max_c, avg(c) as avg_c from t1
group by a,b having max_c < 707) v1,
t2 where (v1.a=8) and (v1.a=t2.a) and (v1.max_c=404);
a	b	max_c	avg_c	a	b	c	d
8	33	404	213.6667	8	64	248	107
8	33	404	213.6667	8	80	800	314
explain select * from
(select a, b, max(c) as max_c, avg(c) as avg_c from t1
group by a,b having max_c < 707) v1,
t2 where (v1.a=8) and (v1.a=t2.a) and (v1.max_c=404);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
explain format=json select * from
(select a, b, max(c) as max_c, avg(c) as avg_c from t1
group by a,b having max_c < 707) v1,
t2 where (v1.a=8) and (v1.a=t2.a) and (v1.max_c=404);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a = 8"
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived2>",
            "access_type": "ALL",
            "loops": 9,
            "rows": 20,
            "cost": "COST_REPLACED",
            "filtered": 100,
            "attached_condition": "v1.a = 8 and v1.max_c = 404"
          },
          "buffer_type": "flat",
          "buffer_size": "238",
          "join_type": "BNL",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c = 404",
              "filesort": {
                "sort_key": "t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.a = 8"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
# conjunctive subformulas : pushing into WHERE and HAVING
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where
(v1.a>3) and (v1.max_c>200) and (t2.b<v1.b) and (t2.d=v1.max_c);
a	b	max_c	avg_c	a	b	c	d
5	16	207	207.0000	2	3	207	207
select * from v1,t2 where
(v1.a>3) and (v1.max_c>200) and (t2.b<v1.b) and (t2.d=v1.max_c);
a	b	max_c	avg_c	a	b	c	d
5	16	207	207.0000	2	3	207	207
explain select * from v1,t2 where
(v1.a>3) and (v1.max_c>200) and (t2.b<v1.b) and (t2.d=v1.max_c);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.d	2	Using where
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
explain format=json select * from v1,t2 where
(v1.a>3) and (v1.max_c>200) and (t2.b<v1.b) and (t2.d=v1.max_c);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.d is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "5",
          "used_key_parts": ["max_c"],
          "ref": ["test.t2.d"],
          "loops": 9,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v1.a > 3 and v1.max_c > 200 and t2.b < v1.b and t2.d = v1.max_c",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707 and max_c > 200",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.a > 3"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
# conjunctive subformula : pushing into WHERE
# extracted or formula : pushing into HAVING using equalities
# pushing equalities
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v_double as v,t2_double as t where
(v.b=v.c) and (v.c=t.c) and ((t.c>10) or (v.a=1));
a	avg_a	b	c	a	b	c
select * from v_double as v,t2_double as t where
(v.b=v.c) and (v.c=t.c) and ((t.c>10) or (v.a=1));
a	avg_a	b	c	a	b	c
explain select * from v_double as v,t2_double as t where
(v.b=v.c) and (v.c=t.c) and ((t.c>10) or (v.a=1));
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key0	key0	18	test.t.c,test.t.c	1	Using where
2	DERIVED	t1_double	ALL	NULL	NULL	NULL	NULL	9	Using where; Using temporary; Using filesort
explain format=json select * from v_double as v,t2_double as t where
(v.b=v.c) and (v.c=t.c) and ((t.c>10) or (v.a=1));
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t.c is not null and t.c is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "18",
          "used_key_parts": ["b", "c"],
          "ref": ["test.t.c", "test.t.c"],
          "loops": 9,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t.c > 10 or v.a = 1",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "avg_a < 22.333 and (t1_double.b > 10 or t1_double.a = 1)",
              "filesort": {
                "sort_key": "t1_double.b, t1_double.c",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1_double",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 9,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1_double.c = t1_double.b and t1_double.b > 12.2"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
# conjunctive subformula : pushing into WHERE
# extracted or formula : pushing into HAVING using equalities
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v_double as v,t2_double as t where
(((v.a>0.2) or (v.b<17)) or (t.c>17)) and (t.c=v.c) and (v.c>18);
a	avg_a	b	c	a	b	c
1	0.50000000	12.5	18.9	1	12.5	18.9
1	0.50000000	12.5	18.9	2	22.4	18.9
1	0.50000000	12.5	18.9	7	17.89	18.9
select * from v_double as v,t2_double as t where
(((v.a>0.2) or (v.b<17)) or (t.c>17)) and (t.c=v.c) and (v.c>18);
a	avg_a	b	c	a	b	c
1	0.50000000	12.5	18.9	1	12.5	18.9
1	0.50000000	12.5	18.9	2	22.4	18.9
1	0.50000000	12.5	18.9	7	17.89	18.9
explain select * from v_double as v,t2_double as t where
(((v.a>0.2) or (v.b<17)) or (t.c>17)) and (t.c=v.c) and (v.c>18);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key0	key0	9	test.t.c	1	Using where
2	DERIVED	t1_double	ALL	NULL	NULL	NULL	NULL	9	Using where; Using temporary; Using filesort
explain format=json select * from v_double as v,t2_double as t where
(((v.a>0.2) or (v.b<17)) or (t.c>17)) and (t.c=v.c) and (v.c>18);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t.c > 18 and t.c is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "9",
          "used_key_parts": ["c"],
          "ref": ["test.t.c"],
          "loops": 9,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v.a > 0.2 or v.b < 17 or t.c > 17",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "avg_a < 22.333 and (t1_double.a > 0.2 or t1_double.b < 17 or t1_double.c > 17)",
              "filesort": {
                "sort_key": "t1_double.b, t1_double.c",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1_double",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 9,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1_double.b > 12.2 and t1_double.c > 18"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
# extracted or formula : pushing into WHERE
# conjunctive subformula : pushing into HAVING
# pushing equalities
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v_decimal as v,t2_decimal as t where
(((v.a>4) or (v.a=2)) or (v.b>3)) and (v.avg_c=13);
a	b	avg_c	a	b	c
2.0	1.0	13.0000	2.0	1.0	13
2.0	3.0	13.0000	2.0	1.0	13
2.0	1.0	13.0000	2.0	2.0	11
2.0	3.0	13.0000	2.0	2.0	11
2.0	1.0	13.0000	3.0	3.0	16
2.0	3.0	13.0000	3.0	3.0	16
2.0	1.0	13.0000	1.0	3.0	22
2.0	3.0	13.0000	1.0	3.0	22
2.0	1.0	13.0000	1.0	3.0	14
2.0	3.0	13.0000	1.0	3.0	14
2.0	1.0	13.0000	2.0	2.0	15
2.0	3.0	13.0000	2.0	2.0	15
2.0	1.0	13.0000	2.0	1.0	43
2.0	3.0	13.0000	2.0	1.0	43
2.0	1.0	13.0000	2.0	3.0	11
2.0	3.0	13.0000	2.0	3.0	11
2.0	1.0	13.0000	2.0	3.0	16
2.0	3.0	13.0000	2.0	3.0	16
select * from v_decimal as v,t2_decimal as t where
(((v.a>4) or (v.a=2)) or (v.b>3)) and (v.avg_c=13);
a	b	avg_c	a	b	c
2.0	1.0	13.0000	2.0	1.0	13
2.0	3.0	13.0000	2.0	1.0	13
2.0	1.0	13.0000	2.0	2.0	11
2.0	3.0	13.0000	2.0	2.0	11
2.0	1.0	13.0000	3.0	3.0	16
2.0	3.0	13.0000	3.0	3.0	16
2.0	1.0	13.0000	1.0	3.0	22
2.0	3.0	13.0000	1.0	3.0	22
2.0	1.0	13.0000	1.0	3.0	14
2.0	3.0	13.0000	1.0	3.0	14
2.0	1.0	13.0000	2.0	2.0	15
2.0	3.0	13.0000	2.0	2.0	15
2.0	1.0	13.0000	2.0	1.0	43
2.0	3.0	13.0000	2.0	1.0	43
2.0	1.0	13.0000	2.0	3.0	11
2.0	3.0	13.0000	2.0	3.0	11
2.0	1.0	13.0000	2.0	3.0	16
2.0	3.0	13.0000	2.0	3.0	16
explain select * from v_decimal as v,t2_decimal as t where
(((v.a>4) or (v.a=2)) or (v.b>3)) and (v.avg_c=13);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	t	ALL	NULL	NULL	NULL	NULL	9	Using join buffer (flat, BNL join)
2	DERIVED	t1_decimal	ALL	NULL	NULL	NULL	NULL	9	Using where; Using temporary; Using filesort
explain format=json select * from v_decimal as v,t2_decimal as t where
(((v.a>4) or (v.a=2)) or (v.b>3)) and (v.avg_c=13);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "(v.a > 4 or v.a = 2 or v.b > 3) and v.avg_c = 13",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "avg_c > 12 and avg_c = 13",
              "filesort": {
                "sort_key": "t1_decimal.a, t1_decimal.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1_decimal",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 9,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1_decimal.a > 4 or t1_decimal.a = 2 or t1_decimal.b > 3"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "t",
            "access_type": "ALL",
            "loops": 9,
            "rows": 9,
            "cost": "COST_REPLACED",
            "filtered": 100
          },
          "buffer_type": "flat",
          "buffer_size": "162",
          "join_type": "BNL"
        }
      }
    ]
  }
}
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from
(select a, b, max(c) as max_c, avg(c) as avg_c from t1
where t1.a>5 group by a,b having max_c < 707) v1,
t2 where (v1.a=t2.a) and (v1.max_c>300) and (v1.a=v1.b);
a	b	max_c	avg_c	a	b	c	d
select * from
(select a, b, max(c) as max_c, avg(c) as avg_c from t1
where t1.a>5 group by a,b having max_c < 707) v1,
t2 where (v1.a=t2.a) and (v1.max_c>300) and (v1.a=v1.b);
a	b	max_c	avg_c	a	b	c	d
explain select * from
(select a, b, max(c) as max_c, avg(c) as avg_c from t1
where t1.a>5 group by a,b having max_c < 707) v1,
t2 where (v1.a=t2.a) and (v1.max_c>300) and (v1.a=v1.b);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key0	key0	10	test.t2.a,test.t2.a	2	Using where
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
explain format=json select * from
(select a, b, max(c) as max_c, avg(c) as avg_c from t1
where t1.a>5 group by a,b having max_c < 707) v1,
t2 where (v1.a=t2.a) and (v1.max_c>300) and (v1.a=v1.b);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a is not null and t2.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "10",
          "used_key_parts": ["a", "b"],
          "ref": ["test.t2.a", "test.t2.a"],
          "loops": 9,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v1.max_c > 300",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707 and max_c > 300",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.b = t1.a and t1.a > 5"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
# nothing to push
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (t2.a<2) and (t2.c>900);
a	b	max_c	avg_c	a	b	c	d
1	19	107	107.0000	1	21	909	12
1	21	500	234.6000	1	21	909	12
5	16	207	207.0000	1	21	909	12
5	27	132	132.0000	1	21	909	12
6	20	315	279.3333	1	21	909	12
8	33	404	213.6667	1	21	909	12
select * from v1,t2 where (t2.a<2) and (t2.c>900);
a	b	max_c	avg_c	a	b	c	d
1	19	107	107.0000	1	21	909	12
1	21	500	234.6000	1	21	909	12
5	16	207	207.0000	1	21	909	12
5	27	132	132.0000	1	21	909	12
6	20	315	279.3333	1	21	909	12
8	33	404	213.6667	1	21	909	12
explain select * from v1,t2 where (t2.a<2) and (t2.c>900);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using join buffer (flat, BNL join)
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using temporary; Using filesort
explain format=json select * from v1,t2 where (t2.a<2) and (t2.c>900);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a < 2 and t2.c > 900"
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived2>",
            "access_type": "ALL",
            "loops": 9,
            "rows": 20,
            "cost": "COST_REPLACED",
            "filtered": 100
          },
          "buffer_type": "flat",
          "buffer_size": "238",
          "join_type": "BNL",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.a=t2.a) and (v1.b=t2.b);
a	b	max_c	avg_c	a	b	c	d
1	21	500	234.6000	1	21	909	12
6	20	315	279.3333	6	20	315	279
1	19	107	107.0000	1	19	203	107
select * from v1,t2 where (v1.a=t2.a) and (v1.b=t2.b);
a	b	max_c	avg_c	a	b	c	d
1	21	500	234.6000	1	21	909	12
6	20	315	279.3333	6	20	315	279
1	19	107	107.0000	1	19	203	107
explain select * from v1,t2 where (v1.a=t2.a) and (v1.b=t2.b);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key0	key0	10	test.t2.a,test.t2.b	2	
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using temporary; Using filesort
explain format=json select * from v1,t2 where (v1.a=t2.a) and (v1.b=t2.b);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a is not null and t2.b is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "10",
          "used_key_parts": ["a", "b"],
          "ref": ["test.t2.a", "test.t2.b"],
          "loops": 9,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where
(t2.a=v1.a) or (v1.b=t2.b) and ((v1.a=1) or (v1.a=6));
a	b	max_c	avg_c	a	b	c	d
1	19	107	107.0000	1	21	909	12
1	19	107	107.0000	1	19	203	107
1	21	500	234.6000	1	21	909	12
1	21	500	234.6000	1	19	203	107
6	20	315	279.3333	6	20	315	279
6	20	315	279.3333	6	23	303	909
8	33	404	213.6667	8	64	248	107
8	33	404	213.6667	8	80	800	314
select * from v1,t2 where
(t2.a=v1.a) or (v1.b=t2.b) and ((v1.a=1) or (v1.a=6));
a	b	max_c	avg_c	a	b	c	d
1	19	107	107.0000	1	21	909	12
1	19	107	107.0000	1	19	203	107
1	21	500	234.6000	1	21	909	12
1	21	500	234.6000	1	19	203	107
6	20	315	279.3333	6	20	315	279
6	20	315	279.3333	6	23	303	909
8	33	404	213.6667	8	64	248	107
8	33	404	213.6667	8	80	800	314
explain select * from v1,t2 where
(t2.a=v1.a) or (v1.b=t2.b) and ((v1.a=1) or (v1.a=6));
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using temporary; Using filesort
explain format=json select * from v1,t2 where
(t2.a=v1.a) or (v1.b=t2.b) and ((v1.a=1) or (v1.a=6));
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived2>",
            "access_type": "ALL",
            "loops": 9,
            "rows": 20,
            "cost": "COST_REPLACED",
            "filtered": 100
          },
          "buffer_type": "flat",
          "buffer_size": "238",
          "join_type": "BNL",
          "attached_condition": "v1.a = t2.a or v1.b = t2.b and (v1.a = 1 or v1.a = 6)",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
set 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);
a	b	max_c	avg_c	a	b	c	d
1	19	107	107.0000	2	3	207	207
1	19	107	107.0000	1	21	909	12
1	19	107	107.0000	7	13	312	406
1	19	107	107.0000	8	64	248	107
1	19	107	107.0000	6	20	315	279
1	19	107	107.0000	1	19	203	107
1	19	107	107.0000	8	80	800	314
1	19	107	107.0000	3	12	231	190
1	19	107	107.0000	6	23	303	909
1	21	500	234.6000	2	3	207	207
1	21	500	234.6000	1	21	909	12
1	21	500	234.6000	7	13	312	406
1	21	500	234.6000	8	64	248	107
1	21	500	234.6000	6	20	315	279
1	21	500	234.6000	1	19	203	107
1	21	500	234.6000	8	80	800	314
1	21	500	234.6000	3	12	231	190
1	21	500	234.6000	6	23	303	909
5	16	207	207.0000	2	3	207	207
5	27	132	132.0000	2	3	207	207
6	20	315	279.3333	2	3	207	207
8	33	404	213.6667	2	3	207	207
select * from v1,t2 where (v1.a=1) or (v1.b=21) or (t2.a=2);
a	b	max_c	avg_c	a	b	c	d
1	19	107	107.0000	2	3	207	207
1	19	107	107.0000	1	21	909	12
1	19	107	107.0000	7	13	312	406
1	19	107	107.0000	8	64	248	107
1	19	107	107.0000	6	20	315	279
1	19	107	107.0000	1	19	203	107
1	19	107	107.0000	8	80	800	314
1	19	107	107.0000	3	12	231	190
1	19	107	107.0000	6	23	303	909
1	21	500	234.6000	2	3	207	207
1	21	500	234.6000	1	21	909	12
1	21	500	234.6000	7	13	312	406
1	21	500	234.6000	8	64	248	107
1	21	500	234.6000	6	20	315	279
1	21	500	234.6000	1	19	203	107
1	21	500	234.6000	8	80	800	314
1	21	500	234.6000	3	12	231	190
1	21	500	234.6000	6	23	303	909
5	16	207	207.0000	2	3	207	207
5	27	132	132.0000	2	3	207	207
6	20	315	279.3333	2	3	207	207
8	33	404	213.6667	2	3	207	207
explain select * from v1,t2 where (v1.a=1) or (v1.b=21) or (t2.a=2);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using temporary; Using filesort
explain format=json select * from v1,t2 where (v1.a=1) or (v1.b=21) or (t2.a=2);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived2>",
            "access_type": "ALL",
            "loops": 9,
            "rows": 20,
            "cost": "COST_REPLACED",
            "filtered": 100
          },
          "buffer_type": "flat",
          "buffer_size": "238",
          "join_type": "BNL",
          "attached_condition": "v1.a = 1 or v1.b = 21 or t2.a = 2",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where
(t2.a<2) and (t2.c>900) and ((v1.a<t2.a) or (t2.a<11));
a	b	max_c	avg_c	a	b	c	d
1	19	107	107.0000	1	21	909	12
1	21	500	234.6000	1	21	909	12
5	16	207	207.0000	1	21	909	12
5	27	132	132.0000	1	21	909	12
6	20	315	279.3333	1	21	909	12
8	33	404	213.6667	1	21	909	12
select * from v1,t2 where
(t2.a<2) and (t2.c>900) and ((v1.a<t2.a) or (t2.a<11));
a	b	max_c	avg_c	a	b	c	d
1	19	107	107.0000	1	21	909	12
1	21	500	234.6000	1	21	909	12
5	16	207	207.0000	1	21	909	12
5	27	132	132.0000	1	21	909	12
6	20	315	279.3333	1	21	909	12
8	33	404	213.6667	1	21	909	12
explain select * from v1,t2 where
(t2.a<2) and (t2.c>900) and ((v1.a<t2.a) or (t2.a<11));
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using temporary; Using filesort
explain format=json select * from v1,t2 where
(t2.a<2) and (t2.c>900) and ((v1.a<t2.a) or (t2.a<11));
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a < 2 and t2.c > 900"
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived2>",
            "access_type": "ALL",
            "loops": 9,
            "rows": 20,
            "cost": "COST_REPLACED",
            "filtered": 100
          },
          "buffer_type": "flat",
          "buffer_size": "238",
          "join_type": "BNL",
          "attached_condition": "v1.a < t2.a or t2.a < 11",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
# using several derived tables : nothing to push
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,v2,t2 where
(v1.a=v2.a) and (v1.a=t2.a) and (v2.b<50);
a	b	max_c	avg_c	a	b	max_c	avg_c	a	b	c	d
8	33	404	213.6667	8	33	404	213.6667	8	64	248	107
6	20	315	279.3333	6	20	315	279.3333	6	20	315	279
8	33	404	213.6667	8	33	404	213.6667	8	80	800	314
6	20	315	279.3333	6	20	315	279.3333	6	23	303	909
select * from v1,v2,t2 where
(v1.a=v2.a) and (v1.a=t2.a) and (v2.b<50);
a	b	max_c	avg_c	a	b	max_c	avg_c	a	b	c	d
8	33	404	213.6667	8	33	404	213.6667	8	64	248	107
6	20	315	279.3333	6	20	315	279.3333	6	20	315	279
8	33	404	213.6667	8	33	404	213.6667	8	80	800	314
6	20	315	279.3333	6	20	315	279.3333	6	23	303	909
explain select * from v1,v2,t2 where
(v1.a=v2.a) and (v1.a=t2.a) and (v2.b<50);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key1	key1	5	test.t2.a	2	
1	PRIMARY	<derived3>	ref	key0	key0	5	test.t2.a	2	Using where
3	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using temporary; Using filesort
explain format=json select * from v1,v2,t2 where
(v1.a=v2.a) and (v1.a=t2.a) and (v2.b<50);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a is not null and t2.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key1"],
          "key": "key1",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t2.a"],
          "loops": 9,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      },
      {
        "table": {
          "table_name": "<derived3>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t2.a"],
          "loops": 18,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v2.b < 50",
          "materialized": {
            "query_block": {
              "select_id": 3,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.a > 5 and t1.b < 50"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
set 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 (t2.b<50) and (v1.b=v2.b);
a	b	max_c	avg_c	a	b	max_c	avg_c	a	b	c	d
6	20	315	279.3333	6	20	315	279.3333	2	3	207	207
8	33	404	213.6667	8	33	404	213.6667	2	3	207	207
6	20	315	279.3333	6	20	315	279.3333	1	21	909	12
8	33	404	213.6667	8	33	404	213.6667	1	21	909	12
6	20	315	279.3333	6	20	315	279.3333	7	13	312	406
8	33	404	213.6667	8	33	404	213.6667	7	13	312	406
6	20	315	279.3333	6	20	315	279.3333	6	20	315	279
8	33	404	213.6667	8	33	404	213.6667	6	20	315	279
6	20	315	279.3333	6	20	315	279.3333	1	19	203	107
8	33	404	213.6667	8	33	404	213.6667	1	19	203	107
6	20	315	279.3333	6	20	315	279.3333	3	12	231	190
8	33	404	213.6667	8	33	404	213.6667	3	12	231	190
6	20	315	279.3333	6	20	315	279.3333	6	23	303	909
8	33	404	213.6667	8	33	404	213.6667	6	23	303	909
select * from v1,v2,t2 where
((v1.a=v2.a) or (v1.a=t2.a)) and (t2.b<50) and (v1.b=v2.b);
a	b	max_c	avg_c	a	b	max_c	avg_c	a	b	c	d
6	20	315	279.3333	6	20	315	279.3333	2	3	207	207
8	33	404	213.6667	8	33	404	213.6667	2	3	207	207
6	20	315	279.3333	6	20	315	279.3333	1	21	909	12
8	33	404	213.6667	8	33	404	213.6667	1	21	909	12
6	20	315	279.3333	6	20	315	279.3333	7	13	312	406
8	33	404	213.6667	8	33	404	213.6667	7	13	312	406
6	20	315	279.3333	6	20	315	279.3333	6	20	315	279
8	33	404	213.6667	8	33	404	213.6667	6	20	315	279
6	20	315	279.3333	6	20	315	279.3333	1	19	203	107
8	33	404	213.6667	8	33	404	213.6667	1	19	203	107
6	20	315	279.3333	6	20	315	279.3333	3	12	231	190
8	33	404	213.6667	8	33	404	213.6667	3	12	231	190
6	20	315	279.3333	6	20	315	279.3333	6	23	303	909
8	33	404	213.6667	8	33	404	213.6667	6	23	303	909
explain select * from v1,v2,t2 where
((v1.a=v2.a) or (v1.a=t2.a)) and (t2.b<50) and (v1.b=v2.b);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where
1	PRIMARY	<derived3>	ref	key0	key0	5	v1.b	2	
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where; Using join buffer (flat, BNL join)
3	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using temporary; Using filesort
explain format=json select * from v1,v2,t2 where
((v1.a=v2.a) or (v1.a=t2.a)) and (t2.b<50) and (v1.b=v2.b);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 20,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v1.b is not null",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      },
      {
        "table": {
          "table_name": "<derived3>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "5",
          "used_key_parts": ["b"],
          "ref": ["v1.b"],
          "loops": 20,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "materialized": {
            "query_block": {
              "select_id": 3,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.a > 5"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "t2",
            "access_type": "ALL",
            "loops": 40,
            "rows": 9,
            "cost": "COST_REPLACED",
            "filtered": 100,
            "attached_condition": "t2.b < 50"
          },
          "buffer_type": "flat",
          "buffer_size": "1KiB",
          "join_type": "BNL",
          "attached_condition": "v2.a = v1.a or t2.a = v1.a"
        }
      }
    ]
  }
}
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,v2,t2 where
((v1.a=v2.a) and (v1.a=t2.a)) or ((v2.b>13) and (t2.c<115));
a	b	max_c	avg_c	a	b	max_c	avg_c	a	b	c	d
6	20	315	279.3333	6	20	315	279.3333	6	20	315	279
6	20	315	279.3333	6	20	315	279.3333	6	23	303	909
8	33	404	213.6667	8	33	404	213.6667	8	64	248	107
8	33	404	213.6667	8	33	404	213.6667	8	80	800	314
select * from v1,v2,t2 where
((v1.a=v2.a) and (v1.a=t2.a)) or ((v2.b>13) and (t2.c<115));
a	b	max_c	avg_c	a	b	max_c	avg_c	a	b	c	d
6	20	315	279.3333	6	20	315	279.3333	6	20	315	279
6	20	315	279.3333	6	20	315	279.3333	6	23	303	909
8	33	404	213.6667	8	33	404	213.6667	8	64	248	107
8	33	404	213.6667	8	33	404	213.6667	8	80	800	314
explain select * from v1,v2,t2 where
((v1.a=v2.a) and (v1.a=t2.a)) or ((v2.b>13) and (t2.c<115));
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
1	PRIMARY	<derived3>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (incremental, BNL join)
3	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using temporary; Using filesort
explain format=json select * from v1,v2,t2 where
((v1.a=v2.a) and (v1.a=t2.a)) or ((v2.b>13) and (t2.c<115));
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived2>",
            "access_type": "ALL",
            "loops": 9,
            "rows": 20,
            "cost": "COST_REPLACED",
            "filtered": 100
          },
          "buffer_type": "flat",
          "buffer_size": "238",
          "join_type": "BNL",
          "attached_condition": "v1.a = t2.a or t2.c < 115",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived3>",
            "access_type": "ALL",
            "loops": 180,
            "rows": 20,
            "cost": "COST_REPLACED",
            "filtered": 100
          },
          "buffer_type": "incremental",
          "buffer_size": "4KiB",
          "join_type": "BNL",
          "attached_condition": "v1.a = t2.a and v2.a = t2.a or v2.b > 13 and t2.c < 115",
          "materialized": {
            "query_block": {
              "select_id": 3,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.a > 5"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
# using several derived tables : pushing in all tables
# conjunctive subformula : pushing into HAVING
# extracted or formula : pushing into WHERE
# pushing equalities
set 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
((v2.b<50) or (v2.b=19)) and (v1.max_c<300);
a	b	max_c	avg_c	a	b	max_c	avg_c	a	b	c	d
1	19	107	107.0000	6	20	315	279.3333	1	21	909	12
1	19	107	107.0000	6	20	315	279.3333	1	19	203	107
1	19	107	107.0000	8	33	404	213.6667	1	21	909	12
1	19	107	107.0000	8	33	404	213.6667	1	19	203	107
select * from v1,v2,t2 where ((v1.a=v2.a) or (v1.a=t2.a)) and
((v2.b<50) or (v2.b=19)) and (v1.max_c<300);
a	b	max_c	avg_c	a	b	max_c	avg_c	a	b	c	d
1	19	107	107.0000	6	20	315	279.3333	1	21	909	12
1	19	107	107.0000	6	20	315	279.3333	1	19	203	107
1	19	107	107.0000	8	33	404	213.6667	1	21	909	12
1	19	107	107.0000	8	33	404	213.6667	1	19	203	107
explain select * from v1,v2,t2 where ((v1.a=v2.a) or (v1.a=t2.a)) and
((v2.b<50) or (v2.b=19)) and (v1.max_c<300);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
1	PRIMARY	<derived3>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (incremental, BNL join)
3	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using temporary; Using filesort
explain format=json select * from v1,v2,t2 where ((v1.a=v2.a) or (v1.a=t2.a)) and
((v2.b<50) or (v2.b=19)) and (v1.max_c<300);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived2>",
            "access_type": "ALL",
            "loops": 9,
            "rows": 20,
            "cost": "COST_REPLACED",
            "filtered": 100,
            "attached_condition": "v1.max_c < 300"
          },
          "buffer_type": "flat",
          "buffer_size": "238",
          "join_type": "BNL",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707 and max_c < 300",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived3>",
            "access_type": "ALL",
            "loops": 180,
            "rows": 20,
            "cost": "COST_REPLACED",
            "filtered": 100,
            "attached_condition": "v2.b < 50 or v2.b = 19"
          },
          "buffer_type": "incremental",
          "buffer_size": "4KiB",
          "join_type": "BNL",
          "attached_condition": "(v2.a = v1.a or v1.a = t2.a) and (v2.b < 50 or v2.b = 19)",
          "materialized": {
            "query_block": {
              "select_id": 3,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.a > 5 and (t1.b < 50 or t1.b = 19)"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
# using several derived tables : pushing only in one table
# conjunctive subformula : pushing into WHERE
# pushing equalities
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,v2,t2 where
(v1.a=t2.a) and (v1.a=v1.b) and (v1.a=v2.a) and (v2.max_c<300);
a	b	max_c	avg_c	a	b	max_c	avg_c	a	b	c	d
select * from v1,v2,t2 where
(v1.a=t2.a) and (v1.a=v1.b) and (v1.a=v2.a) and (v2.max_c<300);
a	b	max_c	avg_c	a	b	max_c	avg_c	a	b	c	d
explain select * from v1,v2,t2 where
(v1.a=t2.a) and (v1.a=v1.b) and (v1.a=v2.a) and (v2.max_c<300);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key1	key1	10	test.t2.a,test.t2.a	2	
1	PRIMARY	<derived3>	ref	key0	key0	5	test.t2.a	2	Using where
3	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
explain format=json select * from v1,v2,t2 where
(v1.a=t2.a) and (v1.a=v1.b) and (v1.a=v2.a) and (v2.max_c<300);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a is not null and t2.a is not null and t2.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key1"],
          "key": "key1",
          "key_length": "10",
          "used_key_parts": ["a", "b"],
          "ref": ["test.t2.a", "test.t2.a"],
          "loops": 9,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.b = t1.a"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      },
      {
        "table": {
          "table_name": "<derived3>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t2.a"],
          "loops": 18,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v2.max_c < 300",
          "materialized": {
            "query_block": {
              "select_id": 3,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707 and max_c < 300",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.a > 5"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
# using several derived tables : pushing only in one table
# extracted and formula : pushing into WHERE
# conjunctive subformula : pushing into WHERE using equalities
# pushing equalities
set 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);
a	b	max_c	avg_c	a	b	max_c	avg_c	a	b	c	d
select * from v1,v2,t2 where (v1.a=1) and (v1.b>10) and (v1.b=v2.b);
a	b	max_c	avg_c	a	b	max_c	avg_c	a	b	c	d
explain select * from v1,v2,t2 where (v1.a=1) and (v1.b>10) and (v1.b=v2.b);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where
1	PRIMARY	<derived3>	ref	key0	key0	5	v1.b	2	
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using join buffer (flat, BNL join)
3	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
explain format=json select * from v1,v2,t2 where (v1.a=1) and (v1.b>10) and (v1.b=v2.b);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 20,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v1.a = 1 and v1.b > 10 and v1.b is not null",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707",
              "filesort": {
                "sort_key": "t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.a = 1 and t1.b > 10"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      },
      {
        "table": {
          "table_name": "<derived3>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "5",
          "used_key_parts": ["b"],
          "ref": ["v1.b"],
          "loops": 20,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "materialized": {
            "query_block": {
              "select_id": 3,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.a > 5 and t1.b > 10"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "t2",
            "access_type": "ALL",
            "loops": 40,
            "rows": 9,
            "cost": "COST_REPLACED",
            "filtered": 100
          },
          "buffer_type": "flat",
          "buffer_size": "1KiB",
          "join_type": "BNL"
        }
      }
    ]
  }
}
# extracted or formula : pushing into WHERE
# conjunctive subformula : pushing into WHERE using equalities
# pushing equalities
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v_char as v,t2_char as t where
(v.a=t.a) and (t.a='b') and ((v.b='Vika') or (v.b='Ali'));
a	b	max_c	a	b	c
b	Vika	2	b	Ivan	1
b	Vika	2	b	Ali	6
b	Vika	2	b	Hermes	3
b	Vika	2	b	Ivan	11
b	Vika	2	b	Harry	4
select * from v_char as v,t2_char as t where
(v.a=t.a) and (t.a='b') and ((v.b='Vika') or (v.b='Ali'));
a	b	max_c	a	b	c
b	Vika	2	b	Ivan	1
b	Vika	2	b	Ali	6
b	Vika	2	b	Hermes	3
b	Vika	2	b	Ivan	11
b	Vika	2	b	Harry	4
explain select * from v_char as v,t2_char as t where
(v.a=t.a) and (t.a='b') and ((v.b='Vika') or (v.b='Ali'));
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	12	Using where
1	PRIMARY	t	ALL	NULL	NULL	NULL	NULL	12	Using where; Using join buffer (flat, BNL join)
2	DERIVED	t1_char	ALL	NULL	NULL	NULL	NULL	12	Using where; Using temporary; Using filesort
explain format=json select * from v_char as v,t2_char as t where
(v.a=t.a) and (t.a='b') and ((v.b='Vika') or (v.b='Ali'));
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 12,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v.a = 'b' and (v.b = 'Vika' or v.b = 'Ali')",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 9",
              "filesort": {
                "sort_key": "t1_char.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1_char",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 12,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1_char.a = 'b' and (t1_char.b = 'Vika' or t1_char.b = 'Ali')"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "t",
            "access_type": "ALL",
            "loops": 12,
            "rows": 12,
            "cost": "COST_REPLACED",
            "filtered": 100,
            "attached_condition": "t.a = 'b'"
          },
          "buffer_type": "flat",
          "buffer_size": "220",
          "join_type": "BNL"
        }
      }
    ]
  }
}
# using several derived tables : pushing in all tables
# extracted or formula : pushing into WHERE
# conjunctive subformulas : pushing into HAVING
# pushing equalities
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,v2,v3,t2 where
((v1.a=v2.a) or (v1.a=t2.a)) and ((v3.b<50) or (v3.b=33))
and (v1.max_c<500) and (v3.a=t2.a) and (v2.max_c>300);
a	b	max_c	avg_c	a	b	max_c	avg_c	a	b	min_c	a	b	c	d
6	20	315	279.3333	6	20	315	279.3333	7	11	708	7	13	312	406
6	20	315	279.3333	6	20	315	279.3333	8	33	114	8	64	248	107
6	20	315	279.3333	6	20	315	279.3333	6	20	214	6	20	315	279
6	20	315	279.3333	6	20	315	279.3333	8	33	114	8	80	800	314
6	20	315	279.3333	6	20	315	279.3333	6	20	214	6	23	303	909
8	33	404	213.6667	6	20	315	279.3333	8	33	114	8	64	248	107
8	33	404	213.6667	6	20	315	279.3333	8	33	114	8	80	800	314
6	20	315	279.3333	8	33	404	213.6667	6	20	214	6	20	315	279
6	20	315	279.3333	8	33	404	213.6667	6	20	214	6	23	303	909
8	33	404	213.6667	8	33	404	213.6667	7	11	708	7	13	312	406
8	33	404	213.6667	8	33	404	213.6667	8	33	114	8	64	248	107
8	33	404	213.6667	8	33	404	213.6667	6	20	214	6	20	315	279
8	33	404	213.6667	8	33	404	213.6667	8	33	114	8	80	800	314
8	33	404	213.6667	8	33	404	213.6667	6	20	214	6	23	303	909
select * from v1,v2,v3,t2 where
((v1.a=v2.a) or (v1.a=t2.a)) and ((v3.b<50) or (v3.b=33))
and (v1.max_c<500) and (v3.a=t2.a) and (v2.max_c>300);
a	b	max_c	avg_c	a	b	max_c	avg_c	a	b	min_c	a	b	c	d
6	20	315	279.3333	6	20	315	279.3333	7	11	708	7	13	312	406
6	20	315	279.3333	6	20	315	279.3333	8	33	114	8	64	248	107
6	20	315	279.3333	6	20	315	279.3333	6	20	214	6	20	315	279
6	20	315	279.3333	6	20	315	279.3333	8	33	114	8	80	800	314
6	20	315	279.3333	6	20	315	279.3333	6	20	214	6	23	303	909
8	33	404	213.6667	6	20	315	279.3333	8	33	114	8	64	248	107
8	33	404	213.6667	6	20	315	279.3333	8	33	114	8	80	800	314
6	20	315	279.3333	8	33	404	213.6667	6	20	214	6	20	315	279
6	20	315	279.3333	8	33	404	213.6667	6	20	214	6	23	303	909
8	33	404	213.6667	8	33	404	213.6667	7	11	708	7	13	312	406
8	33	404	213.6667	8	33	404	213.6667	8	33	114	8	64	248	107
8	33	404	213.6667	8	33	404	213.6667	6	20	214	6	20	315	279
8	33	404	213.6667	8	33	404	213.6667	8	33	114	8	80	800	314
8	33	404	213.6667	8	33	404	213.6667	6	20	214	6	23	303	909
explain select * from v1,v2,v3,t2 where
((v1.a=v2.a) or (v1.a=t2.a)) and ((v3.b<50) or (v3.b=33))
and (v1.max_c<500) and (v3.a=t2.a) and (v2.max_c>300);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived4>	ref	key0	key0	5	test.t2.a	2	Using where
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
1	PRIMARY	<derived3>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (incremental, BNL join)
4	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
3	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using temporary; Using filesort
explain format=json select * from v1,v2,v3,t2 where
((v1.a=v2.a) or (v1.a=t2.a)) and ((v3.b<50) or (v3.b=33))
and (v1.max_c<500) and (v3.a=t2.a) and (v2.max_c>300);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived4>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t2.a"],
          "loops": 9,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v3.b < 50 or v3.b = 33",
          "materialized": {
            "query_block": {
              "select_id": 4,
              "cost": "COST_REPLACED",
              "having_condition": "min_c > 109",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.a < 10 and (t1.b < 50 or t1.b = 33)"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived2>",
            "access_type": "ALL",
            "loops": 18,
            "rows": 20,
            "cost": "COST_REPLACED",
            "filtered": 100,
            "attached_condition": "v1.max_c < 500"
          },
          "buffer_type": "flat",
          "buffer_size": "715",
          "join_type": "BNL",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707 and max_c < 500",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived3>",
            "access_type": "ALL",
            "loops": 360,
            "rows": 20,
            "cost": "COST_REPLACED",
            "filtered": 100,
            "attached_condition": "v2.max_c > 300"
          },
          "buffer_type": "incremental",
          "buffer_size": "9KiB",
          "join_type": "BNL",
          "attached_condition": "v2.a = v1.a or v1.a = t2.a",
          "materialized": {
            "query_block": {
              "select_id": 3,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707 and max_c > 300",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.a > 5"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
# using several derived tables : pushing in all tables
# conjunctive subformulas : pushing into HAVING
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from
(select a, b, max(c) as max_c, avg(c) as avg_c from t1
where t1.a>5 group by a,b having max_c < 707) v1,
(select a, b, min(c) as min_c from t1
where t1.a>5 group by a,b having min_c < 707) v2,
t2 where (v1.a=v2.a) and (v1.b=t2.b) and (v1.max_c>130) and (v2.min_c<130);
a	b	max_c	avg_c	a	b	min_c	a	b	c	d
select * from
(select a, b, max(c) as max_c, avg(c) as avg_c from t1
where t1.a>5 group by a,b having max_c < 707) v1,
(select a, b, min(c) as min_c from t1
where t1.a>5 group by a,b having min_c < 707) v2,
t2 where (v1.a=v2.a) and (v1.b=t2.b) and (v1.max_c>130) and (v2.min_c<130);
a	b	max_c	avg_c	a	b	min_c	a	b	c	d
explain select * from
(select a, b, max(c) as max_c, avg(c) as avg_c from t1
where t1.a>5 group by a,b having max_c < 707) v1,
(select a, b, min(c) as min_c from t1
where t1.a>5 group by a,b having min_c < 707) v2,
t2 where (v1.a=v2.a) and (v1.b=t2.b) and (v1.max_c>130) and (v2.min_c<130);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key1	key1	5	test.t2.b	2	Using where
1	PRIMARY	<derived3>	ref	key0	key0	5	v1.a	2	Using where
3	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
explain format=json select * from
(select a, b, max(c) as max_c, avg(c) as avg_c from t1
where t1.a>5 group by a,b having max_c < 707) v1,
(select a, b, min(c) as min_c from t1
where t1.a>5 group by a,b having min_c < 707) v2,
t2 where (v1.a=v2.a) and (v1.b=t2.b) and (v1.max_c>130) and (v2.min_c<130);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.b is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key1"],
          "key": "key1",
          "key_length": "5",
          "used_key_parts": ["b"],
          "ref": ["test.t2.b"],
          "loops": 9,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v1.max_c > 130 and v1.a is not null",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707 and max_c > 130",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.a > 5"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      },
      {
        "table": {
          "table_name": "<derived3>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["v1.a"],
          "loops": 18,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v2.min_c < 130",
          "materialized": {
            "query_block": {
              "select_id": 3,
              "cost": "COST_REPLACED",
              "having_condition": "min_c < 707 and min_c < 130",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.a > 5"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
# using several derived tables : pushing in all tables
# extracted or formulas : pushing into HAVING
# conjunctive subformula : pushing into HAVING
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from
(select a, b, max(c) as max_c, avg(c) as avg_c from t1
where t1.a>5 group by a,b having max_c < 707) v1,
(select a, b, min(c) as min_c from t1
where t1.a>5 group by a,b having min_c < 707) v2,
(select a, b, avg(c) as avg_c from t1
where t1.a<8 group by a,b) v3,
t2 where (v1.a=v2.a) and (v1.b=v3.b) and ((v3.avg_c>170) or (v3.a<5))
and ((v1.avg_c<400) or (v1.a>1)) and (v2.min_c<200);
a	b	max_c	avg_c	a	b	min_c	a	b	avg_c	a	b	c	d
8	33	404	213.6667	8	33	114	1	33	497.5000	2	3	207	207
8	33	404	213.6667	8	33	114	1	33	497.5000	1	21	909	12
8	33	404	213.6667	8	33	114	1	33	497.5000	7	13	312	406
8	33	404	213.6667	8	33	114	1	33	497.5000	8	64	248	107
8	33	404	213.6667	8	33	114	1	33	497.5000	6	20	315	279
8	33	404	213.6667	8	33	114	1	33	497.5000	1	19	203	107
8	33	404	213.6667	8	33	114	1	33	497.5000	8	80	800	314
8	33	404	213.6667	8	33	114	1	33	497.5000	3	12	231	190
8	33	404	213.6667	8	33	114	1	33	497.5000	6	23	303	909
select * from
(select a, b, max(c) as max_c, avg(c) as avg_c from t1
where t1.a>5 group by a,b having max_c < 707) v1,
(select a, b, min(c) as min_c from t1
where t1.a>5 group by a,b having min_c < 707) v2,
(select a, b, avg(c) as avg_c from t1
where t1.a<8 group by a,b) v3,
t2 where (v1.a=v2.a) and (v1.b=v3.b) and ((v3.avg_c>170) or (v3.a<5))
and ((v1.avg_c<400) or (v1.a>1)) and (v2.min_c<200);
a	b	max_c	avg_c	a	b	min_c	a	b	avg_c	a	b	c	d
8	33	404	213.6667	8	33	114	1	33	497.5000	2	3	207	207
8	33	404	213.6667	8	33	114	1	33	497.5000	1	21	909	12
8	33	404	213.6667	8	33	114	1	33	497.5000	7	13	312	406
8	33	404	213.6667	8	33	114	1	33	497.5000	8	64	248	107
8	33	404	213.6667	8	33	114	1	33	497.5000	6	20	315	279
8	33	404	213.6667	8	33	114	1	33	497.5000	1	19	203	107
8	33	404	213.6667	8	33	114	1	33	497.5000	8	80	800	314
8	33	404	213.6667	8	33	114	1	33	497.5000	3	12	231	190
8	33	404	213.6667	8	33	114	1	33	497.5000	6	23	303	909
explain select * from
(select a, b, max(c) as max_c, avg(c) as avg_c from t1
where t1.a>5 group by a,b having max_c < 707) v1,
(select a, b, min(c) as min_c from t1
where t1.a>5 group by a,b having min_c < 707) v2,
(select a, b, avg(c) as avg_c from t1
where t1.a<8 group by a,b) v3,
t2 where (v1.a=v2.a) and (v1.b=v3.b) and ((v3.avg_c>170) or (v3.a<5))
and ((v1.avg_c<400) or (v1.a>1)) and (v2.min_c<200);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where
1	PRIMARY	<derived3>	ref	key0	key0	5	v1.a	2	Using where
1	PRIMARY	<derived4>	ref	key0	key0	5	v1.b	2	Using where
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using join buffer (flat, BNL join)
4	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
3	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
explain format=json select * from
(select a, b, max(c) as max_c, avg(c) as avg_c from t1
where t1.a>5 group by a,b having max_c < 707) v1,
(select a, b, min(c) as min_c from t1
where t1.a>5 group by a,b having min_c < 707) v2,
(select a, b, avg(c) as avg_c from t1
where t1.a<8 group by a,b) v3,
t2 where (v1.a=v2.a) and (v1.b=v3.b) and ((v3.avg_c>170) or (v3.a<5))
and ((v1.avg_c<400) or (v1.a>1)) and (v2.min_c<200);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 20,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "(v1.avg_c < 400 or v1.a > 1) and v1.a is not null and v1.b is not null",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707 and (avg_c < 400 or t1.a > 1)",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.a > 5"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      },
      {
        "table": {
          "table_name": "<derived3>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["v1.a"],
          "loops": 20,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v2.min_c < 200",
          "materialized": {
            "query_block": {
              "select_id": 3,
              "cost": "COST_REPLACED",
              "having_condition": "min_c < 707 and min_c < 200",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.a > 5"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      },
      {
        "table": {
          "table_name": "<derived4>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "5",
          "used_key_parts": ["b"],
          "ref": ["v1.b"],
          "loops": 40,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v3.avg_c > 170 or v3.a < 5",
          "materialized": {
            "query_block": {
              "select_id": 4,
              "cost": "COST_REPLACED",
              "having_condition": "avg_c > 170 or t1.a < 5",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.a < 8"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "t2",
            "access_type": "ALL",
            "loops": 80,
            "rows": 9,
            "cost": "COST_REPLACED",
            "filtered": 100
          },
          "buffer_type": "flat",
          "buffer_size": "4KiB",
          "join_type": "BNL"
        }
      }
    ]
  }
}
# extracted or formula : pushing into HAVING
# conjunctive subformula : pushing into WHERE
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from
(select a, b, max(c) as max_c, avg(c) as avg_c from t1
group by a,b having max_c < 707) v1,
t2 where ((v1.a=1) or (v1.max_c<300)) and (v1.b>25);
a	b	max_c	avg_c	a	b	c	d
5	27	132	132.0000	2	3	207	207
5	27	132	132.0000	1	21	909	12
5	27	132	132.0000	7	13	312	406
5	27	132	132.0000	8	64	248	107
5	27	132	132.0000	6	20	315	279
5	27	132	132.0000	1	19	203	107
5	27	132	132.0000	8	80	800	314
5	27	132	132.0000	3	12	231	190
5	27	132	132.0000	6	23	303	909
select * from
(select a, b, max(c) as max_c, avg(c) as avg_c from t1
group by a,b having max_c < 707) v1,
t2 where ((v1.a=1) or (v1.max_c<300)) and (v1.b>25);
a	b	max_c	avg_c	a	b	c	d
5	27	132	132.0000	2	3	207	207
5	27	132	132.0000	1	21	909	12
5	27	132	132.0000	7	13	312	406
5	27	132	132.0000	8	64	248	107
5	27	132	132.0000	6	20	315	279
5	27	132	132.0000	1	19	203	107
5	27	132	132.0000	8	80	800	314
5	27	132	132.0000	3	12	231	190
5	27	132	132.0000	6	23	303	909
explain select * from
(select a, b, max(c) as max_c, avg(c) as avg_c from t1
group by a,b having max_c < 707) v1,
t2 where ((v1.a=1) or (v1.max_c<300)) and (v1.b>25);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
explain format=json select * from
(select a, b, max(c) as max_c, avg(c) as avg_c from t1
group by a,b having max_c < 707) v1,
t2 where ((v1.a=1) or (v1.max_c<300)) and (v1.b>25);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived2>",
            "access_type": "ALL",
            "loops": 9,
            "rows": 20,
            "cost": "COST_REPLACED",
            "filtered": 100,
            "attached_condition": "(v1.a = 1 or v1.max_c < 300) and v1.b > 25"
          },
          "buffer_type": "flat",
          "buffer_size": "238",
          "join_type": "BNL",
          "attached_condition": "v1.a = 1 or v1.max_c < 300",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707 and (t1.a = 1 or max_c < 300)",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.b > 25"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
# extracted and formula : pushing into WHERE
# conjunctive subformula : pushing into HAVING
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from
(select a, b, max(c) as max_c, avg(c) as avg_c from t1
where t1.a>5 group by a,b having max_c < 707) v1,
t2 where (v1.a=t2.a) and (v1.max_c>300) and (v1.b<30);
a	b	max_c	avg_c	a	b	c	d
6	20	315	279.3333	6	20	315	279
6	20	315	279.3333	6	23	303	909
select * from
(select a, b, max(c) as max_c, avg(c) as avg_c from t1
where t1.a>5 group by a,b having max_c < 707) v1,
t2 where (v1.a=t2.a) and (v1.max_c>300) and (v1.b<30);
a	b	max_c	avg_c	a	b	c	d
6	20	315	279.3333	6	20	315	279
6	20	315	279.3333	6	23	303	909
explain select * from
(select a, b, max(c) as max_c, avg(c) as avg_c from t1
where t1.a>5 group by a,b having max_c < 707) v1,
t2 where (v1.a=t2.a) and (v1.max_c>300) and (v1.b<30);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.a	2	Using where
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
explain format=json select * from
(select a, b, max(c) as max_c, avg(c) as avg_c from t1
where t1.a>5 group by a,b having max_c < 707) v1,
t2 where (v1.a=t2.a) and (v1.max_c>300) and (v1.b<30);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t2.a"],
          "loops": 9,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v1.max_c > 300 and v1.b < 30",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707 and max_c > 300",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.a > 5 and t1.b < 30"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
# using query with union
# conjunctive subformula : pushing into WHERE
# conjunctive subformulas : pushing into HAVING and WHERE
set 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)
union
select * from v1,t2 where (v1.max_c>100) and (v1.a>7) and (t2.d>800);
a	b	max_c	avg_c	a	b	c	d
1	21	500	234.6000	1	21	909	12
8	33	404	213.6667	6	23	303	909
select * from v1,t2 where (v1.a<5) and (v1.b=t2.b) and (t2.c>800)
union
select * from v1,t2 where (v1.max_c>100) and (v1.a>7) and (t2.d>800);
a	b	max_c	avg_c	a	b	c	d
1	21	500	234.6000	1	21	909	12
8	33	404	213.6667	6	23	303	909
explain select * from v1,t2 where (v1.a<5) and (v1.b=t2.b) and (t2.c>800)
union
select * from v1,t2 where (v1.max_c>100) and (v1.a>7) and (t2.d>800);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived3>	ref	key0	key0	5	test.t2.b	2	Using where
3	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
2	UNION	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
2	UNION	<derived4>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
4	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
NULL	UNION RESULT	<union1,2>	ALL	NULL	NULL	NULL	NULL	NULL	
explain format=json select * from v1,t2 where (v1.a<5) and (v1.b=t2.b) and (t2.c>800)
union
select * from v1,t2 where (v1.max_c>100) and (v1.a>7) and (t2.d>800);
EXPLAIN
{
  "query_block": {
    "union_result": {
      "table_name": "<union1,2>",
      "access_type": "ALL",
      "query_specifications": [
        {
          "query_block": {
            "select_id": 1,
            "cost": "COST_REPLACED",
            "nested_loop": [
              {
                "table": {
                  "table_name": "t2",
                  "access_type": "ALL",
                  "loops": 1,
                  "rows": 9,
                  "cost": "COST_REPLACED",
                  "filtered": 100,
                  "attached_condition": "t2.c > 800 and t2.b is not null"
                }
              },
              {
                "table": {
                  "table_name": "<derived3>",
                  "access_type": "ref",
                  "possible_keys": ["key0"],
                  "key": "key0",
                  "key_length": "5",
                  "used_key_parts": ["b"],
                  "ref": ["test.t2.b"],
                  "loops": 9,
                  "rows": 2,
                  "cost": "COST_REPLACED",
                  "filtered": 100,
                  "attached_condition": "v1.a < 5",
                  "materialized": {
                    "query_block": {
                      "select_id": 3,
                      "cost": "COST_REPLACED",
                      "having_condition": "max_c < 707",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 20,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a < 5"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                }
              }
            ]
          }
        },
        {
          "query_block": {
            "select_id": 2,
            "operation": "UNION",
            "cost": "COST_REPLACED",
            "nested_loop": [
              {
                "table": {
                  "table_name": "t2",
                  "access_type": "ALL",
                  "loops": 1,
                  "rows": 9,
                  "cost": "COST_REPLACED",
                  "filtered": 100,
                  "attached_condition": "t2.d > 800"
                }
              },
              {
                "block-nl-join": {
                  "table": {
                    "table_name": "<derived4>",
                    "access_type": "ALL",
                    "loops": 9,
                    "rows": 20,
                    "cost": "COST_REPLACED",
                    "filtered": 100,
                    "attached_condition": "v1.max_c > 100 and v1.a > 7"
                  },
                  "buffer_type": "flat",
                  "buffer_size": "238",
                  "join_type": "BNL",
                  "materialized": {
                    "query_block": {
                      "select_id": 4,
                      "cost": "COST_REPLACED",
                      "having_condition": "max_c < 707 and max_c > 100",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 20,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a > 7"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}
# using query with union
# extracted and formula : pushing into WHERE
# extracted or formula : pushing into HAVING
# pushing equalities
set 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)
union
select * from v1,t2 where ((v1.max_c>400) or (v1.avg_c>270)) and (v1.a<t2.a);
a	b	max_c	avg_c	a	b	c	d
1	19	107	107.0000	1	19	203	107
1	21	500	234.6000	2	3	207	207
1	21	500	234.6000	7	13	312	406
1	21	500	234.6000	8	64	248	107
1	21	500	234.6000	6	20	315	279
1	21	500	234.6000	8	80	800	314
1	21	500	234.6000	3	12	231	190
1	21	500	234.6000	6	23	303	909
6	20	315	279.3333	7	13	312	406
6	20	315	279.3333	8	64	248	107
6	20	315	279.3333	8	80	800	314
select * from v1,t2 where (v1.a<5) and (v1.b=t2.b) and (v1.b=19)
union
select * from v1,t2 where ((v1.max_c>400) or (v1.avg_c>270)) and (v1.a<t2.a);
a	b	max_c	avg_c	a	b	c	d
1	19	107	107.0000	1	19	203	107
1	21	500	234.6000	2	3	207	207
1	21	500	234.6000	7	13	312	406
1	21	500	234.6000	8	64	248	107
1	21	500	234.6000	6	20	315	279
1	21	500	234.6000	8	80	800	314
1	21	500	234.6000	3	12	231	190
1	21	500	234.6000	6	23	303	909
6	20	315	279.3333	7	13	312	406
6	20	315	279.3333	8	64	248	107
6	20	315	279.3333	8	80	800	314
explain select * from v1,t2 where (v1.a<5) and (v1.b=t2.b) and (v1.b=19)
union
select * from v1,t2 where ((v1.max_c>400) or (v1.avg_c>270)) and (v1.a<t2.a);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived3>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
3	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
2	UNION	t2	ALL	NULL	NULL	NULL	NULL	9	
2	UNION	<derived4>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
4	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using temporary; Using filesort
NULL	UNION RESULT	<union1,2>	ALL	NULL	NULL	NULL	NULL	NULL	
explain format=json select * from v1,t2 where (v1.a<5) and (v1.b=t2.b) and (v1.b=19)
union
select * from v1,t2 where ((v1.max_c>400) or (v1.avg_c>270)) and (v1.a<t2.a);
EXPLAIN
{
  "query_block": {
    "union_result": {
      "table_name": "<union1,2>",
      "access_type": "ALL",
      "query_specifications": [
        {
          "query_block": {
            "select_id": 1,
            "cost": "COST_REPLACED",
            "nested_loop": [
              {
                "table": {
                  "table_name": "t2",
                  "access_type": "ALL",
                  "loops": 1,
                  "rows": 9,
                  "cost": "COST_REPLACED",
                  "filtered": 100,
                  "attached_condition": "t2.b = 19"
                }
              },
              {
                "block-nl-join": {
                  "table": {
                    "table_name": "<derived3>",
                    "access_type": "ALL",
                    "loops": 9,
                    "rows": 20,
                    "cost": "COST_REPLACED",
                    "filtered": 100,
                    "attached_condition": "v1.b = 19 and v1.a < 5"
                  },
                  "buffer_type": "flat",
                  "buffer_size": "238",
                  "join_type": "BNL",
                  "materialized": {
                    "query_block": {
                      "select_id": 3,
                      "cost": "COST_REPLACED",
                      "having_condition": "max_c < 707",
                      "filesort": {
                        "sort_key": "t1.a",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 20,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.b = 19 and t1.a < 5"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                }
              }
            ]
          }
        },
        {
          "query_block": {
            "select_id": 2,
            "operation": "UNION",
            "cost": "COST_REPLACED",
            "nested_loop": [
              {
                "table": {
                  "table_name": "t2",
                  "access_type": "ALL",
                  "loops": 1,
                  "rows": 9,
                  "cost": "COST_REPLACED",
                  "filtered": 100
                }
              },
              {
                "block-nl-join": {
                  "table": {
                    "table_name": "<derived4>",
                    "access_type": "ALL",
                    "loops": 9,
                    "rows": 20,
                    "cost": "COST_REPLACED",
                    "filtered": 100,
                    "attached_condition": "v1.max_c > 400 or v1.avg_c > 270"
                  },
                  "buffer_type": "flat",
                  "buffer_size": "238",
                  "join_type": "BNL",
                  "attached_condition": "(v1.max_c > 400 or v1.avg_c > 270) and v1.a < t2.a",
                  "materialized": {
                    "query_block": {
                      "select_id": 4,
                      "cost": "COST_REPLACED",
                      "having_condition": "max_c < 707 and (max_c > 400 or avg_c > 270)",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 20,
                                "cost": "COST_REPLACED",
                                "filtered": 100
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}
# using query with union
# extracted or formula : pushing into HAVING
# extracted or formula : pushing into WHERE
# pushing equalities
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where
((t2.a=v1.a) or (v1.b=t2.b)) and ((v1.a=1) or (v1.a=6))
union
select * from v1,t2 where ((v1.a>3) and (v1.b>27)) or (v1.max_c>550);
a	b	max_c	avg_c	a	b	c	d
1	19	107	107.0000	1	21	909	12
1	19	107	107.0000	1	19	203	107
1	21	500	234.6000	1	21	909	12
1	21	500	234.6000	1	19	203	107
6	20	315	279.3333	6	20	315	279
6	20	315	279.3333	6	23	303	909
8	33	404	213.6667	2	3	207	207
8	33	404	213.6667	1	21	909	12
8	33	404	213.6667	7	13	312	406
8	33	404	213.6667	8	64	248	107
8	33	404	213.6667	6	20	315	279
8	33	404	213.6667	1	19	203	107
8	33	404	213.6667	8	80	800	314
8	33	404	213.6667	3	12	231	190
8	33	404	213.6667	6	23	303	909
select * from v1,t2 where
((t2.a=v1.a) or (v1.b=t2.b)) and ((v1.a=1) or (v1.a=6))
union
select * from v1,t2 where ((v1.a>3) and (v1.b>27)) or (v1.max_c>550);
a	b	max_c	avg_c	a	b	c	d
1	19	107	107.0000	1	21	909	12
1	19	107	107.0000	1	19	203	107
1	21	500	234.6000	1	21	909	12
1	21	500	234.6000	1	19	203	107
6	20	315	279.3333	6	20	315	279
6	20	315	279.3333	6	23	303	909
8	33	404	213.6667	2	3	207	207
8	33	404	213.6667	1	21	909	12
8	33	404	213.6667	7	13	312	406
8	33	404	213.6667	8	64	248	107
8	33	404	213.6667	6	20	315	279
8	33	404	213.6667	1	19	203	107
8	33	404	213.6667	8	80	800	314
8	33	404	213.6667	3	12	231	190
8	33	404	213.6667	6	23	303	909
explain select * from v1,t2 where
((t2.a=v1.a) or (v1.b=t2.b)) and ((v1.a=1) or (v1.a=6))
union
select * from v1,t2 where ((v1.a>3) and (v1.b>27)) or (v1.max_c>550);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	
1	PRIMARY	<derived3>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
3	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
2	UNION	t2	ALL	NULL	NULL	NULL	NULL	9	
2	UNION	<derived4>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
4	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using temporary; Using filesort
NULL	UNION RESULT	<union1,2>	ALL	NULL	NULL	NULL	NULL	NULL	
explain format=json select * from v1,t2 where
((t2.a=v1.a) or (v1.b=t2.b)) and ((v1.a=1) or (v1.a=6))
union
select * from v1,t2 where ((v1.a>3) and (v1.b>27)) or (v1.max_c>550);
EXPLAIN
{
  "query_block": {
    "union_result": {
      "table_name": "<union1,2>",
      "access_type": "ALL",
      "query_specifications": [
        {
          "query_block": {
            "select_id": 1,
            "cost": "COST_REPLACED",
            "nested_loop": [
              {
                "table": {
                  "table_name": "t2",
                  "access_type": "ALL",
                  "loops": 1,
                  "rows": 9,
                  "cost": "COST_REPLACED",
                  "filtered": 100
                }
              },
              {
                "block-nl-join": {
                  "table": {
                    "table_name": "<derived3>",
                    "access_type": "ALL",
                    "loops": 9,
                    "rows": 20,
                    "cost": "COST_REPLACED",
                    "filtered": 100,
                    "attached_condition": "v1.a = 1 or v1.a = 6"
                  },
                  "buffer_type": "flat",
                  "buffer_size": "238",
                  "join_type": "BNL",
                  "attached_condition": "(v1.a = t2.a or v1.b = t2.b) and (v1.a = 1 or v1.a = 6)",
                  "materialized": {
                    "query_block": {
                      "select_id": 3,
                      "cost": "COST_REPLACED",
                      "having_condition": "max_c < 707",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 20,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a = 1 or t1.a = 6"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                }
              }
            ]
          }
        },
        {
          "query_block": {
            "select_id": 2,
            "operation": "UNION",
            "cost": "COST_REPLACED",
            "nested_loop": [
              {
                "table": {
                  "table_name": "t2",
                  "access_type": "ALL",
                  "loops": 1,
                  "rows": 9,
                  "cost": "COST_REPLACED",
                  "filtered": 100
                }
              },
              {
                "block-nl-join": {
                  "table": {
                    "table_name": "<derived4>",
                    "access_type": "ALL",
                    "loops": 9,
                    "rows": 20,
                    "cost": "COST_REPLACED",
                    "filtered": 100,
                    "attached_condition": "v1.a > 3 and v1.b > 27 or v1.max_c > 550"
                  },
                  "buffer_type": "flat",
                  "buffer_size": "238",
                  "join_type": "BNL",
                  "attached_condition": "v1.a > 3 and v1.b > 27 or v1.max_c > 550",
                  "materialized": {
                    "query_block": {
                      "select_id": 4,
                      "cost": "COST_REPLACED",
                      "having_condition": "max_c < 707 and (t1.a > 3 and t1.b > 27 or max_c > 550)",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 20,
                                "cost": "COST_REPLACED",
                                "filtered": 100
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}
# using query with union
# extracted or formula : pushing into HAVING
# conjunctive subformulas : pushing into WHERE
# pushing equalities
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where
((v1.a=1) and (v1.a=t2.a)) and ((v1.max_c<500) or (v1.avg_c>500))
union
select * from v2,t2 where
((v2.a<t2.b) or (v2.max_c>200)) and (v2.b>10) and (t2.a<2)
union
select * from v2,t2 where
(v2.max_c=t2.c) and (v2.b<10);
a	b	max_c	avg_c	a	b	c	d
1	19	107	107.0000	1	21	909	12
1	19	107	107.0000	1	19	203	107
6	20	315	279.3333	1	21	909	12
6	20	315	279.3333	1	19	203	107
8	33	404	213.6667	1	21	909	12
8	33	404	213.6667	1	19	203	107
select * from v1,t2 where
((v1.a=1) and (v1.a=t2.a)) and ((v1.max_c<500) or (v1.avg_c>500))
union
select * from v2,t2 where
((v2.a<t2.b) or (v2.max_c>200)) and (v2.b>10) and (t2.a<2)
union
select * from v2,t2 where
(v2.max_c=t2.c) and (v2.b<10);
a	b	max_c	avg_c	a	b	c	d
1	19	107	107.0000	1	21	909	12
1	19	107	107.0000	1	19	203	107
6	20	315	279.3333	1	21	909	12
6	20	315	279.3333	1	19	203	107
8	33	404	213.6667	1	21	909	12
8	33	404	213.6667	1	19	203	107
explain select * from v1,t2 where
((v1.a=1) and (v1.a=t2.a)) and ((v1.max_c<500) or (v1.avg_c>500))
union
select * from v2,t2 where
((v2.a<t2.b) or (v2.max_c>200)) and (v2.b>10) and (t2.a<2)
union
select * from v2,t2 where
(v2.max_c=t2.c) and (v2.b<10);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived4>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
4	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
2	UNION	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
2	UNION	<derived5>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
5	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
3	UNION	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
3	UNION	<derived6>	ref	key0	key0	5	test.t2.c	2	Using where
6	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
NULL	UNION RESULT	<union1,2,3>	ALL	NULL	NULL	NULL	NULL	NULL	
explain format=json select * from v1,t2 where
((v1.a=1) and (v1.a=t2.a)) and ((v1.max_c<500) or (v1.avg_c>500))
union
select * from v2,t2 where
((v2.a<t2.b) or (v2.max_c>200)) and (v2.b>10) and (t2.a<2)
union
select * from v2,t2 where
(v2.max_c=t2.c) and (v2.b<10);
EXPLAIN
{
  "query_block": {
    "union_result": {
      "table_name": "<union1,2,3>",
      "access_type": "ALL",
      "query_specifications": [
        {
          "query_block": {
            "select_id": 1,
            "cost": "COST_REPLACED",
            "nested_loop": [
              {
                "table": {
                  "table_name": "t2",
                  "access_type": "ALL",
                  "loops": 1,
                  "rows": 9,
                  "cost": "COST_REPLACED",
                  "filtered": 100,
                  "attached_condition": "t2.a = 1"
                }
              },
              {
                "block-nl-join": {
                  "table": {
                    "table_name": "<derived4>",
                    "access_type": "ALL",
                    "loops": 9,
                    "rows": 20,
                    "cost": "COST_REPLACED",
                    "filtered": 100,
                    "attached_condition": "v1.a = 1 and (v1.max_c < 500 or v1.avg_c > 500)"
                  },
                  "buffer_type": "flat",
                  "buffer_size": "238",
                  "join_type": "BNL",
                  "attached_condition": "v1.max_c < 500 or v1.avg_c > 500",
                  "materialized": {
                    "query_block": {
                      "select_id": 4,
                      "cost": "COST_REPLACED",
                      "having_condition": "max_c < 707 and (max_c < 500 or avg_c > 500)",
                      "filesort": {
                        "sort_key": "t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 20,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a = 1"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                }
              }
            ]
          }
        },
        {
          "query_block": {
            "select_id": 2,
            "operation": "UNION",
            "cost": "COST_REPLACED",
            "nested_loop": [
              {
                "table": {
                  "table_name": "t2",
                  "access_type": "ALL",
                  "loops": 1,
                  "rows": 9,
                  "cost": "COST_REPLACED",
                  "filtered": 100,
                  "attached_condition": "t2.a < 2"
                }
              },
              {
                "block-nl-join": {
                  "table": {
                    "table_name": "<derived5>",
                    "access_type": "ALL",
                    "loops": 9,
                    "rows": 20,
                    "cost": "COST_REPLACED",
                    "filtered": 100,
                    "attached_condition": "v2.b > 10"
                  },
                  "buffer_type": "flat",
                  "buffer_size": "238",
                  "join_type": "BNL",
                  "attached_condition": "v2.a < t2.b or v2.max_c > 200",
                  "materialized": {
                    "query_block": {
                      "select_id": 5,
                      "cost": "COST_REPLACED",
                      "having_condition": "max_c < 707",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 20,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a > 5 and t1.b > 10"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                }
              }
            ]
          }
        },
        {
          "query_block": {
            "select_id": 3,
            "operation": "UNION",
            "cost": "COST_REPLACED",
            "nested_loop": [
              {
                "table": {
                  "table_name": "t2",
                  "access_type": "ALL",
                  "loops": 1,
                  "rows": 9,
                  "cost": "COST_REPLACED",
                  "filtered": 100,
                  "attached_condition": "t2.c is not null"
                }
              },
              {
                "table": {
                  "table_name": "<derived6>",
                  "access_type": "ref",
                  "possible_keys": ["key0"],
                  "key": "key0",
                  "key_length": "5",
                  "used_key_parts": ["max_c"],
                  "ref": ["test.t2.c"],
                  "loops": 9,
                  "rows": 2,
                  "cost": "COST_REPLACED",
                  "filtered": 100,
                  "attached_condition": "v2.b < 10",
                  "materialized": {
                    "query_block": {
                      "select_id": 6,
                      "cost": "COST_REPLACED",
                      "having_condition": "max_c < 707",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 20,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a > 5 and t1.b < 10"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}
# using derived table with union
# conjunctive subformulas : pushing into WHERE and HAVING
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v_union,t2 where (v_union.a<3) and (v_union.c>100);
a	b	c	a	b	c	d
1	19	107	2	3	207	207
1	19	107	1	21	909	12
1	19	107	7	13	312	406
1	19	107	8	64	248	107
1	19	107	6	20	315	279
1	19	107	1	19	203	107
1	19	107	8	80	800	314
1	19	107	3	12	231	190
1	19	107	6	23	303	909
select * from v_union,t2 where (v_union.a<3) and (v_union.c>100);
a	b	c	a	b	c	d
1	19	107	2	3	207	207
1	19	107	1	21	909	12
1	19	107	7	13	312	406
1	19	107	8	64	248	107
1	19	107	6	20	315	279
1	19	107	1	19	203	107
1	19	107	8	80	800	314
1	19	107	3	12	231	190
1	19	107	6	23	303	909
explain select * from v_union,t2 where (v_union.a<3) and (v_union.c>100);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	40	Using where; Using join buffer (flat, BNL join)
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
3	UNION	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL	
explain format=json select * from v_union,t2 where (v_union.a<3) and (v_union.c>100);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived2>",
            "access_type": "ALL",
            "loops": 9,
            "rows": 40,
            "cost": "COST_REPLACED",
            "filtered": 100,
            "attached_condition": "v_union.a < 3 and v_union.c > 100"
          },
          "buffer_type": "flat",
          "buffer_size": "238",
          "join_type": "BNL",
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<union2,3>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "COST_REPLACED",
                      "having_condition": "c > 109 and c > 100",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 20,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a < 10 and t1.a < 3"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "UNION",
                      "cost": "COST_REPLACED",
                      "having_condition": "c < 300 and c > 100",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 20,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.b > 10 and t1.a < 3"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
# using derived table with union
# conjunctive subformula : pushing into WHERE
# extracted or formula : pushing into HAVING
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v_union,t2 where
((v_union.a<2) or (v_union.c>800)) and (v_union.b>12);
a	b	c	a	b	c	d
1	19	107	2	3	207	207
1	19	107	1	21	909	12
1	19	107	7	13	312	406
1	19	107	8	64	248	107
1	19	107	6	20	315	279
1	19	107	1	19	203	107
1	19	107	8	80	800	314
1	19	107	3	12	231	190
1	19	107	6	23	303	909
select * from v_union,t2 where
((v_union.a<2) or (v_union.c>800)) and (v_union.b>12);
a	b	c	a	b	c	d
1	19	107	2	3	207	207
1	19	107	1	21	909	12
1	19	107	7	13	312	406
1	19	107	8	64	248	107
1	19	107	6	20	315	279
1	19	107	1	19	203	107
1	19	107	8	80	800	314
1	19	107	3	12	231	190
1	19	107	6	23	303	909
explain select * from v_union,t2 where
((v_union.a<2) or (v_union.c>800)) and (v_union.b>12);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	40	Using where; Using join buffer (flat, BNL join)
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
3	UNION	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL	
explain format=json select * from v_union,t2 where
((v_union.a<2) or (v_union.c>800)) and (v_union.b>12);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived2>",
            "access_type": "ALL",
            "loops": 9,
            "rows": 40,
            "cost": "COST_REPLACED",
            "filtered": 100,
            "attached_condition": "(v_union.a < 2 or v_union.c > 800) and v_union.b > 12"
          },
          "buffer_type": "flat",
          "buffer_size": "238",
          "join_type": "BNL",
          "attached_condition": "v_union.a < 2 or v_union.c > 800",
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<union2,3>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "COST_REPLACED",
                      "having_condition": "c > 109 and (t1.a < 2 or c > 800)",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 20,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a < 10 and t1.b > 12"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "UNION",
                      "cost": "COST_REPLACED",
                      "having_condition": "c < 300 and (t1.a < 2 or c > 800)",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 20,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.b > 10 and t1.b > 12"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
# using derived table with union
# conjunctive subformula : pushing into HAVING
# conjunctive subformula : pushing into WHERE
# pushing equalities
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v_union,t2 where
(v_union.a=1) and (v_union.a=t2.a) and (v_union.c<200);
a	b	c	a	b	c	d
1	19	107	1	21	909	12
1	19	107	1	19	203	107
select * from v_union,t2 where
(v_union.a=1) and (v_union.a=t2.a) and (v_union.c<200);
a	b	c	a	b	c	d
1	19	107	1	21	909	12
1	19	107	1	19	203	107
explain select * from v_union,t2 where
(v_union.a=1) and (v_union.a=t2.a) and (v_union.c<200);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ALL	distinct_key	NULL	NULL	NULL	40	Using where; Using join buffer (flat, BNL join)
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
3	UNION	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL	
explain format=json select * from v_union,t2 where
(v_union.a=1) and (v_union.a=t2.a) and (v_union.c<200);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a = 1"
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived2>",
            "access_type": "ALL",
            "possible_keys": ["distinct_key"],
            "loops": 9,
            "rows": 40,
            "cost": "COST_REPLACED",
            "filtered": 100,
            "attached_condition": "v_union.a = 1 and v_union.c < 200"
          },
          "buffer_type": "flat",
          "buffer_size": "238",
          "join_type": "BNL",
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<union2,3>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "COST_REPLACED",
                      "having_condition": "c > 109 and c < 200",
                      "filesort": {
                        "sort_key": "t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 20,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a = 1"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "UNION",
                      "cost": "COST_REPLACED",
                      "having_condition": "c < 300 and c < 200",
                      "filesort": {
                        "sort_key": "t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 20,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a = 1 and t1.b > 10"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v_char as v,t2_char as t where
(v.a=t.a) and (v.b='Vika') and (v.max_c>2);
a	b	max_c	a	b	c
c	Vika	7	c	Vinny	3
c	Vika	7	c	Inga	9
c	Vika	7	c	Ivan	2
c	Vika	7	c	Inga	2
select * from v_char as v,t2_char as t where
(v.a=t.a) and (v.b='Vika') and (v.max_c>2);
a	b	max_c	a	b	c
c	Vika	7	c	Vinny	3
c	Vika	7	c	Inga	9
c	Vika	7	c	Ivan	2
c	Vika	7	c	Inga	2
explain select * from v_char as v,t2_char as t where
(v.a=t.a) and (v.b='Vika') and (v.max_c>2);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t	ALL	NULL	NULL	NULL	NULL	12	Using where
1	PRIMARY	<derived2>	ref	key0	key0	2	test.t.a	1	Using where
2	DERIVED	t1_char	ALL	NULL	NULL	NULL	NULL	12	Using where; Using temporary; Using filesort
explain format=json select * from v_char as v,t2_char as t where
(v.a=t.a) and (v.b='Vika') and (v.max_c>2);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t",
          "access_type": "ALL",
          "loops": 1,
          "rows": 12,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "2",
          "used_key_parts": ["a"],
          "ref": ["test.t.a"],
          "loops": 12,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v.b = 'Vika' and v.max_c > 2",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 9 and max_c > 2",
              "filesort": {
                "sort_key": "t1_char.a",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1_char",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 12,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1_char.b = 'Vika'"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
# using derived table with union
# using several derived tables : pushing in all tables
# conjunctive subformula : pushing into WHERE using equalities
# pushing equalities
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v_union,v1,t2 where
(v_union.a=v1.a) and (v1.a=t2.a) and (t2.a=1)
and ((v_union.c>800) or (v1.max_c>200));
a	b	c	a	b	max_c	avg_c	a	b	c	d
1	19	107	1	21	500	234.6000	1	21	909	12
1	19	107	1	21	500	234.6000	1	19	203	107
select * from v_union,v1,t2 where
(v_union.a=v1.a) and (v1.a=t2.a) and (t2.a=1)
and ((v_union.c>800) or (v1.max_c>200));
a	b	c	a	b	max_c	avg_c	a	b	c	d
1	19	107	1	21	500	234.6000	1	21	909	12
1	19	107	1	21	500	234.6000	1	19	203	107
explain select * from v_union,v1,t2 where
(v_union.a=v1.a) and (v1.a=t2.a) and (t2.a=1)
and ((v_union.c>800) or (v1.max_c>200));
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived4>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
1	PRIMARY	<derived2>	ALL	distinct_key	NULL	NULL	NULL	40	Using where; Using join buffer (incremental, BNL join)
4	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
3	UNION	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL	
explain format=json select * from v_union,v1,t2 where
(v_union.a=v1.a) and (v1.a=t2.a) and (t2.a=1)
and ((v_union.c>800) or (v1.max_c>200));
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a = 1"
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived4>",
            "access_type": "ALL",
            "loops": 9,
            "rows": 20,
            "cost": "COST_REPLACED",
            "filtered": 100,
            "attached_condition": "v1.a = 1"
          },
          "buffer_type": "flat",
          "buffer_size": "238",
          "join_type": "BNL",
          "materialized": {
            "query_block": {
              "select_id": 4,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707",
              "filesort": {
                "sort_key": "t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.a = 1"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived2>",
            "access_type": "ALL",
            "possible_keys": ["distinct_key"],
            "loops": 180,
            "rows": 40,
            "cost": "COST_REPLACED",
            "filtered": 100,
            "attached_condition": "v_union.a = 1"
          },
          "buffer_type": "incremental",
          "buffer_size": "4KiB",
          "join_type": "BNL",
          "attached_condition": "v_union.c > 800 or v1.max_c > 200",
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<union2,3>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "COST_REPLACED",
                      "having_condition": "c > 109",
                      "filesort": {
                        "sort_key": "t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 20,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a = 1"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "UNION",
                      "cost": "COST_REPLACED",
                      "having_condition": "c < 300",
                      "filesort": {
                        "sort_key": "t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 20,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a = 1 and t1.b > 10"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
# using derived table with union
# extracted or formula : pushing into WHERE
# conjunctive subformula : pushing into HAVING
# pushing equalities
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v2_union as v,t2 where
((v.a=6) or (v.a=8)) and (v.c>200) and (v.a=t2.a);
a	b	c	a	b	c	d
8	33	404.0000	8	64	248	107
6	20	312.0000	6	20	315	279
6	20	214.0000	6	20	315	279
8	33	404.0000	8	80	800	314
6	20	312.0000	6	23	303	909
6	20	214.0000	6	23	303	909
select * from v2_union as v,t2 where
((v.a=6) or (v.a=8)) and (v.c>200) and (v.a=t2.a);
a	b	c	a	b	c	d
8	33	404.0000	8	64	248	107
6	20	312.0000	6	20	315	279
6	20	214.0000	6	20	315	279
8	33	404.0000	8	80	800	314
6	20	312.0000	6	23	303	909
6	20	214.0000	6	23	303	909
explain select * from v2_union as v,t2 where
((v.a=6) or (v.a=8)) and (v.c>200) and (v.a=t2.a);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key1,distinct_key	key1	5	test.t2.a	6	Using where
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
3	UNION	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
4	UNION	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
NULL	UNION RESULT	<union2,3,4>	ALL	NULL	NULL	NULL	NULL	NULL	
explain format=json select * from v2_union as v,t2 where
((v.a=6) or (v.a=8)) and (v.c>200) and (v.a=t2.a);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "(t2.a = 6 or t2.a = 8) and t2.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key1", "distinct_key"],
          "key": "key1",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t2.a"],
          "loops": 9,
          "rows": 6,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v.c > 200",
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<union2,3,4>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "COST_REPLACED",
                      "having_condition": "c > 109 and c > 200",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 20,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a < 10 and (t1.a = 6 or t1.a = 8)"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "UNION",
                      "cost": "COST_REPLACED",
                      "having_condition": "c < 300 and c > 200",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 20,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.b > 10 and (t1.a = 6 or t1.a = 8)"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 4,
                      "operation": "UNION",
                      "cost": "COST_REPLACED",
                      "having_condition": "c < 707 and c > 200",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 20,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.c > 300 and (t1.a = 6 or t1.a = 8)"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
# using derived table with union of selects without aggregation
# extracted conjunctive predicate: pushing in WHERE of both selects
set 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);
a	b	c	a	b	c	d
1	21	123	1	21	909	12
1	21	101	1	21	909	12
1	21	104	1	21	909	12
1	33	988	1	21	909	12
1	19	107	1	21	909	12
1	21	500	1	21	909	12
1	21	345	1	21	909	12
7	11	708	7	13	312	406
7	11	8	7	13	312	406
8	33	404	8	64	248	107
8	33	123	8	64	248	107
8	33	114	8	64	248	107
8	33	9	8	64	248	107
6	20	214	6	20	315	279
6	20	315	6	20	315	279
6	20	309	6	20	315	279
6	20	7	6	20	315	279
1	21	123	1	19	203	107
1	21	101	1	19	203	107
1	21	104	1	19	203	107
1	33	988	1	19	203	107
1	19	107	1	19	203	107
1	21	500	1	19	203	107
1	21	345	1	19	203	107
8	33	404	8	80	800	314
8	33	123	8	80	800	314
8	33	114	8	80	800	314
8	33	9	8	80	800	314
6	20	214	6	23	303	909
6	20	315	6	23	303	909
6	20	309	6	23	303	909
6	20	7	6	23	303	909
select * from v3_union as v,t2 where (v.a=t2.a) and (v.c>6);
a	b	c	a	b	c	d
1	21	123	1	21	909	12
1	21	101	1	21	909	12
1	21	104	1	21	909	12
1	33	988	1	21	909	12
1	19	107	1	21	909	12
1	21	500	1	21	909	12
1	21	345	1	21	909	12
7	11	708	7	13	312	406
7	11	8	7	13	312	406
8	33	404	8	64	248	107
8	33	123	8	64	248	107
8	33	114	8	64	248	107
8	33	9	8	64	248	107
6	20	214	6	20	315	279
6	20	315	6	20	315	279
6	20	309	6	20	315	279
6	20	7	6	20	315	279
1	21	123	1	19	203	107
1	21	101	1	19	203	107
1	21	104	1	19	203	107
1	33	988	1	19	203	107
1	19	107	1	19	203	107
1	21	500	1	19	203	107
1	21	345	1	19	203	107
8	33	404	8	80	800	314
8	33	123	8	80	800	314
8	33	114	8	80	800	314
8	33	9	8	80	800	314
6	20	214	6	23	303	909
6	20	315	6	23	303	909
6	20	309	6	23	303	909
6	20	7	6	23	303	909
explain select * from v3_union as v,t2 where (v.a=t2.a) and (v.c>6);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key1,distinct_key	key1	5	test.t2.a	4	Using where
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where
3	UNION	t1	ALL	NULL	NULL	NULL	NULL	20	Using where
NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL	
explain format=json select * from v3_union as v,t2 where (v.a=t2.a) and (v.c>6);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key1", "distinct_key"],
          "key": "key1",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t2.a"],
          "loops": 9,
          "rows": 4,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v.c > 6",
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<union2,3>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "COST_REPLACED",
                      "nested_loop": [
                        {
                          "table": {
                            "table_name": "t1",
                            "access_type": "ALL",
                            "loops": 1,
                            "rows": 20,
                            "cost": "COST_REPLACED",
                            "filtered": 100,
                            "attached_condition": "t1.a < 10 and t1.a + 1 > 6"
                          }
                        }
                      ]
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "UNION",
                      "cost": "COST_REPLACED",
                      "nested_loop": [
                        {
                          "table": {
                            "table_name": "t1",
                            "access_type": "ALL",
                            "loops": 1,
                            "rows": 20,
                            "cost": "COST_REPLACED",
                            "filtered": 100,
                            "attached_condition": "t1.b > 10 and t1.c > 100 and t1.c > 6"
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
# using derived table with union of selects without aggregation
# extracted conjunctive OR subformula: pushing in WHERE using equalities
set 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));
a	b	c	a	b	c	d
1	19	107	1	21	909	12
1	19	2	1	21	909	12
7	11	708	7	13	312	406
7	11	8	7	13	312	406
8	33	404	8	64	248	107
8	33	123	8	64	248	107
8	33	114	8	64	248	107
8	33	9	8	64	248	107
6	20	214	6	20	315	279
6	20	315	6	20	315	279
6	20	309	6	20	315	279
6	20	7	6	20	315	279
1	19	107	1	19	203	107
1	19	2	1	19	203	107
8	33	404	8	80	800	314
8	33	123	8	80	800	314
8	33	114	8	80	800	314
8	33	9	8	80	800	314
6	20	214	6	23	303	909
6	20	315	6	23	303	909
6	20	309	6	23	303	909
6	20	7	6	23	303	909
select * from v3_union as v,t2 where (v.a=t2.a) and ((t2.a>1) or (v.b<20));
a	b	c	a	b	c	d
1	19	107	1	21	909	12
1	19	2	1	21	909	12
7	11	708	7	13	312	406
7	11	8	7	13	312	406
8	33	404	8	64	248	107
8	33	123	8	64	248	107
8	33	114	8	64	248	107
8	33	9	8	64	248	107
6	20	214	6	20	315	279
6	20	315	6	20	315	279
6	20	309	6	20	315	279
6	20	7	6	20	315	279
1	19	107	1	19	203	107
1	19	2	1	19	203	107
8	33	404	8	80	800	314
8	33	123	8	80	800	314
8	33	114	8	80	800	314
8	33	9	8	80	800	314
6	20	214	6	23	303	909
6	20	315	6	23	303	909
6	20	309	6	23	303	909
6	20	7	6	23	303	909
explain select * from v3_union as v,t2 where (v.a=t2.a) and ((t2.a>1) or (v.b<20));
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key1,distinct_key	key1	5	test.t2.a	4	Using where
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where
3	UNION	t1	ALL	NULL	NULL	NULL	NULL	20	Using where
NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL	
explain format=json select * from v3_union as v,t2 where (v.a=t2.a) and ((t2.a>1) or (v.b<20));
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key1", "distinct_key"],
          "key": "key1",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t2.a"],
          "loops": 9,
          "rows": 4,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a > 1 or v.b < 20",
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<union2,3>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "COST_REPLACED",
                      "nested_loop": [
                        {
                          "table": {
                            "table_name": "t1",
                            "access_type": "ALL",
                            "loops": 1,
                            "rows": 20,
                            "cost": "COST_REPLACED",
                            "filtered": 100,
                            "attached_condition": "t1.a < 10 and (t1.a > 1 or t1.b < 20)"
                          }
                        }
                      ]
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "UNION",
                      "cost": "COST_REPLACED",
                      "nested_loop": [
                        {
                          "table": {
                            "table_name": "t1",
                            "access_type": "ALL",
                            "loops": 1,
                            "rows": 20,
                            "cost": "COST_REPLACED",
                            "filtered": 100,
                            "attached_condition": "t1.b > 10 and t1.c > 100 and (t1.a > 1 or t1.b < 20)"
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
# using derived table with union of selects without aggregation
# extracted the whole condition: in WHERE of both selects
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v3_union as v,t2 where
(v.a=t2.a) and ((v.b=19) or (v.b=21)) and ((v.c<3) or (v.c>600));
a	b	c	a	b	c	d
1	19	2	1	21	909	12
1	21	2	1	21	909	12
1	19	2	1	19	203	107
1	21	2	1	19	203	107
select * from v3_union as v,t2 where
(v.a=t2.a) and ((v.b=19) or (v.b=21)) and ((v.c<3) or (v.c>600));
a	b	c	a	b	c	d
1	19	2	1	21	909	12
1	21	2	1	21	909	12
1	19	2	1	19	203	107
1	21	2	1	19	203	107
explain select * from v3_union as v,t2 where
(v.a=t2.a) and ((v.b=19) or (v.b=21)) and ((v.c<3) or (v.c>600));
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key1,distinct_key	key1	5	test.t2.a	4	Using where
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where
3	UNION	t1	ALL	NULL	NULL	NULL	NULL	20	Using where
NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL	
explain format=json select * from v3_union as v,t2 where
(v.a=t2.a) and ((v.b=19) or (v.b=21)) and ((v.c<3) or (v.c>600));
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key1", "distinct_key"],
          "key": "key1",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t2.a"],
          "loops": 9,
          "rows": 4,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "(v.b = 19 or v.b = 21) and (v.c < 3 or v.c > 600)",
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<union2,3>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "COST_REPLACED",
                      "nested_loop": [
                        {
                          "table": {
                            "table_name": "t1",
                            "access_type": "ALL",
                            "loops": 1,
                            "rows": 20,
                            "cost": "COST_REPLACED",
                            "filtered": 100,
                            "attached_condition": "t1.a < 10 and (t1.b = 19 or t1.b = 21) and (t1.a + 1 < 3 or t1.a + 1 > 600)"
                          }
                        }
                      ]
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "UNION",
                      "cost": "COST_REPLACED",
                      "nested_loop": [
                        {
                          "table": {
                            "table_name": "t1",
                            "access_type": "ALL",
                            "loops": 1,
                            "rows": 20,
                            "cost": "COST_REPLACED",
                            "filtered": 100,
                            "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)"
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
# using derived table with union of
# a select without aggregation and a select with aggregation
# extracted conjunctive predicate: pushing in WHERE of both selects
set 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);
a	b	c	a	b	c	d
1	19	207	1	21	909	12
7	11	808	7	13	312	406
7	11	608	7	13	312	406
1	19	207	1	19	203	107
select * from v4_union as v,t2 where (v.a=t2.a) and (v.b<20);
a	b	c	a	b	c	d
1	19	207	1	21	909	12
7	11	808	7	13	312	406
7	11	608	7	13	312	406
1	19	207	1	19	203	107
explain select * from v4_union as v,t2 where (v.a=t2.a) and (v.b<20);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key1,distinct_key	key1	5	test.t2.a	4	Using where
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
3	UNION	t1	ALL	NULL	NULL	NULL	NULL	20	Using where
NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL	
explain format=json select * from v4_union as v,t2 where (v.a=t2.a) and (v.b<20);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key1", "distinct_key"],
          "key": "key1",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t2.a"],
          "loops": 9,
          "rows": 4,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v.b < 20",
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<union2,3>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "COST_REPLACED",
                      "having_condition": "c > 109",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 20,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a < 10 and t1.b < 20"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "UNION",
                      "cost": "COST_REPLACED",
                      "nested_loop": [
                        {
                          "table": {
                            "table_name": "t1",
                            "access_type": "ALL",
                            "loops": 1,
                            "rows": 20,
                            "cost": "COST_REPLACED",
                            "filtered": 100,
                            "attached_condition": "t1.b > 10 and t1.b < 20"
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
# using derived table with union of
# a select without aggregation and a select with aggregation
# extracted subformula: pushing in WHERE of one select
# extracted subformula: pushing in HAVING of the other select
# extracted sub-subformula: pushing in WHERE of the other select
# using an equality in all pushdowns
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v4_union as v,t2 where
(v.a=t2.a) and ((t2.a<3) or (v.b<40)) and (v.c>500);
a	b	c	a	b	c	d
1	33	1088	1	21	909	12
1	21	600	1	21	909	12
1	33	888	1	21	909	12
7	11	808	7	13	312	406
7	11	608	7	13	312	406
8	33	504	8	64	248	107
1	33	1088	1	19	203	107
1	21	600	1	19	203	107
1	33	888	1	19	203	107
8	33	504	8	80	800	314
select * from v4_union as v,t2 where
(v.a=t2.a) and ((t2.a<3) or (v.b<40)) and (v.c>500);
a	b	c	a	b	c	d
1	33	1088	1	21	909	12
1	21	600	1	21	909	12
1	33	888	1	21	909	12
7	11	808	7	13	312	406
7	11	608	7	13	312	406
8	33	504	8	64	248	107
1	33	1088	1	19	203	107
1	21	600	1	19	203	107
1	33	888	1	19	203	107
8	33	504	8	80	800	314
explain select * from v4_union as v,t2 where
(v.a=t2.a) and ((t2.a<3) or (v.b<40)) and (v.c>500);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key1,distinct_key	key1	5	test.t2.a	4	Using where
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
3	UNION	t1	ALL	NULL	NULL	NULL	NULL	20	Using where
NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL	
explain format=json select * from v4_union as v,t2 where
(v.a=t2.a) and ((t2.a<3) or (v.b<40)) and (v.c>500);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key1", "distinct_key"],
          "key": "key1",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t2.a"],
          "loops": 9,
          "rows": 4,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "(t2.a < 3 or v.b < 40) and v.c > 500",
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<union2,3>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "COST_REPLACED",
                      "having_condition": "c > 109 and c > 500",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 20,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a < 10 and (t1.a < 3 or t1.b < 40)"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "UNION",
                      "cost": "COST_REPLACED",
                      "nested_loop": [
                        {
                          "table": {
                            "table_name": "t1",
                            "access_type": "ALL",
                            "loops": 1,
                            "rows": 20,
                            "cost": "COST_REPLACED",
                            "filtered": 100,
                            "attached_condition": "t1.b > 10 and (t1.a < 3 or t1.b < 40) and t1.c + 100 > 500"
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
# using embedded derived table : pushing the same conditions
# using several derived tables : pushing in all tables
# conjunctive subformula : pushing into WHERE
# extracted and formula : pushing into WHERE
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v4,v1 where
(v4.a<13) and (v1.a>5) and (v1.b>12);
a	b	min_c	a	b	max_c	avg_c
1	19	107	6	20	315	279.3333
1	21	500	6	20	315	279.3333
5	16	207	6	20	315	279.3333
5	27	132	6	20	315	279.3333
6	20	315	6	20	315	279.3333
8	33	404	6	20	315	279.3333
1	19	107	8	33	404	213.6667
1	21	500	8	33	404	213.6667
5	16	207	8	33	404	213.6667
5	27	132	8	33	404	213.6667
6	20	315	8	33	404	213.6667
8	33	404	8	33	404	213.6667
select * from v4,v1 where
(v4.a<13) and (v1.a>5) and (v1.b>12);
a	b	min_c	a	b	max_c	avg_c
1	19	107	6	20	315	279.3333
1	21	500	6	20	315	279.3333
5	16	207	6	20	315	279.3333
5	27	132	6	20	315	279.3333
6	20	315	6	20	315	279.3333
8	33	404	6	20	315	279.3333
1	19	107	8	33	404	213.6667
1	21	500	8	33	404	213.6667
5	16	207	8	33	404	213.6667
5	27	132	8	33	404	213.6667
6	20	315	8	33	404	213.6667
8	33	404	8	33	404	213.6667
explain select * from v4,v1 where
(v4.a<13) and (v1.a>5) and (v1.b>12);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where
1	PRIMARY	<derived4>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (flat, BNL join)
4	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
2	DERIVED	<derived3>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
3	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
explain format=json select * from v4,v1 where
(v4.a<13) and (v1.a>5) and (v1.b>12);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 20,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v4.a < 13",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "filesort": {
                "sort_key": "v1.a, v1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "<derived3>",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "v1.a < 15 and v1.a < 13",
                        "materialized": {
                          "query_block": {
                            "select_id": 3,
                            "cost": "COST_REPLACED",
                            "having_condition": "max_c < 707",
                            "filesort": {
                              "sort_key": "t1.a, t1.b",
                              "temporary_table": {
                                "nested_loop": [
                                  {
                                    "table": {
                                      "table_name": "t1",
                                      "access_type": "ALL",
                                      "loops": 1,
                                      "rows": 20,
                                      "cost": "COST_REPLACED",
                                      "filtered": 100,
                                      "attached_condition": "t1.a < 15 and t1.a < 13"
                                    }
                                  }
                                ]
                              }
                            }
                          }
                        }
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived4>",
            "access_type": "ALL",
            "loops": 20,
            "rows": 20,
            "cost": "COST_REPLACED",
            "filtered": 100,
            "attached_condition": "v1.a > 5 and v1.b > 12"
          },
          "buffer_type": "flat",
          "buffer_size": "333",
          "join_type": "BNL",
          "materialized": {
            "query_block": {
              "select_id": 4,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.a > 5 and t1.b > 12"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
# using embedded view : nothing to push
# using several derived tables : pushing only in one table
# conjunctive subformula : pushing into WHERE
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v4,v1,t2 where
(v4.a=t2.a) and (v4.a=v1.a) and (v1.b>30);
a	b	min_c	a	b	max_c	avg_c	a	b	c	d
8	33	404	8	33	404	213.6667	8	64	248	107
8	33	404	8	33	404	213.6667	8	80	800	314
select * from v4,v1,t2 where
(v4.a=t2.a) and (v4.a=v1.a) and (v1.b>30);
a	b	min_c	a	b	max_c	avg_c	a	b	c	d
8	33	404	8	33	404	213.6667	8	64	248	107
8	33	404	8	33	404	213.6667	8	80	800	314
explain select * from v4,v1,t2 where
(v4.a=t2.a) and (v4.a=v1.a) and (v1.b>30);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key1	key1	5	test.t2.a	2	
1	PRIMARY	<derived4>	ref	key0	key0	5	test.t2.a	2	Using where
4	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
2	DERIVED	<derived3>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
3	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
explain format=json select * from v4,v1,t2 where
(v4.a=t2.a) and (v4.a=v1.a) and (v1.b>30);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a is not null and t2.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key1"],
          "key": "key1",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t2.a"],
          "loops": 9,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "filesort": {
                "sort_key": "v1.a, v1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "<derived3>",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "v1.a < 15",
                        "materialized": {
                          "query_block": {
                            "select_id": 3,
                            "cost": "COST_REPLACED",
                            "having_condition": "max_c < 707",
                            "filesort": {
                              "sort_key": "t1.a, t1.b",
                              "temporary_table": {
                                "nested_loop": [
                                  {
                                    "table": {
                                      "table_name": "t1",
                                      "access_type": "ALL",
                                      "loops": 1,
                                      "rows": 20,
                                      "cost": "COST_REPLACED",
                                      "filtered": 100,
                                      "attached_condition": "t1.a < 15"
                                    }
                                  }
                                ]
                              }
                            }
                          }
                        }
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      },
      {
        "table": {
          "table_name": "<derived4>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t2.a"],
          "loops": 18,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v1.b > 30",
          "materialized": {
            "query_block": {
              "select_id": 4,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.b > 30"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
# using embedded view : pushing different conditions
# using several derived tables : pushing in all tables
# conjunctive subformula : pushing into WHERE using equalities
# extracted and formula : pushing into WHERE using equalities
# conjunctive subformula : pushing into HAVING
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v4,v1,t2 where
(v4.a=t2.a) and (v4.a>1) and (v4.a=v1.a) and (v4.min_c>100) and (v1.b<30);
a	b	min_c	a	b	max_c	avg_c	a	b	c	d
6	20	315	6	20	315	279.3333	6	20	315	279
6	20	315	6	20	315	279.3333	6	23	303	909
select * from v4,v1,t2 where
(v4.a=t2.a) and (v4.a>1) and (v4.a=v1.a) and (v4.min_c>100) and (v1.b<30);
a	b	min_c	a	b	max_c	avg_c	a	b	c	d
6	20	315	6	20	315	279.3333	6	20	315	279
6	20	315	6	20	315	279.3333	6	23	303	909
explain select * from v4,v1,t2 where
(v4.a=t2.a) and (v4.a>1) and (v4.a=v1.a) and (v4.min_c>100) and (v1.b<30);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key1	key1	5	test.t2.a	2	Using where
1	PRIMARY	<derived4>	ref	key0	key0	5	test.t2.a	2	Using where
4	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
2	DERIVED	<derived3>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
3	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
explain format=json select * from v4,v1,t2 where
(v4.a=t2.a) and (v4.a>1) and (v4.a=v1.a) and (v4.min_c>100) and (v1.b<30);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a > 1 and t2.a is not null and t2.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key1"],
          "key": "key1",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t2.a"],
          "loops": 9,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v4.min_c > 100",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "min_c > 100",
              "filesort": {
                "sort_key": "v1.a, v1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "<derived3>",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "v1.a < 15 and v1.a > 1",
                        "materialized": {
                          "query_block": {
                            "select_id": 3,
                            "cost": "COST_REPLACED",
                            "having_condition": "max_c < 707",
                            "filesort": {
                              "sort_key": "t1.a, t1.b",
                              "temporary_table": {
                                "nested_loop": [
                                  {
                                    "table": {
                                      "table_name": "t1",
                                      "access_type": "ALL",
                                      "loops": 1,
                                      "rows": 20,
                                      "cost": "COST_REPLACED",
                                      "filtered": 100,
                                      "attached_condition": "t1.a < 15 and t1.a > 1"
                                    }
                                  }
                                ]
                              }
                            }
                          }
                        }
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      },
      {
        "table": {
          "table_name": "<derived4>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t2.a"],
          "loops": 18,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v1.b < 30",
          "materialized": {
            "query_block": {
              "select_id": 4,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.a > 1 and t1.b < 30"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
# using embedded view : pushing different conditions
# using several derived tables : pushing in all tables
# extracted or formula : pushing into WHERE
# conjunctive subformula : pushing into HAVING
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v4,v1,t2 where
(((v4.b>10) and (v4.a>1)) or (v4.b<20)) and (v1.max_c>200) and (v1.a=v4.a);
a	b	min_c	a	b	max_c	avg_c	a	b	c	d
1	19	107	1	21	500	234.6000	2	3	207	207
5	16	207	5	16	207	207.0000	2	3	207	207
5	27	132	5	16	207	207.0000	2	3	207	207
6	20	315	6	20	315	279.3333	2	3	207	207
8	33	404	8	33	404	213.6667	2	3	207	207
1	19	107	1	21	500	234.6000	1	21	909	12
5	16	207	5	16	207	207.0000	1	21	909	12
5	27	132	5	16	207	207.0000	1	21	909	12
6	20	315	6	20	315	279.3333	1	21	909	12
8	33	404	8	33	404	213.6667	1	21	909	12
1	19	107	1	21	500	234.6000	7	13	312	406
5	16	207	5	16	207	207.0000	7	13	312	406
5	27	132	5	16	207	207.0000	7	13	312	406
6	20	315	6	20	315	279.3333	7	13	312	406
8	33	404	8	33	404	213.6667	7	13	312	406
1	19	107	1	21	500	234.6000	8	64	248	107
5	16	207	5	16	207	207.0000	8	64	248	107
5	27	132	5	16	207	207.0000	8	64	248	107
6	20	315	6	20	315	279.3333	8	64	248	107
8	33	404	8	33	404	213.6667	8	64	248	107
1	19	107	1	21	500	234.6000	6	20	315	279
5	16	207	5	16	207	207.0000	6	20	315	279
5	27	132	5	16	207	207.0000	6	20	315	279
6	20	315	6	20	315	279.3333	6	20	315	279
8	33	404	8	33	404	213.6667	6	20	315	279
1	19	107	1	21	500	234.6000	1	19	203	107
5	16	207	5	16	207	207.0000	1	19	203	107
5	27	132	5	16	207	207.0000	1	19	203	107
6	20	315	6	20	315	279.3333	1	19	203	107
8	33	404	8	33	404	213.6667	1	19	203	107
1	19	107	1	21	500	234.6000	8	80	800	314
5	16	207	5	16	207	207.0000	8	80	800	314
5	27	132	5	16	207	207.0000	8	80	800	314
6	20	315	6	20	315	279.3333	8	80	800	314
8	33	404	8	33	404	213.6667	8	80	800	314
1	19	107	1	21	500	234.6000	3	12	231	190
5	16	207	5	16	207	207.0000	3	12	231	190
5	27	132	5	16	207	207.0000	3	12	231	190
6	20	315	6	20	315	279.3333	3	12	231	190
8	33	404	8	33	404	213.6667	3	12	231	190
1	19	107	1	21	500	234.6000	6	23	303	909
5	16	207	5	16	207	207.0000	6	23	303	909
5	27	132	5	16	207	207.0000	6	23	303	909
6	20	315	6	20	315	279.3333	6	23	303	909
8	33	404	8	33	404	213.6667	6	23	303	909
select * from v4,v1,t2 where
(((v4.b>10) and (v4.a>1)) or (v4.b<20)) and (v1.max_c>200) and (v1.a=v4.a);
a	b	min_c	a	b	max_c	avg_c	a	b	c	d
1	19	107	1	21	500	234.6000	2	3	207	207
5	16	207	5	16	207	207.0000	2	3	207	207
5	27	132	5	16	207	207.0000	2	3	207	207
6	20	315	6	20	315	279.3333	2	3	207	207
8	33	404	8	33	404	213.6667	2	3	207	207
1	19	107	1	21	500	234.6000	1	21	909	12
5	16	207	5	16	207	207.0000	1	21	909	12
5	27	132	5	16	207	207.0000	1	21	909	12
6	20	315	6	20	315	279.3333	1	21	909	12
8	33	404	8	33	404	213.6667	1	21	909	12
1	19	107	1	21	500	234.6000	7	13	312	406
5	16	207	5	16	207	207.0000	7	13	312	406
5	27	132	5	16	207	207.0000	7	13	312	406
6	20	315	6	20	315	279.3333	7	13	312	406
8	33	404	8	33	404	213.6667	7	13	312	406
1	19	107	1	21	500	234.6000	8	64	248	107
5	16	207	5	16	207	207.0000	8	64	248	107
5	27	132	5	16	207	207.0000	8	64	248	107
6	20	315	6	20	315	279.3333	8	64	248	107
8	33	404	8	33	404	213.6667	8	64	248	107
1	19	107	1	21	500	234.6000	6	20	315	279
5	16	207	5	16	207	207.0000	6	20	315	279
5	27	132	5	16	207	207.0000	6	20	315	279
6	20	315	6	20	315	279.3333	6	20	315	279
8	33	404	8	33	404	213.6667	6	20	315	279
1	19	107	1	21	500	234.6000	1	19	203	107
5	16	207	5	16	207	207.0000	1	19	203	107
5	27	132	5	16	207	207.0000	1	19	203	107
6	20	315	6	20	315	279.3333	1	19	203	107
8	33	404	8	33	404	213.6667	1	19	203	107
1	19	107	1	21	500	234.6000	8	80	800	314
5	16	207	5	16	207	207.0000	8	80	800	314
5	27	132	5	16	207	207.0000	8	80	800	314
6	20	315	6	20	315	279.3333	8	80	800	314
8	33	404	8	33	404	213.6667	8	80	800	314
1	19	107	1	21	500	234.6000	3	12	231	190
5	16	207	5	16	207	207.0000	3	12	231	190
5	27	132	5	16	207	207.0000	3	12	231	190
6	20	315	6	20	315	279.3333	3	12	231	190
8	33	404	8	33	404	213.6667	3	12	231	190
1	19	107	1	21	500	234.6000	6	23	303	909
5	16	207	5	16	207	207.0000	6	23	303	909
5	27	132	5	16	207	207.0000	6	23	303	909
6	20	315	6	20	315	279.3333	6	23	303	909
8	33	404	8	33	404	213.6667	6	23	303	909
explain select * from v4,v1,t2 where
(((v4.b>10) and (v4.a>1)) or (v4.b<20)) and (v1.max_c>200) and (v1.a=v4.a);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where
1	PRIMARY	<derived4>	ref	key0	key0	5	v4.a	2	Using where
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using join buffer (flat, BNL join)
4	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using temporary; Using filesort
2	DERIVED	<derived3>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
3	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
explain format=json select * from v4,v1,t2 where
(((v4.b>10) and (v4.a>1)) or (v4.b<20)) and (v1.max_c>200) and (v1.a=v4.a);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 20,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "(v4.b > 10 and v4.a > 1 or v4.b < 20) and v4.a is not null",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "filesort": {
                "sort_key": "v1.a, v1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "<derived3>",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "v1.a < 15 and (v1.b > 10 and v1.a > 1 or v1.b < 20)",
                        "materialized": {
                          "query_block": {
                            "select_id": 3,
                            "cost": "COST_REPLACED",
                            "having_condition": "max_c < 707",
                            "filesort": {
                              "sort_key": "t1.a, t1.b",
                              "temporary_table": {
                                "nested_loop": [
                                  {
                                    "table": {
                                      "table_name": "t1",
                                      "access_type": "ALL",
                                      "loops": 1,
                                      "rows": 20,
                                      "cost": "COST_REPLACED",
                                      "filtered": 100,
                                      "attached_condition": "t1.a < 15 and (t1.b > 10 and t1.a > 1 or t1.b < 20)"
                                    }
                                  }
                                ]
                              }
                            }
                          }
                        }
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      },
      {
        "table": {
          "table_name": "<derived4>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["v4.a"],
          "loops": 20,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v1.max_c > 200",
          "materialized": {
            "query_block": {
              "select_id": 4,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707 and max_c > 200",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "t2",
            "access_type": "ALL",
            "loops": 40,
            "rows": 9,
            "cost": "COST_REPLACED",
            "filtered": 100
          },
          "buffer_type": "flat",
          "buffer_size": "1KiB",
          "join_type": "BNL"
        }
      }
    ]
  }
}
# using embedded view : pushing different conditions
# using several derived tables : pushing only in one table
# extracted or formula : pushing into WHERE
# extracted or formula : pushing into HAVING
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v4,v2 where
((v4.a>12) and (v4.min_c<300) and (v4.b>13)) or (v4.a<1);
a	b	min_c	a	b	max_c	avg_c
select * from v4,v2 where
((v4.a>12) and (v4.min_c<300) and (v4.b>13)) or (v4.a<1);
a	b	min_c	a	b	max_c	avg_c
explain select * from v4,v2 where
((v4.a>12) and (v4.min_c<300) and (v4.b>13)) or (v4.a<1);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where
1	PRIMARY	<derived4>	ALL	NULL	NULL	NULL	NULL	20	Using join buffer (flat, BNL join)
4	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
2	DERIVED	<derived3>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
3	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
explain format=json select * from v4,v2 where
((v4.a>12) and (v4.min_c<300) and (v4.b>13)) or (v4.a<1);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 20,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v4.a > 12 and v4.min_c < 300 and v4.b > 13 or v4.a < 1",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "v1.a > 12 and min_c < 300 and v1.b > 13 or v1.a < 1",
              "filesort": {
                "sort_key": "v1.a, v1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "<derived3>",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "v1.a < 15 and (v1.a > 12 and v1.b > 13 or v1.a < 1)",
                        "materialized": {
                          "query_block": {
                            "select_id": 3,
                            "cost": "COST_REPLACED",
                            "having_condition": "max_c < 707",
                            "filesort": {
                              "sort_key": "t1.a, t1.b",
                              "temporary_table": {
                                "nested_loop": [
                                  {
                                    "table": {
                                      "table_name": "t1",
                                      "access_type": "ALL",
                                      "loops": 1,
                                      "rows": 20,
                                      "cost": "COST_REPLACED",
                                      "filtered": 100,
                                      "attached_condition": "t1.a < 15 and (t1.a > 12 and t1.b > 13 or t1.a < 1)"
                                    }
                                  }
                                ]
                              }
                            }
                          }
                        }
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived4>",
            "access_type": "ALL",
            "loops": 20,
            "rows": 20,
            "cost": "COST_REPLACED",
            "filtered": 100
          },
          "buffer_type": "flat",
          "buffer_size": "333",
          "join_type": "BNL",
          "materialized": {
            "query_block": {
              "select_id": 4,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.a > 5"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
# using embedded view : pushing different conditions
# using several derived tables : pushing only in one table
# conjunctive subformula : pushing into WHERE
# conjunctive subformula : pushing into HAVING
# pushing equalities
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v4,v2 where
(v4.a=v2.b) and (v4.a=v4.b) and (v4.min_c<100);
a	b	min_c	a	b	max_c	avg_c
select * from v4,v2 where
(v4.a=v2.b) and (v4.a=v4.b) and (v4.min_c<100);
a	b	min_c	a	b	max_c	avg_c
explain select * from v4,v2 where
(v4.a=v2.b) and (v4.a=v4.b) and (v4.min_c<100);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where
1	PRIMARY	<derived4>	ref	key0	key0	5	v4.a	2	
4	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
2	DERIVED	<derived3>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
3	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
explain format=json select * from v4,v2 where
(v4.a=v2.b) and (v4.a=v4.b) and (v4.min_c<100);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 20,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v4.b = v4.a and v4.min_c < 100 and v4.a is not null",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "min_c < 100",
              "filesort": {
                "sort_key": "v1.a, v1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "<derived3>",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "v1.b = v1.a and v1.a < 15",
                        "materialized": {
                          "query_block": {
                            "select_id": 3,
                            "cost": "COST_REPLACED",
                            "having_condition": "max_c < 707",
                            "filesort": {
                              "sort_key": "t1.a, t1.b",
                              "temporary_table": {
                                "nested_loop": [
                                  {
                                    "table": {
                                      "table_name": "t1",
                                      "access_type": "ALL",
                                      "loops": 1,
                                      "rows": 20,
                                      "cost": "COST_REPLACED",
                                      "filtered": 100,
                                      "attached_condition": "t1.b = t1.a and t1.a < 15"
                                    }
                                  }
                                ]
                              }
                            }
                          }
                        }
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      },
      {
        "table": {
          "table_name": "<derived4>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "5",
          "used_key_parts": ["b"],
          "ref": ["v4.a"],
          "loops": 20,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "materialized": {
            "query_block": {
              "select_id": 4,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.a > 5"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
# using embedded view : pushing the same conditions
# using several derived tables : pushing in all tables
# extracted and formula : pushing into WHERE using equalities
# conjunctive subformula : pushing into WHERE
# pushing equalities
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v4,v2 where
(v4.a=v2.b) and (v4.a=v4.b) and (v2.b<30);
a	b	min_c	a	b	max_c	avg_c
select * from v4,v2 where
(v4.a=v2.b) and (v4.a=v4.b) and (v2.b<30);
a	b	min_c	a	b	max_c	avg_c
explain select * from v4,v2 where
(v4.a=v2.b) and (v4.a=v4.b) and (v2.b<30);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where
1	PRIMARY	<derived4>	ref	key0	key0	5	v4.a	2	
4	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
2	DERIVED	<derived3>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
3	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
explain format=json select * from v4,v2 where
(v4.a=v2.b) and (v4.a=v4.b) and (v2.b<30);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 20,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v4.b = v4.a and v4.a < 30 and v4.a is not null",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "filesort": {
                "sort_key": "v1.a, v1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "<derived3>",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "v1.b = v1.a and v1.a < 15 and v1.a < 30",
                        "materialized": {
                          "query_block": {
                            "select_id": 3,
                            "cost": "COST_REPLACED",
                            "having_condition": "max_c < 707",
                            "filesort": {
                              "sort_key": "t1.a, t1.b",
                              "temporary_table": {
                                "nested_loop": [
                                  {
                                    "table": {
                                      "table_name": "t1",
                                      "access_type": "ALL",
                                      "loops": 1,
                                      "rows": 20,
                                      "cost": "COST_REPLACED",
                                      "filtered": 100,
                                      "attached_condition": "t1.b = t1.a and t1.a < 15 and t1.a < 30"
                                    }
                                  }
                                ]
                              }
                            }
                          }
                        }
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      },
      {
        "table": {
          "table_name": "<derived4>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "5",
          "used_key_parts": ["b"],
          "ref": ["v4.a"],
          "loops": 20,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "materialized": {
            "query_block": {
              "select_id": 4,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.a > 5 and t1.b < 30"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
# using embedded view : pushing the same conditions
# using several derived tables : pushing in all tables
# extracted or formula : pushing into WHERE using equalities
# extracted and formula : pushing into WHERE using equalities
# pushing equalities
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v4,v2 where
(v4.a=v2.b) and (v4.a=v4.b) and ((v2.b<30) or (v4.a>2));
a	b	min_c	a	b	max_c	avg_c
select * from v4,v2 where
(v4.a=v2.b) and (v4.a=v4.b) and ((v2.b<30) or (v4.a>2));
a	b	min_c	a	b	max_c	avg_c
explain select * from v4,v2 where
(v4.a=v2.b) and (v4.a=v4.b) and ((v2.b<30) or (v4.a>2));
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where
1	PRIMARY	<derived4>	ref	key0	key0	5	v4.a	2	
4	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
2	DERIVED	<derived3>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
3	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
explain format=json select * from v4,v2 where
(v4.a=v2.b) and (v4.a=v4.b) and ((v2.b<30) or (v4.a>2));
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 20,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v4.b = v4.a and (v4.a < 30 or v4.a > 2) and v4.a is not null",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "filesort": {
                "sort_key": "v1.a, v1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "<derived3>",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "v1.b = v1.a and v1.a < 15 and (v1.a < 30 or v1.a > 2)",
                        "materialized": {
                          "query_block": {
                            "select_id": 3,
                            "cost": "COST_REPLACED",
                            "having_condition": "max_c < 707",
                            "filesort": {
                              "sort_key": "t1.a, t1.b",
                              "temporary_table": {
                                "nested_loop": [
                                  {
                                    "table": {
                                      "table_name": "t1",
                                      "access_type": "ALL",
                                      "loops": 1,
                                      "rows": 20,
                                      "cost": "COST_REPLACED",
                                      "filtered": 100,
                                      "attached_condition": "t1.b = t1.a and t1.a < 15 and (t1.a < 30 or t1.a > 2)"
                                    }
                                  }
                                ]
                              }
                            }
                          }
                        }
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      },
      {
        "table": {
          "table_name": "<derived4>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "5",
          "used_key_parts": ["b"],
          "ref": ["v4.a"],
          "loops": 20,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "materialized": {
            "query_block": {
              "select_id": 4,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.a > 5 and (t1.b < 30 or t1.b > 2)"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
# using embedded view : pushing the same conditions
# using several derived tables : pushing in all tables
# extracted or formula : pushing into WHERE
# conjunctive subformula : pushing into WHERE
# pushing equalities
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v4,v2 where
(((v4.a<12) and (v4.b>13)) or (v4.a>10)) and
(v4.min_c=v2.max_c) and (v4.min_c>100);
a	b	min_c	a	b	max_c	avg_c
6	20	315	6	20	315	279.3333
8	33	404	8	33	404	213.6667
select * from v4,v2 where
(((v4.a<12) and (v4.b>13)) or (v4.a>10)) and
(v4.min_c=v2.max_c) and (v4.min_c>100);
a	b	min_c	a	b	max_c	avg_c
6	20	315	6	20	315	279.3333
8	33	404	8	33	404	213.6667
explain select * from v4,v2 where
(((v4.a<12) and (v4.b>13)) or (v4.a>10)) and
(v4.min_c=v2.max_c) and (v4.min_c>100);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where
1	PRIMARY	<derived4>	ref	key0	key0	5	v4.min_c	2	
4	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
2	DERIVED	<derived3>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
3	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
explain format=json select * from v4,v2 where
(((v4.a<12) and (v4.b>13)) or (v4.a>10)) and
(v4.min_c=v2.max_c) and (v4.min_c>100);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 20,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "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",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "min_c > 100",
              "filesort": {
                "sort_key": "v1.a, v1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "<derived3>",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "v1.a < 15 and (v1.a < 12 and v1.b > 13 or v1.a > 10)",
                        "materialized": {
                          "query_block": {
                            "select_id": 3,
                            "cost": "COST_REPLACED",
                            "having_condition": "max_c < 707",
                            "filesort": {
                              "sort_key": "t1.a, t1.b",
                              "temporary_table": {
                                "nested_loop": [
                                  {
                                    "table": {
                                      "table_name": "t1",
                                      "access_type": "ALL",
                                      "loops": 1,
                                      "rows": 20,
                                      "cost": "COST_REPLACED",
                                      "filtered": 100,
                                      "attached_condition": "t1.a < 15 and (t1.a < 12 and t1.b > 13 or t1.a > 10)"
                                    }
                                  }
                                ]
                              }
                            }
                          }
                        }
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      },
      {
        "table": {
          "table_name": "<derived4>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "5",
          "used_key_parts": ["max_c"],
          "ref": ["v4.min_c"],
          "loops": 20,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "materialized": {
            "query_block": {
              "select_id": 4,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707 and max_c > 100",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.a > 5"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
# using embedded view : pushing the same conditions
# using several derived tables : pushing only in one table
# extracted or formula : pushing into WHERE
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v4,v2,t2 where
(((v4.a<12) and (t2.b>13)) or (v4.a>10)) and
(v4.min_c=t2.c) and (t2.c>100);
a	b	min_c	a	b	max_c	avg_c	a	b	c	d
6	20	315	6	20	315	279.3333	6	20	315	279
6	20	315	8	33	404	213.6667	6	20	315	279
select * from v4,v2,t2 where
(((v4.a<12) and (t2.b>13)) or (v4.a>10)) and
(v4.min_c=t2.c) and (t2.c>100);
a	b	min_c	a	b	max_c	avg_c	a	b	c	d
6	20	315	6	20	315	279.3333	6	20	315	279
6	20	315	8	33	404	213.6667	6	20	315	279
explain select * from v4,v2,t2 where
(((v4.a<12) and (t2.b>13)) or (v4.a>10)) and
(v4.min_c=t2.c) and (t2.c>100);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.c	2	Using where
1	PRIMARY	<derived4>	ALL	NULL	NULL	NULL	NULL	20	Using join buffer (flat, BNL join)
4	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
2	DERIVED	<derived3>	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
3	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
explain format=json select * from v4,v2,t2 where
(((v4.a<12) and (t2.b>13)) or (v4.a>10)) and
(v4.min_c=t2.c) and (t2.c>100);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.c > 100 and t2.c is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "5",
          "used_key_parts": ["min_c"],
          "ref": ["test.t2.c"],
          "loops": 9,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v4.a < 12 and t2.b > 13 or v4.a > 10",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "min_c > 100",
              "filesort": {
                "sort_key": "v1.a, v1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "<derived3>",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "v1.a < 15 and (v1.a < 12 or v1.a > 10)",
                        "materialized": {
                          "query_block": {
                            "select_id": 3,
                            "cost": "COST_REPLACED",
                            "having_condition": "max_c < 707",
                            "filesort": {
                              "sort_key": "t1.a, t1.b",
                              "temporary_table": {
                                "nested_loop": [
                                  {
                                    "table": {
                                      "table_name": "t1",
                                      "access_type": "ALL",
                                      "loops": 1,
                                      "rows": 20,
                                      "cost": "COST_REPLACED",
                                      "filtered": 100,
                                      "attached_condition": "t1.a < 15 and (t1.a < 12 or t1.a > 10)"
                                    }
                                  }
                                ]
                              }
                            }
                          }
                        }
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived4>",
            "access_type": "ALL",
            "loops": 18,
            "rows": 20,
            "cost": "COST_REPLACED",
            "filtered": 100
          },
          "buffer_type": "flat",
          "buffer_size": "715",
          "join_type": "BNL",
          "materialized": {
            "query_block": {
              "select_id": 4,
              "cost": "COST_REPLACED",
              "having_condition": "max_c < 707",
              "filesort": {
                "sort_key": "t1.a, t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.a > 5"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
drop view v1,v2,v3,v4;
drop view v_union,v2_union,v3_union,v4_union;
drop view v_double,v_char,v_decimal;
drop table t1,t2,t1_double,t2_double,t1_char,t2_char,t1_decimal,t2_decimal;
#
# MDEV-10782: condition extracted from a multiple equality
#             pushed into HAVING
#
CREATE TABLE t1 (i int);
INSERT INTO t1 VALUES (1),(2);
EXPLAIN EXTENDED
SELECT *
FROM ( SELECT * FROM ( SELECT MIN(i) as f FROM t1 ) sq1 ) AS sq2
WHERE f = 8;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	PRIMARY	<derived3>	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where
3	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	2	100.00	
Warnings:
Note	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
SELECT *
FROM ( SELECT * FROM ( SELECT MIN(i) as f FROM t1 ) sq1 ) AS sq2
WHERE f = 8;
f
SELECT *
FROM ( SELECT * FROM ( SELECT MIN(i) as f FROM t1 ) sq1 ) AS sq2
WHERE f = 1;
f
1
DROP TABLE t1;
#
# MDEV-10783: pushdown into constant view
#
CREATE TABLE t1 (i int) ENGINE=MyISAM;
CREATE VIEW v AS SELECT 5;
SELECT * FROM t1 WHERE 1 IN ( SELECT * FROM v );
i
DROP VIEW v;
DROP TABLE t1;
#
# MDEV-10785: second execution of a query with condition
#             pushed into view
#
CREATE TABLE t1 (i int);
CREATE VIEW v1 AS SELECT i FROM t1 WHERE i < 5;
CREATE FUNCTION f (in1 int) RETURNS int RETURN in1;
CREATE VIEW v2 AS SELECT * FROM v1 GROUP BY i;
PREPARE stmt FROM "SELECT * FROM v2 WHERE f(0) <> 2";
EXECUTE stmt;
i
EXECUTE stmt;
i
DROP FUNCTION f;
DROP VIEW v2,v1;
DROP TABLE t1;
#
# MDEV-10884: condition pushdown into derived specified by
#             1. unit with  SELECT containing ORDER BY ... LIMIT
#             2. unit containing global ORDER BY ... LIMIT
#
create table t1(a int);
insert into t1 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
select a from t1 order by a limit 5;
a
0
1
2
3
4
set statement optimizer_switch='condition_pushdown_for_derived=off' for
select * from (select a from t1 order by a limit 5) t where t.a not in (1,2,3);
a
0
4
set statement optimizer_switch='condition_pushdown_for_derived=on' for
select * from (select a from t1 order by a limit 5) t where t.a not in (1,2,3);
a
0
4
select a from t1 where a < 4 union select a from t1 where a > 5
order by a limit 5;
a
0
1
2
3
6
set statement optimizer_switch='condition_pushdown_for_derived=off' for
select * from
(select a from t1 where a < 4 union select a from t1 where a > 5
order by a limit 5) t where t.a not in (2,9);
a
0
1
3
6
set statement optimizer_switch='condition_pushdown_for_derived=on' for
select * from
(select a from t1 where a < 4 union select a from t1 where a > 5
order by a limit 5) t where t.a not in (2,9);
a
0
1
3
6
drop table t1;
#
# MDEV-11072: pushdown of the condition obtained
#             after constant row substitution
#
CREATE TABLE t1 (a INT) ENGINE=MyISAM;
CREATE TABLE t2 (b INT) ENGINE=MyISAM;
CREATE OR REPLACE VIEW v2 AS SELECT * FROM t2;
CREATE TABLE t3 (c INT) ENGINE=MyISAM;
CREATE OR REPLACE ALGORITHM=TEMPTABLE VIEW v3 AS SELECT * FROM t3;
SELECT * FROM t1 WHERE a IN (
SELECT b FROM v2 WHERE b < a OR b IN (
SELECT c FROM v3 WHERE c = a
)
);
a
INSERT INTO t1 VALUES (2);
INSERT INTO t2 VALUES (3), (2);
INSERT INTO t3 VALUES (4), (1), (2), (7);
SELECT * FROM t1 WHERE a IN (
SELECT b FROM v2 WHERE b < a OR b IN (
SELECT c FROM v3 WHERE c = a
)
);
a
2
EXPLAIN FORMAT=JSON
SELECT * FROM t1 WHERE a IN (
SELECT b FROM v2 WHERE b < a OR b IN (
SELECT c FROM v3 WHERE c = a
)
);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "const_condition": "0 or <in_optimizer>(2,<exists>(subquery#3))",
    "nested_loop": [
      {
        "table": {
          "table_name": "t1",
          "access_type": "system",
          "rows": 1,
          "filtered": 100
        }
      },
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 50,
          "attached_condition": "t2.b = 2",
          "first_match": "t1"
        }
      }
    ],
    "subqueries": [
      {
        "query_block": {
          "select_id": 3,
          "cost": "COST_REPLACED",
          "nested_loop": [
            {
              "table": {
                "table_name": "<derived5>",
                "access_type": "index_subquery",
                "possible_keys": ["key0"],
                "key": "key0",
                "key_length": "5",
                "used_key_parts": ["c"],
                "ref": ["func"],
                "loops": 1,
                "rows": 1,
                "cost": "COST_REPLACED",
                "filtered": 100,
                "materialized": {
                  "query_block": {
                    "select_id": 5,
                    "cost": "COST_REPLACED",
                    "nested_loop": [
                      {
                        "table": {
                          "table_name": "t3",
                          "access_type": "ALL",
                          "loops": 1,
                          "rows": 4,
                          "cost": "COST_REPLACED",
                          "filtered": 100,
                          "attached_condition": "t3.c = 2"
                        }
                      }
                    ]
                  }
                }
              }
            }
          ]
        }
      }
    ]
  }
}
CREATE TABLE t4 (d INT, e INT) ENGINE=MyISAM;
INSERT INTO t4 VALUES (1,10),(3,11),(2,10),(2,20),(3,21);
CREATE OR REPLACE VIEW v4 AS
SELECT d, sum(e) AS s FROM t4 GROUP BY d;
set statement optimizer_switch='condition_pushdown_for_derived=off' for SELECT * FROM t1 WHERE a IN (
SELECT b FROM v2 WHERE b < a OR b IN (
SELECT d FROM v4 WHERE s > a
)
);
a
2
SELECT * FROM t1 WHERE a IN (
SELECT b FROM v2 WHERE b < a OR b IN (
SELECT d FROM v4 WHERE s > a
)
);
a
2
explain SELECT * FROM t1 WHERE a IN (
SELECT b FROM v2 WHERE b < a OR b IN (
SELECT d FROM v4 WHERE s > a
)
);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; FirstMatch(t1)
3	DEPENDENT SUBQUERY	<derived5>	index_subquery	key0	key0	5	func	1	Using where
5	DERIVED	t4	ALL	NULL	NULL	NULL	NULL	5	Using temporary; Using filesort
explain format=json SELECT * FROM t1 WHERE a IN (
SELECT b FROM v2 WHERE b < a OR b IN (
SELECT d FROM v4 WHERE s > a
)
);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "const_condition": "0 or <in_optimizer>(2,<exists>(subquery#3))",
    "nested_loop": [
      {
        "table": {
          "table_name": "t1",
          "access_type": "system",
          "rows": 1,
          "filtered": 100
        }
      },
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 50,
          "attached_condition": "t2.b = 2",
          "first_match": "t1"
        }
      }
    ],
    "subqueries": [
      {
        "query_block": {
          "select_id": 3,
          "cost": "COST_REPLACED",
          "nested_loop": [
            {
              "table": {
                "table_name": "<derived5>",
                "access_type": "index_subquery",
                "possible_keys": ["key0"],
                "key": "key0",
                "key_length": "5",
                "used_key_parts": ["d"],
                "ref": ["func"],
                "loops": 1,
                "rows": 1,
                "cost": "COST_REPLACED",
                "filtered": 100,
                "materialized": {
                  "query_block": {
                    "select_id": 5,
                    "cost": "COST_REPLACED",
                    "having_condition": "s > 2",
                    "filesort": {
                      "sort_key": "t4.d",
                      "temporary_table": {
                        "nested_loop": [
                          {
                            "table": {
                              "table_name": "t4",
                              "access_type": "ALL",
                              "loops": 1,
                              "rows": 5,
                              "cost": "COST_REPLACED",
                              "filtered": 100
                            }
                          }
                        ]
                      }
                    }
                  }
                }
              }
            }
          ]
        }
      }
    ]
  }
}
DROP VIEW v2,v3,v4;
DROP TABLE t1,t2,t3,t4;
#
# MDEV-10800: pushdown of the condition obtained
#             after constant row substitution
#
CREATE TABLE t1 (a INT) ENGINE=MyISAM;
INSERT INTO t1 VALUES (1);
CREATE TABLE t2 (b INT) ENGINE=MyISAM;
INSERT INTO t2 VALUES (3),(4);
CREATE OR REPLACE ALGORITHM=TEMPTABLE VIEW v2 AS SELECT * FROM t2;
SELECT * FROM
( SELECT * FROM t1
WHERE EXISTS ( SELECT * FROM v2 WHERE b = a ) ) AS sq;
a
EXPLAIN FORMAT=JSON
SELECT * FROM
( SELECT * FROM t1
WHERE EXISTS ( SELECT * FROM v2 WHERE b = a ) ) AS sq;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "const_condition": "<in_optimizer>(1,exists(subquery#3))",
    "nested_loop": [
      {
        "table": {
          "table_name": "t1",
          "access_type": "system",
          "rows": 1,
          "filtered": 100
        }
      }
    ],
    "subqueries": [
      {
        "query_block": {
          "select_id": 3,
          "cost": "COST_REPLACED",
          "nested_loop": [
            {
              "table": {
                "table_name": "<derived4>",
                "access_type": "ALL",
                "loops": 1,
                "rows": 2,
                "cost": "COST_REPLACED",
                "filtered": 100,
                "attached_condition": "v2.b = 1",
                "materialized": {
                  "query_block": {
                    "select_id": 4,
                    "cost": "COST_REPLACED",
                    "nested_loop": [
                      {
                        "table": {
                          "table_name": "t2",
                          "access_type": "ALL",
                          "loops": 1,
                          "rows": 2,
                          "cost": "COST_REPLACED",
                          "filtered": 100,
                          "attached_condition": "t2.b = 1"
                        }
                      }
                    ]
                  }
                }
              }
            }
          ]
        }
      }
    ]
  }
}
DROP VIEW v2;
DROP TABLE t1,t2;
#
# MDEV-11102: condition pushdown into materialized inner table
#              of outer join is not applied as not being valid
#
CREATE TABLE t1 (a INT);
INSERT INTO t1 VALUES (0),(2);
CREATE TABLE t2 (b INT);
INSERT INTO t2 VALUES (1),(2);
CREATE OR REPLACE ALGORITHM=TEMPTABLE VIEW v2 AS SELECT * FROM t2;
SELECT * FROM t1 LEFT JOIN t2 ON a = b WHERE b IS NULL;
a	b
0	NULL
SELECT * FROM t1 LEFT JOIN v2 ON a = b WHERE b IS NULL;
a	b
0	NULL
EXPLAIN FORMAT=JSON
SELECT * FROM t1 LEFT JOIN v2 ON a = b WHERE b IS NULL;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t1",
          "access_type": "ALL",
          "loops": 1,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "5",
          "used_key_parts": ["b"],
          "ref": ["test.t1.a"],
          "loops": 2,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "trigcond(v2.b is null) and trigcond(trigcond(t1.a is not null))",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "nested_loop": [
                {
                  "table": {
                    "table_name": "t2",
                    "access_type": "ALL",
                    "loops": 1,
                    "rows": 2,
                    "cost": "COST_REPLACED",
                    "filtered": 100
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}
DROP VIEW v2;
DROP TABLE t1,t2;
#
# MDEV-11103: pushdown condition with ANY subquery
#
CREATE TABLE t1 (i INT);
CREATE OR REPLACE ALGORITHM=TEMPTABLE VIEW v1 AS SELECT * FROM t1;
INSERT INTO t1 VALUES (1),(2);
EXPLAIN FORMAT=JSON
SELECT * FROM v1 WHERE i <= ANY ( SELECT 3 );
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<derived3>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "<nop>(v1.i <= 3)",
          "materialized": {
            "query_block": {
              "select_id": 3,
              "cost": "COST_REPLACED",
              "nested_loop": [
                {
                  "table": {
                    "table_name": "t1",
                    "access_type": "ALL",
                    "loops": 1,
                    "rows": 2,
                    "cost": "COST_REPLACED",
                    "filtered": 100,
                    "attached_condition": "<nop>(t1.i <= 3)"
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}
Warnings:
Note	1249	Select 2 was reduced during optimization
SELECT * FROM v1 WHERE i <= ANY ( SELECT 3 );
i
1
2
DROP VIEW v1;
DROP TABLE t1;
#
# MDEV-11315: condition with outer reference to mergeable derived
#
CREATE TABLE t1 (pk1 INT PRIMARY KEY, a INT, b INT) ENGINE=MyISAM;
INSERT INTO t1 VALUES (10,7,1),(11,0,2);
CREATE TABLE t2 (pk2 INT PRIMARY KEY, c INT, d DATETIME) ENGINE=MyISAM;
INSERT INTO t2 VALUES
(1,4,'2008-09-27 00:34:58'),
(2,5,'2007-05-28 00:00:00'),
(3,6,'2009-07-25 09:21:20');
CREATE VIEW v1 AS SELECT * FROM t1;
CREATE ALGORITHM=TEMPTABLE VIEW v2 AS SELECT * FROM t2;
SELECT * FROM v1 AS sq
WHERE b IN ( SELECT pk2 FROM v2 WHERE c > sq.b ) OR b = 100;
pk1	a	b
10	7	1
11	0	2
EXPLAIN FORMAT=JSON
SELECT * FROM v1 AS sq
WHERE b IN ( SELECT pk2 FROM v2 WHERE c > sq.b ) OR b = 100;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t1",
          "access_type": "ALL",
          "loops": 1,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "<in_optimizer>(t1.b,<exists>(subquery#2)) or t1.b = 100"
        }
      }
    ],
    "subqueries": [
      {
        "query_block": {
          "select_id": 2,
          "cost": "COST_REPLACED",
          "nested_loop": [
            {
              "table": {
                "table_name": "<derived4>",
                "access_type": "index_subquery",
                "possible_keys": ["key0"],
                "key": "key0",
                "key_length": "4",
                "used_key_parts": ["pk2"],
                "ref": ["func"],
                "loops": 1,
                "rows": 1,
                "cost": "COST_REPLACED",
                "filtered": 100,
                "materialized": {
                  "query_block": {
                    "select_id": 4,
                    "cost": "COST_REPLACED",
                    "nested_loop": [
                      {
                        "table": {
                          "table_name": "t2",
                          "access_type": "ALL",
                          "loops": 1,
                          "rows": 3,
                          "cost": "COST_REPLACED",
                          "filtered": 100
                        }
                      }
                    ]
                  }
                }
              }
            }
          ]
        }
      }
    ]
  }
}
SELECT * FROM ( SELECT * FROM t1 ) AS sq
WHERE b IN ( SELECT pk2 FROM v2 WHERE c > sq.b ) OR b = 100;
pk1	a	b
10	7	1
11	0	2
EXPLAIN FORMAT=JSON
SELECT * FROM ( SELECT * FROM t1 ) AS sq
WHERE b IN ( SELECT pk2 FROM v2 WHERE c > sq.b ) OR b = 100;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t1",
          "access_type": "ALL",
          "loops": 1,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "<in_optimizer>(t1.b,<exists>(subquery#3)) or t1.b = 100"
        }
      }
    ],
    "subqueries": [
      {
        "query_block": {
          "select_id": 3,
          "cost": "COST_REPLACED",
          "nested_loop": [
            {
              "table": {
                "table_name": "<derived4>",
                "access_type": "index_subquery",
                "possible_keys": ["key0"],
                "key": "key0",
                "key_length": "4",
                "used_key_parts": ["pk2"],
                "ref": ["func"],
                "loops": 1,
                "rows": 1,
                "cost": "COST_REPLACED",
                "filtered": 100,
                "materialized": {
                  "query_block": {
                    "select_id": 4,
                    "cost": "COST_REPLACED",
                    "nested_loop": [
                      {
                        "table": {
                          "table_name": "t2",
                          "access_type": "ALL",
                          "loops": 1,
                          "rows": 3,
                          "cost": "COST_REPLACED",
                          "filtered": 100
                        }
                      }
                    ]
                  }
                }
              }
            }
          ]
        }
      }
    ]
  }
}
DROP VIEW v1,v2;
DROP TABLE t1,t2;
#
# MDEV-11313: pushdown of the condition obtained
#             after constant row substitution
#
CREATE TABLE t1 (a INT) ENGINE=MyISAM;
INSERT INTO t1 VALUES (1),(2);
CREATE TABLE t2 (b INT) ENGINE=MyISAM;
INSERT INTO t2 VALUES (50);
CREATE ALGORITHM=TEMPTABLE VIEW v1 AS SELECT * FROM t1;
SELECT ( SELECT COUNT(*) FROM v1 WHERE a = t2.b ) AS f FROM t2 GROUP BY f;
f
0
EXPLAIN FORMAT=JSON
SELECT ( SELECT COUNT(*) FROM v1 WHERE a = t2.b ) AS f FROM t2 GROUP BY f;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "system",
          "rows": 1,
          "filtered": 100
        }
      }
    ],
    "subqueries": [
      {
        "query_block": {
          "select_id": 2,
          "cost": "COST_REPLACED",
          "nested_loop": [
            {
              "table": {
                "table_name": "<derived3>",
                "access_type": "ALL",
                "loops": 1,
                "rows": 2,
                "cost": "COST_REPLACED",
                "filtered": 100,
                "attached_condition": "v1.a = 50",
                "materialized": {
                  "query_block": {
                    "select_id": 3,
                    "cost": "COST_REPLACED",
                    "nested_loop": [
                      {
                        "table": {
                          "table_name": "t1",
                          "access_type": "ALL",
                          "loops": 1,
                          "rows": 2,
                          "cost": "COST_REPLACED",
                          "filtered": 100,
                          "attached_condition": "t1.a = 50"
                        }
                      }
                    ]
                  }
                }
              }
            }
          ]
        }
      }
    ]
  }
}
CREATE TABLE t3 (a INT, b INT) ENGINE=MYISAM;
INSERT INTO t3 VALUES (1,10),(3,11),(2,10),(2,20),(3,21);
CREATE VIEW v2 AS SELECT a, sum(b) AS s FROM t3 GROUP BY a ;
SELECT ( SELECT COUNT(*) FROM v2 WHERE s < t2.b ) AS f FROM t2 GROUP BY f;
f
3
EXPLAIN FORMAT=JSON
SELECT ( SELECT COUNT(*) FROM v2 WHERE s < t2.b ) AS f FROM t2 GROUP BY f;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "system",
          "rows": 1,
          "filtered": 100
        }
      }
    ],
    "subqueries": [
      {
        "query_block": {
          "select_id": 2,
          "cost": "COST_REPLACED",
          "nested_loop": [
            {
              "table": {
                "table_name": "<derived3>",
                "access_type": "ALL",
                "loops": 1,
                "rows": 5,
                "cost": "COST_REPLACED",
                "filtered": 100,
                "attached_condition": "v2.s < 50",
                "materialized": {
                  "query_block": {
                    "select_id": 3,
                    "cost": "COST_REPLACED",
                    "having_condition": "s < 50",
                    "filesort": {
                      "sort_key": "t3.a",
                      "temporary_table": {
                        "nested_loop": [
                          {
                            "table": {
                              "table_name": "t3",
                              "access_type": "ALL",
                              "loops": 1,
                              "rows": 5,
                              "cost": "COST_REPLACED",
                              "filtered": 100
                            }
                          }
                        ]
                      }
                    }
                  }
                }
              }
            }
          ]
        }
      }
    ]
  }
}
DROP VIEW v1,v2;
DROP TABLE t1,t2,t3;
#
# MDEV-10882: pushdown of the predicate with cached value
#
CREATE TABLE t1 (a INT NOT NULL, b INT NOT NULL) ENGINE=MyISAM;
CREATE OR REPLACE ALGORITHM=TEMPTABLE VIEW v1 AS SELECT * FROM t1;
INSERT INTO t1 VALUES (1,2),(3,4);
CREATE TABLE t2 (c INT NOT NULL) ENGINE=MyISAM;
INSERT INTO t2 VALUES (5),(6);
SELECT a, GROUP_CONCAT(b) FROM v1
WHERE b IN ( SELECT COUNT(c) FROM t2 ) GROUP BY a;
a	GROUP_CONCAT(b)
1	2
EXPLAIN FORMAT=JSON
SELECT a, GROUP_CONCAT(b) FROM v1
WHERE b IN ( SELECT COUNT(c) FROM t2 ) GROUP BY a;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<subquery2>",
          "access_type": "system",
          "rows": 0,
          "filtered": 0,
          "materialized": {
            "unique": 1,
            "materialization": {
              "query_block": {
                "select_id": 2,
                "table": {
                  "message": "Select tables optimized away"
                }
              }
            }
          }
        }
      },
      {
        "read_sorted_file": {
          "filesort": {
            "sort_key": "v1.a",
            "table": {
              "table_name": "<derived3>",
              "access_type": "ALL",
              "loops": 1,
              "rows": 2,
              "cost": "COST_REPLACED",
              "filtered": 100,
              "attached_condition": "v1.b = 2",
              "materialized": {
                "query_block": {
                  "select_id": 3,
                  "cost": "COST_REPLACED",
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 2,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.b = 2"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
DROP VIEW v1;
DROP TABLE t1,t2;
#
# MDEV-10836: pushdown of the predicate with cached value
#
CREATE TABLE t (pk INT PRIMARY KEY, f INT) ENGINE=MyISAM;
CREATE ALGORITHM=TEMPTABLE VIEW v AS SELECT * FROM t;
INSERT INTO t VALUES (1,1),(3,2);
SELECT * FROM v AS v1, v AS v2
WHERE v2.pk > v1.f AND v1.f IN ( SELECT COUNT(pk) FROM t );
pk	f	pk	f
3	2	3	2
EXPLAIN FORMAT=JSON
SELECT * FROM v AS v1, v AS v2
WHERE v2.pk > v1.f AND v1.f IN ( SELECT COUNT(pk) FROM t );
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<subquery2>",
          "access_type": "system",
          "rows": 0,
          "filtered": 0,
          "materialized": {
            "unique": 1,
            "materialization": {
              "query_block": {
                "select_id": 2,
                "table": {
                  "message": "Select tables optimized away"
                }
              }
            }
          }
        }
      },
      {
        "table": {
          "table_name": "<derived3>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v1.f = 2",
          "materialized": {
            "query_block": {
              "select_id": 3,
              "cost": "COST_REPLACED",
              "nested_loop": [
                {
                  "table": {
                    "table_name": "t",
                    "access_type": "ALL",
                    "loops": 1,
                    "rows": 2,
                    "cost": "COST_REPLACED",
                    "filtered": 100,
                    "attached_condition": "t.f = 2"
                  }
                }
              ]
            }
          }
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived4>",
            "access_type": "ALL",
            "loops": 2,
            "rows": 2,
            "cost": "COST_REPLACED",
            "filtered": 100,
            "attached_condition": "v2.pk > 2"
          },
          "buffer_type": "flat",
          "buffer_size": "119",
          "join_type": "BNL",
          "materialized": {
            "query_block": {
              "select_id": 4,
              "cost": "COST_REPLACED",
              "nested_loop": [
                {
                  "table": {
                    "table_name": "t",
                    "access_type": "range",
                    "possible_keys": ["PRIMARY"],
                    "key": "PRIMARY",
                    "key_length": "4",
                    "used_key_parts": ["pk"],
                    "loops": 1,
                    "rows": 1,
                    "cost": "COST_REPLACED",
                    "filtered": 100,
                    "index_condition": "t.pk > 2"
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}
DROP VIEW v;
DROP TABLE t;
#
# MDEV-11488: pushdown of the predicate with cached value
#
CREATE TABLE t1 (i INT) ENGINE=MyISAM;
INSERT INTO t1 VALUES (1),(3),(2);
CREATE TABLE t2 (j INT, KEY(j)) ENGINE=MyISAM;
INSERT INTO t2 VALUES (3),(4);
SELECT * FROM ( SELECT DISTINCT * FROM t1 ) AS sq
WHERE i IN ( SELECT MIN(j) FROM t2 );
i
3
EXPLAIN FORMAT=JSON
SELECT * FROM ( SELECT DISTINCT * FROM t1 ) AS sq
WHERE i IN ( SELECT MIN(j) FROM t2 );
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<subquery3>",
          "access_type": "system",
          "rows": 0,
          "filtered": 0,
          "materialized": {
            "unique": 1,
            "materialization": {
              "query_block": {
                "select_id": 3,
                "table": {
                  "message": "Select tables optimized away"
                }
              }
            }
          }
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 3,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "sq.i = 3",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "nested_loop": [
                {
                  "table": {
                    "table_name": "t1",
                    "access_type": "ALL",
                    "loops": 1,
                    "rows": 3,
                    "cost": "COST_REPLACED",
                    "filtered": 100,
                    "attached_condition": "t1.i = 3"
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}
UPDATE t2 SET j = 2 WHERE j = 3;
SELECT * FROM ( SELECT DISTINCT * FROM t1 ) AS sq
WHERE i IN ( SELECT MIN(j) FROM t2 );
i
2
DROP TABLE t1,t2;
CREATE TABLE t1 (i FLOAT) ENGINE=MyISAM;
INSERT INTO t1 VALUES (1.5),(3.2),(2.71);
CREATE TABLE t2 (j FLOAT, KEY(j)) ENGINE=MyISAM;
INSERT INTO t2 VALUES (3.2),(2.71);
SELECT * FROM ( SELECT DISTINCT * FROM t1 ) AS sq
WHERE i IN ( SELECT MIN(j) FROM t2 );
i
2.71
EXPLAIN FORMAT=JSON
SELECT * FROM ( SELECT DISTINCT * FROM t1 ) AS sq
WHERE i IN ( SELECT MIN(j) FROM t2 );
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<subquery3>",
          "access_type": "system",
          "rows": 0,
          "filtered": 0,
          "materialized": {
            "unique": 1,
            "materialization": {
              "query_block": {
                "select_id": 3,
                "table": {
                  "message": "Select tables optimized away"
                }
              }
            }
          }
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 3,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "sq.i = 2.71",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "nested_loop": [
                {
                  "table": {
                    "table_name": "t1",
                    "access_type": "ALL",
                    "loops": 1,
                    "rows": 3,
                    "cost": "COST_REPLACED",
                    "filtered": 100,
                    "attached_condition": "t1.i = 2.7100000381469727"
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}
DROP TABLE t1,t2;
CREATE TABLE t1 (i DECIMAL(10,2)) ENGINE=MyISAM;
INSERT INTO t1 VALUES (1.5),(3.21),(2.47);
CREATE TABLE t2 (j DECIMAL(10,2), KEY(j)) ENGINE=MyISAM;
INSERT INTO t2 VALUES (3.21),(4.55);
SELECT * FROM ( SELECT DISTINCT * FROM t1 ) AS sq
WHERE i IN ( SELECT MIN(j) FROM t2 );
i
3.21
EXPLAIN FORMAT=JSON
SELECT * FROM ( SELECT DISTINCT * FROM t1 ) AS sq
WHERE i IN ( SELECT MIN(j) FROM t2 );
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<subquery3>",
          "access_type": "system",
          "rows": 0,
          "filtered": 0,
          "materialized": {
            "unique": 1,
            "materialization": {
              "query_block": {
                "select_id": 3,
                "table": {
                  "message": "Select tables optimized away"
                }
              }
            }
          }
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 3,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "sq.i = 3.21",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "nested_loop": [
                {
                  "table": {
                    "table_name": "t1",
                    "access_type": "ALL",
                    "loops": 1,
                    "rows": 3,
                    "cost": "COST_REPLACED",
                    "filtered": 100,
                    "attached_condition": "t1.i = 3.21"
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}
DROP TABLE t1,t2;
CREATE TABLE t1 (i VARCHAR(32)) ENGINE=MyISAM;
INSERT INTO t1 VALUES ('cc'),('aa'),('ddd');
CREATE TABLE t2 (j VARCHAR(16), KEY(j)) ENGINE=MyISAM;
INSERT INTO t2 VALUES ('bbb'),('aa');
SELECT * FROM ( SELECT DISTINCT * FROM t1 ) AS sq
WHERE i IN ( SELECT MIN(j) FROM t2 );
i
aa
EXPLAIN FORMAT=JSON
SELECT * FROM ( SELECT DISTINCT * FROM t1 ) AS sq
WHERE i IN ( SELECT MIN(j) FROM t2 );
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<subquery3>",
          "access_type": "system",
          "rows": 0,
          "filtered": 0,
          "materialized": {
            "unique": 1,
            "materialization": {
              "query_block": {
                "select_id": 3,
                "table": {
                  "message": "Select tables optimized away"
                }
              }
            }
          }
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 3,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "sq.i = 'aa'",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "nested_loop": [
                {
                  "table": {
                    "table_name": "t1",
                    "access_type": "ALL",
                    "loops": 1,
                    "rows": 3,
                    "cost": "COST_REPLACED",
                    "filtered": 100,
                    "attached_condition": "t1.i = 'aa'"
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}
DROP TABLE t1,t2;
CREATE TABLE t1 (i DATETIME) ENGINE=MyISAM;
INSERT INTO t1 VALUES
('2008-09-27 00:34:58'),('2007-05-28 00:00:00'), ('2009-07-25 09:21:20');
CREATE TABLE t2 (j DATETIME, KEY(j)) ENGINE=MyISAM;
INSERT INTO t2 VALUES
('2007-05-28 00:00:00'), ('2010-08-25 00:00:00');
SELECT * FROM ( SELECT DISTINCT * FROM t1 ) AS sq
WHERE i IN ( SELECT MIN(j) FROM t2 );
i
2007-05-28 00:00:00
EXPLAIN FORMAT=JSON
SELECT * FROM ( SELECT DISTINCT * FROM t1 ) AS sq
WHERE i IN ( SELECT MIN(j) FROM t2 );
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<subquery3>",
          "access_type": "system",
          "rows": 0,
          "filtered": 0,
          "materialized": {
            "unique": 1,
            "materialization": {
              "query_block": {
                "select_id": 3,
                "table": {
                  "message": "Select tables optimized away"
                }
              }
            }
          }
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 3,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "sq.i = '2007-05-28 00:00:00'",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "nested_loop": [
                {
                  "table": {
                    "table_name": "t1",
                    "access_type": "ALL",
                    "loops": 1,
                    "rows": 3,
                    "cost": "COST_REPLACED",
                    "filtered": 100,
                    "attached_condition": "t1.i = TIMESTAMP'2007-05-28 00:00:00'"
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}
DROP TABLE t1,t2;
CREATE TABLE t1 (i DATE) ENGINE=MyISAM;
INSERT INTO t1 VALUES ('2008-09-27'),('2007-05-28'), ('2009-07-25');
CREATE TABLE t2 (j DATE, KEY(j)) ENGINE=MyISAM;
INSERT INTO t2 VALUES ('2007-05-28'), ('2010-08-25');
SELECT * FROM ( SELECT DISTINCT * FROM t1 ) AS sq
WHERE i IN ( SELECT MIN(j) FROM t2 );
i
2007-05-28
EXPLAIN FORMAT=JSON
SELECT * FROM ( SELECT DISTINCT * FROM t1 ) AS sq
WHERE i IN ( SELECT MIN(j) FROM t2 );
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<subquery3>",
          "access_type": "system",
          "rows": 0,
          "filtered": 0,
          "materialized": {
            "unique": 1,
            "materialization": {
              "query_block": {
                "select_id": 3,
                "table": {
                  "message": "Select tables optimized away"
                }
              }
            }
          }
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 3,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "sq.i = '2007-05-28'",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "nested_loop": [
                {
                  "table": {
                    "table_name": "t1",
                    "access_type": "ALL",
                    "loops": 1,
                    "rows": 3,
                    "cost": "COST_REPLACED",
                    "filtered": 100,
                    "attached_condition": "t1.i = DATE'2007-05-28'"
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}
DROP TABLE t1,t2;
CREATE TABLE t1 (i TIME) ENGINE=MyISAM;
INSERT INTO t1 VALUES ('00:34:58'),('10:00:02'), ('09:21:20');
CREATE TABLE t2 (j TIME, KEY(j)) ENGINE=MyISAM;
INSERT INTO t2 VALUES ('10:00:02'), ('11:00:10');
SELECT * FROM ( SELECT DISTINCT * FROM t1 ) AS sq
WHERE i IN ( SELECT MIN(j) FROM t2 );
i
10:00:02
EXPLAIN FORMAT=JSON
SELECT * FROM ( SELECT DISTINCT * FROM t1 ) AS sq
WHERE i IN ( SELECT MIN(j) FROM t2 );
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<subquery3>",
          "access_type": "system",
          "rows": 0,
          "filtered": 0,
          "materialized": {
            "unique": 1,
            "materialization": {
              "query_block": {
                "select_id": 3,
                "table": {
                  "message": "Select tables optimized away"
                }
              }
            }
          }
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 3,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "sq.i = '10:00:02'",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "nested_loop": [
                {
                  "table": {
                    "table_name": "t1",
                    "access_type": "ALL",
                    "loops": 1,
                    "rows": 3,
                    "cost": "COST_REPLACED",
                    "filtered": 100,
                    "attached_condition": "t1.i = TIME'10:00:02'"
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}
DROP TABLE t1,t2;
#
# MDEV-11593: pushdown of condition with NULLIF
#
CREATE TABLE t1 (i INT);
CREATE OR REPLACE ALGORITHM=TEMPTABLE VIEW v1 AS SELECT * FROM t1;
INSERT INTO t1 VALUES (2), (1);
SELECT * FROM v1 WHERE NULLIF(1, i);
i
2
EXPLAIN FORMAT=JSON
SELECT * FROM v1 WHERE NULLIF(1, i);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "nullif(1,v1.i)",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "nested_loop": [
                {
                  "table": {
                    "table_name": "t1",
                    "access_type": "ALL",
                    "loops": 1,
                    "rows": 2,
                    "cost": "COST_REPLACED",
                    "filtered": 100,
                    "attached_condition": "nullif(1,t1.i)"
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}
DROP VIEW v1;
DROP TABLE t1;
#
# MDEV-11608: pushdown of the predicate with cached null value
#
CREATE TABLE t1 (c VARCHAR(3));
CREATE ALGORITHM=TEMPTABLE VIEW v1 AS SELECT * FROM t1;
INSERT INTO t1 VALUES ('foo'),('bar');
CREATE TABLE t2 (c VARCHAR(3));
INSERT INTO t2 VALUES ('foo'),('xyz');
SELECT * FROM v1 WHERE v1.c IN ( SELECT MIN(c) FROM t2 WHERE 0 );
c
EXPLAIN FORMAT=JSON
SELECT * FROM v1 WHERE v1.c IN ( SELECT MIN(c) FROM t2 WHERE 0 );
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "table": {
      "message": "Impossible WHERE"
    },
    "subqueries": [
      {
        "materialization": {
          "query_block": {
            "select_id": 2,
            "table": {
              "message": "Impossible WHERE"
            }
          }
        }
      }
    ]
  }
}
DROP VIEW v1;
DROP TABLE t1,t2;
CREATE TABLE t1 (d DECIMAL(10,2));
CREATE ALGORITHM=TEMPTABLE VIEW v1 AS SELECT * FROM t1;
INSERT INTO t1 VALUES (5.37),(1.1);
CREATE TABLE t2 (d DECIMAL(10,2));
INSERT INTO t2 VALUES ('1.1'),('2.23');
SELECT * FROM v1 WHERE v1.d IN ( SELECT MIN(d) FROM t2 WHERE 0 );
d
DROP VIEW v1;
DROP TABLE t1,t2;
#
# MDEV-11820: second execution of PS for query
#             with false subquery predicate in WHERE
#
CREATE TABLE t1 (c VARCHAR(3)) ENGINE=MyISAM;
INSERT INTO t1 VALUES ('foo'),('bar');
CREATE ALGORITHM=TEMPTABLE VIEW v1 AS SELECT * FROM t1;
CREATE TABLE t2 (a INT);
INSERT INTO t2 VALUES (3), (4);
PREPARE stmt1 FROM
" SELECT * FROM v1 WHERE 1 IN (SELECT a FROM t2) OR c = 'foo'";
PREPARE stmt2 FROM
"EXPLAIN FORMAT=JSON
 SELECT * FROM v1 WHERE 1 IN (SELECT a FROM t2) OR c = 'foo'";
EXECUTE stmt1;
c
foo
EXECUTE stmt2;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<derived3>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v1.c = 'foo'",
          "materialized": {
            "query_block": {
              "select_id": 3,
              "cost": "COST_REPLACED",
              "nested_loop": [
                {
                  "table": {
                    "table_name": "t1",
                    "access_type": "ALL",
                    "loops": 1,
                    "rows": 2,
                    "cost": "COST_REPLACED",
                    "filtered": 100,
                    "attached_condition": "t1.c = 'foo'"
                  }
                }
              ]
            }
          }
        }
      }
    ],
    "subqueries": [
      {
        "query_block": {
          "select_id": 2,
          "cost": "COST_REPLACED",
          "nested_loop": [
            {
              "table": {
                "table_name": "t2",
                "access_type": "ALL",
                "loops": 1,
                "rows": 2,
                "cost": "COST_REPLACED",
                "filtered": 100,
                "attached_condition": "1 = t2.a"
              }
            }
          ]
        }
      }
    ]
  }
}
INSERT INTO t2 SELECT a+1 FROM t2;
INSERT INTO t2 SELECT a+1 FROM t2;
INSERT INTO t2 SELECT a+1 FROM t2;
INSERT INTO t2 SELECT a+1 FROM t2;
INSERT INTO t2 SELECT a+1 FROM t2;
INSERT INTO t2 SELECT a+1 FROM t2;
EXECUTE stmt1;
c
foo
EXECUTE stmt2;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<derived3>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "<cache>(<in_optimizer>(1,<exists>(subquery#2))) or v1.c = 'foo'",
          "materialized": {
            "query_block": {
              "select_id": 3,
              "cost": "COST_REPLACED",
              "nested_loop": [
                {
                  "table": {
                    "table_name": "t1",
                    "access_type": "ALL",
                    "loops": 1,
                    "rows": 2,
                    "cost": "COST_REPLACED",
                    "filtered": 100
                  }
                }
              ]
            }
          }
        }
      }
    ],
    "subqueries": [
      {
        "query_block": {
          "select_id": 2,
          "cost": "COST_REPLACED",
          "nested_loop": [
            {
              "table": {
                "table_name": "t2",
                "access_type": "ALL",
                "loops": 1,
                "rows": 128,
                "cost": "COST_REPLACED",
                "filtered": 100,
                "attached_condition": "1 = t2.a"
              }
            }
          ]
        }
      }
    ]
  }
}
DEALLOCATE PREPARE stmt1;
DEALLOCATE PREPARE stmt2;
DROP VIEW v1;
DROP TABLE t1,t2;
#
# MDEV-12373: pushdown into derived with side effects is prohibited
#
CREATE TABLE sales_documents (
id           int NOT NULL AUTO_INCREMENT,
sale_id      int NULL DEFAULT NULL,
type         tinyint unsigned NULL DEFAULT NULL,
data         text NULL DEFAULT NULL COLLATE 'utf8_unicode_ci',
date         date NULL DEFAULT NULL,
order_number int unsigned NULL DEFAULT NULL,
created_at   int NULL DEFAULT NULL,
updated_at   int NULL DEFAULT NULL,
generated    tinyint NOT NULL DEFAULT '0',
synced_at    int NOT NULL DEFAULT '0',
sum          decimal(13,2) NOT NULL DEFAULT '0',
PRIMARY KEY (id)
);
INSERT INTO sales_documents
(id, sale_id, type, order_number, data, created_at,
updated_at, date, generated, synced_at, sum)
VALUES
(555, 165, 3, 5, '{}', 1486538300, 1486722835, '2017-02-17', 0, 1486538313, 2320.00),
(556, 165, 2, 3, '{}', 1486538304, 1486563125, '2017-02-08', 1, 1486538302, 2320.00),
(557, 158, 2, 2, '{}', 1486538661, 1486538661, '2017-02-08', 0, 1486538660, 2320.00),
(558, 171, 1, 3, '{}', 1486539104, 1488203405, '2017-02-08', 1, 1486539102, 23230.00),
(559, 171, 2, 5, '{}', 1486549233, 1487146010, '2017-02-08', 1, 1486549225, 37690.00),
(560, 172, 1, 1, '{}', 1486658260, 1488203409, '2017-02-09', 1, 1486658256, 40312.00),
(561, 172, 2, 1, '{}', 1486711997, 1486711997, '2017-02-10', 1, 1486711996, 40312.00),
(562, 172, 3, 1, '{}', 1486712104, 1486721395, '2017-02-10', 1, 1486712101, 40312.00),
(563, 171, 3, 2, '{}', 1486712953, 1486720244, '2017-02-10', 1, 1486712910, 23230.00),
(564, 170, 1, 2, '{}', 1486715948, 1488203410, '2017-02-10', 1, 1486715930, 28873.00),
(565, 170, 3, 3, '{}', 1486716782, 1486717426, '2017-02-10', 1, 1486716779, 61948.00),
(566, 166, 3, 4, '{}', 1486720947, 1486720947, '2017-02-10', 1, 1486720945, 4640.00),
(567, 167, 3, 5, '{}', 1486722741, 1486722783, '2017-02-26', 0, 1486722738, 14755.00),
(568, 165, 1, 4, '{}', 1486722849, 1486722849, '2017-02-10', 0, 1486722846, 2320.00),
(569, 173, 2, 2, '{}', 1486723073, 1487071275, '2017-02-10', 1, 1486723071, 14282.00),
(570, 173, 1, 4, '{}', 1486723100, 1488203412, '2017-02-10', 1, 1486723099, 14282.00),
(571, 167, 2, 4, '{}', 1486730859, 1486730859, '2017-02-10', 1, 1486730856, 18655.00),
(572, 167, 1, 5, '{}', 1486730883, 1488203412, '2017-02-10', 1, 1486730877, 18655.00),
(573, 174, 2, 51, '{}', 1486731622, 1487060259, '2017-02-10', 1, 1486731620, 7140.00),
(574, 174, 3, 5, '{}', 1486993472, 1486993472, '2017-02-13', 1, 1488216147, 28020.00),
(575, 174, 1, 6, '{}', 1486993530, 1488203412, '2017-02-13', 1, 1486993505, 7140.00),
(576, 173, 3, 6, '{}', 1487071425, 1487071425, '2017-02-14', 0, 1487071422, 14282.00),
(577, 178, 2, 6, '{}', 1487327372, 1487327372, '2017-02-17', 1, 1487327370, 12321.00),
(578, 177, 2, 7, '{}', 1487327394, 1487327394, '2017-02-17', 0, 1487327391, 4270.00),
(579, 182, 3, 6, '{}', 1487750589, 1487751693, '2017-02-22', 1, 1487751688, 4270.00),
(580, 182, 2, 7, '{}', 1487750601, 1487750663, '2017-02-22', 1, 1487750598, 4270.00),
(581, 182, 1, 7, '{}', 1487750694, 1488203412, '2017-02-22', 1, 1487750692, 4270.00),
(582, 185, 3, 7, '{}', 1487774051, 1487774051, '2017-02-22', 0, 1487774043, 8913.00),
(583, 184, 3, 7, '{}', 1487774071, 1487774235, '2017-02-22', 0, 1487774093, 3285.00),
(584, 184, 2, 8, '{}', 1487774074, 1487774074, '2017-02-22', 0, 1487774073, 3285.00),
(585, 184, 1, 8, '{}', 1487774081, 1487774081, '2017-02-22', 0, 1487774075, 3285.00),
(586, 193, 2, 8, '{}', 1487955294, 1487955318, '2017-02-24', 0, 1487955311, 4270.00),
(587, 193, 1, 8, '{}', 1487955324, 1487955324, '2017-02-24', 0, 1487955320, 4270.00),
(588, 193, 3, 7, '{}', 1487955341, 1487955341, '2017-02-24', 0, 1487955325, 4270.00),
(589, 186, 1, 8, '{}', 1487957291, 1487957464, '2017-02-24', 0, 1487957459, 6960.00),
(590, 186, 2, 8, '{}', 1487957308, 1487957468, '2017-02-24', 0, 1487957465, 6960.00),
(591, 186, 3, 7, '{}', 1487957312, 1487957473, '2017-02-24', 0, 1487957469, 6960.00),
(592, 194, 1, 8, '{}', 1488193293, 1488203412, '2017-02-27', 1, 1488193280, 2320.00),
(593, 194, 2, 8, '{}', 1488193304, 1488193304, '2017-02-27', 1, 1488193303, 2320.00),
(594, 210, 1, 9, '{}', 1488198896, 1488198896, '2017-02-27', 0, 1488198885, 4270.00),
(595, 210, 2, 12, '{}', 1488198901, 1488198901, '2017-02-27', 1, 1488532585, 4270.00),
(596, 210, 3, 10, '{}', 1488198904, 1488198904, '2017-02-27', 1, 1488532565, 4270.00),
(597, 209, 2, 9, '{}', 1488200016, 1488450772, '2017-02-27', 1, 1488450449, 4270.00),
(598, 209, 1, 9, '{}', 1488200020, 1488200063, '2017-02-27', 1, 1488200017, 4271.00),
(599, 209, 3, 7, '{}', 1488200053, 1488200053, '2017-02-27', 0, 1488200021, 4271.00),
(600, 211, 2, 10, '{}', 1488216265, 1489402027, '2017-02-27', 1, 1488216264, 2320.00),
(601, 211, 3, 7, '{}', 1488216281, 1488216281, '2017-02-27', 1, 1488216276, 2320.00),
(602, 211, 1, 10, '{}', 1488216283, 1488216283, '2017-02-27', 1, 1488216282, 2320.00),
(603, 198, 2, 11, '{}', 1488280125, 1488280125, '2017-02-28', 0, 1488280095, 4270.00),
(604, 198, 1, 11, '{}', 1488280160, 1488280160, '2017-02-28', 0, 1488280126, 4270.00),
(605, 198, 3, 8, '{}', 1488280440, 1488280440, '2017-02-28', 0, 1488280435, 4270.00),
(606, 212, 1, 12, '{}', 1488286301, 1489402168, '2017-02-28', 1, 1488286295, 13825.00),
(607, 212, 3, 8, '{}', 1488289644, 1488289690, '2017-02-28', 1, 1488289642, 25295.00),
(608, 212, 2, 13, '{}', 1488290350, 1488290431, '2017-02-28', 1, 1488290347, 13133.75),
(609, 213, 1, 11, '{}', 1488529470, 1488529470, '2017-03-03', 1, 1488529461, 5660.00),
(610, 213, 2, 11, '{}', 1488529484, 1488529484, '2017-03-03', 1, 1488529479, 5660.00),
(611, 213, 3, 9, '{}', 1488529493, 1488529493, '2017-03-03', 1, 1488529489, 5660.00),
(612, 197, 2, 13, '{}', 1489400715, 1489400715, '2017-03-13', 0, 1489398959, 4270.00),
(613, 219, 3, 11, '{}', 1490084337, 1490181958, '2017-03-21', 1, 1490084334, 73526.00),
(614, 216, 3, 11, '{}', 1490085757, 1490086717, '2017-03-21', 0, 1490085755, 5377.00);
SELECT * FROM
(SELECT @row := @row + 1 as row, a.* from (
SELECT t.order_number
FROM sales_documents t
WHERE
t.type = 2 AND
t.date >= '2017-01-01' AND
t.date <= '2017-12-31' AND
t.order_number IS NOT NULL AND
t.generated = 1
GROUP BY t.order_number
) a, (SELECT @row := 0) r) t
WHERE row <> order_number;
row	order_number
14	51
DROP TABLE sales_documents;
#
# MDEV-12845: pushdown from merged derived using equalities
#
create table t1 (a int);
insert into t1 values
(4), (8), (5), (3), (10), (2), (7);
create table t2 (b int, c int);
insert into t2 values
(2,1), (5,2), (2,2), (4,1), (4,3),
(5,3), (2,4), (4,6), (2,1);
create view v1 as
select b, sum(c) as s from t2 group by b;
create view v2 as
select distinct b, c from t2;
create view v3 as
select b, max(c) as m from t2 group by b;
select b
from ( select t1.a, v1.b, v1.s from t1, v1 where t1.a = v1.b ) as t
where b > 2;
b
4
5
explain format=json select b
from ( select t1.a, v1.b, v1.s from t1, v1 where t1.a = v1.b ) as t
where b > 2;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t1",
          "access_type": "ALL",
          "loops": 1,
          "rows": 7,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t1.a > 2 and t1.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived3>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "5",
          "used_key_parts": ["b"],
          "ref": ["test.t1.a"],
          "loops": 7,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "materialized": {
            "query_block": {
              "select_id": 3,
              "cost": "COST_REPLACED",
              "filesort": {
                "sort_key": "t2.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t2",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 9,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t2.b > 2"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
select a
from ( select t1.a, v1.b, v1.s from t1, v1 where t1.a = v1.b ) as t
where a > 2;
a
4
5
explain format=json select a
from ( select t1.a, v1.b, v1.s from t1, v1 where t1.a = v1.b ) as t
where a > 2;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t1",
          "access_type": "ALL",
          "loops": 1,
          "rows": 7,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t1.a > 2 and t1.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived3>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "5",
          "used_key_parts": ["b"],
          "ref": ["test.t1.a"],
          "loops": 7,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "materialized": {
            "query_block": {
              "select_id": 3,
              "cost": "COST_REPLACED",
              "filesort": {
                "sort_key": "t2.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t2",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 9,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t2.b > 2"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
select a
from  ( select t1.a, v2.b, v2.c from t1, v2 where t1.a = v2.b ) as t
where a > 2;
a
4
4
4
5
5
explain format=json select a
from  ( select t1.a, v2.b, v2.c from t1, v2 where t1.a = v2.b ) as t
where a > 2;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t1",
          "access_type": "ALL",
          "loops": 1,
          "rows": 7,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t1.a > 2 and t1.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived3>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "5",
          "used_key_parts": ["b"],
          "ref": ["test.t1.a"],
          "loops": 7,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "materialized": {
            "query_block": {
              "select_id": 3,
              "cost": "COST_REPLACED",
              "temporary_table": {
                "nested_loop": [
                  {
                    "table": {
                      "table_name": "t2",
                      "access_type": "ALL",
                      "loops": 1,
                      "rows": 9,
                      "cost": "COST_REPLACED",
                      "filtered": 100,
                      "attached_condition": "t2.b > 2"
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
select a
from  ( select t1.a, v3.b, v3.m from t1, v3 where t1.a = v3.m ) as t
where a > 2;
a
4
3
explain format=json select a
from  ( select t1.a, v3.b, v3.m from t1, v3 where t1.a = v3.m ) as t
where a > 2;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t1",
          "access_type": "ALL",
          "loops": 1,
          "rows": 7,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t1.a > 2 and t1.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived3>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "5",
          "used_key_parts": ["m"],
          "ref": ["test.t1.a"],
          "loops": 7,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "materialized": {
            "query_block": {
              "select_id": 3,
              "cost": "COST_REPLACED",
              "having_condition": "m > 2",
              "filesort": {
                "sort_key": "t2.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t2",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 9,
                        "cost": "COST_REPLACED",
                        "filtered": 100
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
drop view v1,v2,v3;
drop table t1,t2;
#
# MDEV-13166: pushdown from merged derived
#
CREATE TABLE t1 (i int) ENGINE=MyISAM;
INSERT INTO t1 VALUES (1),(2);
CREATE VIEW v1 AS SELECT MAX(i) AS f FROM t1;
SELECT * FROM ( SELECT * FROM v1 ) AS sq WHERE f > 0;
f
2
explain format=json SELECT * FROM ( SELECT * FROM v1 ) AS sq WHERE f > 0;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<derived3>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v1.f > 0",
          "materialized": {
            "query_block": {
              "select_id": 3,
              "cost": "COST_REPLACED",
              "having_condition": "f > 0",
              "nested_loop": [
                {
                  "table": {
                    "table_name": "t1",
                    "access_type": "ALL",
                    "loops": 1,
                    "rows": 2,
                    "cost": "COST_REPLACED",
                    "filtered": 100
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}
DROP VIEW v1;
DROP TABLE t1;
#
# MDEV-13193: pushdown of equality extracted from multiple equality
#
CREATE TABLE t1 (i1 int, KEY(i1)) ENGINE=MyISAM;
INSERT INTO t1 VALUES (1),(2);
CREATE TABLE t2 (i2 int) ENGINE=MyISAM;
INSERT INTO t2 VALUES (2),(4);
CREATE ALGORITHM=TEMPTABLE VIEW v2 AS SELECT * FROM t2;
SELECT * FROM t1, ( SELECT * FROM v2 ) AS sq
WHERE i1 = 1 AND ( i1 = i2 OR i1 = 2 );
i1	i2
explain format=json SELECT * FROM t1, ( SELECT * FROM v2 ) AS sq
WHERE i1 = 1 AND ( i1 = i2 OR i1 = 2 );
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t1",
          "access_type": "ref",
          "possible_keys": ["i1"],
          "key": "i1",
          "key_length": "5",
          "used_key_parts": ["i1"],
          "ref": ["const"],
          "loops": 1,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "using_index": true
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived3>",
            "access_type": "ALL",
            "loops": 1,
            "rows": 2,
            "cost": "COST_REPLACED",
            "filtered": 100,
            "attached_condition": "v2.i2 = 1"
          },
          "buffer_type": "flat",
          "buffer_size": "65",
          "join_type": "BNL",
          "materialized": {
            "query_block": {
              "select_id": 3,
              "cost": "COST_REPLACED",
              "nested_loop": [
                {
                  "table": {
                    "table_name": "t2",
                    "access_type": "ALL",
                    "loops": 1,
                    "rows": 2,
                    "cost": "COST_REPLACED",
                    "filtered": 100,
                    "attached_condition": "t2.i2 = 1"
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}
DROP VIEW v2;
DROP TABLE t1,t2;
#
# MDEV-14237: derived with regexp_substr() in select list
#
create table t1 (a char(8));
insert into t1 values ('b'), ('a'), ('xx');
select *
from ( select distinct regexp_substr(t1.a,'^[A-Za-z]+') as f from t1) as t
where t.f = 'a' or t.f = 'b';
f
b
a
explain format=json select *
from ( select distinct regexp_substr(t1.a,'^[A-Za-z]+') as f from t1) as t
where t.f = 'a' or t.f = 'b';
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 3,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t.f = 'a' or t.f = 'b'",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "temporary_table": {
                "nested_loop": [
                  {
                    "table": {
                      "table_name": "t1",
                      "access_type": "ALL",
                      "loops": 1,
                      "rows": 3,
                      "cost": "COST_REPLACED",
                      "filtered": 100
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
drop table t1;
#
# MDEV-13454: consequence of mdev-14368 fixed for 5.5
#
SET  sql_mode = 'ONLY_FULL_GROUP_BY';
create table t1 (id int, id2 int);
insert into t1 values (1,1),(2,3),(3,4),(7,2);
create table t2(id2 int);
insert  t2 values (1),(2),(3);
SELECT * FROM t1
LEFT OUTER JOIN
(SELECT id2, COUNT(*) as ct FROM t2 GROUP BY id2) vc USING (id2)
WHERE (vc.ct>0);
id2	id	ct
1	1	1
3	2	1
2	7	1
EXPLAIN FORMAT=JSON SELECT * FROM t1
LEFT OUTER JOIN
(SELECT id2, COUNT(*) as ct FROM t2 GROUP BY id2) vc USING (id2)
WHERE (vc.ct>0);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t1",
          "access_type": "ALL",
          "loops": 1,
          "rows": 4,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t1.id2 is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "5",
          "used_key_parts": ["id2"],
          "ref": ["test.t1.id2"],
          "loops": 4,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "vc.ct > 0",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "ct > 0",
              "filesort": {
                "sort_key": "t2.id2",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t2",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 3,
                        "cost": "COST_REPLACED",
                        "filtered": 100
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
DROP TABLE t1,t2;
SET  sql_mode = DEFAULT;
#
# MDEV-15579: incorrect removal of sub-formulas to be pushed
#             into WHERE of materialized derived with GROUP BY
#
CREATE TABLE t1 (a INT, b INT, c INT, d INT);
CREATE TABLE t2 (x INT, y INT, z INT);
INSERT INTO t1 VALUES (1,1,66,1), (1,1,56,2), (3,2,42,3);
INSERT INTO t2 VALUES (1,1,66), (1,12,32);
SELECT *
FROM t2,
(
SELECT a, b, max(c) AS max_c
FROM t1
GROUP BY a
HAVING max_c > 37
) AS v1
WHERE (v1.a=1) AND (v1.b=v1.a) AND
(v1.a=t2.x) AND (v1.max_c>30);
x	y	z	a	b	max_c
1	1	66	1	1	66
1	12	32	1	1	66
EXPLAIN SELECT *
FROM t2,
(
SELECT a, b, max(c) AS max_c
FROM t1
GROUP BY a
HAVING max_c > 37
) AS v1
WHERE (v1.a=1) AND (v1.b=v1.a) AND
(v1.a=t2.x) AND (v1.max_c>30);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	3	Using where; Using join buffer (flat, BNL join)
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	3	Using where
EXPLAIN FORMAT=JSON SELECT *
FROM t2,
(
SELECT a, b, max(c) AS max_c
FROM t1
GROUP BY a
HAVING max_c > 37
) AS v1
WHERE (v1.a=1) AND (v1.b=v1.a) AND
(v1.a=t2.x) AND (v1.max_c>30);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.x = 1"
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived2>",
            "access_type": "ALL",
            "loops": 2,
            "rows": 3,
            "cost": "COST_REPLACED",
            "filtered": 100,
            "attached_condition": "v1.a = 1 and v1.b = 1 and v1.max_c > 30"
          },
          "buffer_type": "flat",
          "buffer_size": "173",
          "join_type": "BNL",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c > 37 and max_c > 30",
              "nested_loop": [
                {
                  "table": {
                    "table_name": "t1",
                    "access_type": "ALL",
                    "loops": 1,
                    "rows": 3,
                    "cost": "COST_REPLACED",
                    "filtered": 100,
                    "attached_condition": "t1.a = 1 and t1.b = 1"
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}
SELECT *
FROM t2,
(
SELECT a, b, d, max(c) AS max_c
FROM t1
GROUP BY a,d
HAVING max_c > 37
) AS v1
WHERE (v1.a=1) AND (v1.b=v1.a) AND (v1.b=v1.d) AND
(v1.a=t2.x) AND (v1.max_c>30);
x	y	z	a	b	d	max_c
1	1	66	1	1	1	66
1	12	32	1	1	1	66
EXPLAIN SELECT *
FROM t2,
(
SELECT a, b, d, max(c) AS max_c
FROM t1
GROUP BY a,d
HAVING max_c > 37
) AS v1
WHERE (v1.a=1) AND (v1.b=v1.a) AND (v1.b=v1.d) AND
(v1.a=t2.x) AND (v1.max_c>30);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	3	Using where; Using join buffer (flat, BNL join)
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	3	Using where
EXPLAIN FORMAT=JSON SELECT *
FROM t2,
(
SELECT a, b, d, max(c) AS max_c
FROM t1
GROUP BY a,d
HAVING max_c > 37
) AS v1
WHERE (v1.a=1) AND (v1.b=v1.a) AND (v1.b=v1.d) AND
(v1.a=t2.x) AND (v1.max_c>30);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.x = 1"
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived2>",
            "access_type": "ALL",
            "loops": 2,
            "rows": 3,
            "cost": "COST_REPLACED",
            "filtered": 100,
            "attached_condition": "v1.a = 1 and v1.b = 1 and v1.d = 1 and v1.max_c > 30"
          },
          "buffer_type": "flat",
          "buffer_size": "173",
          "join_type": "BNL",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "max_c > 37 and max_c > 30",
              "nested_loop": [
                {
                  "table": {
                    "table_name": "t1",
                    "access_type": "ALL",
                    "loops": 1,
                    "rows": 3,
                    "cost": "COST_REPLACED",
                    "filtered": 100,
                    "attached_condition": "t1.a = 1 and t1.b = 1 and t1.d = 1"
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}
DROP TABLE t1,t2;
#
# MDEV-15765: pushing condition with temporal constants
#             into constant tables
#
select * from (select  date('2018-01-01') as d
union all
select  date('2018-01-01') as d) as t
where t.d between date ('2017-01-01') and date ('2019-01-01');
d
2018-01-01
2018-01-01
select * from (select  date('2018-01-01') as d) as t
where t.d between date ('2017-01-01') and date ('2019-01-01');
d
2018-01-01
#
# MDEV-16088: pushdown into derived defined in the IN subquery
#
CREATE TABLE t1 (a INT, b INT);
CREATE TABLE t2 (e INT, f INT, g INT);
INSERT INTO t1 VALUES (1,14),(2,13),(1,19),(2,32),(3,24);
INSERT INTO t2 VALUES (1,19,2),(3,24,1),(1,12,2),(3,11,3),(2,32,1);
SELECT * FROM t1
WHERE (t1.a,t1.b) IN
(
SELECT d_tab.e,d_tab.max_f
FROM (
SELECT t2.e, MAX(t2.f) AS max_f
FROM t2
GROUP BY t2.e
HAVING max_f>18
) as d_tab
WHERE d_tab.e>1
)
;
a	b
2	32
3	24
EXPLAIN SELECT * FROM t1
WHERE (t1.a,t1.b) IN
(
SELECT d_tab.e,d_tab.max_f
FROM (
SELECT t2.e, MAX(t2.f) AS max_f
FROM t2
GROUP BY t2.e
HAVING max_f>18
) as d_tab
WHERE d_tab.e>1
)
;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	5	Using where
1	PRIMARY	<derived3>	eq_ref	distinct_key	distinct_key	10	test.t1.a,test.t1.b	1	
3	DERIVED	t2	ALL	NULL	NULL	NULL	NULL	5	Using where; Using temporary; Using filesort
EXPLAIN FORMAT=JSON SELECT * FROM t1
WHERE (t1.a,t1.b) IN
(
SELECT d_tab.e,d_tab.max_f
FROM (
SELECT t2.e, MAX(t2.f) AS max_f
FROM t2
GROUP BY t2.e
HAVING max_f>18
) as d_tab
WHERE d_tab.e>1
)
;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t1",
          "access_type": "ALL",
          "loops": 1,
          "rows": 5,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t1.a > 1 and t1.a is not null and t1.b is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived3>",
          "access_type": "eq_ref",
          "possible_keys": ["distinct_key"],
          "key": "distinct_key",
          "key_length": "10",
          "used_key_parts": ["e", "max_f"],
          "ref": ["test.t1.a", "test.t1.b"],
          "loops": 5,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "materialized": {
            "query_block": {
              "select_id": 3,
              "cost": "COST_REPLACED",
              "having_condition": "max_f > 18",
              "filesort": {
                "sort_key": "t2.e",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t2",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 5,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t2.e > 1"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
SELECT * FROM t1
WHERE (t1.a,t1.b) IN
(
SELECT d_tab.e,d_tab.max_f
FROM (
SELECT t2.e, MAX(t2.f) AS max_f
FROM t2
GROUP BY t2.e
HAVING max_f>18
) as d_tab
WHERE d_tab.max_f<25
)
;
a	b
1	19
3	24
EXPLAIN SELECT * FROM t1
WHERE (t1.a,t1.b) IN
(
SELECT d_tab.e,d_tab.max_f
FROM (
SELECT t2.e, MAX(t2.f) AS max_f
FROM t2
GROUP BY t2.e
HAVING max_f>18
) as d_tab
WHERE d_tab.max_f<25
)
;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	5	Using where
1	PRIMARY	<derived3>	eq_ref	distinct_key	distinct_key	10	test.t1.a,test.t1.b	1	
3	DERIVED	t2	ALL	NULL	NULL	NULL	NULL	5	Using temporary; Using filesort
EXPLAIN FORMAT=JSON SELECT * FROM t1
WHERE (t1.a,t1.b) IN
(
SELECT d_tab.e,d_tab.max_f
FROM (
SELECT t2.e, MAX(t2.f) AS max_f
FROM t2
GROUP BY t2.e
HAVING max_f>18
) as d_tab
WHERE d_tab.max_f<25
)
;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t1",
          "access_type": "ALL",
          "loops": 1,
          "rows": 5,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t1.b < 25 and t1.a is not null and t1.b is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived3>",
          "access_type": "eq_ref",
          "possible_keys": ["distinct_key"],
          "key": "distinct_key",
          "key_length": "10",
          "used_key_parts": ["e", "max_f"],
          "ref": ["test.t1.a", "test.t1.b"],
          "loops": 5,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "materialized": {
            "query_block": {
              "select_id": 3,
              "cost": "COST_REPLACED",
              "having_condition": "max_f > 18 and max_f < 25",
              "filesort": {
                "sort_key": "t2.e",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t2",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 5,
                        "cost": "COST_REPLACED",
                        "filtered": 100
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
SELECT * FROM t1
WHERE (t1.a,t1.b) IN
(
SELECT d_tab.e, MAX(d_tab.max_f) AS max_f
FROM (
SELECT t2.e, MAX(t2.f) as max_f, t2.g
FROM t2
GROUP BY t2.e
) as d_tab
WHERE d_tab.e>1
GROUP BY d_tab.g
)
;
a	b
2	32
EXPLAIN SELECT * FROM t1
WHERE (t1.a,t1.b) IN
(
SELECT d_tab.e, MAX(d_tab.max_f) AS max_f
FROM (
SELECT t2.e, MAX(t2.f) as max_f, t2.g
FROM t2
GROUP BY t2.e
) as d_tab
WHERE d_tab.e>1
GROUP BY d_tab.g
)
;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	5	Using where
1	PRIMARY	<subquery2>	eq_ref	distinct_key	distinct_key	8	test.t1.a,test.t1.b	1	
2	MATERIALIZED	<derived3>	ALL	NULL	NULL	NULL	NULL	5	Using where; Using temporary
3	DERIVED	t2	ALL	NULL	NULL	NULL	NULL	5	Using where; Using temporary; Using filesort
EXPLAIN FORMAT=JSON SELECT * FROM t1
WHERE (t1.a,t1.b) IN
(
SELECT d_tab.e, MAX(d_tab.max_f) AS max_f
FROM (
SELECT t2.e, MAX(t2.f) as max_f, t2.g
FROM t2
GROUP BY t2.e
) as d_tab
WHERE d_tab.e>1
GROUP BY d_tab.g
)
;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t1",
          "access_type": "ALL",
          "loops": 1,
          "rows": 5,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t1.a is not null and t1.b is not null"
        }
      },
      {
        "table": {
          "table_name": "<subquery2>",
          "access_type": "eq_ref",
          "possible_keys": ["distinct_key"],
          "key": "distinct_key",
          "key_length": "8",
          "used_key_parts": ["e", "max_f"],
          "ref": ["test.t1.a", "test.t1.b"],
          "loops": 5,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "materialized": {
            "unique": 1,
            "materialization": {
              "query_block": {
                "select_id": 2,
                "cost": "COST_REPLACED",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "<derived3>",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 5,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "d_tab.e > 1",
                        "materialized": {
                          "query_block": {
                            "select_id": 3,
                            "cost": "COST_REPLACED",
                            "filesort": {
                              "sort_key": "t2.e",
                              "temporary_table": {
                                "nested_loop": [
                                  {
                                    "table": {
                                      "table_name": "t2",
                                      "access_type": "ALL",
                                      "loops": 1,
                                      "rows": 5,
                                      "cost": "COST_REPLACED",
                                      "filtered": 100,
                                      "attached_condition": "t2.e > 1"
                                    }
                                  }
                                ]
                              }
                            }
                          }
                        }
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
SELECT * FROM t1
WHERE (t1.a,t1.b) IN
(
SELECT d_tab.e, MAX(d_tab.max_f) AS max_f
FROM (
SELECT t2.e, MAX(t2.f) as max_f, t2.g
FROM t2
GROUP BY t2.e
) as d_tab
WHERE d_tab.max_f>20
GROUP BY d_tab.g
)
;
a	b
2	32
EXPLAIN SELECT * FROM t1
WHERE (t1.a,t1.b) IN
(
SELECT d_tab.e, MAX(d_tab.max_f) AS max_f
FROM (
SELECT t2.e, MAX(t2.f) as max_f, t2.g
FROM t2
GROUP BY t2.e
) as d_tab
WHERE d_tab.max_f>20
GROUP BY d_tab.g
)
;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	5	Using where
1	PRIMARY	<subquery2>	eq_ref	distinct_key	distinct_key	8	test.t1.a,test.t1.b	1	
2	MATERIALIZED	<derived3>	ALL	NULL	NULL	NULL	NULL	5	Using where; Using temporary
3	DERIVED	t2	ALL	NULL	NULL	NULL	NULL	5	Using temporary; Using filesort
EXPLAIN FORMAT=JSON SELECT * FROM t1
WHERE (t1.a,t1.b) IN
(
SELECT d_tab.e, MAX(d_tab.max_f) AS max_f
FROM (
SELECT t2.e, MAX(t2.f) as max_f, t2.g
FROM t2
GROUP BY t2.e
) as d_tab
WHERE d_tab.max_f>20
GROUP BY d_tab.g
)
;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t1",
          "access_type": "ALL",
          "loops": 1,
          "rows": 5,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t1.a is not null and t1.b is not null"
        }
      },
      {
        "table": {
          "table_name": "<subquery2>",
          "access_type": "eq_ref",
          "possible_keys": ["distinct_key"],
          "key": "distinct_key",
          "key_length": "8",
          "used_key_parts": ["e", "max_f"],
          "ref": ["test.t1.a", "test.t1.b"],
          "loops": 5,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "materialized": {
            "unique": 1,
            "materialization": {
              "query_block": {
                "select_id": 2,
                "cost": "COST_REPLACED",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "<derived3>",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 5,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "d_tab.max_f > 20",
                        "materialized": {
                          "query_block": {
                            "select_id": 3,
                            "cost": "COST_REPLACED",
                            "having_condition": "max_f > 20",
                            "filesort": {
                              "sort_key": "t2.e",
                              "temporary_table": {
                                "nested_loop": [
                                  {
                                    "table": {
                                      "table_name": "t2",
                                      "access_type": "ALL",
                                      "loops": 1,
                                      "rows": 5,
                                      "cost": "COST_REPLACED",
                                      "filtered": 100
                                    }
                                  }
                                ]
                              }
                            }
                          }
                        }
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
DROP TABLE t1,t2;
#
# MDEV-15765: pushing condition with IN subquery defined with constants
#             using substitution
#
CREATE TABLE t1 (a INT);
INSERT INTO t1 VALUES (1),(2);
SELECT * FROM
(
SELECT DISTINCT * FROM t1
) der_tab
WHERE (a>0 AND a<2 OR a IN (2,3)) AND
(a=2 OR 0);
a
2
DROP TABLE t1;
#
# MDEV-16386: pushing condition into the HAVING clause when ambiguous
#             fields warning appears
#
CREATE TABLE t1 (a INT, b INT);
INSERT INTO t1 VALUES (1,2),(2,3),(3,4);
SELECT * FROM
(
SELECT t1.b AS a
FROM t1
GROUP BY t1.a
) dt
WHERE (dt.a=2);
a
2
EXPLAIN FORMAT=JSON SELECT * FROM
(
SELECT t1.b AS a
FROM t1
GROUP BY t1.a
) dt
WHERE (dt.a=2);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 3,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "dt.a = 2",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "a = 2",
              "filesort": {
                "sort_key": "t1.a",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 3,
                        "cost": "COST_REPLACED",
                        "filtered": 100
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
SELECT * FROM
(
SELECT t1.b AS a
FROM t1
GROUP BY t1.a
HAVING (t1.a<3)
) dt
WHERE (dt.a>1);
a
2
3
EXPLAIN FORMAT=JSON SELECT * FROM
(
SELECT t1.b AS a
FROM t1
GROUP BY t1.a
HAVING (t1.a<3)
) dt
WHERE (dt.a>1);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 3,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "dt.a > 1",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "a > 1",
              "filesort": {
                "sort_key": "t1.a",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 3,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.a < 3"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
SELECT * FROM
(
SELECT 'ab' AS a
FROM t1
GROUP BY t1.a
) dt
WHERE (dt.a='ab');
a
ab
ab
ab
EXPLAIN FORMAT=JSON SELECT * FROM
(
SELECT 'ab' AS a
FROM t1
GROUP BY t1.a
) dt
WHERE (dt.a='ab');
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 3,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "dt.a = 'ab'",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "filesort": {
                "sort_key": "t1.a",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 3,
                        "cost": "COST_REPLACED",
                        "filtered": 100
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
SELECT * FROM
(
SELECT 1 AS a
FROM t1
GROUP BY t1.a
) dt
WHERE (dt.a=1);
a
1
1
1
EXPLAIN FORMAT=JSON SELECT * FROM
(
SELECT 1 AS a
FROM t1
GROUP BY t1.a
) dt
WHERE (dt.a=1);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 3,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "dt.a = 1",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "filesort": {
                "sort_key": "t1.a",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 3,
                        "cost": "COST_REPLACED",
                        "filtered": 100
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
DROP TABLE t1;
#
# MDEV-16517: pushdown condition with the IN predicate defined
#             with non-constant values
#
CREATE TABLE t1 (a INT, b INT);
INSERT INTO t1 VALUES (1,2),(1,3);
SELECT * FROM
(
SELECT t1.a
FROM t1
WHERE 1 IN (0,t1.a)
GROUP BY t1.a
) AS dt1
JOIN
(
SELECT t1.a
FROM t1
WHERE 1 IN (0,t1.a)
) AS dt2
ON dt1.a = dt2.a;
a	a
1	1
1	1
EXPLAIN FORMAT=JSON SELECT * FROM
(
SELECT t1.a
FROM t1
WHERE 1 IN (0,t1.a)
GROUP BY t1.a
) AS dt1
JOIN
(
SELECT t1.a
FROM t1
WHERE 1 IN (0,t1.a)
) AS dt2
ON dt1.a = dt2.a;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t1",
          "access_type": "ALL",
          "loops": 1,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "1 in (0,t1.a) and t1.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t1.a"],
          "loops": 2,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "filesort": {
                "sort_key": "t1.a",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 2,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "1 in (0,t1.a) and 1 in (0,t1.a)"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
SELECT * FROM
(
SELECT t1.a,MAX(t1.b)
FROM t1
GROUP BY t1.a
) AS dt, t1
WHERE dt.a=t1.a AND dt.a IN (1,t1.a);
a	MAX(t1.b)	a	b
1	3	1	2
1	3	1	3
EXPLAIN FORMAT=JSON SELECT * FROM
(
SELECT t1.a,MAX(t1.b)
FROM t1
GROUP BY t1.a
) AS dt, t1
WHERE dt.a=t1.a AND dt.a IN (1,t1.a);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t1",
          "access_type": "ALL",
          "loops": 1,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t1.a in (1,t1.a) and t1.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t1.a"],
          "loops": 2,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "filesort": {
                "sort_key": "t1.a",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 2,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.a in (1,t1.a)"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
DROP TABLE t1;
#
# MDEV-15087: error from inexpensive subquery before check
#                   for condition pushdown into derived
#
CREATE TABLE t1 (i1 int, v1 varchar(1));
INSERT INTO t1 VALUES (7,'x');
CREATE TABLE t2 (i1 int);
INSERT INTO t2 VALUES (8);
CREATE TABLE t3 (i1 int ,v1 varchar(1), v2 varchar(1));
INSERT INTO t3 VALUES (4, 'v','v'),(62,'v','k'),(7, 'n', NULL);
SELECT 1
FROM (t1 AS a1
JOIN (((SELECT  DISTINCT t3.*
FROM t3) AS a2
JOIN t1 ON (t1.v1 = a2.v2))) ON (t1.v1 = a2.v1))
WHERE (SELECT  BIT_COUNT(t2.i1)
FROM (t2 JOIN t3)) IS NULL;
ERROR 21000: Subquery returns more than 1 row
DROP TABLE t1, t2, t3;
#
# MDEV-16614 signal 7 after calling stored procedure, that uses regexp
#
CREATE PROCEDURE p1(m1 varchar(5), m2 varchar(5))
BEGIN
SELECT a FROM
(SELECT "aa" a) t
JOIN (SELECT "aa" b) t1 on t.a=t1.b
WHERE t.a regexp m1 and t1.b regexp m2
GROUP BY a;
END$$
CALL p1('a','a');
a
aa
DROP PROCEDURE p1;
CREATE PROCEDURE p1(m1 varchar(5))
BEGIN
SELECT a FROM (SELECT "aa" a) t WHERE t.a regexp m1;
END$$
CALL p1('a');
a
aa
DROP PROCEDURE p1;
SELECT a FROM (SELECT "aa" a) t WHERE REGEXP_INSTR(t.a, (SELECT MAX('aa') FROM DUAL LIMIT 1));
a
aa
CREATE OR REPLACE FUNCTION f1(a VARCHAR(10), b VARCHAR(10)) RETURNS INT
BEGIN
RETURN 1;
END;$$
CREATE OR REPLACE PROCEDURE p1(m1 varchar(5))
BEGIN
SELECT a FROM (SELECT "aa" a) t WHERE f1(t.a, m1);
END$$
CALL p1('a');
a
aa
DROP PROCEDURE p1;
DROP FUNCTION f1;
CREATE OR REPLACE FUNCTION f1(a VARCHAR(10), b VARCHAR(10)) RETURNS INT
BEGIN
RETURN 1;
END;$$
SELECT a FROM (SELECT "aa" a) t WHERE f1(t.a, (SELECT MAX('aa') FROM DUAL LIMIT 1));
a
aa
DROP FUNCTION f1;
#
# MDEV-17011: condition pushdown into materialized derived used
#             in INSERT SELECT, multi-table UPDATE and DELETE
#
CREATE TABLE t1 (a int ,b int) ENGINE=MyISAM;
INSERT INTO t1 VALUES
(1, 1), (1, 2), (2, 1), (2, 2), (3,1), (3,3), (4,2);
CREATE TABLE t2 (a int) ENGINE MYISAM;
INSERT INTO t2 VALUES
(3), (7), (1), (4), (1);
CREATE TABLE t3 (a int, b int) ENGINE MYISAM;
EXPLAIN FORMAT=JSON INSERT INTO t3
SELECT * FROM (SELECT a, count(*) as c FROM t1 GROUP BY a) t WHERE a<=2;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 7,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t.a <= 2",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "filesort": {
                "sort_key": "t1.a",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 7,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.a <= 2"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
INSERT INTO t3
SELECT * FROM (SELECT a, count(*) as c FROM t1 GROUP BY a) t WHERE a<=2;
SELECT * FROM t3;
a	b
1	2
2	2
EXPLAIN FORMAT=JSON UPDATE t2, (SELECT a, count(*) as c FROM t1 GROUP BY a) t SET t2.a=t.c+10
WHERE t2.a= t.c and t.a>=3;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 5,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "8",
          "used_key_parts": ["c"],
          "ref": ["test.t2.a"],
          "loops": 5,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a = t.c and t.a >= 3",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "filesort": {
                "sort_key": "t1.a",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 7,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.a >= 3"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
UPDATE t2, (SELECT a, count(*) as c FROM t1 GROUP BY a) t SET t2.a=t.c+10
WHERE t2.a= t.c and t.a>=3;
SELECT * FROM t2;
a
3
7
11
4
11
EXPLAIN FORMAT=JSON DELETE t2 FROM t2, (SELECT a, count(*) as c FROM t1 GROUP BY a) t
WHERE t2.a= t.c+9 and t.a=2;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 5,
          "cost": "COST_REPLACED",
          "filtered": 100
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ALL",
          "loops": 5,
          "rows": 7,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t.a = 2 and t2.a = t.c + 9",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "nested_loop": [
                {
                  "table": {
                    "table_name": "t1",
                    "access_type": "ALL",
                    "loops": 1,
                    "rows": 7,
                    "cost": "COST_REPLACED",
                    "filtered": 100,
                    "attached_condition": "t1.a = 2"
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}
DELETE t2 FROM t2, (SELECT a, count(*) as c FROM t1 GROUP BY a) t
WHERE t2.a= t.c+9 and t.a=2;
SELECT * FROM t2;
a
3
7
4
DROP TABLE t1,t2,t3;
#
# MDEV-16765: pushdown condition with the CASE structure
#             defined with Item_cond item
#
CREATE TABLE t1(a INT, b INT);
INSERT INTO t1 VALUES (1,2), (3,4), (2,3);
SELECT *
FROM
(
SELECT CASE WHEN ((tab2.max_a=1) OR (tab2.max_a=2))
THEN 1 ELSE 0 END AS max_a,b
FROM (SELECT MAX(a) as max_a,b FROM t1 GROUP BY t1.b) AS tab2
) AS tab1
WHERE (tab1.max_a=1);
max_a	b
1	2
1	3
EXPLAIN FORMAT=JSON SELECT *
FROM
(
SELECT CASE WHEN ((tab2.max_a=1) OR (tab2.max_a=2))
THEN 1 ELSE 0 END AS max_a,b
FROM (SELECT MAX(a) as max_a,b FROM t1 GROUP BY t1.b) AS tab2
) AS tab1
WHERE (tab1.max_a=1);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<derived3>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 3,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "case when tab2.max_a = 1 or tab2.max_a = 2 then 1 else 0 end = 1",
          "materialized": {
            "query_block": {
              "select_id": 3,
              "cost": "COST_REPLACED",
              "having_condition": "case when max_a = 1 or max_a = 2 then 1 else 0 end = 1",
              "filesort": {
                "sort_key": "t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 3,
                        "cost": "COST_REPLACED",
                        "filtered": 100
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
SELECT *
FROM
(
SELECT CASE WHEN ((tab2.max_a=1) OR ((tab2.max_a>2) AND (tab2.max_a<4)))
THEN 1 ELSE 0 END AS max_a,b
FROM (SELECT MAX(a) as max_a,b FROM t1 GROUP BY t1.b) AS tab2
) AS tab1
WHERE (tab1.max_a=1);
max_a	b
1	2
1	4
EXPLAIN FORMAT=JSON SELECT *
FROM
(
SELECT CASE WHEN ((tab2.max_a=1) OR ((tab2.max_a>2) AND (tab2.max_a<4)))
THEN 1 ELSE 0 END AS max_a,b
FROM (SELECT MAX(a) as max_a,b FROM t1 GROUP BY t1.b) AS tab2
) AS tab1
WHERE (tab1.max_a=1);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<derived3>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 3,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "case when tab2.max_a = 1 or tab2.max_a > 2 and tab2.max_a < 4 then 1 else 0 end = 1",
          "materialized": {
            "query_block": {
              "select_id": 3,
              "cost": "COST_REPLACED",
              "having_condition": "case when max_a = 1 or max_a > 2 and max_a < 4 then 1 else 0 end = 1",
              "filesort": {
                "sort_key": "t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 3,
                        "cost": "COST_REPLACED",
                        "filtered": 100
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
SELECT *
FROM
(
SELECT CASE WHEN ((tab2.max_a>1) AND ((tab2.max_a=2) OR (tab2.max_a>2)))
THEN 1 ELSE 0 END AS max_a,b
FROM (SELECT MAX(a) as max_a,b FROM t1 GROUP BY t1.b) AS tab2
) AS tab1
WHERE (tab1.max_a=1);
max_a	b
1	3
1	4
EXPLAIN FORMAT=JSON SELECT *
FROM
(
SELECT CASE WHEN ((tab2.max_a>1) AND ((tab2.max_a=2) OR (tab2.max_a>2)))
THEN 1 ELSE 0 END AS max_a,b
FROM (SELECT MAX(a) as max_a,b FROM t1 GROUP BY t1.b) AS tab2
) AS tab1
WHERE (tab1.max_a=1);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<derived3>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 3,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "case when tab2.max_a > 1 and (tab2.max_a = 2 or tab2.max_a > 2) then 1 else 0 end = 1",
          "materialized": {
            "query_block": {
              "select_id": 3,
              "cost": "COST_REPLACED",
              "having_condition": "case when max_a > 1 and (max_a = 2 or max_a > 2) then 1 else 0 end = 1",
              "filesort": {
                "sort_key": "t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 3,
                        "cost": "COST_REPLACED",
                        "filtered": 100
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
SELECT *
FROM
(
SELECT CASE WHEN ((tab2.b=2) OR (tab2.b=4))
THEN 1 ELSE 0 END AS max_a,b
FROM (SELECT MAX(a) as max_a,b FROM t1 GROUP BY t1.b) AS tab2
) AS tab1
WHERE (tab1.max_a=1);
max_a	b
1	2
1	4
EXPLAIN FORMAT=JSON SELECT *
FROM
(
SELECT CASE WHEN ((tab2.b=2) OR (tab2.b=4))
THEN 1 ELSE 0 END AS max_a,b
FROM (SELECT MAX(a) as max_a,b FROM t1 GROUP BY t1.b) AS tab2
) AS tab1
WHERE (tab1.max_a=1);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<derived3>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 3,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "case when tab2.b = 2 or tab2.b = 4 then 1 else 0 end = 1",
          "materialized": {
            "query_block": {
              "select_id": 3,
              "cost": "COST_REPLACED",
              "filesort": {
                "sort_key": "t1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 3,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "case when t1.b = 2 or t1.b = 4 then 1 else 0 end = 1"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
DROP TABLE t1;
#
# MDEV-16803: pushdown condition with IN predicate in the derived table
#             defined with several SELECT statements
#
CREATE TABLE t1 (a INT, b INT);
INSERT INTO t1 VALUES (1,2),(3,2),(1,1);
SELECT * FROM
(
SELECT a,b,1 as c
FROM t1
UNION ALL
SELECT a,b,2 as c
FROM t1
) AS tab
WHERE ((a,b) IN ((1,2),(3,2)));
a	b	c
1	2	1
3	2	1
1	2	2
3	2	2
DROP TABLE t1;
#
# MDEV-17354: INSERT SELECT with condition pushdown into derived
#
CREATE TABLE t1 (f INT NOT NULL);
INSERT INTO t1 VALUES (3), (7), (3);
CREATE ALGORITHM= TEMPTABLE VIEW v1 AS SELECT * FROM ( SELECT * FROM t1 ) AS sq;
INSERT INTO t1
SELECT * FROM ( SELECT t1.f FROM v1 JOIN t1 ) AS t WHERE f IS NOT NULL;
EXPLAIN INSERT INTO t1
SELECT * FROM ( SELECT t1.f FROM v1 JOIN t1 ) AS t WHERE f IS NOT NULL;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	<derived4>	ALL	NULL	NULL	NULL	NULL	12	Using temporary
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	12	Using where; Using join buffer (flat, BNL join)
4	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	12	
EXPLAIN FORMAT=JSON INSERT INTO t1
SELECT * FROM ( SELECT t1.f FROM v1 JOIN t1 ) AS t WHERE f IS NOT NULL;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "temporary_table": {
      "nested_loop": [
        {
          "table": {
            "table_name": "<derived4>",
            "access_type": "ALL",
            "loops": 1,
            "rows": 12,
            "cost": "COST_REPLACED",
            "filtered": 100,
            "materialized": {
              "query_block": {
                "select_id": 4,
                "cost": "COST_REPLACED",
                "nested_loop": [
                  {
                    "table": {
                      "table_name": "t1",
                      "access_type": "ALL",
                      "loops": 1,
                      "rows": 12,
                      "cost": "COST_REPLACED",
                      "filtered": 100
                    }
                  }
                ]
              }
            }
          }
        },
        {
          "block-nl-join": {
            "table": {
              "table_name": "t1",
              "access_type": "ALL",
              "loops": 12,
              "rows": 12,
              "cost": "COST_REPLACED",
              "filtered": 100,
              "attached_condition": "t1.f is not null"
            },
            "buffer_type": "flat",
            "buffer_size": "64",
            "join_type": "BNL"
          }
        }
      ]
    }
  }
}
SELECT * FROM t1;
f
3
7
3
3
3
3
7
7
7
3
3
3
DELETE FROM t1;
INSERT INTO t1 VALUES (3), (7), (3);
INSERT INTO t1
SELECT * FROM ( SELECT t1.f FROM v1 JOIN t1 ON v1.f=t1.f) AS t
WHERE f IS NOT NULL;
EXPLAIN FORMAT=JSON INSERT INTO t1
SELECT * FROM ( SELECT t1.f FROM v1 JOIN t1 ON v1.f=t1.f) AS t
WHERE f IS NOT NULL;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "temporary_table": {
      "nested_loop": [
        {
          "table": {
            "table_name": "t1",
            "access_type": "ALL",
            "loops": 1,
            "rows": 8,
            "cost": "COST_REPLACED",
            "filtered": 100,
            "attached_condition": "t1.f is not null"
          }
        },
        {
          "table": {
            "table_name": "<derived4>",
            "access_type": "ref",
            "possible_keys": ["key0"],
            "key": "key0",
            "key_length": "4",
            "used_key_parts": ["f"],
            "ref": ["test.t1.f"],
            "loops": 8,
            "rows": 1,
            "cost": "COST_REPLACED",
            "filtered": 100,
            "materialized": {
              "query_block": {
                "select_id": 4,
                "cost": "COST_REPLACED",
                "nested_loop": [
                  {
                    "table": {
                      "table_name": "t1",
                      "access_type": "ALL",
                      "loops": 1,
                      "rows": 8,
                      "cost": "COST_REPLACED",
                      "filtered": 100,
                      "attached_condition": "t1.f is not null"
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}
SELECT * FROM t1;
f
3
7
3
3
3
7
3
3
DROP VIEW v1;
DROP TABLE t1;
#
# MDEV-17574: pushdown into derived from mergeable view
#             used in multi-table UPDATE
#             pushdown into materialized derived from mergeable view
#             used in SELECT
#
CREATE TABLE t1 (f1 text, f2 int);
INSERT INTO t1 VALUES ('x',1), ('y',2);
CREATE VIEW v1 AS SELECT f2 FROM ( SELECT f2 FROM t1 GROUP BY f2) AS t;
UPDATE v1, t1 SET t1.f1 = 'z' WHERE v1.f2 < 2 AND t1.f2 = v1.f2;
EXPLAIN FORMAT=JSON UPDATE v1, t1 SET t1.f1 = 'z' WHERE v1.f2 < 2 AND t1.f2 = v1.f2;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t1",
          "access_type": "ALL",
          "loops": 1,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t1.f2 < 2 and t1.f2 is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived3>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "5",
          "used_key_parts": ["f2"],
          "ref": ["test.t1.f2"],
          "loops": 2,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "materialized": {
            "query_block": {
              "select_id": 3,
              "cost": "COST_REPLACED",
              "filesort": {
                "sort_key": "t1.f2",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 2,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.f2 < 2"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
SELECT * FROM t1;
f1	f2
z	1
y	2
CREATE VIEW v2 AS SELECT f2 FROM ( SELECT DISTINCT f2 FROM t1 ) AS t;
SELECT * FROM v2, t1 WHERE v2.f2 < 2 AND t1.f2 = v2.f2;
f2	f1	f2
1	z	1
EXPLAIN FORMAT=JSON SELECT * FROM v2, t1 WHERE v2.f2 < 2 AND t1.f2 = v2.f2;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t1",
          "access_type": "ALL",
          "loops": 1,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t1.f2 < 2 and t1.f2 is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived3>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "5",
          "used_key_parts": ["f2"],
          "ref": ["test.t1.f2"],
          "loops": 2,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "materialized": {
            "query_block": {
              "select_id": 3,
              "cost": "COST_REPLACED",
              "temporary_table": {
                "nested_loop": [
                  {
                    "table": {
                      "table_name": "t1",
                      "access_type": "ALL",
                      "loops": 1,
                      "rows": 2,
                      "cost": "COST_REPLACED",
                      "filtered": 100,
                      "attached_condition": "t1.f2 < 2"
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
DROP VIEW v1,v2;
DROP TABLE t1;
#
# MDEV-18383: pushdown condition with the IF structure
#             defined with Item_cond item
#
CREATE TABLE t1(a INT, b INT);
CREATE TABLE t2(c INT, d INT);
INSERT INTO t1 VALUES (1,2),(3,4),(5,6);
INSERT INTO t2 VALUES (1,3),(3,7),(5,1);
SELECT *
FROM t1,
(
SELECT MAX(d) AS max_d,c
FROM t2
GROUP BY c
) AS tab
WHERE t1.a=tab.c AND
IF(2,t1.a=1 OR t1.b>5,1=1);
a	b	max_d	c
1	2	3	1
5	6	1	5
DROP TABLE t1,t2;
#
# MDEV-19139: pushdown condition with Item_func_set_user_var
#
CREATE TABLE t1 (a INT, b INT);
CREATE VIEW v1 AS SELECT a, MAX(b) FROM t1 GROUP BY a;
SELECT * FROM (SELECT 1 FROM v1 UNION (SELECT 1 FROM v1 WHERE @a := uuid())) dt;
1
EXPLAIN FORMAT=JSON
SELECT * FROM (SELECT 1 FROM v1 UNION (SELECT 1 FROM v1 WHERE @a := uuid())) dt;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<union2,3>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "table": {
                        "message": "no matching row in const table"
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "UNION",
                      "table": {
                        "message": "no matching row in const table"
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
DROP TABLE t1;
DROP VIEW v1;
#
# MDEV-21388 Wrong result of DAYNAME()=xxx in combination with condition_pushdown_for_derived=on
#
CREATE TABLE t1 (a INT, b DATE, c INT);
INSERT INTO t1 VALUES
(1,'2001-01-21',345),
(6,'2001-01-20',315),
(6,'2001-01-20',214);
CREATE TABLE t2 (a INT, b INT);
INSERT INTO t2 VALUES (2,19), (7,20);
CREATE VIEW v1 AS SELECT a, b, max(c) AS max_c FROM t1
GROUP BY a,b HAVING max_c < 707;
SELECT *, dayname(v1.b) FROM v1,t2 WHERE (v1.max_c>214) AND (t2.a>v1.a);
a	b	max_c	a	b	dayname(v1.b)
1	2001-01-21	345	2	19	Sunday
1	2001-01-21	345	7	20	Sunday
6	2001-01-20	315	7	20	Saturday
SET optimizer_switch='condition_pushdown_for_derived=off';
SELECT *, dayname(v1.b) FROM v1,t2 WHERE (v1.max_c>214) AND (t2.a>v1.a) AND dayname(v1.b)='Sunday';
a	b	max_c	a	b	dayname(v1.b)
1	2001-01-21	345	2	19	Sunday
1	2001-01-21	345	7	20	Sunday
SET optimizer_switch='condition_pushdown_for_derived=on';
SELECT *, dayname(v1.b) FROM v1,t2 WHERE (v1.max_c>214) AND (t2.a>v1.a) AND dayname(v1.b)='Sunday';
a	b	max_c	a	b	dayname(v1.b)
1	2001-01-21	345	2	19	Sunday
1	2001-01-21	345	7	20	Sunday
DROP VIEW v1;
DROP TABLE t1, t2;
SET optimizer_switch=DEFAULT;
#
# MDEV-17177: an attempt to push down IN predicate when one of
#             the arguments is too complex to be cloned
#
CREATE TABLE t1 (a VARCHAR(8));
INSERT INTO t1 VALUES ('abc'),('def');
CREATE VIEW v1 AS SELECT * FROM t1 GROUP BY a;
SELECT * FROM v1 WHERE IF( a REGEXP 'def', 'foo', a ) IN ('abc', 'foobar');
a
abc
DROP VIEW v1;
DROP TABLE t1;
#
# MDEV-19179: pushdown into UNION of aggregation selects whose
#             corresponding columns have different names
#
create table t1 (a int);
insert into t1 values (3), (7), (1);
select *
from (select min(a) as x from t1 union all select max(a) as y from t1) t
where x>0;
x
1
7
explain extended select *
from (select min(a) as x from t1 union all select max(a) as y from t1) t
where x>0;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	6	100.00	Using where
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	3	100.00	
3	UNION	t1	ALL	NULL	NULL	NULL	NULL	3	100.00	
Warnings:
Note	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
prepare stmt from "select *
from (select min(a) as x from t1 union all select max(a) as y from t1) t
where x>0";
execute stmt;
x
1
7
execute stmt;
x
1
7
deallocate prepare stmt;
create view v1(m) as
select min(a) as x from t1 union all select max(a) as y from t1;
select * from v1 where m > 0;
m
1
7
drop view v1;
drop table t1;
#
# MDEV-25635: pushdown into grouping view using aggregate functions
#             with constant arguments via a mergeable derived table
#
create table t1 (a int);
insert into t1 values (3), (7), (1), (3), (7), (7), (3);
create view v1 as select a, sum(1) as f, sum(1) as g from t1 group by a;
select * from v1;
a	f	g
1	1	1
3	3	3
7	3	3
select * from (select * from v1) as dt where a=f and a=g;
a	f	g
1	1	1
3	3	3
explain extended select * from (select * from v1) as dt where a=f and a=g;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	PRIMARY	<derived3>	ALL	NULL	NULL	NULL	NULL	7	100.00	Using where
3	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	7	100.00	Using temporary; Using filesort
Warnings:
Note	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`
create view v2 as select a, min(1) as f, min(1) as g from t1 group by a;
select * from v2;
a	f	g
1	1	1
3	1	1
7	1	1
select * from (select * from v2) as dt where a=f and a=g;
a	f	g
1	1	1
explain extended select * from (select * from v2) as dt where a=f and a=g;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	PRIMARY	<derived3>	ALL	NULL	NULL	NULL	NULL	7	100.00	Using where
3	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	7	100.00	Using temporary; Using filesort
Warnings:
Note	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`
drop view v1,v2;
drop table t1;
#
# MDEV-25969: Condition pushdown into derived table doesn't work if select list uses SP
#
create function f1(a int) returns int DETERMINISTIC return (a+1);
create table t1 (
pk int primary key,
a int,
b int,
key(a)
);
create table t2(a int);
insert into t2 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
create table t3(a int);
insert into t3 select A.a + B.a* 10 + C.a * 100 from t2 A, t2 B, t2 C;
insert into t1 select a,a,a from t3;
create view v1 as
select
t1.a as col1,
f1(t1.b) as col2
from
t1;
create view v2 as
select
t1.a as col1,
f1(t1.b) as col2
from
t1;
create view v3 as
select col2, col1 from v1
union all
select col2, col1 from v2;
explain select * from v3 where col1=123;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	2	Using where
2	DERIVED	t1	ref	a	a	5	const	1	
3	UNION	t1	ref	a	a	5	const	1	
# This must use ref accesses for reading table t1, not full scans:
explain format=json
select * from v3 where col1=123 and col2=321;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v3.col1 = 123 and v3.col2 = 321",
          "materialized": {
            "query_block": {
              "union_result": {
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "COST_REPLACED",
                      "nested_loop": [
                        {
                          "table": {
                            "table_name": "t1",
                            "access_type": "ref",
                            "possible_keys": ["a"],
                            "key": "a",
                            "key_length": "5",
                            "used_key_parts": ["a"],
                            "ref": ["const"],
                            "loops": 1,
                            "rows": 1,
                            "cost": "COST_REPLACED",
                            "filtered": 100
                          }
                        }
                      ]
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "UNION",
                      "cost": "COST_REPLACED",
                      "nested_loop": [
                        {
                          "table": {
                            "table_name": "t1",
                            "access_type": "ref",
                            "possible_keys": ["a"],
                            "key": "a",
                            "key_length": "5",
                            "used_key_parts": ["a"],
                            "ref": ["const"],
                            "loops": 1,
                            "rows": 1,
                            "cost": "COST_REPLACED",
                            "filtered": 100
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
drop function f1;
drop view v1,v2,v3;
drop table t1, t2,t3;
#
# Another testcase, with pushdown through GROUP BY
#
create table t1 (a int, b  int);
insert into t1 values (1,1),(2,2),(3,3);
create function f1(a int) returns int DETERMINISTIC return (a+1);
create view v2(a, a2, s) as 
select a, f1(a), sum(b) from t1 group by a, f1(a);
# Here, 
#   "(s+1) > 10" will be pushed into HAVING
#   "a > 1" will be pushed all the way to the table scan on t1
#   "a2>123" will be pushed into HAVING (as it refers to an SP call which
#                                       prevents pushing it to the WHERE)
explain format=json
select * from v2 where (s+1) > 10 AND a > 1 and a2>123;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 3,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v2.s + 1 > 10 and v2.a > 1 and v2.a2 > 123",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "s + 1 > 10 and a2 > 123",
              "filesort": {
                "sort_key": "t1.a, f1(t1.a)",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 3,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.a > 1"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
# Extra test for 10.4+: Check that this works for pushdown into IN
# subqueries:
create table t4 (a int, b int, c decimal);
insert into t4 select a,a,a from t1;
# The subquery must be materialized and must have
#  "attached_condition": "t1.a + 1 > 10",
#  "having_condition": "`f1(a)` > 1 and `sum(b)` > 123",
explain format=json
select *
from t4
where
(a,b,c) in (select a, f1(a), sum(b) from t1 group by a, f1(a))
and
(a+1) > 10 AND b > 1 and c>123;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t4",
          "access_type": "ALL",
          "loops": 1,
          "rows": 3,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "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"
        }
      },
      {
        "table": {
          "table_name": "<subquery2>",
          "access_type": "eq_ref",
          "possible_keys": ["distinct_key"],
          "key": "distinct_key",
          "key_length": "23",
          "used_key_parts": ["a", "f1(a)", "sum(b)"],
          "ref": ["test.t4.a", "test.t4.b", "test.t4.c"],
          "loops": 3,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t4.c = `<subquery2>`.`sum(b)`",
          "materialized": {
            "unique": 1,
            "materialization": {
              "query_block": {
                "select_id": 2,
                "cost": "COST_REPLACED",
                "having_condition": "`f1(a)` > 1 and `sum(b)` > 123",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t1",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 3,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t1.a + 1 > 10"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
drop view v2;
drop function f1;
drop table t1, t4;
# End of 10.2 tests
#
# MDEV-14579: pushdown conditions into materialized views/derived tables
# that are defined with EXIST or/and INTERSECT
#
create table t1 (a int, b int, c int);
create table t2 (a int, b int, c int);
insert into t1 values
(1,21,345), (1,33,7), (8,33,114), (1,21,500), (1,19,117), (5,14,787),
(8,33,123), (9,10,211), (5,16,207), (1,33,988), (5,27,132), (1,21,104),
(6,20,309), (6,20,315), (1,21,101), (4,33,404), (9,10,800), (1,21,123);
insert into t2 values
(2,3,207), (1,16,909), (5,14,312),
(5,33,207), (6,20,211), (1,19,132),
(8,33,117), (3,21,231), (6,23,303);
create view v1 as
select a, b, min(c) as c from t1
where t1.a<9 group by a,b having c < 300
intersect
select a, b, min(c) as c from t1
where t1.b>10 group by a,b having c > 100;
# using intersect in view definition
# conjunctive subformulas : pushing into WHERE
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.a=t2.a) and (v1.a<5);
a	b	c	a	b	c
1	21	101	1	16	909
1	19	117	1	16	909
1	21	101	1	19	132
1	19	117	1	19	132
select * from v1,t2 where (v1.a=t2.a) and (v1.a<5);
a	b	c	a	b	c
1	21	101	1	16	909
1	19	117	1	16	909
1	21	101	1	19	132
1	19	117	1	19	132
explain select * from v1,t2 where (v1.a=t2.a) and (v1.a<5);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key1,distinct_key	key1	5	test.t2.a	1	
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
3	INTERSECT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
NULL	INTERSECT RESULT	<intersect2,3>	ALL	NULL	NULL	NULL	NULL	NULL	
explain format=json select * from v1,t2 where (v1.a=t2.a) and (v1.a<5);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a < 5 and t2.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key1", "distinct_key"],
          "key": "key1",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t2.a"],
          "loops": 9,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<intersect2,3>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "COST_REPLACED",
                      "having_condition": "c < 300",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a < 9 and t1.a < 5"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "INTERSECT",
                      "cost": "COST_REPLACED",
                      "having_condition": "c > 100",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.b > 10 and t1.a < 5"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
# using intersect in view definition
# conjunctive subformulas : pushing into WHERE
# pushing equalities
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.a=t2.a) and (v1.a=8);
a	b	c	a	b	c
8	33	114	8	33	117
select * from v1,t2 where (v1.a=t2.a) and (v1.a=8);
a	b	c	a	b	c
8	33	114	8	33	117
explain select * from v1,t2 where (v1.a=t2.a) and (v1.a=8);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ALL	distinct_key	NULL	NULL	NULL	18	Using where; Using join buffer (flat, BNL join)
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
3	INTERSECT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
NULL	INTERSECT RESULT	<intersect2,3>	ALL	NULL	NULL	NULL	NULL	NULL	
explain format=json select * from v1,t2 where (v1.a=t2.a) and (v1.a=8);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a = 8"
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived2>",
            "access_type": "ALL",
            "possible_keys": ["distinct_key"],
            "loops": 9,
            "rows": 18,
            "cost": "COST_REPLACED",
            "filtered": 100,
            "attached_condition": "v1.a = 8"
          },
          "buffer_type": "flat",
          "buffer_size": "173",
          "join_type": "BNL",
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<intersect2,3>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "COST_REPLACED",
                      "having_condition": "c < 300",
                      "filesort": {
                        "sort_key": "t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a = 8"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "INTERSECT",
                      "cost": "COST_REPLACED",
                      "having_condition": "c > 100",
                      "filesort": {
                        "sort_key": "t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a = 8 and t1.b > 10"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
# using intersect in view definition
# conjunctive subformulas : pushing into WHERE using equalities
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.a=t2.a) and (t2.a=8);
a	b	c	a	b	c
8	33	114	8	33	117
select * from v1,t2 where (v1.a=t2.a) and (t2.a=8);
a	b	c	a	b	c
8	33	114	8	33	117
explain select * from v1,t2 where (v1.a=t2.a) and (t2.a=8);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ALL	distinct_key	NULL	NULL	NULL	18	Using where; Using join buffer (flat, BNL join)
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
3	INTERSECT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
NULL	INTERSECT RESULT	<intersect2,3>	ALL	NULL	NULL	NULL	NULL	NULL	
explain format=json select * from v1,t2 where (v1.a=t2.a) and (t2.a=8);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a = 8"
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived2>",
            "access_type": "ALL",
            "possible_keys": ["distinct_key"],
            "loops": 9,
            "rows": 18,
            "cost": "COST_REPLACED",
            "filtered": 100,
            "attached_condition": "v1.a = 8"
          },
          "buffer_type": "flat",
          "buffer_size": "173",
          "join_type": "BNL",
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<intersect2,3>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "COST_REPLACED",
                      "having_condition": "c < 300",
                      "filesort": {
                        "sort_key": "t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a = 8"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "INTERSECT",
                      "cost": "COST_REPLACED",
                      "having_condition": "c > 100",
                      "filesort": {
                        "sort_key": "t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a = 8 and t1.b > 10"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
# using intersect in view definition
# conjunctive subformulas : pushing into HAVING
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.a=t2.a) and (v1.c>200);
a	b	c	a	b	c
5	16	207	5	14	312
5	16	207	5	33	207
select * from v1,t2 where (v1.a=t2.a) and (v1.c>200);
a	b	c	a	b	c
5	16	207	5	14	312
5	16	207	5	33	207
explain select * from v1,t2 where (v1.a=t2.a) and (v1.c>200);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key1,distinct_key	key1	5	test.t2.a	1	Using where
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
3	INTERSECT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
NULL	INTERSECT RESULT	<intersect2,3>	ALL	NULL	NULL	NULL	NULL	NULL	
explain format=json select * from v1,t2 where (v1.a=t2.a) and (v1.c>200);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key1", "distinct_key"],
          "key": "key1",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t2.a"],
          "loops": 9,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v1.c > 200",
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<intersect2,3>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "COST_REPLACED",
                      "having_condition": "c < 300 and c > 200",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a < 9"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "INTERSECT",
                      "cost": "COST_REPLACED",
                      "having_condition": "c > 100 and c > 200",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.b > 10"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
# using intersect in view definition
# conjunctive subformulas : pushing into WHERE
# conjunctive subformulas : pushing into HAVING
set 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);
a	b	c	a	b	c
1	19	117	1	16	909
1	19	117	1	19	132
select * from v1,t2 where (v1.a=t2.a) and (v1.a<5) and (v1.c>110);
a	b	c	a	b	c
1	19	117	1	16	909
1	19	117	1	19	132
explain select * from v1,t2 where (v1.a=t2.a) and (v1.a<5) and (v1.c>110);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key1,distinct_key	key1	5	test.t2.a	1	Using where
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
3	INTERSECT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
NULL	INTERSECT RESULT	<intersect2,3>	ALL	NULL	NULL	NULL	NULL	NULL	
explain format=json select * from v1,t2 where (v1.a=t2.a) and (v1.a<5) and (v1.c>110);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a < 5 and t2.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key1", "distinct_key"],
          "key": "key1",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t2.a"],
          "loops": 9,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v1.c > 110",
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<intersect2,3>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "COST_REPLACED",
                      "having_condition": "c < 300 and c > 110",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a < 9 and t1.a < 5"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "INTERSECT",
                      "cost": "COST_REPLACED",
                      "having_condition": "c > 100 and c > 110",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.b > 10 and t1.a < 5"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
# using intersect in view definition
# extracted or formula : pushing into WHERE
set 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));
a	b	c	a	b	c
5	16	207	5	14	312
5	16	207	5	33	207
8	33	114	8	33	117
select * from v1,t2 where (v1.a=t2.a) and ((v1.b>27) or (v1.b<19));
a	b	c	a	b	c
5	16	207	5	14	312
5	16	207	5	33	207
8	33	114	8	33	117
explain select * from v1,t2 where (v1.a=t2.a) and ((v1.b>27) or (v1.b<19));
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key1,distinct_key	key1	5	test.t2.a	1	Using where
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
3	INTERSECT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
NULL	INTERSECT RESULT	<intersect2,3>	ALL	NULL	NULL	NULL	NULL	NULL	
explain format=json select * from v1,t2 where (v1.a=t2.a) and ((v1.b>27) or (v1.b<19));
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key1", "distinct_key"],
          "key": "key1",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t2.a"],
          "loops": 9,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v1.b > 27 or v1.b < 19",
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<intersect2,3>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "COST_REPLACED",
                      "having_condition": "c < 300",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a < 9 and (t1.b > 27 or t1.b < 19)"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "INTERSECT",
                      "cost": "COST_REPLACED",
                      "having_condition": "c > 100",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.b > 10 and (t1.b > 27 or t1.b < 19)"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
# using intersect in view definition
# extracted or formula : pushing into HAVING
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where
(v1.a=t2.a) and ((v1.c>200) or (v1.c<105));
a	b	c	a	b	c
1	21	101	1	16	909
5	16	207	5	14	312
5	16	207	5	33	207
1	21	101	1	19	132
select * from v1,t2 where
(v1.a=t2.a) and ((v1.c>200) or (v1.c<105));
a	b	c	a	b	c
1	21	101	1	16	909
5	16	207	5	14	312
5	16	207	5	33	207
1	21	101	1	19	132
explain select * from v1,t2 where
(v1.a=t2.a) and ((v1.c>200) or (v1.c<105));
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key1,distinct_key	key1	5	test.t2.a	1	Using where
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
3	INTERSECT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
NULL	INTERSECT RESULT	<intersect2,3>	ALL	NULL	NULL	NULL	NULL	NULL	
explain format=json select * from v1,t2 where
(v1.a=t2.a) and ((v1.c>200) or (v1.c<105));
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key1", "distinct_key"],
          "key": "key1",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t2.a"],
          "loops": 9,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v1.c > 200 or v1.c < 105",
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<intersect2,3>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "COST_REPLACED",
                      "having_condition": "c < 300 and (c > 200 or c < 105)",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a < 9"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "INTERSECT",
                      "cost": "COST_REPLACED",
                      "having_condition": "c > 100 and (c > 200 or c < 105)",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.b > 10"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
# using intersect in view definition
# extracted or formula : pushing into WHERE
# extracted or formula : pushing into HAVING using equalities
# pushing equalities
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where
((v1.a>3) and (t2.c>110) and (v1.c=t2.c)) or
((v1.a=1) and (v1.c<110));
a	b	c	a	b	c
1	21	101	2	3	207
1	21	101	1	16	909
1	21	101	5	14	312
1	21	101	5	33	207
1	21	101	6	20	211
1	21	101	1	19	132
1	21	101	8	33	117
1	21	101	3	21	231
1	21	101	6	23	303
5	16	207	2	3	207
5	16	207	5	33	207
5	27	132	1	19	132
select * from v1,t2 where
((v1.a>3) and (t2.c>110) and (v1.c=t2.c)) or
((v1.a=1) and (v1.c<110));
a	b	c	a	b	c
1	21	101	2	3	207
1	21	101	1	16	909
1	21	101	5	14	312
1	21	101	5	33	207
1	21	101	6	20	211
1	21	101	1	19	132
1	21	101	8	33	117
1	21	101	3	21	231
1	21	101	6	23	303
5	16	207	2	3	207
5	16	207	5	33	207
5	27	132	1	19	132
explain select * from v1,t2 where
((v1.a>3) and (t2.c>110) and (v1.c=t2.c)) or
((v1.a=1) and (v1.c<110));
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	18	Using where; Using join buffer (flat, BNL join)
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
3	INTERSECT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
NULL	INTERSECT RESULT	<intersect2,3>	ALL	NULL	NULL	NULL	NULL	NULL	
explain format=json select * from v1,t2 where
((v1.a>3) and (t2.c>110) and (v1.c=t2.c)) or
((v1.a=1) and (v1.c<110));
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived2>",
            "access_type": "ALL",
            "loops": 9,
            "rows": 18,
            "cost": "COST_REPLACED",
            "filtered": 100,
            "attached_condition": "v1.a > 3 or v1.a = 1 and v1.c < 110"
          },
          "buffer_type": "flat",
          "buffer_size": "173",
          "join_type": "BNL",
          "attached_condition": "v1.c = t2.c and v1.a > 3 and t2.c > 110 or v1.a = 1 and v1.c < 110",
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<intersect2,3>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "COST_REPLACED",
                      "having_condition": "c < 300 and (t1.a > 3 and c > 110 or t1.a = 1 and c < 110)",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a < 9 and (t1.a > 3 or t1.a = 1)"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "INTERSECT",
                      "cost": "COST_REPLACED",
                      "having_condition": "c > 100 and (t1.a > 3 and c > 110 or t1.a = 1 and c < 110)",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.b > 10 and (t1.a > 3 or t1.a = 1)"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
# using intersect in view definition
# prepare of a query
# conjunctive subformulas : pushing into WHERE
# conjunctive subformulas : pushing into HAVING
prepare stmt from "select * from v1,t2
                   where (v1.a=t2.a) and (v1.a<5) and (v1.c>110);";
execute stmt;
a	b	c	a	b	c
1	19	117	1	16	909
1	19	117	1	19	132
execute stmt;
a	b	c	a	b	c
1	19	117	1	16	909
1	19	117	1	19	132
deallocate prepare stmt;
# using intersect in derived table definition
# extracted or formula : pushing into WHERE using equalities
# extracted or formula : pushing into HAVING
# pushing equalities
set statement optimizer_switch='condition_pushdown_for_derived=off' for select *
from t2,
(select a, b, min(c) as c from t1
where t1.a<9 group by a,b having c < 300
intersect
select a, b, min(c) as c from t1
where t1.b>10 group by a,b having c > 100) as d1
where
(d1.b=t2.b) and
(((t2.b>13) and (t2.c=909)) or
((d1.a<4) and (d1.c<200)));
a	b	c	a	b	c
1	16	909	5	16	207
1	19	132	1	19	117
3	21	231	1	21	101
select *
from t2,
(select a, b, min(c) as c from t1
where t1.a<9 group by a,b having c < 300
intersect
select a, b, min(c) as c from t1
where t1.b>10 group by a,b having c > 100) as d1
where
(d1.b=t2.b) and
(((t2.b>13) and (t2.c=909)) or
((d1.a<4) and (d1.c<200)));
a	b	c	a	b	c
1	16	909	5	16	207
1	19	132	1	19	117
3	21	231	1	21	101
explain select *
from t2,
(select a, b, min(c) as c from t1
where t1.a<9 group by a,b having c < 300
intersect
select a, b, min(c) as c from t1
where t1.b>10 group by a,b having c > 100) as d1
where
(d1.b=t2.b) and
(((t2.b>13) and (t2.c=909)) or
((d1.a<4) and (d1.c<200)));
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key1,distinct_key	key1	5	test.t2.b	1	Using where
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
3	INTERSECT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
NULL	INTERSECT RESULT	<intersect2,3>	ALL	NULL	NULL	NULL	NULL	NULL	
explain format=json select *
from t2,
(select a, b, min(c) as c from t1
where t1.a<9 group by a,b having c < 300
intersect
select a, b, min(c) as c from t1
where t1.b>10 group by a,b having c > 100) as d1
where
(d1.b=t2.b) and
(((t2.b>13) and (t2.c=909)) or
((d1.a<4) and (d1.c<200)));
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.b is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key1", "distinct_key"],
          "key": "key1",
          "key_length": "5",
          "used_key_parts": ["b"],
          "ref": ["test.t2.b"],
          "loops": 9,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.c = 909 and t2.b > 13 or d1.a < 4 and d1.c < 200",
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<intersect2,3>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "COST_REPLACED",
                      "having_condition": "c < 300 and (t1.b > 13 or t1.a < 4 and c < 200)",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a < 9 and (t1.b > 13 or t1.a < 4)"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "INTERSECT",
                      "cost": "COST_REPLACED",
                      "having_condition": "c > 100 and (t1.b > 13 or t1.a < 4 and c < 200)",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.b > 10 and (t1.b > 13 or t1.a < 4)"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
drop view v1;
create view v1 as
select a, b, max(c) as c from t1
where t1.a<9 group by a,b having c > 200
except
select a, b, max(c) as c from t1
where t1.b>10 group by a,b having c < 300;
# using except in view definition
# conjunctive subformulas : pushing into WHERE
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.a=t2.a) and (v1.a<5);
a	b	c	a	b	c
1	33	988	1	16	909
1	21	500	1	16	909
1	33	988	1	19	132
1	21	500	1	19	132
select * from v1,t2 where (v1.a=t2.a) and (v1.a<5);
a	b	c	a	b	c
1	33	988	1	16	909
1	21	500	1	16	909
1	33	988	1	19	132
1	21	500	1	19	132
explain select * from v1,t2 where (v1.a=t2.a) and (v1.a<5);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key1,distinct_key	key1	5	test.t2.a	1	
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
3	EXCEPT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
NULL	EXCEPT RESULT	<except2,3>	ALL	NULL	NULL	NULL	NULL	NULL	
explain format=json select * from v1,t2 where (v1.a=t2.a) and (v1.a<5);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a < 5 and t2.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key1", "distinct_key"],
          "key": "key1",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t2.a"],
          "loops": 9,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<except2,3>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "COST_REPLACED",
                      "having_condition": "c > 200",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a < 9 and t1.a < 5"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "EXCEPT",
                      "cost": "COST_REPLACED",
                      "having_condition": "c < 300",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.b > 10 and t1.a < 5"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
# using except in view definition
# conjunctive subformulas : pushing into WHERE
# pushing equalities
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.a=t2.a) and (v1.a=6);
a	b	c	a	b	c
6	20	315	6	20	211
6	20	315	6	23	303
select * from v1,t2 where (v1.a=t2.a) and (v1.a=6);
a	b	c	a	b	c
6	20	315	6	20	211
6	20	315	6	23	303
explain select * from v1,t2 where (v1.a=t2.a) and (v1.a=6);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ALL	distinct_key	NULL	NULL	NULL	18	Using where; Using join buffer (flat, BNL join)
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
3	EXCEPT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
NULL	EXCEPT RESULT	<except2,3>	ALL	NULL	NULL	NULL	NULL	NULL	
explain format=json select * from v1,t2 where (v1.a=t2.a) and (v1.a=6);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a = 6"
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived2>",
            "access_type": "ALL",
            "possible_keys": ["distinct_key"],
            "loops": 9,
            "rows": 18,
            "cost": "COST_REPLACED",
            "filtered": 100,
            "attached_condition": "v1.a = 6"
          },
          "buffer_type": "flat",
          "buffer_size": "173",
          "join_type": "BNL",
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<except2,3>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "COST_REPLACED",
                      "having_condition": "c > 200",
                      "filesort": {
                        "sort_key": "t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a = 6"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "EXCEPT",
                      "cost": "COST_REPLACED",
                      "having_condition": "c < 300",
                      "filesort": {
                        "sort_key": "t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a = 6 and t1.b > 10"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
# using except in view definition
# conjunctive subformulas : pushing into WHERE using equalities
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.a=t2.a) and (t2.a=6);
a	b	c	a	b	c
6	20	315	6	20	211
6	20	315	6	23	303
select * from v1,t2 where (v1.a=t2.a) and (t2.a=6);
a	b	c	a	b	c
6	20	315	6	20	211
6	20	315	6	23	303
explain select * from v1,t2 where (v1.a=t2.a) and (t2.a=6);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ALL	distinct_key	NULL	NULL	NULL	18	Using where; Using join buffer (flat, BNL join)
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
3	EXCEPT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
NULL	EXCEPT RESULT	<except2,3>	ALL	NULL	NULL	NULL	NULL	NULL	
explain format=json select * from v1,t2 where (v1.a=t2.a) and (t2.a=6);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a = 6"
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived2>",
            "access_type": "ALL",
            "possible_keys": ["distinct_key"],
            "loops": 9,
            "rows": 18,
            "cost": "COST_REPLACED",
            "filtered": 100,
            "attached_condition": "v1.a = 6"
          },
          "buffer_type": "flat",
          "buffer_size": "173",
          "join_type": "BNL",
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<except2,3>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "COST_REPLACED",
                      "having_condition": "c > 200",
                      "filesort": {
                        "sort_key": "t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a = 6"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "EXCEPT",
                      "cost": "COST_REPLACED",
                      "having_condition": "c < 300",
                      "filesort": {
                        "sort_key": "t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a = 6 and t1.b > 10"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
# using except in view definition
# conjunctive subformulas : pushing into HAVING
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.a=t2.a) and (v1.c>500);
a	b	c	a	b	c
1	33	988	1	16	909
5	14	787	5	14	312
5	14	787	5	33	207
1	33	988	1	19	132
select * from v1,t2 where (v1.a=t2.a) and (v1.c>500);
a	b	c	a	b	c
1	33	988	1	16	909
5	14	787	5	14	312
5	14	787	5	33	207
1	33	988	1	19	132
explain select * from v1,t2 where (v1.a=t2.a) and (v1.c>500);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key1,distinct_key	key1	5	test.t2.a	1	Using where
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
3	EXCEPT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
NULL	EXCEPT RESULT	<except2,3>	ALL	NULL	NULL	NULL	NULL	NULL	
explain format=json select * from v1,t2 where (v1.a=t2.a) and (v1.c>500);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key1", "distinct_key"],
          "key": "key1",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t2.a"],
          "loops": 9,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v1.c > 500",
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<except2,3>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "COST_REPLACED",
                      "having_condition": "c > 200 and c > 500",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a < 9"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "EXCEPT",
                      "cost": "COST_REPLACED",
                      "having_condition": "c < 300 and c > 500",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.b > 10"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
# using except in view definition
# conjunctive subformulas : pushing into WHERE
# conjunctive subformulas : pushing into HAVING
set 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);
a	b	c	a	b	c
1	33	988	1	16	909
1	33	988	1	19	132
select * from v1,t2 where (v1.a=t2.a) and (v1.a<5) and (v1.c>500);
a	b	c	a	b	c
1	33	988	1	16	909
1	33	988	1	19	132
explain select * from v1,t2 where (v1.a=t2.a) and (v1.a<5) and (v1.c>500);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key1,distinct_key	key1	5	test.t2.a	1	Using where
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
3	EXCEPT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
NULL	EXCEPT RESULT	<except2,3>	ALL	NULL	NULL	NULL	NULL	NULL	
explain format=json select * from v1,t2 where (v1.a=t2.a) and (v1.a<5) and (v1.c>500);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a < 5 and t2.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key1", "distinct_key"],
          "key": "key1",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t2.a"],
          "loops": 9,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v1.c > 500",
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<except2,3>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "COST_REPLACED",
                      "having_condition": "c > 200 and c > 500",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a < 9 and t1.a < 5"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "EXCEPT",
                      "cost": "COST_REPLACED",
                      "having_condition": "c < 300 and c > 500",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.b > 10 and t1.a < 5"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
# using except in view definition
# extracted or formula : pushing into WHERE
set 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));
a	b	c	a	b	c
1	33	988	1	16	909
5	14	787	5	14	312
5	14	787	5	33	207
1	33	988	1	19	132
select * from v1,t2 where (v1.a=t2.a) and ((v1.b>27) or (v1.b<19));
a	b	c	a	b	c
1	33	988	1	16	909
5	14	787	5	14	312
5	14	787	5	33	207
1	33	988	1	19	132
explain select * from v1,t2 where (v1.a=t2.a) and ((v1.b>27) or (v1.b<19));
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key1,distinct_key	key1	5	test.t2.a	1	Using where
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
3	EXCEPT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
NULL	EXCEPT RESULT	<except2,3>	ALL	NULL	NULL	NULL	NULL	NULL	
explain format=json select * from v1,t2 where (v1.a=t2.a) and ((v1.b>27) or (v1.b<19));
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key1", "distinct_key"],
          "key": "key1",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t2.a"],
          "loops": 9,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v1.b > 27 or v1.b < 19",
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<except2,3>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "COST_REPLACED",
                      "having_condition": "c > 200",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a < 9 and (t1.b > 27 or t1.b < 19)"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "EXCEPT",
                      "cost": "COST_REPLACED",
                      "having_condition": "c < 300",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.b > 10 and (t1.b > 27 or t1.b < 19)"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
# using except in view definition
# extracted or formula : pushing into HAVING
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where
(v1.a=t2.a) and ((v1.c<400) or (v1.c>800));
a	b	c	a	b	c
1	33	988	1	16	909
6	20	315	6	20	211
1	33	988	1	19	132
6	20	315	6	23	303
select * from v1,t2 where
(v1.a=t2.a) and ((v1.c<400) or (v1.c>800));
a	b	c	a	b	c
1	33	988	1	16	909
6	20	315	6	20	211
1	33	988	1	19	132
6	20	315	6	23	303
explain select * from v1,t2 where
(v1.a=t2.a) and ((v1.c<400) or (v1.c>800));
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key1,distinct_key	key1	5	test.t2.a	1	Using where
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
3	EXCEPT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
NULL	EXCEPT RESULT	<except2,3>	ALL	NULL	NULL	NULL	NULL	NULL	
explain format=json select * from v1,t2 where
(v1.a=t2.a) and ((v1.c<400) or (v1.c>800));
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key1", "distinct_key"],
          "key": "key1",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t2.a"],
          "loops": 9,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v1.c < 400 or v1.c > 800",
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<except2,3>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "COST_REPLACED",
                      "having_condition": "c > 200 and (c < 400 or c > 800)",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a < 9"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "EXCEPT",
                      "cost": "COST_REPLACED",
                      "having_condition": "c < 300 and (c < 400 or c > 800)",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.b > 10"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
# using except in view definition
# extracted or formula : pushing into WHERE
# extracted or formula : pushing into HAVING using equalities
# pushing equalities
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where
(v1.c=t2.c) and
((v1.a>1) and (t2.c<500)) or
((v1.a=1) and (v1.c>500));
a	b	c	a	b	c
1	33	988	2	3	207
1	33	988	1	16	909
1	33	988	5	14	312
1	33	988	5	33	207
1	33	988	6	20	211
1	33	988	1	19	132
1	33	988	8	33	117
1	33	988	3	21	231
1	33	988	6	23	303
select * from v1,t2 where
(v1.c=t2.c) and
((v1.a>1) and (t2.c<500)) or
((v1.a=1) and (v1.c>500));
a	b	c	a	b	c
1	33	988	2	3	207
1	33	988	1	16	909
1	33	988	5	14	312
1	33	988	5	33	207
1	33	988	6	20	211
1	33	988	1	19	132
1	33	988	8	33	117
1	33	988	3	21	231
1	33	988	6	23	303
explain select * from v1,t2 where
(v1.c=t2.c) and
((v1.a>1) and (t2.c<500)) or
((v1.a=1) and (v1.c>500));
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	18	Using where; Using join buffer (flat, BNL join)
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
3	EXCEPT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
NULL	EXCEPT RESULT	<except2,3>	ALL	NULL	NULL	NULL	NULL	NULL	
explain format=json select * from v1,t2 where
(v1.c=t2.c) and
((v1.a>1) and (t2.c<500)) or
((v1.a=1) and (v1.c>500));
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived2>",
            "access_type": "ALL",
            "loops": 9,
            "rows": 18,
            "cost": "COST_REPLACED",
            "filtered": 100,
            "attached_condition": "v1.a > 1 or v1.a = 1 and v1.c > 500"
          },
          "buffer_type": "flat",
          "buffer_size": "173",
          "join_type": "BNL",
          "attached_condition": "v1.c = t2.c and v1.a > 1 and t2.c < 500 or v1.a = 1 and v1.c > 500",
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<except2,3>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "COST_REPLACED",
                      "having_condition": "c > 200 and (t1.a > 1 and c < 500 or t1.a = 1 and c > 500)",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a < 9 and (t1.a > 1 or t1.a = 1)"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "EXCEPT",
                      "cost": "COST_REPLACED",
                      "having_condition": "c < 300 and (t1.a > 1 and c < 500 or t1.a = 1 and c > 500)",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.b > 10 and (t1.a > 1 or t1.a = 1)"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
# using except in view definition
# prepare of a query
# conjunctive subformulas : pushing into WHERE
# conjunctive subformulas : pushing into HAVING
prepare stmt from "select * from v1,t2
                   where (v1.a=t2.a) and (v1.a<5) and (v1.c>500);";
execute stmt;
a	b	c	a	b	c
1	33	988	1	16	909
1	33	988	1	19	132
execute stmt;
a	b	c	a	b	c
1	33	988	1	16	909
1	33	988	1	19	132
deallocate prepare stmt;
# using except in view definition
# extracted or formula : pushing into WHERE using equalities
# extracted or formula : pushing into HAVING
# pushing equalities
set statement optimizer_switch='condition_pushdown_for_derived=off' for select *
from t2,
(select a, b, max(c) as c from t1
where t1.a<9 group by a,b having c > 200
except
select a, b, max(c) as c from t1
where t1.b>10 group by a,b having c < 300) as d1
where
(d1.b=t2.b) and
(((t2.b>13) and (t2.c=988)) or
((d1.a>4) and (d1.c>500)));
a	b	c	a	b	c
5	14	312	5	14	787
select *
from t2,
(select a, b, max(c) as c from t1
where t1.a<9 group by a,b having c > 200
except
select a, b, max(c) as c from t1
where t1.b>10 group by a,b having c < 300) as d1
where
(d1.b=t2.b) and
(((t2.b>13) and (t2.c=988)) or
((d1.a>4) and (d1.c>500)));
a	b	c	a	b	c
5	14	312	5	14	787
explain select *
from t2,
(select a, b, max(c) as c from t1
where t1.a<9 group by a,b having c > 200
except
select a, b, max(c) as c from t1
where t1.b>10 group by a,b having c < 300) as d1
where
(d1.b=t2.b) and
(((t2.b>13) and (t2.c=988)) or
((d1.a>4) and (d1.c>500)));
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key1,distinct_key	key1	5	test.t2.b	1	Using where
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
3	EXCEPT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
NULL	EXCEPT RESULT	<except2,3>	ALL	NULL	NULL	NULL	NULL	NULL	
explain format=json select *
from t2,
(select a, b, max(c) as c from t1
where t1.a<9 group by a,b having c > 200
except
select a, b, max(c) as c from t1
where t1.b>10 group by a,b having c < 300) as d1
where
(d1.b=t2.b) and
(((t2.b>13) and (t2.c=988)) or
((d1.a>4) and (d1.c>500)));
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.b is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key1", "distinct_key"],
          "key": "key1",
          "key_length": "5",
          "used_key_parts": ["b"],
          "ref": ["test.t2.b"],
          "loops": 9,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.c = 988 and t2.b > 13 or d1.a > 4 and d1.c > 500",
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<except2,3>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "COST_REPLACED",
                      "having_condition": "c > 200 and (t1.b > 13 or t1.a > 4 and c > 500)",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a < 9 and (t1.b > 13 or t1.a > 4)"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "EXCEPT",
                      "cost": "COST_REPLACED",
                      "having_condition": "c < 300 and (t1.b > 13 or t1.a > 4 and c > 500)",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.b > 10 and (t1.b > 13 or t1.a > 4)"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
drop view v1;
# using union and intersect in view definition
# conjunctive subformulas : pushing into WHERE and HAVING
create view v1 as
select a, b, min(c) as c from t1
where t1.a<9 group by a,b having c > 200
union
select a, b, max(c) as c from t1
where t1.b>10 group by a,b having c < 300
intersect
select a, b, max(c) as c from t1
where t1.a>3 group by a,b having c < 530;
set 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);
a	b	c	a	b	c
6	20	309	6	20	211
6	20	309	6	23	303
select * from v1,t2 where (v1.a=t2.a) and (v1.a>5) and (v1.c>200);
a	b	c	a	b	c
6	20	309	6	20	211
6	20	309	6	23	303
explain select * from v1,t2 where (v1.a=t2.a) and (v1.a>5) and (v1.c>200);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key1,distinct_key	key1	5	test.t2.a	3	Using where
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
3	UNION	<derived4>	ALL	NULL	NULL	NULL	NULL	18	Using where
4	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
5	INTERSECT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
NULL	INTERSECT RESULT	<intersect4,5>	ALL	NULL	NULL	NULL	NULL	NULL	
NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL	
explain format=json select * from v1,t2 where (v1.a=t2.a) and (v1.a>5) and (v1.c>200);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a > 5 and t2.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key1", "distinct_key"],
          "key": "key1",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t2.a"],
          "loops": 9,
          "rows": 3,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v1.c > 200",
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<union2,3>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "COST_REPLACED",
                      "having_condition": "c > 200 and c > 200",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a < 9 and t1.a > 5"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "UNION",
                      "cost": "COST_REPLACED",
                      "nested_loop": [
                        {
                          "table": {
                            "table_name": "<derived4>",
                            "access_type": "ALL",
                            "loops": 1,
                            "rows": 18,
                            "cost": "COST_REPLACED",
                            "filtered": 100,
                            "attached_condition": "__5.a > 5 and __5.c > 200",
                            "materialized": {
                              "query_block": {
                                "union_result": {
                                  "table_name": "<intersect4,5>",
                                  "access_type": "ALL",
                                  "query_specifications": [
                                    {
                                      "query_block": {
                                        "select_id": 4,
                                        "cost": "COST_REPLACED",
                                        "having_condition": "c < 300 and c > 200",
                                        "filesort": {
                                          "sort_key": "t1.a, t1.b",
                                          "temporary_table": {
                                            "nested_loop": [
                                              {
                                                "table": {
                                                  "table_name": "t1",
                                                  "access_type": "ALL",
                                                  "loops": 1,
                                                  "rows": 18,
                                                  "cost": "COST_REPLACED",
                                                  "filtered": 100,
                                                  "attached_condition": "t1.b > 10 and t1.a > 5"
                                                }
                                              }
                                            ]
                                          }
                                        }
                                      }
                                    },
                                    {
                                      "query_block": {
                                        "select_id": 5,
                                        "operation": "INTERSECT",
                                        "cost": "COST_REPLACED",
                                        "having_condition": "c < 530 and c > 200",
                                        "filesort": {
                                          "sort_key": "t1.a, t1.b",
                                          "temporary_table": {
                                            "nested_loop": [
                                              {
                                                "table": {
                                                  "table_name": "t1",
                                                  "access_type": "ALL",
                                                  "loops": 1,
                                                  "rows": 18,
                                                  "cost": "COST_REPLACED",
                                                  "filtered": 100,
                                                  "attached_condition": "t1.a > 3 and t1.a > 5"
                                                }
                                              }
                                            ]
                                          }
                                        }
                                      }
                                    }
                                  ]
                                }
                              }
                            }
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
drop view v1;
# using union and intersect in view definition
# conjunctive subformulas : pushing into WHERE and HAVING
create view v1 as
select a, b, min(c) as c from t1
where t1.a<9 group by a,b having c > 200
intersect
select a, b, max(c) as c from t1
where t1.a>3 group by a,b having c < 500
union
select a, b, max(c) as c from t1
where t1.b>10 group by a,b having c < 300;
set 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);
a	b	c	a	b	c
5	27	132	5	14	312
5	27	132	5	33	207
8	33	123	8	33	117
select * from v1,t2 where (v1.a=t2.a) and (v1.a>4) and (v1.c<200);
a	b	c	a	b	c
5	27	132	5	14	312
5	27	132	5	33	207
8	33	123	8	33	117
explain select * from v1,t2 where (v1.a=t2.a) and (v1.a>4) and (v1.c<200);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key1,distinct_key	key1	5	test.t2.a	3	Using where
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
3	INTERSECT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
4	UNION	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
NULL	UNIT RESULT	<unit2,3,4>	ALL	NULL	NULL	NULL	NULL	NULL	
explain format=json select * from v1,t2 where (v1.a=t2.a) and (v1.a>4) and (v1.c<200);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a > 4 and t2.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key1", "distinct_key"],
          "key": "key1",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t2.a"],
          "loops": 9,
          "rows": 3,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v1.c < 200",
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<unit2,3,4>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "COST_REPLACED",
                      "having_condition": "c > 200 and c < 200",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a < 9 and t1.a > 4"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "INTERSECT",
                      "cost": "COST_REPLACED",
                      "having_condition": "c < 500 and c < 200",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a > 3 and t1.a > 4"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 4,
                      "operation": "UNION",
                      "cost": "COST_REPLACED",
                      "having_condition": "c < 300 and c < 200",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.b > 10 and t1.a > 4"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
drop view v1;
# using union and except in view definition
# conjunctive subformulas : pushing into WHERE and HAVING
create view v1 as
select a, b, min(c) as c from t1
where t1.a<9 group by a,b having c > 200
union
select a, b, max(c) as c from t1
where t1.b>10 group by a,b having c < 300
except
select a, b, max(c) as c from t1
where t1.a>3 group by a,b having c < 530;
set 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);
a	b	c	a	b	c
6	20	309	6	20	211
6	20	309	6	23	303
select * from v1,t2 where (v1.a=t2.a) and (v1.a>5) and (v1.c>200);
a	b	c	a	b	c
6	20	309	6	20	211
6	20	309	6	23	303
explain select * from v1,t2 where (v1.a=t2.a) and (v1.a>5) and (v1.c>200);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key1,distinct_key	key1	5	test.t2.a	3	Using where
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
3	UNION	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
4	EXCEPT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
NULL	UNIT RESULT	<unit2,3,4>	ALL	NULL	NULL	NULL	NULL	NULL	
explain format=json select * from v1,t2 where (v1.a=t2.a) and (v1.a>5) and (v1.c>200);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a > 5 and t2.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key1", "distinct_key"],
          "key": "key1",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t2.a"],
          "loops": 9,
          "rows": 3,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v1.c > 200",
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<unit2,3,4>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "COST_REPLACED",
                      "having_condition": "c > 200 and c > 200",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a < 9 and t1.a > 5"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "UNION",
                      "cost": "COST_REPLACED",
                      "having_condition": "c < 300 and c > 200",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.b > 10 and t1.a > 5"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 4,
                      "operation": "EXCEPT",
                      "cost": "COST_REPLACED",
                      "having_condition": "c < 530 and c > 200",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a > 3 and t1.a > 5"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
drop view v1;
# using union and except in view definition
# conjunctive subformulas : pushing into WHERE and HAVING
create view v1 as
select a, b, min(c) as c from t1
where t1.a<9 group by a,b having c > 200
except
select a, b, max(c) as c from t1
where t1.a>3 group by a,b having c < 500
union
select a, b, max(c) as c from t1
where t1.b>10 group by a,b having c < 300;
set 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);
a	b	c	a	b	c
5	27	132	5	14	312
5	27	132	5	33	207
8	33	123	8	33	117
select * from v1,t2 where (v1.a=t2.a) and (v1.a>4) and (v1.c<200);
a	b	c	a	b	c
5	27	132	5	14	312
5	27	132	5	33	207
8	33	123	8	33	117
explain select * from v1,t2 where (v1.a=t2.a) and (v1.a>4) and (v1.c<200);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key1,distinct_key	key1	5	test.t2.a	3	Using where
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
3	EXCEPT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
4	UNION	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
NULL	UNIT RESULT	<unit2,3,4>	ALL	NULL	NULL	NULL	NULL	NULL	
explain format=json select * from v1,t2 where (v1.a=t2.a) and (v1.a>4) and (v1.c<200);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a > 4 and t2.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key1", "distinct_key"],
          "key": "key1",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t2.a"],
          "loops": 9,
          "rows": 3,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v1.c < 200",
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<unit2,3,4>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "COST_REPLACED",
                      "having_condition": "c > 200 and c < 200",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a < 9 and t1.a > 4"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "EXCEPT",
                      "cost": "COST_REPLACED",
                      "having_condition": "c < 500 and c < 200",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a > 3 and t1.a > 4"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 4,
                      "operation": "UNION",
                      "cost": "COST_REPLACED",
                      "having_condition": "c < 300 and c < 200",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.b > 10 and t1.a > 4"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
drop view v1;
# using except and intersect in view definition
# conjunctive subformulas : pushing into WHERE and HAVING
create view v1 as
select a, b, max(c) as c from t1
where t1.b>10 group by a,b having c < 300
intersect
select a, b, max(c) as c from t1
where t1.a<7 group by a,b having c < 500
except
select a, b, max(c) as c from t1
where t1.a<9 group by a,b having c > 150;
set 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);
a	b	c	a	b	c
5	27	132	5	14	312
5	27	132	5	33	207
select * from v1,t2 where (v1.a=t2.a) and (v1.a>4) and (v1.c<150);
a	b	c	a	b	c
5	27	132	5	14	312
5	27	132	5	33	207
explain select * from v1,t2 where (v1.a=t2.a) and (v1.a>4) and (v1.c<150);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key1,distinct_key	key1	5	test.t2.a	1	Using where
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
3	INTERSECT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
4	EXCEPT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
NULL	UNIT RESULT	<unit2,3,4>	ALL	NULL	NULL	NULL	NULL	NULL	
explain format=json select * from v1,t2 where (v1.a=t2.a) and (v1.a>4) and (v1.c<150);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a > 4 and t2.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key1", "distinct_key"],
          "key": "key1",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t2.a"],
          "loops": 9,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v1.c < 150",
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<unit2,3,4>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "COST_REPLACED",
                      "having_condition": "c < 300 and c < 150",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.b > 10 and t1.a > 4"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "INTERSECT",
                      "cost": "COST_REPLACED",
                      "having_condition": "c < 500 and c < 150",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a < 7 and t1.a > 4"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 4,
                      "operation": "EXCEPT",
                      "cost": "COST_REPLACED",
                      "having_condition": "c > 150 and c < 150",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a < 9 and t1.a > 4"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
drop view v1;
# using except and intersect in view definition
# conjunctive subformulas : pushing into WHERE and HAVING
create view v1 as
select a, b, max(c) as c from t1
where t1.b>10 group by a,b having c < 300
except
select a, b, max(c) as c from t1
where t1.a<9 group by a,b having c > 150
intersect
select a, b, max(c) as c from t1
where t1.a<7 group by a,b having c < 500;
set 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);
a	b	c	a	b	c
8	33	123	8	33	117
select * from v1,t2 where (v1.a=t2.a) and (v1.a>4) and (v1.c<130);
a	b	c	a	b	c
8	33	123	8	33	117
explain select * from v1,t2 where (v1.a=t2.a) and (v1.a>4) and (v1.c<130);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key1,distinct_key	key1	5	test.t2.a	1	Using where
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
3	EXCEPT	<derived4>	ALL	NULL	NULL	NULL	NULL	18	Using where
4	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
5	INTERSECT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
NULL	INTERSECT RESULT	<intersect4,5>	ALL	NULL	NULL	NULL	NULL	NULL	
NULL	EXCEPT RESULT	<except2,3>	ALL	NULL	NULL	NULL	NULL	NULL	
explain format=json select * from v1,t2 where (v1.a=t2.a) and (v1.a>4) and (v1.c<130);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a > 4 and t2.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key1", "distinct_key"],
          "key": "key1",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t2.a"],
          "loops": 9,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v1.c < 130",
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<except2,3>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "COST_REPLACED",
                      "having_condition": "c < 300 and c < 130",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.b > 10 and t1.a > 4"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "EXCEPT",
                      "cost": "COST_REPLACED",
                      "nested_loop": [
                        {
                          "table": {
                            "table_name": "<derived4>",
                            "access_type": "ALL",
                            "loops": 1,
                            "rows": 18,
                            "cost": "COST_REPLACED",
                            "filtered": 100,
                            "attached_condition": "__5.a > 4 and __5.c < 130",
                            "materialized": {
                              "query_block": {
                                "union_result": {
                                  "table_name": "<intersect4,5>",
                                  "access_type": "ALL",
                                  "query_specifications": [
                                    {
                                      "query_block": {
                                        "select_id": 4,
                                        "cost": "COST_REPLACED",
                                        "having_condition": "c > 150 and c < 130",
                                        "filesort": {
                                          "sort_key": "t1.a, t1.b",
                                          "temporary_table": {
                                            "nested_loop": [
                                              {
                                                "table": {
                                                  "table_name": "t1",
                                                  "access_type": "ALL",
                                                  "loops": 1,
                                                  "rows": 18,
                                                  "cost": "COST_REPLACED",
                                                  "filtered": 100,
                                                  "attached_condition": "t1.a < 9 and t1.a > 4"
                                                }
                                              }
                                            ]
                                          }
                                        }
                                      }
                                    },
                                    {
                                      "query_block": {
                                        "select_id": 5,
                                        "operation": "INTERSECT",
                                        "cost": "COST_REPLACED",
                                        "having_condition": "c < 500 and c < 130",
                                        "filesort": {
                                          "sort_key": "t1.a, t1.b",
                                          "temporary_table": {
                                            "nested_loop": [
                                              {
                                                "table": {
                                                  "table_name": "t1",
                                                  "access_type": "ALL",
                                                  "loops": 1,
                                                  "rows": 18,
                                                  "cost": "COST_REPLACED",
                                                  "filtered": 100,
                                                  "attached_condition": "t1.a < 7 and t1.a > 4"
                                                }
                                              }
                                            ]
                                          }
                                        }
                                      }
                                    }
                                  ]
                                }
                              }
                            }
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
drop view v1;
# using except, intersect and union in view definition
# conjunctive subformulas : pushing into WHERE and HAVING
create view v1 as
select a, b, max(c) as c from t1
where t1.b>10 group by a,b having c < 300
except
select a, b, max(c) as c from t1
where t1.a<9 group by a,b having c > 150
intersect
select a, b, max(c) as c from t1
where t1.a<7 group by a,b having c < 500
union
select a, b, max(c) as c from t1
where t1.a<7 group by a,b having c < 120;
set 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);
a	b	c	a	b	c
8	33	123	8	33	117
select * from v1,t2 where (v1.a=t2.a) and (v1.a>4) and (v1.c<130);
a	b	c	a	b	c
8	33	123	8	33	117
explain select * from v1,t2 where (v1.a=t2.a) and (v1.a>4) and (v1.c<130);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key1,distinct_key	key1	5	test.t2.a	3	Using where
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
3	EXCEPT	<derived4>	ALL	NULL	NULL	NULL	NULL	18	Using where
4	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
5	INTERSECT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
NULL	INTERSECT RESULT	<intersect4,5>	ALL	NULL	NULL	NULL	NULL	NULL	
6	UNION	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
NULL	UNIT RESULT	<unit2,3,6>	ALL	NULL	NULL	NULL	NULL	NULL	
explain format=json select * from v1,t2 where (v1.a=t2.a) and (v1.a>4) and (v1.c<130);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a > 4 and t2.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key1", "distinct_key"],
          "key": "key1",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t2.a"],
          "loops": 9,
          "rows": 3,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v1.c < 130",
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<unit2,3,6>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "COST_REPLACED",
                      "having_condition": "c < 300 and c < 130",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.b > 10 and t1.a > 4"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "EXCEPT",
                      "cost": "COST_REPLACED",
                      "nested_loop": [
                        {
                          "table": {
                            "table_name": "<derived4>",
                            "access_type": "ALL",
                            "loops": 1,
                            "rows": 18,
                            "cost": "COST_REPLACED",
                            "filtered": 100,
                            "attached_condition": "__6.a > 4 and __6.c < 130",
                            "materialized": {
                              "query_block": {
                                "union_result": {
                                  "table_name": "<intersect4,5>",
                                  "access_type": "ALL",
                                  "query_specifications": [
                                    {
                                      "query_block": {
                                        "select_id": 4,
                                        "cost": "COST_REPLACED",
                                        "having_condition": "c > 150 and c < 130",
                                        "filesort": {
                                          "sort_key": "t1.a, t1.b",
                                          "temporary_table": {
                                            "nested_loop": [
                                              {
                                                "table": {
                                                  "table_name": "t1",
                                                  "access_type": "ALL",
                                                  "loops": 1,
                                                  "rows": 18,
                                                  "cost": "COST_REPLACED",
                                                  "filtered": 100,
                                                  "attached_condition": "t1.a < 9 and t1.a > 4"
                                                }
                                              }
                                            ]
                                          }
                                        }
                                      }
                                    },
                                    {
                                      "query_block": {
                                        "select_id": 5,
                                        "operation": "INTERSECT",
                                        "cost": "COST_REPLACED",
                                        "having_condition": "c < 500 and c < 130",
                                        "filesort": {
                                          "sort_key": "t1.a, t1.b",
                                          "temporary_table": {
                                            "nested_loop": [
                                              {
                                                "table": {
                                                  "table_name": "t1",
                                                  "access_type": "ALL",
                                                  "loops": 1,
                                                  "rows": 18,
                                                  "cost": "COST_REPLACED",
                                                  "filtered": 100,
                                                  "attached_condition": "t1.a < 7 and t1.a > 4"
                                                }
                                              }
                                            ]
                                          }
                                        }
                                      }
                                    }
                                  ]
                                }
                              }
                            }
                          }
                        }
                      ]
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 6,
                      "operation": "UNION",
                      "cost": "COST_REPLACED",
                      "having_condition": "c < 120 and c < 130",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a < 7 and t1.a > 4"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
drop view v1;
# using intersect in view definition
# using embedded view
# conjunctive subformulas : pushing into WHERE and HAVING
create view v1 as
select a, b, max(c) as c from t1
where t1.b>10 group by a,b having c < 300
intersect
select a, b, max(c) as c from t1
where t1.a<9 group by a,b having c > 120;
create view v2 as
select a, b, max(c) as c from v1
where v1.a<7 group by a,b;
set 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);
a	b	c	a	b	c
5	27	132	5	14	312
5	27	132	5	33	207
select * from v2,t2 where (v2.a=t2.a) and (v2.a>4) and (v2.c<150);
a	b	c	a	b	c
5	27	132	5	14	312
5	27	132	5	33	207
explain select * from v2,t2 where (v2.a=t2.a) and (v2.a>4) and (v2.c<150);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.a	1	Using where
2	DERIVED	<derived3>	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
3	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
4	INTERSECT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
NULL	INTERSECT RESULT	<intersect3,4>	ALL	NULL	NULL	NULL	NULL	NULL	
explain format=json select * from v2,t2 where (v2.a=t2.a) and (v2.a>4) and (v2.c<150);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a > 4 and t2.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t2.a"],
          "loops": 9,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v2.c < 150",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "c < 150",
              "filesort": {
                "sort_key": "v1.a, v1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "<derived3>",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 18,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "v1.a < 7 and v1.a > 4",
                        "materialized": {
                          "query_block": {
                            "union_result": {
                              "table_name": "<intersect3,4>",
                              "access_type": "ALL",
                              "query_specifications": [
                                {
                                  "query_block": {
                                    "select_id": 3,
                                    "cost": "COST_REPLACED",
                                    "having_condition": "c < 300",
                                    "filesort": {
                                      "sort_key": "t1.a, t1.b",
                                      "temporary_table": {
                                        "nested_loop": [
                                          {
                                            "table": {
                                              "table_name": "t1",
                                              "access_type": "ALL",
                                              "loops": 1,
                                              "rows": 18,
                                              "cost": "COST_REPLACED",
                                              "filtered": 100,
                                              "attached_condition": "t1.b > 10 and t1.a < 7 and t1.a > 4"
                                            }
                                          }
                                        ]
                                      }
                                    }
                                  }
                                },
                                {
                                  "query_block": {
                                    "select_id": 4,
                                    "operation": "INTERSECT",
                                    "cost": "COST_REPLACED",
                                    "having_condition": "c > 120",
                                    "filesort": {
                                      "sort_key": "t1.a, t1.b",
                                      "temporary_table": {
                                        "nested_loop": [
                                          {
                                            "table": {
                                              "table_name": "t1",
                                              "access_type": "ALL",
                                              "loops": 1,
                                              "rows": 18,
                                              "cost": "COST_REPLACED",
                                              "filtered": 100,
                                              "attached_condition": "t1.a < 9 and t1.a < 7 and t1.a > 4"
                                            }
                                          }
                                        ]
                                      }
                                    }
                                  }
                                }
                              ]
                            }
                          }
                        }
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
drop view v1,v2;
# using except in view definition
# using embedded view
# conjunctive subformulas : pushing into WHERE and HAVING
create view v1 as
select a, b, max(c) as c from t1
where t1.b>10 group by a,b having c < 300
except
select a, b, max(c) as c from t1
where t1.a<9 group by a,b having c > 150;
create view v2 as
select a, b, max(c) as c from v1
where v1.a<7 group by a,b;
set 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);
a	b	c	a	b	c
5	27	132	5	14	312
5	27	132	5	33	207
select * from v2,t2 where (v2.a=t2.a) and (v2.a>4) and (v2.c<150);
a	b	c	a	b	c
5	27	132	5	14	312
5	27	132	5	33	207
explain select * from v2,t2 where (v2.a=t2.a) and (v2.a>4) and (v2.c<150);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.a	1	Using where
2	DERIVED	<derived3>	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
3	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
4	EXCEPT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
NULL	EXCEPT RESULT	<except3,4>	ALL	NULL	NULL	NULL	NULL	NULL	
explain format=json select * from v2,t2 where (v2.a=t2.a) and (v2.a>4) and (v2.c<150);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a > 4 and t2.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t2.a"],
          "loops": 9,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v2.c < 150",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "having_condition": "c < 150",
              "filesort": {
                "sort_key": "v1.a, v1.b",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "<derived3>",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 18,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "v1.a < 7 and v1.a > 4",
                        "materialized": {
                          "query_block": {
                            "union_result": {
                              "table_name": "<except3,4>",
                              "access_type": "ALL",
                              "query_specifications": [
                                {
                                  "query_block": {
                                    "select_id": 3,
                                    "cost": "COST_REPLACED",
                                    "having_condition": "c < 300",
                                    "filesort": {
                                      "sort_key": "t1.a, t1.b",
                                      "temporary_table": {
                                        "nested_loop": [
                                          {
                                            "table": {
                                              "table_name": "t1",
                                              "access_type": "ALL",
                                              "loops": 1,
                                              "rows": 18,
                                              "cost": "COST_REPLACED",
                                              "filtered": 100,
                                              "attached_condition": "t1.b > 10 and t1.a < 7 and t1.a > 4"
                                            }
                                          }
                                        ]
                                      }
                                    }
                                  }
                                },
                                {
                                  "query_block": {
                                    "select_id": 4,
                                    "operation": "EXCEPT",
                                    "cost": "COST_REPLACED",
                                    "having_condition": "c > 150",
                                    "filesort": {
                                      "sort_key": "t1.a, t1.b",
                                      "temporary_table": {
                                        "nested_loop": [
                                          {
                                            "table": {
                                              "table_name": "t1",
                                              "access_type": "ALL",
                                              "loops": 1,
                                              "rows": 18,
                                              "cost": "COST_REPLACED",
                                              "filtered": 100,
                                              "attached_condition": "t1.a < 9 and t1.a < 7 and t1.a > 4"
                                            }
                                          }
                                        ]
                                      }
                                    }
                                  }
                                }
                              ]
                            }
                          }
                        }
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
drop view v1,v2;
# using intersect in view definition
# conditions are pushed in different parts of selects
# conjunctive subformulas : pushing into WHERE and HAVING
create view v1 as
select a, b, max(c) as c from t1
where t1.a<9 group by a having c > 300
intersect
select a, b, max(c) as c from t1
where t1.b<21 group by b having c > 200;
set 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);
a	b	c	a	b	c
6	20	315	6	20	211
6	20	315	6	23	303
select * from v1,t2 where (v1.a=t2.a) and (v1.a>4) and (v1.b>12) and (v1.c<450);
a	b	c	a	b	c
6	20	315	6	20	211
6	20	315	6	23	303
explain select * from v1,t2 where (v1.a=t2.a) and (v1.a>4) and (v1.b>12) and (v1.c<450);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key1,distinct_key	key1	5	test.t2.a	1	Using where
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
3	INTERSECT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
NULL	INTERSECT RESULT	<intersect2,3>	ALL	NULL	NULL	NULL	NULL	NULL	
explain format=json select * from v1,t2 where (v1.a=t2.a) and (v1.a>4) and (v1.b>12) and (v1.c<450);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a > 4 and t2.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key1", "distinct_key"],
          "key": "key1",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t2.a"],
          "loops": 9,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v1.b > 12 and v1.c < 450",
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<intersect2,3>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "COST_REPLACED",
                      "having_condition": "c > 300 and t1.b > 12 and c < 450",
                      "filesort": {
                        "sort_key": "t1.a",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a < 9 and t1.a > 4"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "INTERSECT",
                      "cost": "COST_REPLACED",
                      "having_condition": "c > 200 and t1.a > 4 and c < 450",
                      "filesort": {
                        "sort_key": "t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.b < 21 and t1.b > 12"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
drop view v1;
# using except in view definition
# conditions are pushed in different parts of selects
# conjunctive subformulas : pushing into WHERE and HAVING
create view v1 as
select a, b, max(c) as c from t1
where t1.b>20 group by a having c > 300
except
select a, b, max(c) as c from t1
where t1.a<7 group by b having c > 150;
set 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);
a	b	c	a	b	c
1	21	988	1	16	909
1	21	988	1	19	132
select * from v1,t2 where (v1.a=t2.a) and (v1.a<2) and (v1.b<30) and (v1.c>450);
a	b	c	a	b	c
1	21	988	1	16	909
1	21	988	1	19	132
explain select * from v1,t2 where (v1.a=t2.a) and (v1.a<2) and (v1.b<30) and (v1.c>450);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key1,distinct_key	key1	5	test.t2.a	1	Using where
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
3	EXCEPT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
NULL	EXCEPT RESULT	<except2,3>	ALL	NULL	NULL	NULL	NULL	NULL	
explain format=json select * from v1,t2 where (v1.a=t2.a) and (v1.a<2) and (v1.b<30) and (v1.c>450);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a < 2 and t2.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key1", "distinct_key"],
          "key": "key1",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t2.a"],
          "loops": 9,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v1.b < 30 and v1.c > 450",
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<except2,3>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "COST_REPLACED",
                      "having_condition": "c > 300 and t1.b < 30 and c > 450",
                      "filesort": {
                        "sort_key": "t1.a",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.b > 20 and t1.a < 2"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "EXCEPT",
                      "cost": "COST_REPLACED",
                      "having_condition": "c > 150 and t1.a < 2 and c > 450",
                      "filesort": {
                        "sort_key": "t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a < 7 and t1.b < 30"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
drop view v1;
# using except and union in view definition
# conditions are pushed in different parts of selects
# conjunctive subformulas : pushing into HAVING
# extracted or formula : pushing into WHERE
# extracted or formula : pushing into HAVING
create view v1 as
select a, b, max(c) as c from t1
where t1.b>20 group by a having c > 300
except
select a, b, max(c) as c from t1
where t1.a<7 group by b having c > 150;
set 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);
a	b	c	a	b	c
1	21	988	1	16	909
1	21	988	1	19	132
select * from v1,t2 where (v1.a=t2.a) and ((v1.a<2) or (v1.a<5)) and (v1.c>450);
a	b	c	a	b	c
1	21	988	1	16	909
1	21	988	1	19	132
explain select * from v1,t2 where (v1.a=t2.a) and ((v1.a<2) or (v1.a<5)) and (v1.c>450);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key1,distinct_key	key1	5	test.t2.a	1	Using where
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
3	EXCEPT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
NULL	EXCEPT RESULT	<except2,3>	ALL	NULL	NULL	NULL	NULL	NULL	
explain format=json select * from v1,t2 where (v1.a=t2.a) and ((v1.a<2) or (v1.a<5)) and (v1.c>450);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "(t2.a < 2 or t2.a < 5) and t2.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key1", "distinct_key"],
          "key": "key1",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t2.a"],
          "loops": 9,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v1.c > 450",
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<except2,3>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "COST_REPLACED",
                      "having_condition": "c > 300 and c > 450",
                      "filesort": {
                        "sort_key": "t1.a",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.b > 20 and (t1.a < 2 or t1.a < 5)"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "EXCEPT",
                      "cost": "COST_REPLACED",
                      "having_condition": "c > 150 and (t1.a < 2 or t1.a < 5) and c > 450",
                      "filesort": {
                        "sort_key": "t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a < 7"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
drop view v1;
# using union and intersect in view definition
# conditions are pushed in different parts of selects
# conjunctive subformulas : pushing into WHERE and HAVING
create view v1 as
select a, b, max(c) as c from t1
where t1.a<9 group by a having c > 100
intersect
select a, b, max(c) as c from t1
where t1.a>3 group by b having c < 800
union
select a, b, max(c) as c from t1
where t1.b>10 group by a,b having c > 300;
set 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);
a	b	c	a	b	c
5	14	787	5	14	312
5	14	787	5	33	207
select * from v1,t2 where (v1.a=t2.a) and (v1.a>1) and (v1.b > 12) and (v1.c>400);
a	b	c	a	b	c
5	14	787	5	14	312
5	14	787	5	33	207
explain select * from v1,t2 where (v1.a=t2.a) and (v1.a>1) and (v1.b > 12) and (v1.c>400);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key1,distinct_key	key1	5	test.t2.a	3	Using where
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
3	INTERSECT	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
4	UNION	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using temporary; Using filesort
NULL	UNIT RESULT	<unit2,3,4>	ALL	NULL	NULL	NULL	NULL	NULL	
explain format=json select * from v1,t2 where (v1.a=t2.a) and (v1.a>1) and (v1.b > 12) and (v1.c>400);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.a > 1 and t2.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key1", "distinct_key"],
          "key": "key1",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t2.a"],
          "loops": 9,
          "rows": 3,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v1.b > 12 and v1.c > 400",
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<unit2,3,4>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "COST_REPLACED",
                      "having_condition": "c > 100 and t1.b > 12 and c > 400",
                      "filesort": {
                        "sort_key": "t1.a",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a < 9 and t1.a > 1"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "INTERSECT",
                      "cost": "COST_REPLACED",
                      "having_condition": "c < 800 and t1.a > 1 and c > 400",
                      "filesort": {
                        "sort_key": "t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.a > 3 and t1.b > 12"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 4,
                      "operation": "UNION",
                      "cost": "COST_REPLACED",
                      "having_condition": "c > 300 and c > 400",
                      "filesort": {
                        "sort_key": "t1.a, t1.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 18,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.b > 10 and t1.a > 1 and t1.b > 12"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
drop view v1;
create table t3 (a int, b int, c int);
insert into t3 values
(1,21,345), (2,33,7), (8,33,114), (3,21,500), (1,19,107), (5,14,787),
(4,33,123), (9,10,211), (11,16,207), (10,33,988), (5,27,132), (12,21,104),
(6,20,309), (16,20,315), (16,21,101), (18,33,404), (19,10,800), (10,21,123),
(17,11,708), (6,20,214);
create index i1 on t3(a);
# conjunctive subformulas : pushing into WHERE
# pushed condition gives range access
create view v1 as
select a, b, max(c) as max_c from t3
where a>0 group by a;
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.b=t2.b) and (v1.a<5);
a	b	max_c	a	b	c
4	33	123	5	33	207
2	33	7	5	33	207
4	33	123	8	33	117
2	33	7	8	33	117
3	21	500	3	21	231
1	21	345	3	21	231
select * from v1,t2 where (v1.b=t2.b) and (v1.a<5);
a	b	max_c	a	b	c
4	33	123	5	33	207
2	33	7	5	33	207
4	33	123	8	33	117
2	33	7	8	33	117
3	21	500	3	21	231
1	21	345	3	21	231
explain select * from v1,t2 where (v1.b=t2.b) and (v1.a<5);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.b	1	Using where
2	DERIVED	t3	range	i1	i1	5	NULL	5	Using index condition
explain format=json select * from v1,t2 where (v1.b=t2.b) and (v1.a<5);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.b is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "5",
          "used_key_parts": ["b"],
          "ref": ["test.t2.b"],
          "loops": 9,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v1.a < 5",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "nested_loop": [
                {
                  "table": {
                    "table_name": "t3",
                    "access_type": "range",
                    "possible_keys": ["i1"],
                    "key": "i1",
                    "key_length": "5",
                    "used_key_parts": ["a"],
                    "loops": 1,
                    "rows": 5,
                    "cost": "COST_REPLACED",
                    "filtered": 100,
                    "index_condition": "t3.a > 0 and t3.a < 5"
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}
drop view v1;
# using union in view definition
# conjunctive subformulas : pushing into WHERE
# pushed condition gives range access
create view v1 as
select a, b, max(c) as c from t3
where t3.a>1 group by a
union
select a, b, max(c) as c from t3
where t3.a>2 group by a;
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.b=t2.b) and (v1.a<4);
a	b	c	a	b	c
2	33	7	5	33	207
2	33	7	8	33	117
3	21	500	3	21	231
select * from v1,t2 where (v1.b=t2.b) and (v1.a<4);
a	b	c	a	b	c
2	33	7	5	33	207
2	33	7	8	33	117
3	21	500	3	21	231
explain select * from v1,t2 where (v1.b=t2.b) and (v1.a<4);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key1,distinct_key	key1	5	test.t2.b	1	Using where
2	DERIVED	t3	range	i1	i1	5	NULL	2	Using index condition
3	UNION	t3	range	i1	i1	5	NULL	1	Using index condition
NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL	
explain format=json select * from v1,t2 where (v1.b=t2.b) and (v1.a<4);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.b is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key1", "distinct_key"],
          "key": "key1",
          "key_length": "5",
          "used_key_parts": ["b"],
          "ref": ["test.t2.b"],
          "loops": 9,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v1.a < 4",
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<union2,3>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "COST_REPLACED",
                      "nested_loop": [
                        {
                          "table": {
                            "table_name": "t3",
                            "access_type": "range",
                            "possible_keys": ["i1"],
                            "key": "i1",
                            "key_length": "5",
                            "used_key_parts": ["a"],
                            "loops": 1,
                            "rows": 2,
                            "cost": "COST_REPLACED",
                            "filtered": 100,
                            "index_condition": "t3.a > 1 and t3.a < 4"
                          }
                        }
                      ]
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "UNION",
                      "cost": "COST_REPLACED",
                      "nested_loop": [
                        {
                          "table": {
                            "table_name": "t3",
                            "access_type": "range",
                            "possible_keys": ["i1"],
                            "key": "i1",
                            "key_length": "5",
                            "used_key_parts": ["a"],
                            "loops": 1,
                            "rows": 1,
                            "cost": "COST_REPLACED",
                            "filtered": 100,
                            "index_condition": "t3.a > 2 and t3.a < 4"
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
drop view v1;
# using union in view definition
# conjunctive subformulas : pushing into WHERE
# pushed condition gives range access in one of the selects
create view v1 as
select a, b, max(c) as c from t3
where t3.a>1 group by a
union
select a, b, max(c) as c from t3
where t3.b<21 group by b;
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from v1,t2 where (v1.b=t2.b) and (v1.a<3);
a	b	c	a	b	c
2	33	7	5	33	207
1	19	107	1	19	132
2	33	7	8	33	117
select * from v1,t2 where (v1.b=t2.b) and (v1.a<3);
a	b	c	a	b	c
2	33	7	5	33	207
1	19	107	1	19	132
2	33	7	8	33	117
explain select * from v1,t2 where (v1.b=t2.b) and (v1.a<3);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
1	PRIMARY	<derived2>	ref	key1,distinct_key	key1	5	test.t2.b	2	Using where
2	DERIVED	t3	range	i1	i1	5	NULL	1	Using index condition
3	UNION	t3	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL	
explain format=json select * from v1,t2 where (v1.b=t2.b) and (v1.a<3);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 9,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t2.b is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key1", "distinct_key"],
          "key": "key1",
          "key_length": "5",
          "used_key_parts": ["b"],
          "ref": ["test.t2.b"],
          "loops": 9,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "v1.a < 3",
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<union2,3>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "COST_REPLACED",
                      "nested_loop": [
                        {
                          "table": {
                            "table_name": "t3",
                            "access_type": "range",
                            "possible_keys": ["i1"],
                            "key": "i1",
                            "key_length": "5",
                            "used_key_parts": ["a"],
                            "loops": 1,
                            "rows": 1,
                            "cost": "COST_REPLACED",
                            "filtered": 100,
                            "index_condition": "t3.a > 1 and t3.a < 3"
                          }
                        }
                      ]
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "UNION",
                      "cost": "COST_REPLACED",
                      "having_condition": "t3.a < 3",
                      "filesort": {
                        "sort_key": "t3.b",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t3",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 20,
                                "cost": "COST_REPLACED",
                                "filtered": 100,
                                "attached_condition": "t3.b < 21"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
drop view v1;
alter table t3 drop index i1;
drop table t1,t2,t3;
#
# MDEV-10855: Pushdown into derived with window functions
#
set @save_optimizer_switch= @@optimizer_switch;
set optimizer_switch='split_materialized=off';
create table t1 (a int, c varchar(16));
insert into t1 values
(8,'aa'), (5,'cc'), (1,'bb'), (2,'aa'), (9,'cc'),
(7,'aa'), (2,'aa'), (7,'bb');
create table t2 (a int, b int, c varchar(16), index idx(a,c));
insert into t2 values
(7,10,'cc'), (1,20,'aa'), (2,23,'bb'), (7,18,'cc'), (1,30,'bb'),
(4,71,'xx'), (3,15,'aa'), (7,82,'bb'), (8,12,'dd'), (4,15,'aa'),
(11,33,'yy'), (10,42,'zz'), (4,53,'xx'), (10,17,'yy'), (7,12,'bb'),
(8,20,'dd'), (7,32,'bb'), (1,50,'aa'), (3,40,'bb'), (3,77,'aa');
set 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
where t.a > 2 and t.c in ('aa','bb','cc');
a	c	sum(b) over (partition by a,c)
3	aa	92
3	aa	92
3	bb	40
4	aa	15
7	bb	126
7	bb	126
7	bb	126
7	cc	28
7	cc	28
select * from (select a, c, sum(b) over (partition by a,c) from t2) as t
where t.a > 2 and t.c in ('aa','bb','cc');
a	c	sum(b) over (partition by a,c)
3	aa	92
3	aa	92
3	bb	40
4	aa	15
7	bb	126
7	bb	126
7	bb	126
7	cc	28
7	cc	28
explain select * from (select a, c, sum(b) over (partition by a,c) from t2) as t
where t.a > 2 and t.c in ('aa','bb','cc');
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	16	Using where
2	DERIVED	t2	ALL	idx	NULL	NULL	NULL	20	Using where; Using temporary
explain format=json select * from (select a, c, sum(b) over (partition by a,c) from t2) as t
where t.a > 2 and t.c in ('aa','bb','cc');
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 16,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t.a > 2 and t.c in ('aa','bb','cc')",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "window_functions_computation": {
                "sorts": [
                  {
                    "filesort": {
                      "sort_key": "t2.a, t2.c"
                    }
                  }
                ],
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t2",
                        "access_type": "ALL",
                        "possible_keys": ["idx"],
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 80,
                        "attached_condition": "t2.a > 2 and t2.c in ('aa','bb','cc')"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from
(
select 1 as n, a, c, sum(b) over (partition by a,c) as s from t2
union all
select 2 as n, a, c, sum(b) over (partition by a) as s from t2
) as t
where t.a > 2 and t.c in ('aa','bb','cc');
n	a	c	s
1	3	aa	92
1	3	aa	92
1	3	bb	40
1	4	aa	15
1	7	bb	126
1	7	bb	126
1	7	bb	126
1	7	cc	28
1	7	cc	28
2	3	aa	132
2	3	aa	132
2	3	bb	132
2	4	aa	139
2	7	bb	154
2	7	bb	154
2	7	bb	154
2	7	cc	154
2	7	cc	154
select * from
(
select 1 as n, a, c, sum(b) over (partition by a,c) as s from t2
union all
select 2 as n, a, c, sum(b) over (partition by a) as s from t2
) as t
where t.a > 2 and t.c in ('aa','bb','cc');
n	a	c	s
1	3	aa	92
1	3	aa	92
1	3	bb	40
1	4	aa	15
1	7	bb	126
1	7	bb	126
1	7	bb	126
1	7	cc	28
1	7	cc	28
2	3	aa	132
2	3	aa	132
2	3	bb	132
2	4	aa	139
2	7	bb	154
2	7	bb	154
2	7	bb	154
2	7	cc	154
2	7	cc	154
explain select * from
(
select 1 as n, a, c, sum(b) over (partition by a,c) as s from t2
union all
select 2 as n, a, c, sum(b) over (partition by a) as s from t2
) as t
where t.a > 2 and t.c in ('aa','bb','cc');
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	32	Using where
2	DERIVED	t2	ALL	idx	NULL	NULL	NULL	20	Using where; Using temporary
3	UNION	t2	ALL	idx	NULL	NULL	NULL	20	Using where; Using temporary
explain format=json select * from
(
select 1 as n, a, c, sum(b) over (partition by a,c) as s from t2
union all
select 2 as n, a, c, sum(b) over (partition by a) as s from t2
) as t
where t.a > 2 and t.c in ('aa','bb','cc');
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 32,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t.a > 2 and t.c in ('aa','bb','cc')",
          "materialized": {
            "query_block": {
              "union_result": {
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "COST_REPLACED",
                      "window_functions_computation": {
                        "sorts": [
                          {
                            "filesort": {
                              "sort_key": "t2.a, t2.c"
                            }
                          }
                        ],
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t2",
                                "access_type": "ALL",
                                "possible_keys": ["idx"],
                                "loops": 1,
                                "rows": 20,
                                "cost": "COST_REPLACED",
                                "filtered": 80,
                                "attached_condition": "t2.a > 2 and t2.c in ('aa','bb','cc')"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "UNION",
                      "cost": "COST_REPLACED",
                      "window_functions_computation": {
                        "sorts": [
                          {
                            "filesort": {
                              "sort_key": "t2.a"
                            }
                          }
                        ],
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t2",
                                "access_type": "ALL",
                                "possible_keys": ["idx"],
                                "loops": 1,
                                "rows": 20,
                                "cost": "COST_REPLACED",
                                "filtered": 80,
                                "attached_condition": "t2.a > 2"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
set statement optimizer_switch='condition_pushdown_for_derived=off' for select *
from (select a, c, sum(b) over (partition by a,c) as s from t2) as t, t1
where t1.a=t.a and t1.c=t.c and t1.c in ('aa','bb','cc');
a	c	s	a	c
1	bb	30	1	bb
7	bb	126	7	bb
7	bb	126	7	bb
7	bb	126	7	bb
select *
from (select a, c, sum(b) over (partition by a,c) as s from t2) as t, t1
where t1.a=t.a and t1.c=t.c and t1.c in ('aa','bb','cc');
a	c	s	a	c
1	bb	30	1	bb
7	bb	126	7	bb
7	bb	126	7	bb
7	bb	126	7	bb
explain select *
from (select a, c, sum(b) over (partition by a,c) as s from t2) as t, t1
where t1.a=t.a and t1.c=t.c and t1.c in ('aa','bb','cc');
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	8	Using where
1	PRIMARY	<derived2>	ref	key0	key0	24	test.t1.a,test.t1.c	2	
2	DERIVED	t2	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary
explain format=json select *
from (select a, c, sum(b) over (partition by a,c) as s from t2) as t, t1
where t1.a=t.a and t1.c=t.c and t1.c in ('aa','bb','cc');
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t1",
          "access_type": "ALL",
          "loops": 1,
          "rows": 8,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t1.c in ('aa','bb','cc') and t1.a is not null and t1.c is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "24",
          "used_key_parts": ["a", "c"],
          "ref": ["test.t1.a", "test.t1.c"],
          "loops": 8,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "window_functions_computation": {
                "sorts": [
                  {
                    "filesort": {
                      "sort_key": "t2.a, t2.c"
                    }
                  }
                ],
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t2",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t2.c in ('aa','bb','cc')"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from
(
select 1 as n, a, c, sum(b) over (partition by a,c) as s from t2
union all
select 2 as n, a, c, sum(b) over (partition by a) as s from t2
union all
select 3 as n, a, c, sum(b) as s from t2 group by a
) as t
where t.a > 2 and t.c in ('aa','bb','cc');
n	a	c	s
1	3	aa	92
1	3	aa	92
1	3	bb	40
1	4	aa	15
1	7	bb	126
1	7	bb	126
1	7	bb	126
1	7	cc	28
1	7	cc	28
2	3	aa	132
2	3	aa	132
2	3	bb	132
2	4	aa	139
2	7	bb	154
2	7	bb	154
2	7	bb	154
2	7	cc	154
2	7	cc	154
3	3	aa	132
3	7	cc	154
select * from
(
select 1 as n, a, c, sum(b) over (partition by a,c) as s from t2
union all
select 2 as n, a, c, sum(b) over (partition by a) as s from t2
union all
select 3 as n, a, c, sum(b) as s from t2 group by a
) as t
where t.a > 2 and t.c in ('aa','bb','cc');
n	a	c	s
1	3	aa	92
1	3	aa	92
1	3	bb	40
1	4	aa	15
1	7	bb	126
1	7	bb	126
1	7	bb	126
1	7	cc	28
1	7	cc	28
2	3	aa	132
2	3	aa	132
2	3	bb	132
2	4	aa	139
2	7	bb	154
2	7	bb	154
2	7	bb	154
2	7	cc	154
2	7	cc	154
3	3	aa	132
3	7	cc	154
explain select * from
(
select 1 as n, a, c, sum(b) over (partition by a,c) as s from t2
union all
select 2 as n, a, c, sum(b) over (partition by a) as s from t2
union all
select 3 as n, a, c, sum(b) as s from t2 group by a
) as t
where t.a > 2 and t.c in ('aa','bb','cc');
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	48	Using where
2	DERIVED	t2	ALL	idx	NULL	NULL	NULL	20	Using where; Using temporary
3	UNION	t2	ALL	idx	NULL	NULL	NULL	20	Using where; Using temporary
4	UNION	t2	ALL	idx	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
explain format=json select * from
(
select 1 as n, a, c, sum(b) over (partition by a,c) as s from t2
union all
select 2 as n, a, c, sum(b) over (partition by a) as s from t2
union all
select 3 as n, a, c, sum(b) as s from t2 group by a
) as t
where t.a > 2 and t.c in ('aa','bb','cc');
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 48,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t.a > 2 and t.c in ('aa','bb','cc')",
          "materialized": {
            "query_block": {
              "union_result": {
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "COST_REPLACED",
                      "window_functions_computation": {
                        "sorts": [
                          {
                            "filesort": {
                              "sort_key": "t2.a, t2.c"
                            }
                          }
                        ],
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t2",
                                "access_type": "ALL",
                                "possible_keys": ["idx"],
                                "loops": 1,
                                "rows": 20,
                                "cost": "COST_REPLACED",
                                "filtered": 80,
                                "attached_condition": "t2.a > 2 and t2.c in ('aa','bb','cc')"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "UNION",
                      "cost": "COST_REPLACED",
                      "window_functions_computation": {
                        "sorts": [
                          {
                            "filesort": {
                              "sort_key": "t2.a"
                            }
                          }
                        ],
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t2",
                                "access_type": "ALL",
                                "possible_keys": ["idx"],
                                "loops": 1,
                                "rows": 20,
                                "cost": "COST_REPLACED",
                                "filtered": 80,
                                "attached_condition": "t2.a > 2"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 4,
                      "operation": "UNION",
                      "cost": "COST_REPLACED",
                      "having_condition": "t2.c in ('aa','bb','cc')",
                      "filesort": {
                        "sort_key": "t2.a",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t2",
                                "access_type": "ALL",
                                "possible_keys": ["idx"],
                                "loops": 1,
                                "rows": 20,
                                "cost": "COST_REPLACED",
                                "filtered": 80,
                                "attached_condition": "t2.a > 2"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from (select a, c,
sum(b) over (partition by a,c) as sum_b,
avg(b) over (partition by a,c) as avg_b
from t2 ) as t
where t.a > 2 and t.c in ('aa','bb','cc');
a	c	sum_b	avg_b
3	aa	92	46.0000
3	aa	92	46.0000
3	bb	40	40.0000
4	aa	15	15.0000
7	bb	126	42.0000
7	bb	126	42.0000
7	bb	126	42.0000
7	cc	28	14.0000
7	cc	28	14.0000
select * from (select a, c,
sum(b) over (partition by a,c) as sum_b,
avg(b) over (partition by a,c) as avg_b
from t2 ) as t
where t.a > 2 and t.c in ('aa','bb','cc');
a	c	sum_b	avg_b
3	aa	92	46.0000
3	aa	92	46.0000
3	bb	40	40.0000
4	aa	15	15.0000
7	bb	126	42.0000
7	bb	126	42.0000
7	bb	126	42.0000
7	cc	28	14.0000
7	cc	28	14.0000
explain select * from (select a, c,
sum(b) over (partition by a,c) as sum_b,
avg(b) over (partition by a,c) as avg_b
from t2 ) as t
where t.a > 2 and t.c in ('aa','bb','cc');
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	16	Using where
2	DERIVED	t2	ALL	idx	NULL	NULL	NULL	20	Using where; Using temporary
explain format=json select * from (select a, c,
sum(b) over (partition by a,c) as sum_b,
avg(b) over (partition by a,c) as avg_b
from t2 ) as t
where t.a > 2 and t.c in ('aa','bb','cc');
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 16,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t.a > 2 and t.c in ('aa','bb','cc')",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "window_functions_computation": {
                "sorts": [
                  {
                    "filesort": {
                      "sort_key": "t2.a, t2.c"
                    }
                  }
                ],
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t2",
                        "access_type": "ALL",
                        "possible_keys": ["idx"],
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 80,
                        "attached_condition": "t2.a > 2 and t2.c in ('aa','bb','cc')"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from (select a, c,
sum(b) over (partition by a,c) as sum_b,
avg(b) over (partition by a) as avg_b
from t2 ) as t
where t.a > 2 and t.c in ('aa','bb','cc');
a	c	sum_b	avg_b
3	aa	92	44.0000
3	aa	92	44.0000
3	bb	40	44.0000
4	aa	15	46.3333
7	bb	126	30.8000
7	bb	126	30.8000
7	bb	126	30.8000
7	cc	28	30.8000
7	cc	28	30.8000
select * from (select a, c,
sum(b) over (partition by a,c) as sum_b,
avg(b) over (partition by a) as avg_b
from t2 ) as t
where t.a > 2 and t.c in ('aa','bb','cc');
a	c	sum_b	avg_b
3	aa	92	44.0000
3	aa	92	44.0000
3	bb	40	44.0000
4	aa	15	46.3333
7	bb	126	30.8000
7	bb	126	30.8000
7	bb	126	30.8000
7	cc	28	30.8000
7	cc	28	30.8000
explain select * from (select a, c,
sum(b) over (partition by a,c) as sum_b,
avg(b) over (partition by a) as avg_b
from t2 ) as t
where t.a > 2 and t.c in ('aa','bb','cc');
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	16	Using where
2	DERIVED	t2	ALL	idx	NULL	NULL	NULL	20	Using where; Using temporary
explain format=json select * from (select a, c,
sum(b) over (partition by a,c) as sum_b,
avg(b) over (partition by a) as avg_b
from t2 ) as t
where t.a > 2 and t.c in ('aa','bb','cc');
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 16,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t.a > 2 and t.c in ('aa','bb','cc')",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "window_functions_computation": {
                "sorts": [
                  {
                    "filesort": {
                      "sort_key": "t2.a, t2.c"
                    }
                  }
                ],
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t2",
                        "access_type": "ALL",
                        "possible_keys": ["idx"],
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 80,
                        "attached_condition": "t2.a > 2"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
set statement optimizer_switch='condition_pushdown_for_derived=off' for select * from (select a, c,
sum(b) over (partition by a,c) as sum_b,
avg(b) over (partition by c) as avg_b
from t2 ) as t
where t.a > 2 and t.c in ('aa','bb','cc');
a	c	sum_b	avg_b
3	aa	92	35.4000
3	aa	92	35.4000
3	bb	40	36.5000
4	aa	15	35.4000
7	bb	126	36.5000
7	bb	126	36.5000
7	bb	126	36.5000
7	cc	28	14.0000
7	cc	28	14.0000
select * from (select a, c,
sum(b) over (partition by a,c) as sum_b,
avg(b) over (partition by c) as avg_b
from t2 ) as t
where t.a > 2 and t.c in ('aa','bb','cc');
a	c	sum_b	avg_b
3	aa	92	35.4000
3	aa	92	35.4000
3	bb	40	36.5000
4	aa	15	35.4000
7	bb	126	36.5000
7	bb	126	36.5000
7	bb	126	36.5000
7	cc	28	14.0000
7	cc	28	14.0000
explain select * from (select a, c,
sum(b) over (partition by a,c) as sum_b,
avg(b) over (partition by c) as avg_b
from t2 ) as t
where t.a > 2 and t.c in ('aa','bb','cc');
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	20	Using where
2	DERIVED	t2	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary
explain format=json select * from (select a, c,
sum(b) over (partition by a,c) as sum_b,
avg(b) over (partition by c) as avg_b
from t2 ) as t
where t.a > 2 and t.c in ('aa','bb','cc');
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 20,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t.a > 2 and t.c in ('aa','bb','cc')",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "window_functions_computation": {
                "sorts": [
                  {
                    "filesort": {
                      "sort_key": "t2.a, t2.c"
                    }
                  },
                  {
                    "filesort": {
                      "sort_key": "t2.c"
                    }
                  }
                ],
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t2",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 20,
                        "cost": "COST_REPLACED",
                        "filtered": 100,
                        "attached_condition": "t2.c in ('aa','bb','cc')"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
drop table t1,t2;
set optimizer_switch= @save_optimizer_switch;
#
# MDEV-13369: Optimization for equi-joins of grouping derived tables
#             (Splitting derived tables / views with GROUP BY)
# MDEV-13389: Optimization for equi-joins of derived tables with WF
#             (Splitting derived tables / views with window functions)
#
create table t1 (a int, b int, index idx_b(b)) engine=myisam;
insert into t1 values
(8,3), (5,7), (1,2), (2,1), (9,7), (7,5), (2,2), (7,3),
(9,3), (8,1), (4,5), (2,3);
create table t2 (a int, b int, c char(127), index idx_a(a)) engine=myisam;
insert into t2 values
(7,10,'x'), (1,20,'a'), (2,23,'b'), (7,18,'z'), (1,30,'c'),
(4,71,'d'), (3,15,'x'), (7,82,'y'), (8,12,'t'), (4,15,'b'),
(11,33,'a'), (10,42,'u'), (4,53,'p'), (10,17,'r'), (2,90,'x'),
(17,10,'s'), (11,20,'v'), (12,23,'y'), (17,18,'a'), (11,30,'d'),
(24,71,'h'), (23,15,'i'), (27,82,'k'), (28,12,'p'), (24,15,'q'),
(31,33,'f'), (30,42,'h'), (40,53,'m'), (30,17,'o'), (21,90,'b'),
(37,10,'e'), (31,20,'g'), (32,23,'f'), (37,18,'n'), (41,30,'l'),
(54,71,'j'), (53,15,'w'), (57,82,'z'), (58,12,'k'), (54,15,'p'),
(61,33,'c'), (60,42,'a'), (62,53,'x'), (67,17,'g'), (64,90,'v');
insert into t2 select a+10, b+10, concat(c,'f') from t2;
analyze table t1,t2;
Table	Op	Msg_type	Msg_text
test.t1	analyze	status	Engine-independent statistics collected
test.t1	analyze	status	OK
test.t2	analyze	status	Engine-independent statistics collected
test.t2	analyze	status	OK
set statement optimizer_switch='split_materialized=off' for select t1.a,t.s,t.m
from t1 join
(select a, sum(t2.b) as s, min(t2.c) as m from t2 group by t2.a) t
on t1.a=t.a
where t1.b < 3;
a	s	m
2	113	b
8	12	t
1	50	a
2	113	b
select t1.a,t.s,t.m
from t1 join
(select a, sum(t2.b) as s, min(t2.c) as m from t2 group by t2.a) t
on t1.a=t.a
where t1.b < 3;
a	s	m
2	113	b
8	12	t
1	50	a
2	113	b
explain extended select t1.a,t.s,t.m
from t1 join
(select a, sum(t2.b) as s, min(t2.c) as m from t2 group by t2.a) t
on t1.a=t.a
where t1.b < 3;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	PRIMARY	t1	range	idx_b	idx_b	5	NULL	4	100.00	Using index condition; Using where
1	PRIMARY	<derived2>	ref	key0	key0	5	test.t1.a	1	100.00	
2	LATERAL DERIVED	t2	ref	idx_a	idx_a	5	test.t1.a	1	100.00	
Warnings:
Note	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
explain format=json select t1.a,t.s,t.m
from t1 join
(select a, sum(t2.b) as s, min(t2.c) as m from t2 group by t2.a) t
on t1.a=t.a
where t1.b < 3;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t1",
          "access_type": "range",
          "possible_keys": ["idx_b"],
          "key": "idx_b",
          "key_length": "5",
          "used_key_parts": ["b"],
          "loops": 1,
          "rows": 4,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "index_condition": "t1.b < 3",
          "attached_condition": "t1.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t1.a"],
          "loops": 4,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "materialized": {
            "lateral": 1,
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "outer_ref_condition": "t1.a is not null",
              "nested_loop": [
                {
                  "table": {
                    "table_name": "t2",
                    "access_type": "ref",
                    "possible_keys": ["idx_a"],
                    "key": "idx_a",
                    "key_length": "5",
                    "used_key_parts": ["a"],
                    "ref": ["test.t1.a"],
                    "loops": 1,
                    "rows": 1,
                    "cost": "COST_REPLACED",
                    "filtered": 100
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}
prepare stmt from "select t1.a,t.s,t.m
from t1 join
(select a, sum(t2.b) as s, min(t2.c) as m from t2 group by t2.a) t
on t1.a=t.a
where t1.b < 3";
execute stmt;
a	s	m
2	113	b
8	12	t
1	50	a
2	113	b
execute stmt;
a	s	m
2	113	b
8	12	t
1	50	a
2	113	b
deallocate prepare stmt;
set statement optimizer_switch='split_materialized=off' for select t1.a,t.s,t.m
from t1 join
(select a, sum(t2.b) as s, min(t2.b) as m from t2 group by t2.a) t
on t1.a=t.a
where t1.b <= 5;
a	s	m
8	12	12
1	50	20
2	113	23
7	110	10
2	113	23
7	110	10
8	12	12
4	139	15
2	113	23
select t1.a,t.s,t.m
from t1 join
(select a, sum(t2.b) as s, min(t2.b) as m from t2 group by t2.a) t
on t1.a=t.a
where t1.b <= 5;
a	s	m
8	12	12
1	50	20
2	113	23
7	110	10
2	113	23
7	110	10
8	12	12
4	139	15
2	113	23
explain extended select t1.a,t.s,t.m
from t1 join
(select a, sum(t2.b) as s, min(t2.b) as m from t2 group by t2.a) t
on t1.a=t.a
where t1.b <= 5;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	PRIMARY	t1	ALL	idx_b	NULL	NULL	NULL	12	83.33	Using where
1	PRIMARY	<derived2>	ref	key0	key0	5	test.t1.a	1	100.00	
2	LATERAL DERIVED	t2	ref	idx_a	idx_a	5	test.t1.a	1	100.00	
Warnings:
Note	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
explain format=json select t1.a,t.s,t.m
from t1 join
(select a, sum(t2.b) as s, min(t2.b) as m from t2 group by t2.a) t
on t1.a=t.a
where t1.b <= 5;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t1",
          "access_type": "ALL",
          "possible_keys": ["idx_b"],
          "loops": 1,
          "rows": 12,
          "cost": "COST_REPLACED",
          "filtered": 83.33333588,
          "attached_condition": "t1.b <= 5 and t1.a is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t1.a"],
          "loops": 10,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "materialized": {
            "lateral": 1,
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "outer_ref_condition": "t1.a is not null",
              "nested_loop": [
                {
                  "table": {
                    "table_name": "t2",
                    "access_type": "ref",
                    "possible_keys": ["idx_a"],
                    "key": "idx_a",
                    "key_length": "5",
                    "used_key_parts": ["a"],
                    "ref": ["test.t1.a"],
                    "loops": 1,
                    "rows": 1,
                    "cost": "COST_REPLACED",
                    "filtered": 100
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}
prepare stmt from "select t1.a,t.s,t.m
from t1 join
(select a, sum(t2.b) as s, min(t2.b) as m from t2 group by t2.a) t
on t1.a=t.a
where t1.b <= 5";
execute stmt;
a	s	m
8	12	12
1	50	20
2	113	23
7	110	10
2	113	23
7	110	10
8	12	12
4	139	15
2	113	23
execute stmt;
a	s	m
8	12	12
1	50	20
2	113	23
7	110	10
2	113	23
7	110	10
8	12	12
4	139	15
2	113	23
deallocate prepare stmt;
delete from t1 where t1.b between 2 and 5;
set statement optimizer_switch='split_materialized=off' for select t1.a,t.max,t.min
from t1 left join
(select a, max(t2.b) max, min(t2.b) min from t2 group by t2.a) t
on t1.a=t.a;
a	max	min
5	NULL	NULL
2	90	23
9	NULL	NULL
8	12	12
select t1.a,t.max,t.min
from t1 left join
(select a, max(t2.b) max, min(t2.b) min from t2 group by t2.a) t
on t1.a=t.a;
a	max	min
5	NULL	NULL
2	90	23
9	NULL	NULL
8	12	12
explain extended select t1.a,t.max,t.min
from t1 left join
(select a, max(t2.b) max, min(t2.b) min from t2 group by t2.a) t
on t1.a=t.a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	12	100.00	
1	PRIMARY	<derived2>	ref	key0	key0	5	test.t1.a	1	100.00	Using where
2	LATERAL DERIVED	t2	ref	idx_a	idx_a	5	test.t1.a	1	100.00	
Warnings:
Note	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` where `test`.`t2`.`a` = `test`.`t1`.`a` group by `test`.`t2`.`a`) `t` on(`t`.`a` = `test`.`t1`.`a` and `test`.`t1`.`a` is not null) where 1
explain format=json select t1.a,t.max,t.min
from t1 left join
(select a, max(t2.b) max, min(t2.b) min from t2 group by t2.a) t
on t1.a=t.a;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "const_condition": "1",
    "nested_loop": [
      {
        "table": {
          "table_name": "t1",
          "access_type": "ALL",
          "loops": 1,
          "rows": 12,
          "cost": "COST_REPLACED",
          "filtered": 100
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t1.a"],
          "loops": 12,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "trigcond(trigcond(t1.a is not null))",
          "materialized": {
            "lateral": 1,
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "outer_ref_condition": "t1.a is not null",
              "nested_loop": [
                {
                  "table": {
                    "table_name": "t2",
                    "access_type": "ref",
                    "possible_keys": ["idx_a"],
                    "key": "idx_a",
                    "key_length": "5",
                    "used_key_parts": ["a"],
                    "ref": ["test.t1.a"],
                    "loops": 1,
                    "rows": 1,
                    "cost": "COST_REPLACED",
                    "filtered": 100
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}
create table t3 (a int, b int, c char(127), index idx_b(b)) engine=myisam;
insert into t3 values
(8,11,'aa'), (5,15,'cc'), (1,14,'bb'), (2,12,'aa'), (7,17,'cc'),
(7,18,'aa'), (2,11,'aa'), (7,10,'bb'), (3,11,'dd'), (4,12,'ee'),
(5,14,'dd'), (9,12,'ee');
create table t4 (a int, b int, c char(127), index idx(a,c)) engine=myisam;
insert into t4 values
(7,10,'cc'), (1,20,'aa'), (2,23,'bb'), (7,18,'cc'), (1,30,'bb'),
(4,71,'xx'), (3,15,'aa'), (7,82,'aa'), (8,12,'dd'), (4,15,'aa'),
(11,33,'yy'), (10,42,'zz'), (4,53,'xx'), (10,17,'yy'), (7,12,'cc'),
(8,20,'dd'), (7,32,'bb'), (1,50,'aa'), (3,40,'bb'), (3,77,'aa');
insert into t4 select a+10, b+10, concat(c,'f') from t4;
analyze table t3,t4;
Table	Op	Msg_type	Msg_text
test.t3	analyze	status	Engine-independent statistics collected
test.t3	analyze	status	OK
test.t4	analyze	status	Engine-independent statistics collected
test.t4	analyze	status	OK
set statement optimizer_switch='split_materialized=off' for select t3.a,t3.c,t.max,t.min
from t3 join
(select a, c, max(b) max, min(b) min from t4 group by a,c) t
on t3.a=t.a and t3.c=t.c
where t3.b > 15;
a	c	max	min
7	cc	18	10
7	aa	82	82
select t3.a,t3.c,t.max,t.min
from t3 join
(select a, c, max(b) max, min(b) min from t4 group by a,c) t
on t3.a=t.a and t3.c=t.c
where t3.b > 15;
a	c	max	min
7	cc	18	10
7	aa	82	82
explain extended select t3.a,t3.c,t.max,t.min
from t3 join
(select a, c, max(b) max, min(b) min from t4 group by a,c) t
on t3.a=t.a and t3.c=t.c
where t3.b > 15;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	PRIMARY	t3	range	idx_b	idx_b	5	NULL	2	100.00	Using index condition; Using where
1	PRIMARY	<derived2>	ref	key0	key0	133	test.t3.a,test.t3.c	1	100.00	
2	LATERAL DERIVED	t4	ref	idx	idx	133	test.t3.a,test.t3.c	1	100.00	
Warnings:
Note	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
explain format=json select t3.a,t3.c,t.max,t.min
from t3 join
(select a, c, max(b) max, min(b) min from t4 group by a,c) t
on t3.a=t.a and t3.c=t.c
where t3.b > 15;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t3",
          "access_type": "range",
          "possible_keys": ["idx_b"],
          "key": "idx_b",
          "key_length": "5",
          "used_key_parts": ["b"],
          "loops": 1,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "index_condition": "t3.b > 15",
          "attached_condition": "t3.a is not null and t3.c is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "133",
          "used_key_parts": ["a", "c"],
          "ref": ["test.t3.a", "test.t3.c"],
          "loops": 2,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "materialized": {
            "lateral": 1,
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "outer_ref_condition": "t3.a is not null and t3.c is not null",
              "nested_loop": [
                {
                  "table": {
                    "table_name": "t4",
                    "access_type": "ref",
                    "possible_keys": ["idx"],
                    "key": "idx",
                    "key_length": "133",
                    "used_key_parts": ["a", "c"],
                    "ref": ["test.t3.a", "test.t3.c"],
                    "loops": 1,
                    "rows": 1,
                    "cost": "COST_REPLACED",
                    "filtered": 100
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}
set statement optimizer_switch='split_materialized=off' for select t3.a,t3.c,t.max,t.min
from t3 join
(select a, c, max(b) max, min(b) min from t4 group by a,c) t
on t3.a=t.a and t3.c=t.c
where t3.b <= 15;
a	c	max	min
1	bb	30	30
7	bb	32	32
select t3.a,t3.c,t.max,t.min
from t3 join
(select a, c, max(b) max, min(b) min from t4 group by a,c) t
on t3.a=t.a and t3.c=t.c
where t3.b <= 15;
a	c	max	min
1	bb	30	30
7	bb	32	32
explain extended select t3.a,t3.c,t.max,t.min
from t3 join
(select a, c, max(b) max, min(b) min from t4 group by a,c) t
on t3.a=t.a and t3.c=t.c
where t3.b <= 15;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	PRIMARY	t3	ALL	idx_b	NULL	NULL	NULL	12	83.33	Using where
1	PRIMARY	<derived2>	ref	key0	key0	133	test.t3.a,test.t3.c	1	100.00	
2	LATERAL DERIVED	t4	ref	idx	idx	133	test.t3.a,test.t3.c	1	100.00	
Warnings:
Note	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
explain format=json select t3.a,t3.c,t.max,t.min
from t3 join
(select a, c, max(b) max, min(b) min from t4 group by a,c) t
on t3.a=t.a and t3.c=t.c
where t3.b <= 15;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t3",
          "access_type": "ALL",
          "possible_keys": ["idx_b"],
          "loops": 1,
          "rows": 12,
          "cost": "COST_REPLACED",
          "filtered": 83.33333588,
          "attached_condition": "t3.b <= 15 and t3.a is not null and t3.c is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "133",
          "used_key_parts": ["a", "c"],
          "ref": ["test.t3.a", "test.t3.c"],
          "loops": 10,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "materialized": {
            "lateral": 1,
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "outer_ref_condition": "t3.a is not null and t3.c is not null",
              "nested_loop": [
                {
                  "table": {
                    "table_name": "t4",
                    "access_type": "ref",
                    "possible_keys": ["idx"],
                    "key": "idx",
                    "key_length": "133",
                    "used_key_parts": ["a", "c"],
                    "ref": ["test.t3.a", "test.t3.c"],
                    "loops": 1,
                    "rows": 1,
                    "cost": "COST_REPLACED",
                    "filtered": 100
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}
set statement optimizer_switch='split_materialized=off' for select t3.a,t3.c,t.max,t.min
from t3 join
(select a, c, max(b) max, min(b) min from t4 group by c,a) t
on t3.a=t.a and t3.c=t.c
where t3.b > 15;
a	c	max	min
7	cc	18	10
7	aa	82	82
select t3.a,t3.c,t.max,t.min
from t3 join
(select a, c, max(b) max, min(b) min from t4 group by c,a) t
on t3.a=t.a and t3.c=t.c
where t3.b > 15;
a	c	max	min
7	cc	18	10
7	aa	82	82
explain extended select t3.a,t3.c,t.max,t.min
from t3 join
(select a, c, max(b) max, min(b) min from t4 group by c,a) t
on t3.a=t.a and t3.c=t.c
where t3.b > 15;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	PRIMARY	t3	range	idx_b	idx_b	5	NULL	2	100.00	Using index condition; Using where
1	PRIMARY	<derived2>	ref	key0	key0	133	test.t3.a,test.t3.c	1	100.00	
2	LATERAL DERIVED	t4	ref	idx	idx	133	test.t3.a,test.t3.c	1	100.00	
Warnings:
Note	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
explain format=json select t3.a,t3.c,t.max,t.min
from t3 join
(select a, c, max(b) max, min(b) min from t4 group by c,a) t
on t3.a=t.a and t3.c=t.c
where t3.b > 15;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t3",
          "access_type": "range",
          "possible_keys": ["idx_b"],
          "key": "idx_b",
          "key_length": "5",
          "used_key_parts": ["b"],
          "loops": 1,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "index_condition": "t3.b > 15",
          "attached_condition": "t3.a is not null and t3.c is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "133",
          "used_key_parts": ["a", "c"],
          "ref": ["test.t3.a", "test.t3.c"],
          "loops": 2,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "materialized": {
            "lateral": 1,
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "outer_ref_condition": "t3.a is not null and t3.c is not null",
              "nested_loop": [
                {
                  "table": {
                    "table_name": "t4",
                    "access_type": "ref",
                    "possible_keys": ["idx"],
                    "key": "idx",
                    "key_length": "133",
                    "used_key_parts": ["a", "c"],
                    "ref": ["test.t3.a", "test.t3.c"],
                    "loops": 1,
                    "rows": 1,
                    "cost": "COST_REPLACED",
                    "filtered": 100
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}
set statement optimizer_switch='split_materialized=off' for select t3.a,t3.c,t.max,t.min
from t3 join
(select a, c, max(b) max, min(b) min from t4 group by c,a) t
on t3.a=t.a and t3.c=t.c
where t3.b <= 15;
a	c	max	min
1	bb	30	30
7	bb	32	32
select t3.a,t3.c,t.max,t.min
from t3 join
(select a, c, max(b) max, min(b) min from t4 group by c,a) t
on t3.a=t.a and t3.c=t.c
where t3.b <= 15;
a	c	max	min
1	bb	30	30
7	bb	32	32
explain extended select t3.a,t3.c,t.max,t.min
from t3 join
(select a, c, max(b) max, min(b) min from t4 group by c,a) t
on t3.a=t.a and t3.c=t.c
where t3.b <= 15;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	PRIMARY	t3	ALL	idx_b	NULL	NULL	NULL	12	83.33	Using where
1	PRIMARY	<derived2>	ref	key0	key0	133	test.t3.a,test.t3.c	1	100.00	
2	LATERAL DERIVED	t4	ref	idx	idx	133	test.t3.a,test.t3.c	1	100.00	
Warnings:
Note	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
explain format=json select t3.a,t3.c,t.max,t.min
from t3 join
(select a, c, max(b) max, min(b) min from t4 group by c,a) t
on t3.a=t.a and t3.c=t.c
where t3.b <= 15;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t3",
          "access_type": "ALL",
          "possible_keys": ["idx_b"],
          "loops": 1,
          "rows": 12,
          "cost": "COST_REPLACED",
          "filtered": 83.33333588,
          "attached_condition": "t3.b <= 15 and t3.a is not null and t3.c is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "133",
          "used_key_parts": ["a", "c"],
          "ref": ["test.t3.a", "test.t3.c"],
          "loops": 10,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "materialized": {
            "lateral": 1,
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "outer_ref_condition": "t3.a is not null and t3.c is not null",
              "nested_loop": [
                {
                  "table": {
                    "table_name": "t4",
                    "access_type": "ref",
                    "possible_keys": ["idx"],
                    "key": "idx",
                    "key_length": "133",
                    "used_key_parts": ["a", "c"],
                    "ref": ["test.t3.a", "test.t3.c"],
                    "loops": 1,
                    "rows": 1,
                    "cost": "COST_REPLACED",
                    "filtered": 100
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}
drop index idx_a on t2;
create index idx on t2(c,b);
create index idx_a on t3(a);
create index idx_c on t4(c);
insert into t3 select a+10, b+10, concat(c,'f') from t3;
insert into t3 select a+100, b+100, concat(c,'g')  from t3;
insert into t4 select a+100, b+100, concat(c,'g') from t4;
insert into t4 select a+1000, b+1000, concat(c,'h') from t4;
analyze table t2,t3,t4;
Table	Op	Msg_type	Msg_text
test.t2	analyze	status	Engine-independent statistics collected
test.t2	analyze	status	OK
test.t3	analyze	status	Engine-independent statistics collected
test.t3	analyze	status	OK
test.t4	analyze	status	Engine-independent statistics collected
test.t4	analyze	status	OK
set statement optimizer_switch='split_materialized=off' for select t2.a,t2.b,t2.c,t.c as t_c,t.max,t.min
from t2, t3, (select c, max(b) max, min(b) min from t4 group by c) t
where t2.b between 80 and 85 and t2.c in ('y','z') and t2.a=t3.a and t3.c=t.c;
a	b	c	t_c	max	min
7	82	y	aa	82	15
7	82	y	bb	40	23
7	82	y	cc	18	10
select t2.a,t2.b,t2.c,t.c as t_c,t.max,t.min
from t2, t3, (select c, max(b) max, min(b) min from t4 group by c) t
where t2.b between 80 and 85 and t2.c in ('y','z') and t2.a=t3.a and t3.c=t.c;
a	b	c	t_c	max	min
7	82	y	aa	82	15
7	82	y	bb	40	23
7	82	y	cc	18	10
explain extended select t2.a,t2.b,t2.c,t.c as t_c,t.max,t.min
from t2, t3, (select c, max(b) max, min(b) min from t4 group by c) t
where t2.b between 80 and 85 and t2.c in ('y','z') and t2.a=t3.a and t3.c=t.c;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	PRIMARY	t2	range	idx	idx	133	NULL	2	100.00	Using index condition; Using where
1	PRIMARY	t3	ref	idx_a	idx_a	5	test.t2.a	1	100.00	Using where
1	PRIMARY	<derived2>	ref	key0	key0	128	test.t3.c	1	100.00	
2	LATERAL DERIVED	t4	ref	idx_c	idx_c	128	test.t3.c	2	100.00	
Warnings:
Note	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')
explain format=json select t2.a,t2.b,t2.c,t.c as t_c,t.max,t.min
from t2, t3, (select c, max(b) max, min(b) min from t4 group by c) t
where t2.b between 80 and 85 and t2.c in ('y','z') and t2.a=t3.a and t3.c=t.c;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "range",
          "possible_keys": ["idx"],
          "key": "idx",
          "key_length": "133",
          "used_key_parts": ["c", "b"],
          "loops": 1,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "index_condition": "t2.b between 80 and 85 and t2.c in ('y','z')",
          "attached_condition": "t2.a is not null"
        }
      },
      {
        "table": {
          "table_name": "t3",
          "access_type": "ref",
          "possible_keys": ["idx_a"],
          "key": "idx_a",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t2.a"],
          "loops": 2,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t3.c is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "128",
          "used_key_parts": ["c"],
          "ref": ["test.t3.c"],
          "loops": 3,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "materialized": {
            "lateral": 1,
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "outer_ref_condition": "t3.c is not null",
              "nested_loop": [
                {
                  "table": {
                    "table_name": "t4",
                    "access_type": "ref",
                    "possible_keys": ["idx_c"],
                    "key": "idx_c",
                    "key_length": "128",
                    "used_key_parts": ["c"],
                    "ref": ["test.t3.c"],
                    "loops": 1,
                    "rows": 2,
                    "cost": "COST_REPLACED",
                    "filtered": 100
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}
set statement optimizer_switch='split_materialized=off' for select t2.a,t2.b,t2.c,t.c as t_c,t.max,t.min
from t2, t3, (select c, max(b) max, min(b) min from t4 group by c) t
where t2.b < 40 and t2.a=t3.a and t3.c=t.c;
a	b	c	t_c	max	min
7	10	x	cc	18	10
7	10	x	aa	82	15
7	10	x	bb	40	23
1	20	a	bb	40	23
2	23	b	aa	82	15
2	23	b	aa	82	15
7	18	z	cc	18	10
7	18	z	aa	82	15
7	18	z	bb	40	23
1	30	c	bb	40	23
3	15	x	dd	20	12
8	12	t	aa	82	15
11	33	a	bbf	50	33
17	10	s	ccf	28	20
17	10	s	aaf	92	25
17	10	s	bbf	50	33
11	20	v	bbf	50	33
12	23	y	aaf	92	25
12	23	y	aaf	92	25
17	18	a	ccf	28	20
17	18	a	aaf	92	25
17	18	a	bbf	50	33
11	30	d	bbf	50	33
17	20	xf	ccf	28	20
17	20	xf	aaf	92	25
17	20	xf	bbf	50	33
11	30	af	bbf	50	33
12	33	bf	aaf	92	25
12	33	bf	aaf	92	25
17	28	zf	ccf	28	20
17	28	zf	aaf	92	25
17	28	zf	bbf	50	33
13	25	xf	ddf	30	22
18	22	tf	aaf	92	25
select t2.a,t2.b,t2.c,t.c as t_c,t.max,t.min
from t2, t3, (select c, max(b) max, min(b) min from t4 group by c) t
where t2.b < 40 and t2.a=t3.a and t3.c=t.c;
a	b	c	t_c	max	min
7	10	x	cc	18	10
7	10	x	aa	82	15
7	10	x	bb	40	23
1	20	a	bb	40	23
2	23	b	aa	82	15
2	23	b	aa	82	15
7	18	z	cc	18	10
7	18	z	aa	82	15
7	18	z	bb	40	23
1	30	c	bb	40	23
3	15	x	dd	20	12
8	12	t	aa	82	15
11	33	a	bbf	50	33
17	10	s	ccf	28	20
17	10	s	aaf	92	25
17	10	s	bbf	50	33
11	20	v	bbf	50	33
12	23	y	aaf	92	25
12	23	y	aaf	92	25
17	18	a	ccf	28	20
17	18	a	aaf	92	25
17	18	a	bbf	50	33
11	30	d	bbf	50	33
17	20	xf	ccf	28	20
17	20	xf	aaf	92	25
17	20	xf	bbf	50	33
11	30	af	bbf	50	33
12	33	bf	aaf	92	25
12	33	bf	aaf	92	25
17	28	zf	ccf	28	20
17	28	zf	aaf	92	25
17	28	zf	bbf	50	33
13	25	xf	ddf	30	22
18	22	tf	aaf	92	25
explain extended select t2.a,t2.b,t2.c,t.c as t_c,t.max,t.min
from t2, t3, (select c, max(b) max, min(b) min from t4 group by c) t
where t2.b < 40 and t2.a=t3.a and t3.c=t.c;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	90	60.00	Using where
1	PRIMARY	t3	ref	idx_a	idx_a	5	test.t2.a	1	100.00	Using where
1	PRIMARY	<derived2>	ref	key0	key0	128	test.t3.c	5	100.00	
2	DERIVED	t4	ALL	idx_c	NULL	NULL	NULL	160	100.00	Using temporary; Using filesort
Warnings:
Note	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
explain format=json select t2.a,t2.b,t2.c,t.c as t_c,t.max,t.min
from t2, t3, (select c, max(b) max, min(b) min from t4 group by c) t
where t2.b < 40 and t2.a=t3.a and t3.c=t.c;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 90,
          "cost": "COST_REPLACED",
          "filtered": 60,
          "attached_condition": "t2.b < 40 and t2.a is not null"
        }
      },
      {
        "table": {
          "table_name": "t3",
          "access_type": "ref",
          "possible_keys": ["idx_a"],
          "key": "idx_a",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t2.a"],
          "loops": 53.99999991,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t3.c is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "128",
          "used_key_parts": ["c"],
          "ref": ["test.t3.c"],
          "loops": 80.99999987,
          "rows": 5,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "filesort": {
                "sort_key": "t4.c",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t4",
                        "access_type": "ALL",
                        "possible_keys": ["idx_c"],
                        "loops": 1,
                        "rows": 160,
                        "cost": "COST_REPLACED",
                        "filtered": 100
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
set statement optimizer_switch='split_materialized=off' for select *
from t2, t3, (select c, b, sum(b) over (partition by c) from t4 ) t
where t2.b between 80 and 85 and t2.c in ('y','z') and t2.a=t3.a and t3.c=t.c;
a	b	c	a	b	c	c	b	sum(b) over (partition by c)
7	82	y	7	10	bb	bb	23	125
7	82	y	7	10	bb	bb	30	125
7	82	y	7	10	bb	bb	32	125
7	82	y	7	10	bb	bb	40	125
7	82	y	7	17	cc	cc	10	40
7	82	y	7	17	cc	cc	12	40
7	82	y	7	17	cc	cc	18	40
7	82	y	7	18	aa	aa	15	259
7	82	y	7	18	aa	aa	15	259
7	82	y	7	18	aa	aa	20	259
7	82	y	7	18	aa	aa	50	259
7	82	y	7	18	aa	aa	77	259
7	82	y	7	18	aa	aa	82	259
select *
from t2, t3, (select c, b, sum(b) over (partition by c) from t4 ) t
where t2.b between 80 and 85 and t2.c in ('y','z') and t2.a=t3.a and t3.c=t.c;
a	b	c	a	b	c	c	b	sum(b) over (partition by c)
7	82	y	7	10	bb	bb	23	125
7	82	y	7	10	bb	bb	30	125
7	82	y	7	10	bb	bb	32	125
7	82	y	7	10	bb	bb	40	125
7	82	y	7	17	cc	cc	10	40
7	82	y	7	17	cc	cc	12	40
7	82	y	7	17	cc	cc	18	40
7	82	y	7	18	aa	aa	15	259
7	82	y	7	18	aa	aa	15	259
7	82	y	7	18	aa	aa	20	259
7	82	y	7	18	aa	aa	50	259
7	82	y	7	18	aa	aa	77	259
7	82	y	7	18	aa	aa	82	259
explain extended select *
from t2, t3, (select c, b, sum(b) over (partition by c) from t4 ) t
where t2.b between 80 and 85 and t2.c in ('y','z') and t2.a=t3.a and t3.c=t.c;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	PRIMARY	t2	range	idx	idx	133	NULL	2	100.00	Using index condition; Using where
1	PRIMARY	t3	ref	idx_a	idx_a	5	test.t2.a	1	100.00	Using where
1	PRIMARY	<derived2>	ref	key0	key0	128	test.t3.c	1	100.00	
2	LATERAL DERIVED	t4	ref	idx_c	idx_c	128	test.t3.c	2	100.00	Using temporary
Warnings:
Note	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')
explain format=json select *
from t2, t3, (select c, b, sum(b) over (partition by c) from t4 ) t
where t2.b between 80 and 85 and t2.c in ('y','z') and t2.a=t3.a and t3.c=t.c;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "range",
          "possible_keys": ["idx"],
          "key": "idx",
          "key_length": "133",
          "used_key_parts": ["c", "b"],
          "loops": 1,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "index_condition": "t2.b between 80 and 85 and t2.c in ('y','z')",
          "attached_condition": "t2.a is not null"
        }
      },
      {
        "table": {
          "table_name": "t3",
          "access_type": "ref",
          "possible_keys": ["idx_a"],
          "key": "idx_a",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t2.a"],
          "loops": 2,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t3.c is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "128",
          "used_key_parts": ["c"],
          "ref": ["test.t3.c"],
          "loops": 3,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "materialized": {
            "lateral": 1,
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "outer_ref_condition": "t3.c is not null",
              "window_functions_computation": {
                "sorts": [
                  {
                    "filesort": {
                      "sort_key": "t4.c"
                    }
                  }
                ],
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t4",
                        "access_type": "ref",
                        "possible_keys": ["idx_c"],
                        "key": "idx_c",
                        "key_length": "128",
                        "used_key_parts": ["c"],
                        "ref": ["test.t3.c"],
                        "loops": 1,
                        "rows": 2,
                        "cost": "COST_REPLACED",
                        "filtered": 100
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
set statement optimizer_switch='split_materialized=off' for select *
from t2, t3, (select c, b, sum(b) over (partition by c) from t4 ) t
where t2.b < 40 and t2.a=t3.a and t3.c=t.c;
a	b	c	a	b	c	c	b	sum(b) over (partition by c)
1	20	a	1	14	bb	bb	23	125
1	20	a	1	14	bb	bb	30	125
1	20	a	1	14	bb	bb	32	125
1	20	a	1	14	bb	bb	40	125
1	30	c	1	14	bb	bb	23	125
1	30	c	1	14	bb	bb	30	125
1	30	c	1	14	bb	bb	32	125
1	30	c	1	14	bb	bb	40	125
11	20	v	11	24	bbf	bbf	33	165
11	20	v	11	24	bbf	bbf	40	165
11	20	v	11	24	bbf	bbf	42	165
11	20	v	11	24	bbf	bbf	50	165
11	30	af	11	24	bbf	bbf	33	165
11	30	af	11	24	bbf	bbf	40	165
11	30	af	11	24	bbf	bbf	42	165
11	30	af	11	24	bbf	bbf	50	165
11	30	d	11	24	bbf	bbf	33	165
11	30	d	11	24	bbf	bbf	40	165
11	30	d	11	24	bbf	bbf	42	165
11	30	d	11	24	bbf	bbf	50	165
11	33	a	11	24	bbf	bbf	33	165
11	33	a	11	24	bbf	bbf	40	165
11	33	a	11	24	bbf	bbf	42	165
11	33	a	11	24	bbf	bbf	50	165
12	23	y	12	21	aaf	aaf	25	319
12	23	y	12	21	aaf	aaf	25	319
12	23	y	12	21	aaf	aaf	30	319
12	23	y	12	21	aaf	aaf	60	319
12	23	y	12	21	aaf	aaf	87	319
12	23	y	12	21	aaf	aaf	92	319
12	23	y	12	22	aaf	aaf	25	319
12	23	y	12	22	aaf	aaf	25	319
12	23	y	12	22	aaf	aaf	30	319
12	23	y	12	22	aaf	aaf	60	319
12	23	y	12	22	aaf	aaf	87	319
12	23	y	12	22	aaf	aaf	92	319
12	33	bf	12	21	aaf	aaf	25	319
12	33	bf	12	21	aaf	aaf	25	319
12	33	bf	12	21	aaf	aaf	30	319
12	33	bf	12	21	aaf	aaf	60	319
12	33	bf	12	21	aaf	aaf	87	319
12	33	bf	12	21	aaf	aaf	92	319
12	33	bf	12	22	aaf	aaf	25	319
12	33	bf	12	22	aaf	aaf	25	319
12	33	bf	12	22	aaf	aaf	30	319
12	33	bf	12	22	aaf	aaf	60	319
12	33	bf	12	22	aaf	aaf	87	319
12	33	bf	12	22	aaf	aaf	92	319
13	25	xf	13	21	ddf	ddf	22	52
13	25	xf	13	21	ddf	ddf	30	52
17	10	s	17	20	bbf	bbf	33	165
17	10	s	17	20	bbf	bbf	40	165
17	10	s	17	20	bbf	bbf	42	165
17	10	s	17	20	bbf	bbf	50	165
17	10	s	17	27	ccf	ccf	20	70
17	10	s	17	27	ccf	ccf	22	70
17	10	s	17	27	ccf	ccf	28	70
17	10	s	17	28	aaf	aaf	25	319
17	10	s	17	28	aaf	aaf	25	319
17	10	s	17	28	aaf	aaf	30	319
17	10	s	17	28	aaf	aaf	60	319
17	10	s	17	28	aaf	aaf	87	319
17	10	s	17	28	aaf	aaf	92	319
17	18	a	17	20	bbf	bbf	33	165
17	18	a	17	20	bbf	bbf	40	165
17	18	a	17	20	bbf	bbf	42	165
17	18	a	17	20	bbf	bbf	50	165
17	18	a	17	27	ccf	ccf	20	70
17	18	a	17	27	ccf	ccf	22	70
17	18	a	17	27	ccf	ccf	28	70
17	18	a	17	28	aaf	aaf	25	319
17	18	a	17	28	aaf	aaf	25	319
17	18	a	17	28	aaf	aaf	30	319
17	18	a	17	28	aaf	aaf	60	319
17	18	a	17	28	aaf	aaf	87	319
17	18	a	17	28	aaf	aaf	92	319
17	20	xf	17	20	bbf	bbf	33	165
17	20	xf	17	20	bbf	bbf	40	165
17	20	xf	17	20	bbf	bbf	42	165
17	20	xf	17	20	bbf	bbf	50	165
17	20	xf	17	27	ccf	ccf	20	70
17	20	xf	17	27	ccf	ccf	22	70
17	20	xf	17	27	ccf	ccf	28	70
17	20	xf	17	28	aaf	aaf	25	319
17	20	xf	17	28	aaf	aaf	25	319
17	20	xf	17	28	aaf	aaf	30	319
17	20	xf	17	28	aaf	aaf	60	319
17	20	xf	17	28	aaf	aaf	87	319
17	20	xf	17	28	aaf	aaf	92	319
17	28	zf	17	20	bbf	bbf	33	165
17	28	zf	17	20	bbf	bbf	40	165
17	28	zf	17	20	bbf	bbf	42	165
17	28	zf	17	20	bbf	bbf	50	165
17	28	zf	17	27	ccf	ccf	20	70
17	28	zf	17	27	ccf	ccf	22	70
17	28	zf	17	27	ccf	ccf	28	70
17	28	zf	17	28	aaf	aaf	25	319
17	28	zf	17	28	aaf	aaf	25	319
17	28	zf	17	28	aaf	aaf	30	319
17	28	zf	17	28	aaf	aaf	60	319
17	28	zf	17	28	aaf	aaf	87	319
17	28	zf	17	28	aaf	aaf	92	319
18	22	tf	18	21	aaf	aaf	25	319
18	22	tf	18	21	aaf	aaf	25	319
18	22	tf	18	21	aaf	aaf	30	319
18	22	tf	18	21	aaf	aaf	60	319
18	22	tf	18	21	aaf	aaf	87	319
18	22	tf	18	21	aaf	aaf	92	319
2	23	b	2	11	aa	aa	15	259
2	23	b	2	11	aa	aa	15	259
2	23	b	2	11	aa	aa	20	259
2	23	b	2	11	aa	aa	50	259
2	23	b	2	11	aa	aa	77	259
2	23	b	2	11	aa	aa	82	259
2	23	b	2	12	aa	aa	15	259
2	23	b	2	12	aa	aa	15	259
2	23	b	2	12	aa	aa	20	259
2	23	b	2	12	aa	aa	50	259
2	23	b	2	12	aa	aa	77	259
2	23	b	2	12	aa	aa	82	259
3	15	x	3	11	dd	dd	12	32
3	15	x	3	11	dd	dd	20	32
7	10	x	7	10	bb	bb	23	125
7	10	x	7	10	bb	bb	30	125
7	10	x	7	10	bb	bb	32	125
7	10	x	7	10	bb	bb	40	125
7	10	x	7	17	cc	cc	10	40
7	10	x	7	17	cc	cc	12	40
7	10	x	7	17	cc	cc	18	40
7	10	x	7	18	aa	aa	15	259
7	10	x	7	18	aa	aa	15	259
7	10	x	7	18	aa	aa	20	259
7	10	x	7	18	aa	aa	50	259
7	10	x	7	18	aa	aa	77	259
7	10	x	7	18	aa	aa	82	259
7	18	z	7	10	bb	bb	23	125
7	18	z	7	10	bb	bb	30	125
7	18	z	7	10	bb	bb	32	125
7	18	z	7	10	bb	bb	40	125
7	18	z	7	17	cc	cc	10	40
7	18	z	7	17	cc	cc	12	40
7	18	z	7	17	cc	cc	18	40
7	18	z	7	18	aa	aa	15	259
7	18	z	7	18	aa	aa	15	259
7	18	z	7	18	aa	aa	20	259
7	18	z	7	18	aa	aa	50	259
7	18	z	7	18	aa	aa	77	259
7	18	z	7	18	aa	aa	82	259
8	12	t	8	11	aa	aa	15	259
8	12	t	8	11	aa	aa	15	259
8	12	t	8	11	aa	aa	20	259
8	12	t	8	11	aa	aa	50	259
8	12	t	8	11	aa	aa	77	259
8	12	t	8	11	aa	aa	82	259
select *
from t2, t3, (select c, b, sum(b) over (partition by c) from t4 ) t
where t2.b < 40 and t2.a=t3.a and t3.c=t.c;
a	b	c	a	b	c	c	b	sum(b) over (partition by c)
1	20	a	1	14	bb	bb	23	125
1	20	a	1	14	bb	bb	30	125
1	20	a	1	14	bb	bb	32	125
1	20	a	1	14	bb	bb	40	125
1	30	c	1	14	bb	bb	23	125
1	30	c	1	14	bb	bb	30	125
1	30	c	1	14	bb	bb	32	125
1	30	c	1	14	bb	bb	40	125
11	20	v	11	24	bbf	bbf	33	165
11	20	v	11	24	bbf	bbf	40	165
11	20	v	11	24	bbf	bbf	42	165
11	20	v	11	24	bbf	bbf	50	165
11	30	af	11	24	bbf	bbf	33	165
11	30	af	11	24	bbf	bbf	40	165
11	30	af	11	24	bbf	bbf	42	165
11	30	af	11	24	bbf	bbf	50	165
11	30	d	11	24	bbf	bbf	33	165
11	30	d	11	24	bbf	bbf	40	165
11	30	d	11	24	bbf	bbf	42	165
11	30	d	11	24	bbf	bbf	50	165
11	33	a	11	24	bbf	bbf	33	165
11	33	a	11	24	bbf	bbf	40	165
11	33	a	11	24	bbf	bbf	42	165
11	33	a	11	24	bbf	bbf	50	165
12	23	y	12	21	aaf	aaf	25	319
12	23	y	12	21	aaf	aaf	25	319
12	23	y	12	21	aaf	aaf	30	319
12	23	y	12	21	aaf	aaf	60	319
12	23	y	12	21	aaf	aaf	87	319
12	23	y	12	21	aaf	aaf	92	319
12	23	y	12	22	aaf	aaf	25	319
12	23	y	12	22	aaf	aaf	25	319
12	23	y	12	22	aaf	aaf	30	319
12	23	y	12	22	aaf	aaf	60	319
12	23	y	12	22	aaf	aaf	87	319
12	23	y	12	22	aaf	aaf	92	319
12	33	bf	12	21	aaf	aaf	25	319
12	33	bf	12	21	aaf	aaf	25	319
12	33	bf	12	21	aaf	aaf	30	319
12	33	bf	12	21	aaf	aaf	60	319
12	33	bf	12	21	aaf	aaf	87	319
12	33	bf	12	21	aaf	aaf	92	319
12	33	bf	12	22	aaf	aaf	25	319
12	33	bf	12	22	aaf	aaf	25	319
12	33	bf	12	22	aaf	aaf	30	319
12	33	bf	12	22	aaf	aaf	60	319
12	33	bf	12	22	aaf	aaf	87	319
12	33	bf	12	22	aaf	aaf	92	319
13	25	xf	13	21	ddf	ddf	22	52
13	25	xf	13	21	ddf	ddf	30	52
17	10	s	17	20	bbf	bbf	33	165
17	10	s	17	20	bbf	bbf	40	165
17	10	s	17	20	bbf	bbf	42	165
17	10	s	17	20	bbf	bbf	50	165
17	10	s	17	27	ccf	ccf	20	70
17	10	s	17	27	ccf	ccf	22	70
17	10	s	17	27	ccf	ccf	28	70
17	10	s	17	28	aaf	aaf	25	319
17	10	s	17	28	aaf	aaf	25	319
17	10	s	17	28	aaf	aaf	30	319
17	10	s	17	28	aaf	aaf	60	319
17	10	s	17	28	aaf	aaf	87	319
17	10	s	17	28	aaf	aaf	92	319
17	18	a	17	20	bbf	bbf	33	165
17	18	a	17	20	bbf	bbf	40	165
17	18	a	17	20	bbf	bbf	42	165
17	18	a	17	20	bbf	bbf	50	165
17	18	a	17	27	ccf	ccf	20	70
17	18	a	17	27	ccf	ccf	22	70
17	18	a	17	27	ccf	ccf	28	70
17	18	a	17	28	aaf	aaf	25	319
17	18	a	17	28	aaf	aaf	25	319
17	18	a	17	28	aaf	aaf	30	319
17	18	a	17	28	aaf	aaf	60	319
17	18	a	17	28	aaf	aaf	87	319
17	18	a	17	28	aaf	aaf	92	319
17	20	xf	17	20	bbf	bbf	33	165
17	20	xf	17	20	bbf	bbf	40	165
17	20	xf	17	20	bbf	bbf	42	165
17	20	xf	17	20	bbf	bbf	50	165
17	20	xf	17	27	ccf	ccf	20	70
17	20	xf	17	27	ccf	ccf	22	70
17	20	xf	17	27	ccf	ccf	28	70
17	20	xf	17	28	aaf	aaf	25	319
17	20	xf	17	28	aaf	aaf	25	319
17	20	xf	17	28	aaf	aaf	30	319
17	20	xf	17	28	aaf	aaf	60	319
17	20	xf	17	28	aaf	aaf	87	319
17	20	xf	17	28	aaf	aaf	92	319
17	28	zf	17	20	bbf	bbf	33	165
17	28	zf	17	20	bbf	bbf	40	165
17	28	zf	17	20	bbf	bbf	42	165
17	28	zf	17	20	bbf	bbf	50	165
17	28	zf	17	27	ccf	ccf	20	70
17	28	zf	17	27	ccf	ccf	22	70
17	28	zf	17	27	ccf	ccf	28	70
17	28	zf	17	28	aaf	aaf	25	319
17	28	zf	17	28	aaf	aaf	25	319
17	28	zf	17	28	aaf	aaf	30	319
17	28	zf	17	28	aaf	aaf	60	319
17	28	zf	17	28	aaf	aaf	87	319
17	28	zf	17	28	aaf	aaf	92	319
18	22	tf	18	21	aaf	aaf	25	319
18	22	tf	18	21	aaf	aaf	25	319
18	22	tf	18	21	aaf	aaf	30	319
18	22	tf	18	21	aaf	aaf	60	319
18	22	tf	18	21	aaf	aaf	87	319
18	22	tf	18	21	aaf	aaf	92	319
2	23	b	2	11	aa	aa	15	259
2	23	b	2	11	aa	aa	15	259
2	23	b	2	11	aa	aa	20	259
2	23	b	2	11	aa	aa	50	259
2	23	b	2	11	aa	aa	77	259
2	23	b	2	11	aa	aa	82	259
2	23	b	2	12	aa	aa	15	259
2	23	b	2	12	aa	aa	15	259
2	23	b	2	12	aa	aa	20	259
2	23	b	2	12	aa	aa	50	259
2	23	b	2	12	aa	aa	77	259
2	23	b	2	12	aa	aa	82	259
3	15	x	3	11	dd	dd	12	32
3	15	x	3	11	dd	dd	20	32
7	10	x	7	10	bb	bb	23	125
7	10	x	7	10	bb	bb	30	125
7	10	x	7	10	bb	bb	32	125
7	10	x	7	10	bb	bb	40	125
7	10	x	7	17	cc	cc	10	40
7	10	x	7	17	cc	cc	12	40
7	10	x	7	17	cc	cc	18	40
7	10	x	7	18	aa	aa	15	259
7	10	x	7	18	aa	aa	15	259
7	10	x	7	18	aa	aa	20	259
7	10	x	7	18	aa	aa	50	259
7	10	x	7	18	aa	aa	77	259
7	10	x	7	18	aa	aa	82	259
7	18	z	7	10	bb	bb	23	125
7	18	z	7	10	bb	bb	30	125
7	18	z	7	10	bb	bb	32	125
7	18	z	7	10	bb	bb	40	125
7	18	z	7	17	cc	cc	10	40
7	18	z	7	17	cc	cc	12	40
7	18	z	7	17	cc	cc	18	40
7	18	z	7	18	aa	aa	15	259
7	18	z	7	18	aa	aa	15	259
7	18	z	7	18	aa	aa	20	259
7	18	z	7	18	aa	aa	50	259
7	18	z	7	18	aa	aa	77	259
7	18	z	7	18	aa	aa	82	259
8	12	t	8	11	aa	aa	15	259
8	12	t	8	11	aa	aa	15	259
8	12	t	8	11	aa	aa	20	259
8	12	t	8	11	aa	aa	50	259
8	12	t	8	11	aa	aa	77	259
8	12	t	8	11	aa	aa	82	259
explain extended select *
from t2, t3, (select c, b, sum(b) over (partition by c) from t4 ) t
where t2.b < 40 and t2.a=t3.a and t3.c=t.c;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	90	60.00	Using where
1	PRIMARY	t3	ref	idx_a	idx_a	5	test.t2.a	1	100.00	Using where
1	PRIMARY	<derived2>	ref	key0	key0	128	test.t3.c	10	100.00	
2	DERIVED	t4	ALL	idx_c	NULL	NULL	NULL	160	100.00	Using temporary
Warnings:
Note	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
explain format=json select *
from t2, t3, (select c, b, sum(b) over (partition by c) from t4 ) t
where t2.b < 40 and t2.a=t3.a and t3.c=t.c;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 90,
          "cost": "COST_REPLACED",
          "filtered": 60,
          "attached_condition": "t2.b < 40 and t2.a is not null"
        }
      },
      {
        "table": {
          "table_name": "t3",
          "access_type": "ref",
          "possible_keys": ["idx_a"],
          "key": "idx_a",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t2.a"],
          "loops": 53.99999991,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t3.c is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "128",
          "used_key_parts": ["c"],
          "ref": ["test.t3.c"],
          "loops": 80.99999987,
          "rows": 10,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "window_functions_computation": {
                "sorts": [
                  {
                    "filesort": {
                      "sort_key": "t4.c"
                    }
                  }
                ],
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t4",
                        "access_type": "ALL",
                        "possible_keys": ["idx_c"],
                        "loops": 1,
                        "rows": 160,
                        "cost": "COST_REPLACED",
                        "filtered": 100
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
drop table t1,t2,t3,t4;
#
# MDEV-13709: Optimization for semi-joins of grouping derived tables
#             (Splitting derived tables / views with GROUP BY)
#
CREATE TABLE t1 (i int);
INSERT INTO t1 VALUES (1),(9),(3);
CREATE TABLE t2 (a int, i int);
INSERT INTO t2 VALUES (1,9),(2,3),(3,7),(4,1);
CREATE TABLE t3 (a int, c char(127), index(c));
INSERT INTO t3 VALUES (1,'foo'),(3,'bar'),(4,'foo'),(2,'bar');
INSERT INTO t3 SELECT a, concat(c,'a') FROM t3;
CREATE TABLE t4 (a int, c char(127), index(a));
INSERT INTO t4 VALUES
(3,'abc'),(1,'foo'),(4,'def'),(8,'xxx'),(3,'yyy'),
(5,'zzz'),(9,'xyz'),(2,'yxz'),(5,'zxy'),(7,'zyx') ;
ANALYZE TABLE t1,t2,t3,t4;
Table	Op	Msg_type	Msg_text
test.t1	analyze	status	Engine-independent statistics collected
test.t1	analyze	status	OK
test.t2	analyze	status	Engine-independent statistics collected
test.t2	analyze	status	OK
test.t3	analyze	status	Engine-independent statistics collected
test.t3	analyze	status	OK
test.t4	analyze	status	Engine-independent statistics collected
test.t4	analyze	status	OK
CREATE VIEW v1 AS
SELECT c FROM t3
WHERE a IN ( SELECT t2.a FROM t1 JOIN t2 WHERE t1.i = t2.i ) GROUP BY c ;
set statement optimizer_switch='split_materialized=off' for SELECT * FROM t4 WHERE c IN ( SELECT c FROM v1 ) and a < 2;
a	c
1	foo
SELECT * FROM t4 WHERE c IN ( SELECT c FROM v1 ) and a < 2;
a	c
1	foo
explain extended SELECT * FROM t4 WHERE c IN ( SELECT c FROM v1 ) and a < 2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	PRIMARY	t4	range	a	a	5	NULL	1	100.00	Using index condition; Using where
1	PRIMARY	<derived3>	ref	key0	key0	128	test.t4.c	1	100.00	FirstMatch(t4)
3	LATERAL DERIVED	t3	ref	c	c	128	test.t4.c	2	100.00	
3	LATERAL DERIVED	<subquery4>	eq_ref	distinct_key	distinct_key	4	func	1	100.00	
4	MATERIALIZED	t1	ALL	NULL	NULL	NULL	NULL	3	100.00	
4	MATERIALIZED	t2	ALL	NULL	NULL	NULL	NULL	4	100.00	Using where; Using join buffer (flat, BNL join)
Warnings:
Note	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
explain format=json SELECT * FROM t4 WHERE c IN ( SELECT c FROM v1 ) and a < 2;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t4",
          "access_type": "range",
          "possible_keys": ["a"],
          "key": "a",
          "key_length": "5",
          "used_key_parts": ["a"],
          "loops": 1,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "index_condition": "t4.a < 2",
          "attached_condition": "t4.c is not null"
        }
      },
      {
        "table": {
          "table_name": "<derived3>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "128",
          "used_key_parts": ["c"],
          "ref": ["test.t4.c"],
          "loops": 1,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "first_match": "t4",
          "materialized": {
            "lateral": 1,
            "query_block": {
              "select_id": 3,
              "cost": "COST_REPLACED",
              "const_condition": "1",
              "outer_ref_condition": "t4.c is not null",
              "nested_loop": [
                {
                  "table": {
                    "table_name": "t3",
                    "access_type": "ref",
                    "possible_keys": ["c"],
                    "key": "c",
                    "key_length": "128",
                    "used_key_parts": ["c"],
                    "ref": ["test.t4.c"],
                    "loops": 1,
                    "rows": 2,
                    "cost": "COST_REPLACED",
                    "filtered": 100
                  }
                },
                {
                  "table": {
                    "table_name": "<subquery4>",
                    "access_type": "eq_ref",
                    "possible_keys": ["distinct_key"],
                    "key": "distinct_key",
                    "key_length": "4",
                    "used_key_parts": ["a"],
                    "ref": ["func"],
                    "rows": 1,
                    "filtered": 100,
                    "materialized": {
                      "unique": 1,
                      "query_block": {
                        "select_id": 4,
                        "nested_loop": [
                          {
                            "table": {
                              "table_name": "t1",
                              "access_type": "ALL",
                              "loops": 1,
                              "rows": 3,
                              "cost": "COST_REPLACED",
                              "filtered": 100
                            }
                          },
                          {
                            "block-nl-join": {
                              "table": {
                                "table_name": "t2",
                                "access_type": "ALL",
                                "loops": 3,
                                "rows": 4,
                                "cost": "COST_REPLACED",
                                "filtered": 100
                              },
                              "buffer_type": "flat",
                              "buffer_size": "65",
                              "join_type": "BNL",
                              "attached_condition": "t2.i = t1.i and t2.i = t1.i"
                            }
                          }
                        ]
                      }
                    }
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}
DROP VIEW v1;
DROP TABLE t1,t2,t3,t4;
#
# MDEV-13710: Optimization for equi-joins of grouping derived tables
#             (Splitting derived tables / views with GROUP BY) :
#             FROM list of the derived table contains constant tables
#
CREATE TABLE t1 (a int, INDEX(a)) ENGINE=MyISAM;
INSERT INTO t1 VALUES (9),(5),(1);
CREATE TABLE t2 (b int) ENGINE=MyISAM;
CREATE TABLE t3 (c varchar(8), d int) ENGINE=MyISAM;
INSERT INTO t3 VALUES ('foo',2),('bar',6);
CREATE VIEW v1 AS SELECT a FROM t1, t2 GROUP BY a;
SELECT * FROM t3
WHERE d IN ( SELECT * FROM v1 ) AND c LIKE 'z%' OR c IS NULL;
c	d
DROP VIEW v1;
DROP TABLE t1,t2,t3;
#
# MDEV-13734: Optimization for equi-joins of grouping derived tables
#             (Splitting derived tables / views with GROUP BY) :
#             derived table / view is empty
#
CREATE TABLE t1 (a int, b int, INDEX(a)) ENGINE=MyISAM;
CREATE TABLE t2 (c int) ENGINE=MyISAM;
CREATE VIEW v1 AS SELECT a, b FROM t1 STRAIGHT_JOIN t2;
CREATE VIEW v2 AS SELECT a, max(b) as bmax FROM v1 GROUP BY a;
CREATE VIEW v3 AS SELECT v2.* FROM t1 JOIN v2 ON t1.b = v2.bmax ;
SELECT * FROM v3 JOIN t1 ON (bmax = b);
a	bmax	a	b
DROP VIEW v1,v2,v3;
DROP TABLE t1,t2;
#
# MDEV-14845: Impossible where for derived with GROUP BY
#
CREATE TABLE t1 (pk INT PRIMARY KEY);
INSERT INTO t1 VALUES (1),(2);
WITH cte AS ( SELECT pk FROM t1 WHERE pk IS NULL GROUP BY pk )
SELECT * FROM cte;
pk
EXPLAIN EXTENDED WITH cte AS ( SELECT pk FROM t1 WHERE pk IS NULL GROUP BY pk )
SELECT * FROM cte;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	PRIMARY	<derived2>	system	NULL	NULL	NULL	NULL	0	0.00	Const row not found
2	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
Warnings:
Note	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`
DROP TABLE t1;
#
# MDEV-14880: assertion failure in optimizer when splitting is applied
#
CREATE TABLE t1 (pk1 INT PRIMARY KEY, f INT) ENGINE=Aria;
INSERT INTO t1 VALUES (1,0),(2,0);
CREATE TABLE t2 (pk2 INT PRIMARY KEY) ENGINE=Aria;
INSERT INTO t2 VALUES (1),(2),(3),(11),(12),(13);
CREATE VIEW v2 AS SELECT pk2, COUNT(*) AS cnt FROM t2 GROUP BY pk2;
SELECT * FROM t1 INNER JOIN v2 ON pk1 = pk2 WHERE f <> 5;
pk1	f	pk2	cnt
1	0	1	1
2	0	2	1
EXPLAIN EXTENDED SELECT * FROM t1 INNER JOIN v2 ON pk1 = pk2 WHERE f <> 5;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	PRIMARY	t1	ALL	PRIMARY	NULL	NULL	NULL	2	100.00	Using where
1	PRIMARY	<derived2>	ref	key0	key0	4	test.t1.pk1	1	100.00	
2	LATERAL DERIVED	t2	eq_ref	PRIMARY	PRIMARY	4	test.t1.pk1	1	100.00	Using index
Warnings:
Note	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
EXPLAIN FORMAT=JSON SELECT * FROM t1 INNER JOIN v2 ON pk1 = pk2 WHERE f <> 5;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t1",
          "access_type": "ALL",
          "possible_keys": ["PRIMARY"],
          "loops": 1,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t1.f <> 5"
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "4",
          "used_key_parts": ["pk2"],
          "ref": ["test.t1.pk1"],
          "loops": 2,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "materialized": {
            "lateral": 1,
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "nested_loop": [
                {
                  "table": {
                    "table_name": "t2",
                    "access_type": "eq_ref",
                    "possible_keys": ["PRIMARY"],
                    "key": "PRIMARY",
                    "key_length": "4",
                    "used_key_parts": ["pk2"],
                    "ref": ["test.t1.pk1"],
                    "loops": 1,
                    "rows": 1,
                    "cost": "COST_REPLACED",
                    "filtered": 100,
                    "using_index": true
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}
DROP VIEW v2;
DROP TABLE t1,t2;
#
# MDEV-15017: splittable table is constant table
#
CREATE TABLE t1 (a INT) ENGINE=MyISAM;
CREATE TABLE t2 (pk INT, b INT, PRIMARY KEY (pk)) ENGINE=MyISAM;
INSERT INTO t2 VALUES (1,2),(3,4);
CREATE VIEW v2 AS SELECT pk, MIN(b) FROM t2 GROUP BY pk;
SELECT * FROM t1 LEFT JOIN v2 ON (a = pk);
a	pk	MIN(b)
DROP VIEW v2;
DROP TABLE t1,t2;
#
# MDEV-14994: splittable table with no rows
#
CREATE TABLE t1 (f INT PRIMARY KEY) ENGINE=MyISAM;
CREATE VIEW v1 AS SELECT a.* FROM t1 AS a STRAIGHT_JOIN t1 AS b;
CREATE VIEW v2 AS SELECT f FROM v1 GROUP BY f;
SELECT * FROM v1 JOIN v2 ON v1.f = v2.f;
f	f
EXPLAIN EXTENDED
SELECT * FROM v1 JOIN v2 ON v1.f = v2.f;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE noticed after reading const tables
3	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
Warnings:
Note	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
DROP VIEW v1,v2;
DROP TABLE t1;
#
# MDEV-15899: derived with WF without any key access
#
create table t1 (f1 int, f2 int, f4 int);
insert into t1 values
(3,1,1), (3,0,9), (0,1,8), (9,0,0), (3,0,9);
with
cte as (select  median(f2) over (partition by f1) as k1 from t1 order by f1),
cte1 as (select median(f4) over (partition by f1) as k2 from t1)
select k1,k2 from cte1, cte;
k1	k2
0.0000000000	0.0000000000
0.0000000000	0.0000000000
0.0000000000	0.0000000000
0.0000000000	0.0000000000
0.0000000000	8.0000000000
0.0000000000	8.0000000000
0.0000000000	8.0000000000
0.0000000000	8.0000000000
0.0000000000	9.0000000000
0.0000000000	9.0000000000
0.0000000000	9.0000000000
0.0000000000	9.0000000000
0.0000000000	9.0000000000
0.0000000000	9.0000000000
0.0000000000	9.0000000000
0.0000000000	9.0000000000
0.0000000000	9.0000000000
0.0000000000	9.0000000000
0.0000000000	9.0000000000
0.0000000000	9.0000000000
1.0000000000	0.0000000000
1.0000000000	8.0000000000
1.0000000000	9.0000000000
1.0000000000	9.0000000000
1.0000000000	9.0000000000
explain with
cte as (select  median(f2) over (partition by f1) as k1 from t1 order by f1),
cte1 as (select median(f4) over (partition by f1) as k2 from t1)
select k1,k2 from cte1, cte;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	<derived3>	ALL	NULL	NULL	NULL	NULL	5	
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	5	Using join buffer (flat, BNL join)
3	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	5	Using temporary
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	5	Using temporary; Using filesort
drop table t1;
#
# MDEV-16104: embedded splittable materialized derived/views
#
CREATE TABLE t1 (f int PRIMARY KEY) ENGINE=MyISAM;
INSERT INTO t1
VALUES (3), (7), (1), (4), (8), (5), (9);
CREATE ALGORITHM=MERGE VIEW v1 AS
SELECT a2.*
FROM
( SELECT f, COUNT(*) as c FROM t1 GROUP BY f ) AS a1
JOIN
t1 AS a2
USING (f);
EXPLAIN EXTENDED
SELECT * FROM ( SELECT STRAIGHT_JOIN f, COUNT(*) as c FROM v1 GROUP BY f ) AS s;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	7	100.00	
2	DERIVED	<derived4>	ALL	NULL	NULL	NULL	NULL	7	100.00	Using temporary; Using filesort
2	DERIVED	a2	eq_ref	PRIMARY	PRIMARY	4	a1.f	1	100.00	Using index
4	DERIVED	t1	index	PRIMARY	PRIMARY	4	NULL	7	100.00	Using index; Using temporary; Using filesort
Warnings:
Note	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`
SELECT * FROM ( SELECT STRAIGHT_JOIN f, COUNT(*) as c FROM v1 GROUP BY f ) AS s;
f	c
1	1
3	1
4	1
5	1
7	1
8	1
9	1
DROP VIEW v1;
DROP TABLE t1;
#
# MDEV-16801: splittable materialized derived/views with
#             one grouping field from table without keys
#
CREATE TABLE t1 (a int, b int, INDEX idx_a(a), INDEX idx_b(b)) ENGINE=MYISAM;
CREATE TABLE t2 (c int) ENGINE=MYISAM;
CREATE TABLE t3 (d int) ENGINE=MYISAM;
INSERT INTO t1 VALUES
(77,7), (11,1), (33,3), (44,4), (8,88),
(78,7), (98,9), (38,3), (28,2), (79,7),
(58,5), (42,4), (71,7), (27,2), (91,9);
INSERT INTO t1 SELECT a+100, b+10 FROM t1;
INSERT INTO t2 VALUES
(100), (700), (200), (100), (200);
INSERT INTO t3 VALUES
(3), (4), (1), (8), (3);
ANALYZE tables t1,t2,t3;
Table	Op	Msg_type	Msg_text
test.t1	analyze	status	Engine-independent statistics collected
test.t1	analyze	status	OK
test.t2	analyze	status	Engine-independent statistics collected
test.t2	analyze	status	OK
test.t3	analyze	status	Engine-independent statistics collected
test.t3	analyze	status	OK
SELECT *
FROM t3,
(SELECT t1.b, t2.c
FROM  t1, t2
GROUP BY t1.b,t2.c) dt
WHERE t3.d = dt.b;
d	b	c
3	3	700
3	3	200
3	3	100
4	4	700
4	4	200
4	4	100
1	1	700
1	1	200
1	1	100
3	3	700
3	3	200
3	3	100
EXPLAIN EXTENDED SELECT *
FROM t3,
(SELECT t1.b, t2.c
FROM  t1, t2
GROUP BY t1.b,t2.c) dt
WHERE t3.d = dt.b;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	5	100.00	Using where
1	PRIMARY	<derived2>	ref	key0	key0	5	test.t3.d	1	100.00	
2	LATERAL DERIVED	t1	ref	idx_b	idx_b	5	test.t3.d	1	100.00	Using index; Using temporary; Using filesort
2	LATERAL DERIVED	t2	ALL	NULL	NULL	NULL	NULL	5	100.00	Using join buffer (flat, BNL join)
Warnings:
Note	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`
DROP TABLE t1,t2,t3;
#
# MDEV-17419: splittable materialized derived/view
#             when join_cache_level = 4
#
set join_cache_level = 4;
CREATE TABLE t1 (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
username VARCHAR(50) NULL DEFAULT '0',
PRIMARY KEY (id)
) COLLATE='utf8_general_ci';
CREATE TABLE t2 (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
userid INT UNSIGNED NOT NULL,
logindate DATETIME NOT NULL,
PRIMARY KEY (id)
) COLLATE='utf8_general_ci';
INSERT INTO t1 (id, username) VALUES
(1,"user1"), (2, "user2");
INSERT INTO t2 (id, userid, logindate) VALUES
(1,1,"2015-06-19 12:17:02.828"),
(2,1,"2016-06-19 12:17:02.828"),
(3,2,"2017-06-19 12:17:02.828"),
(4,2,"2018-06-19 12:17:02.828");
EXPLAIN select * from t1 as u
left join
(select * from t2 as au group by au.userid) as auditlastlogin
on u.id=auditlastlogin.userid;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	u	ALL	NULL	NULL	NULL	NULL	2	
1	PRIMARY	<derived2>	ref	key0	key0	5	test.u.id	1	
2	DERIVED	au	ALL	NULL	NULL	NULL	NULL	4	Using temporary; Using filesort
select * from t1 as u
left join
(select * from t2 as au group by au.userid) as auditlastlogin
on u.id=auditlastlogin.userid;
id	username	id	userid	logindate
1	user1	1	1	2015-06-19 12:17:02
2	user2	3	2	2017-06-19 12:17:02
set join_cache_level=default;
DROP TABLE t1,t2;
#
# MDEV-21614: potentially splittable materialized derived/view
#             within materialized semi-join
#
create table t1 (
id int not null auto_increment primary key,
a int not null
) engine=myisam;
create table t2 (
id int not null auto_increment primary key,
ro_id int not null,
flag int not null, key (ro_id)
) engine=myisam;
insert into t1(a) select seq+100 from seq_1_to_20;
insert into t2(ro_id,flag) select seq, 1 from seq_1_to_20;
insert into t2(ro_id,flag) select seq, 0 from seq_1_to_20;
create view v1 as
select t1.* from t1 left join t2 on (t1.id = t2.ro_id AND t2.flag = 1)
group by t1.id;
select id, a from t1 where  id in (select id from v1);
id	a
1	101
2	102
3	103
4	104
5	105
6	106
7	107
8	108
9	109
10	110
11	111
12	112
13	113
14	114
15	115
16	116
17	117
18	118
19	119
20	120
explain extended select id, a from t1 where  id in (select id from v1);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	PRIMARY	t1	ALL	PRIMARY	NULL	NULL	NULL	20	100.00	
1	PRIMARY	<derived3>	ref	key0	key0	4	test.t1.id	2	50.00	FirstMatch(t1)
3	DERIVED	t1	ALL	PRIMARY	NULL	NULL	NULL	20	100.00	Using temporary; Using filesort
3	DERIVED	t2	ref	ro_id	ro_id	4	test.t1.id	1	100.00	Using where
Warnings:
Note	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`
select id, a from t1
where id in (select id
from (select t1.* from t1 left join t2
on (t1.id = t2.ro_id AND t2.flag = 1)
group by t1.id) dt);
id	a
1	101
2	102
3	103
4	104
5	105
6	106
7	107
8	108
9	109
10	110
11	111
12	112
13	113
14	114
15	115
16	116
17	117
18	118
19	119
20	120
explain extended select id, a from t1
where id in (select id
from (select t1.* from t1 left join t2
on (t1.id = t2.ro_id AND t2.flag = 1)
group by t1.id) dt);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	PRIMARY	t1	ALL	PRIMARY	NULL	NULL	NULL	20	100.00	
1	PRIMARY	<derived3>	ref	key1,distinct_key	key1	4	test.t1.id	2	50.00	FirstMatch(t1)
3	DERIVED	t1	ALL	PRIMARY	NULL	NULL	NULL	20	100.00	Using temporary; Using filesort
3	DERIVED	t2	ref	ro_id	ro_id	4	test.t1.id	1	100.00	Using where
Warnings:
Note	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 1 group by `test`.`t1`.`id`) `dt`) where `dt`.`id` = `test`.`t1`.`id`
drop view v1;
drop table t1,t2;
#
# MDEV-21883: potentially splittable materialized derived
#             that uses a join of 32 tables
#
CREATE TABLE t (id INT NOT NULL PRIMARY KEY);
INSERT INTO t values (1),(2),(3);
set statement optimizer_switch='split_materialized=off' for SELECT t.id FROM t
LEFT JOIN (
SELECT t0.id FROM t AS t0
LEFT JOIN t AS t1 ON 0=1
LEFT JOIN t AS t2 ON 0=1
LEFT JOIN t AS t3 ON 0=1
LEFT JOIN t AS t4 ON 0=1
LEFT JOIN t AS t5 ON 0=1
LEFT JOIN t AS t6 ON 0=1
LEFT JOIN t AS t7 ON 0=1
LEFT JOIN t AS t8 ON 0=1
LEFT JOIN t AS t9 ON 0=1
LEFT JOIN t AS t10 ON 0=1
LEFT JOIN t AS t11 ON 0=1
LEFT JOIN t AS t12 ON 0=1
LEFT JOIN t AS t13 ON 0=1
LEFT JOIN t AS t14 ON 0=1
LEFT JOIN t AS t15 ON 0=1
LEFT JOIN t AS t16 ON 0=1
LEFT JOIN t AS t17 ON 0=1
LEFT JOIN t AS t18 ON 0=1
LEFT JOIN t AS t19 ON 0=1
LEFT JOIN t AS t20 ON 0=1
LEFT JOIN t AS t21 ON 0=1
LEFT JOIN t AS t22 ON 0=1
LEFT JOIN t AS t23 ON 0=1
LEFT JOIN t AS t24 ON 0=1
LEFT JOIN t AS t25 ON 0=1
LEFT JOIN t AS t26 ON 0=1
LEFT JOIN t AS t27 ON 0=1
LEFT JOIN t AS t28 ON 0=1
LEFT JOIN t AS t29 ON 0=1
LEFT JOIN t AS t30 ON 0=1
LEFT JOIN t AS t31 ON 0=1
GROUP BY t0.id) AS dt ON dt.id = t.id;
id
1
2
3
set statement optimizer_switch='split_materialized=on' for SELECT t.id FROM t
LEFT JOIN (
SELECT t0.id FROM t AS t0
LEFT JOIN t AS t1 ON 0=1
LEFT JOIN t AS t2 ON 0=1
LEFT JOIN t AS t3 ON 0=1
LEFT JOIN t AS t4 ON 0=1
LEFT JOIN t AS t5 ON 0=1
LEFT JOIN t AS t6 ON 0=1
LEFT JOIN t AS t7 ON 0=1
LEFT JOIN t AS t8 ON 0=1
LEFT JOIN t AS t9 ON 0=1
LEFT JOIN t AS t10 ON 0=1
LEFT JOIN t AS t11 ON 0=1
LEFT JOIN t AS t12 ON 0=1
LEFT JOIN t AS t13 ON 0=1
LEFT JOIN t AS t14 ON 0=1
LEFT JOIN t AS t15 ON 0=1
LEFT JOIN t AS t16 ON 0=1
LEFT JOIN t AS t17 ON 0=1
LEFT JOIN t AS t18 ON 0=1
LEFT JOIN t AS t19 ON 0=1
LEFT JOIN t AS t20 ON 0=1
LEFT JOIN t AS t21 ON 0=1
LEFT JOIN t AS t22 ON 0=1
LEFT JOIN t AS t23 ON 0=1
LEFT JOIN t AS t24 ON 0=1
LEFT JOIN t AS t25 ON 0=1
LEFT JOIN t AS t26 ON 0=1
LEFT JOIN t AS t27 ON 0=1
LEFT JOIN t AS t28 ON 0=1
LEFT JOIN t AS t29 ON 0=1
LEFT JOIN t AS t30 ON 0=1
LEFT JOIN t AS t31 ON 0=1
GROUP BY t0.id) AS dt ON dt.id = t.id;
id
1
2
3
DROP TABLE t;
#
# MDEV-23804: Server crashes in st_select_lex::collect_grouping_fields_for_derived
#
CREATE TABLE t1 (a INT);
INSERT INTO t1 VALUES (3),(4);
CREATE VIEW v1 AS SELECT a FROM t1 UNION VALUES (3),(4);
ANALYZE FORMAT=JSON SELECT * from v1 WHERE a=3;
ANALYZE
{
  "query_optimization": {
    "r_total_time_ms": "REPLACED"
  },
  "query_block": {
    "select_id": 1,
    "cost": "REPLACED",
    "r_loops": 1,
    "r_total_time_ms": "REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "const",
          "possible_keys": ["distinct_key"],
          "key": "distinct_key",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["const"],
          "loops": 1,
          "r_loops": 1,
          "rows": 1,
          "r_rows": 1,
          "cost": "REPLACED",
          "r_table_time_ms": "REPLACED",
          "r_other_time_ms": "REPLACED",
          "filtered": 100,
          "r_filtered": 100,
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<union2,3>",
                "access_type": "ALL",
                "r_loops": 1,
                "r_rows": 2,
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "REPLACED",
                      "r_loops": 1,
                      "r_total_time_ms": "REPLACED",
                      "nested_loop": [
                        {
                          "table": {
                            "table_name": "t1",
                            "access_type": "ALL",
                            "loops": 1,
                            "r_loops": 1,
                            "rows": 2,
                            "r_rows": 2,
                            "cost": "REPLACED",
                            "r_table_time_ms": "REPLACED",
                            "r_other_time_ms": "REPLACED",
                            "r_engine_stats": REPLACED,
                            "filtered": 100,
                            "r_filtered": 50,
                            "attached_condition": "t1.a = 3"
                          }
                        }
                      ]
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "UNION",
                      "table": {
                        "message": "No tables used"
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
SELECT * from v1 WHERE a=3;
a
3
DROP VIEW v1;
DROP TABLE t1;
#
# MDEV-25128: Split optimization for join with materialized semi-join
#
create table t1 (id int, a int, index (a), index (id, a)) engine=myisam;
insert into t1 values
(17,1),(17,3010),(17,3013),(17,3053),(21,2446),(21,2467),(21,2);
create table t2 (a int) engine=myisam;
insert into t2 values (1),(2),(3),(1000),(2000),(3000);
insert into t2 select 5000 from seq_5000_to_6000;
create  table t3 (id int) engine=myisam;
insert into t3 values (1),(2);
analyze table t1,t2,t3;
Table	Op	Msg_type	Msg_text
test.t1	analyze	status	Engine-independent statistics collected
test.t1	analyze	status	OK
test.t2	analyze	status	Engine-independent statistics collected
test.t2	analyze	status	OK
test.t3	analyze	status	Engine-independent statistics collected
test.t3	analyze	status	OK
set optimizer_switch="split_materialized=off";
select * from t1, (select a from t1 cp2 group by a) dt, t3
where dt.a = t1.a and t1.a = t3.id and t1.a in (select a from t2);
id	a	a	id
17	1	1	1
21	2	2	2
explain select * from t1, (select a from t1 cp2 group by a) dt, t3
where dt.a = t1.a and t1.a = t3.id and t1.a in (select a from t2);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where
1	PRIMARY	t1	ref	a	a	5	test.t3.id	1	
1	PRIMARY	<derived2>	ref	key0	key0	5	test.t3.id	1	
1	PRIMARY	<subquery3>	eq_ref	distinct_key	distinct_key	4	func	1	
3	MATERIALIZED	t2	ALL	NULL	NULL	NULL	NULL	1007	
2	DERIVED	cp2	range	NULL	a	5	NULL	7	Using index for group-by
explain format=json select * from t1, (select a from t1 cp2 group by a) dt, t3
where dt.a = t1.a and t1.a = t3.id and t1.a in (select a from t2);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t3",
          "access_type": "ALL",
          "loops": 1,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t3.`id` is not null and t3.`id` is not null"
        }
      },
      {
        "table": {
          "table_name": "t1",
          "access_type": "ref",
          "possible_keys": ["a"],
          "key": "a",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t3.id"],
          "loops": 2,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t3.id"],
          "loops": 2,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "nested_loop": [
                {
                  "table": {
                    "table_name": "cp2",
                    "access_type": "range",
                    "key": "a",
                    "key_length": "5",
                    "used_key_parts": ["a"],
                    "loops": 1,
                    "rows": 7,
                    "cost": "COST_REPLACED",
                    "filtered": 100,
                    "using_index_for_group_by": true
                  }
                }
              ]
            }
          }
        }
      },
      {
        "table": {
          "table_name": "<subquery3>",
          "access_type": "eq_ref",
          "possible_keys": ["distinct_key"],
          "key": "distinct_key",
          "key_length": "4",
          "used_key_parts": ["a"],
          "ref": ["func"],
          "rows": 1,
          "filtered": 100,
          "materialized": {
            "unique": 1,
            "query_block": {
              "select_id": 3,
              "nested_loop": [
                {
                  "table": {
                    "table_name": "t2",
                    "access_type": "ALL",
                    "loops": 1,
                    "rows": 1007,
                    "cost": "COST_REPLACED",
                    "filtered": 100
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}
set optimizer_switch="split_materialized=default";
select * from t1, (select a from t1 cp2 group by a) dt, t3
where dt.a = t1.a and t1.a = t3.id and t1.a in (select a from t2);
id	a	a	id
17	1	1	1
21	2	2	2
explain select * from t1, (select a from t1 cp2 group by a) dt, t3
where dt.a = t1.a and t1.a = t3.id and t1.a in (select a from t2);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where
1	PRIMARY	t1	ref	a	a	5	test.t3.id	1	
1	PRIMARY	<derived2>	ref	key0	key0	5	test.t3.id	1	
1	PRIMARY	<subquery3>	eq_ref	distinct_key	distinct_key	4	func	1	
3	MATERIALIZED	t2	ALL	NULL	NULL	NULL	NULL	1007	
2	LATERAL DERIVED	cp2	ref	a	a	5	test.t1.a	1	Using where; Using index
explain format=json select * from t1, (select a from t1 cp2 group by a) dt, t3
where dt.a = t1.a and t1.a = t3.id and t1.a in (select a from t2);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t3",
          "access_type": "ALL",
          "loops": 1,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t3.`id` is not null and t3.`id` is not null"
        }
      },
      {
        "table": {
          "table_name": "t1",
          "access_type": "ref",
          "possible_keys": ["a"],
          "key": "a",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t3.id"],
          "loops": 2,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t3.id"],
          "loops": 2,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "materialized": {
            "lateral": 1,
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "outer_ref_condition": "t1.a is not null",
              "nested_loop": [
                {
                  "table": {
                    "table_name": "cp2",
                    "access_type": "ref",
                    "possible_keys": ["a"],
                    "key": "a",
                    "key_length": "5",
                    "used_key_parts": ["a"],
                    "ref": ["test.t1.a"],
                    "loops": 1,
                    "rows": 1,
                    "cost": "COST_REPLACED",
                    "filtered": 100,
                    "attached_condition": "cp2.a = t3.`id`",
                    "using_index": true
                  }
                }
              ]
            }
          }
        }
      },
      {
        "table": {
          "table_name": "<subquery3>",
          "access_type": "eq_ref",
          "possible_keys": ["distinct_key"],
          "key": "distinct_key",
          "key_length": "4",
          "used_key_parts": ["a"],
          "ref": ["func"],
          "rows": 1,
          "filtered": 100,
          "materialized": {
            "unique": 1,
            "query_block": {
              "select_id": 3,
              "nested_loop": [
                {
                  "table": {
                    "table_name": "t2",
                    "access_type": "ALL",
                    "loops": 1,
                    "rows": 1007,
                    "cost": "COST_REPLACED",
                    "filtered": 100
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}
prepare stmt from "select * from t1, (select a from t1 cp2 group by a) dt, t3
where dt.a = t1.a and t1.a = t3.id and t1.a in (select a from t2)";
execute stmt;
id	a	a	id
17	1	1	1
21	2	2	2
execute stmt;
id	a	a	id
17	1	1	1
21	2	2	2
deallocate prepare stmt;
drop table t1,t2,t3;
#
# MDEV-27132: Splittable derived with equality in WHERE
#
CREATE TABLE t1 (
id int PRIMARY KEY
) ENGINE=MyISAM;
INSERT INTO t1 VALUES
(-1),(2070),(4826),(4827),(4828),(4829),(4830),(4831),(4832),(4833),(4834),
(4835),(4836),(4837),(4838),(4839),(4840),(4841),(4842),(4843),(4844),
(4845),(4846),(4847),(4848),(4849),(4850),(4851),(4852),(4853),(4854),
(4855),(4856),(4857),(4858),(4859),(4860),(4861),(4862),(4863),(4864),
(4865),(4866),(4867),(4868),(4869),(4870),(4871),(4872),(4873),(4874),
(4875),(4876);
CREATE TABLE t2 (
id int PRIMARY KEY AUTO_INCREMENT,
deleted int(1),
t1_id int,
email varchar(255),
reporting_person int(1),
KEY t1_id (t1_id)
) ENGINE=MyISAM;
INSERT INTO t2 VALUES
(1,0,2064,'1test@test.ee',1),(2,1626095588,2066,'2test@test.ee',1),
(3,0,2066,'3test@test.ee',1),(4,0,2068,'4test@test.ee',1),
(5,0,2068,'5test@test.ee',1),(6,0,2069,'6test@test.ee',1),(7,0,2070,'',0),
(8,0,2070,'',0),(9,0,2071,'',0),(10,0,2071,'',0),(11,0,2072,'',0),
(12,0,2072,'',0),(13,0,2072,'13test@test.ee',1),(14,0,2073,'14test@test.ee',1),
(15,0,2074,'15test@test.ee',1),(16,0,2075,'16test@test.ee',1),(17,0,2075,'',0),
(18,0,2075,'',0),(19,0,2076,'19test@test.ee',1),(20,0,2077,'',0),
(21,0,2078,'21test@test.ee',1),(22,0,2078,'22test@test.ee',1);
INSERT INTO t2(deleted, t1_id, email, reporting_person)
SELECT deleted, t1_id, email, reporting_person FROM t2;
INSERT INTO t2(deleted, t1_id, email, reporting_person)
SELECT deleted, t1_id+10000, email, reporting_person FROM t2;
INSERT INTO t2(deleted, t1_id, email, reporting_person)
SELECT deleted, t1_id+20000, email, reporting_person FROM t2;
INSERT INTO t2(deleted, t1_id, email, reporting_person)
SELECT deleted, t1_id+40000, email, reporting_person FROM t2;
INSERT INTO t2(deleted, t1_id, email, reporting_person)
SELECT deleted, t1_id+80000, email, reporting_person FROM t2;
INSERT INTO t2(deleted, t1_id, email, reporting_person)
SELECT deleted, t1_id+160000, email, reporting_person FROM t2;
insert into t2 (id,t1_id) select -seq,-seq from seq_1_to_1000;
CREATE TABLE t3 (
id int PRIMARY KEY,
deleted int,
t1_id int,
YEAR int(4),
quarter int(1),
KEY t1_id (t1_id,year,quarter)
) ENGINE=MyISAM;
INSERT INTO t3 VALUES
(1,0,3885,2020,1),(2,0,2064,2020,1),(3,1611670734,2225,2020,1),
(4,0,2070,2020,1),(5,1611055981,2095,2020,1),(6,1610970096,2102,2020,1),
(7,0,3974,2020,1),(153,1609851928,3892,2020,2),(154,0,3885,2020,2),
(155,0,2064,2020,2),(156,1611670717,2225,2020,2),(157,0,2070,2020,2),
(317,0,2257,2020,2),(318,0,3885,2020,3),(319,0,2064,2020,3),
(320,1611670709,2225,2020,3),(321,0,2070,2020,3),(322,0,2095,2020,3),
(323,0,2102,2020,3),(324,0,3974,2020,3),(325,0,3886,2020,3),
(326,1609939963,2104,2020,3),(327,0,3887,2020,3),(328,0,3888,2020,3),
(329,0,2148,2020,3),(330,0,3889,2020,3),(331,0,3890,2020,3),
(332,0,2179,2020,3),(333,0,2115,2020,3),(334,0,2193,2020,3),
(335,0,2213,2020,3),(336,0,3891,2020,3),(337,1609851955,3892,2020,3),
(338,1610447706,2232,2020,3),(339,0,2235,2020,3),(340,0,2237,2020,3),
(341,0,3972,2020,3),(342,1610449357,2242,2020,3),(343,0,3893,2020,3),
(344,0,2257,2020,3),(345,0,3951,2020,3),(346,0,3894,2020,3),
(347,0,3912,2020,3),(348,0,3895,2020,3),(349,0,2301,2020,3),
(350,0,2304,2020,3),(351,0,3896,2020,3);
ANALYZE TABLE t1,t2,t3;
Table	Op	Msg_type	Msg_text
test.t1	analyze	status	Engine-independent statistics collected
test.t1	analyze	status	OK
test.t2	analyze	status	Engine-independent statistics collected
test.t2	analyze	status	OK
test.t3	analyze	status	Engine-independent statistics collected
test.t3	analyze	status	OK
set  optimizer_switch='split_materialized=on';
SELECT t1.id
FROM t1
JOIN t3
ON t3.t1_id = t1.id
JOIN (SELECT t1_id FROM t2 WHERE reporting_person = 1 GROUP BY t1_id) tx
ON tx.t1_id = t1.id
WHERE t1.id BETWEEN 200 AND 100000;
id
EXPLAIN SELECT t1.id
FROM t1
JOIN t3
ON t3.t1_id = t1.id
JOIN (SELECT t1_id FROM t2 WHERE reporting_person = 1 GROUP BY t1_id) tx
ON tx.t1_id = t1.id
WHERE t1.id BETWEEN 200 AND 100000;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t3	range	t1_id	t1_id	5	NULL	47	Using where; Using index
1	PRIMARY	t1	eq_ref	PRIMARY	PRIMARY	4	test.t3.t1_id	1	Using index
1	PRIMARY	<derived2>	ref	key0	key0	5	test.t3.t1_id	1	
2	LATERAL DERIVED	t2	ref	t1_id	t1_id	5	test.t1.id	1	Using index condition; Using where
EXPLAIN FORMAT=JSON SELECT t1.id
FROM t1
JOIN t3
ON t3.t1_id = t1.id
JOIN (SELECT t1_id FROM t2 WHERE reporting_person = 1 GROUP BY t1_id) tx
ON tx.t1_id = t1.id
WHERE t1.id BETWEEN 200 AND 100000;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t3",
          "access_type": "range",
          "possible_keys": ["t1_id"],
          "key": "t1_id",
          "key_length": "5",
          "used_key_parts": ["t1_id"],
          "loops": 1,
          "rows": 47,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "attached_condition": "t3.t1_id between 200 and 100000 and t3.t1_id is not null",
          "using_index": true
        }
      },
      {
        "table": {
          "table_name": "t1",
          "access_type": "eq_ref",
          "possible_keys": ["PRIMARY"],
          "key": "PRIMARY",
          "key_length": "4",
          "used_key_parts": ["id"],
          "ref": ["test.t3.t1_id"],
          "loops": 47,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "using_index": true
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "5",
          "used_key_parts": ["t1_id"],
          "ref": ["test.t3.t1_id"],
          "loops": 47,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "materialized": {
            "lateral": 1,
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "nested_loop": [
                {
                  "table": {
                    "table_name": "t2",
                    "access_type": "ref",
                    "possible_keys": ["t1_id"],
                    "key": "t1_id",
                    "key_length": "5",
                    "used_key_parts": ["t1_id"],
                    "ref": ["test.t1.id"],
                    "loops": 1,
                    "rows": 1,
                    "cost": "COST_REPLACED",
                    "filtered": 34.55045319,
                    "index_condition": "t2.t1_id between 200 and 100000 and t2.t1_id = t3.t1_id",
                    "attached_condition": "t2.reporting_person = 1"
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}
set  optimizer_switch='split_materialized=off';
SELECT t1.id
FROM t1
JOIN t3
ON t3.t1_id = t1.id
JOIN (SELECT t1_id FROM t2 WHERE reporting_person = 1 GROUP BY t1_id) tx
ON tx.t1_id = t1.id
WHERE t1.id BETWEEN 200 AND 100000;
id
EXPLAIN SELECT t1.id
FROM t1
JOIN t3
ON t3.t1_id = t1.id
JOIN (SELECT t1_id FROM t2 WHERE reporting_person = 1 GROUP BY t1_id) tx
ON tx.t1_id = t1.id
WHERE t1.id BETWEEN 200 AND 100000;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t3	range	t1_id	t1_id	5	NULL	47	Using where; Using index
1	PRIMARY	t1	eq_ref	PRIMARY	PRIMARY	4	test.t3.t1_id	1	Using index
1	PRIMARY	<derived2>	ref	key0	key0	5	test.t3.t1_id	10	
2	DERIVED	t2	ALL	t1_id	NULL	NULL	NULL	2408	Using where; Using temporary; Using filesort
set  optimizer_switch='split_materialized=default';
DROP TABLE t1,t2,t3;
#
# MDEV-27510: Splittable derived with grouping over two tables
#
CREATE TABLE ledgers (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(32)
) ENGINE=MyISAM;
CREATE TABLE charges (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
from_ledger_id BIGINT UNSIGNED NOT NULL,
to_ledger_id BIGINT UNSIGNED NOT NULL,
amount INT NOT NULL,
KEY fk_charge_from_ledger (from_ledger_id),
KEY fk_charge_to_ledger (to_ledger_id)
) ENGINE=MyISAM;
CREATE TABLE transactions (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
ledger_id BIGINT UNSIGNED NOT NULL,
KEY fk_transactions_ledger (ledger_id)
) ENGINE=MyISAM;
CREATE TABLE transaction_items (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
transaction_id BIGINT UNSIGNED NOT NULL,
charge_id BIGINT UNSIGNED,
amount INT NOT NULL,
KEY fk_items_transaction (transaction_id),
KEY fk_items_charge (charge_id)
) ENGINE=MyISAM;
INSERT INTO ledgers (id, name) VALUES
(1, 'Anna'), (2, 'John'), (3, 'Fred');
INSERT INTO charges (id, from_ledger_id, to_ledger_id, amount) VALUES
(1, 2, 1, 200), (2, 1, 2, 330), (3, 1, 2, 640), (4, 3, 1, 640), (5, 3, 2, 1000),
(6, 3, 1, 660), (7, 2, 3, 650), (8, 3, 2, 160), (9, 2, 1, 740), (10, 3, 2, 310),
(11, 2, 1, 640), (12, 3, 2, 240), (13, 3, 2, 340), (14, 2, 1, 720),
(15, 2, 3, 100),
(16, 2, 3, 980), (17, 2, 1, 80), (18, 1, 2, 760), (19, 2, 3, 740),
(20, 2, 1, 990);
INSERT INTO transactions (id, ledger_id) VALUES
(2, 1), (3, 1), (5, 1), (8, 1), (12, 1), (18, 1), (22, 1), (28, 1),
(34, 1), (35, 1),
(40, 1), (1, 2), (4, 2), (6, 2), (10, 2), (13, 2), (16, 2), (17, 2),
(20, 2), (21, 2),
(24, 2), (26, 2), (27, 2), (29, 2), (31, 2), (33, 2), (36, 2), (37, 2),
(39, 2), (7, 3),
(9, 3), (11, 3), (14, 3), (15, 3), (19, 3), (23, 3), (25, 3), (30, 3),
(32, 3), (38, 3);
INSERT INTO transaction_items (id, transaction_id, charge_id, amount) VALUES
(1, 1, 1, -200), (2, 2, 1, 200), (3, 3, 2, -330), (4, 4, 2, 330),
(5, 5, 3, -640),
(6, 6, 3, 640), (7, 7, 4, -640), (8, 8, 4, 640), (9, 9, 5, -1000),
(10, 10, 5, 1000),
(11, 11, 6, -660), (12, 12, 6, 660), (13, 13, 7, -650), (14, 14, 7, 650),
(15, 15, 8, -160),
(16, 16, 8, 160), (17, 17, 9, -740), (18, 18, 9, 740), (19, 19, 10, -310),
(20, 20, 10, 310),
(21, 21, 11, -640), (22, 22, 11, 640), (23, 23, 12, -240), (24, 24, 12, 240),
(25, 25, 13, -340),
(26, 26, 13, 340), (27, 27, 14, -720), (28, 28, 14, 720), (29, 29, 15, -100),
(30, 30, 15, 100),
(31, 31, 16, -980), (32, 32, 16, 980), (33, 33, 17, -80), (34, 34, 17, 80),
(35, 35, 18, -760),
(36, 36, 18, 760), (37, 37, 19, -740), (38, 38, 19, 740), (39, 39, 20, -990),
(40, 40, 20, 990);
ANALYZE TABLE ledgers, charges, transactions, transaction_items;
Table	Op	Msg_type	Msg_text
test.ledgers	analyze	status	Engine-independent statistics collected
test.ledgers	analyze	status	OK
test.charges	analyze	status	Engine-independent statistics collected
test.charges	analyze	status	OK
test.transactions	analyze	status	Engine-independent statistics collected
test.transactions	analyze	status	OK
test.transaction_items	analyze	status	Engine-independent statistics collected
test.transaction_items	analyze	status	OK
set optimizer_switch='split_materialized=on';
SELECT
charges.id,
charges.from_ledger_id,
charges.to_ledger_id,
from_agg_items.num_rows AS from_num_rows
FROM charges
INNER JOIN (
SELECT
transactions.ledger_id,
transaction_items.charge_id,
count(*) as num_rows
FROM transaction_items
INNER JOIN transactions ON transaction_items.transaction_id = transactions.id
GROUP BY transactions.ledger_id, transaction_items.charge_id
) AS from_agg_items
ON from_agg_items.charge_id = charges.id AND
from_agg_items.ledger_id = charges.from_ledger_id
WHERE charges.to_ledger_id = 2;
id	from_ledger_id	to_ledger_id	from_num_rows
2	1	2	1
3	1	2	1
5	3	2	1
8	3	2	1
10	3	2	1
12	3	2	1
13	3	2	1
18	1	2	1
EXPLAIN SELECT
charges.id,
charges.from_ledger_id,
charges.to_ledger_id,
from_agg_items.num_rows AS from_num_rows
FROM charges
INNER JOIN (
SELECT
transactions.ledger_id,
transaction_items.charge_id,
count(*) as num_rows
FROM transaction_items
INNER JOIN transactions ON transaction_items.transaction_id = transactions.id
GROUP BY transactions.ledger_id, transaction_items.charge_id
) AS from_agg_items
ON from_agg_items.charge_id = charges.id AND
from_agg_items.ledger_id = charges.from_ledger_id
WHERE charges.to_ledger_id = 2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	charges	ref	PRIMARY,fk_charge_from_ledger,fk_charge_to_ledger	fk_charge_to_ledger	8	const	8	
1	PRIMARY	<derived2>	ref	key0	key0	17	test.charges.from_ledger_id,test.charges.id	1	
2	LATERAL DERIVED	transaction_items	ref	fk_items_transaction,fk_items_charge	fk_items_charge	9	test.charges.id	2	
2	LATERAL DERIVED	transactions	eq_ref	PRIMARY,fk_transactions_ledger	PRIMARY	8	test.transaction_items.transaction_id	1	Using where
EXPLAIN FORMAT=JSON SELECT
charges.id,
charges.from_ledger_id,
charges.to_ledger_id,
from_agg_items.num_rows AS from_num_rows
FROM charges
INNER JOIN (
SELECT
transactions.ledger_id,
transaction_items.charge_id,
count(*) as num_rows
FROM transaction_items
INNER JOIN transactions ON transaction_items.transaction_id = transactions.id
GROUP BY transactions.ledger_id, transaction_items.charge_id
) AS from_agg_items
ON from_agg_items.charge_id = charges.id AND
from_agg_items.ledger_id = charges.from_ledger_id
WHERE charges.to_ledger_id = 2;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "charges",
          "access_type": "ref",
          "possible_keys": [
            "PRIMARY",
            "fk_charge_from_ledger",
            "fk_charge_to_ledger"
          ],
          "key": "fk_charge_to_ledger",
          "key_length": "8",
          "used_key_parts": ["to_ledger_id"],
          "ref": ["const"],
          "loops": 1,
          "rows": 8,
          "cost": "COST_REPLACED",
          "filtered": 100
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "17",
          "used_key_parts": ["ledger_id", "charge_id"],
          "ref": ["test.charges.from_ledger_id", "test.charges.id"],
          "loops": 8,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "materialized": {
            "lateral": 1,
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "nested_loop": [
                {
                  "table": {
                    "table_name": "transaction_items",
                    "access_type": "ref",
                    "possible_keys": ["fk_items_transaction", "fk_items_charge"],
                    "key": "fk_items_charge",
                    "key_length": "9",
                    "used_key_parts": ["charge_id"],
                    "ref": ["test.charges.id"],
                    "loops": 1,
                    "rows": 2,
                    "cost": "COST_REPLACED",
                    "filtered": 100
                  }
                },
                {
                  "table": {
                    "table_name": "transactions",
                    "access_type": "eq_ref",
                    "possible_keys": ["PRIMARY", "fk_transactions_ledger"],
                    "key": "PRIMARY",
                    "key_length": "8",
                    "used_key_parts": ["id"],
                    "ref": ["test.transaction_items.transaction_id"],
                    "loops": 2,
                    "rows": 1,
                    "cost": "COST_REPLACED",
                    "filtered": 100,
                    "attached_condition": "transactions.ledger_id = charges.from_ledger_id"
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}
set optimizer_switch='split_materialized=off';
SELECT
charges.id,
charges.from_ledger_id,
charges.to_ledger_id,
from_agg_items.num_rows AS from_num_rows
FROM charges
INNER JOIN (
SELECT
transactions.ledger_id,
transaction_items.charge_id,
count(*) as num_rows
FROM transaction_items
INNER JOIN transactions ON transaction_items.transaction_id = transactions.id
GROUP BY transactions.ledger_id, transaction_items.charge_id
) AS from_agg_items
ON from_agg_items.charge_id = charges.id AND
from_agg_items.ledger_id = charges.from_ledger_id
WHERE charges.to_ledger_id = 2;
id	from_ledger_id	to_ledger_id	from_num_rows
2	1	2	1
3	1	2	1
5	3	2	1
8	3	2	1
10	3	2	1
12	3	2	1
13	3	2	1
18	1	2	1
EXPLAIN SELECT
charges.id,
charges.from_ledger_id,
charges.to_ledger_id,
from_agg_items.num_rows AS from_num_rows
FROM charges
INNER JOIN (
SELECT
transactions.ledger_id,
transaction_items.charge_id,
count(*) as num_rows
FROM transaction_items
INNER JOIN transactions ON transaction_items.transaction_id = transactions.id
GROUP BY transactions.ledger_id, transaction_items.charge_id
) AS from_agg_items
ON from_agg_items.charge_id = charges.id AND
from_agg_items.ledger_id = charges.from_ledger_id
WHERE charges.to_ledger_id = 2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	charges	ref	PRIMARY,fk_charge_from_ledger,fk_charge_to_ledger	fk_charge_to_ledger	8	const	8	
1	PRIMARY	<derived2>	ref	key0	key0	17	test.charges.from_ledger_id,test.charges.id	4	
2	DERIVED	transaction_items	ALL	fk_items_transaction	NULL	NULL	NULL	40	Using temporary; Using filesort
2	DERIVED	transactions	eq_ref	PRIMARY	PRIMARY	8	test.transaction_items.transaction_id	1	
INSERT INTO charges (id, from_ledger_id, to_ledger_id, amount) VALUES
(101, 4, 2, 100), (102, 7, 2, 200);
set optimizer_switch='split_materialized=on';
SELECT
charges.id,
charges.from_ledger_id,
charges.to_ledger_id,
from_agg_items.num_rows AS from_num_rows
FROM charges
LEFT JOIN (
SELECT
transactions.ledger_id,
transaction_items.charge_id,
count(*) as num_rows
FROM transaction_items
INNER JOIN transactions ON transaction_items.transaction_id = transactions.id
GROUP BY transactions.ledger_id, transaction_items.charge_id
) AS from_agg_items
ON from_agg_items.charge_id = charges.id AND
from_agg_items.ledger_id = charges.from_ledger_id
WHERE charges.to_ledger_id = 2;
id	from_ledger_id	to_ledger_id	from_num_rows
2	1	2	1
3	1	2	1
5	3	2	1
8	3	2	1
10	3	2	1
12	3	2	1
13	3	2	1
18	1	2	1
101	4	2	NULL
102	7	2	NULL
EXPLAIN SELECT
charges.id,
charges.from_ledger_id,
charges.to_ledger_id,
from_agg_items.num_rows AS from_num_rows
FROM charges
LEFT JOIN (
SELECT
transactions.ledger_id,
transaction_items.charge_id,
count(*) as num_rows
FROM transaction_items
INNER JOIN transactions ON transaction_items.transaction_id = transactions.id
GROUP BY transactions.ledger_id, transaction_items.charge_id
) AS from_agg_items
ON from_agg_items.charge_id = charges.id AND
from_agg_items.ledger_id = charges.from_ledger_id
WHERE charges.to_ledger_id = 2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	charges	ref	fk_charge_to_ledger	fk_charge_to_ledger	8	const	10	
1	PRIMARY	<derived2>	ref	key0	key0	18	test.charges.from_ledger_id,test.charges.id	1	
2	LATERAL DERIVED	transaction_items	ref	fk_items_transaction,fk_items_charge	fk_items_charge	9	test.charges.id	2	
2	LATERAL DERIVED	transactions	eq_ref	PRIMARY,fk_transactions_ledger	PRIMARY	8	test.transaction_items.transaction_id	1	Using where
EXPLAIN FORMAT=JSON SELECT
charges.id,
charges.from_ledger_id,
charges.to_ledger_id,
from_agg_items.num_rows AS from_num_rows
FROM charges
LEFT JOIN (
SELECT
transactions.ledger_id,
transaction_items.charge_id,
count(*) as num_rows
FROM transaction_items
INNER JOIN transactions ON transaction_items.transaction_id = transactions.id
GROUP BY transactions.ledger_id, transaction_items.charge_id
) AS from_agg_items
ON from_agg_items.charge_id = charges.id AND
from_agg_items.ledger_id = charges.from_ledger_id
WHERE charges.to_ledger_id = 2;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "charges",
          "access_type": "ref",
          "possible_keys": ["fk_charge_to_ledger"],
          "key": "fk_charge_to_ledger",
          "key_length": "8",
          "used_key_parts": ["to_ledger_id"],
          "ref": ["const"],
          "loops": 1,
          "rows": 10,
          "cost": "COST_REPLACED",
          "filtered": 100
        }
      },
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ref",
          "possible_keys": ["key0"],
          "key": "key0",
          "key_length": "18",
          "used_key_parts": ["ledger_id", "charge_id"],
          "ref": ["test.charges.from_ledger_id", "test.charges.id"],
          "loops": 10,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 100,
          "materialized": {
            "lateral": 1,
            "query_block": {
              "select_id": 2,
              "cost": "COST_REPLACED",
              "nested_loop": [
                {
                  "table": {
                    "table_name": "transaction_items",
                    "access_type": "ref",
                    "possible_keys": ["fk_items_transaction", "fk_items_charge"],
                    "key": "fk_items_charge",
                    "key_length": "9",
                    "used_key_parts": ["charge_id"],
                    "ref": ["test.charges.id"],
                    "loops": 1,
                    "rows": 2,
                    "cost": "COST_REPLACED",
                    "filtered": 100
                  }
                },
                {
                  "table": {
                    "table_name": "transactions",
                    "access_type": "eq_ref",
                    "possible_keys": ["PRIMARY", "fk_transactions_ledger"],
                    "key": "PRIMARY",
                    "key_length": "8",
                    "used_key_parts": ["id"],
                    "ref": ["test.transaction_items.transaction_id"],
                    "loops": 2,
                    "rows": 1,
                    "cost": "COST_REPLACED",
                    "filtered": 100,
                    "attached_condition": "transactions.ledger_id = charges.from_ledger_id"
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}
set optimizer_switch='split_materialized=off';
SELECT
charges.id,
charges.from_ledger_id,
charges.to_ledger_id,
from_agg_items.num_rows AS from_num_rows
FROM charges
LEFT JOIN (
SELECT
transactions.ledger_id,
transaction_items.charge_id,
count(*) as num_rows
FROM transaction_items
INNER JOIN transactions ON transaction_items.transaction_id = transactions.id
GROUP BY transactions.ledger_id, transaction_items.charge_id
) AS from_agg_items
ON from_agg_items.charge_id = charges.id AND
from_agg_items.ledger_id = charges.from_ledger_id
WHERE charges.to_ledger_id = 2;
id	from_ledger_id	to_ledger_id	from_num_rows
2	1	2	1
3	1	2	1
5	3	2	1
8	3	2	1
10	3	2	1
12	3	2	1
13	3	2	1
18	1	2	1
101	4	2	NULL
102	7	2	NULL
EXPLAIN SELECT
charges.id,
charges.from_ledger_id,
charges.to_ledger_id,
from_agg_items.num_rows AS from_num_rows
FROM charges
LEFT JOIN (
SELECT
transactions.ledger_id,
transaction_items.charge_id,
count(*) as num_rows
FROM transaction_items
INNER JOIN transactions ON transaction_items.transaction_id = transactions.id
GROUP BY transactions.ledger_id, transaction_items.charge_id
) AS from_agg_items
ON from_agg_items.charge_id = charges.id AND
from_agg_items.ledger_id = charges.from_ledger_id
WHERE charges.to_ledger_id = 2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	charges	ref	fk_charge_to_ledger	fk_charge_to_ledger	8	const	10	
1	PRIMARY	<derived2>	ref	key0	key0	18	test.charges.from_ledger_id,test.charges.id	4	
2	DERIVED	transaction_items	ALL	fk_items_transaction	NULL	NULL	NULL	40	Using temporary; Using filesort
2	DERIVED	transactions	eq_ref	PRIMARY	PRIMARY	8	test.transaction_items.transaction_id	1	
set optimizer_switch='split_materialized=default';
DROP TABLE transaction_items;
DROP TABLE transactions;
DROP TABLE charges;
DROP TABLE ledgers;
#
# MDEV-30081: Splitting from a constant mergeable derived table
#             used in inner part of an outer join.
#
CREATE TABLE t1 ( id int PRIMARY KEY ) ENGINE=MyISAM;
INSERT INTO t1 VALUES (3),(4),(7);
CREATE TABLE t2 (
id int,  id1 int, wid int, PRIMARY KEY (id), KEY (id1), KEY (wid)
) ENGINE=MyISAM;
INSERT INTO t2 VALUES (4,4,6),(7,7,7);
CREATE TABLE t3 (
wid int, wtid int, otid int, oid int,
PRIMARY KEY (wid), KEY (wtid), KEY (otid), KEY (oid)
) ENGINE=MyISAM;
INSERT INTO t3 VALUES (6,30,6,6),(7,17,7,7);
CREATE TABLE t4 ( id int, a int, PRIMARY KEY (id), KEY (a) )  ENGINE=MyISAM;
INSERT INTO t4 VALUES (1,17),(2,15),(3,49),(4,3),(5,45),(6,38),(7,17);
CREATE TABLE t5 (
id int, id1 int, PRIMARY KEY (id), KEY id1 (id1)
) ENGINE=MyISAM ;
INSERT INTO t5 VALUES (1,17),(2,15),(3,49),(4,3),(5,45),(6,38),(7,17);
ANALYZE TABLE t1,t2,t3,t4,t5;
Table	Op	Msg_type	Msg_text
test.t1	analyze	status	Engine-independent statistics collected
test.t1	analyze	status	OK
test.t2	analyze	status	Engine-independent statistics collected
test.t2	analyze	status	OK
test.t3	analyze	status	Engine-independent statistics collected
test.t3	analyze	status	OK
test.t4	analyze	status	Engine-independent statistics collected
test.t4	analyze	status	OK
test.t5	analyze	status	Engine-independent statistics collected
test.t5	analyze	status	OK
CREATE VIEW v1 AS (SELECT id1 FROM t5 GROUP BY id1);
SELECT t3.*, t1.id AS t1_id, t2.id AS t2_id, dt.*, v1.*
FROM
t1, t2, t3
LEFT JOIN
(SELECT t4.* FROM t4 WHERE t4.a=3) dt
ON t3.oid = dt.id AND t3.otid = 14
LEFT JOIN v1
ON (v1.id1 = dt.a)
WHERE t3.oid = t1.id AND t3.oid = t2.id AND t3.wid = 7;
wid	wtid	otid	oid	t1_id	t2_id	id	a	id1
7	17	7	7	7	7	NULL	NULL	NULL
EXPLAIN SELECT t3.*, t1.id AS t1_id, t2.id AS t2_id, dt.*, v1.*
FROM
t1, t2, t3
LEFT JOIN
(SELECT t4.* FROM t4 WHERE t4.a=3) dt
ON t3.oid = dt.id AND t3.otid = 14
LEFT JOIN v1
ON (v1.id1 = dt.a)
WHERE t3.oid = t1.id AND t3.oid = t2.id AND t3.wid = 7;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t3	const	PRIMARY,oid	PRIMARY	4	const	1	
1	PRIMARY	t1	const	PRIMARY	PRIMARY	4	const	1	Using index
1	PRIMARY	t2	const	PRIMARY	PRIMARY	4	const	1	Using index
1	PRIMARY	t4	const	PRIMARY,a	NULL	NULL	NULL	0	Impossible ON condition
1	PRIMARY	<derived3>	ref	key0	key0	5	const	0	Using where
3	LATERAL DERIVED	t5	ref	id1	id1	5	const	0	Using index
DROP VIEW v1;
DROP TABLE t1,t2,t3,t4,t5;
# End of 10.3 tests
#
# MDEV-18679: materialized view with SELECT S containing materialized
#             derived when impossible WHERE has been detected for S
#
create table t1 (pk int, f varchar(1));
insert into t1 values
(3,'y'), (1,'x'), (7,'z');
create view v1 as
select t1.f
from t1, (select distinct * from t1) t
where t.f = t1.f and 1 = 0
group by t1.f;
select * from v1;
f
explain select * from v1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	<derived2>	system	NULL	NULL	NULL	NULL	0	Const row not found
2	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
3	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	3	Using temporary
drop view v1;
drop table t1;
#
# MDEV-31102: execution of PS for query where pushdown of condition
#             into view defined as union is applied
#
create table t1 (
n int,
lv varchar(31) charset latin1,
mv varchar(31) charset utf8mb3
) engine=myisam;
insert into t1 values (1,'aa','xxx'), ('2','bb','yyy'), (3,'cc','zzz');
create view v1 as
select case when n=1 then lv when n=2 then mv else NULL end as r from t1
union
select 'a';
select * from v1 where r < 'x';
r
aa
a
explain extended select * from v1 where r < 'x';
id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where
3	UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL	NULL	
Warnings:
Note	1003	/* select#1 */ select `v1`.`r` AS `r` from `test`.`v1` where `v1`.`r` < 'x'
explain format=json select * from v1 where r < 'x';
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": 0.010134054,
    "nested_loop": [
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 3,
          "cost": 0.010134054,
          "filtered": 100,
          "attached_condition": "v1.r < 'x'",
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<union2,3>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": 0.010504815,
                      "nested_loop": [
                        {
                          "table": {
                            "table_name": "t1",
                            "access_type": "ALL",
                            "loops": 1,
                            "rows": 3,
                            "cost": 0.010504815,
                            "filtered": 100,
                            "attached_condition": "case when t1.n = 1 then convert(t1.lv using utf8mb3) when t1.n = 2 then t1.mv else NULL end < 'x'"
                          }
                        }
                      ]
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "UNION",
                      "table": {
                        "message": "No tables used"
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
prepare stmt from "select * from v1 where r < 'x'";
execute stmt;
r
aa
a
execute stmt;
r
aa
a
deallocate prepare stmt;
drop view v1;
drop table t1;
#
# MDEV-31240: condition pushed into splittable derived has reference to
#             outer column and does not refer to any column of embedding
#             select
#
create table t1 (a int);
insert into t1 select seq from seq_1_to_1000;
create table t2 (a int, b int, key (a));
insert into t2 select mod(seq,100), rand(13) * mod(seq,500) from seq_1_to_1000;
create table t3 (a int);
insert into t3 values (3), (1);
analyze table t1, t2, t3 persistent for all;
Table	Op	Msg_type	Msg_text
test.t1	analyze	status	Engine-independent statistics collected
test.t1	analyze	status	OK
test.t2	analyze	status	Engine-independent statistics collected
test.t2	analyze	status	Table is already up to date
test.t3	analyze	status	Engine-independent statistics collected
test.t3	analyze	status	OK
explain select
a,
( select concat(t3.a,'=',dt.s)
from
(select a, sum(b) as s from t2 group by a) as dt,
t3
where dt.a=t1.a and t3.a < 3
) as SUBQ
from t1 limit 5;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	1000	
2	DEPENDENT SUBQUERY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where
2	DEPENDENT SUBQUERY	<derived3>	ref	key0	key0	5	test.t1.a	1	
3	LATERAL DERIVED	t2	ref	a	a	5	test.t1.a	10	
select
a,
( select concat(t3.a,'=',dt.s)
from
(select a, sum(b) as s from t2 group by a) as dt,
t3
where dt.a=t1.a and t3.a < 3
) as SUBQ
from t1 limit 5;
a	SUBQ
1	1=804
2	1=1056
3	1=846
4	1=947
5	1=973
truncate table t2;
insert into t2 select mod(seq,10), rand(15) * mod(seq,500) from seq_1_to_1000;
analyze table t2 persistent for all;
Table	Op	Msg_type	Msg_text
test.t2	analyze	status	Engine-independent statistics collected
test.t2	analyze	status	Table is already up to date
explain select
a,
( select concat(t3.a,'=',dt.s)
from
(select a, sum(b) as s from t2 group by a) as dt,
t3
where dt.a=t1.a and t3.a < 3
) as SUBQ
from t1 limit 5;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	1000	
2	DEPENDENT SUBQUERY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where
2	DEPENDENT SUBQUERY	<derived3>	ref	key0	key0	5	test.t1.a	1	
3	DERIVED	t2	ALL	a	NULL	NULL	NULL	1000	Using temporary; Using filesort
select
a,
( select concat(t3.a,'=',dt.s)
from
(select a, sum(b) as s from t2 group by a) as dt,
t3
where dt.a=t1.a and t3.a < 3
) as SUBQ
from t1 limit 5;
a	SUBQ
1	1=11858
2	1=11380
3	1=11588
4	1=11373
5	1=11612
drop table t1,t2,t3;
#
# MDEV-32064: usage of splittable derived table in query
#             with IN subquery in WHERE
#
CREATE TABLE t1 (
id int unsigned NOT NULL,
valint1 int  unsigned,
valdouble double,
valdate datetime,
PRIMARY KEY (id),
KEY (valint1),
KEY (valint1,valdate)
) ENGINE=MyISAM;
INSERT INTO t1 VALUES
(1,3289763,1,'2021-02-09 18:31:35'),(2,3289750,1,'2021-02-09 18:31:35'),
(3,3289780,1173,'2021-02-09 18:31:35'),(4,3289762,2,'2021-02-09 18:31:36'),
(5,3289774,2334,'2021-02-09 18:31:36'),(6,3289739,1934,'2021-02-09 18:31:36'),
(7,3289761,1,'2021-02-09 18:31:37'),(8,3289763,1,'2021-02-10 11:05:19'),
(9,3289750,1,'2021-02-10 11:05:19'),(10,3289780,0,'2021-02-10 11:05:35'),
(11,3289762,2,'2021-02-10 11:05:47'),(12,3289774,429,'2021-02-10 11:05:47'),
(13,3289739,1958,'2021-02-10 11:06:00'),(14,3289761,1,'2021-02-10 11:06:08'),
(15,3289957,0,'2021-02-10 13:04:44'),(16,3289988,1993,'2021-02-10 13:04:45'),
(17,3289951,1896,'2021-02-10 13:04:59'),(18,3289957,1994,'2021-02-10 13:07:40'),
(19,3289988,5,'2021-02-10 13:07:40'),(20,3289951,1897,'2021-02-10 13:07:40'),
(21,3289594,0,'2021-02-11 14:19:38'),(22,3289642,0,'2021-02-11 14:19:38'),
(23,3289626,2150,'2021-02-11 14:19:38'),(24,3289562,0,'2021-02-11 14:19:39'),
(25,3289593,1046,'2021-02-11 14:19:39'),(26,3289496,1,'2021-02-11 14:19:45'),
(27,3289475,1074,'2021-02-11 14:19:50'),(28,3289658,1155,'2021-02-11 14:19:56'),
(29,3289595,0,'2021-02-11 14:20:01'),(30,3290334,903,'2021-02-11 16:22:44'),
(31,3290284,479,'2021-02-11 16:23:00'),(32,3290327,236,'2021-02-11 16:23:00'),
(33,3290854,0,'2021-02-15 17:29:59'),(34,3290824,0,'2021-02-15 17:30:13'),
(35,3290875,0,'2021-02-15 17:30:14'),(36,3290897,2,'2021-02-15 17:30:19'),
(37,3290800,0,'2021-02-15 17:30:24'),(38,3290822,0,'2021-02-15 17:30:25'),
(39,3290901,2667,'2021-02-15 17:30:30'),(40,3290835,0,'2021-02-15 17:30:36'),
(41,3290875,0,'2021-02-15 17:35:33'),(42,3290824,1330,'2021-02-15 17:35:39'),
(43,3290854,769,'2021-02-15 17:35:44'),(44,3290897,2,'2021-02-15 17:35:50'),
(45,3290822,748,'2021-02-15 17:35:50'),(46,3290800,1007,'2021-02-15 17:35:56'),
(47,3290901,7018,'2021-02-15 17:35:56'),(48,3290835,779,'2021-02-15 17:36:17'),
(49,3290824,1329,'2021-02-15 17:40:30'),(50,3290875,764,'2021-02-15 17:40:31'),
(51,3290854,763,'2021-02-15 17:40:36'),(52,3290897,2347,'2021-02-15 17:40:47'),
(53,3290822,1,'2021-02-15 17:41:01'),(54,3290800,1018,'2021-02-15 17:41:07'),
(55,3290901,3936,'2021-02-15 17:41:08'),(56,3290835,784,'2021-02-15 17:41:24'),
(57,3290824,1313,'2021-02-15 17:44:47'),(58,3290875,758,'2021-02-15 17:44:48'),
(59,3290854,767,'2021-02-15 17:44:48'),(60,3290897,2438,'2021-02-15 17:44:48'),
(61,3290822,738,'2021-02-15 17:44:49'),(62,3290800,1003,'2021-02-15 17:44:54'),
(63,3290901,4686,'2021-02-15 17:44:55'),(64,3290835,778,'2021-02-15 17:45:13'),
(65,3290824,1303,'2021-02-15 17:51:16'),(66,3290875,753,'2021-02-15 17:51:16'),
(67,3290854,766,'2021-02-15 17:51:22'),(68,3290897,1,'2021-02-15 17:51:22'),
(69,3290822,743,'2021-02-15 17:51:28'),(70,3290901,5718,'2021-02-15 17:51:33'),
(71,3290800,1018,'2021-02-15 17:51:34'),(72,3290835,785,'2021-02-15 17:51:48'),
(73,3290824,1310,'2021-02-15 18:21:30'),(74,3290875,754,'2021-02-15 18:21:30'),
(75,3290854,782,'2021-02-15 18:21:36'),(76,3290897,2,'2021-02-15 18:21:36'),
(77,3290822,745,'2021-02-15 18:21:53'),(78,3290800,1011,'2021-02-15 18:21:54'),
(79,3290901,8998,'2021-02-15 18:21:54'),(80,3290835,0,'2021-02-15 18:22:00'),
(81,3290936,0,'2021-02-15 18:25:28'),(82,3290895,0,'2021-02-15 18:25:28'),
(83,3290832,0,'2021-02-15 18:25:28'),(84,3290878,796,'2021-02-15 18:25:52'),
(85,3290900,730,'2021-02-15 18:25:52'),(86,3290856,0,'2021-02-15 18:26:11'),
(87,3290904,816,'2021-02-15 18:26:17'),(88,3290882,0,'2021-02-15 18:26:25'),
(89,3290883,1031,'2021-02-15 18:27:16'),(90,3290918,1749,'2021-02-15 18:27:17'),
(91,3290831,0,'2021-02-15 18:59:11'),(92,3290884,477,'2021-02-15 18:59:12'),
(93,3290899,483,'2021-02-15 18:59:12'),(94,3290848,486,'2021-02-15 18:59:35'),
(95,3290880,487,'2021-02-15 18:59:35'),(96,3290798,0,'2021-02-15 18:59:52'),
(97,3290777,983,'2021-02-15 19:00:10'),(98,3290811,488,'2021-02-15 19:00:10'),
(99,3290917,1283,'2021-02-15 19:00:36'),(100,3290858,482,'2021-02-15 19:00:42');
CREATE TABLE t2 (a int) ENGINE=MYISAM;
INSERT INTO t2 VALUES
(3289475),(3289496),(3289562),(3289593),(3289594),(3289595),(3289626),
(3289642),(3289658),(3289739),(3289750),(3289761),(3289762),(3289763),
(3289774),(3289780),(3289951),(3289957),(3289988),(3290034),(1231562);
ANALYZE TABLE t1,t2;
Table	Op	Msg_type	Msg_text
test.t1	analyze	status	Engine-independent statistics collected
test.t1	analyze	status	Table is already up to date
test.t2	analyze	status	Engine-independent statistics collected
test.t2	analyze	status	OK
EXPLAIN SELECT t1.valdouble, t1.valint1
FROM t1,
(SELECT max(t.valdate) AS maxdate, t.valint1 FROM t1 t GROUP BY t.valint1)
AS dt
WHERE t1.valint1 = dt.valint1 AND
t1.valdate = dt.maxdate AND
t1.valint1 IN (SELECT * FROM t2);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	21	Using where; Start temporary
1	PRIMARY	t1	ref	valint1,valint1_2	valint1	5	test.t2.a	2	Using index condition; Using where; End temporary
1	PRIMARY	<derived2>	ref	key0	key0	11	test.t1.valdate,test.t1.valint1	1	
2	LATERAL DERIVED	t	ref	valint1,valint1_2	valint1	5	test.t2.a	2	Using index condition
SELECT t1.valdouble, t1.valint1
FROM t1,
(SELECT max(t.valdate) AS maxdate, t.valint1 FROM t1 t GROUP BY t.valint1)
AS dt
WHERE t1.valint1 = dt.valint1 AND
t1.valdate = dt.maxdate AND
t1.valint1 IN (SELECT * FROM t2);
valdouble	valint1
1074	3289475
1	3289496
0	3289562
1046	3289593
0	3289594
0	3289595
2150	3289626
0	3289642
1155	3289658
1958	3289739
1	3289750
1	3289761
2	3289762
1	3289763
429	3289774
0	3289780
1897	3289951
1994	3289957
5	3289988
DROP TABLE t1,t2;
# End of 10.4 tests
# MDEV-34506 2nd execution name resolution problem with pushdown into
#   unions
#
#  Statements affected by this bug need all the following to be true
#  1) a derived table table or view whose specification contains a set
#       operation at the top level.
#  2) a grouping operator (group by/having) operating on a column alias
#       other than in the first select of the union/intersect
#  3) an outer condition that will be pushed into all selects in this
#      union/intersect, either into the where or having clause
#
#  When pushing a condition into all selects of a unit with more than one
#  select, pushdown_cond_for_derived() renames items so we can re-use the
#  condition being pushed.
#  These names need to be saved and reset for correct name resolution on
#  second execution of prepared statements.
create table t1 (c1 int, c2 int, c3 int);
insert into t1 values (1,2,3),(1,2,2),(4,5,6);
insert into t1 values (17,8,9),(11,11,12);
create table t2 (c4 int, c5 int, c6 int);
insert into t2 values (7,8,9),(10,11,12);
prepare stmt from 'select * from
(
select c1,  sum(c3) as s from t1 group by c1
union
select c4 as c, sum(c6) as u from t2 group by c
) dt
where c1 > 6';
execute stmt;
c1	s
11	12
17	9
7	9
10	12
execute stmt;
c1	s
11	12
17	9
7	9
10	12
prepare stmt from 'explain format=json select * from
(
select c1,  sum(c3) as s from t1 group by c1
union
select c4 as c, sum(c6) as u from t2 group by c
) dt
where c1 > 6';
execute stmt;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 7,
          "cost": "REPLACED",
          "filtered": 100,
          "attached_condition": "dt.c1 > 6",
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<union2,3>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "REPLACED",
                      "filesort": {
                        "sort_key": "t1.c1",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 5,
                                "cost": "REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.c1 > 6"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "UNION",
                      "cost": "REPLACED",
                      "filesort": {
                        "sort_key": "t2.c4",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t2",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 2,
                                "cost": "REPLACED",
                                "filtered": 100,
                                "attached_condition": "t2.c4 > 6"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
execute stmt;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 7,
          "cost": "REPLACED",
          "filtered": 100,
          "attached_condition": "dt.c1 > 6",
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<union2,3>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "REPLACED",
                      "filesort": {
                        "sort_key": "t1.c1",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 5,
                                "cost": "REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.c1 > 6"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "UNION",
                      "cost": "REPLACED",
                      "filesort": {
                        "sort_key": "t2.c4",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t2",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 2,
                                "cost": "REPLACED",
                                "filtered": 100,
                                "attached_condition": "t2.c4 > 6"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
prepare stmt from 'select * from
(
select c1, c2, sum(c3) as s from t1 group by c1, c2 having s > 2
union
select c4, c5, sum(c6) as u from t2 group by c4, c5 having u > 3
) dt
where c2 > 5';
execute stmt;
c1	c2	s
11	11	12
17	8	9
7	8	9
10	11	12
execute stmt;
c1	c2	s
11	11	12
17	8	9
7	8	9
10	11	12
prepare stmt from 'explain format=json select * from
(
select c1, c2, sum(c3) as s from t1 group by c1, c2 having s > 2
union
select c4, c5, sum(c6) as u from t2 group by c4, c5 having u > 3
) dt
where c2 > 5';
execute stmt;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 7,
          "cost": "REPLACED",
          "filtered": 100,
          "attached_condition": "dt.c2 > 5",
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<union2,3>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "REPLACED",
                      "having_condition": "s > 2",
                      "filesort": {
                        "sort_key": "t1.c1, t1.c2",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 5,
                                "cost": "REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.c2 > 5"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "UNION",
                      "cost": "REPLACED",
                      "having_condition": "s > 3",
                      "filesort": {
                        "sort_key": "t2.c4, t2.c5",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t2",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 2,
                                "cost": "REPLACED",
                                "filtered": 100,
                                "attached_condition": "t2.c5 > 5"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
execute stmt;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 7,
          "cost": "REPLACED",
          "filtered": 100,
          "attached_condition": "dt.c2 > 5",
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<union2,3>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "REPLACED",
                      "having_condition": "s > 2",
                      "filesort": {
                        "sort_key": "t1.c1, t1.c2",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 5,
                                "cost": "REPLACED",
                                "filtered": 100,
                                "attached_condition": "t1.c2 > 5"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "UNION",
                      "cost": "REPLACED",
                      "having_condition": "s > 3",
                      "filesort": {
                        "sort_key": "t2.c4, t2.c5",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t2",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 2,
                                "cost": "REPLACED",
                                "filtered": 100,
                                "attached_condition": "t2.c5 > 5"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
prepare stmt from 'select *
from
(
select c1, c2, max(c3) as max_c, avg(c3) as avg_c
from t1
group by c1,c2
having max_c < 7
union
select c4, c5, max(c6) as u, avg(c6) as w
from t2
group by c4, c5
having u < 10
) dt,
t2
where dt.max_c > 6 and t2.c6 > dt.c1';
execute stmt;
c1	c2	max_c	avg_c	c4	c5	c6
7	8	9	9.0000	7	8	9
7	8	9	9.0000	10	11	12
execute stmt;
c1	c2	max_c	avg_c	c4	c5	c6
7	8	9	9.0000	7	8	9
7	8	9	9.0000	10	11	12
prepare stmt from 'explain format=json select *
from
(
select c1, c2, max(c3) as max_c, avg(c3) as avg_c
from t1
group by c1,c2
having max_c < 7
union
select c4, c5, max(c6) as u, avg(c6) as w
from t2
group by c4, c5
having u < 10
) dt,
t2
where dt.max_c > 6 and t2.c6 > dt.c1';
execute stmt;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 2,
          "cost": "REPLACED",
          "filtered": 100
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived2>",
            "access_type": "ALL",
            "loops": 2,
            "rows": 7,
            "cost": "REPLACED",
            "filtered": 100,
            "attached_condition": "dt.max_c > 6"
          },
          "buffer_type": "flat",
          "buffer_size": "173",
          "join_type": "BNL",
          "attached_condition": "t2.c6 > dt.c1",
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<union2,3>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "REPLACED",
                      "having_condition": "max_c < 7 and max_c > 6",
                      "filesort": {
                        "sort_key": "t1.c1, t1.c2",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 5,
                                "cost": "REPLACED",
                                "filtered": 100
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "UNION",
                      "cost": "REPLACED",
                      "having_condition": "max_c < 10 and max_c > 6",
                      "filesort": {
                        "sort_key": "t2.c4, t2.c5",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t2",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 2,
                                "cost": "REPLACED",
                                "filtered": 100
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
execute stmt;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "loops": 1,
          "rows": 2,
          "cost": "REPLACED",
          "filtered": 100
        }
      },
      {
        "block-nl-join": {
          "table": {
            "table_name": "<derived2>",
            "access_type": "ALL",
            "loops": 2,
            "rows": 7,
            "cost": "REPLACED",
            "filtered": 100,
            "attached_condition": "dt.max_c > 6"
          },
          "buffer_type": "flat",
          "buffer_size": "173",
          "join_type": "BNL",
          "attached_condition": "t2.c6 > dt.c1",
          "materialized": {
            "query_block": {
              "union_result": {
                "table_name": "<union2,3>",
                "access_type": "ALL",
                "query_specifications": [
                  {
                    "query_block": {
                      "select_id": 2,
                      "cost": "REPLACED",
                      "having_condition": "max_c < 7 and max_c > 6",
                      "filesort": {
                        "sort_key": "t1.c1, t1.c2",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t1",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 5,
                                "cost": "REPLACED",
                                "filtered": 100
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "query_block": {
                      "select_id": 3,
                      "operation": "UNION",
                      "cost": "REPLACED",
                      "having_condition": "max_c < 10 and max_c > 6",
                      "filesort": {
                        "sort_key": "t2.c4, t2.c5",
                        "temporary_table": {
                          "nested_loop": [
                            {
                              "table": {
                                "table_name": "t2",
                                "access_type": "ALL",
                                "loops": 1,
                                "rows": 2,
                                "cost": "REPLACED",
                                "filtered": 100
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}
drop table t1, t2;
#
# MDEV-35276: Assertion failure in find_producing_item upon a query
# from a view
#
# Note: for all followng queries with DEFAULT(f) hishdown is
# prohibited
CREATE VIEW v AS select 0 AS f;
SELECT * FROM v WHERE f = DEFAULT(f);
f
0
CREATE TABLE t1 (c int DEFAULT 0);
insert into t1 values (0), (1);
SELECT * FROM v,t1 WHERE f = DEFAULT(c);
f	c
0	0
0	1
ALTER TABLE t1 ALTER c SET DEFAULT 1;
SELECT * FROM v,t1 WHERE f = DEFAULT(c);
f	c
drop table t1;
create table t1 (a int, c int DEFAULT a);
insert into t1 values (0, -1), (1, -2);
SELECT * FROM v,t1 WHERE f = DEFAULT(c);
f	a	c
0	0	-1
DROP VIEW v;
DROP TABLE t1;
#
# MDEV-32395: update_depend_map_for_order: SEGV at
#             /mariadb-11.3.0/sql/sql_select.cc:16583
#
create table t1 (c1 int);
insert into t1 values (1), (2);
create table t2 (c2 int);
insert into t2 values (1), (2);
create table t3 (c3 int);
insert into t3 values (1), (2);
set statement optimizer_switch='condition_pushdown_for_derived=off,condition_pushdown_for_subquery=off,condition_pushdown_from_having=off' for
select t1.c1 as a from t2, t1 where t1.c1=t2.c2
order by (select c3 from t3 group by a having a=2);
a
1
2
drop table t1, t2, t3;
# Test case 2
CREATE TABLE t1 ( x BOOLEAN NOT NULL );
INSERT INTO t1 ( x ) VALUES ( 1 ) ;
UPDATE t1 SET x = 1 WHERE x = 1 ;
INSERT INTO t1 ( x ) VALUES ( 1 ) , ( x IN ( SELECT x FROM ( SELECT ( SELECT EXISTS ( SELECT * FROM ( SELECT DISTINCT ( - CASE WHEN x = 1 THEN 1 ELSE x + 1 END >= x IS NOT NULL = 1 AND x = 1 ) OR x = x OR x = 'x' FROM t1 AS x GROUP BY x ) AS x WHERE 1 / x GROUP BY x HAVING ( 1 = 1 AND x = 1 ) ) FROM t1 GROUP BY EXISTS ( SELECT 1 ) ) FROM t1 UNION SELECT x FROM t1 ) AS x ) ) ;
DROP TABLE t1;
# Test case 3
CREATE TABLE t0 ( c6 INT , c21 INT ) ;
INSERT INTO t0 VALUES ( 55 , -95 ) , ( 9 , 90 ) ;
ALTER TABLE t0 ADD COLUMN c37 INT AFTER c6 ;
INSERT INTO t0 VALUES ( ) , ( ) ;
SELECT t0 . c6 AS c42 FROM ( SELECT t0 . c6 = TRIM( TRAILING FROM 96 ) SOUNDS LIKE CONVERT ( t0 . c6 , UNSIGNED ) >> PI ( ) AS c49 FROM t0 ) AS t1 JOIN t0 ON RTRIM ( - RAND ( -66 ) BETWEEN FIND_IN_SET ( 20 , UNHEX ( -80 ) IS NULL OR IF ( 85 , -83 , -113 ) ) AND -125 ) / EXP ( c21 ) = t1 . c49 ORDER BY c42 , ( c42 + ( SELECT c21 AS c61 FROM t0 WHERE t0 . c37 >= -19.601384 = RAND ( ) / TRIM( t0 . c21 FROM 'C@rG3D(#9*17(a.,rV' ) = -106 GROUP BY c21 , c42 HAVING c42 = -73 LIMIT 1 ) ) ;
c42
9
9
55
55
drop table t0;
#
# MDEV-32329 pushdown from having into where: Server crashes at sub_select
#
WITH RECURSIVE cte AS ( SELECT 1 as x UNION SELECT x FROM cte)
SELECT ( SELECT 1 FROM ( SELECT 1 FROM cte) dt GROUP BY x HAVING x= 1 )
FROM cte
GROUP BY 1 ;
( SELECT 1 FROM ( SELECT 1 FROM cte) dt GROUP BY x HAVING x= 1 )
1
# Test case 2
WITH
cte1 AS ( SELECT 1 as x UNION SELECT 1),
cte2 AS ( SELECT 1 as x UNION SELECT 1)
SELECT
( SELECT 1 FROM ( SELECT 1 FROM cte1) dt GROUP BY x HAVING x= 1 )
FROM cte2
GROUP BY 1 ;
( SELECT 1 FROM ( SELECT 1 FROM cte1) dt GROUP BY x HAVING x= 1 )
1
create table t1 (f int);
create view v1 as select f, count(*) c from t1 group by f;
#
# MDEV-25012 Server crash in find_field_in_tables, Assertion `name' failed in find_field_in_table_ref
#
select * from v1 where export_set(1, default(f), 'x', aes_decrypt('secret', f));
f	c
show warnings;
Level	Code	Message
drop view v1;
drop table t1;
create table t(c3 longtext) ;
with cte1 as
(
select default(c3) as a
from t group by 1
)
select * from cte1
where cte1.a >= 1;
a
drop table t;
# End of 10.5 tests
#
# MDEV-38487: Prevent aggregate functions cloning when pushing HAVING into WHERE
#
CREATE TABLE t (a INT, b INT);
INSERT INTO t VALUES (1, 10), (1, 20), (2, 50), (3, 100);
explain format=JSON SELECT * FROM (SELECT a, MAX(b) as max_b FROM t GROUP BY a) as dt WHERE dt.max_b > 25;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": 0.010178738,
    "nested_loop": [
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 4,
          "cost": 0.010178738,
          "filtered": 100,
          "attached_condition": "dt.max_b > 25",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": 0.013196268,
              "having_condition": "max_b > 25",
              "filesort": {
                "sort_key": "t.a",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 4,
                        "cost": 0.01066122,
                        "filtered": 100
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
SELECT * FROM (SELECT a, MAX(b) as max_b FROM t GROUP BY a) as dt WHERE dt.max_b > 25;
a	max_b
2	50
3	100
CREATE VIEW v AS SELECT a, MAX(b) as max_b FROM t GROUP BY a;
explain format=JSON SELECT * FROM v where max_b > 25;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": 0.010178738,
    "nested_loop": [
      {
        "table": {
          "table_name": "<derived2>",
          "access_type": "ALL",
          "loops": 1,
          "rows": 4,
          "cost": 0.010178738,
          "filtered": 100,
          "attached_condition": "v.max_b > 25",
          "materialized": {
            "query_block": {
              "select_id": 2,
              "cost": 0.013196268,
              "having_condition": "max_b > 25",
              "filesort": {
                "sort_key": "t.a",
                "temporary_table": {
                  "nested_loop": [
                    {
                      "table": {
                        "table_name": "t",
                        "access_type": "ALL",
                        "loops": 1,
                        "rows": 4,
                        "cost": 0.01066122,
                        "filtered": 100
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
SELECT * FROM v where max_b > 25;
a	max_b
2	50
3	100
DROP VIEW v;
DROP TABLE t;
# End of 10.6 tests
#
# MDEV-28958: condition pushable into view after simplification
#             contains constant TRUE/FALSE as subformula
#
create table t1 (c1 int);
insert into t1 values (3), (7), (1), (3), (1), (3);
create table t2 (c2 int);
insert into t2 values (3), (5), (7), (3);
create view v1 as select * from t1 group by c1;
create view v2 as select c1 as a, c2 as b from v1,t2 where c1=c2;
select * from v2 group by a,b having a=b or b > a+10;
a	b
3	3
7	7
drop view v1,v2;
drop table t1,t2;
# End of 10.7 tests
#
# MDEV-36686: Crash with DEFAULT() in WHERE clause with nested views
#
create table t1 (a varchar(128));
create view v1 as select a, count(*) from t1 group by a;
create view v2 as select * from v1;
select * from v2 where greatest(a, default(a));
a	count(*)
drop view v2, v1;
drop table t1;
#
# MDEV-29360 Crash when pushing condition with always false IS NULL
#   into derived contains constant TRUE/FALSE as subformula
#
create table t1 (id int not null);
insert into t1 values (0),(1),(3);
create table t2 select * from t1;
create table t3 select * from t1;
select * from (select id  from t1 group by id) dt1, (select id from t2) dt2, t3
where dt2.id = t3.id and dt1.id between 0 and (dt2.id is null);
id	id	id
0	0	0
0	1	1
0	3	3
select * from (select id  from t1 union select id from t2) dt1,
(select id from t2) dt2,
t3
where dt2.id = t3.id and dt1.id between 0 and (dt2.id is null);
id	id	id
0	0	0
0	1	1
0	3	3
drop table t1,t2,t3;
# End of 10.11 tests
