summaryrefslogtreecommitdiff
path: root/sql/opt_split.cc
blob: 0cb76f0391780e4330ffa72dc498155d6eb9d79f (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
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
/*
   Copyright (c) 2017 MariaDB

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

   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 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 */

/*
  This file contains functions to support the splitting technique.
  This optimization technique can be applied to equi-joins involving
  materialized tables such as materialized views, materialized derived tables
  and materialized CTEs. The technique also could be applied to materialized
  semi-joins though the code below does not support this usage yet.

  Here are the main ideas behind this technique that we'll call SM optimization
  (SplitMaterialization).

  Consider the query
    SELECT t1.a, t.min
      FROM t1, (SELECT t2.a, MIN(t2.b) as min FROM t2 GROUP BY t2.a) t
    WHERE t1.a = t.a and t1.b < const

  Re-write the query into
    SELECT t1.a, t.min
      FROM t1, LATERAL (SELECT t2.a, MIN(t2.b) as min
                        FROM t2 WHERE t2.a = t1.a GROUP BY t2.a) t
    WHERE t1.b < const

  The execution of the original query (Q1) does the following:
    1. Executes the query in the specification of the derived table
       and puts the result set into a temporary table with an index
       on the first column.
    2. Joins t1 with the temporary table using the its index.

  The execution of the transformed query (Q1R) follows these steps:
    1. For each row of t1 where t1.b < const a temporary table
       containing all rows of of t2 with t2.a = t1.a is created
    2. If there are any rows in the temporary table aggregation
       is performed for them
    3. The result of the aggregation is joined with t1.

  The second execution can win if:
    a) There is an efficient way to select rows of t2 for which t2.a = t1.a
       (For example if there is an index on t2.a)
    and
    b) The number of temporary tables created for partitions
       is much smaller that the total number of partitions

  It should be noted that for the transformed query aggregation
  for a partition may be performed several times.

  As we can see the optimization basically splits table t2 into
  partitions and performs aggregation over each of them
  independently.

  If we have only one equi-join condition then we either push it as
  for Q1R or we don't. In a general case we may have much more options.
  Consider the query (Q3)
    SELECT
      FROM t1,t2 (SELECT t3.a, t3.b, MIN(t3.c) as min
                  FROM t3 GROUP BY a,b) t
    WHERE t.a = t1.a AND t.b = t2.b
          AND t1.c < c1 and t2.c < c2
          AND P(t1,t2);
  (P(t1,t2) designates  some additional conditions over columns of t1,t2).

  Assuming that there indexes on t3(a,b) and t3(b) here we have several
  reasonable options to push equi-join conditions into the derived.
  All these options should be taken into account when the optimizer
  evaluates different join orders. When the join order (t1,t,t2) is
  evaluated there is only one way of splitting : to push the condition
  t.a = t1.a into t. With the join order (t2,t,t1) only the condition
  t.b = t2.b can be pushed. When the join orders (t1,t2,t) and (t2,t1,t)
  are evaluated then the optimizer should consider pushing t.a = t1.a,
  t.b = t2.b and (t.a = t1.a AND t.b = t2.b) to choose the best condition
  for splitting. Apparently here last condition is the best one because
  it provides the miximum possible number of partitions.

  If we dropped the index on t3(a,b) and created the index on t3(a) instead
  then we would have two options for splitting: to push t.a = t1.a or to
  push t.b = t2.b. If the selectivity of the index t3(a) is better than
  the selectivity of t3(b) then the first option is preferred.

  Although the condition (t.a = t1.a AND t.b = t2.b) provides a better
  splitting than the condition t.a = t1.a the latter will be used for
  splitting if the execution plan with the join order (t1,t,t2) turns out
  to be the cheapest one. It's quite possible when the join condition
  P(t1,t2) has a bad selectivity.

  Whenever the optimizer evaluates the cost of using a splitting it
  compares it with the cost of materialization without splitting.

  If we just drop the index on t3(a,b) the chances that the splitting
  will be used becomes much lower but they still exists providing that
  the fanout of the partial join of t1 and t2 is small enough.
*/

/*
  Splitting can be applied to a materialized table specified by the query
  with post-join operations that require partitioning of the result set produced
  by the join expression used in the FROM clause the query such as GROUP BY
  operation and window function operation. In any of these cases the post-join
  operation can be executed independently for any partition only over the rows
  of this partition. Also if the set of all partitions is divided into disjoint
  subsets the operation can applied to each subset independently. In this case
  all rows are first partitioned into the groups each of which contains all the
  rows from the partitions belonging the same subset and then each group
  is subpartitioned into groups in the the post join operation.

  The set of all rows belonging to the union of several partitions is called
  here superpartition. If a grouping operation is defined by the list
  e_1,...,e_n then any set S = {e_i1,...,e_ik} can be used to devide all rows
  into superpartions such that for any two rows r1, r2  the following holds:
  e_ij(r1) = e_ij(r2) for each e_ij from S. We use the splitting technique
  only if S consists of references to colums  of the joined tables.
  For example if the GROUP BY list looks like this a, g(b), c we can consider
  applying the splitting technique to the superpartitions defined by {a,c},
  {a}, {c} (a and c here may be the references to the columns from different
  tables).
*/

 /*
   The following describes when and how the optimizer decides whether it
   makes sense to employ the splitting technique.

   1. For each instance of a materialized table (derived/view/CTE) it is
      checked that it is potentially splittable. Now it is done right after the
      execution plan for the select specifying this table has been chosen.

   2. Any potentially splittable materialized table T is subject to two-phase
      optimization. It means that the optimizer first builds the best execution
      plan for join that specifies T. Then the control is passed back to the
      optimization process of the embedding select Q. After the execution plan
      for Q has been chosen the optimizer finishes the optimization of the join
      specifying T.

   3. When the optimizer builds the container with the KEYUSE structures
      for the join of embedding select it detects the equi-join conditions
      PC that potentially could be pushed into a potentially splittable
      materialized table T. The collected information about such conditions
      is stored together with other facts on potential splittings for table T.

   4. When the optimizer starts looking for the best execution plan for the
      embedding select Q for each potentially splittable materialized table T
      it creates special KEYUSE structures for pushable equi-join conditions
      PC. These structures are used to add new elements to the container
      of KEYUSE structures built for T. The specifics of these elements is
      that they can be ebabled and disabled during the process of choosing
      the best plan for Q.

   5. When the optimizer extends a partial join order with a potentially
      splittable materialized table T (in function best_access_path) it
      first evaluates a new execution plan for the modified specification
      of T that adds all equi-join conditions that can be pushed with
      current join prefix to the WHERE conditions of the original
      specification of T. If the cost of the new plan is better than the
      the cost of the original materialized table then the optimizer
      prefers to use splitting for the current join prefix. As the cost
      of the plan depends only on the pushed conditions it makes sense
      to cache this plan for other prefixes.

   6. The optimizer takes into account the cost of splitting / materialization
      of a potentially splittable materialized table T as a startup cost
      to access table T.

   7. When the optimizer finally chooses the best execution plan for
      the embedding select Q and this plan prefers using splitting
      for table T with pushed equi-join conditions PC then the execution
      plan for the underlying join with these conditions is chosen for T.
*/

/*
  The implementation of the splitting technique below allows to apply
  the technique only to a materialized derived table / view / CTE whose
  specification is either a select with GROUP BY or a non-grouping select
  with window functions that share the same PARTITION BY list.
*/

#include "mariadb.h"
#include "sql_select.h"

/* Info on a splitting field */
struct SplM_field_info
{
  /* Splitting field in the materialized table T */
  Field *mat_field;
  /* The item from the select list of the specification of T */
  Item *producing_item;
  /* The corresponding splitting field from the specification of T */
  Field *underlying_field;
};


/* Info on the splitting execution plan saved in SplM_opt_info::cache */
struct SplM_plan_info
{
  /* The cached splitting execution plan P */
  struct st_position *best_positions;
  /* The cost of the above plan */
  double cost;
  /* Selectivity of splitting used in P */
  double split_sel;
  /* For fast search of KEYUSE_EXT elements used for splitting in P */
  struct KEYUSE_EXT *keyuse_ext_start;
  /* The tables that contains the fields used for splitting in P */
  TABLE *table;
  /* The number of the key from 'table' used for splitting in P */
  uint key;
  /* Number of the components of 'key' used for splitting in P */
  uint parts;
};


/*
  The structure contains the information that is used by the optimizer
  for potentially splittable materialization of T  that is a materialized
  derived_table / view / CTE
*/
class SplM_opt_info : public Sql_alloc
{
public:
  /* The join for the select specifying T */
  JOIN *join;
  /* The map of tables from 'join' whose columns can be used for partitioning */  
  table_map tables_usable_for_splitting;
  /* Info about the fields of the joined tables usable for splitting */
  SplM_field_info *spl_fields;
  /* The number of elements in the above list */
  uint spl_field_cnt;
  /* Contains the structures to generate all KEYUSEs for pushable equalities */
  List<KEY_FIELD> added_key_fields;
  /* The cache of evaluated execution plans for 'join' with pushed equalities */
  List<SplM_plan_info> plan_cache;
  /* Cost of best execution plan for join when nothing is pushed */
  double unsplit_cost;
  /* Cardinality of T when nothing is pushed */
  double unsplit_card;
  /* Lastly evaluated execution plan for 'join' with pushed equalities */
  SplM_plan_info *last_plan;

  SplM_plan_info *find_plan(TABLE *table, uint key, uint parts);
};


void TABLE::set_spl_opt_info(SplM_opt_info *spl_info)
{
  if (spl_info)
    spl_info->join->spl_opt_info= spl_info;
  spl_opt_info= spl_info;
}


void TABLE::deny_splitting()
{
  DBUG_ASSERT(spl_opt_info != NULL);
  spl_opt_info->join->spl_opt_info= NULL;
  spl_opt_info= NULL;
}


/* This structure is auxiliary and used only in the function that follows it */
struct SplM_field_ext_info: public SplM_field_info
{
  uint item_no;
  bool is_usable_for_ref_access;
};


/**
  @brief
    Check whether this join is one for potentially splittable materialized table

  @details
    The function checks whether this join is for select that specifies
    a potentially splittable materialized table T. If so, the collected
    info on potential splittability of T is attached to the field spl_opt_info
    of the TABLE structure for T.

    The function returns a positive answer if the following holds:
    1. the optimizer switch 'split_materialized' is set 'on'
    2. the select owning this join specifies a materialized derived/view/cte T
    3. this is the only select in the specification of T
    4. condition pushdown is not prohibited into T
    5. T is not recursive
    6. not all of this join are constant or optimized away
    7. T is either
       7.1. a grouping table with GROUP BY list P
       or
       7.2. a non-grouping table with window functions over the same non-empty
            partition specified by the PARTITION BY list P
    8. P contains some references on the columns of the joined tables C
       occurred also in the select list of this join
    9. There are defined some keys usable for ref access of fields from C
       with available statistics. 

  @retval
    true   if the answer is positive
    false  otherwise
*/

bool JOIN::check_for_splittable_materialized()
{
  ORDER *partition_list= 0;
  st_select_lex_unit *unit= select_lex->master_unit();
  TABLE_LIST *derived= unit->derived;
  if (!(optimizer_flag(thd, OPTIMIZER_SWITCH_SPLIT_MATERIALIZED)) ||  // !(1)
      !(derived && derived->is_materialized_derived()) ||             // !(2)
      (unit->first_select()->next_select()) ||                        // !(3)
      (derived->prohibit_cond_pushdown) ||                            // !(4)
      (derived->is_recursive_with_table()) ||                         // !(5)
      (table_count == 0 || const_tables == top_join_tab_count))       // !(6)
    return false;
  if (group_list)                                                     // (7.1)
  {
    if (!select_lex->have_window_funcs())
      partition_list= group_list;
  }
  else if (select_lex->have_window_funcs() &&
           select_lex->window_specs.elements == 1)                    // (7.2)
  {
    partition_list=
      select_lex->window_specs.head()->partition_list->first;
  }
  if (!partition_list)
    return false;

  ORDER *ord;
  Dynamic_array<SplM_field_ext_info> candidates;

  /*
    Select from partition_list all candidates for splitting.
    A candidate must be
    - field item or refer to such (8.1)
    - item mentioned in the select list (8.2)
    Put info about such candidates into the array candidates
  */
  table_map usable_tables= 0;  // tables that contains the candidate
  for (ord= partition_list; ord; ord= ord->next)
  {
    Item *ord_item= *ord->item;
    if (ord_item->real_item()->type() != Item::FIELD_ITEM)   // !(8.1)
      continue;

    Field *ord_field= ((Item_field *) (ord_item->real_item()))->field;

    JOIN_TAB *tab= ord_field->table->reginfo.join_tab;
    if (tab->is_inner_table_of_outer_join())
      continue;

    List_iterator<Item> li(fields_list);
    Item *item;
    uint item_no= 0;
    while ((item= li++))
    {
      if ((*ord->item)->eq(item, 0))       // (8.2)
      {
	SplM_field_ext_info new_elem;
        new_elem.producing_item= item;
        new_elem.item_no= item_no;
        new_elem.mat_field= derived->table->field[item_no];
        new_elem.underlying_field= ord_field;
        new_elem.is_usable_for_ref_access= false;
        candidates.push(new_elem);
        usable_tables|= ord_field->table->map;
        break;
      }
      item_no++;
    }
  }
  if (candidates.elements() == 0)  // no candidates satisfying (8.1) && (8.2)
    return false;

  /*
    For each table from this join find the keys that can be used for ref access
    of the fields mentioned in the 'array candidates'
  */

  SplM_field_ext_info *cand;
  SplM_field_ext_info *cand_start= &candidates.at(0);
  SplM_field_ext_info *cand_end= cand_start + candidates.elements();

  for (JOIN_TAB *tab= join_tab;
       tab < join_tab + top_join_tab_count; tab++)
  {
    TABLE *table= tab->table;
    if (!(table->map & usable_tables))
      continue;

    table->keys_usable_for_splitting.clear_all();
    uint i;
    for (i= 0; i < table->s->keys; i++)
    {
      if (!table->keys_in_use_for_query.is_set(i))
        continue;
      KEY *key_info= table->key_info + i;
      uint key_parts= table->actual_n_key_parts(key_info);
      uint usable_kp_cnt= 0;
      for ( ; usable_kp_cnt < key_parts; usable_kp_cnt++)
      {
        if (key_info->actual_rec_per_key(usable_kp_cnt) == 0)
          break;
        int fldnr= key_info->key_part[usable_kp_cnt].fieldnr;

        for (cand= cand_start; cand < cand_end; cand++)
        {
          if (cand->underlying_field->field_index + 1 == fldnr)
	  {
            cand->is_usable_for_ref_access= true;
            break;
          }
        }
        if (cand == cand_end)
          break;
      }
      if (usable_kp_cnt)
        table->keys_usable_for_splitting.set_bit(i);
    }
  }

  /* Count the candidate fields that can be accessed by ref */
  uint spl_field_cnt= (uint)candidates.elements();
  for (cand= cand_start; cand < cand_end; cand++)
  {
    if (!cand->is_usable_for_ref_access)
      spl_field_cnt--;
  }

  if (!spl_field_cnt)  // No candidate field can be accessed by ref => !(9)
    return false;

  /*
    Create a structure of the type SplM_opt_info and fill it with
    the collected info on potential splittability of T
  */
  SplM_opt_info *spl_opt_info= new (thd->mem_root) SplM_opt_info();
  SplM_field_info *spl_field=
    (SplM_field_info *) (thd->calloc(sizeof(SplM_field_info) *
                                            spl_field_cnt));

  if (!(spl_opt_info && spl_field)) // consider T as not good for splitting
    return false;

  spl_opt_info->join= this;
  spl_opt_info->tables_usable_for_splitting= 0;
  spl_opt_info->spl_field_cnt= spl_field_cnt;
  spl_opt_info->spl_fields= spl_field;
  for (cand= cand_start; cand < cand_end; cand++)
  {
    if (!cand->is_usable_for_ref_access)
      continue;
    spl_field->producing_item= cand->producing_item;
    spl_field->underlying_field= cand->underlying_field;
    spl_field->mat_field= cand->mat_field;
    spl_opt_info->tables_usable_for_splitting|=
    cand->underlying_field->table->map;
    spl_field++;
  }

  /* Attach this info to the table T */
  derived->table->set_spl_opt_info(spl_opt_info);

  return true;
}


/**
  @brief
    Collect info on KEY_FIELD usable for splitting

  @param
    key_field     KEY_FIELD to collect info on

  @details
    The function assumes that this table is potentially splittable.
    The function checks whether the KEY_FIELD structure key_field built for
    this  table was created for a splitting field f. If so, the function does
    the following using info from key_field:
    1. Builds an equality of the form f = key_field->val that could be
       pushed into this table.
    2. Creates a new KEY_FIELD structure for this equality and stores
       a reference to this structure in this->spl_opt_info.
*/

void TABLE::add_splitting_info_for_key_field(KEY_FIELD *key_field)
{
  DBUG_ASSERT(spl_opt_info != NULL);
  JOIN *join= spl_opt_info->join;
  Field *field= key_field->field;
  SplM_field_info *spl_field= spl_opt_info->spl_fields;
  uint i= spl_opt_info->spl_field_cnt;
  for ( ; i; i--, spl_field++)
  {
    if (spl_field->mat_field == field)
      break;
  }
  if (!i)         // field is not usable for splitting
    return;

  /*
    Any equality condition that can be potentially pushed into the
    materialized derived table is constructed now though later it may turn out
    that it is not needed, because it is not used for splitting.
    The reason for this is that the failure to construct it when it has to be
    injected causes denial for further processing of the query.
    Formally this equality is needed in the KEY_FIELD structure constructed
    here that will be used to generate additional keyuses usable for splitting.
    However key_field.cond could be used for this purpose (see implementations
    of virtual function can_optimize_keypart_ref()).

    The condition is built in such a form that it can be added to the WHERE
    condition of the select that specifies this table.
  */
  THD *thd= in_use;
  Item *left_item= spl_field->producing_item->build_clone(thd);
  Item *right_item= key_field->val->build_clone(thd);
  Item_func_eq *eq_item= 0;
  if (left_item && right_item)
  {
    right_item->walk(&Item::set_fields_as_dependent_processor,
                     false, join->select_lex);
    right_item->update_used_tables();
    eq_item= new (thd->mem_root) Item_func_eq(thd, left_item, right_item);
  }
  if (!eq_item)
    return;
  KEY_FIELD *added_key_field=
    (KEY_FIELD *) thd->alloc(sizeof(KEY_FIELD));
  if (!added_key_field ||
      spl_opt_info->added_key_fields.push_back(added_key_field,thd->mem_root))
    return;
  added_key_field->field= spl_field->underlying_field;
  added_key_field->cond= eq_item;
  added_key_field->val= key_field->val;
  added_key_field->level= 0;
  added_key_field->optimize= KEY_OPTIMIZE_EQ;
  added_key_field->eq_func= true;
  added_key_field->null_rejecting= true;
  added_key_field->cond_guard= NULL;
  added_key_field->sj_pred_no= UINT_MAX;
  return;
}


static bool
add_ext_keyuse_for_splitting(Dynamic_array<KEYUSE_EXT> *ext_keyuses,
                             KEY_FIELD *added_key_field, uint key, uint part)
{
  KEYUSE_EXT keyuse_ext;
  Field *field= added_key_field->field;

  JOIN_TAB *tab=field->table->reginfo.join_tab;
  key_map possible_keys=field->get_possible_keys();
  possible_keys.intersect(field->table->keys_usable_for_splitting);
  tab->keys.merge(possible_keys);

  Item_func_eq *eq_item= (Item_func_eq *) (added_key_field->cond);
  keyuse_ext.table= field->table;
  keyuse_ext.val= eq_item->arguments()[1];
  keyuse_ext.key= key;
  keyuse_ext.keypart=part;
  keyuse_ext.keypart_map= (key_part_map) 1 << part;
  keyuse_ext.used_tables= keyuse_ext.val->used_tables();
  keyuse_ext.optimize= added_key_field->optimize & KEY_OPTIMIZE_REF_OR_NULL;
  keyuse_ext.ref_table_rows= 0;
  keyuse_ext.null_rejecting= added_key_field->null_rejecting;
  keyuse_ext.cond_guard= added_key_field->cond_guard;
  keyuse_ext.sj_pred_no= added_key_field->sj_pred_no;
  keyuse_ext.validity_ref= 0;
  keyuse_ext.needed_in_prefix= added_key_field->val->used_tables();
  keyuse_ext.validity_var= false;
  return ext_keyuses->push(keyuse_ext);
}


static int
sort_ext_keyuse(KEYUSE_EXT *a, KEYUSE_EXT *b)
{
  if (a->table->tablenr != b->table->tablenr)
    return (int) (a->table->tablenr - b->table->tablenr);
  if (a->key != b->key)
    return (int) (a->key - b->key);
  return (int) (a->keypart - b->keypart);
}


static void
sort_ext_keyuses(Dynamic_array<KEYUSE_EXT> *keyuses)
{
  KEYUSE_EXT *first_keyuse= &keyuses->at(0);
  my_qsort(first_keyuse, keyuses->elements(), sizeof(KEYUSE_EXT),
           (qsort_cmp) sort_ext_keyuse);
}


/**
  @brief
    Add info on keyuses usable for splitting into an array
*/

static bool
add_ext_keyuses_for_splitting_field(Dynamic_array<KEYUSE_EXT> *ext_keyuses,
                                    KEY_FIELD *added_key_field)
{
  Field *field= added_key_field->field;
  TABLE *table= field->table;
  for (uint key= 0; key < table->s->keys; key++)
  {
    if (!(table->keys_usable_for_splitting.is_set(key)))
      continue;
    KEY *key_info= table->key_info + key;
    uint key_parts= table->actual_n_key_parts(key_info);
    KEY_PART_INFO *key_part_info= key_info->key_part;
    for (uint part=0; part <  key_parts; part++, key_part_info++)
    {
      if (!field->eq(key_part_info->field))
        continue;
      if (add_ext_keyuse_for_splitting(ext_keyuses, added_key_field, key, part))
        return true;
    }
  }
  return false;
}


/*
  @brief
    Cost of the post join operation used in specification of splittable table
*/

static
double spl_postjoin_oper_cost(THD *thd, double join_record_count, uint rec_len)
{
  double cost;
  cost=  get_tmp_table_write_cost(thd, join_record_count,rec_len) *
         join_record_count;   // cost to fill tmp table
  cost+= get_tmp_table_lookup_cost(thd, join_record_count,rec_len) *
         join_record_count;   // cost to perform post join operation used here
  cost+= get_tmp_table_lookup_cost(thd, join_record_count, rec_len) +
         (join_record_count == 0 ? 0 :
          join_record_count * log2 (join_record_count)) *
         SORT_INDEX_CMP_COST;             // cost to perform  sorting
  return cost;
}

/**
  @brief
    Add KEYUSE structures that can be usable for splitting

  @details
    This function is called only for joins created for potentially
    splittable materialized tables. The function does the following:
    1. Creates the dynamic array ext_keyuses_for_splitting of KEYUSE_EXT
       structures and fills is with info about all keyuses that
       could be used for splitting.
    2. Sort the array ext_keyuses_for_splitting for fast access by key
       on certain columns.
    3. Collects and stores cost and cardinality info on the best execution
       plan that does not use splitting and save this plan together with
       corresponding array of keyuses.
    4. Expand this array with KEYUSE elements built from the info stored
       in ext_keyuses_for_splitting that could be produced by pushed
       equalities employed for splitting.
    5. Prepare the extended array of keyuses to be used in the function
       best_access_plan()
*/

void JOIN::add_keyuses_for_splitting()
{
  uint i;
  uint idx;
  KEYUSE_EXT *keyuse_ext;
  KEYUSE_EXT keyuse_ext_end;
  double oper_cost;
  uint rec_len;
  uint added_keyuse_count;
  TABLE *table= select_lex->master_unit()->derived->table;
  List_iterator_fast<KEY_FIELD> li(spl_opt_info->added_key_fields);
  KEY_FIELD *added_key_field;
  if (!spl_opt_info->added_key_fields.elements)
    goto err;
  if (!(ext_keyuses_for_splitting= new Dynamic_array<KEYUSE_EXT>))
    goto err;
  while ((added_key_field= li++))
  {
    (void) add_ext_keyuses_for_splitting_field(ext_keyuses_for_splitting,
                                               added_key_field);
  }
  added_keyuse_count= (uint)ext_keyuses_for_splitting->elements();
  if (!added_keyuse_count)
    goto err;
  sort_ext_keyuses(ext_keyuses_for_splitting);
  bzero((char*) &keyuse_ext_end, sizeof(keyuse_ext_end));
  if (ext_keyuses_for_splitting->push(keyuse_ext_end))
    goto err;

  spl_opt_info->unsplit_card= join_record_count;

  rec_len= table->s->rec_buff_length;

  oper_cost= spl_postjoin_oper_cost(thd, join_record_count, rec_len);

  spl_opt_info->unsplit_cost= best_positions[table_count-1].read_time +
                              oper_cost;

  if (!(save_qep= new Join_plan_state(table_count + 1)))
    goto err;

  save_query_plan(save_qep);

  if (!keyuse.buffer &&
       my_init_dynamic_array(&keyuse, sizeof(KEYUSE), 20, 64,
                             MYF(MY_THREAD_SPECIFIC)))
    goto err;

  if (allocate_dynamic(&keyuse,
                       save_qep->keyuse.elements +
                       added_keyuse_count))
    goto err;

  memcpy(keyuse.buffer,
         save_qep->keyuse.buffer,
         (size_t) save_qep->keyuse.elements * keyuse.size_of_element);
  keyuse.elements= save_qep->keyuse.elements;

  keyuse_ext= &ext_keyuses_for_splitting->at(0);
  idx= save_qep->keyuse.elements;
  for (i=0; i < added_keyuse_count; i++, keyuse_ext++, idx++)
  {
    set_dynamic(&keyuse, (KEYUSE *) keyuse_ext, idx);
    KEYUSE *added_keyuse= ((KEYUSE *) (keyuse.buffer)) + idx;
    added_keyuse->validity_ref= &keyuse_ext->validity_var;
  }

  if (sort_and_filter_keyuse(thd, &keyuse, true))
    goto err;
  optimize_keyuse(this, &keyuse);

  for (uint i= 0; i < table_count; i++)
  {
    JOIN_TAB *tab= join_tab + i;
    map2table[tab->table->tablenr]= tab;
  }

  return;

err:
  if (save_qep)
    restore_query_plan(save_qep);
  table->deny_splitting();
  return;
}


/**
  @brief
    Add KEYUSE structures that can be usable for splitting of this joined table
*/

void JOIN_TAB::add_keyuses_for_splitting()
{
  DBUG_ASSERT(table->spl_opt_info != NULL);
  SplM_opt_info *spl_opt_info= table->spl_opt_info;
  spl_opt_info->join->add_keyuses_for_splitting();
}


/*
  @brief
     Find info on the splitting plan by the splitting key
*/

SplM_plan_info *SplM_opt_info::find_plan(TABLE *table, uint key, uint parts)
{
  List_iterator_fast<SplM_plan_info> li(plan_cache);
  SplM_plan_info *spl_plan;
  while ((spl_plan= li++))
  {
    if (spl_plan->table == table &&
        spl_plan->key == key &&
        spl_plan->parts == parts)
      break;
  }
  return spl_plan;
}


/*
  @breaf
    Enable/Disable a keyuses that can be used for splitting
 */

static
void reset_validity_vars_for_keyuses(KEYUSE_EXT *key_keyuse_ext_start,
                                     TABLE *table, uint key,
                                     table_map remaining_tables,
                                     bool validity_val)
{
  KEYUSE_EXT *keyuse_ext= key_keyuse_ext_start;
  do
  {
    if (!(keyuse_ext->needed_in_prefix & remaining_tables))
    {
      /*
        The enabling/disabling flags are set just in KEYUSE_EXT structures.
        Yet keyuses that are used by best_access_path() have pointers
        to these flags.
      */
      keyuse_ext->validity_var= validity_val;
    }
    keyuse_ext++;
  }
  while (keyuse_ext->key == key && keyuse_ext->table == table);
}


/**
  @brief
    Choose the best splitting to extend the evaluated partial join

  @param
    record_count      estimated cardinality of the extended partial join
    remaining_tables  tables not joined yet

  @details
    This function is called during the search for the best execution
    plan of the join that contains this table T. The function is called
    every time when the optimizer tries to extend a partial join by
    joining it with table T. Depending on what tables are already in the
    partial join different equalities usable for splitting can be pushed
    into T. The function evaluates different variants and chooses the
    best one. Then the function finds the plan for the materializing join
    with the chosen equality conditions pushed into it. If the cost of the
    plan turns out to be less than the cost of the best plan without
    splitting the function set it as the true plan of materialization
    of the table T.
    The function caches the found plans for materialization of table T
    together if the info what key was used for splitting. Next time when
    the optimizer prefers to use the same key the plan is taken from
    the cache of plans

  @retval
    Pointer to the info on the found plan that employs the pushed equalities
    if the plan has been chosen, NULL - otherwise.
*/

SplM_plan_info * JOIN_TAB::choose_best_splitting(double record_count,
                                                 table_map remaining_tables)
{
  SplM_opt_info *spl_opt_info= table->spl_opt_info;
  DBUG_ASSERT(spl_opt_info != NULL);
  JOIN *join= spl_opt_info->join;
  THD *thd= join->thd;
  table_map tables_usable_for_splitting=
              spl_opt_info->tables_usable_for_splitting;
  KEYUSE_EXT *keyuse_ext= &join->ext_keyuses_for_splitting->at(0);
  KEYUSE_EXT *best_key_keyuse_ext_start;
  TABLE *best_table= 0;
  double best_rec_per_key= DBL_MAX;
  SplM_plan_info *spl_plan= 0;
  uint best_key;
  uint best_key_parts;

  /*
    Check whether there are keys that can be used to join T employing splitting
    and if so, select the best out of such keys
  */
  for (uint tablenr= 0; tablenr < join->table_count; tablenr++)
  {
    if (!((1ULL << tablenr) & tables_usable_for_splitting))
      continue;
    JOIN_TAB *tab= join->map2table[tablenr];
    TABLE *table= tab->table;
    do
    {
      uint key= keyuse_ext->key;
      KEYUSE_EXT *key_keyuse_ext_start= keyuse_ext;
      key_part_map found_parts= 0;
      do
      {
        if (keyuse_ext->needed_in_prefix & remaining_tables)
	{
          keyuse_ext++;
          continue;
        }
        if (!(keyuse_ext->keypart_map & found_parts))
	{
          if ((!found_parts && !keyuse_ext->keypart) ||
              (found_parts && ((keyuse_ext->keypart_map >> 1) & found_parts)))
            found_parts|= keyuse_ext->keypart_map;
          else
	  {
            do
	    {
              keyuse_ext++;
            }
            while (keyuse_ext->key == key && keyuse_ext->table == table);
            break;
          }
        }
        KEY *key_info= table->key_info + key;
        double rec_per_key=
                 key_info->actual_rec_per_key(keyuse_ext->keypart);
        if (rec_per_key < best_rec_per_key)
	{
          best_table= keyuse_ext->table;
          best_key= keyuse_ext->key;
	  best_key_parts= keyuse_ext->keypart + 1;
          best_rec_per_key= rec_per_key;
          best_key_keyuse_ext_start= key_keyuse_ext_start;
        }
        keyuse_ext++;
      }
      while (keyuse_ext->key == key && keyuse_ext->table == table);
    }
    while (keyuse_ext->table == table);
  }
  spl_opt_info->last_plan= 0;
  if (best_table)
  {
    /*
      The key for splitting was chosen, look for the plan for this key
      in the cache
    */
    spl_plan= spl_opt_info->find_plan(best_table, best_key, best_key_parts);
    if (!spl_plan &&
	(spl_plan= (SplM_plan_info *) thd->alloc(sizeof(SplM_plan_info))) &&
	(spl_plan->best_positions=
	   (POSITION *) thd->alloc(sizeof(POSITION) * join->table_count)) &&
	!spl_opt_info->plan_cache.push_back(spl_plan))
    {
      /*
        The plan for the chosen key has not been found in the cache.
        Build a new plan and save info on it in the cache
      */
      table_map all_table_map= (1 << join->table_count) - 1;
      reset_validity_vars_for_keyuses(best_key_keyuse_ext_start, best_table,
                                      best_key, remaining_tables, true);
      choose_plan(join, all_table_map & ~join->const_table_map);
      spl_plan->keyuse_ext_start= best_key_keyuse_ext_start;
      spl_plan->table= best_table;
      spl_plan->key= best_key;
      spl_plan->parts= best_key_parts;
      spl_plan->split_sel= best_rec_per_key /
                           (spl_opt_info->unsplit_card ?
                            spl_opt_info->unsplit_card : 1); 

      uint rec_len= table->s->rec_buff_length;

      double split_card= spl_opt_info->unsplit_card * spl_plan->split_sel;
      double oper_cost= split_card *
                        spl_postjoin_oper_cost(thd, split_card, rec_len);
      spl_plan->cost= join->best_positions[join->table_count-1].read_time +
                      + oper_cost;

      memcpy((char *) spl_plan->best_positions,
             (char *) join->best_positions,
             sizeof(POSITION) * join->table_count);
      reset_validity_vars_for_keyuses(best_key_keyuse_ext_start, best_table,
                                      best_key, remaining_tables, false);
    }
    if (spl_plan)
    {
      if(record_count * spl_plan->cost < spl_opt_info->unsplit_cost)
      {
        /*
          The best plan that employs splitting is cheaper than
          the plan without splitting
	*/
        spl_opt_info->last_plan= spl_plan;
      }
    }
  }

  /* Set the cost of the preferred materialization for this partial join */
  records= (ha_rows)spl_opt_info->unsplit_card;
  spl_plan= spl_opt_info->last_plan;
  if (spl_plan)
  {
    startup_cost= record_count * spl_plan->cost;
    records= (ha_rows) (records * spl_plan->split_sel);
  }
  else
    startup_cost= spl_opt_info->unsplit_cost;
  return spl_plan;
}


/**
  @brief
    Inject equalities for splitting used by the materialization join

  @param
    remaining_tables  used to filter out the equalities that cannot
                      be pushed.

  @details
    This function is called by JOIN_TAB::fix_splitting that is used
    to fix the chosen splitting of a splittable materialized table T
    in the final query execution plan. In this plan the table T
    is joined just before the 'remaining_tables'. So all equalities
    usable for splitting whose right parts do not depend on any of
    remaining tables can be pushed into join for T.
    The function also marks the select that specifies T as
    UNCACHEABLE_DEPENDENT_INJECTED.

  @retval
    false  on success
    true   on failure
*/

bool JOIN::inject_best_splitting_cond(table_map remaining_tables)
{
  Item *inj_cond= 0;
  List<Item> inj_cond_list;
  List_iterator<KEY_FIELD> li(spl_opt_info->added_key_fields);
  KEY_FIELD *added_key_field;
  while ((added_key_field= li++))
  {
    if (remaining_tables & added_key_field->val->used_tables())
      continue;
    if (inj_cond_list.push_back(added_key_field->cond, thd->mem_root))
      return true;
  }
  DBUG_ASSERT(inj_cond_list.elements);
  switch (inj_cond_list.elements) {
  case 1:
    inj_cond= inj_cond_list.head(); break;
  default:
    inj_cond= new (thd->mem_root) Item_cond_and(thd, inj_cond_list);
    if (!inj_cond)
      return true;
  }
  if (inj_cond)
    inj_cond->fix_fields(thd,0);

  if (inject_cond_into_where(inj_cond))
    return true;

  select_lex->uncacheable|= UNCACHEABLE_DEPENDENT_INJECTED;
  st_select_lex_unit *unit= select_lex->master_unit();
  unit->uncacheable|= UNCACHEABLE_DEPENDENT_INJECTED;

  return false;
}


/**
  @brief
    Fix the splitting chosen for a splittable table in the final query plan

  @param
    spl_plan   info on the splitting plan chosen for the splittable table T
    remaining_tables  the table T is joined just before these tables

  @details
    If in the final query plan the optimizer has chosen a splitting plan
    then the function sets this plan as the final execution plan to
    materialized the table T. Otherwise the plan that does not use
    splitting is set for the materialization.

  @retval
    false  on success
    true   on failure
*/

bool JOIN_TAB::fix_splitting(SplM_plan_info *spl_plan,
                             table_map remaining_tables)
{
  SplM_opt_info *spl_opt_info= table->spl_opt_info;
  DBUG_ASSERT(table->spl_opt_info != 0);
  JOIN *md_join= spl_opt_info->join;
  if (spl_plan)
  {
    memcpy((char *) md_join->best_positions,
           (char *) spl_plan->best_positions,
           sizeof(POSITION) * md_join->table_count);
    if (md_join->inject_best_splitting_cond(remaining_tables))
      return true;
    /*
      This is called for a proper work of JOIN::get_best_combination()
      called for the join that materializes T
    */
    reset_validity_vars_for_keyuses(spl_plan->keyuse_ext_start,
                                    spl_plan->table,
                                    spl_plan->key,
                                    remaining_tables,
                                    true);
  }
  else
  {
    md_join->restore_query_plan(md_join->save_qep);
  }
  return false;
}


/**
  @brief
    Fix the splittings chosen splittable tables in the final query plan

  @details
    The function calls JOIN_TAB::fix_splittins for all potentially
    splittable tables in this join to set all final materialization
    plans chosen for these tables.

  @retval
    false  on success
    true   on failure
*/

bool JOIN::fix_all_splittings_in_plan()
{
  table_map prev_tables= 0;
  table_map all_tables= (1 << table_count) - 1;
  for (uint tablenr= 0; tablenr < table_count; tablenr++)
  {
    POSITION *cur_pos= &best_positions[tablenr];
    JOIN_TAB *tab= cur_pos->table;
    if (tablenr >= const_tables && tab->table->is_splittable())
    {
      SplM_plan_info *spl_plan= cur_pos->spl_plan;
      if (tab->fix_splitting(spl_plan, all_tables & ~prev_tables))
          return true;
    }
    prev_tables|= tab->table->map;
  }
  return false;
}