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
|
/* 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_traditional.h"
#include <assert.h>
#include <sys/types.h>
#include <cstddef> // size_t
#include "m_ctype.h"
#include "m_string.h"
#include "my_inttypes.h"
#include "sql/current_thd.h"
#include "sql/item.h"
#include "sql/query_result.h"
#include "sql/sql_class.h"
#include "sql/sql_lex.h"
#include "sql/sql_list.h"
#include "sql_string.h"
/**
Heads of "extra" column parts
This array must be in sync with Extra_tag enum.
*/
static const char *traditional_extra_tags[ET_total] = {
nullptr, // ET_none
"Using temporary", // ET_USING_TEMPORARY
"Using filesort", // ET_USING_FILESORT
"Using index condition", // ET_USING_INDEX_CONDITION
"Using", // ET_USING
"Range checked for each record", // ET_RANGE_CHECKED_FOR_EACH_RECORD
"Using 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
"Start temporary", // ET_START_TEMPORARY
"End temporary", // ET_END_TEMPORARY
"FirstMatch", // ET_FIRST_MATCH
"Materialize", // ET_MATERIALIZE
"Start materialize", // ET_START_MATERIALIZE
"End materialize", // ET_END_MATERIALIZE
"Scan", // 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
"", // ET_PUSHED_JOIN
"Ft_hints:", // ET_FT_HINTS
"Backward index scan", // ET_BACKWARD_SCAN
"Recursive", // ET_RECURSIVE
"Table function:", // ET_TABLE_FUNCTION
"Index dive skipped due to FORCE", // ET_SKIP_RECORDS_IN_RANGE
"Using secondary engine", // ET_USING_SECONDARY_ENGINE
"Rematerialize" // ET_REMATERIALIZE
};
static const char *mod_type_name[] = {"NONE", "INSERT", "UPDATE", "DELETE",
"REPLACE"};
bool Explain_format_traditional::send_headers(Query_result *result) {
return ((nil = new Item_null) == nullptr ||
Explain_format::send_headers(result) ||
current_thd->send_explain_fields(output));
}
static bool push(mem_root_deque<Item *> *items, qep_row::mem_root_str &s,
Item_null *nil) {
if (s.is_empty()) {
items->push_back(nil);
return false;
}
Item_string *item = new Item_string(s.str, s.length, system_charset_info);
if (item == nullptr) return true;
items->push_back(item);
return false;
}
static bool push(mem_root_deque<Item *> *items, const char *s, size_t length) {
Item_string *item = new Item_string(s, length, system_charset_info);
if (item == nullptr) return true;
items->push_back(item);
return false;
}
static bool push(mem_root_deque<Item *> *items, List<const char> &c,
Item_null *nil) {
if (c.is_empty()) {
items->push_back(nil);
return false;
}
StringBuffer<1024> buff;
List_iterator<const char> it(c);
const char *s;
while ((s = it++)) {
buff.append(s);
buff.append(",");
}
if (!buff.is_empty()) buff.length(buff.length() - 1); // remove last ","
Item_string *item = new Item_string(buff.dup(current_thd->mem_root),
buff.length(), system_charset_info);
if (item == nullptr) {
return true;
}
items->push_back(item);
return false;
}
static bool push(mem_root_deque<Item *> *items, const qep_row::column<uint> &c,
Item_null *nil) {
if (c.is_empty()) {
items->push_back(nil);
return false;
}
Item_uint *item = new Item_uint(c.get());
if (item == nullptr) {
return true;
}
items->push_back(item);
return false;
}
static bool push(mem_root_deque<Item *> *items,
const qep_row::column<ulonglong> &c, Item_null *nil) {
if (c.is_empty()) {
items->push_back(nil);
return false;
}
Item_int *item = new Item_int(c.get(), MY_INT64_NUM_DECIMAL_DIGITS);
if (item == nullptr) {
return true;
}
items->push_back(item);
return false;
}
static bool push(mem_root_deque<Item *> *items, const qep_row::column<float> &c,
Item_null *nil) {
if (c.is_empty()) {
items->push_back(nil);
return false;
}
Item_float *item = new Item_float(c.get(), 2);
if (item == nullptr) {
return true;
}
items->push_back(item);
return false;
}
bool Explain_format_traditional::push_select_type(
mem_root_deque<Item *> *items) {
assert(!column_buffer.col_select_type.is_empty());
StringBuffer<32> buff;
if (column_buffer.is_dependent) {
if (buff.append(STRING_WITH_LEN("DEPENDENT "), system_charset_info))
return true;
} else if (!column_buffer.is_cacheable) {
if (buff.append(STRING_WITH_LEN("UNCACHEABLE "), system_charset_info))
return true;
}
const enum_explain_type sel_type = column_buffer.col_select_type.get();
const char *type = (column_buffer.mod_type != MT_NONE &&
(sel_type == enum_explain_type::EXPLAIN_PRIMARY ||
sel_type == enum_explain_type::EXPLAIN_SIMPLE))
? mod_type_name[column_buffer.mod_type]
: Query_block::get_type_str(sel_type);
if (buff.append(type)) return true;
Item_string *item = new Item_string(buff.dup(current_thd->mem_root),
buff.length(), system_charset_info);
if (item == nullptr) {
return true;
}
items->push_back(item);
return false;
}
class Buffer_cleanup {
public:
explicit Buffer_cleanup(qep_row *row) : m_row(row) {}
~Buffer_cleanup() { m_row->cleanup(); }
private:
qep_row *m_row;
};
bool Explain_format_traditional::flush_entry() {
/*
Buffer_cleanup will empty column_buffer upon exit. So column values start
clear for the next row.
*/
Buffer_cleanup bc(&column_buffer);
mem_root_deque<Item *> items(current_thd->mem_root);
if (push(&items, column_buffer.col_id, nil) || push_select_type(&items) ||
push(&items, column_buffer.col_table_name, nil) ||
push(&items, column_buffer.col_partitions, nil) ||
push(&items, column_buffer.col_join_type, nil) ||
push(&items, column_buffer.col_possible_keys, nil) ||
push(&items, column_buffer.col_key, nil) ||
push(&items, column_buffer.col_key_len, nil) ||
push(&items, column_buffer.col_ref, nil) ||
push(&items, column_buffer.col_rows, nil) ||
push(&items, column_buffer.col_filtered, nil))
return true;
if (column_buffer.col_message.is_empty() &&
column_buffer.col_extra.is_empty()) {
items.push_back(nil);
} else if (!column_buffer.col_extra.is_empty()) {
StringBuffer<64> buff(system_charset_info);
List_iterator<qep_row::extra> it(column_buffer.col_extra);
qep_row::extra *e;
while ((e = it++)) {
assert(traditional_extra_tags[e->tag] != nullptr);
if (buff.append(traditional_extra_tags[e->tag])) return true;
if (e->data) {
bool brackets = false;
switch (e->tag) {
case ET_RANGE_CHECKED_FOR_EACH_RECORD:
case ET_USING_INDEX_FOR_GROUP_BY:
case ET_USING_JOIN_BUFFER:
case ET_FIRST_MATCH:
case ET_REMATERIALIZE:
brackets = true; // for backward compatibility
break;
default:
break;
}
if (e->tag != ET_FIRST_MATCH && // for backward compatibility
e->tag != ET_PUSHED_JOIN && buff.append(" "))
return true;
if (brackets && buff.append("(")) return true;
if (buff.append(e->data)) return true;
if (brackets && buff.append(")")) return true;
}
if (buff.append("; ")) return true;
}
if (!buff.is_empty()) buff.length(buff.length() - 2); // remove last "; "
if (push(&items, buff.dup(current_thd->mem_root), buff.length()))
return true;
} else {
if (push(&items, column_buffer.col_message, nil)) return true;
}
if (output->send_data(current_thd, items)) return true;
return false;
}
|