File: opt_explain_json.cc

package info (click to toggle)
mysql-8.0 8.0.43-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,924 kB
  • sloc: cpp: 4,684,605; ansic: 412,450; pascal: 108,398; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; sh: 24,181; python: 21,816; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,076; makefile: 2,194; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (2115 lines) | stat: -rw-r--r-- 74,500 bytes parent folder | download
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
/* Copyright (c) 2011, 2025, Oracle and/or its affiliates.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License, version 2.0,
   as published by the Free Software Foundation.

   This program is designed to work with certain software (including
   but not limited to OpenSSL) that is licensed under separate terms,
   as designated in a particular file or component or in included license
   documentation.  The authors of MySQL hereby grant you an additional
   permission to link the program and your derivative works with the
   separately licensed software that they have either included with
   the program or referenced in the documentation.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License, version 2.0, for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */

#include "sql/opt_explain_json.h"

#include <assert.h>
#include <sys/types.h>  // uint

#include <climits>
#include <cstddef>  // size_t
#include <cstdio>

#include "m_ctype.h"
#include "m_string.h"
#include "my_alloc.h"  // operator new
#include "my_compiler.h"

#include "sql-common/json_dom.h"
#include "sql/current_thd.h"  // current_thd
#include "sql/enum_query_type.h"
#include "sql/item.h"
#include "sql/item_sum.h"
#include "sql/key_spec.h"
#include "sql/mysqld.h"
#include "sql/opt_trace.h"          // Opt_trace_object
#include "sql/opt_trace_context.h"  // Opt_trace_context
#include "sql/protocol.h"           // Protocol
#include "sql/query_result.h"       // Query_result
#include "sql/sql_class.h"          // THD
#include "sql/sql_list.h"
#include "sql/system_variables.h"
#include "sql/table.h"
#include "sql/temp_table_param.h"
#include "sql/window.h"
#include "sql_string.h"

class Query_expression;

/**
  Property names, former parts of traditional "extra" column

  This array must be in sync with Extra_tag enum.
*/
static const char *json_extra_tags[ET_total] = {
    nullptr,                               // ET_none
    "using_temporary_table",               // ET_USING_TEMPORARY
    "using_filesort",                      // ET_USING_FILESORT
    "index_condition",                     // ET_USING_INDEX_CONDITION
    nullptr,                               // ET_USING
    "range_checked_for_each_record",       // ET_RANGE_CHECKED_FOR_EACH_RECORD
    "pushed_condition",                    // ET_USING_PUSHED_CONDITION
    "using_where",                         // ET_USING_WHERE
    "not_exists",                          // ET_NOT_EXISTS
    "using_MRR",                           // ET_USING_MRR
    "using_index",                         // ET_USING_INDEX
    "full_scan_on_NULL_key",               // ET_FULL_SCAN_ON_NULL_KEY
    "using_index_for_group_by",            // ET_USING_INDEX_FOR_GROUP_BY
    "using_index_for_skip_scan",           // ET_USING_INDEX_FOR_SKIP_SCAN
    "distinct",                            // ET_DISTINCT
    "loosescan",                           // ET_LOOSESCAN
    nullptr,                               // ET_START_TEMPORARY
    nullptr,                               // ET_END_TEMPORARY
    "first_match",                         // ET_FIRST_MATCH
    nullptr,                               // ET_MATERIALIZE
    nullptr,                               // ET_START_MATERIALIZE
    nullptr,                               // ET_END_MATERIALIZE
    nullptr,                               // ET_SCAN
    "using_join_buffer",                   // ET_USING_JOIN_BUFFER
    "const_row_not_found",                 // ET_CONST_ROW_NOT_FOUND
    "unique_row_not_found",                // ET_UNIQUE_ROW_NOT_FOUND
    "impossible_on_condition",             // ET_IMPOSSIBLE_ON_CONDITION
    "pushed_join",                         // ET_PUSHED_JOIN
    "ft_hints",                            // ET_FT_HINTS
    "backward_index_scan",                 // ET_BACKWARD_SCAN
    "recursive",                           // ET_RECURSIVE
    "table_function",                      // ET_TABLE_FUNCTION
    "skip_records_in_range_due_to_force",  // ET_SKIP_RECORDS_IN_RANGE
    "using_secondary_engine",              // ET_USING_SECONDARY_ENGINE
    "rematerialize"                        // ET_REMATERIALIZE
};

// JSON key names
static const char K_ACCESS_TYPE[] = "access_type";
static const char K_ATTACHED_CONDITION[] = "attached_condition";
static const char K_ATTACHED_SUBQUERIES[] = "attached_subqueries";
static const char K_BUFFER_RESULT[] = "buffer_result";
static const char K_CACHEABLE[] = "cacheable";
static const char K_DEPENDENT[] = "dependent";
static const char K_DUPLICATES_REMOVAL[] = "duplicates_removal";
static const char K_FILTERED[] = "filtered";
static const char K_FRAME_BUFFER[] = "frame_buffer";
static const char K_FUNCTIONS[] = "functions";

static const char K_GROUPING_OPERATION[] = "grouping_operation";
static const char K_GROUP_BY_SUBQUERIES[] = "group_by_subqueries";
static const char K_HAVING_SUBQUERIES[] = "having_subqueries";
static const char K_INSERT_VALUES_SUBQUERIES[] = "insert_values_subqueries";
static const char K_INSERT_UPDATE_SUBQUERIES[] = "insert_update_subqueries";
static const char K_KEY[] = "key";
static const char K_KEY_LENGTH[] = "key_length";
static const char K_MATERIALIZED_FROM_SUBQUERY[] = "materialized_from_subquery";
static const char K_MESSAGE[] = "message";
static const char K_NAME[] = "name";
static const char K_NESTED_LOOP[] = "nested_loop";
static const char K_OPTIMIZED_AWAY_SUBQUERIES[] = "optimized_away_subqueries";
static const char K_OPTIMIZED_FRAME_EVALUATION[] = "optimized_frame_evaluation";
static const char K_ORDERING_OPERATION[] = "ordering_operation";
static const char K_ORDER_BY_SUBQUERIES[] = "order_by_subqueries";
static const char K_PARTITIONS[] = "partitions";
static const char K_POSSIBLE_KEYS[] = "possible_keys";
static const char K_QUERY_BLOCK[] = "query_block";
static const char K_QUERY_SPECIFICATIONS[] = "query_specifications";
static const char K_REF[] = "ref";
static const char K_SELECT_ID[] = "select_id";
static const char K_SELECT_LIST_SUBQUERIES[] = "select_list_subqueries";
static const char K_SHARING_TMP_TABLE[] = "sharing_temporary_table_with";
static const char K_TABLE[] = "table";
static const char K_TABLE_NAME[] = "table_name";
static const char K_UNION_RESULT[] = "union_result";
static const char K_INTERSECT_RESULT[] = "intersect_result";
static const char K_EXCEPT_RESULT[] = "except_result";
static const char K_UNARY_RESULT[] = "unary_result";
static const char K_UPDATE_VALUE_SUBQUERIES[] = "update_value_subqueries";
static const char K_USED_KEY_PARTS[] = "used_key_parts";
static const char K_USING_FILESORT[] = "using_filesort";
static const char K_FILESORT_KEY[] = "filesort_key";

static const char K_USING_TMP_TABLE[] = "using_temporary_table";

static const char K_WINDOW_DEF_POS[] = "definition_position";
static const char K_WINDOW_LAST_EXECUTED[] = "last_executed_window";
static const char K_WINDOWS[] = "windows";
static const char K_WINDOWING[] = "windowing";

static const char K_ROWS[] = "rows_examined_per_scan";
static const char K_PREFIX_ROWS[] = "rows_produced_per_join";

static const char K_COST_INFO[] = "cost_info";
static const char K_READ_TIME[] = "read_cost";
static const char K_PREFIX_COST[] = "prefix_cost";
static const char K_COND_COST[] = "eval_cost";
static const char K_SORT_COST[] = "sort_cost";
static const char K_QUERY_COST[] = "query_cost";
static const char K_DATA_SIZE_QUERY[] = "data_read_per_join";
static const char K_USED_COLUMNS[] = "used_columns";

static const char *mod_type_name[] = {"", "insert", "update", "delete",
                                      "replace"};

/*
  see commentary at the beginning of opt_trace.cc
*/
namespace opt_explain_json_namespace {

class joinable_ctx;
class sort_ctx;
class subquery_ctx;
class setop_result_ctx;
class window_ctx;

/**
  @note Keep in sync with the @c list_names array.
*/
enum subquery_list_enum {
  SQ_SELECT_LIST,     ///< SELECT list subqueries
  SQ_UPDATE_VALUE,    ///< UPDATE ... SET field=(subquery)
  SQ_INSERT_VALUES,   ///< subqueries in VALUES of INSERT ... VALUES
  SQ_INSERT_UPDATE,   ///< subqueries in UPDATE of
                      ///< INSERT ... ON DUPLICATE KEY UPDATE
  SQ_HAVING,          ///< HAVING clause subqueries
  SQ_OPTIMIZED_AWAY,  ///< "optimized_away_subqueries"
  //--------------
  SQ_toplevel,  ///< SQ array size for unit_ctx
  //--------------
  SQ_ORDER_BY,  ///< ORDER BY clause subqueries
  SQ_GROUP_BY,  ///< GROUP BY clause subqueries
  //--------------
  SQ_total
};

/**
  @note Keep in sync with @c subquery_list_enum.
*/
static const char *list_names[SQ_total] = {
    K_SELECT_LIST_SUBQUERIES,
    K_UPDATE_VALUE_SUBQUERIES,
    K_INSERT_VALUES_SUBQUERIES,
    K_INSERT_UPDATE_SUBQUERIES,
    K_HAVING_SUBQUERIES,
    K_OPTIMIZED_AWAY_SUBQUERIES,
    "",
    K_ORDER_BY_SUBQUERIES,
    K_GROUP_BY_SUBQUERIES,
};

/**
  Base class for all intermediate tree nodes
*/

class context : public Explain_context {
 protected:
  const char *name;

 public:
  context *parent;  ///< link to parent node or NULL

  context(enum_parsing_context type_arg, const char *name_arg,
          context *parent_arg)
      : Explain_context(type_arg), name(name_arg), parent(parent_arg) {}

  virtual ~context() = default;

  /**
    Pass the node with its child nodes to a JSON formatter

    @param json         Formatter

    @retval false       Ok
    @retval true        Error

    @note The @c join_ctx class overloads this function.
  */
  virtual bool format(Opt_trace_context *json) {
    Opt_trace_object obj(json, name);
    return format_body(json, &obj);
  }

  bool is_query_block() const { return name == K_QUERY_BLOCK; }

 private:
  /**
    Format JSON object body

    @param json         Formatter
    @param obj          Object of this body

    @retval false       Ok
    @retval true        Error
  */
  virtual bool format_body(Opt_trace_context *json, Opt_trace_object *obj) = 0;

 public:
  /**
    Analogue of the "id" column in the traditional EXPLAIN output

    @param hide         if true, ban the output of K_SELECT_ID JSON property
                        in the underlying table_with_where_and_derived_ctx
                        objects

    @returns "Select number" that is associated with this node
  */
  virtual size_t id(bool hide = false) = 0;

  virtual bool cacheable() {
    assert(0);
    return true;
  }
  virtual bool dependent() {
    assert(0);
    return false;
  }

  virtual class qep_row *entry() {
    assert(0);
    return nullptr;
  }
  virtual enum_mod_type get_mod_type() { return MT_NONE; }

  /**
    Associate a child node with this node

    This function is to be overloaded by subquery_ctx.
  */
  virtual void set_child(context *) {}

  /// associate CTX_UNION_RESULT node with CTX_UNION node
  virtual void set_setop_result(setop_result_ctx *) { assert(0); }

  /**
    Append a subquery node to the specified list of the unit node

    @param subquery_type    Describes the Item tree where the subquery exists
    @param ctx              Subquery node

    @retval false           Ok
    @retval true            Error
  */
  virtual bool add_subquery(subquery_list_enum subquery_type [[maybe_unused]],
                            subquery_ctx *ctx [[maybe_unused]]) {
    assert(0);
    return true;
  }
  /**
    Format nested loop join subtree (if any) to JSON formatter

    @param json                 Formatter

    @retval false               Ok
    @retval true                Error
  */
  virtual bool format_nested_loop(Opt_trace_context *json [[maybe_unused]]) {
    assert(0);
    return true;
  }

  /**
    Add a CTX_QEP_TAB node to a CTX_JOIN node

    @param ctx          CTX_QEP_TAB node

    @retval false           Ok
    @retval true            Error
  */
  virtual bool add_join_tab(joinable_ctx *ctx [[maybe_unused]]) {
    assert(0);
    return true;
  }

  /**
    Set nested ORDER BY/GROUP BY/DISTINCT node to @c ctx
  */
  virtual void set_sort(sort_ctx *ctx [[maybe_unused]]) { assert(0); }

  /**
    Set nested WINDOW node to @c ctx
  */
  virtual void set_window(window_ctx *ctx [[maybe_unused]]) { assert(0); }

  /**
    Add a query specification node to the CTX_UNION node

    @param ctx              query specification node

    @retval false           Ok
    @retval true            Error
  */
  virtual bool add_query_spec(context *ctx [[maybe_unused]]) { return false; }

  /**
    Try to associate a derived subquery node with this or underlying node

    @param subquery     Derived subquery node

    @retval true        Success
    @retval false       Can't associate: this node or its child nodes are not
                        derived from the subquery
  */
  virtual bool find_and_set_derived(context *subquery [[maybe_unused]]) {
    assert(0);
    return false;
  }

  /**
    Associate WHERE subqueries of given context and unit with this object

    @param ctx          Context of WHERE subquery
    @param subquery     For CTX_QEP_TAB: match given unit with a previously
                        collected by the register_where_subquery function.
    @returns
      -1   subquery wasn't found
       0   subqusery were added
       1   error occurred
  */
  virtual int add_where_subquery(subquery_ctx *ctx [[maybe_unused]],
                                 Query_expression *subquery [[maybe_unused]]) {
    assert(0);
    return false;
  }

  /// Helper function to format output for derived subquery if any
  virtual bool format_derived(Opt_trace_context *) { return false; }

  /// Helper function to format output for associated WHERE subqueries if any
  virtual bool format_where(Opt_trace_context *) { return false; }

  /// Helper function to format output for HAVING, ORDER/GROUP BY subqueries
  virtual bool format_query_expression(Opt_trace_context *) { return false; }
};

/**
  Node class to wrap a subquery node tree

  Implements CTX_WHERE, CTX_HAVING, CTX_ORDER_BY_SQ, CTX_GROUP_BY_SQ and
  CTX_OPTIMIZED_AWAY_SUBQUERY context nodes.
  This class hosts underlying join_ctx or uion_ctx.

  Is used for a subquery, a derived table.
*/

class subquery_ctx : virtual public context, public qep_row {
  /*
    TODO: After the conversion from multiple inheritace to templates
    convert "context" to "unit_ctx" (common base of uion_ctx & join_ctx).
  */
  context *subquery;  ///< hosted subquery tree: CTX_JOIN, CTX_UNION,
                      ///< CTX_INTERSECT, CTX_EXCEPT or CTX_UNARY

 public:
  subquery_ctx(enum_parsing_context type_arg, const char *name_arg,
               context *parent_arg)
      : context(type_arg, name_arg, parent_arg), subquery(nullptr) {}

  qep_row *entry() override { return this; }

  /*
    Materialized subquery statuses of dependency on the outer query and
    cacheability may differ from the source subquery, for example, if
    we "push down" the outer look up value for SJ.
    Thus, for materialized subqueries return direct is_cacheable and
    is_dependent values instead of source subquery statuses:
  */
  bool cacheable() override {
    return is_materialized_from_subquery ? is_cacheable : subquery->cacheable();
  }
  bool dependent() override {
    return is_materialized_from_subquery ? is_dependent : subquery->dependent();
  }

  bool format(Opt_trace_context *json) override {
    if (name)
      return context::format(json);
    else {
      /*
        Subquery is always a homogeneous array element,
        create anonymous  wrapper object:
      */
      Opt_trace_object anonymous_wrapper(json);
      return format_body(json, &anonymous_wrapper);
    }
  }

 private:
  bool format_body(Opt_trace_context *json, Opt_trace_object *obj) override {
    if (type == CTX_DERIVED) {
      if (derived_clone_id) {
        Opt_trace_object(json, K_SHARING_TMP_TABLE)
            .add(K_SELECT_ID, derived_clone_id);
        // Don't show underlying tables of derived table clone
        return false;
      }
      obj->add(K_USING_TMP_TABLE, true);
      obj->add(K_DEPENDENT, dependent());
      obj->add(K_CACHEABLE, cacheable());
      return subquery->format(json);
    } else if (using_temporary) {
      if (!is_materialized_from_subquery) {
        obj->add(K_USING_TMP_TABLE, true);
        obj->add(K_DEPENDENT, dependent());
        obj->add(K_CACHEABLE, cacheable());
      }

      {
        Opt_trace_object tmp_table(json, K_TABLE);

        if (!col_table_name.is_empty())
          tmp_table.add_utf8(K_TABLE_NAME, col_table_name.str);
        if (!col_join_type.is_empty())
          tmp_table.add_alnum(K_ACCESS_TYPE, col_join_type.str);
        if (!col_key.is_empty()) tmp_table.add_utf8(K_KEY, col_key.str);
        if (!col_key_len.is_empty())
          tmp_table.add_alnum(K_KEY_LENGTH, col_key_len.str);
        if (!col_rows.is_empty()) tmp_table.add(K_ROWS, col_rows.value);

        if (is_materialized_from_subquery) {
          Opt_trace_object materialized(json, K_MATERIALIZED_FROM_SUBQUERY);
          materialized.add(K_USING_TMP_TABLE, true);
          materialized.add(K_DEPENDENT, dependent());
          materialized.add(K_CACHEABLE, cacheable());
          return format_query_block(json);
        }
      }
      return format_query_block(json);
    } else {
      obj->add(K_DEPENDENT, dependent());
      obj->add(K_CACHEABLE, cacheable());
      return subquery->format(json);
    }
  }

  bool format_query_block(Opt_trace_context *json) {
    if (subquery->is_query_block()) return subquery->format(json);

    Opt_trace_object query_block(json, K_QUERY_BLOCK);
    return subquery->format(json);
  }

 public:
  void set_child(context *child) override {
    assert(subquery == nullptr);
    assert(child->type == CTX_JOIN || child->type == CTX_UNION ||
           child->type == CTX_INTERSECT || child->type == CTX_EXCEPT ||
           child->type == CTX_UNARY);
    subquery = child;
  }

  size_t id(bool hide) override { return subquery->id(hide); }
};

/**
  Helper function to pass a subquery list to a JSON formatter

  @param json         output formatter
  @param subqueries   subquery list to output
  @param name         name for the output section

  @retval false       Ok
  @retval true        Error
*/
static bool format_list(Opt_trace_context *json, List<subquery_ctx> &subqueries,
                        const char *name) {
  if (!subqueries.is_empty()) {
    Opt_trace_array subs(json, name);

    List_iterator<subquery_ctx> it(subqueries);
    subquery_ctx *t;
    while ((t = it++)) {
      // Homogeneous array: additional anonymous wrapper object is not needed
      if (t->format(json)) return true;
    }
  }
  return false;
}

/**
  Helper base class to host HAVING, ORDER BY and GROUP BY subquery nodes
*/
class unit_ctx : virtual public context {
  List<subquery_ctx> subquery_lists[SQ_total];

 public:
  unit_ctx(enum_parsing_context type_arg, const char *name_arg,
           context *parent_arg)
      : context(type_arg, name_arg, parent_arg) {}

  /**
    Helper function to distinguish subquery-less nodes

    @retval true        Node hosts no subqueries
    @retval false       Node hosts some subqueries
  */
  bool has_no_subqueries() const {
    for (size_t i = 0; i < SQ_total; i++) {
      if (!subquery_lists[i].is_empty()) return false;
    }
    return true;
  }

  bool format_query_expression(Opt_trace_context *json) override {
    for (size_t i = 0; i < SQ_total; i++) {
      if (format_list(json, subquery_lists[i], list_names[i])) return true;
    }
    return false;
  }

  bool add_subquery(subquery_list_enum subquery_type,
                    subquery_ctx *ctx) override {
    return subquery_lists[subquery_type].push_back(ctx);
  }
};

class table_base_ctx : virtual public context, virtual public qep_row {
 protected:
  bool is_hidden_id;  ///< if true, don't output K_SELECT_ID property

 public:
  table_base_ctx(enum_parsing_context type_arg, const char *name_arg,
                 context *parent_arg)
      : context(type_arg, name_arg, parent_arg), is_hidden_id(false) {}

  qep_row *entry() override { return this; }

 protected:
  bool format_body(Opt_trace_context *json, Opt_trace_object *obj) override;

 public:
  size_t id(bool) override { return col_id.is_empty() ? 0 : col_id.value; }

  bool cacheable() override { return is_cacheable; }
  bool dependent() override { return is_dependent; }
};

static void add_string_array(Opt_trace_context *json, const char *list_name,
                             List<const char> &strings) {
  if (!strings.is_empty()) {
    Opt_trace_array extra(json, list_name);

    List_iterator<const char> it(strings);
    const char *s;
    while ((s = it++)) extra.add_utf8(s);
  }
}

static void print_cost(char *buf, uint buf_len, double cost) {
  if (cost < 100000000000000.0)
    snprintf(buf, buf_len, "%.2f", cost);
  else
    snprintf(buf, buf_len, "%.14g", cost);
}

static void print_filtered(char *buf, uint buf_len, double filtered) {
  snprintf(buf, buf_len, "%.2f", filtered);
}

bool table_base_ctx::format_body(Opt_trace_context *json,
                                 Opt_trace_object *obj) {
  StringBuffer<64> buff;

  if (mod_type != MT_NONE) obj->add(mod_type_name[mod_type], true);

  if (!col_id.is_empty() && !is_hidden_id) obj->add(K_SELECT_ID, col_id.value);

  if (!col_table_name.is_empty())
    obj->add_utf8(K_TABLE_NAME, col_table_name.str);

  add_string_array(json, K_PARTITIONS, col_partitions);

  if (!col_join_type.is_empty())
    obj->add_alnum(K_ACCESS_TYPE, col_join_type.str);

  add_string_array(json, K_POSSIBLE_KEYS, col_possible_keys);

  if (!col_key.is_empty()) obj->add_utf8(K_KEY, col_key.str);

  if (!col_key_parts.is_empty())
    add_string_array(json, K_USED_KEY_PARTS, col_key_parts);

  if (!col_key_len.is_empty()) obj->add_alnum(K_KEY_LENGTH, col_key_len.str);

  add_string_array(json, K_REF, col_ref);

  if (!col_rows.is_empty()) obj->add(K_ROWS, col_rows.value);
  if (!col_prefix_rows.is_empty())
    obj->add(K_PREFIX_ROWS, col_prefix_rows.value);

  if (!col_filtered.is_empty()) {
    char buf[32];  // 32 is enough for digits of a double
    print_filtered(buf, sizeof(buf), col_filtered.value);
    obj->add_utf8(K_FILTERED, buf);
  }

  format_extra(obj);

  if (!col_read_cost.is_empty()) {
    Opt_trace_object cost_info(json, K_COST_INFO);
    char buf[32];  // 32 is enough for digits of a double

    print_cost(buf, sizeof(buf), col_read_cost.value);
    cost_info.add_utf8(K_READ_TIME, buf);

    if (!col_cond_cost.is_empty()) {
      print_cost(buf, sizeof(buf), col_cond_cost.value);
      cost_info.add_utf8(K_COND_COST, buf);
    }
    if (!col_prefix_cost.is_empty()) {
      print_cost(buf, sizeof(buf), col_prefix_cost.value);
      cost_info.add_utf8(K_PREFIX_COST, buf);
    }
    if (!col_data_size_query.is_empty())
      cost_info.add_utf8(K_DATA_SIZE_QUERY, col_data_size_query.str);
  }

  if (!col_used_columns.is_empty())
    add_string_array(json, K_USED_COLUMNS, col_used_columns);

  if (!col_partial_update_columns.is_empty())
    add_string_array(json, "partial_update_columns",
                     col_partial_update_columns);

  if (!col_message.is_empty() && type != CTX_MESSAGE) {
    assert(col_extra.is_empty());
    obj->add_alnum(K_MESSAGE, col_message.str);
  }

  {  // Keep together for better output readability
    if (!col_attached_condition.is_empty())
      obj->add_utf8(K_ATTACHED_CONDITION, col_attached_condition.str);
    if (format_where(json)) return true;
  }

  return format_derived(json) || format_query_expression(json);
}

/**
  Node class for the CTX_UNION_RESULT
*/
class setop_result_ctx : public table_base_ctx, public unit_ctx {
  List<context> *query_specs;  ///< query specification nodes (inner selects)
  List<subquery_ctx> order_by_subqueries;
  List<subquery_ctx> optimized_away_subqueries;
  joinable_ctx *message;

 public:
  explicit setop_result_ctx(context *parent_arg, enum_parsing_context ctx,
                            const char *str)
      : context(ctx, str, parent_arg),
        table_base_ctx(ctx, str, parent_arg),
        unit_ctx(ctx, str, parent_arg),
        message(nullptr) {}

  // Remove warnings: 'inherits ... from ... via dominance'
  size_t id(bool hide) override { return table_base_ctx::id(hide); }
  bool cacheable() override { return table_base_ctx::cacheable(); }
  bool dependent() override { return table_base_ctx::dependent(); }
  qep_row *entry() override { return table_base_ctx::entry(); }
  bool format_query_expression(Opt_trace_context *json) override {
    return table_base_ctx::format_query_expression(json);
  }

  void push_down_query_specs(List<context> *specs) { query_specs = specs; }

  bool add_subquery(subquery_list_enum subquery_type,
                    subquery_ctx *ctx) override {
    switch (subquery_type) {
      case SQ_ORDER_BY:
        return order_by_subqueries.push_back(ctx);
      case SQ_OPTIMIZED_AWAY:
        return optimized_away_subqueries.push_back(ctx);
      default:
        assert(!"Unknown query type!");
        return false;  // ignore in production
    }
  }

  bool add_join_tab(joinable_ctx *ctx) override {
    assert(!message);
    message = ctx;
    return false;
  }

  bool format(Opt_trace_context *json) override;

  bool format_body(Opt_trace_context *json, Opt_trace_object *obj) override;
};

/**
  Common part of CTX_QEP_TAB and CTX_MESSAGE nodes

  This class implements functionality for WHERE and derived subqueries that
  are associated with the table node.
*/
class table_with_where_and_derived : public table_base_ctx {
 public:
  List<subquery_ctx> where_subqueries;  ///< associated WHERE clause subqueries

  table_with_where_and_derived(enum_parsing_context type_arg,
                               const char *name_arg, context *parent_arg)
      : context(type_arg, name_arg, parent_arg),
        table_base_ctx(type_arg, name_arg, parent_arg) {}

  size_t id(bool hide) override {
    if (hide) is_hidden_id = true;
    return table_base_ctx::id(hide);
  }

  bool format_where(Opt_trace_context *json) override {
    return format_list(json, where_subqueries, K_ATTACHED_SUBQUERIES);
  }

  bool format_derived(Opt_trace_context *json) override {
    if (derived_from.elements == 0) return false;
    if (derived_from.elements == 1) return derived_from.head()->format(json);
    Opt_trace_array loops(json, K_NESTED_LOOP);

    List_iterator<context> it(derived_from);
    context *c;
    while ((c = it++)) {
      Opt_trace_object anonymous_wrapper(json);
      if (c->format(json)) return true;
    }
    return false;
  }
};

/**
  Base for CTX_QEP_TAB, CTX_DUPLICATES_WEEDOUT and CTX_MATERIALIZATION nodes

  This class implements a base to explain individual JOIN_TABs as well
  as JOIN_TAB groups like in semi-join materialization.
*/
class joinable_ctx : virtual public context {
 public:
  joinable_ctx(enum_parsing_context type_arg, const char *name_arg,
               context *parent_arg)
      : context(type_arg, name_arg, parent_arg) {}
};

/**
  Node class for CTX_MESSAGE

  This class is designed to represent fake tables with some messages in the
  "extra" column ("Impossible where" etc).
  We do EXPLAIN of these fake tables to replace explanation of:
    1) usual actual JOIN_TABs of the whole JOIN or
    2) a modifying TABLE of single-table UPDATE/DELETE/etc.
  So, message_ctx always represent a single half-empty fake table in a
  "query_block" node with optional subqueries.
*/
class message_ctx : public joinable_ctx, public table_with_where_and_derived {
 public:
  explicit message_ctx(context *parent_arg)
      : context(CTX_MESSAGE, K_TABLE, parent_arg),
        joinable_ctx(CTX_MESSAGE, K_TABLE, parent_arg),
        table_with_where_and_derived(CTX_MESSAGE, K_TABLE, parent_arg) {}

  // Remove warnings: 'inherits ... from ... via dominance'
  bool format_body(Opt_trace_context *json, Opt_trace_object *obj) override {
    return table_base_ctx::format_body(json, obj);
  }
  size_t id(bool hide) override {
    return table_with_where_and_derived::id(hide);
  }
  bool cacheable() override { return table_base_ctx::cacheable(); }
  bool dependent() override { return table_base_ctx::dependent(); }
  qep_row *entry() override { return table_base_ctx::entry(); }
  bool format_derived(Opt_trace_context *json) override {
    return table_with_where_and_derived::format_derived(json);
  }
  bool format_where(Opt_trace_context *json) override {
    return table_with_where_and_derived::format_where(json);
  }

  bool find_and_set_derived(context *subquery) override {
    /*
      message_ctx is designed to represent a single fake JOIN_TAB in the JOIN,
      so if the JOIN have a derived table, then this message_ctx represent this
      derived table.
      Unconditionally add subquery:
    */
    derived_from.push_back(subquery);
    return true;
  }

  int add_where_subquery(subquery_ctx *ctx, Query_expression *) override {
    return where_subqueries.push_back(ctx);
  }
};

/**
  Node class for the CTX_QEP_TAB context
*/
class join_tab_ctx : public joinable_ctx, public table_with_where_and_derived {
  /**
    Subquery units that are associated with this JOIN_TAB's condition

    This list is used to match with the @c subquery parameter of
    the @c add_where_subquery function.
  */
  List<Query_expression> where_subquery_units;

 public:
  join_tab_ctx(enum_parsing_context type_arg, context *parent_arg)
      : context(type_arg, K_TABLE, parent_arg),
        joinable_ctx(type_arg, K_TABLE, parent_arg),
        table_with_where_and_derived(type_arg, K_TABLE, parent_arg) {}

  // Remove warnings: 'inherits ... from ... via dominance'
  bool format_body(Opt_trace_context *json, Opt_trace_object *obj) override {
    return table_base_ctx::format_body(json, obj);
  }
  size_t id(bool hide) override {
    return table_with_where_and_derived::id(hide);
  }
  bool cacheable() override { return table_base_ctx::cacheable(); }
  bool dependent() override { return table_base_ctx::dependent(); }
  qep_row *entry() override { return table_base_ctx::entry(); }
  bool format_derived(Opt_trace_context *json) override {
    return table_with_where_and_derived::format_derived(json);
  }
  bool format_where(Opt_trace_context *json) override {
    return table_with_where_and_derived::format_where(json);
  }

  void register_where_subquery(Query_expression *subquery) override {
    List_iterator<Query_expression> it(where_subquery_units);
    Query_expression *u;
    while ((u = it++)) {
      /*
        The server may transform (x = (SELECT FROM DUAL)) to
        (x <=> (SELECT FROM DUAL) AND x = (SELECT FROM DUAL)),
        so ignore duplicates:
      */
      if (u == subquery) return;
    }
    where_subquery_units.push_back(subquery);
  }

  int add_where_subquery(subquery_ctx *ctx,
                         Query_expression *subquery) override {
    List_iterator<Query_expression> it(where_subquery_units);
    Query_expression *u;
    while ((u = it++)) {
      if (u == subquery) return where_subqueries.push_back(ctx);
    }
    return -1;
  }

  bool find_and_set_derived(context *subquery) override {
    if (query_block_id == subquery->id()) {
      derived_from.push_back(subquery);
      return true;
    }
    return false;
  }
  enum_mod_type get_mod_type() override { return entry()->mod_type; }
};

/**
  Base class for CTX_ORDER_BY, CTX_GROUP_BY and node class for CTX_DISTINCT

  This class represents context for simple ORDER BY/GROUP BY/DISTINCT clauses
  (the clause is effective for the single JOIN_TAB).
*/

class simple_sort_ctx : public joinable_ctx {
 protected:
  /** Single JOIN_TAB that we sort. */
  joinable_ctx *join_tab;

 private:
  /** True if the clause creates intermediate table. */
  const bool using_tmptable;
  /** True if the clause uses filesort. */
  const bool using_filesort;

 public:
  simple_sort_ctx(enum_parsing_context type_arg, const char *name_arg,
                  context *parent_arg, const Explain_format_flags *flags,
                  Explain_sort_clause clause)
      : context(type_arg, name_arg, parent_arg),
        joinable_ctx(type_arg, name_arg, parent_arg),
        join_tab(nullptr),
        using_tmptable(flags->get(clause, ESP_USING_TMPTABLE)),
        using_filesort(flags->get(clause, ESP_USING_FILESORT)) {}

  bool add_join_tab(joinable_ctx *ctx) override {
    join_tab = ctx;
    return false;
  }

  int add_where_subquery(subquery_ctx *ctx,
                         Query_expression *subquery) override {
    return join_tab->add_where_subquery(ctx, subquery);
  }

  bool find_and_set_derived(context *subquery) override {
    return join_tab->find_and_set_derived(subquery);
  }

  size_t id(bool hide) override { return join_tab->id(hide); }
  bool cacheable() override { return join_tab->cacheable(); }
  bool dependent() override { return join_tab->dependent(); }

 protected:
  bool format_body(Opt_trace_context *json, Opt_trace_object *obj) override {
    if (using_tmptable) obj->add(K_USING_TMP_TABLE, true);
    obj->add(K_USING_FILESORT, using_filesort);
    return join_tab->format(json);
  }
};

/**
  Node class for "simple" CTX_ORDER_BY and CTX_GROUP_BY

  This class represents context for simple ORDER BY or GROUP BY clauses
  (the clause is effective for the single JOIN_TAB).
*/

class simple_sort_with_subqueries_ctx : public simple_sort_ctx {
  /** Type of this clause subqueries. */
  const subquery_list_enum subquery_type;
  List<subquery_ctx> subqueries;

 public:
  simple_sort_with_subqueries_ctx(enum_parsing_context type_arg,
                                  const char *name_arg, context *parent_arg,
                                  subquery_list_enum subquery_type_arg,
                                  const Explain_format_flags *flags,
                                  Explain_sort_clause clause)
      : context(type_arg, name_arg, parent_arg),
        simple_sort_ctx(type_arg, name_arg, parent_arg, flags, clause),
        subquery_type(subquery_type_arg) {}

  bool add_subquery(subquery_list_enum subquery_type_arg,
                    subquery_ctx *ctx) override {
    if (subquery_type != subquery_type_arg)
      return simple_sort_ctx::add_subquery(subquery_type_arg, ctx);
    return subqueries.push_back(ctx);
  }

 private:
  bool format_body(Opt_trace_context *json, Opt_trace_object *obj) override {
    return (simple_sort_ctx::format_body(json, obj) ||
            (format_list(json, subqueries, list_names[subquery_type])));
  }
};

/**
  Node class for the CTX_JOIN context
*/

class join_ctx : public unit_ctx, virtual public qep_row {
 protected:
  List<joinable_ctx> join_tabs;  ///< hosted JOIN_TAB nodes
  sort_ctx *sort;
  window_ctx *window;

 public:
  join_ctx(enum_parsing_context type_arg, const char *name_arg,
           context *parent_arg)
      : context(type_arg, name_arg, parent_arg),
        unit_ctx(type_arg, name_arg, parent_arg),
        sort(nullptr),
        window(nullptr) {}

  bool add_join_tab(joinable_ctx *ctx) override {
    return join_tabs.push_back(ctx);
  }

  void set_sort(sort_ctx *ctx) override {
    assert(!sort);
    sort = ctx;
  }

  void set_window(window_ctx *ctx) override {
    assert(!sort);
    window = ctx;
  }

  qep_row *entry() override { return this; }

  /**
    Associate a CTX_DERIVED node with its CTX_QEP_TAB node

    @param subquery     derived subquery tree
  */
  bool find_and_set_derived(context *subquery) override;

  bool add_subquery(subquery_list_enum subquery_type,
                    subquery_ctx *ctx) override;

 protected:
  bool format_body(Opt_trace_context *json, Opt_trace_object *obj) override;
  bool format_body_inner(Opt_trace_context *json, Opt_trace_object *obj);
  virtual const char *get_cost_tag() { return K_QUERY_COST; }

 public:
  bool format_nested_loop(Opt_trace_context *json) override;
  size_t id(bool hide) override;
  bool cacheable() override;
  bool dependent() override;
  int add_where_subquery(subquery_ctx *ctx,
                         Query_expression *subquery) override;
};

/**
  Node class for CTX_SIMPLE_ORDER_BY, CTX_SIMPLE_GROUP_BY and
CTX_SIMPLE_DISTINCT

  CTX_JOIN context (see join_ctx class) may contain nested loop join node *or*
  ORDER BY/GROUP BY/DISTINCT node that is represented by this class:

    join: { nested_loop: [ ... ] }
  or
    join: { order_by|group_by|distinct : { ... } }

  CTX_ORDER_BY may contain nested loop join tree *or* GROUP BY/DISTINCT node:

    order_by: { nested_loop|group_by|distinct: ... }

  CTX_DISTINCT context structure:

    distinct: { nested_loop|group_by: ... }

  CTX_GROUP_BY:

    group_by: { nested_loop: [ ... ] }

  I.e. the most complex CTX_JOIN may have such a structure of JSON output as:

    join: {
      order_by: {
        distinct: {
          group_by: {
            nested_loop: [ ... ]
          }
        }
      }
    }
TODO
*/

class sort_ctx : public join_ctx {
  /** The clause creates temporary table. */
  const bool using_tmptable;
  /** The clause uses filesort. */
  const bool using_filesort;

 public:
  sort_ctx(enum_parsing_context type_arg, const char *name_arg,
           context *parent_arg, const Explain_format_flags *flags,
           Explain_sort_clause clause)
      : context(type_arg, name_arg, parent_arg),
        join_ctx(type_arg, name_arg, parent_arg),
        using_tmptable(flags->get(clause, ESP_USING_TMPTABLE)),
        using_filesort(flags->get(clause, ESP_USING_FILESORT)) {}

 protected:
  bool format_body(Opt_trace_context *json, Opt_trace_object *obj) override {
    assert(!sort || join_tabs.is_empty());

    if (using_tmptable) obj->add(K_USING_TMP_TABLE, true);
    if (type != CTX_BUFFER_RESULT) obj->add(K_USING_FILESORT, using_filesort);

    return join_ctx::format_body(json, obj);
  }
  const char *get_cost_tag() override { return K_SORT_COST; }
};

class sort_with_subqueries_ctx : public sort_ctx {
  /** Subquery type for this clause. */
  const subquery_list_enum subquery_type;
  List<subquery_ctx> subqueries;

 public:
  sort_with_subqueries_ctx(enum_parsing_context type_arg, const char *name_arg,
                           context *parent_arg,
                           subquery_list_enum subquery_type_arg,
                           const Explain_format_flags *flags,
                           Explain_sort_clause clause)
      : context(type_arg, name_arg, parent_arg),
        sort_ctx(type_arg, name_arg, parent_arg, flags, clause),
        subquery_type(subquery_type_arg) {}

  bool add_subquery(subquery_list_enum subquery_type_arg,
                    subquery_ctx *ctx) override {
    if (subquery_type_arg != subquery_type)
      return sort_ctx::add_subquery(subquery_type_arg, ctx);
    return subqueries.push_back(ctx);
  }

 private:
  bool format_body(Opt_trace_context *json, Opt_trace_object *obj) override {
    return (sort_ctx::format_body(json, obj) ||
            format_list(json, subqueries, list_names[subquery_type]));
  }
};

/**
  When the query has window functions, an outer node named "windowing" is
  added:
    query_block: {
      select_id: 1,
      windowing: {
        windows: [ ... list of all windows' details ...
        ],
        nested_loop: [ ... ]

  Due to this "outer-node" layout, the implementation of window_ctx is similar
  to that of sort_ctx. Except that while it makes sense to describe to the
  user the "subqueries included in ORDER BY", it doesn't for "subqueries
  included in the window function's arguments" (the window function belongs to
  the SELECT list or ORDER BY: so does the subquery, simply), so there is no
  window_with_subqueries_ctx.
*/
class window_ctx : public join_ctx {
 public:
  window_ctx(context *parent_arg)
      : context(CTX_WINDOW, K_WINDOWING, parent_arg),
        join_ctx(CTX_WINDOW, K_WINDOWING, parent_arg) {}

 private:
  bool format_body(Opt_trace_context *json, Opt_trace_object *obj) override {
    Opt_trace_array windows(json, K_WINDOWS);
    List_iterator<Window> li(*m_windows);
    Window *w;

    while ((w = li++)) {
      Opt_trace_object to(json);
      to.add_utf8(K_NAME, w->printable_name());
      if (m_windows->elements > 1) {
        // Help the user relate this to a window in his query:
        if (w->def_pos())
          to.add(K_WINDOW_DEF_POS, w->def_pos());
        else
          to.add_alnum(K_WINDOW_DEF_POS, "artificial_window");  // dummy
        // Make him notice the top-to-bottom order of execution of windows:
        if (w->is_last()) to.add(K_WINDOW_LAST_EXECUTED, true);
      }
      if (!w->short_circuit()) to.add(K_USING_TMP_TABLE, true);
      if (w->needs_sorting()) {
        obj->add(K_USING_FILESORT, true);
        Opt_trace_array sort_order(json, K_FILESORT_KEY);
        ORDER *ord = w->sorting_order();
        for (; ord != nullptr; ord = ord->next) {
          String str;
          (*ord->item)
              ->print_for_order(current_thd, &str,
                                (enum_query_type)(QT_NO_DB | QT_NO_TABLE),
                                ord->used_alias);
          if (ord->direction == ORDER_DESC)
            str.append(STRING_WITH_LEN(" desc"));
          sort_order.add_utf8(str.ptr(), str.length());
        }
      }
      if (w->needs_buffering()) {
        Opt_trace_object to_buf(json, K_FRAME_BUFFER);
        to_buf.add(K_USING_TMP_TABLE, true);
        if (w->optimizable_range_aggregates() ||
            w->optimizable_row_aggregates() || w->static_aggregates())
          to_buf.add(K_OPTIMIZED_FRAME_EVALUATION, true);
      }
      Opt_trace_array wfs(json, K_FUNCTIONS);
      List_iterator<Item_sum> wfs_it(w->functions());
      Item_sum *wf;
      while ((wf = wfs_it++)) wfs.add_utf8(wf->func_name());
    }

    windows.end();
    return join_ctx::format_body(json, obj);
  }

 protected:
  const char *get_cost_tag() override { return K_SORT_COST; }
};

bool join_ctx::find_and_set_derived(context *subquery) {
  assert(subquery->id() != 0);

  if (sort) return sort->find_and_set_derived(subquery);
  if (window) return window->find_and_set_derived(subquery);

  List_iterator<joinable_ctx> it(join_tabs);
  joinable_ctx *t;
  while ((t = it++)) {
    if (t->find_and_set_derived(subquery)) return true;
  }
  return false;
}

bool join_ctx::add_subquery(subquery_list_enum subquery_type,
                            subquery_ctx *ctx) {
  if (sort) return sort->add_subquery(subquery_type, ctx);
  if (window) return window->add_subquery(subquery_type, ctx);

  if (subquery_type > SQ_toplevel) {
    List_iterator<joinable_ctx> it(join_tabs);
    joinable_ctx *j;
    while ((j = it++)) {
      switch (j->type) {
        case CTX_ORDER_BY:
        case CTX_DISTINCT:
        case CTX_GROUP_BY:
        case CTX_SIMPLE_ORDER_BY:
        case CTX_SIMPLE_DISTINCT:
        case CTX_SIMPLE_GROUP_BY:
          return j->add_subquery(subquery_type, ctx);
        case CTX_MESSAGE:  // The 'no plan' case
          assert(subquery_type == SQ_ORDER_BY || subquery_type == SQ_GROUP_BY);
          return unit_ctx::add_subquery(subquery_type, ctx);
        default:
          assert(0); /* purecov: inspected */
      }
    }
  } else
    return unit_ctx::add_subquery(subquery_type, ctx);
  return true; /* purecov: inspected */
}

bool join_ctx::format_body(Opt_trace_context *json, Opt_trace_object *obj) {
  if (type == CTX_JOIN) obj->add(K_SELECT_ID, id(true));

  format_extra(obj);

  if (!col_read_cost.is_empty()) {
    char buf[32];  // 32 is enough for digits of a double

    Opt_trace_object cost_info(json, K_COST_INFO);
    print_cost(buf, sizeof(buf), col_read_cost.value);
    cost_info.add_utf8(get_cost_tag(), buf);
  }
  // Print target table for INSERT/REPLACE SELECT outside of nested loop
  if (join_tabs.elements && (join_tabs.head()->get_mod_type() == MT_INSERT ||
                             join_tabs.head()->get_mod_type() == MT_REPLACE)) {
    join_tabs.head()->format(json);
    if (sort || join_tabs.elements > 1) {
      Opt_trace_object insert_from(json, "insert_from");
      if (format_body_inner(json, obj)) return true; /* purecov: inspected */
    }
  } else if (format_body_inner(json, obj))
    return true; /* purecov: inspected */
  return format_query_expression(json);
}

bool join_ctx::format_body_inner(Opt_trace_context *json,
                                 Opt_trace_object *obj) {
  if (sort) {
    if (sort->format(json)) return true; /* purecov: inspected */
  } else if (window) {
    if (window->format(json)) return true; /* purecov: inspected */
  } else if (join_tabs.elements && join_tabs.head()->type == CTX_MESSAGE) {
    // Could be only 1 message per join
    assert(join_tabs.elements == 1);
    message_ctx *msg = (message_ctx *)join_tabs.head();
    obj->add_alnum(K_MESSAGE, msg->entry()->col_message.str);
    if (msg->derived_from.elements)
      msg->format(json);
    else if (msg->where_subqueries.elements)
      msg->format_where(json);
  } else if (format_nested_loop(json))
    return true;
  return false;
}

bool join_ctx::format_nested_loop(Opt_trace_context *json) {
  List_iterator<joinable_ctx> it(join_tabs);
  uint join_tab_num = join_tabs.elements;
  assert(join_tabs.elements > 0);

  if (join_tabs.head()->get_mod_type() == MT_INSERT ||
      join_tabs.head()->get_mod_type() == MT_REPLACE) {
    it++;
    join_tab_num--;
  }
  /*
    For single table skip "nested_loop" object creation and
    format its contents only (the 1st join_tab).
  */
  if (join_tab_num == 1) return (it++)->format(json);

  Opt_trace_array loops(json, K_NESTED_LOOP);

  joinable_ctx *t;
  while ((t = it++)) {
    Opt_trace_object anonymous_wrapper(json);
    if (t->format(json)) return true;
  }
  return false;
}

bool setop_result_ctx::format_body(Opt_trace_context *json,
                                   Opt_trace_object *obj) {
  obj->add(K_USING_TMP_TABLE, true);

  if (table_base_ctx::format_body(json, obj))
    return true; /* purecov: inspected */

  if (message) {
    auto *msg = (message_ctx *)message;
    obj->add_alnum(K_MESSAGE, msg->entry()->col_message.str);
  }

  Opt_trace_array specs(json, K_QUERY_SPECIFICATIONS);

  List_iterator<context> it(*query_specs);
  context *ctx;
  while ((ctx = it++)) {
    if (ctx->format(json)) return true; /* purecov: inspected */
  }
  return false;
}

/**
  Auxiliary function to walk through the list and propagate "hide" value

  @param list   list of context (*_ctx)  objects
  @param hide   if true, ban the output of K_SELECT_ID JSON property
                in the underlying table_with_where_and_derived_ctx
                and materialize_ctx objects

  @return       id of underlying objects
*/
template <typename T>
static size_t get_id(List<T> &list, bool hide) {
  if (!hide) return list.head()->id();

  List_iterator<T> it(list);
  T *j;
  size_t ret = 0;
  while ((j = it++)) ret = j->id(hide);
  return ret;
}

size_t join_ctx::id(bool hide) {
  return (sort ? sort->id(hide)
               : (window ? window->id(hide) : get_id(join_tabs, hide)));
}

bool join_ctx::cacheable() {
  return (sort
              ? sort->cacheable()
              : (window ? window->cacheable() : join_tabs.head()->cacheable()));
}

bool join_ctx::dependent() {
  return (sort
              ? sort->dependent()
              : (window ? window->dependent() : join_tabs.head()->dependent()));
}

int join_ctx::add_where_subquery(subquery_ctx *ctx,
                                 Query_expression *subquery) {
  if (sort) return sort->join_ctx::add_where_subquery(ctx, subquery);
  if (window) return window->join_ctx::add_where_subquery(ctx, subquery);

  List_iterator<joinable_ctx> it(join_tabs);
  joinable_ctx *j;
  bool found = false;
  while ((j = it++)) {
    int ret = j->add_where_subquery(ctx, subquery);
    if (ret > 0) return true;
    found |= (ret == 0);
  }
  if (!found) return add_subquery(SQ_OPTIMIZED_AWAY, ctx);
  return false;
}

/**
  Context class to group materialized JOIN_TABs to "materialized" array.
  Is used for semijoin materialization.
*/

class materialize_ctx : public joinable_ctx,
                        public join_ctx,
                        public table_base_ctx {
 public:
  explicit materialize_ctx(context *parent_arg)
      : context(CTX_MATERIALIZATION, K_TABLE, parent_arg),
        joinable_ctx(CTX_MATERIALIZATION, K_TABLE, parent_arg),
        join_ctx(CTX_MATERIALIZATION, K_TABLE, parent_arg),
        table_base_ctx(CTX_MATERIALIZATION, K_TABLE, parent_arg) {}

  size_t id(bool hide) override {
    if (hide) {
      is_hidden_id = true;
      /* Set the materizlize table's id to hide */
      join_ctx::id(hide);
    }
    return table_base_ctx::id(hide);
  }
  bool cacheable() override { return join_ctx::cacheable(); }
  bool dependent() override { return join_ctx::dependent(); }

  // Remove warnings: 'inherits ... from ... via dominance'
  qep_row *entry() override { return table_base_ctx::entry(); }
  bool add_subquery(subquery_list_enum subquery_type,
                    subquery_ctx *ctx) override {
    return join_ctx::add_subquery(subquery_type, ctx);
  }
  bool add_join_tab(joinable_ctx *ctx) override {
    return join_ctx::add_join_tab(ctx);
  }
  int add_where_subquery(subquery_ctx *ctx,
                         Query_expression *subquery) override {
    return join_ctx::add_where_subquery(ctx, subquery);
  }
  bool find_and_set_derived(context *subquery) override {
    return join_ctx::find_and_set_derived(subquery);
  }
  bool format_query_expression(Opt_trace_context *json) override {
    return unit_ctx::format_query_expression(json);
  }
  bool format_nested_loop(Opt_trace_context *json) override {
    return join_ctx::format_nested_loop(json);
  }
  void set_sort(sort_ctx *ctx) override { return join_ctx::set_sort(ctx); }
  void set_window(window_ctx *ctx) override {
    return join_ctx::set_window(ctx);
  }

 private:
  bool format_body(Opt_trace_context *json, Opt_trace_object *obj) override {
    assert(!col_join_type.is_empty());

    if (!col_table_name.is_empty())
      obj->add_utf8(K_TABLE_NAME, col_table_name.str);

    obj->add_alnum(K_ACCESS_TYPE, col_join_type.str);

    if (!col_key.is_empty()) obj->add_utf8(K_KEY, col_key.str);

    if (!col_key_len.is_empty()) obj->add_alnum(K_KEY_LENGTH, col_key_len.str);

    add_string_array(json, K_REF, col_ref);

    if (!col_rows.is_empty()) obj->add(K_ROWS, col_rows.value);

    format_extra(obj);

    /*
      Currently K-REF/col_ref is not shown; it would always be "func", since
      {subquery,semijoin} materialization use store_key; using
      get_store_key() instead would allow "const" and outer column's name,
      if applicable.
      The looked up expression can anyway be inferred from the condition:
    */
    if (!col_attached_condition.is_empty())
      obj->add_utf8(K_ATTACHED_CONDITION, col_attached_condition.str);
    if (format_where(json)) return true;

    Opt_trace_object m(json, K_MATERIALIZED_FROM_SUBQUERY);
    obj->add(K_USING_TMP_TABLE, true);
    Opt_trace_object q(json, K_QUERY_BLOCK);
    return format_nested_loop(json);
  }
};

/**
  Context class to represent JOIN_TABs in duplication weedout sequence
*/

class duplication_weedout_ctx : public joinable_ctx, public join_ctx {
 public:
  explicit duplication_weedout_ctx(context *parent_arg)
      : context(CTX_DUPLICATES_WEEDOUT, K_DUPLICATES_REMOVAL, parent_arg),
        joinable_ctx(CTX_DUPLICATES_WEEDOUT, K_DUPLICATES_REMOVAL, parent_arg),
        join_ctx(CTX_DUPLICATES_WEEDOUT, K_DUPLICATES_REMOVAL, parent_arg) {}

  size_t id(bool hide) override { return join_ctx::id(hide); }
  bool cacheable() override { return join_ctx::cacheable(); }
  bool dependent() override { return join_ctx::dependent(); }

  // Remove warnings: 'inherits ... from ... via dominance'
  bool add_join_tab(joinable_ctx *ctx) override {
    return join_ctx::add_join_tab(ctx);
  }
  bool add_subquery(subquery_list_enum subquery_type,
                    subquery_ctx *ctx) override {
    return join_ctx::add_subquery(subquery_type, ctx);
  }
  int add_where_subquery(subquery_ctx *ctx,
                         Query_expression *subquery) override {
    return join_ctx::add_where_subquery(ctx, subquery);
  }
  bool find_and_set_derived(context *subquery) override {
    return join_ctx::find_and_set_derived(subquery);
  }
  bool format_nested_loop(Opt_trace_context *json) override {
    return join_ctx::format_nested_loop(json);
  }
  bool format_query_expression(Opt_trace_context *json) override {
    return unit_ctx::format_query_expression(json);
  }
  void set_sort(sort_ctx *ctx) override { return join_ctx::set_sort(ctx); }
  void set_window(window_ctx *ctx) override {
    return join_ctx::set_window(ctx);
  }
  qep_row *entry() override { return join_ctx::entry(); }

 private:
  bool format_body(Opt_trace_context *json, Opt_trace_object *obj) override {
    obj->add(K_USING_TMP_TABLE, true);
    return format_nested_loop(json);
  }
};

/**
  Node class for UNION, EXCEPT, INTERSECT, UNARY (query expression)
*/

class setop_ctx : public unit_ctx {
  setop_result_ctx *setop_result;  ///< associated e.g. CTX_UNION_RESULT node
  List<context> query_specs;  ///< query specification nodes (inner selects)

 public:
  explicit setop_ctx(context *parent_arg, enum_parsing_context ctx,
                     const char *str)
      : context(ctx, str, parent_arg),
        unit_ctx(ctx, str, parent_arg),
        setop_result(nullptr) {}

 private:
  bool format_body(Opt_trace_context *json, Opt_trace_object *) override {
    if (setop_result)
      return (setop_result->format(json)) || format_query_expression(json);
    else {
      /*
        UNION without temporary table. There is no union_result since
        there is no fake_query_block.
      */
      Opt_trace_object union_res(json, K_UNION_RESULT);
      union_res.add(K_USING_TMP_TABLE, false);
      Opt_trace_array specs(json, K_QUERY_SPECIFICATIONS);
      List_iterator<context> it(query_specs);
      context *ctx;
      while ((ctx = it++)) {
        if (ctx->format(json)) return true; /* purecov: inspected */
      }
      return format_query_expression(json);
    }
  }

 public:
  size_t id(bool hide) override { return get_id(query_specs, hide); }
  bool cacheable() override { return query_specs.head()->cacheable(); }
  bool dependent() override { return query_specs.head()->dependent(); }

  void set_setop_result(setop_result_ctx *ctx) override {
    assert(setop_result == nullptr);
    setop_result = ctx;
    setop_result->push_down_query_specs(&query_specs);
  }
  bool add_query_spec(context *ctx) override {
    return query_specs.push_back(ctx);
  }
};

bool setop_result_ctx::format(Opt_trace_context *json) {
  if (order_by_subqueries.is_empty() && optimized_away_subqueries.is_empty())
    return table_base_ctx::format(json);

  Opt_trace_object order_by(json, K_ORDERING_OPERATION);

  order_by.add(K_USING_FILESORT, !order_by_subqueries.is_empty());

  if (table_base_ctx::format(json)) return true; /* purecov: inspected */

  if (!order_by_subqueries.is_empty() &&
      format_list(json, order_by_subqueries, K_ORDER_BY_SUBQUERIES))
    return true; /* purecov: inspected */

  if (!optimized_away_subqueries.is_empty() &&
      format_list(json, optimized_away_subqueries, K_OPTIMIZED_AWAY_SUBQUERIES))
    return true; /* purecov: inspected */

  return false;
}

}  // namespace opt_explain_json_namespace

qep_row *Explain_format_JSON::entry() { return current_context->entry(); }

bool Explain_format_JSON::begin_context(enum_parsing_context ctx_arg,
                                        Query_expression *subquery,
                                        const Explain_format_flags *flags) {
  using namespace opt_explain_json_namespace;

  context *prev_context = current_context;
  switch (ctx_arg) {
    case CTX_JOIN:
      assert(current_context == nullptr || current_context->type == CTX_UNION ||
             current_context->type == CTX_INTERSECT ||
             current_context->type == CTX_EXCEPT ||
             current_context->type == CTX_UNARY ||
             // subqueries:
             current_context->type == CTX_SELECT_LIST ||
             current_context->type == CTX_UPDATE_VALUE ||
             current_context->type == CTX_INSERT_VALUES ||
             current_context->type == CTX_INSERT_UPDATE ||
             current_context->type == CTX_DERIVED ||
             current_context->type == CTX_OPTIMIZED_AWAY_SUBQUERY ||
             current_context->type == CTX_WHERE ||
             current_context->type == CTX_HAVING ||
             current_context->type == CTX_ORDER_BY_SQ ||
             current_context->type == CTX_GROUP_BY_SQ ||
             current_context->type == CTX_QUERY_SPEC);
      if ((current_context = new (*THR_MALLOC)
               join_ctx(CTX_JOIN, K_QUERY_BLOCK, current_context)) == nullptr)
        return true;
      break;
    case CTX_ORDER_BY: {
      assert(current_context->type == CTX_JOIN);
      sort_ctx *ctx = new (*THR_MALLOC) sort_with_subqueries_ctx(
          CTX_ORDER_BY, K_ORDERING_OPERATION, current_context, SQ_ORDER_BY,
          flags, ESC_ORDER_BY);
      if (ctx == nullptr) return true;
      current_context->set_sort(ctx);
      current_context = ctx;
      break;
    }
    case CTX_GROUP_BY: {
      assert(current_context->type == CTX_JOIN ||
             current_context->type == CTX_ORDER_BY ||
             current_context->type == CTX_DISTINCT ||
             current_context->type == CTX_WINDOW);
      sort_ctx *ctx = new (*THR_MALLOC) sort_with_subqueries_ctx(
          CTX_GROUP_BY, K_GROUPING_OPERATION, current_context, SQ_GROUP_BY,
          flags, ESC_GROUP_BY);
      if (ctx == nullptr) return true;
      current_context->set_sort(ctx);
      current_context = ctx;
      break;
    }
    case CTX_DISTINCT: {
      assert(current_context->type == CTX_JOIN ||
             current_context->type == CTX_ORDER_BY);
      sort_ctx *ctx =
          new (*THR_MALLOC) sort_ctx(CTX_DISTINCT, K_DUPLICATES_REMOVAL,
                                     current_context, flags, ESC_DISTINCT);
      if (ctx == nullptr) return true;
      current_context->set_sort(ctx);
      current_context = ctx;
      break;
    }
    case CTX_BUFFER_RESULT: {
      assert(current_context->type == CTX_JOIN ||
             current_context->type == CTX_ORDER_BY ||
             current_context->type == CTX_DISTINCT ||
             current_context->type == CTX_WINDOW ||
             current_context->type == CTX_GROUP_BY);
      sort_ctx *ctx =
          new (*THR_MALLOC) sort_ctx(CTX_BUFFER_RESULT, K_BUFFER_RESULT,
                                     current_context, flags, ESC_BUFFER_RESULT);
      if (ctx == nullptr) return true;
      current_context->set_sort(ctx);
      current_context = ctx;
      break;
    }
    case CTX_QEP_TAB: {
      assert(current_context->type == CTX_JOIN ||
             current_context->type == CTX_MATERIALIZATION ||
             current_context->type == CTX_DUPLICATES_WEEDOUT ||
             current_context->type == CTX_GROUP_BY ||
             current_context->type == CTX_ORDER_BY ||
             current_context->type == CTX_DISTINCT ||
             current_context->type == CTX_WINDOW ||
             current_context->type == CTX_BUFFER_RESULT ||
             current_context->type == CTX_SIMPLE_GROUP_BY ||
             current_context->type == CTX_SIMPLE_ORDER_BY ||
             current_context->type == CTX_SIMPLE_DISTINCT);
      join_tab_ctx *ctx =
          new (*THR_MALLOC) join_tab_ctx(CTX_QEP_TAB, current_context);
      if (ctx == nullptr || current_context->add_join_tab(ctx)) return true;
      current_context = ctx;
      break;
    }
    case CTX_SIMPLE_ORDER_BY: {
      assert(current_context->type == CTX_JOIN ||
             current_context->type == CTX_MATERIALIZATION ||
             current_context->type == CTX_DUPLICATES_WEEDOUT ||
             current_context->type == CTX_GROUP_BY ||
             current_context->type == CTX_ORDER_BY ||
             current_context->type == CTX_BUFFER_RESULT ||
             current_context->type == CTX_WINDOW ||
             current_context->type == CTX_DISTINCT);
      simple_sort_ctx *ctx = new (*THR_MALLOC) simple_sort_with_subqueries_ctx(
          CTX_SIMPLE_ORDER_BY, K_ORDERING_OPERATION, current_context,
          SQ_ORDER_BY, flags, ESC_ORDER_BY);

      if (ctx == nullptr || current_context->add_join_tab(ctx)) return true;
      current_context = ctx;
      break;
    }
    case CTX_SIMPLE_GROUP_BY: {
      assert(current_context->type == CTX_JOIN ||
             current_context->type == CTX_MATERIALIZATION ||
             current_context->type == CTX_DUPLICATES_WEEDOUT ||
             current_context->type == CTX_GROUP_BY ||
             current_context->type == CTX_ORDER_BY ||
             current_context->type == CTX_DISTINCT ||
             current_context->type == CTX_WINDOW ||
             current_context->type == CTX_BUFFER_RESULT ||
             current_context->type == CTX_SIMPLE_ORDER_BY ||
             current_context->type == CTX_SIMPLE_DISTINCT);
      simple_sort_ctx *ctx = new (*THR_MALLOC) simple_sort_with_subqueries_ctx(
          CTX_SIMPLE_GROUP_BY, K_GROUPING_OPERATION, current_context,
          SQ_GROUP_BY, flags, ESC_GROUP_BY);
      if (ctx == nullptr || current_context->add_join_tab(ctx)) return true;
      current_context = ctx;
      break;
    }
    case CTX_SIMPLE_DISTINCT: {
      assert(current_context->type == CTX_JOIN ||
             current_context->type == CTX_MATERIALIZATION ||
             current_context->type == CTX_DUPLICATES_WEEDOUT ||
             current_context->type == CTX_GROUP_BY ||
             current_context->type == CTX_ORDER_BY ||
             current_context->type == CTX_DISTINCT ||
             current_context->type == CTX_WINDOW ||
             current_context->type == CTX_BUFFER_RESULT ||
             current_context->type == CTX_SIMPLE_ORDER_BY);
      simple_sort_ctx *ctx = new (*THR_MALLOC)
          simple_sort_ctx(CTX_SIMPLE_DISTINCT, K_DUPLICATES_REMOVAL,
                          current_context, flags, ESC_DISTINCT);
      if (ctx == nullptr || current_context->add_join_tab(ctx)) return true;
      current_context = ctx;
      break;
    }
    case CTX_MATERIALIZATION: {
      assert(current_context->type == CTX_JOIN ||
             current_context->type == CTX_GROUP_BY ||
             current_context->type == CTX_ORDER_BY ||
             current_context->type == CTX_DISTINCT ||
             current_context->type == CTX_WINDOW ||
             current_context->type == CTX_BUFFER_RESULT ||
             current_context->type == CTX_DUPLICATES_WEEDOUT);
      materialize_ctx *ctx = new (*THR_MALLOC) materialize_ctx(current_context);
      if (ctx == nullptr || current_context->add_join_tab(ctx)) return true;
      current_context = ctx;
      break;
    }
    case CTX_DUPLICATES_WEEDOUT: {
      assert(current_context->type == CTX_JOIN ||
             current_context->type == CTX_GROUP_BY ||
             current_context->type == CTX_ORDER_BY ||
             current_context->type == CTX_DISTINCT ||
             current_context->type == CTX_WINDOW ||
             current_context->type == CTX_BUFFER_RESULT ||
             current_context->type == CTX_MATERIALIZATION);
      duplication_weedout_ctx *ctx =
          new (*THR_MALLOC) duplication_weedout_ctx(current_context);
      if (ctx == nullptr || current_context->add_join_tab(ctx)) return true;
      current_context = ctx;
      break;
    }
    case CTX_SELECT_LIST: {
      subquery_ctx *ctx = new (*THR_MALLOC)
          subquery_ctx(CTX_SELECT_LIST, nullptr, current_context);
      if (ctx == nullptr || current_context->add_subquery(SQ_SELECT_LIST, ctx))
        return true;
      current_context = ctx;
      break;
    }
    case CTX_UPDATE_VALUE: {
      subquery_ctx *ctx = new (*THR_MALLOC)
          subquery_ctx(CTX_UPDATE_VALUE, nullptr, current_context);
      if (ctx == nullptr || current_context->add_subquery(SQ_UPDATE_VALUE, ctx))
        return true;
      current_context = ctx;
      break;
    }
    case CTX_INSERT_VALUES: {
      subquery_ctx *ctx = new (*THR_MALLOC)
          subquery_ctx(CTX_INSERT_VALUES, nullptr, current_context);
      if (ctx == nullptr ||
          current_context->add_subquery(SQ_INSERT_VALUES, ctx))
        return true;
      current_context = ctx;
      break;
    }
    case CTX_INSERT_UPDATE: {
      subquery_ctx *ctx = new (*THR_MALLOC)
          subquery_ctx(CTX_INSERT_UPDATE, nullptr, current_context);
      if (ctx == nullptr ||
          current_context->add_subquery(SQ_INSERT_UPDATE, ctx))
        return true;
      current_context = ctx;
      break;
    }
    case CTX_DERIVED: {
      current_context = new (*THR_MALLOC) subquery_ctx(
          CTX_DERIVED, K_MATERIALIZED_FROM_SUBQUERY, current_context);
      if (current_context == nullptr) return true;
      break;
    }
    case CTX_OPTIMIZED_AWAY_SUBQUERY: {
      subquery_ctx *ctx = new (*THR_MALLOC)
          subquery_ctx(CTX_OPTIMIZED_AWAY_SUBQUERY, nullptr, current_context);
      if (ctx == nullptr ||
          current_context->add_subquery(SQ_OPTIMIZED_AWAY, ctx))
        return true;
      current_context = ctx;
      break;
    }
    case CTX_WHERE: {
      assert(subquery != nullptr);
      subquery_ctx *ctx =
          new (*THR_MALLOC) subquery_ctx(CTX_WHERE, nullptr, current_context);
      if (ctx == nullptr || current_context->add_where_subquery(ctx, subquery))
        return true;
      current_context = ctx;
      break;
    }
    case CTX_HAVING: {
      subquery_ctx *ctx =
          new (*THR_MALLOC) subquery_ctx(CTX_HAVING, nullptr, current_context);
      if (ctx == nullptr || current_context->add_subquery(SQ_HAVING, ctx))
        return true;
      current_context = ctx;
      break;
    }
    case CTX_ORDER_BY_SQ: {
      subquery_ctx *ctx = new (*THR_MALLOC)
          subquery_ctx(CTX_ORDER_BY_SQ, nullptr, current_context);
      if (ctx == nullptr || current_context->add_subquery(SQ_ORDER_BY, ctx))
        return true;
      current_context = ctx;
      break;
    }
    case CTX_GROUP_BY_SQ: {
      subquery_ctx *ctx = new (*THR_MALLOC)
          subquery_ctx(CTX_GROUP_BY_SQ, nullptr, current_context);
      if (ctx == nullptr || current_context->add_subquery(SQ_GROUP_BY, ctx))
        return true;
      current_context = ctx;
      break;
    }
    case CTX_UNION: {
      assert(current_context == nullptr || current_context->type == CTX_UNION ||
             current_context->type == CTX_INTERSECT ||
             current_context->type == CTX_EXCEPT ||
             current_context->type == CTX_UNARY ||
             // subqueries:
             current_context->type == CTX_SELECT_LIST ||
             current_context->type == CTX_UPDATE_VALUE ||
             current_context->type == CTX_INSERT_VALUES ||
             current_context->type == CTX_DERIVED ||
             current_context->type == CTX_OPTIMIZED_AWAY_SUBQUERY ||
             current_context->type == CTX_WHERE ||
             current_context->type == CTX_HAVING ||
             current_context->type == CTX_ORDER_BY_SQ ||
             current_context->type == CTX_GROUP_BY_SQ ||
             current_context->type == CTX_QUERY_SPEC);
      setop_ctx *ctx = new (*THR_MALLOC)
          setop_ctx(current_context, CTX_UNION, K_QUERY_BLOCK);
      if (ctx == nullptr) return true;
      if (current_context != nullptr) current_context->add_query_spec(ctx);
      current_context = ctx;
      break;
    }
    case CTX_INTERSECT: {
      setop_ctx *ctx = new (*THR_MALLOC)
          setop_ctx(current_context, CTX_INTERSECT, K_QUERY_BLOCK);
      if (ctx == nullptr) return true;
      if (current_context != nullptr) current_context->add_query_spec(ctx);
      current_context = ctx;
      break;
    }
    case CTX_EXCEPT: {
      setop_ctx *ctx = new (*THR_MALLOC)
          setop_ctx(current_context, CTX_EXCEPT, K_QUERY_BLOCK);
      if (ctx == nullptr) return true;
      if (current_context != nullptr) current_context->add_query_spec(ctx);
      current_context = ctx;
      break;
    }
    case CTX_UNARY: {
      setop_ctx *ctx = new (*THR_MALLOC)
          setop_ctx(current_context, CTX_UNARY, K_QUERY_BLOCK);
      if (ctx == nullptr) return true;
      if (current_context != nullptr) current_context->add_query_spec(ctx);
      current_context = ctx;
      break;
    }
    case CTX_UNION_RESULT: {
      setop_result_ctx *ctx = new (*THR_MALLOC)
          setop_result_ctx(current_context, CTX_UNION_RESULT, K_UNION_RESULT);
      if (ctx == nullptr) return true;
      current_context->set_setop_result(ctx);
      current_context = ctx;
      break;
    }
    case CTX_INTERSECT_RESULT: {
      setop_result_ctx *ctx = new (*THR_MALLOC) setop_result_ctx(
          current_context, CTX_INTERSECT_RESULT, K_INTERSECT_RESULT);
      if (ctx == nullptr) return true;
      current_context->set_setop_result(ctx);
      current_context = ctx;
      break;
    }
    case CTX_EXCEPT_RESULT: {
      setop_result_ctx *ctx = new (*THR_MALLOC)
          setop_result_ctx(current_context, CTX_EXCEPT_RESULT, K_EXCEPT_RESULT);
      if (ctx == nullptr) return true;
      current_context->set_setop_result(ctx);
      current_context = ctx;
      break;
    }
    case CTX_UNARY_RESULT: {
      setop_result_ctx *ctx = new (*THR_MALLOC)
          setop_result_ctx(current_context, CTX_UNARY_RESULT, K_UNARY_RESULT);
      if (ctx == nullptr) return true;
      current_context->set_setop_result(ctx);
      current_context = ctx;
      break;
    }
    case CTX_QUERY_SPEC: {
      subquery_ctx *ctx = new (*THR_MALLOC)
          subquery_ctx(CTX_QUERY_SPEC, nullptr, current_context);
      if (ctx == nullptr || current_context->add_query_spec(ctx)) return true;
      current_context = ctx;
      break;
    }
    case CTX_MESSAGE: {
      /*
        Like CTX_QEP_TAB:
      */
      assert(current_context->type == CTX_JOIN ||
             current_context->type == CTX_MATERIALIZATION ||
             current_context->type == CTX_DUPLICATES_WEEDOUT ||
             current_context->type == CTX_GROUP_BY ||
             current_context->type == CTX_ORDER_BY ||
             current_context->type == CTX_DISTINCT ||
             current_context->type == CTX_WINDOW ||
             current_context->type == CTX_BUFFER_RESULT ||
             current_context->type == CTX_SIMPLE_GROUP_BY ||
             current_context->type == CTX_SIMPLE_ORDER_BY ||
             current_context->type == CTX_SIMPLE_DISTINCT ||
             current_context->type == CTX_UNION_RESULT ||
             current_context->type == CTX_INTERSECT_RESULT ||
             current_context->type == CTX_EXCEPT_RESULT);
      joinable_ctx *ctx = new (*THR_MALLOC) message_ctx(current_context);
      if (ctx == nullptr || current_context->add_join_tab(ctx)) return true;
      current_context = ctx;
      break;
    }
    case CTX_WINDOW: {
      window_ctx *ctx = new (*THR_MALLOC) window_ctx(current_context);
      if (ctx == nullptr) return true;
      current_context->set_window(ctx);
      current_context = ctx;
      break;
    }
    default:
      assert(!"Unknown EXPLAIN context!");
      return true;
  }

  if (prev_context) prev_context->set_child(current_context);

  return false;
}

bool Explain_format_JSON::end_context(enum_parsing_context ctx) {
  assert(current_context->type == ctx);

  bool ret = false;
  if (current_context->parent == nullptr) {
    Item *item;
    Opt_trace_context json;
    const size_t max_size = ULONG_MAX;
    if (json.start(true,   // support_I_S (enable JSON generation)
                   false,  // support_dbug_or_missing_priv
                   current_thd->variables.end_markers_in_json,  // end_marker
                   false,                                       // one_line
                   0,                                           // offset
                   1,                                           // limit
                   max_size,                                    // max_mem_size
                   Opt_trace_context::MISC))
      return true;

    {
      Opt_trace_object braces(&json);

      if (current_context->format(&json)) return true;
    }
    json.end();

    Opt_trace_iterator it(&json);
    if (!it.at_end()) {
      Opt_trace_info info;
      it.get_value(&info);
      item =
          new Item_string(info.trace_ptr, static_cast<uint>(info.trace_length),
                          system_charset_info);
    } else
      item = new Item_null();

    mem_root_deque<Item *> field_list(current_thd->mem_root);
    field_list.push_back(item);
    ret = (item == nullptr || output->send_data(current_thd, field_list));
  } else if (ctx == CTX_DERIVED) {
    if (!current_context->parent->find_and_set_derived(current_context)) {
      assert(!"No derived table found!");
      return true;
    }
  }

  current_context = current_context->parent;
  return ret;
}

bool Explain_format_JSON::send_headers(Query_result *result) {
  if (Explain_format::send_headers(result)) return true;

  mem_root_deque<Item *> field_list(current_thd->mem_root);
  Item *item = new Item_empty_string("EXPLAIN", 78, system_charset_info);
  if (item == nullptr) return true;
  field_list.push_back(item);
  return result->send_result_set_metadata(
      current_thd, field_list, Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
}

void qep_row::format_extra(Opt_trace_object *obj) {
  List_iterator<qep_row::extra> it(col_extra);
  qep_row::extra *e;
  while ((e = it++)) {
    assert(json_extra_tags[e->tag] != nullptr);
    if (e->data)
      obj->add_utf8(json_extra_tags[e->tag], e->data);
    else
      obj->add(json_extra_tags[e->tag], true);
  }
}

/* Convert Json object to string */
std::string Explain_format_JSON::ExplainJsonToString(Json_object *json) {
  // Serialize the JSON object to a string.
  Json_wrapper wrapper(json, /*alias=*/true);
  StringBuffer<STRING_BUFFER_USUAL_SIZE> explain;
  if (wrapper.to_pretty_string(&explain, "ExplainJsonToString()",
                               JsonDocumentDefaultDepthHandler)) {
    return "";
  }
  return {explain.ptr(), explain.length()};
}