Skip to content

Commit 44bfbbd

Browse files
author
Amit Khandekar
committed
WL#15040 Add system variable to control EXPLAIN format.
Introduce a system variable 'explain_format' to specify a default format to be used in EXPLAIN statement when the user does not specify the format in the EXPLAIN statement. The compiled-in default value is TRADITIONAL. This variable can be set either globally or in a session. With hypergraph optimizer on, no format specified in EXPLAIN, and explain_format set to TRADITIONAL, display the plan using TREE format. With EXPLAIN ANALYZE, no format specified in EXPLAIN, and explain_format set to TRADITIONAL, again use TREE format. This preserves the existing behaviour. Besides the usual format values, explain_format can have an additional value "TRADITIONAL_STRICT", only for the sake of mtr tests. The mtr tests would fail with hypergraph because of the silent conversion from TRADITIONAL to TREE since that would result in plan outputs that would differ in hypergraph. So in mtr now this variable explain_format will be set to TRADITIONAL_STRICT, so that tests continue to show existing behaviour of failing EXPLAIN statements with hypergraph and still managing to pass because this particular error is ignored. Change-Id: Ib6b1819ede619de94b3b551aae812caec418ba8e
1 parent 5381646 commit 44bfbbd

19 files changed

+477
-67
lines changed

mysql-test/mysql-test-run.pl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6244,6 +6244,11 @@ ($$$)
62446244
mtr_add_arg($args, "--log-output=file");
62456245
}
62466246

6247+
# Force this initial explain_format value, so that tests don't fail with
6248+
# --hypergraph due to implicit conversion from TRADITIONAL to TREE. Check the
6249+
# definition of enum Explain_format_type for more details.
6250+
mtr_add_arg($args, "--explain-format=TRADITIONAL_STRICT");
6251+
62476252
# Indicate to mysqld it will be debugged in debugger
62486253
if ($glob_debugger) {
62496254
mtr_add_arg($args, "--gdb");

mysql-test/r/all_persisted_variables.result

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,17 @@ include/assert.inc [Expect 500+ variables in the table. Due to open Bugs, we are
4040

4141
# Test SET PERSIST
4242

43-
include/assert.inc [Expect 448 persisted variables in the table.]
43+
include/assert.inc [Expect 449 persisted variables in the table.]
4444

4545
************************************************************
4646
* 3. Restart server, it must preserve the persisted variable
4747
* settings. Verify persisted configuration.
4848
************************************************************
4949
# restart
5050

51-
include/assert.inc [Expect 448 persisted variables in persisted_variables table.]
52-
include/assert.inc [Expect 448 persisted variables shown as PERSISTED in variables_info table.]
53-
include/assert.inc [Expect 448 persisted variables with matching peristed and global values.]
51+
include/assert.inc [Expect 449 persisted variables in persisted_variables table.]
52+
include/assert.inc [Expect 449 persisted variables shown as PERSISTED in variables_info table.]
53+
include/assert.inc [Expect 449 persisted variables with matching peristed and global values.]
5454

5555
************************************************************
5656
* 4. Test RESET PERSIST IF EXISTS. Verify persisted variable

mysql-test/r/explain_tree.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ DROP TABLE t1;
727727
EXPLAIN ANALYZE FOR CONNECTION 1;
728728
ERROR 42000: This version of MySQL doesn't yet support 'EXPLAIN ANALYZE FOR CONNECTION'
729729
EXPLAIN ANALYZE FORMAT=TRADITIONAL SELECT 1;
730-
ERROR 42000: This version of MySQL doesn't yet support 'FORMAT=TRADITIONAL with EXPLAIN ANALYZE'
730+
ERROR 42000: This version of MySQL doesn't yet support 'EXPLAIN ANALYZE with TRADITIONAL format'
731731
EXPLAIN ANALYZE FORMAT=JSON SELECT 1;
732732
ERROR 42000: This version of MySQL doesn't yet support 'EXPLAIN ANALYZE with JSON format'
733733
EXPLAIN ANALYZE FORMAT=TREE SELECT 1;

mysql-test/r/mysqld--help-notwin.result

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,12 @@ The following options may be given as the first argument:
378378
options are set to nonzero values,
379379
binlog-expire-logs-seconds takes priority. Possible
380380
purges happen at startup and at binary log rotation.
381+
--explain-format[=name]
382+
The default format in which the EXPLAIN statement
383+
displays information. Valid values are TRADITIONAL
384+
(default), TREE, JSON and TRADITIONAL_STRICT.
385+
TRADITIONAL_STRICT is only used internally by the mtr
386+
test suite, and is not meant to be used anywhere else.
381387
--explicit-defaults-for-timestamp
382388
This option causes CREATE TABLE to create all TIMESTAMP
383389
columns as NULL with DEFAULT NULL attribute, Without this
@@ -1725,6 +1731,7 @@ enforce-gtid-consistency FALSE
17251731
eq-range-index-dive-limit 200
17261732
event-scheduler ON
17271733
expire-logs-days 0
1734+
explain-format TRADITIONAL
17281735
explicit-defaults-for-timestamp TRUE
17291736
external-locking FALSE
17301737
flush FALSE

mysql-test/r/mysqld--help-win.result

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,12 @@ The following options may be given as the first argument:
377377
options are set to nonzero values,
378378
binlog-expire-logs-seconds takes priority. Possible
379379
purges happen at startup and at binary log rotation.
380+
--explain-format[=name]
381+
The default format in which the EXPLAIN statement
382+
displays information. Valid values are TRADITIONAL
383+
(default), TREE, JSON and TRADITIONAL_STRICT.
384+
TRADITIONAL_STRICT is only used internally by the mtr
385+
test suite, and is not meant to be used anywhere else.
380386
--explicit-defaults-for-timestamp
381387
This option causes CREATE TABLE to create all TIMESTAMP
382388
columns as NULL with DEFAULT NULL attribute, Without this
@@ -1735,6 +1741,7 @@ enforce-gtid-consistency FALSE
17351741
eq-range-index-dive-limit 200
17361742
event-scheduler ON
17371743
expire-logs-days 0
1744+
explain-format TRADITIONAL
17381745
explicit-defaults-for-timestamp TRUE
17391746
external-locking FALSE
17401747
flush FALSE
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# WL15040: Add system variable to control EXPLAIN format
2+
3+
# Save the original explain_format value.
4+
set @orig_explain_format=@@global.explain_format;
5+
6+
# Test the initial value. It should be TRADITIONAL_STRICT.
7+
select @@explain_format, @@global.explain_format;
8+
9+
# hypergraph with TRADITIONAL_STRICT should error out, rather than convert to TREE.
10+
--error 0, ER_HYPERGRAPH_NOT_SUPPORTED_YET
11+
explain select 1;
12+
13+
set explain_format=traditional;
14+
15+
--echo # With hypergraph and TRADITIONAL, EXPLAIN should use TREE format.
16+
explain select 1;
17+
18+
--echo # Test that explain_format can be set to any of the scopes; i.e. session,
19+
--echo # global and persist.
20+
21+
set persist explain_format=tree;
22+
select @@explain_format, @@global.explain_format;
23+
set global explain_format=json;
24+
select @@explain_format, @@global.explain_format;
25+
set explain_format=tree;
26+
select @@explain_format, @@global.explain_format;
27+
set explain_format=default;
28+
select @@explain_format, @@global.explain_format;
29+
set global explain_format=default;
30+
select @@explain_format, @@global.explain_format;
31+
set persist explain_format=default;
32+
select @@explain_format, @@global.explain_format;
33+
set explain_format=default;
34+
select @@explain_format, @@global.explain_format;
35+
reset persist explain_format; # Required to clear the conf file.
36+
select @@explain_format, @@global.explain_format;
37+
# Also delete the conf file to remote test foot-prints
38+
let $MYSQLD_DATADIR= `select @@datadir`;
39+
--remove_file $MYSQLD_DATADIR/mysqld-auto.cnf
40+
41+
explain select 1;
42+
explain format=tree select 1;
43+
explain format=json select 1;
44+
45+
set explain_format=tree;
46+
explain select 1;
47+
explain format=json select 1;
48+
49+
set explain_format=json;
50+
explain format=tree select 1;
51+
explain select 1;
52+
53+
set explain_format=default;
54+
explain select 1;
55+
explain format=tree select 1;
56+
57+
set global explain_format=json; # Test that global setting does not interfere
58+
set explain_format=traditional;
59+
explain select 1;
60+
61+
# Analyze should work with both traditional and traditional_strict, regardless
62+
# of hypergraph.
63+
set explain_format=traditional;
64+
--replace_regex /time=\d+\.\d+\.\.\d+\.\d+/time=N.NNN..N.NNN/
65+
explain analyze select 1;
66+
set explain_format=traditional_strict;
67+
--replace_regex /time=\d+\.\d+\.\.\d+\.\d+/time=N.NNN..N.NNN/
68+
explain analyze select 1;
69+
70+
# But explicit use of unsupported format should error out.
71+
--error ER_NOT_SUPPORTED_YET
72+
explain analyze format=traditional select 1;
73+
74+
# TRADITIONAL_STRICT value is only allowed in the sys variable; not in EXPLAIN.
75+
--error ER_UNKNOWN_EXPLAIN_FORMAT
76+
explain format=traditional_strict select 1;
77+
78+
# Reset the global var to the original value
79+
set global explain_format=@orig_explain_format;
80+
81+
select @@explain_format, @@global.explain_format;

mysql-test/suite/sys_vars/r/all_vars.result

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ default_collation_for_utf8mb4
3030
default_collation_for_utf8mb4
3131
disabled_storage_engines
3232
disabled_storage_engines
33+
explain_format
34+
explain_format
3335
generated_random_password_length
3436
generated_random_password_length
3537
group_replication_consistency
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
set @orig_explain_format=@@global.explain_format;
2+
select @@explain_format, @@global.explain_format;
3+
@@explain_format @@global.explain_format
4+
TRADITIONAL_STRICT TRADITIONAL_STRICT
5+
explain select 1;
6+
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
7+
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL NULL No tables used
8+
Warnings:
9+
Note 1003 /* select#1 */ select 1 AS `1`
10+
set explain_format=traditional;
11+
# With hypergraph and TRADITIONAL, EXPLAIN should use TREE format.
12+
explain select 1;
13+
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
14+
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL NULL No tables used
15+
Warnings:
16+
Note 1003 /* select#1 */ select 1 AS `1`
17+
# Test that explain_format can be set to any of the scopes; i.e. session,
18+
# global and persist.
19+
set persist explain_format=tree;
20+
select @@explain_format, @@global.explain_format;
21+
@@explain_format @@global.explain_format
22+
TRADITIONAL TREE
23+
set global explain_format=json;
24+
select @@explain_format, @@global.explain_format;
25+
@@explain_format @@global.explain_format
26+
TRADITIONAL JSON
27+
set explain_format=tree;
28+
select @@explain_format, @@global.explain_format;
29+
@@explain_format @@global.explain_format
30+
TREE JSON
31+
set explain_format=default;
32+
select @@explain_format, @@global.explain_format;
33+
@@explain_format @@global.explain_format
34+
JSON JSON
35+
set global explain_format=default;
36+
select @@explain_format, @@global.explain_format;
37+
@@explain_format @@global.explain_format
38+
JSON TRADITIONAL
39+
set persist explain_format=default;
40+
select @@explain_format, @@global.explain_format;
41+
@@explain_format @@global.explain_format
42+
JSON TRADITIONAL
43+
set explain_format=default;
44+
select @@explain_format, @@global.explain_format;
45+
@@explain_format @@global.explain_format
46+
TRADITIONAL TRADITIONAL
47+
reset persist explain_format;
48+
select @@explain_format, @@global.explain_format;
49+
@@explain_format @@global.explain_format
50+
TRADITIONAL TRADITIONAL
51+
explain select 1;
52+
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
53+
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL NULL No tables used
54+
Warnings:
55+
Note 1003 /* select#1 */ select 1 AS `1`
56+
explain format=tree select 1;
57+
EXPLAIN
58+
-> Rows fetched before execution (cost=0.00..0.00 rows=1)
59+
60+
explain format=json select 1;
61+
EXPLAIN
62+
{
63+
"query_block": {
64+
"select_id": 1,
65+
"message": "No tables used"
66+
}
67+
}
68+
Warnings:
69+
Note 1003 /* select#1 */ select 1 AS `1`
70+
set explain_format=tree;
71+
explain select 1;
72+
EXPLAIN
73+
-> Rows fetched before execution (cost=0.00..0.00 rows=1)
74+
75+
explain format=json select 1;
76+
EXPLAIN
77+
{
78+
"query_block": {
79+
"select_id": 1,
80+
"message": "No tables used"
81+
}
82+
}
83+
Warnings:
84+
Note 1003 /* select#1 */ select 1 AS `1`
85+
set explain_format=json;
86+
explain format=tree select 1;
87+
EXPLAIN
88+
-> Rows fetched before execution (cost=0.00..0.00 rows=1)
89+
90+
explain select 1;
91+
EXPLAIN
92+
{
93+
"query_block": {
94+
"select_id": 1,
95+
"message": "No tables used"
96+
}
97+
}
98+
Warnings:
99+
Note 1003 /* select#1 */ select 1 AS `1`
100+
set explain_format=default;
101+
explain select 1;
102+
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
103+
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL NULL No tables used
104+
Warnings:
105+
Note 1003 /* select#1 */ select 1 AS `1`
106+
explain format=tree select 1;
107+
EXPLAIN
108+
-> Rows fetched before execution (cost=0.00..0.00 rows=1)
109+
110+
set global explain_format=json;
111+
set explain_format=traditional;
112+
explain select 1;
113+
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
114+
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL NULL No tables used
115+
Warnings:
116+
Note 1003 /* select#1 */ select 1 AS `1`
117+
set explain_format=traditional;
118+
explain analyze select 1;
119+
EXPLAIN
120+
-> Rows fetched before execution (cost=0.00..0.00 rows=1) (actual time=N.NNN..N.NNN rows=1 loops=1)
121+
122+
set explain_format=traditional_strict;
123+
explain analyze select 1;
124+
EXPLAIN
125+
-> Rows fetched before execution (cost=0.00..0.00 rows=1) (actual time=N.NNN..N.NNN rows=1 loops=1)
126+
127+
explain analyze format=traditional select 1;
128+
ERROR 42000: This version of MySQL doesn't yet support 'EXPLAIN ANALYZE with TRADITIONAL format'
129+
explain format=traditional_strict select 1;
130+
ERROR HY000: Unknown EXPLAIN format name: 'traditional_strict'
131+
set global explain_format=@orig_explain_format;
132+
select @@explain_format, @@global.explain_format;
133+
@@explain_format @@global.explain_format
134+
TRADITIONAL_STRICT TRADITIONAL_STRICT

0 commit comments

Comments
 (0)