~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_select.cc

Much closer toward UTF8 being around all the time...

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
#include <mysys/my_bit.h>
31
31
#include <drizzled/drizzled_error_messages.h>
32
32
#include <libdrizzle/gettext.h>
33
 
#include <drizzled/util/test.h>
34
33
 
35
34
const char *join_type_str[]={ "UNKNOWN","system","const","eq_ref","ref",
36
35
                              "MAYBE_REF","ALL","range","index",
45
44
                                 DYNAMIC_ARRAY *keyuse);
46
45
static bool update_ref_and_keys(THD *thd, DYNAMIC_ARRAY *keyuse,
47
46
                                JOIN_TAB *join_tab,
48
 
                                uint32_t tables, COND *conds,
 
47
                                uint tables, COND *conds,
49
48
                                COND_EQUAL *cond_equal,
50
49
                                table_map table_map, SELECT_LEX *select_lex,
51
50
                                st_sargable_param **sargables);
52
51
static int sort_keyuse(KEYUSE *a,KEYUSE *b);
53
 
static void set_position(JOIN *join,uint32_t index,JOIN_TAB *table,KEYUSE *key);
 
52
static void set_position(JOIN *join,uint index,JOIN_TAB *table,KEYUSE *key);
54
53
static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse,
55
54
                               table_map used_tables);
56
55
static bool choose_plan(JOIN *join,table_map join_tables);
57
56
 
58
57
static void best_access_path(JOIN *join, JOIN_TAB *s, THD *thd,
59
 
                             table_map remaining_tables, uint32_t idx,
 
58
                             table_map remaining_tables, uint idx,
60
59
                             double record_count, double read_time);
61
60
static void optimize_straight_join(JOIN *join, table_map join_tables);
62
61
static bool greedy_search(JOIN *join, table_map remaining_tables,
63
 
                             uint32_t depth, uint32_t prune_level);
 
62
                             uint depth, uint prune_level);
64
63
static bool best_extension_by_limited_search(JOIN *join,
65
64
                                             table_map remaining_tables,
66
 
                                             uint32_t idx, double record_count,
67
 
                                             double read_time, uint32_t depth,
68
 
                                             uint32_t prune_level);
69
 
static uint32_t determine_search_depth(JOIN* join);
 
65
                                             uint idx, double record_count,
 
66
                                             double read_time, uint depth,
 
67
                                             uint prune_level);
 
68
static uint determine_search_depth(JOIN* join);
70
69
static int join_tab_cmp(const void* ptr1, const void* ptr2);
71
70
static int join_tab_cmp_straight(const void* ptr1, const void* ptr2);
72
71
/*
73
72
  TODO: 'find_best' is here only temporarily until 'greedy_search' is
74
73
  tested and approved.
75
74
*/
76
 
static bool find_best(JOIN *join,table_map rest_tables,uint32_t index,
 
75
static bool find_best(JOIN *join,table_map rest_tables,uint index,
77
76
                      double record_count,double read_time);
78
 
static uint32_t cache_record_length(JOIN *join,uint32_t index);
79
 
static double prev_record_reads(JOIN *join, uint32_t idx, table_map found_ref);
 
77
static uint cache_record_length(JOIN *join,uint index);
 
78
static double prev_record_reads(JOIN *join, uint idx, table_map found_ref);
80
79
static bool get_best_combination(JOIN *join);
81
80
static store_key *get_store_key(THD *thd,
82
81
                                KEYUSE *keyuse, table_map used_tables,
83
 
                                KEY_PART_INFO *key_part, unsigned char *key_buff,
84
 
                                uint32_t maybe_null);
 
82
                                KEY_PART_INFO *key_part, uchar *key_buff,
 
83
                                uint maybe_null);
85
84
static bool make_simple_join(JOIN *join,Table *tmp_table);
86
85
static void make_outerjoin_info(JOIN *join);
87
86
static bool make_join_select(JOIN *join,SQL_SELECT *select,COND *item);
88
 
static bool make_join_readinfo(JOIN *join, uint64_t options, uint32_t no_jbuf_after);
 
87
static bool make_join_readinfo(JOIN *join, uint64_t options, uint no_jbuf_after);
89
88
static bool only_eq_ref_tables(JOIN *join, order_st *order, table_map tables);
90
89
static void update_depend_map(JOIN *join);
91
90
static void update_depend_map(JOIN *join, order_st *order);
107
106
static bool check_interleaving_with_nj(JOIN_TAB *last, JOIN_TAB *next);
108
107
static void restore_prev_nj_state(JOIN_TAB *last);
109
108
static void reset_nj_counters(List<TableList> *join_list);
110
 
static uint32_t build_bitmap_for_nested_joins(List<TableList> *join_list,
111
 
                                          uint32_t first_unused);
 
109
static uint build_bitmap_for_nested_joins(List<TableList> *join_list,
 
110
                                          uint first_unused);
112
111
 
113
112
static 
114
113
void advance_sj_state(const table_map remaining_tables, const JOIN_TAB *tab);
176
175
static int remove_dup_with_compare(THD *thd, Table *entry, Field **field,
177
176
                                   ulong offset,Item *having);
178
177
static int remove_dup_with_hash_index(THD *thd,Table *table,
179
 
                                      uint32_t field_count, Field **first_field,
 
178
                                      uint field_count, Field **first_field,
180
179
 
181
180
                                      ulong key_length,Item *having);
182
 
static int join_init_cache(THD *thd,JOIN_TAB *tables,uint32_t table_count);
 
181
static int join_init_cache(THD *thd,JOIN_TAB *tables,uint table_count);
183
182
static ulong used_blob_length(CACHE_FIELD **ptr);
184
183
static bool store_record_in_cache(JOIN_CACHE *cache);
185
184
static void reset_cache_read(JOIN_CACHE *cache);
199
198
static bool change_to_use_tmp_fields(THD *thd, Item **ref_pointer_array,
200
199
                                     List<Item> &new_list1,
201
200
                                     List<Item> &new_list2,
202
 
                                     uint32_t elements, List<Item> &items);
 
201
                                     uint elements, List<Item> &items);
203
202
// Create list for using with tempory table
204
203
static bool change_refs_to_tmp_fields(THD *thd, Item **ref_pointer_array,
205
204
                                      List<Item> &new_list1,
206
205
                                      List<Item> &new_list2,
207
 
                                      uint32_t elements, List<Item> &items);
 
206
                                      uint elements, List<Item> &items);
208
207
static void init_tmptable_sum_functions(Item_sum **func);
209
208
static void update_tmptable_sum_func(Item_sum **func,Table *tmp_table);
210
209
static void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end);
213
212
static bool init_sum_functions(Item_sum **func, Item_sum **end);
214
213
static bool update_sum_func(Item_sum **func);
215
214
void select_describe(JOIN *join, bool need_tmp_table,bool need_order,
216
 
                            bool distinct, const char *message=NULL);
 
215
                            bool distinct, const char *message=NullS);
217
216
static Item *remove_additional_cond(Item* conds);
218
217
static void add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab);
219
218
static bool test_if_ref(Item_field *left_item,Item *right_item);
439
438
int
440
439
JOIN::prepare(Item ***rref_pointer_array,
441
440
              TableList *tables_init,
442
 
              uint32_t wild_num, COND *conds_init, uint32_t og_num,
 
441
              uint wild_num, COND *conds_init, uint og_num,
443
442
              order_st *order_init, order_st *group_init,
444
443
              Item *having_init,
445
444
              order_st *proc_param_init, SELECT_LEX *select_lex_arg,
780
779
    join_tab->packed_info |= TAB_INFO_USING_INDEX;
781
780
  if (where)
782
781
    join_tab->packed_info |= TAB_INFO_USING_WHERE;
783
 
  for (uint32_t i = 0; i < join_tab->ref.key_parts; i++)
 
782
  for (uint i = 0; i < join_tab->ref.key_parts; i++)
784
783
  {
785
784
    if (join_tab->ref.cond_guards[i])
786
785
    {
830
829
  if (join_tab->type == JT_EQ_REF)
831
830
  {
832
831
    Table_map_iterator it(join_tab->ref.depend_map & ~PSEUDO_TABLE_BITS);
833
 
    uint32_t idx;
 
832
    uint idx;
834
833
    while ((idx= it.next_bit())!=Table_map_iterator::BITMAP_END)
835
834
    {
836
835
      JOIN_TAB *ref_tab= join->join_tab + idx;
942
941
*/
943
942
 
944
943
static
945
 
int setup_semijoin_dups_elimination(JOIN *join, uint64_t options, uint32_t no_jbuf_after)
 
944
int setup_semijoin_dups_elimination(JOIN *join, uint64_t options, uint no_jbuf_after)
946
945
{
947
946
  table_map cur_map= join->const_table_map | PSEUDO_TABLE_BITS;
948
947
  struct {
952
951
      2 - Temptable (maybe confluent),
953
952
      3 - Temptable with join buffering
954
953
    */
955
 
    uint32_t strategy;
956
 
    uint32_t start_idx; /* Left range bound */
957
 
    uint32_t end_idx;   /* Right range bound */
 
954
    uint strategy;
 
955
    uint start_idx; /* Left range bound */
 
956
    uint end_idx;   /* Right range bound */
958
957
    /* 
959
958
      For Temptable strategy: Bitmap of all outer and correlated tables from 
960
959
      all involved join nests.
969
968
  table_map range_start_map= 0; /* table_map at current range start */
970
969
  bool dealing_with_jbuf= false; /* true <=> table within cur range uses join buf */
971
970
  int cur_range= 0;
972
 
  uint32_t i;
 
971
  uint i;
973
972
 
974
973
  /*
975
974
    First pass: locate the duplicate-generating ranges and pick the strategies.
1110
1109
    {
1111
1110
      SJ_TMP_TABLE::TAB sjtabs[MAX_TABLES];
1112
1111
      table_map cur_map= join->const_table_map | PSEUDO_TABLE_BITS;
1113
 
      uint32_t jt_rowid_offset= 0; // # tuple bytes are already occupied (w/o NULL bytes)
1114
 
      uint32_t jt_null_bits= 0;    // # null bits in tuple bytes
 
1112
      uint jt_rowid_offset= 0; // # tuple bytes are already occupied (w/o NULL bytes)
 
1113
      uint jt_null_bits= 0;    // # null bits in tuple bytes
1115
1114
      SJ_TMP_TABLE::TAB *last_tab= sjtabs;
1116
 
      uint32_t rowid_keep_flags= JOIN_TAB::CALL_POSITION | JOIN_TAB::KEEP_ROWID;
 
1115
      uint rowid_keep_flags= JOIN_TAB::CALL_POSITION | JOIN_TAB::KEEP_ROWID;
1117
1116
      JOIN_TAB *last_outer_tab= tab - 1;
1118
1117
      /*
1119
1118
        Walk through the range and remember
1145
1144
      if (jt_rowid_offset) /* Temptable has at least one rowid */
1146
1145
      {
1147
1146
        SJ_TMP_TABLE *sjtbl;
1148
 
        uint32_t tabs_size= (last_tab - sjtabs) * sizeof(SJ_TMP_TABLE::TAB);
 
1147
        uint tabs_size= (last_tab - sjtabs) * sizeof(SJ_TMP_TABLE::TAB);
1149
1148
        if (!(sjtbl= (SJ_TMP_TABLE*)thd->alloc(sizeof(SJ_TMP_TABLE))) ||
1150
1149
            !(sjtbl->tabs= (SJ_TMP_TABLE::TAB*) thd->alloc(tabs_size)))
1151
1150
          return(true);
1198
1197
  join->sj_tmp_tables= NULL;
1199
1198
}
1200
1199
 
1201
 
uint32_t make_join_orderinfo(JOIN *join);
 
1200
uint make_join_orderinfo(JOIN *join);
1202
1201
 
1203
1202
/**
1204
1203
  global select optimisation.
1220
1219
    return(0);
1221
1220
  optimized= 1;
1222
1221
 
1223
 
  thd->set_proc_info("optimizing");
 
1222
  thd_proc_info(thd, "optimizing");
1224
1223
  row_limit= ((select_distinct || order || group_list) ? HA_POS_ERROR :
1225
1224
              unit->select_limit_cnt);
1226
1225
  /* select_limit is used to decide if we are likely to scan the whole table */
1254
1253
    }
1255
1254
  }
1256
1255
#endif
 
1256
  SELECT_LEX *sel= thd->lex->current_select;
 
1257
  if (sel->first_cond_optimization)
 
1258
  {
 
1259
    /*
 
1260
      The following code will allocate the new items in a permanent
 
1261
      MEMROOT for prepared statements and stored procedures.
 
1262
    */
 
1263
    sel->first_cond_optimization= 0;
1257
1264
 
1258
 
  /* Convert all outer joins to inner joins if possible */
1259
 
  conds= simplify_joins(this, join_list, conds, true, false);
1260
 
  build_bitmap_for_nested_joins(join_list, 0);
 
1265
    /* Convert all outer joins to inner joins if possible */
 
1266
    conds= simplify_joins(this, join_list, conds, true, false);
 
1267
    build_bitmap_for_nested_joins(join_list, 0);
 
1268
  }
1261
1269
 
1262
1270
  conds= optimize_cond(this, conds, join_list, &cond_value);   
1263
1271
  if (thd->is_error())
1288
1296
    }
1289
1297
  }
1290
1298
 
1291
 
  /* Optimize count(*), cmin() and cmax() */
 
1299
  /* Optimize count(*), min() and max() */
1292
1300
  if (tables_list && tmp_table_param.sum_func_count && ! group_list)
1293
1301
  {
1294
1302
    int res;
1346
1354
  sort_by_table= get_sort_by_table(order, group_list, select_lex->leaf_tables);
1347
1355
 
1348
1356
  /* Calculate how to do the join */
1349
 
  thd->set_proc_info("statistics");
 
1357
  thd_proc_info(thd, "statistics");
1350
1358
  if (make_join_statistics(this, select_lex->leaf_tables, conds, &keyuse) ||
1351
1359
      thd->is_fatal_error)
1352
1360
  {
1355
1363
 
1356
1364
  /* Remove distinct if only const tables */
1357
1365
  select_distinct= select_distinct && (const_tables != tables);
1358
 
  thd->set_proc_info("preparing");
 
1366
  thd_proc_info(thd, "preparing");
1359
1367
  if (result->initialize_tables(this))
1360
1368
  {
1361
1369
    return(1);                          // error == -1
1628
1636
              (group_list && order) ||
1629
1637
              test(select_options & OPTION_BUFFER_RESULT)));
1630
1638
 
1631
 
  uint32_t no_jbuf_after= make_join_orderinfo(this);
 
1639
  uint no_jbuf_after= make_join_orderinfo(this);
1632
1640
  uint64_t select_opts_for_readinfo= 
1633
1641
    (select_options & (SELECT_DESCRIBE | SELECT_NO_JOIN_CACHE)) | (0);
1634
1642
 
1712
1720
  */
1713
1721
  if (need_tmp || select_distinct || group_list || order)
1714
1722
  {
1715
 
    for (uint32_t i = const_tables; i < tables; i++)
 
1723
    for (uint i = const_tables; i < tables; i++)
1716
1724
      join_tab[i].table->prepare_for_position();
1717
1725
  }
1718
1726
 
1790
1798
  /* Create a tmp table if distinct or if the sort is too complicated */
1791
1799
  if (need_tmp)
1792
1800
  {
1793
 
    thd->set_proc_info("Creating tmp table");
 
1801
    thd_proc_info(thd, "Creating tmp table");
1794
1802
 
1795
1803
    init_items_ref_array();
1796
1804
 
1838
1846
    /* if group or order on first table, sort first */
1839
1847
    if (group_list && simple_group)
1840
1848
    {
1841
 
      thd->set_proc_info("Sorting for group");
 
1849
      thd_proc_info(thd, "Sorting for group");
1842
1850
      if (create_sort_index(thd, this, group_list,
1843
1851
                            HA_POS_ERROR, HA_POS_ERROR, false) ||
1844
1852
          alloc_group_fields(this, group_list) ||
1859
1867
 
1860
1868
      if (!group_list && ! exec_tmp_table1->distinct && order && simple_order)
1861
1869
      {
1862
 
        thd->set_proc_info("Sorting for order");
 
1870
        thd_proc_info(thd, "Sorting for order");
1863
1871
        if (create_sort_index(thd, this, order,
1864
1872
                              HA_POS_ERROR, HA_POS_ERROR, true))
1865
1873
        {
1925
1933
{
1926
1934
  unit->offset_limit_cnt= (ha_rows)(select_lex->offset_limit ?
1927
1935
                                    select_lex->offset_limit->val_uint() :
1928
 
                                    0UL);
 
1936
                                    0ULL);
1929
1937
 
1930
1938
  first_record= 0;
1931
1939
 
1990
1998
{
1991
1999
  if (!join_tab_save && select_lex->master_unit()->uncacheable)
1992
2000
  {
1993
 
    if (!(join_tab_save= (JOIN_TAB*)thd->memdup((unsigned char*) join_tab,
 
2001
    if (!(join_tab_save= (JOIN_TAB*)thd->memdup((uchar*) join_tab,
1994
2002
                                                sizeof(JOIN_TAB) * tables)))
1995
2003
      return 1;
1996
2004
  }
2015
2023
  List<Item> *columns_list= &fields_list;
2016
2024
  int      tmp_error;
2017
2025
 
2018
 
  thd->set_proc_info("executing");
 
2026
  thd_proc_info(thd, "executing");
2019
2027
  error= 0;
2020
2028
  (void) result->prepare2(); // Currently, this cannot fail.
2021
2029
 
2110
2118
    select_describe(this, need_tmp,
2111
2119
                    order != 0 && !skip_sort_order,
2112
2120
                    select_distinct,
2113
 
                    !tables ? "No tables used" : NULL);
 
2121
                    !tables ? "No tables used" : NullS);
2114
2122
    return;
2115
2123
  }
2116
2124
 
2141
2149
    curr_tmp_table= exec_tmp_table1;
2142
2150
 
2143
2151
    /* Copy data to the temporary table */
2144
 
    thd->set_proc_info("Copying to tmp table");
 
2152
    thd_proc_info(thd, "Copying to tmp table");
2145
2153
    if (!curr_join->sort_and_group &&
2146
2154
        curr_join->const_tables != curr_join->tables)
2147
2155
      curr_join->join_tab[curr_join->const_tables].sorted= 0;
2256
2264
      }
2257
2265
      if (curr_join->group_list)
2258
2266
      {
2259
 
        thd->set_proc_info("Creating sort index");
 
2267
        thd_proc_info(thd, "Creating sort index");
2260
2268
        if (curr_join->join_tab == join_tab && save_join_tab())
2261
2269
        {
2262
2270
          return;
2270
2278
        sortorder= curr_join->sortorder;
2271
2279
      }
2272
2280
      
2273
 
      thd->set_proc_info("Copying to group table");
 
2281
      thd_proc_info(thd, "Copying to group table");
2274
2282
      tmp_error= -1;
2275
2283
      if (curr_join != this)
2276
2284
      {
2327
2335
    curr_join->join_free();                     /* Free quick selects */
2328
2336
    if (curr_join->select_distinct && ! curr_join->group_list)
2329
2337
    {
2330
 
      thd->set_proc_info("Removing duplicates");
 
2338
      thd_proc_info(thd, "Removing duplicates");
2331
2339
      if (curr_join->tmp_having)
2332
2340
        curr_join->tmp_having->update_used_tables();
2333
2341
      if (remove_duplicates(curr_join, curr_tmp_table,
2385
2393
  }
2386
2394
  if (curr_join->group_list || curr_join->order)
2387
2395
  {
2388
 
    thd->set_proc_info("Sorting result");
 
2396
    thd_proc_info(thd, "Sorting result");
2389
2397
    /* If we have already done the group, add HAVING to sorted table */
2390
2398
    if (curr_join->tmp_having && ! curr_join->group_list && 
2391
2399
        ! curr_join->sort_and_group)
2495
2503
  curr_join->fields= curr_fields_list;
2496
2504
 
2497
2505
  {
2498
 
    thd->set_proc_info("Sending data");
 
2506
    thd_proc_info(thd, "Sending data");
2499
2507
    result->send_fields(*curr_fields_list,
2500
2508
                        Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
2501
2509
    error= do_select(curr_join, curr_fields_list, NULL);
2603
2611
 
2604
2612
bool
2605
2613
mysql_select(THD *thd, Item ***rref_pointer_array,
2606
 
             TableList *tables, uint32_t wild_num, List<Item> &fields,
2607
 
             COND *conds, uint32_t og_num,  order_st *order, order_st *group,
 
2614
             TableList *tables, uint wild_num, List<Item> &fields,
 
2615
             COND *conds, uint og_num,  order_st *order, order_st *group,
2608
2616
             Item *having, order_st *proc_param, uint64_t select_options,
2609
2617
             select_result *result, SELECT_LEX_UNIT *unit,
2610
2618
             SELECT_LEX *select_lex)
2649
2657
  {
2650
2658
    if (!(join= new JOIN(thd, fields, select_options, result)))
2651
2659
        return(true);
2652
 
    thd->set_proc_info("init");
 
2660
    thd_proc_info(thd, "init");
2653
2661
    thd->used_tables=0;                         // Updated by setup_fields
2654
2662
    if ((err= join->prepare(rref_pointer_array, tables, wild_num,
2655
2663
                           conds, og_num, order, group, having, proc_param,
2692
2700
err:
2693
2701
  if (free_join)
2694
2702
  {
2695
 
    thd->set_proc_info("end");
 
2703
    thd_proc_info(thd, "end");
2696
2704
    err|= select_lex->cleanup();
2697
2705
    return(err || thd->is_error());
2698
2706
  }
2720
2728
  if (!(tbl= (TableList*) thd->calloc(ALIGN_SIZE(sizeof(TableList))+
2721
2729
                                       sizeof(nested_join_st))))
2722
2730
    return NULL;
2723
 
  tbl->nested_join= (nested_join_st*) ((unsigned char*)tbl + 
 
2731
  tbl->nested_join= (nested_join_st*) ((uchar*)tbl + 
2724
2732
                                    ALIGN_SIZE(sizeof(TableList)));
2725
2733
  return tbl;
2726
2734
}
2936
2944
  /*TODO: also reset the 'with_subselect' there. */
2937
2945
 
2938
2946
  /* n. Adjust the parent_join->tables counter */
2939
 
  uint32_t table_no= parent_join->tables;
 
2947
  uint table_no= parent_join->tables;
2940
2948
  /* n. Walk through child's tables and adjust table->map */
2941
2949
  for (tl= subq_lex->leaf_tables; tl; tl= tl->next_leaf, table_no++)
2942
2950
  {
2994
3002
  }
2995
3003
  else
2996
3004
  {
2997
 
    for (uint32_t i= 0; i < subq_pred->left_expr->cols(); i++)
 
3005
    for (uint i= 0; i < subq_pred->left_expr->cols(); i++)
2998
3006
    {
2999
3007
      nested_join->sj_outer_expr_list.push_back(subq_pred->left_expr->
3000
3008
                                                element_index(i));
3196
3204
bool find_eq_ref_candidate(Table *table, table_map sj_inner_tables)
3197
3205
{
3198
3206
  KEYUSE *keyuse= table->reginfo.join_tab->keyuse;
3199
 
  uint32_t key;
 
3207
  uint key;
3200
3208
 
3201
3209
  if (keyuse)
3202
3210
  {
3394
3402
{
3395
3403
  Field *field;              /* field against which to check sargability */
3396
3404
  Item **arg_value;          /* values of potential keys for lookups     */
3397
 
  uint32_t num_values;           /* number of values in the above array      */
 
3405
  uint num_values;           /* number of values in the above array      */
3398
3406
} SARGABLE_PARAM;  
3399
3407
 
3400
3408
/**
3412
3420
{
3413
3421
  int error;
3414
3422
  Table *table;
3415
 
  uint32_t i,table_count,const_count,key;
 
3423
  uint i,table_count,const_count,key;
3416
3424
  table_map found_const_table_map, all_table_map, found_ref, refs;
3417
3425
  key_map const_ref, eq_part;
3418
3426
  Table **table_vector;
3521
3529
    */
3522
3530
    for (i= 0, s= stat ; i < table_count ; i++, s++)
3523
3531
    {
3524
 
      for (uint32_t j= 0 ; j < table_count ; j++)
 
3532
      for (uint j= 0 ; j < table_count ; j++)
3525
3533
      {
3526
3534
        table= stat[j].table;
3527
3535
        if (s->dependent & table->map)
3718
3726
      key_map possible_keys= field->key_start;
3719
3727
      possible_keys.intersect(field->table->keys_in_use_for_query);
3720
3728
      bool is_const= 1;
3721
 
      for (uint32_t j=0; j < sargables->num_values; j++)
 
3729
      for (uint j=0; j < sargables->num_values; j++)
3722
3730
        is_const&= sargables->arg_value[j]->const_item();
3723
3731
      if (is_const)
3724
3732
        join_tab[0].const_keys.merge(possible_keys);
3747
3755
      This is can't be to high as otherwise we are likely to use
3748
3756
      table scan.
3749
3757
    */
3750
 
    s->worst_seeks= cmin((double) s->found_records / 10,
 
3758
    s->worst_seeks= min((double) s->found_records / 10,
3751
3759
                        (double) s->read_time*3);
3752
3760
    if (s->worst_seeks < 2.0)                   // Fix for small tables
3753
3761
      s->worst_seeks=2.0;
3848
3856
  */
3849
3857
  bool          null_rejecting; 
3850
3858
  bool          *cond_guard; /* See KEYUSE::cond_guard */
3851
 
  uint32_t          sj_pred_no; /* See KEYUSE::sj_pred_no */
 
3859
  uint          sj_pred_no; /* See KEYUSE::sj_pred_no */
3852
3860
} KEY_FIELD;
3853
3861
 
3854
3862
/**
3877
3885
 
3878
3886
static KEY_FIELD *
3879
3887
merge_key_fields(KEY_FIELD *start,KEY_FIELD *new_fields,KEY_FIELD *end,
3880
 
                 uint32_t and_level)
 
3888
                 uint and_level)
3881
3889
{
3882
3890
  if (start == new_fields)
3883
3891
    return start;                               // Impossible or
4004
4012
*/
4005
4013
 
4006
4014
static void
4007
 
add_key_field(KEY_FIELD **key_fields,uint32_t and_level, Item_func *cond,
4008
 
              Field *field, bool eq_func, Item **value, uint32_t num_values,
 
4015
add_key_field(KEY_FIELD **key_fields,uint and_level, Item_func *cond,
 
4016
              Field *field, bool eq_func, Item **value, uint num_values,
4009
4017
              table_map usable_tables, SARGABLE_PARAM **sargables)
4010
4018
{
4011
 
  uint32_t exists_optimize= 0;
 
4019
  uint exists_optimize= 0;
4012
4020
  if (!(field->flags & PART_KEY_FLAG))
4013
4021
  {
4014
4022
    // Don't remove column IS NULL on a LEFT JOIN table
4022
4030
  {
4023
4031
    table_map used_tables=0;
4024
4032
    bool optimizable=0;
4025
 
    for (uint32_t i=0; i<num_values; i++)
 
4033
    for (uint i=0; i<num_values; i++)
4026
4034
    {
4027
4035
      used_tables|=(value[i])->used_tables();
4028
4036
      if (!((value[i])->used_tables() & (field->table->map | RAND_TABLE_BIT)))
4058
4066
      stat[0].key_dependent|=used_tables;
4059
4067
 
4060
4068
      bool is_const=1;
4061
 
      for (uint32_t i=0; i<num_values; i++)
 
4069
      for (uint i=0; i<num_values; i++)
4062
4070
      {
4063
4071
        if (!(is_const&= value[i]->const_item()))
4064
4072
          break;
4176
4184
*/
4177
4185
 
4178
4186
static void
4179
 
add_key_equal_fields(KEY_FIELD **key_fields, uint32_t and_level,
 
4187
add_key_equal_fields(KEY_FIELD **key_fields, uint and_level,
4180
4188
                     Item_func *cond, Item_field *field_item,
4181
4189
                     bool eq_func, Item **val,
4182
 
                     uint32_t num_values, table_map usable_tables,
 
4190
                     uint num_values, table_map usable_tables,
4183
4191
                     SARGABLE_PARAM **sargables)
4184
4192
{
4185
4193
  Field *field= field_item->field;
4207
4215
}
4208
4216
 
4209
4217
static void
4210
 
add_key_fields(JOIN *join, KEY_FIELD **key_fields, uint32_t *and_level,
 
4218
add_key_fields(JOIN *join, KEY_FIELD **key_fields, uint *and_level,
4211
4219
               COND *cond, table_map usable_tables,
4212
4220
               SARGABLE_PARAM **sargables)
4213
4221
{
4300
4308
    if (cond_func->functype() == Item_func::BETWEEN)
4301
4309
    {
4302
4310
      values= cond_func->arguments();
4303
 
      for (uint32_t i= 1 ; i < cond_func->argument_count() ; i++)
 
4311
      for (uint i= 1 ; i < cond_func->argument_count() ; i++)
4304
4312
      {
4305
4313
        Item_field *field_item;
4306
4314
        if (cond_func->arguments()[i]->real_item()->type() == Item::FIELD_ITEM
4411
4419
static uint
4412
4420
max_part_bit(key_part_map bits)
4413
4421
{
4414
 
  uint32_t found;
 
4422
  uint found;
4415
4423
  for (found=0; bits & 1 ; found++,bits>>=1) ;
4416
4424
  return found;
4417
4425
}
4425
4433
 
4426
4434
  if (key_field->eq_func && !(key_field->optimize & KEY_OPTIMIZE_EXISTS))
4427
4435
  {
4428
 
    for (uint32_t key= 0 ; key < form->sizeKeys() ; key++)
 
4436
    for (uint key= 0 ; key < form->sizeKeys() ; key++)
4429
4437
    {
4430
4438
      if (!(form->keys_in_use_for_query.is_set(key)))
4431
4439
        continue;
4432
4440
 
4433
 
      uint32_t key_parts= (uint) form->key_info[key].key_parts;
4434
 
      for (uint32_t part=0 ; part <  key_parts ; part++)
 
4441
      uint key_parts= (uint) form->key_info[key].key_parts;
 
4442
      for (uint part=0 ; part <  key_parts ; part++)
4435
4443
      {
4436
4444
        if (field->eq(form->key_info[key].key_part[part].field))
4437
4445
        {
4445
4453
          keyuse.null_rejecting= key_field->null_rejecting;
4446
4454
          keyuse.cond_guard= key_field->cond_guard;
4447
4455
          keyuse.sj_pred_no= key_field->sj_pred_no;
4448
 
          insert_dynamic(keyuse_array,(unsigned char*) &keyuse);
 
4456
          VOID(insert_dynamic(keyuse_array,(uchar*) &keyuse));
4449
4457
        }
4450
4458
      }
4451
4459
    }
4508
4516
*/
4509
4517
 
4510
4518
static void add_key_fields_for_nj(JOIN *join, TableList *nested_join_table,
4511
 
                                  KEY_FIELD **end, uint32_t *and_level,
 
4519
                                  KEY_FIELD **end, uint *and_level,
4512
4520
                                  SARGABLE_PARAM **sargables)
4513
4521
{
4514
4522
  List_iterator<TableList> li(nested_join_table->nested_join->join_list);
4566
4574
 
4567
4575
static bool
4568
4576
update_ref_and_keys(THD *thd, DYNAMIC_ARRAY *keyuse,JOIN_TAB *join_tab,
4569
 
                    uint32_t tables, COND *cond,
 
4577
                    uint tables, COND *cond,
4570
4578
                    COND_EQUAL *cond_equal __attribute__((unused)),
4571
4579
                    table_map normal_tables, SELECT_LEX *select_lex,
4572
4580
                    SARGABLE_PARAM **sargables)
4573
4581
{
4574
4582
  uint  and_level,i,found_eq_constant;
4575
4583
  KEY_FIELD *key_fields, *end, *field;
4576
 
  uint32_t sz;
4577
 
  uint32_t m= cmax(select_lex->max_equal_elems,(uint32_t)1);
 
4584
  uint sz;
 
4585
  uint m= max(select_lex->max_equal_elems,(uint32_t)1);
4578
4586
  
4579
4587
  /* 
4580
4588
    We use the same piece of memory to store both  KEY_FIELD 
4597
4605
    can be not more than select_lex->max_equal_elems such 
4598
4606
    substitutions.
4599
4607
  */ 
4600
 
  sz= cmax(sizeof(KEY_FIELD),sizeof(SARGABLE_PARAM))*
 
4608
  sz= max(sizeof(KEY_FIELD),sizeof(SARGABLE_PARAM))*
4601
4609
      (((thd->lex->current_select->cond_count+1)*2 +
4602
4610
        thd->lex->current_select->between_count)*m+1);
4603
4611
  if (!(key_fields=(KEY_FIELD*) thd->alloc(sz)))
4674
4682
          (qsort_cmp) sort_keyuse);
4675
4683
 
4676
4684
    memset(&key_end, 0, sizeof(key_end)); /* Add for easy testing */
4677
 
    insert_dynamic(keyuse,(unsigned char*) &key_end);
 
4685
    VOID(insert_dynamic(keyuse,(uchar*) &key_end));
4678
4686
 
4679
4687
    use=save_pos=dynamic_element(keyuse,0,KEYUSE*);
4680
4688
    prev= &key_end;
4707
4715
      save_pos++;
4708
4716
    }
4709
4717
    i=(uint) (save_pos-(KEYUSE*) keyuse->buffer);
4710
 
    set_dynamic(keyuse,(unsigned char*) &key_end,i);
 
4718
    VOID(set_dynamic(keyuse,(uchar*) &key_end,i));
4711
4719
    keyuse->elements=i;
4712
4720
  }
4713
4721
  return false;
4737
4745
        (map= (keyuse->used_tables & ~join->const_table_map &
4738
4746
               ~OUTER_REF_TABLE_BIT)))
4739
4747
    {
4740
 
      uint32_t tablenr;
 
4748
      uint tablenr;
4741
4749
      for (tablenr=0 ; ! (map & 1) ; map>>=1, tablenr++) ;
4742
4750
      if (map == 1)                     // Only one table
4743
4751
      {
4744
4752
        Table *tmp_table=join->all_tables[tablenr];
4745
 
        keyuse->ref_table_rows= cmax(tmp_table->file->stats.records, (ha_rows)100);
 
4753
        keyuse->ref_table_rows= max(tmp_table->file->stats.records, (ha_rows)100);
4746
4754
      }
4747
4755
    }
4748
4756
    /*
4786
4794
  { /* Collect all query fields referenced in the GROUP clause. */
4787
4795
    for (cur_group= join->group_list; cur_group; cur_group= cur_group->next)
4788
4796
      (*cur_group->item)->walk(&Item::collect_item_field_processor, 0,
4789
 
                               (unsigned char*) &indexed_fields);
 
4797
                               (uchar*) &indexed_fields);
4790
4798
  }
4791
4799
  else if (join->select_distinct)
4792
4800
  { /* Collect all query fields referenced in the SELECT clause. */
4795
4803
    Item *item;
4796
4804
    while ((item= select_items_it++))
4797
4805
      item->walk(&Item::collect_item_field_processor, 0,
4798
 
                 (unsigned char*) &indexed_fields);
 
4806
                 (uchar*) &indexed_fields);
4799
4807
  }
4800
4808
  else
4801
4809
    return;
4824
4832
/** Save const tables first as used tables. */
4825
4833
 
4826
4834
static void
4827
 
set_position(JOIN *join,uint32_t idx,JOIN_TAB *table,KEYUSE *key)
 
4835
set_position(JOIN *join,uint idx,JOIN_TAB *table,KEYUSE *key)
4828
4836
{
4829
4837
  join->positions[idx].table= table;
4830
4838
  join->positions[idx].key=key;
4866
4874
{
4867
4875
  List_iterator<Item> li(sj_nest->nested_join->sj_outer_expr_list);
4868
4876
  Item *item;
4869
 
  uint32_t i= 0;
 
4877
  uint i= 0;
4870
4878
  uint64_t res= 0;
4871
4879
  while ((item= li++))
4872
4880
  {
4878
4886
    */
4879
4887
    if (!(item->used_tables() & remaining_tables))
4880
4888
    {
4881
 
      res |= 1UL < i;
 
4889
      res |= 1ULL < i;
4882
4890
    }
4883
4891
  }
4884
4892
  return res;
4915
4923
                 JOIN_TAB  *s,
4916
4924
                 THD       *thd,
4917
4925
                 table_map remaining_tables,
4918
 
                 uint32_t      idx,
 
4926
                 uint      idx,
4919
4927
                 double    record_count,
4920
4928
                 double    read_time __attribute__((unused)))
4921
4929
{
4922
4930
  KEYUSE *best_key=         0;
4923
 
  uint32_t best_max_key_part=   0;
 
4931
  uint best_max_key_part=   0;
4924
4932
  bool found_constraint= 0;
4925
4933
  double best=              DBL_MAX;
4926
4934
  double best_time=         DBL_MAX;
4928
4936
  table_map best_ref_depends_map= 0;
4929
4937
  double tmp;
4930
4938
  ha_rows rec;
4931
 
  uint32_t best_is_sj_inside_out=    0;
 
4939
  uint best_is_sj_inside_out=    0;
4932
4940
 
4933
4941
  if (s->keyuse)
4934
4942
  {                                            /* Use key if possible */
4966
4974
    {
4967
4975
      key_part_map found_part= 0;
4968
4976
      table_map found_ref= 0;
4969
 
      uint32_t key= keyuse->key;
 
4977
      uint key= keyuse->key;
4970
4978
      KEY *keyinfo= table->key_info+key;
4971
4979
      /* Bitmap of keyparts where the ref access is over 'keypart=const': */
4972
4980
      key_part_map const_part= 0;
4980
4988
 
4981
4989
      do /* For each keypart */
4982
4990
      {
4983
 
        uint32_t keypart= keyuse->keypart;
 
4991
        uint keypart= keyuse->keypart;
4984
4992
        table_map best_part_found_ref= 0;
4985
4993
        double best_prev_record_reads= DBL_MAX;
4986
4994
        
5019
5027
          if (try_sj_inside_out && keyuse->sj_pred_no != UINT_MAX)
5020
5028
          {
5021
5029
            if (!(remaining_tables & keyuse->used_tables))
5022
 
              bound_sj_equalities |= 1UL << keyuse->sj_pred_no;
 
5030
              bound_sj_equalities |= 1ULL << keyuse->sj_pred_no;
5023
5031
            else
5024
5032
            {
5025
 
              handled_sj_equalities |= 1UL << keyuse->sj_pred_no;
 
5033
              handled_sj_equalities |= 1ULL << keyuse->sj_pred_no;
5026
5034
              sj_insideout_map |= ((key_part_map)1) << keyuse->keypart;
5027
5035
            }
5028
5036
          }
5056
5064
            (handled_sj_equalities | bound_sj_equalities) ==     // (1)
5057
5065
            PREV_BITS(uint64_t, s->emb_sj_nest->sj_in_exprs)) // (1)
5058
5066
        {
5059
 
          uint32_t n_fixed_parts= max_part_bit(found_part);
 
5067
          uint n_fixed_parts= max_part_bit(found_part);
5060
5068
          if (n_fixed_parts != keyinfo->key_parts &&
5061
5069
              (PREV_BITS(uint, n_fixed_parts) | sj_insideout_map) ==
5062
5070
               PREV_BITS(uint, keyinfo->key_parts))
5171
5179
              tmp= record_count * table->file->index_only_read_time(key, tmp);
5172
5180
            }
5173
5181
            else
5174
 
              tmp= record_count*cmin(tmp,s->worst_seeks);
 
5182
              tmp= record_count*min(tmp,s->worst_seeks);
5175
5183
          }
5176
5184
        }
5177
5185
        else
5227
5235
            */
5228
5236
            if (table->quick_keys.is_set(key) && !found_ref &&          //(C1)
5229
5237
                table->quick_key_parts[key] == max_key_part &&          //(C2)
5230
 
                table->quick_n_ranges[key] == 1+((ref_or_null_part)?1:0)) //(C3)
 
5238
                table->quick_n_ranges[key] == 1+test(ref_or_null_part)) //(C3)
5231
5239
            {
5232
5240
              tmp= records= (double) table->quick_rows[key];
5233
5241
            }
5320
5328
              if (table->quick_keys.is_set(key) &&
5321
5329
                  table->quick_key_parts[key] <= max_key_part &&
5322
5330
                  const_part & (1 << table->quick_key_parts[key]) &&
5323
 
                  table->quick_n_ranges[key] == 1 + ((ref_or_null_part &
5324
 
                                                     const_part) ? 1 : 0) &&
 
5331
                  table->quick_n_ranges[key] == 1 + test(ref_or_null_part &
 
5332
                                                         const_part) &&
5325
5333
                  records > (double) table->quick_rows[key])
5326
5334
              {
5327
5335
                tmp= records= (double) table->quick_rows[key];
5336
5344
              tmp= record_count * table->file->index_only_read_time(key, tmp);
5337
5345
            }
5338
5346
            else
5339
 
              tmp= record_count * cmin(tmp,s->worst_seeks);
 
5347
              tmp= record_count * min(tmp,s->worst_seeks);
5340
5348
          }
5341
5349
          else
5342
5350
            tmp= best_time;                    // Do nothing
5537
5545
static bool
5538
5546
choose_plan(JOIN *join, table_map join_tables)
5539
5547
{
5540
 
  uint32_t search_depth= join->thd->variables.optimizer_search_depth;
5541
 
  uint32_t prune_level=  join->thd->variables.optimizer_prune_level;
 
5548
  uint search_depth= join->thd->variables.optimizer_search_depth;
 
5549
  uint prune_level=  join->thd->variables.optimizer_prune_level;
5542
5550
  bool straight_join= test(join->select_options & SELECT_STRAIGHT_JOIN);
5543
5551
 
5544
5552
  join->cur_embedding_map= 0;
5672
5680
 
5673
5681
  @todo
5674
5682
    this value should be determined dynamically, based on statistics:
5675
 
    uint32_t max_tables_for_exhaustive_opt= 7;
 
5683
    uint max_tables_for_exhaustive_opt= 7;
5676
5684
 
5677
5685
  @todo
5678
5686
    this value could be determined by some mapping of the form:
5687
5695
static uint
5688
5696
determine_search_depth(JOIN *join)
5689
5697
{
5690
 
  uint32_t table_count=  join->tables - join->const_tables;
5691
 
  uint32_t search_depth;
 
5698
  uint table_count=  join->tables - join->const_tables;
 
5699
  uint search_depth;
5692
5700
  /* TODO: this value should be determined dynamically, based on statistics: */
5693
 
  uint32_t max_tables_for_exhaustive_opt= 7;
 
5701
  uint max_tables_for_exhaustive_opt= 7;
5694
5702
 
5695
5703
  if (table_count <= max_tables_for_exhaustive_opt)
5696
5704
    search_depth= table_count+1; // use exhaustive for small number of tables
5732
5740
optimize_straight_join(JOIN *join, table_map join_tables)
5733
5741
{
5734
5742
  JOIN_TAB *s;
5735
 
  uint32_t idx= join->const_tables;
 
5743
  uint idx= join->const_tables;
5736
5744
  double    record_count= 1.0;
5737
5745
  double    read_time=    0.0;
5738
5746
 
5842
5850
static bool
5843
5851
greedy_search(JOIN      *join,
5844
5852
              table_map remaining_tables,
5845
 
              uint32_t      search_depth,
5846
 
              uint32_t      prune_level)
 
5853
              uint      search_depth,
 
5854
              uint      prune_level)
5847
5855
{
5848
5856
  double    record_count= 1.0;
5849
5857
  double    read_time=    0.0;
5850
 
  uint32_t      idx= join->const_tables; // index into 'join->best_ref'
5851
 
  uint32_t      best_idx;
5852
 
  uint32_t      size_remain;    // cardinality of remaining_tables
 
5858
  uint      idx= join->const_tables; // index into 'join->best_ref'
 
5859
  uint      best_idx;
 
5860
  uint      size_remain;    // cardinality of remaining_tables
5853
5861
  POSITION  best_pos;
5854
5862
  JOIN_TAB  *best_table; // the next plan node to be added to the curr QEP
5855
5863
 
6022
6030
static bool
6023
6031
best_extension_by_limited_search(JOIN      *join,
6024
6032
                                 table_map remaining_tables,
6025
 
                                 uint32_t      idx,
 
6033
                                 uint      idx,
6026
6034
                                 double    record_count,
6027
6035
                                 double    read_time,
6028
 
                                 uint32_t      search_depth,
6029
 
                                 uint32_t      prune_level)
 
6036
                                 uint      search_depth,
 
6037
                                 uint      prune_level)
6030
6038
{
6031
6039
  THD *thd= join->thd;
6032
6040
  if (thd->killed)  // Abort
6150
6158
    true        Fatal error
6151
6159
*/
6152
6160
static bool
6153
 
find_best(JOIN *join,table_map rest_tables,uint32_t idx,double record_count,
 
6161
find_best(JOIN *join,table_map rest_tables,uint idx,double record_count,
6154
6162
          double read_time)
6155
6163
{
6156
6164
  THD *thd= join->thd;
6227
6235
static void calc_used_field_length(THD *thd __attribute__((unused)),
6228
6236
                                   JOIN_TAB *join_tab)
6229
6237
{
6230
 
  uint32_t null_fields,blobs,fields,rec_length;
 
6238
  uint null_fields,blobs,fields,rec_length;
6231
6239
  Field **f_ptr,*field;
6232
6240
  MY_BITMAP *read_set= join_tab->table->read_set;;
6233
6241
 
6236
6244
  {
6237
6245
    if (bitmap_is_set(read_set, field->field_index))
6238
6246
    {
6239
 
      uint32_t flags=field->flags;
 
6247
      uint flags=field->flags;
6240
6248
      fields++;
6241
6249
      rec_length+=field->pack_length();
6242
6250
      if (flags & BLOB_FLAG)
6251
6259
    rec_length+=sizeof(bool);
6252
6260
  if (blobs)
6253
6261
  {
6254
 
    uint32_t blob_length=(uint) (join_tab->table->file->stats.mean_rec_length-
 
6262
    uint blob_length=(uint) (join_tab->table->file->stats.mean_rec_length-
6255
6263
                             (join_tab->table->getRecordLength()- rec_length));
6256
 
    rec_length+=(uint) cmax((uint)4,blob_length);
 
6264
    rec_length+=(uint) max((uint)4,blob_length);
6257
6265
  }
6258
6266
  join_tab->used_fields=fields;
6259
6267
  join_tab->used_fieldlength=rec_length;
6262
6270
 
6263
6271
 
6264
6272
static uint
6265
 
cache_record_length(JOIN *join,uint32_t idx)
 
6273
cache_record_length(JOIN *join,uint idx)
6266
6274
{
6267
 
  uint32_t length=0;
 
6275
  uint length=0;
6268
6276
  JOIN_TAB **pos,**end;
6269
6277
  THD *thd=join->thd;
6270
6278
 
6333
6341
*/
6334
6342
 
6335
6343
static double
6336
 
prev_record_reads(JOIN *join, uint32_t idx, table_map found_ref)
 
6344
prev_record_reads(JOIN *join, uint idx, table_map found_ref)
6337
6345
{
6338
6346
  double found=1.0;
6339
6347
  POSITION *pos_end= join->positions - 1;
6373
6381
static bool
6374
6382
get_best_combination(JOIN *join)
6375
6383
{
6376
 
  uint32_t i,tablenr;
 
6384
  uint i,tablenr;
6377
6385
  table_map used_tables;
6378
6386
  JOIN_TAB *join_tab,*j;
6379
6387
  KEYUSE *keyuse;
6380
 
  uint32_t table_count;
 
6388
  uint table_count;
6381
6389
  THD *thd=join->thd;
6382
6390
 
6383
6391
  table_count=join->tables;
6427
6435
{
6428
6436
  KEYUSE *keyuse=org_keyuse;
6429
6437
  THD  *thd= join->thd;
6430
 
  uint32_t keyparts,length,key;
 
6438
  uint keyparts,length,key;
6431
6439
  Table *table;
6432
6440
  KEY *keyinfo;
6433
6441
 
6438
6446
 
6439
6447
  {
6440
6448
    keyparts=length=0;
6441
 
    uint32_t found_part_ref_or_null= 0;
 
6449
    uint found_part_ref_or_null= 0;
6442
6450
    /*
6443
6451
      Calculate length for the used key
6444
6452
      Stop if there is a missing key part or when we find second key_part
6465
6473
  j->ref.key_parts=keyparts;
6466
6474
  j->ref.key_length=length;
6467
6475
  j->ref.key=(int) key;
6468
 
  if (!(j->ref.key_buff= (unsigned char*) thd->calloc(ALIGN_SIZE(length)*2)) ||
 
6476
  if (!(j->ref.key_buff= (uchar*) thd->calloc(ALIGN_SIZE(length)*2)) ||
6469
6477
      !(j->ref.key_copy= (store_key**) thd->alloc((sizeof(store_key*) *
6470
6478
                                                   (keyparts+1)))) ||
6471
6479
      !(j->ref.items=    (Item**) thd->alloc(sizeof(Item*)*keyparts)) ||
6480
6488
  keyuse=org_keyuse;
6481
6489
 
6482
6490
  store_key **ref_key= j->ref.key_copy;
6483
 
  unsigned char *key_buff=j->ref.key_buff, *null_ref_key= 0;
 
6491
  uchar *key_buff=j->ref.key_buff, *null_ref_key= 0;
6484
6492
  bool keyuse_uses_no_tables= true;
6485
6493
  {
6486
 
    uint32_t i;
 
6494
    uint i;
6487
6495
    for (i=0 ; i < keyparts ; keyuse++,i++)
6488
6496
    {
6489
6497
      while (keyuse->keypart != i ||
6490
6498
             ((~used_tables) & keyuse->used_tables))
6491
6499
        keyuse++;                               /* Skip other parts */
6492
6500
 
6493
 
      uint32_t maybe_null= test(keyinfo->key_part[i].null_bit);
 
6501
      uint maybe_null= test(keyinfo->key_part[i].null_bit);
6494
6502
      j->ref.items[i]=keyuse->val;              // Save for cond removal
6495
6503
      j->ref.cond_guards[i]= keyuse->cond_guard;
6496
6504
      if (keyuse->null_rejecting) 
6552
6560
 
6553
6561
static store_key *
6554
6562
get_store_key(THD *thd, KEYUSE *keyuse, table_map used_tables,
6555
 
              KEY_PART_INFO *key_part, unsigned char *key_buff, uint32_t maybe_null)
 
6563
              KEY_PART_INFO *key_part, uchar *key_buff, uint maybe_null)
6556
6564
{
6557
6565
  if (!((~used_tables) & keyuse->used_tables))          // if const item
6558
6566
  {
6749
6757
 
6750
6758
static void add_not_null_conds(JOIN *join)
6751
6759
{
6752
 
  for (uint32_t i=join->const_tables ; i < join->tables ; i++)
 
6760
  for (uint i=join->const_tables ; i < join->tables ; i++)
6753
6761
  {
6754
6762
    JOIN_TAB *tab=join->join_tab+i;
6755
6763
    if ((tab->type == JT_REF || tab->type == JT_EQ_REF || 
6756
6764
         tab->type == JT_REF_OR_NULL) &&
6757
6765
        !tab->table->maybe_null)
6758
6766
    {
6759
 
      for (uint32_t keypart= 0; keypart < tab->ref.key_parts; keypart++)
 
6767
      for (uint keypart= 0; keypart < tab->ref.key_parts; keypart++)
6760
6768
      {
6761
6769
        if (tab->ref.null_rejecting & (1 << keypart))
6762
6770
        {
6869
6877
static void
6870
6878
make_outerjoin_info(JOIN *join)
6871
6879
{
6872
 
  for (uint32_t i=join->const_tables ; i < join->tables ; i++)
 
6880
  for (uint i=join->const_tables ; i < join->tables ; i++)
6873
6881
  {
6874
6882
    JOIN_TAB *tab=join->join_tab+i;
6875
6883
    Table *table=tab->table;
6971
6979
    }
6972
6980
    used_tables=((select->const_tables=join->const_table_map) |
6973
6981
                 OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
6974
 
    for (uint32_t i=join->const_tables ; i < join->tables ; i++)
 
6982
    for (uint i=join->const_tables ; i < join->tables ; i++)
6975
6983
    {
6976
6984
      JOIN_TAB *tab=join->join_tab+i;
6977
6985
      /*
7040
7048
          tab->type == JT_EQ_REF)
7041
7049
      {
7042
7050
        SQL_SELECT *sel= tab->select= ((SQL_SELECT*)
7043
 
                                       thd->memdup((unsigned char*) select,
 
7051
                                       thd->memdup((uchar*) select,
7044
7052
                                                   sizeof(*select)));
7045
7053
        if (!sel)
7046
7054
          return(1);                    // End of memory
7095
7103
          }
7096
7104
          tab->quick=0;
7097
7105
        }
7098
 
        uint32_t ref_key=(uint) sel->head->reginfo.join_tab->ref.key+1;
 
7106
        uint ref_key=(uint) sel->head->reginfo.join_tab->ref.key+1;
7099
7107
        if (i == join->const_tables && ref_key)
7100
7108
        {
7101
7109
          if (!tab->const_keys.is_clear_all() &&
7187
7195
                                         current_map, 0)))
7188
7196
            {
7189
7197
              tab->cache.select=(SQL_SELECT*)
7190
 
                thd->memdup((unsigned char*) sel, sizeof(SQL_SELECT));
 
7198
                thd->memdup((uchar*) sel, sizeof(SQL_SELECT));
7191
7199
              tab->cache.select->cond=tmp;
7192
7200
              tab->cache.select->read_tables=join->const_table_map;
7193
7201
            }
7305
7313
    false  No
7306
7314
*/
7307
7315
 
7308
 
bool uses_index_fields_only(Item *item, Table *tbl, uint32_t keyno, 
 
7316
bool uses_index_fields_only(Item *item, Table *tbl, uint keyno, 
7309
7317
                            bool other_tbls_ok)
7310
7318
{
7311
7319
  if (item->const_item())
7400
7408
    Index condition, or NULL if no condition could be inferred.
7401
7409
*/
7402
7410
 
7403
 
Item *make_cond_for_index(Item *cond, Table *table, uint32_t keyno,
 
7411
Item *make_cond_for_index(Item *cond, Table *table, uint keyno,
7404
7412
                          bool other_tbls_ok)
7405
7413
{
7406
7414
  if (!cond)
7407
7415
    return NULL;
7408
7416
  if (cond->type() == Item::COND_ITEM)
7409
7417
  {
7410
 
    uint32_t n_marked= 0;
 
7418
    uint n_marked= 0;
7411
7419
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
7412
7420
    {
7413
7421
      Item_cond_and *new_cond=new Item_cond_and;
7539
7547
    Try to extract and push the index condition down to table handler
7540
7548
*/
7541
7549
 
7542
 
static void push_index_cond(JOIN_TAB *tab, uint32_t keyno, bool other_tbls_ok)
 
7550
static void push_index_cond(JOIN_TAB *tab, uint keyno, bool other_tbls_ok)
7543
7551
{
7544
7552
  Item *idx_cond;
7545
7553
  if (tab->table->file->index_flags(keyno, 0, 1) & HA_DO_INDEX_COND_PUSHDOWN &&
7601
7609
      ordered. If there is a temp table the ordering is done as a last
7602
7610
      operation and doesn't prevent join cache usage.
7603
7611
    */
7604
 
uint32_t make_join_orderinfo(JOIN *join)
 
7612
uint make_join_orderinfo(JOIN *join)
7605
7613
{
7606
 
  uint32_t i;
 
7614
  uint i;
7607
7615
  if (join->need_tmp)
7608
7616
    return join->tables;
7609
7617
 
7645
7653
*/
7646
7654
 
7647
7655
static bool
7648
 
make_join_readinfo(JOIN *join, uint64_t options, uint32_t no_jbuf_after)
 
7656
make_join_readinfo(JOIN *join, uint64_t options, uint no_jbuf_after)
7649
7657
{
7650
 
  uint32_t i;
 
7658
  uint i;
7651
7659
  bool statistics= test(!(join->select_options & SELECT_DESCRIBE));
7652
7660
  bool sorted= 1;
7653
7661
 
7667
7675
    sorted= 0;                                  // only first must be sorted
7668
7676
    if (tab->insideout_match_tab)
7669
7677
    {
7670
 
      if (!(tab->insideout_buf= (unsigned char*)join->thd->alloc(tab->table->key_info
 
7678
      if (!(tab->insideout_buf= (uchar*)join->thd->alloc(tab->table->key_info
7671
7679
                                                         [tab->index].
7672
7680
                                                         key_length)))
7673
7681
        return true;
7883
7891
  select= 0;
7884
7892
  delete quick;
7885
7893
  quick= 0;
7886
 
  if (cache.buff)
7887
 
    free(cache.buff);
 
7894
  x_free(cache.buff);
7888
7895
  cache.buff= 0;
7889
7896
  limit= 0;
7890
7897
  if (table)
7938
7945
    a correlated subquery itself, but has subqueries, we can free it
7939
7946
    fully and also free JOINs of all its subqueries. The exception
7940
7947
    is a subquery in SELECT list, e.g: @n
7941
 
    SELECT a, (select cmax(b) from t1) group by c @n
 
7948
    SELECT a, (select max(b) from t1) group by c @n
7942
7949
    This subquery will not be evaluated at first sweep and its value will
7943
7950
    not be inserted into the temporary table. Instead, it's evaluated
7944
7951
    when selecting from the temporary table. Therefore, it can't be freed
8114
8121
    return (tab->eq_ref_table=0);               // We must use this
8115
8122
  Item **ref_item=tab->ref.items;
8116
8123
  Item **end=ref_item+tab->ref.key_parts;
8117
 
  uint32_t found=0;
 
8124
  uint found=0;
8118
8125
  table_map map=tab->table->map;
8119
8126
 
8120
8127
  for (; ref_item != end ; ref_item++)
8176
8183
    TABLE_REF *ref= &join_tab->ref;
8177
8184
    table_map depend_map=0;
8178
8185
    Item **item=ref->items;
8179
 
    uint32_t i;
 
8186
    uint i;
8180
8187
    for (i=0 ; i < ref->key_parts ; i++,item++)
8181
8188
      depend_map|=(*item)->used_tables();
8182
8189
    ref->depend_map=depend_map & ~OUTER_REF_TABLE_BIT;
8346
8353
    must clear only the non-const tables, as const tables
8347
8354
    are not re-calculated.
8348
8355
  */
8349
 
  for (uint32_t i=join->const_tables ; i < join->tables ; i++)
 
8356
  for (uint i=join->const_tables ; i < join->tables ; i++)
8350
8357
    mark_as_null_row(join->table[i]);           // All fields are NULL
8351
8358
}
8352
8359
 
8710
8717
static bool check_row_equality(THD *thd, Item *left_row, Item_row *right_row,
8711
8718
                               COND_EQUAL *cond_equal, List<Item>* eq_list)
8712
8719
8713
 
  uint32_t n= left_row->cols();
8714
 
  for (uint32_t i= 0 ; i < n; i++)
 
8720
  uint n= left_row->cols();
 
8721
  for (uint i= 0 ; i < n; i++)
8715
8722
  {
8716
8723
    bool is_converted;
8717
8724
    Item *left_item= left_row->element_index(i);
8998
9005
      as soon the field is not of a string type or the field reference is
8999
9006
      an argument of a comparison predicate. 
9000
9007
    */ 
9001
 
    unsigned char *is_subst_valid= (unsigned char *) 1;
 
9008
    uchar *is_subst_valid= (uchar *) 1;
9002
9009
    cond= cond->compile(&Item::subst_argument_checker,
9003
9010
                        &is_subst_valid, 
9004
9011
                        &Item::equal_fields_propagator,
9005
 
                        (unsigned char *) inherited);
 
9012
                        (uchar *) inherited);
9006
9013
    cond->update_used_tables();
9007
9014
  }
9008
9015
  return cond;
9938
9945
    First unused bit in nested_join_map after the call.
9939
9946
*/
9940
9947
 
9941
 
static uint32_t build_bitmap_for_nested_joins(List<TableList> *join_list, 
9942
 
                                          uint32_t first_unused)
 
9948
static uint build_bitmap_for_nested_joins(List<TableList> *join_list, 
 
9949
                                          uint first_unused)
9943
9950
{
9944
9951
  List_iterator<TableList> li(*join_list);
9945
9952
  TableList *table;
10270
10277
        li.remove();
10271
10278
      else if (item != new_item)
10272
10279
      {
10273
 
        li.replace(new_item);
 
10280
        VOID(li.replace(new_item));
10274
10281
        should_fix_fields=1;
10275
10282
      }
10276
10283
      if (*cond_value == Item::COND_UNDEF)
10595
10602
 
10596
10603
  if (table)
10597
10604
  {
10598
 
    table->file->extra(HA_EXTRA_WRITE_CACHE);
 
10605
    VOID(table->file->extra(HA_EXTRA_WRITE_CACHE));
10599
10606
    empty_record(table);
10600
10607
    if (table->group && join->tmp_table_param.sum_func_count &&
10601
10608
        table->s->keys && !table->file->inited)
10923
10930
  int error;
10924
10931
  SJ_TMP_TABLE::TAB *tab= sjtbl->tabs;
10925
10932
  SJ_TMP_TABLE::TAB *tab_end= sjtbl->tabs_end;
10926
 
  unsigned char *ptr= sjtbl->tmp_table->record[0] + 1;
10927
 
  unsigned char *nulls_ptr= ptr;
 
10933
  uchar *ptr= sjtbl->tmp_table->record[0] + 1;
 
10934
  uchar *nulls_ptr= ptr;
10928
10935
  
10929
10936
  /* Put the the rowids tuple into table->record[0]: */
10930
10937
 
10931
10938
  // 1. Store the length 
10932
10939
  if (((Field_varstring*)(sjtbl->tmp_table->field[0]))->length_bytes == 1)
10933
10940
  {
10934
 
    *ptr= (unsigned char)(sjtbl->rowid_len + sjtbl->null_bytes);
 
10941
    *ptr= (uchar)(sjtbl->rowid_len + sjtbl->null_bytes);
10935
10942
    ptr++;
10936
10943
  }
10937
10944
  else
10948
10955
  }
10949
10956
 
10950
10957
  // 3. Put the rowids
10951
 
  for (uint32_t i=0; tab != tab_end; tab++, i++)
 
10958
  for (uint i=0; tab != tab_end; tab++, i++)
10952
10959
  {
10953
10960
    handler *h= tab->join_tab->table->file;
10954
10961
    if (tab->join_tab->table->maybe_null && tab->join_tab->table->null_row)
11249
11256
    if (rc == NESTED_LOOP_OK &&
11250
11257
        (!join_tab->cache.select || !join_tab->cache.select->skip_record()))
11251
11258
    {
11252
 
      uint32_t i;
 
11259
      uint i;
11253
11260
      reset_cache_read(&join_tab->cache);
11254
11261
      for (i=(join_tab->cache.records- (skip_last ? 1 : 0)) ; i-- > 0 ;)
11255
11262
      {
11392
11399
      empty_record(table);                      // Make empty record
11393
11400
      return -1;
11394
11401
    }
11395
 
    update_virtual_fields_marked_for_write(table);
11396
11402
    store_record(table,record[1]);
11397
11403
  }
11398
11404
  else if (!table->status)                      // Only happens with left join
11428
11434
    else
11429
11435
    {
11430
11436
      error=table->file->index_read_idx_map(table->record[0],tab->ref.key,
11431
 
                                            (unsigned char*) tab->ref.key_buff,
 
11437
                                            (uchar*) tab->ref.key_buff,
11432
11438
                                            make_prev_keypart_map(tab->ref.key_parts),
11433
11439
                                            HA_READ_KEY_EXACT);
11434
11440
    }
11441
11447
        return table->report_error(error);
11442
11448
      return -1;
11443
11449
    }
11444
 
    update_virtual_fields_marked_for_write(table);
11445
11450
    store_record(table,record[1]);
11446
11451
  }
11447
11452
  else if (!(table->status & ~STATUS_NULL_ROW)) // Only happens with left join
11533
11538
    table->file->ha_index_init(tab->ref.key, tab->sorted);
11534
11539
 
11535
11540
  /* Perform "Late NULLs Filtering" (see internals manual for explanations) */
11536
 
  for (uint32_t i= 0 ; i < tab->ref.key_parts ; i++)
 
11541
  for (uint i= 0 ; i < tab->ref.key_parts ; i++)
11537
11542
  {
11538
11543
    if ((tab->ref.null_rejecting & 1 << i) && tab->ref.items[i]->is_null())
11539
11544
        return -1;
11550
11555
      return table->report_error(error);
11551
11556
    return -1; /* purecov: inspected */
11552
11557
  }
11553
 
  update_virtual_fields_marked_for_write(table);
11554
11558
  return 0;
11555
11559
}
11556
11560
 
11637
11641
    table->status= STATUS_GARBAGE;
11638
11642
    return -1;
11639
11643
  }
11640
 
  update_virtual_fields_marked_for_write(table);
11641
11644
  return 0;
11642
11645
}
11643
11646
 
11657
11660
    table->status=STATUS_NOT_FOUND;
11658
11661
    error= -1;
11659
11662
  }
11660
 
  update_virtual_fields_marked_for_write(table);
11661
11663
  return error;
11662
11664
}
11663
11665
 
11738
11740
      table->report_error(error);
11739
11741
    return -1;
11740
11742
  }
11741
 
  if (not error)
11742
 
    update_virtual_fields_marked_for_write(tab->table);
11743
11743
  return 0;
11744
11744
}
11745
11745
 
11759
11759
 
11760
11760
      if ((error=info->file->index_next(info->record)))
11761
11761
        return info->table->report_error(error);
11762
 
      if (not error)
11763
 
        update_virtual_fields_marked_for_write(tab->table);
 
11762
      
11764
11763
    } while (!key_cmp(tab->table->key_info[tab->index].key_part, 
11765
11764
                      tab->insideout_buf, key->key_length));
11766
11765
    tab->insideout_match_tab->found_match= 0;
11777
11776
  int error;
11778
11777
  if ((error=info->file->index_next(info->record)))
11779
11778
    return info->table->report_error(error);
11780
 
  if (not error)
11781
 
    update_virtual_fields_marked_for_write(info->table);
11782
11779
  return 0;
11783
11780
}
11784
11781
 
11804
11801
    table->file->ha_index_init(tab->index, 1);
11805
11802
  if ((error= tab->table->file->index_last(tab->table->record[0])))
11806
11803
    return table->report_error(error);
11807
 
  if (not error)
11808
 
    update_virtual_fields_marked_for_write(tab->table);
11809
11804
  return 0;
11810
11805
}
11811
11806
 
11816
11811
  int error;
11817
11812
  if ((error= info->file->index_prev(info->record)))
11818
11813
    return info->table->report_error(error);
11819
 
  if (not error)
11820
 
    update_virtual_fields_marked_for_write(info->table);
11821
11814
  return 0;
11822
11815
}
11823
11816
 
12022
12015
      if (end_of_records)
12023
12016
        return(NESTED_LOOP_OK);
12024
12017
      join->first_record=1;
12025
 
      test_if_item_cache_changed(join->group_fields);
 
12018
      VOID(test_if_item_cache_changed(join->group_fields));
12026
12019
    }
12027
12020
    if (idx < (int) join->send_group_parts)
12028
12021
    {
12069
12062
        if (item->maybe_null)
12070
12063
        {
12071
12064
          Field *field=item->get_tmp_table_field();
12072
 
          field->ptr[-1]= (unsigned char) (field->is_null() ? 1 : 0);
 
12065
          field->ptr[-1]= (uchar) (field->is_null() ? 1 : 0);
12073
12066
        }
12074
12067
      }
12075
12068
    }
12280
12273
      if (end_of_records)
12281
12274
        return(NESTED_LOOP_OK);
12282
12275
      join->first_record=1;
12283
 
      test_if_item_cache_changed(join->group_fields);
 
12276
      VOID(test_if_item_cache_changed(join->group_fields));
12284
12277
    }
12285
12278
    if (idx < (int) join->send_group_parts)
12286
12279
    {
12547
12540
  if (!table->reginfo.join_tab)
12548
12541
    return (Item*) 0;             // field from outer non-select (UPDATE,...)
12549
12542
 
12550
 
  uint32_t ref_parts=table->reginfo.join_tab->ref.key_parts;
 
12543
  uint ref_parts=table->reginfo.join_tab->ref.key_parts;
12551
12544
  if (ref_parts)
12552
12545
  {
12553
12546
    KEY_PART_INFO *key_part=
12554
12547
      table->key_info[table->reginfo.join_tab->ref.key].key_part;
12555
 
    uint32_t part;
 
12548
    uint part;
12556
12549
 
12557
12550
    for (part=0 ; part < ref_parts ; part++)
12558
12551
    {
12700
12693
*/
12701
12694
 
12702
12695
static uint
12703
 
test_if_subkey(order_st *order, Table *table, uint32_t ref, uint32_t ref_key_parts,
 
12696
test_if_subkey(order_st *order, Table *table, uint ref, uint ref_key_parts,
12704
12697
               const key_map *usable_keys)
12705
12698
{
12706
12699
  uint32_t nr;
12763
12756
list_contains_unique_index(Table *table,
12764
12757
                          bool (*find_func) (Field *, void *), void *data)
12765
12758
{
12766
 
  for (uint32_t keynr= 0; keynr < table->s->keys; keynr++)
 
12759
  for (uint keynr= 0; keynr < table->s->keys; keynr++)
12767
12760
  {
12768
12761
    if (keynr == table->s->primary_key ||
12769
12762
         (table->key_info[keynr].flags & HA_NOSAME))
12887
12880
                        bool no_changes, const key_map *map)
12888
12881
{
12889
12882
  int32_t ref_key;
12890
 
  uint32_t ref_key_parts;
 
12883
  uint ref_key_parts;
12891
12884
  int order_direction;
12892
12885
  uint32_t used_key_parts;
12893
12886
  Table *table=tab->table;
12951
12944
      /*
12952
12945
        We come here when ref_key is not among usable_keys
12953
12946
      */
12954
 
      uint32_t new_ref_key;
 
12947
      uint new_ref_key;
12955
12948
      /*
12956
12949
        If using index only read, only consider other possible index only
12957
12950
        keys
13021
13014
      or a table scan.
13022
13015
      It may be the case if order_st/GROUP BY is used with LIMIT.
13023
13016
    */
13024
 
    uint32_t nr;
 
13017
    uint nr;
13025
13018
    key_map keys;
13026
 
    uint32_t best_key_parts= 0;
 
13019
    uint best_key_parts= 0;
13027
13020
    int best_key_direction= 0;
13028
13021
    ha_rows best_records= 0;
13029
13022
    double read_time;
13031
13024
    bool is_best_covering= false;
13032
13025
    double fanout= 1;
13033
13026
    JOIN *join= tab->join;
13034
 
    uint32_t tablenr= tab - join->join_tab;
 
13027
    uint tablenr= tab - join->join_tab;
13035
13028
    ha_rows table_records= table->file->stats.records;
13036
13029
    bool group= join->group && order == join->group_list;
13037
13030
 
13065
13058
      keys= usable_keys;
13066
13059
 
13067
13060
    read_time= join->best_positions[tablenr].read_time;
13068
 
    for (uint32_t i= tablenr+1; i < join->tables; i++)
 
13061
    for (uint i= tablenr+1; i < join->tables; i++)
13069
13062
      fanout*= join->best_positions[i].records_read; // fanout is always >= 1
13070
13063
 
13071
13064
    for (nr=0; nr < table->s->keys ; nr++)
13150
13143
            index entry.
13151
13144
          */
13152
13145
          index_scan_time= select_limit/rec_per_key *
13153
 
                           cmin(rec_per_key, table->file->scan_time());
 
13146
                           min(rec_per_key, table->file->scan_time());
13154
13147
          if (is_covering || (ref_key < 0 && (group || table->force_index)) ||
13155
13148
              index_scan_time < read_time)
13156
13149
          {
13160
13153
            if (table->quick_keys.is_set(nr))
13161
13154
              quick_records= table->quick_rows[nr];
13162
13155
            if (best_key < 0 ||
13163
 
                (select_limit <= cmin(quick_records,best_records) ?
 
13156
                (select_limit <= min(quick_records,best_records) ?
13164
13157
                 keyinfo->key_parts < best_key_parts :
13165
13158
                 quick_records < best_records))
13166
13159
            {
13331
13324
                  ha_rows filesort_limit, ha_rows select_limit,
13332
13325
                  bool is_order_by)
13333
13326
{
13334
 
  uint32_t length= 0;
 
13327
  uint length= 0;
13335
13328
  ha_rows examined_rows;
13336
13329
  Table *table;
13337
13330
  SQL_SELECT *select;
13455
13448
{
13456
13449
  int error;
13457
13450
  ulong reclength,offset;
13458
 
  uint32_t field_count;
 
13451
  uint field_count;
13459
13452
  THD *thd= join->thd;
13460
13453
 
13461
13454
  entry->reginfo.lock_type=TL_WRITE;
13504
13497
{
13505
13498
  handler *file=table->file;
13506
13499
  char *org_record,*new_record;
13507
 
  unsigned char *record;
 
13500
  uchar *record;
13508
13501
  int error;
13509
13502
  ulong reclength= table->s->reclength-offset;
13510
13503
 
13591
13584
*/
13592
13585
 
13593
13586
static int remove_dup_with_hash_index(THD *thd, Table *table,
13594
 
                                      uint32_t field_count,
 
13587
                                      uint field_count,
13595
13588
                                      Field **first_field,
13596
13589
                                      ulong key_length,
13597
13590
                                      Item *having)
13598
13591
{
13599
 
  unsigned char *key_buffer, *key_pos, *record=table->record[0];
 
13592
  uchar *key_buffer, *key_pos, *record=table->record[0];
13600
13593
  int error;
13601
13594
  handler *file= table->file;
13602
13595
  ulong extra_length= ALIGN_SIZE(key_length)-key_length;
13603
 
  uint32_t *field_lengths,*field_length;
 
13596
  uint *field_lengths,*field_length;
13604
13597
  HASH hash;
13605
13598
 
13606
13599
  if (!my_multi_malloc(MYF(MY_WME),
13609
13602
                               (long) file->stats.records),
13610
13603
                       &field_lengths,
13611
13604
                       (uint) (field_count*sizeof(*field_lengths)),
13612
 
                       NULL))
 
13605
                       NullS))
13613
13606
    return(1);
13614
13607
 
13615
13608
  {
13617
13610
    ulong total_length= 0;
13618
13611
    for (ptr= first_field, field_length=field_lengths ; *ptr ; ptr++)
13619
13612
    {
13620
 
      uint32_t length= (*ptr)->sort_length();
 
13613
      uint length= (*ptr)->sort_length();
13621
13614
      (*field_length++)= length;
13622
13615
      total_length+= length;
13623
13616
    }
13629
13622
  if (hash_init(&hash, &my_charset_bin, (uint) file->stats.records, 0, 
13630
13623
                key_length, (hash_get_key) 0, 0, 0))
13631
13624
  {
13632
 
    free((char*) key_buffer);
 
13625
    my_free((char*) key_buffer,MYF(0));
13633
13626
    return(1);
13634
13627
  }
13635
13628
 
13637
13630
  key_pos=key_buffer;
13638
13631
  for (;;)
13639
13632
  {
13640
 
    unsigned char *org_key_pos;
 
13633
    uchar *org_key_pos;
13641
13634
    if (thd->killed)
13642
13635
    {
13643
13636
      thd->send_kill_message();
13678
13671
      (void) my_hash_insert(&hash, org_key_pos);
13679
13672
    key_pos+=extra_length;
13680
13673
  }
13681
 
  free((char*) key_buffer);
 
13674
  my_free((char*) key_buffer,MYF(0));
13682
13675
  hash_free(&hash);
13683
13676
  file->extra(HA_EXTRA_NO_CACHE);
13684
13677
  (void) file->ha_rnd_end();
13685
13678
  return(0);
13686
13679
 
13687
13680
err:
13688
 
  free((char*) key_buffer);
 
13681
  my_free((char*) key_buffer,MYF(0));
13689
13682
  hash_free(&hash);
13690
13683
  file->extra(HA_EXTRA_NO_CACHE);
13691
13684
  (void) file->ha_rnd_end();
13695
13688
}
13696
13689
 
13697
13690
 
13698
 
SORT_FIELD *make_unireg_sortorder(order_st *order, uint32_t *length,
 
13691
SORT_FIELD *make_unireg_sortorder(order_st *order, uint *length,
13699
13692
                                  SORT_FIELD *sortorder)
13700
13693
{
13701
 
  uint32_t count;
 
13694
  uint count;
13702
13695
  SORT_FIELD *sort,*pos;
13703
13696
 
13704
13697
  count=0;
13706
13699
    count++;
13707
13700
  if (!sortorder)
13708
13701
    sortorder= (SORT_FIELD*) sql_alloc(sizeof(SORT_FIELD) *
13709
 
                                       (cmax(count, *length) + 1));
 
13702
                                       (max(count, *length) + 1));
13710
13703
  pos= sort= sortorder;
13711
13704
 
13712
13705
  if (!pos)
13741
13734
******************************************************************************/
13742
13735
 
13743
13736
static int
13744
 
join_init_cache(THD *thd,JOIN_TAB *tables,uint32_t table_count)
 
13737
join_init_cache(THD *thd,JOIN_TAB *tables,uint table_count)
13745
13738
{
13746
13739
  register unsigned int i;
13747
13740
  unsigned int length, blobs;
13773
13766
 
13774
13767
                  sizeof(CACHE_FIELD*))))
13775
13768
  {
13776
 
    free((unsigned char*) cache->buff);         /* purecov: inspected */
 
13769
    my_free((uchar*) cache->buff,MYF(0));               /* purecov: inspected */
13777
13770
    cache->buff=0;                              /* purecov: inspected */
13778
13771
    return(1);                          /* purecov: inspected */
13779
13772
  }
13784
13777
  length=0;
13785
13778
  for (i=0 ; i < table_count ; i++)
13786
13779
  {
13787
 
    uint32_t null_fields=0, used_fields;
 
13780
    uint null_fields=0, used_fields;
13788
13781
    Field **f_ptr,*field;
13789
13782
    MY_BITMAP *read_set= tables[i].table->read_set;
13790
13783
    for (f_ptr=tables[i].table->field,used_fields=tables[i].used_fields ;
13819
13812
    /* If outer join table, copy null_row flag */
13820
13813
    if (tables[i].table->maybe_null)
13821
13814
    {
13822
 
      copy->str= (unsigned char*) &tables[i].table->null_row;
 
13815
      copy->str= (uchar*) &tables[i].table->null_row;
13823
13816
      copy->length=sizeof(tables[i].table->null_row);
13824
13817
      copy->strip=0;
13825
13818
      copy->blob_field=0;
13850
13843
  cache->length=length+blobs*sizeof(char*);
13851
13844
  cache->blobs=blobs;
13852
13845
  *blob_ptr=0;                                  /* End sequentel */
13853
 
  size=cmax(thd->variables.join_buff_size, (ulong)cache->length);
13854
 
  if (!(cache->buff=(unsigned char*) my_malloc(size,MYF(0))))
 
13846
  size=max(thd->variables.join_buff_size, (ulong)cache->length);
 
13847
  if (!(cache->buff=(uchar*) my_malloc(size,MYF(0))))
13855
13848
    return(1);                          /* Don't use cache */ /* purecov: inspected */
13856
13849
  cache->end=cache->buff+size;
13857
13850
  reset_cache_write(cache);
13862
13855
static ulong
13863
13856
used_blob_length(CACHE_FIELD **ptr)
13864
13857
{
13865
 
  uint32_t length,blob_length;
 
13858
  uint length,blob_length;
13866
13859
  for (length=0 ; *ptr ; ptr++)
13867
13860
  {
13868
13861
    (*ptr)->blob_length=blob_length=(*ptr)->blob_field->get_length();
13876
13869
static bool
13877
13870
store_record_in_cache(JOIN_CACHE *cache)
13878
13871
{
13879
 
  uint32_t length;
13880
 
  unsigned char *pos;
 
13872
  uint length;
 
13873
  uchar *pos;
13881
13874
  CACHE_FIELD *copy,*end_field;
13882
13875
  bool last_record;
13883
13876
 
13919
13912
 
13920
13913
      if (copy->strip)
13921
13914
      {
13922
 
        unsigned char *str,*end;
 
13915
        uchar *str,*end;
13923
13916
        for (str=copy->str,end= str+copy->length;
13924
13917
             end > str && end[-1] == ' ' ;
13925
13918
             end--) ;
13959
13952
static void
13960
13953
read_cached_record(JOIN_TAB *tab)
13961
13954
{
13962
 
  unsigned char *pos;
13963
 
  uint32_t length;
 
13955
  uchar *pos;
 
13956
  uint length;
13964
13957
  bool last_record;
13965
13958
  CACHE_FIELD *copy,*end_field;
13966
13959
 
14112
14105
  Item::Type order_item_type;
14113
14106
  Item **select_item; /* The corresponding item from the SELECT clause. */
14114
14107
  Field *from_field;  /* The corresponding field from the FROM clause. */
14115
 
  uint32_t counter;
 
14108
  uint counter;
14116
14109
  enum_resolution_type resolution;
14117
14110
 
14118
14111
  /*
14121
14114
  */
14122
14115
  if (order_item->type() == Item::INT_ITEM && order_item->basic_const_item())
14123
14116
  {                                             /* Order by position */
14124
 
    uint32_t count= (uint) order_item->val_int();
 
14117
    uint count= (uint) order_item->val_int();
14125
14118
    if (!count || count > fields.elements)
14126
14119
    {
14127
14120
      my_error(ER_BAD_FIELD_ERROR, MYF(0),
14225
14218
       thd->is_fatal_error))
14226
14219
    return true; /* Wrong field. */
14227
14220
 
14228
 
  uint32_t el= all_fields.elements;
 
14221
  uint el= all_fields.elements;
14229
14222
  all_fields.push_front(order_item); /* Add new field to field list. */
14230
14223
  ref_pointer_array[el]= order_item;
14231
14224
  order->item= ref_pointer_array + el;
14291
14284
  if (!order)
14292
14285
    return 0;                           /* Everything is ok */
14293
14286
 
14294
 
  uint32_t org_fields=all_fields.elements;
 
14287
  uint org_fields=all_fields.elements;
14295
14288
 
14296
14289
  thd->where="group statement";
14297
14290
  for (ord= order; ord; ord= ord->next)
14474
14467
            param->quick_group=0;                       // UDF SUM function
14475
14468
          param->sum_func_count++;
14476
14469
 
14477
 
          for (uint32_t i=0 ; i < sum_item->arg_count ; i++)
 
14470
          for (uint i=0 ; i < sum_item->arg_count ; i++)
14478
14471
          {
14479
14472
            if (sum_item->args[0]->real_item()->type() == Item::FIELD_ITEM)
14480
14473
              param->field_count++;
14553
14546
static void
14554
14547
calc_group_buffer(JOIN *join,order_st *group)
14555
14548
{
14556
 
  uint32_t key_length=0, parts=0, null_parts=0;
 
14549
  uint key_length=0, parts=0, null_parts=0;
14557
14550
 
14558
14551
  if (group)
14559
14552
    join->group= 1;
14742
14735
setup_copy_fields(THD *thd, TMP_TABLE_PARAM *param,
14743
14736
                  Item **ref_pointer_array,
14744
14737
                  List<Item> &res_selected_fields, List<Item> &res_all_fields,
14745
 
                  uint32_t elements, List<Item> &all_fields)
 
14738
                  uint elements, List<Item> &all_fields)
14746
14739
{
14747
14740
  Item *pos;
14748
14741
  List_iterator_fast<Item> li(all_fields);
14751
14744
  res_all_fields.empty();
14752
14745
  List_iterator_fast<Item> itr(res_all_fields);
14753
14746
  List<Item> extra_funcs;
14754
 
  uint32_t i, border= all_fields.elements - elements;
 
14747
  uint i, border= all_fields.elements - elements;
14755
14748
 
14756
14749
  if (param->field_count && 
14757
14750
      !(copy=param->copy_field= new Copy_field[param->field_count]))
14761
14754
  for (i= 0; (pos= li++); i++)
14762
14755
  {
14763
14756
    Field *field;
14764
 
    unsigned char *tmp;
 
14757
    uchar *tmp;
14765
14758
    Item *real_pos= pos->real_item();
14766
14759
    if (real_pos->type() == Item::FIELD_ITEM)
14767
14760
    {
14806
14799
          another extra byte to not get warnings from purify in
14807
14800
          Field_varstring::val_int
14808
14801
        */
14809
 
        if (!(tmp= (unsigned char*) sql_alloc(field->pack_length()+2)))
 
14802
        if (!(tmp= (uchar*) sql_alloc(field->pack_length()+2)))
14810
14803
          goto err;
14811
14804
        if (copy)
14812
14805
        {
14903
14896
 
14904
14897
bool JOIN::alloc_func_list()
14905
14898
{
14906
 
  uint32_t func_count, group_parts;
 
14899
  uint func_count, group_parts;
14907
14900
 
14908
14901
  func_count= tmp_table_param.sum_func_count;
14909
14902
  /*
14981
14974
  }
14982
14975
  else if (rollup.state == ROLLUP::STATE_NONE)
14983
14976
  {
14984
 
    for (uint32_t i=0 ; i <= send_group_parts ;i++)
 
14977
    for (uint i=0 ; i <= send_group_parts ;i++)
14985
14978
      sum_funcs_end[i]= func;
14986
14979
  }
14987
14980
  else if (rollup.state == ROLLUP::STATE_READY)
15012
15005
change_to_use_tmp_fields(THD *thd, Item **ref_pointer_array,
15013
15006
                         List<Item> &res_selected_fields,
15014
15007
                         List<Item> &res_all_fields,
15015
 
                         uint32_t elements, List<Item> &all_fields)
 
15008
                         uint elements, List<Item> &all_fields)
15016
15009
{
15017
15010
  List_iterator_fast<Item> it(all_fields);
15018
15011
  Item *item_field,*item;
15020
15013
  res_selected_fields.empty();
15021
15014
  res_all_fields.empty();
15022
15015
 
15023
 
  uint32_t i, border= all_fields.elements - elements;
 
15016
  uint i, border= all_fields.elements - elements;
15024
15017
  for (i= 0; (item= it++); i++)
15025
15018
  {
15026
15019
    Field *field;
15091
15084
static bool
15092
15085
change_refs_to_tmp_fields(THD *thd, Item **ref_pointer_array,
15093
15086
                          List<Item> &res_selected_fields,
15094
 
                          List<Item> &res_all_fields, uint32_t elements,
 
15087
                          List<Item> &res_all_fields, uint elements,
15095
15088
                          List<Item> &all_fields)
15096
15089
{
15097
15090
  List_iterator_fast<Item> it(all_fields);
15099
15092
  res_selected_fields.empty();
15100
15093
  res_all_fields.empty();
15101
15094
 
15102
 
  uint32_t i, border= all_fields.elements - elements;
 
15095
  uint i, border= all_fields.elements - elements;
15103
15096
  for (i= 0; (item= it++); i++)
15104
15097
  {
15105
15098
    res_all_fields.push_back(new_item= item->get_tmp_table_item(thd));
15233
15226
  if (!cond)
15234
15227
    return(true);
15235
15228
 
15236
 
  for (uint32_t i=0 ; i < join_tab->ref.key_parts ; i++)
 
15229
  for (uint i=0 ; i < join_tab->ref.key_parts ; i++)
15237
15230
  {
15238
15231
    Field *field=table->field[table->key_info[join_tab->ref.key].key_part[i].
15239
15232
                              fieldnr-1];
15367
15360
 
15368
15361
bool JOIN::rollup_init()
15369
15362
{
15370
 
  uint32_t i,j;
 
15363
  uint i,j;
15371
15364
  Item **ref_array;
15372
15365
 
15373
15366
  tmp_table_param.quick_group= 0;       // Can't create groups in tmp table
15487
15480
{
15488
15481
  List_iterator_fast<Item> it(fields_arg);
15489
15482
  Item *first_field= sel_fields.head();
15490
 
  uint32_t level;
 
15483
  uint level;
15491
15484
 
15492
15485
  /*
15493
15486
    Create field lists for the different levels
15512
15505
 
15513
15506
  for (level=0 ; level < send_group_parts ; level++)
15514
15507
  {
15515
 
    uint32_t i;
15516
 
    uint32_t pos= send_group_parts - level -1;
 
15508
    uint i;
 
15509
    uint pos= send_group_parts - level -1;
15517
15510
    bool real_fields= 0;
15518
15511
    Item *item;
15519
15512
    List_iterator<Item> new_it(rollup.fields[pos]);
15616
15609
    1   If send_data_failed()
15617
15610
*/
15618
15611
 
15619
 
int JOIN::rollup_send_data(uint32_t idx)
 
15612
int JOIN::rollup_send_data(uint idx)
15620
15613
{
15621
 
  uint32_t i;
 
15614
  uint i;
15622
15615
  for (i= send_group_parts ; i-- > idx ; )
15623
15616
  {
15624
15617
    /* Get reference pointers to sum functions in place */
15657
15650
    1   if write_data_failed()
15658
15651
*/
15659
15652
 
15660
 
int JOIN::rollup_write_data(uint32_t idx, Table *table_arg)
 
15653
int JOIN::rollup_write_data(uint idx, Table *table_arg)
15661
15654
{
15662
 
  uint32_t i;
 
15655
  uint i;
15663
15656
  for (i= send_group_parts ; i-- > idx ; )
15664
15657
  {
15665
15658
    /* Get reference pointers to sum functions in place */
15739
15732
                                     join->select_lex->select_number));
15740
15733
    item_list.push_back(new Item_string(join->select_lex->type,
15741
15734
                                        strlen(join->select_lex->type), cs));
15742
 
    for (uint32_t i=0 ; i < 7; i++)
 
15735
    for (uint i=0 ; i < 7; i++)
15743
15736
      item_list.push_back(item_null);
15744
15737
    if (join->thd->lex->describe & DESCRIBE_EXTENDED)
15745
15738
      item_list.push_back(item_null);
15768
15761
    /* table */
15769
15762
    {
15770
15763
      SELECT_LEX *sl= join->unit->first_select();
15771
 
      uint32_t len= 6, lastop= 0;
 
15764
      uint len= 6, lastop= 0;
15772
15765
      memcpy(table_name_buffer, STRING_WITH_LEN("<union"));
15773
15766
      for (; sl && len + lastop + 5 < NAME_LEN; sl= sl->next_select())
15774
15767
      {
15818
15811
  else
15819
15812
  {
15820
15813
    table_map used_tables=0;
15821
 
    for (uint32_t i=0 ; i < join->tables ; i++)
 
15814
    for (uint i=0 ; i < join->tables ; i++)
15822
15815
    {
15823
15816
      JOIN_TAB *tab=join->join_tab+i;
15824
15817
      Table *table=tab->table;
15878
15871
      /* Build "possible_keys" value and add it to item_list */
15879
15872
      if (!tab->keys.is_clear_all())
15880
15873
      {
15881
 
        uint32_t j;
 
15874
        uint j;
15882
15875
        for (j=0 ; j < table->s->keys ; j++)
15883
15876
        {
15884
15877
          if (tab->keys.is_set(j))
15900
15893
      if (tab->ref.key_parts)
15901
15894
      {
15902
15895
        KEY *key_info=table->key_info+ tab->ref.key;
15903
 
        register uint32_t length;
 
15896
        register uint length;
15904
15897
        item_list.push_back(new Item_string(key_info->name,
15905
15898
                                            strlen(key_info->name),
15906
15899
                                            system_charset_info));
15920
15913
      else if (tab->type == JT_NEXT)
15921
15914
      {
15922
15915
        KEY *key_info=table->key_info+ tab->index;
15923
 
        register uint32_t length;
 
15916
        register uint length;
15924
15917
        item_list.push_back(new Item_string(key_info->name,
15925
15918
                                            strlen(key_info->name),cs));
15926
15919
        length= int64_t2str(key_info->key_length, keylen_str_buf, 10) - 
16033
16026
      }
16034
16027
      else
16035
16028
      {
16036
 
        uint32_t keyno= MAX_KEY;
 
16029
        uint keyno= MAX_KEY;
16037
16030
        if (tab->ref.key_parts)
16038
16031
          keyno= tab->ref.key;
16039
16032
        else if (tab->select && tab->select->quick)
16154
16147
          extra.append(STRING_WITH_LEN(")"));
16155
16148
        }
16156
16149
 
16157
 
        for (uint32_t part= 0; part < tab->ref.key_parts; part++)
 
16150
        for (uint part= 0; part < tab->ref.key_parts; part++)
16158
16151
        {
16159
16152
          if (tab->ref.cond_guards[part])
16160
16153
          {
16352
16345
  if (key_name.length)
16353
16346
  {
16354
16347
    if (thd && !my_strnncoll(system_charset_info,
16355
 
                             (const unsigned char *)key_name.str, key_name.length, 
16356
 
                             (const unsigned char *)primary_key_name, 
 
16348
                             (const uchar *)key_name.str, key_name.length, 
 
16349
                             (const uchar *)primary_key_name, 
16357
16350
                             strlen(primary_key_name)))
16358
16351
      str->append(primary_key_name);
16359
16352
    else
16417
16410
      {
16418
16411
        if (alias && alias[0])
16419
16412
        {
16420
 
          my_stpcpy(t_alias_buff, alias);
 
16413
          stpcpy(t_alias_buff, alias);
16421
16414
          my_casedn_str(files_charset_info, t_alias_buff);
16422
16415
          t_alias= t_alias_buff;
16423
16416
        }