1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
|
# name: test/optimizer/join_elimination.test
# description: test join elimination
# group: [optimizer]
statement ok
PRAGMA enable_verification;
statement ok
create table a(ida int, valuea int);
statement ok
create table b(idb int, valueb int);
statement ok
create table c(idc int, valuec int);
statement ok
insert into a values (1,1), (2,2), (1,1),(NUll, NULL),(3, NULL),(NULL,4);
statement ok
insert into b values (1,1), (2,2), (3,3),(4,4),(3,3),(NUll, NULL),(3, NULL),(NULL,4);
statement ok
insert into c values (1,1), (2,2), (3,3),(4,4),(3,3),(NUll, NULL),(3, NULL),(NULL,4);
query II
explain select distinct b.* from b left join a on a.ida=b.idb;
----
physical_plan <!REGEX>:.*JOIN.*
query II
explain select distinct b.* from b left join a on a.ida=b.idb where b.valueb=1;
----
physical_plan <!REGEX>:.*JOIN.*
query II
explain select distinct b.* from b left join a on a.ida=b.idb where a.valuea=1;
----
physical_plan <REGEX>:.*JOIN.*
query II
explain select b.* from b left join (select distinct ida as ida from a) as a1 on a1.ida=b.idb;
----
physical_plan <!REGEX>:.*JOIN.*
query II
explain select b.* from b left join (select * from a group by all) as a1 on b.idb=a1.ida;
----
physical_plan <REGEX>:.*JOIN.*
query II
explain select b.* from b left join (select a.ida from a group by ida) as a1 on b.idb=a1.ida;
----
physical_plan <!REGEX>:.*JOIN.*
query II
explain select b.idb,sum(b.valueb) from b left join a on b.idb=a.ida group by b.idb;
----
physical_plan <REGEX>:.*JOIN.*
query II
explain select b.idb,sum(a.valuea) from b left join a on b.idb=a.ida group by b.idb;
----
physical_plan <REGEX>:.*JOIN.*
query II
explain select b.* from b left join (select distinct ida as ida from a) as a1 on a1.ida=b.idb and a1.ida = 1;
----
physical_plan <REGEX>:.*JOIN.*
query II
explain select b.* from b left join (select distinct ida as ida from a) as a1 on a1.ida=b.idb and a1.ida = b.valueb ;
----
physical_plan <!REGEX>:.*JOIN.*
query II
explain select b.* from b left join (select distinct ida as ida, valuea as valuea from a) as a1 on a1.ida=b.idb and a1.ida = b.valueb and a1.valuea = b.idb;
----
physical_plan <!REGEX>:.*JOIN.*
query II
explain select b.* from b left join (select distinct ida as ida, valuea as valuea from a) as a1 on a1.ida=b.idb;
----
physical_plan <REGEX>:.*JOIN.*
query II
explain select b.* from b left join (select distinct ida as ida from a) as a1 on a1.ida=b.idb or a1.ida = 1 ;
----
physical_plan <REGEX>:.*JOIN.*
query II
explain select b.*, a.* from b left join a on a.ida=b.idb;
----
physical_plan <REGEX>:.*JOIN.*
query II
explain select b.* from b left join a on a.ida=b.idb+1;
----
physical_plan <REGEX>:.*JOIN.*
query II
explain select tan(b.idb) from b left join a on a.ida=b.idb;
----
physical_plan <REGEX>:.*JOIN.*
query II
explain select b.valueb from b left join a on a.ida=b.idb;
----
physical_plan <REGEX>:.*JOIN.*
query II
explain select b.*, tan(a.ida) from b left join a on a.ida=b.idb;
----
physical_plan <REGEX>:.*JOIN.*
# this should eliminate inner join but keep outer one
query II
explain SELECT c.*
FROM c
LEFT JOIN (
SELECT DISTINCT b.*
FROM b
LEFT JOIN a ON a.ida = b.idb
) AS ab ON c.idc = ab.idb;
----
physical_plan <REGEX>:.*JOIN.*
# this should eliminate all join
query II
explain SELECT c.*
FROM c
LEFT JOIN (
SELECT DISTINCT b.*
FROM b
LEFT JOIN a ON a.ida = b.idb
) AS ab ON c.idc = ab.idb and c.valuec = ab.valueb;
----
physical_plan <!REGEX>:.*JOIN.*
query II
explain select b.*, tan(a.ida) from b left join a on a.ida=b.idb;
----
physical_plan <REGEX>:.*JOIN.*
# just verify results are same that optimized plan compare to original.
statement ok
select distinct b.* from b left join a on a.ida=b.idb;
statement ok
select distinct b.* from b left join a on a.ida=b.idb where b.valueb=1;
statement ok
select distinct b.* from b left join a on a.ida=b.idb where a.valuea=1;
statement ok
select b.* from b left join (select distinct ida as ida from a) as a1 on a1.ida=b.idb;
statement ok
select b.* from b left join (select * from a group by all) as a1 on b.idb=a1.ida;
statement ok
select b.* from b left join (select a.ida from a group by ida) as a1 on b.idb=a1.ida;
statement ok
select b.idb,sum(b.valueb) from b left join a on b.idb=a.ida group by b.idb;
statement ok
select b.idb,sum(a.valuea) from b left join a on b.idb=a.ida group by b.idb;
statement ok
select b.* from b left join (select distinct ida as ida from a) as a1 on a1.ida=b.idb and a1.ida = 1;
statement ok
select b.* from b left join (select distinct ida as ida from a) as a1 on a1.ida=b.idb and a1.ida = b.valueb ;
statement ok
select b.* from b left join (select distinct ida as ida, valuea as valuea from a) as a1 on a1.ida=b.idb and a1.ida = b.valueb and a1.valuea = b.idb;
statement ok
select b.* from b left join (select distinct ida as ida, valuea as valuea from a) as a1 on a1.ida=b.idb;
statement ok
select b.* from b left join (select distinct ida as ida from a) as a1 on a1.ida=b.idb or a1.ida = 1 ;
statement ok
select b.*, a.* from b left join a on a.ida=b.idb;
statement ok
select b.* from b left join a on a.ida=b.idb+1;
statement ok
select tan(b.idb) from b left join a on a.ida=b.idb;
statement ok
select b.valueb from b left join a on a.ida=b.idb;
statement ok
select b.*, tan(a.ida) from b left join a on a.ida=b.idb;
statement ok
SELECT c.*
FROM c
LEFT JOIN (
SELECT DISTINCT b.*
FROM b
LEFT JOIN a ON a.ida = b.idb
) AS ab ON c.idc = ab.idb;
statement ok
SELECT c.*
FROM c
LEFT JOIN (
SELECT DISTINCT b.*
FROM b
LEFT JOIN a ON a.ida = b.idb
) AS ab ON c.idc = ab.idb and c.valuec = ab.valueb;
statement ok
select b.*, tan(a.ida) from b left join a on a.ida=b.idb;
# union intersect unnest, pivot, delim scan, inline cte, materialized
query II
explain
select b.* from b left join (select distinct a.ida from a ) as a on a.ida=b.idb union all select b.* from b left join (select distinct a.ida from a ) as a on a.ida=b.idb;
----
physical_plan <!REGEX>:.*JOIN.*
statement ok
select b.* from b left join (select distinct a.ida from a ) as a on a.ida=b.idb union all select b.* from b left join (select distinct a.ida from a ) as a on a.ida=b.idb;
# we cannot use distinct statistics from outer child
statement ok
select a.ida from (select ida from a group by ida) as a left join b on ida < idb;
query II
explain select a.ida from (select ida from a group by ida) as a left join b on ida < idb;
----
physical_plan <REGEX>:.*JOIN.*
statement ok
select a.ida from b right join (select ida from a group by ida) as a on ida < idb;
query II
explain select a.ida from b right join (select ida from a group by ida) as a on ida < idb;
----
physical_plan <REGEX>:.*JOIN.*
|