summaryrefslogtreecommitdiff
path: root/sql/opt_qpf.cc
blob: 37a01a4f832c31c88351425e94107daff0a7b5e9 (plain)
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
/*
   TODO MP AB copyright
*/

#ifdef USE_PRAGMA_IMPLEMENTATION
#pragma implementation				// gcc: Class implementation
#endif

#include "sql_priv.h"
#include "sql_select.h"


QPF_query::QPF_query()
{
  upd_del_plan= NULL;
  operations= 0;
  //memset(&unions, 0, sizeof(unions));
  //memset(&selects, 0, sizeof(selects));
}


QPF_query::~QPF_query()
{
  delete upd_del_plan;
  uint i;
  for (i= 0 ; i < unions.elements(); i++)
    delete unions.at(i);
  for (i= 0 ; i < selects.elements(); i++)
    delete selects.at(i);
}


QPF_node *QPF_query::get_node(uint select_id)
{
  QPF_union *u;
  if ((u= get_union(select_id)))
    return u;
  else
    return get_select(select_id);
}

QPF_union *QPF_query::get_union(uint select_id)
{
  return (unions.elements() > select_id) ? unions.at(select_id) : NULL;
}

QPF_select *QPF_query::get_select(uint select_id)
{
  return (selects.elements() > select_id) ? selects.at(select_id) : NULL;
}


void QPF_query::add_node(QPF_node *node)
{
  operations++;
  if (node->get_type() == QPF_node::QPF_UNION)
  {
    QPF_union *u= (QPF_union*)node;
    uint select_id= u->get_select_id();
    if (unions.elements() <= select_id)
      unions.resize(max(select_id+1, unions.elements()*2), NULL);

    QPF_union *old_node;
    if ((old_node= get_union(select_id)))
      delete old_node;

    unions.at(select_id)= u;
  }
  else
  {
    QPF_select *sel= (QPF_select*)node;
    if (sel->select_id == (int)UINT_MAX)
    {
      //TODO this is a "fake select" from a UNION.
      DBUG_ASSERT(0);
    }
    else
    {
      uint select_id= sel->select_id;
      QPF_select *old_node;
      //DBUG_ASSERT(!get_select(select_id));
      if (selects.elements() <= select_id)
        selects.resize(max(select_id+1, selects.elements()*2), NULL);

      if ((old_node= get_select(select_id)))
        delete old_node;

      selects.at(select_id)= sel;
    }
  }
}


/*
  The main entry point to print EXPLAIN of the entire query
*/

int QPF_query::print_explain(select_result_sink *output, 
                             uint8 explain_flags)
{
  if (upd_del_plan)
  {
    upd_del_plan->print_explain(this, output, explain_flags);
    return 0;
  }
  else
  {
    // Start with id=1
    QPF_node *node= get_node(1);
    return node->print_explain(this, output, explain_flags);
  }
}


void QPF_union::push_table_name(List<Item> *item_list)
{
}


static void push_str(List<Item> *item_list, const char *str)
{
  item_list->push_back(new Item_string(str,
                                      strlen(str), system_charset_info));
}


static void push_string(List<Item> *item_list, String *str)
{
  item_list->push_back(new Item_string(str->ptr(), str->length(),
                       system_charset_info));
}


int QPF_union::print_explain(QPF_query *query, select_result_sink *output, 
                             uint8 explain_flags)
{
  // print all children, in order
  for (int i= 0; i < (int) union_members.elements(); i++)
  {
    QPF_select *sel= query->get_select(union_members.at(i));
    sel->print_explain(query, output, explain_flags);
  }

  /* Print a line with "UNION RESULT" */
  List<Item> item_list;
  Item *item_null= new Item_null();

  /* `id` column */
  item_list.push_back(item_null);

  /* `select_type` column */
  push_str(&item_list, fake_select_type);

  /* `table` column: something like "<union1,2>" */
  //
  {
    char table_name_buffer[SAFE_NAME_LEN];
    uint childno= 0;
    uint len= 6, lastop= 0;
    memcpy(table_name_buffer, STRING_WITH_LEN("<union"));

    for (; childno < union_members.elements() && len + lastop + 5 < NAME_LEN;
         childno++)
    {
      len+= lastop;
      lastop= my_snprintf(table_name_buffer + len, NAME_LEN - len,
                          "%u,", union_members.at(childno));
    }

    if (childno < union_members.elements() || len + lastop >= NAME_LEN)
    {
      memcpy(table_name_buffer + len, STRING_WITH_LEN("...>") + 1);
      len+= 4;
    }
    else
    {
      len+= lastop;
      table_name_buffer[len - 1]= '>';  // change ',' to '>'
    }
    const CHARSET_INFO *cs= system_charset_info;
    item_list.push_back(new Item_string(table_name_buffer, len, cs));
  }
  //
  push_table_name(&item_list);
  
  /* `partitions` column */
  if (explain_flags & DESCRIBE_PARTITIONS)
    item_list.push_back(item_null);

  /* `type` column */
  push_str(&item_list, join_type_str[JT_ALL]);

  /* `possible_keys` column */
  item_list.push_back(item_null);

  /* `key` */
  item_list.push_back(item_null);

  /* `key_len` */
  item_list.push_back(item_null);

  /* `ref` */
  item_list.push_back(item_null);
 
  /* `rows` */
  item_list.push_back(item_null);

  /* `filtered` */
  if (explain_flags & DESCRIBE_EXTENDED)
    item_list.push_back(item_null);

  /* `Extra` */
  StringBuffer<256> extra_buf;
  if (using_filesort)
  {
    extra_buf.append(STRING_WITH_LEN("Using filesort"));
  }
  const CHARSET_INFO *cs= system_charset_info;
  item_list.push_back(new Item_string(extra_buf.ptr(), extra_buf.length(), cs));

  //output->unit.offset_limit_cnt= 0; 
  if (output->send_data(item_list))
    return 1;

  return print_explain_for_children(query, output, explain_flags);
}


int QPF_node::print_explain_for_children(QPF_query *query, 
                                         select_result_sink *output,
                                         uint8 explain_flags)
{
  for (int i= 0; i < (int) children.elements(); i++)
  {
    QPF_node *node= query->get_node(children.at(i));
    if (node->print_explain(query, output, explain_flags))
      return 1;
  }
  return 0;
}


QPF_select::~QPF_select()
{
  if (join_tabs)
  {
    for (uint i= 0; i< n_join_tabs; i++)
      delete join_tabs[i];
    my_free(join_tabs);
  }
} 


int QPF_select::print_explain(QPF_query *query, select_result_sink *output,
                              uint8 explain_flags)
{
  //output->unit.offset_limit_cnt= 0; 

  if (message)
  {
    List<Item> item_list;
    const CHARSET_INFO *cs= system_charset_info;
    Item *item_null= new Item_null();

    item_list.push_back(new Item_int((int32) select_id));
    item_list.push_back(new Item_string(select_type,
                                        strlen(select_type), cs));
    for (uint i=0 ; i < 7; i++)
      item_list.push_back(item_null);
    if (explain_flags & DESCRIBE_PARTITIONS)
      item_list.push_back(item_null);
    if (explain_flags & DESCRIBE_EXTENDED)
      item_list.push_back(item_null);

    item_list.push_back(new Item_string(message,strlen(message),cs));

    if (output->send_data(item_list))
      return 1;
  }
  else
  {
    bool using_tmp= using_temporary;
    bool using_fs= using_filesort;
    for (uint i=0; i< n_join_tabs; i++)
    {
      join_tabs[i]->print_explain(output, explain_flags, select_id,
                                  select_type, using_tmp, using_fs);
      if (i == 0)
      {
        /* 
          "Using temporary; Using filesort" should only be shown near the 1st
          table
        */
        using_tmp= false;
        using_fs= false;
      }
    }
  }

  return print_explain_for_children(query, output, explain_flags);
}


int QPF_table_access::print_explain(select_result_sink *output, uint8 explain_flags, 
                                    uint select_id, const char *select_type,
                                    bool using_temporary, bool using_filesort)
{
  List<Item> item_list;
  Item *item_null= new Item_null();
  //const CHARSET_INFO *cs= system_charset_info;
  
  if (sjm_nest_select_id)
    select_id= sjm_nest_select_id;

  /* `id` column */
  item_list.push_back(new Item_int((int32) select_id));

  /* `select_type` column */
  if (sjm_nest_select_id)
    push_str(&item_list, "MATERIALIZED");
  else
    push_str(&item_list, select_type);

  /* `table` column */
  push_string(&item_list, &table_name);
  
  /* `partitions` column */
  if (explain_flags & DESCRIBE_PARTITIONS)
  {
    if (used_partitions_set)
    {
      push_string(&item_list, &used_partitions);
    }
    else
      item_list.push_back(item_null); 
  }

  /* `type` column */
  push_str(&item_list, join_type_str[type]);

  /* `possible_keys` column */
  //push_str(item_list, "TODO");
  if (possible_keys_str.length() > 0)
    push_string(&item_list, &possible_keys_str);
  else
    item_list.push_back(item_null); 

  /* `key` */
  if (key_set)
    push_string(&item_list, &key);
  else
    item_list.push_back(item_null); 

  /* `key_len` */
  if (key_len_set)
    push_string(&item_list, &key_len);
  else
    item_list.push_back(item_null);

  /* `ref` */
  if (ref_set)
    push_string(&item_list, &ref);
  else
    item_list.push_back(item_null);
 
  /* `rows` */
  if (rows_set)
  {
    item_list.push_back(new Item_int((longlong) (ulonglong) rows, 
                         MY_INT64_NUM_DECIMAL_DIGITS));
  }
  else
    item_list.push_back(item_null);

  /* `filtered` */
  if (explain_flags & DESCRIBE_EXTENDED)
  {
    if (filtered_set)
    {
      item_list.push_back(new Item_float(filtered, 2));
    }
    else
      item_list.push_back(item_null);
  }

  /* `Extra` */
  StringBuffer<256> extra_buf;
  bool first= true;
  for (int i=0; i < (int)extra_tags.elements(); i++)
  {
    if (first)
      first= false;
    else
      extra_buf.append(STRING_WITH_LEN("; "));
    append_tag_name(&extra_buf, extra_tags.at(i));
  }

  if (using_temporary)
  {
    if (first)
      first= false;
    else
      extra_buf.append(STRING_WITH_LEN("; "));
    extra_buf.append(STRING_WITH_LEN("Using temporary"));
  }

  if (using_filesort)
  {
    if (first)
      first= false;
    else
      extra_buf.append(STRING_WITH_LEN("; "));
    extra_buf.append(STRING_WITH_LEN("Using filesort"));
  }

  const CHARSET_INFO *cs= system_charset_info;
  item_list.push_back(new Item_string(extra_buf.ptr(), extra_buf.length(), cs));

  if (output->send_data(item_list))
    return 1;

  return 0;
}


const char * extra_tag_text[]=
{
  "ET_none",
  "Using index condition",
  "Using index condition(BKA)",
  "Using ", //special
  "Range checked for each record (index map: 0x", //special
  "Using where with pushed condition",
  "Using where",
  "Not exists",
  
  "Using index",
  "Full scan on NULL key",
  "Skip_open_table",
  "Open_frm_only",
  "Open_full_table", 

  "Scanned 0 databases",
  "Scanned 1 database",
  "Scanned all databases",

  "Using index for group-by", // Special?

  "USING MRR: DONT PRINT ME", // Special!

  "Distinct",
  "LooseScan",
  "Start temporary",
  "End temporary",
  "FirstMatch", //TODO: also handle special variant!

  "Using join buffer", // Special!,

  "const row not found",
  "unique row not found",
  "Impossible ON condition"
};


void QPF_table_access::append_tag_name(String *str, enum Extra_tag tag)
{
  switch (tag) {
    case ET_USING:
    {
      // quick select
      str->append(STRING_WITH_LEN("Using "));
      str->append(quick_info);
      break;
    }
    case ET_RANGE_CHECKED_FOR_EACH_RECORD:
    {
      /* 4 bits per 1 hex digit + terminating '\0' */
      char buf[MAX_KEY / 4 + 1];
      str->append(STRING_WITH_LEN("Range checked for each "
                                   "record (index map: 0x"));
      str->append(range_checked_map.print(buf));
      str->append(')');
      break;
    }
    case ET_USING_MRR:
    {
      str->append(mrr_type);
      break;
    }
    case ET_USING_JOIN_BUFFER:
    {
      str->append(extra_tag_text[tag]);
      str->append(join_buffer_type);
      break;
    }
    case ET_FIRST_MATCH:
    {
      if (firstmatch_table_name.length())
      {
        str->append("FirstMatch(");
        str->append(firstmatch_table_name);
        str->append(")");
      }
      else
        str->append(extra_tag_text[tag]);
      break;
    }
    case ET_USING_INDEX_FOR_GROUP_BY:
    {
      str->append(extra_tag_text[tag]);
      str->append(loose_scan_type);
      break;
    }
    default:
     str->append(extra_tag_text[tag]);
  }
}


int QPF_delete::print_explain(QPF_query *query, select_result_sink *output, 
                              uint8 explain_flags)
{
  if (deleting_all_rows)
  {
    const char *msg= "Deleting all rows";
    int res= print_explain_message_line(output, explain_flags,
                                        1 /*select number*/,
                                        select_type, msg);
    return res;

  }
  else
  {
    return QPF_update::print_explain(query, output, explain_flags);
  }
}


int QPF_update::print_explain(QPF_query *query, select_result_sink *output, 
                              uint8 explain_flags)
{
  StringBuffer<64> extra_str;
  if (impossible_where)
  {
    const char *msg= "Impossible where";
    int res= print_explain_message_line(output, explain_flags,
                                        1 /*select number*/,
                                        select_type, msg);
    return res;
  }

  if (using_where)
    extra_str.append(STRING_WITH_LEN("Using where"));

  if (mrr_type.length() != 0)
  {
    if (extra_str.length() !=0)
      extra_str.append(STRING_WITH_LEN("; "));
    extra_str.append(mrr_type);
  }

  if (using_filesort)
  {
    if (extra_str.length() !=0)
      extra_str.append(STRING_WITH_LEN("; "));
    extra_str.append(STRING_WITH_LEN("Using filesort"));
  }

  /* 
    Single-table DELETE commands do not do "Using temporary".
    "Using index condition" is also not possible (which is an unjustified limitation)
  */

  print_explain_row(output, explain_flags, 
                    1, /* id */
                    select_type,
                    table_name.c_ptr(), 
                    // partitions,
                    jtype,
                    possible_keys_line.length()? possible_keys_line.c_ptr(): NULL,
                    key_str.length()? key_str.c_ptr() : NULL,
                    key_len_str.length() ? key_len_str.c_ptr() : NULL,
                    NULL, /* 'ref' is always NULL in single-table EXPLAIN DELETE */
                    rows,
                    extra_str.c_ptr());

  return print_explain_for_children(query, output, explain_flags);
}


void delete_qpf_query(LEX *lex)
{
  delete lex->query_plan_footprint;
  lex->query_plan_footprint= NULL;
}


void create_qpf_query(LEX *lex, MEM_ROOT *mem_root)
{
  DBUG_ASSERT(!lex->query_plan_footprint);
  lex->query_plan_footprint= new QPF_query;
  DBUG_ASSERT(mem_root == current_thd->mem_root);
  lex->query_plan_footprint->mem_root= mem_root;
}