1 # This file is a collection of regression and coverage tests
2 # for WL#5855: Print EXPLAIN in JSON format
4 --source include/have_innodb.inc
6 set @save_storage_engine= @@default_storage_engine;
7 set default_storage_engine=MyISAM;
8 set end_markers_in_json=on;
10 --echo #
new "FORMAT" keyword doesn
't conflict with the FORMAT() function name:
12 SELECT FORMAT(1, 2), FORMAT(1, 2, 3);
14 --echo # new "FORMAT" keyword is a valid identifier:
19 CREATE TABLE t1 (format INT);
20 SELECT format FROM t1;
23 --echo # different ways of format name writing:
25 EXPLAIN FORMAT=traditional SELECT 1;
26 EXPLAIN FORMAT='TrAdItIoNaL
' SELECT 1;
27 EXPLAIN FORMAT=JSON SELECT 1;
29 --error ER_UNKNOWN_EXPLAIN_FORMAT
30 EXPLAIN FORMAT=foo SELECT 1;
32 --echo # various EXPLAIN output
34 CREATE TABLE t1 (i INT);
35 CREATE TABLE t2 (i INT);
36 CREATE TABLE t3 (i INT);
37 CREATE TABLE t4 (i INT);
39 --echo # no end markers in JSON:
41 set end_markers_in_json=off;
42 EXPLAIN FORMAT=JSON SELECT * FROM t1;
43 set end_markers_in_json=on;
45 EXPLAIN INSERT INTO t1 VALUES (10);
46 EXPLAIN FORMAT=JSON INSERT INTO t1 VALUES (10);
48 EXPLAIN SELECT * FROM t1;
49 PREPARE stmt FROM 'EXPLAIN FORMAT=JSON SELECT * FROM t1
';
53 INSERT INTO t1 VALUES (1), (2), (3), (4), (5), (6), (7);
54 INSERT INTO t2 VALUES (1), (2);
57 SELECT * FROM (SELECT * FROM (SELECT * FROM (SELECT a1.i FROM (SELECT * FROM t1) a1, t2) a2) a3) a4;
59 SELECT * FROM (SELECT * FROM (SELECT * FROM (SELECT a1.i FROM (SELECT * FROM t1) a1, t2) a2) a3) a4;
61 --echo # subquery in WHERE
62 EXPLAIN SELECT * FROM t1 WHERE i IN (SELECT i FROM t2 WHERE t1.i = 10 ORDER BY RAND());
63 EXPLAIN FORMAT=JSON SELECT * FROM t1 WHERE i IN (SELECT i FROM t2 WHERE t1.i = 10 ORDER BY RAND());
65 --echo # two subqueries in WHERE
66 EXPLAIN SELECT * FROM t1
67 WHERE i IN (SELECT i FROM t2 WHERE t1.i = 10 ORDER BY RAND())
68 OR i IN (SELECT i FROM t4 ORDER BY RAND());
69 EXPLAIN FORMAT=JSON SELECT * FROM t1
70 WHERE i IN (SELECT i FROM t2 WHERE t1.i = 10 ORDER BY RAND())
71 OR i IN (SELECT i FROM t4 ORDER BY RAND());
75 EXPLAIN SELECT * FROM t1 UNION SELECT * FROM t2 UNION SELECT * FROM t3;
76 EXPLAIN FORMAT=JSON SELECT * FROM t1 UNION SELECT * FROM t2 UNION SELECT * FROM t3;
78 --echo # more complex UNION
80 EXPLAIN (SELECT t1.i FROM t1 JOIN t2) UNION ALL (SELECT * FROM t3 WHERE i IN (SELECT i FROM t4 ORDER BY RAND()));
81 EXPLAIN FORMAT=JSON (SELECT t1.i FROM t1 JOIN t2) UNION ALL (SELECT * FROM t3 WHERE i IN (SELECT i FROM t4 ORDER BY RAND()));
83 --echo # UNION with subquery in outer ORDER BY
85 EXPLAIN (SELECT * FROM t1) UNION (SELECT * FROM t2) ORDER BY (SELECT i LIMIT 1);
86 EXPLAIN FORMAT=JSON (SELECT * FROM t1) UNION (SELECT * FROM t2) ORDER BY (SELECT i LIMIT 1);
88 --echo # optimizer-time subquery
90 --let $query=SELECT * FROM t1 ORDER BY (SELECT LENGTH(1) FROM t2 LIMIT 1)
92 --eval EXPLAIN FORMAT=JSON $query
94 --echo # subquery in the HAVING clause
96 --let $query=SELECT * FROM t1 HAVING i > ALL (SELECT i FROM t2) OR i < ALL (SELECT i FROM t2);
98 --eval EXPLAIN FORMAT=JSON $query
100 --echo # subquery in the GROUP BY clause
102 --let $query=SELECT * FROM t1 GROUP BY i > ALL (SELECT i FROM t2) OR i < ALL (SELECT i FROM t2);
103 --eval EXPLAIN $query
104 --eval EXPLAIN FORMAT=JSON $query
106 --echo # subquery in the SELECT list
108 --let $query=SELECT (SELECT i + 1 FROM t1 ORDER BY RAND() LIMIT 1), i FROM t1;
109 --eval EXPLAIN $query
110 --eval EXPLAIN FORMAT=JSON $query
112 DROP TABLE t1, t2, t3, t4;
114 --echo # derived table that is optimized out
116 CREATE TABLE t1 (i INT);
117 EXPLAIN SELECT 1 FROM (SELECT 1 AS x FROM t1) tt WHERE x;
118 EXPLAIN FORMAT= JSON SELECT 1 FROM (SELECT 1 AS x FROM t1) tt WHERE x;
121 --echo # complex subqueries
123 CREATE TABLE t1 (a INT, b INT);
124 CREATE TABLE t2 (c INT, d INT);
125 CREATE TABLE t3 (e INT);
126 CREATE TABLE t4 (f INT, g INT);
127 INSERT INTO t1 VALUES (1,10), (2,10);
128 INSERT INTO t2 VALUES (2,10), (2,20);
129 INSERT INTO t3 VALUES (10), (30);
130 INSERT INTO t4 VALUES (2,10), (2,10);
132 --let $query = SELECT * FROM t1 WHERE t1.a IN (SELECT c FROM t2 WHERE (SELECT e FROM t3) < SOME(SELECT e FROM t3 WHERE t1.b));
133 --eval EXPLAIN $query
134 --eval EXPLAIN FORMAT=JSON $query
136 DROP TABLE t1, t2, t3, t4;
138 --echo # semi-join materialization (if enabled)
140 CREATE TABLE t1 (a INT);
141 INSERT INTO t1 VALUES (1), (1), (1);
142 CREATE TABLE t2 (a INT) SELECT * FROM t1;
143 CREATE TABLE t3 (a INT) SELECT * FROM t1;
144 CREATE TABLE t4 (a INT) SELECT * FROM t1;
148 WHERE t1.a IN (SELECT t2.a FROM t2 WHERE t2.a > 0) AND
149 t1.a IN (SELECT t3.a FROM t3 WHERE t3.a IN
150 (SELECT t4.a FROM t4 WHERE a > 0));
152 DROP TABLE t1, t2, t3, t4;
154 --echo # the same subquery is associated with two different JOIN_TABs
158 c1 VARCHAR(1) NOT NULL
161 INSERT INTO t1 VALUES (2,'w
');
165 c1 VARCHAR(1) NOT NULL,
166 c2 VARCHAR(1) NOT NULL,
170 INSERT INTO t2 VALUES (8,'d
','d
');
171 INSERT INTO t2 VALUES (4,'v
','v
');
174 c1 VARCHAR(1) NOT NULL
177 INSERT INTO t3 VALUES ('v
');
182 WHERE EXISTS (SELECT t2.c1
183 FROM (t2 INNER JOIN t3 ON (t3.c1 = t2.c1))
184 WHERE t2.c2 != t1.c1 AND t2.c2 = (SELECT MIN(t3.c1)
187 DROP TABLE t1, t2, t3;
189 --echo # multiple materialization groups
191 CREATE TABLE t1 (c_key INT, KEY c_key (c_key));
192 INSERT INTO t1 VALUES (1), (2), (3);
193 CREATE TABLE t2 (c INT, c_key INT);
194 INSERT INTO t2 VALUES (8,5),(4,5),(8,1);
195 CREATE TABLE t3 LIKE t1;
196 INSERT INTO t3 SELECT * FROM t1;
197 CREATE TABLE t4 LIKE t2;
198 INSERT INTO t4 SELECT * FROM t2;
199 CREATE TABLE t5 (c INT);
200 INSERT INTO t5 VALUES (1), (2), (3);
202 --echo # This should show two materialization groups where applicable
204 let $query=SELECT * FROM t5
205 WHERE c IN (SELECT t2.c FROM t1 JOIN t2 ON t2.c_key = t1.c_key)
206 AND c IN (SELECT t4.c FROM t3 JOIN t4 ON t4.c_key = t3.c_key);
207 --eval EXPLAIN $query
208 --eval EXPLAIN FORMAT=JSON $query
210 DROP TABLE t1, t2, t3, t4, t5;
212 CREATE TABLE t1 (i INT);
213 CREATE TABLE t2 (i INT);
214 CREATE TABLE t3 (i INT);
216 INSERT INTO t1 VALUES (1);
217 INSERT INTO t2 VALUES (1);
218 INSERT INTO t3 VALUES (1);
220 --echo # Subqueries in UPDATE values list
222 let $query=UPDATE t1 SET i=(SELECT i FROM t2);
223 --eval EXPLAIN $query
224 --eval EXPLAIN FORMAT=JSON $query
225 let $query=UPDATE t1, t2 SET t1.i=(SELECT i FROM t3);
226 --eval EXPLAIN $query
227 --eval EXPLAIN FORMAT=JSON $query
229 --echo # INSERT ... ON DUPLICATE KEY UPDATE x=(SELECT ...) value list
231 let $query=INSERT INTO t1 (i)
232 SELECT i FROM t2 ON DUPLICATE KEY UPDATE i=(SELECT i FROM t2);
233 --eval EXPLAIN $query
234 --eval EXPLAIN FORMAT=JSON $query
236 let $query=INSERT INTO t1 VALUES (1)
237 ON DUPLICATE KEY UPDATE i = (SELECT i FROM t2);
238 --eval EXPLAIN $query
239 --eval EXPLAIN FORMAT=JSON $query
241 --echo # Subqueries in INSERT VALUES tuples:
243 let $query=INSERT INTO t3 VALUES((SELECT i FROM t1)), ((SELECT i FROM t2));
244 --eval EXPLAIN $query
245 --eval EXPLAIN FORMAT=JSON $query
247 DROP TABLE t1, t2, t3;
249 --echo # Various queries
253 (SELECT 1 AS a, 2 AS b
255 SELECT 1 AS a, 2 AS b) t1
258 --eval EXPLAIN $query
259 --eval EXPLAIN FORMAT=JSON $query
263 CREATE TABLE t1(a INT, b INT);
264 INSERT INTO t1 VALUES (), ();
265 let $query=SELECT 1 FROM t1 GROUP BY GREATEST(t1.a, (SELECT 1 FROM (SELECT t1.b FROM t1, t1 t2 ORDER BY t1.a, t1.a LIMIT 1) AS d));
266 --eval EXPLAIN $query
267 --eval EXPLAIN FORMAT=JSON $query
272 CREATE TABLE t1(f1 INT);
273 INSERT INTO t1 VALUES (1),(1);
274 let $query=SELECT 1 FROM t1 WHERE (SELECT (SELECT 1 FROM t1 GROUP BY f1));
275 --eval EXPLAIN $query
276 --eval EXPLAIN FORMAT=JSON $query
281 CREATE TABLE t1 (i INT);
282 CREATE TABLE t2 (i INT, j INT);
283 INSERT INTO t1 VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10);
284 INSERT INTO t2 SELECT i, i * 10 FROM t1;
285 let $query=SELECT * FROM t1 ORDER BY (SELECT t2.j FROM t2 WHERE t2.i = t1.i);
286 --eval EXPLAIN $query
287 --eval EXPLAIN FORMAT=JSON $query
288 let $query=SELECT * FROM t1 GROUP BY (SELECT t2.j FROM t2 WHERE t2.i = t1.i);
289 --eval EXPLAIN $query
290 --eval EXPLAIN FORMAT=JSON $query
293 CREATE TABLE t1 (a INT NOT NULL, b INT NOT NULL, PRIMARY KEY(a, b));
294 INSERT INTO t1 VALUES (10,1),(10,2),(10,3),(20,4),(20,5),(20,6),
295 (30,7),(30,8),(30,9),(40,10),(40,11),(40,12),(40,13);
297 EXPLAIN FORMAT=JSON SELECT a, MIN(b) AS b FROM t1 GROUP BY a ORDER BY b;
303 CREATE TABLE t1 (a INT NOT NULL, b CHAR(3) NOT NULL, PRIMARY KEY (a));
304 INSERT INTO t1 VALUES (1,'ABC
'), (2,'EFG
'), (3,'HIJ
');
305 CREATE TABLE t2 (a INT NOT NULL,b CHAR(3) NOT NULL,PRIMARY KEY (a, b));
306 INSERT INTO t2 VALUES (1,'a
'),(1,'b
'),(3,'F
');
308 EXPLAIN FORMAT=JSON SELECT t1.a, GROUP_CONCAT(t2.b) AS b FROM t1 LEFT JOIN t2 ON t1.a=t2.a GROUP BY t1.a ORDER BY t1.b;
315 CREATE TABLE t1 (a INT, b INT);
317 INSERT INTO t1 VALUES
320 (4,1), (4,1), (4,1), (4,1),
323 EXPLAIN FORMAT=JSON SELECT SUM(b) FROM t1 GROUP BY a WITH ROLLUP;
327 --echo # Composition of DISTINCT, GROUP BY and ORDER BY
329 CREATE TABLE t1 (a INT, b INT);
330 INSERT INTO t1 VALUES (1, 1), (1, 2), (2, 1), (2, 2), (3, 1);
332 let $query=SELECT DISTINCT SUM(b) s FROM t1 GROUP BY a ORDER BY s;
333 --eval EXPLAIN FORMAT=JSON $query
336 SHOW SESSION STATUS WHERE (Variable_name LIKE 'Sort_%
' OR Variable_name LIKE 'Created_%_tables
') AND Value > 0;
340 --echo # "buffer_result" node
342 CREATE TABLE t1 (a INT NOT NULL);
343 CREATE TABLE t2 (a INT NOT NULL, PRIMARY KEY (a));
344 INSERT INTO t1 VALUES (1);
345 INSERT INTO t2 VALUES (1),(2);
346 EXPLAIN FORMAT=JSON SELECT SQL_BIG_RESULT DISTINCT t1.a FROM t1,t2 ORDER BY t2.a;
351 CREATE TABLE t1 (a INT NOT NULL, b INT, PRIMARY KEY (a));
352 CREATE TABLE t2 (a INT NOT NULL, PRIMARY KEY (a));
353 INSERT INTO t1 VALUES (1,10), (2,20), (3,30), (4,40);
354 INSERT INTO t2 VALUES (2), (3), (4), (5);
355 EXPLAIN FORMAT=JSON SELECT * FROM t2 WHERE t2.a IN (SELECT a FROM t1 WHERE t1.b <> 30);
358 set default_storage_engine= @save_storage_engine;