~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_select.cc

  • Committer: Brian Aker
  • Date: 2009-05-05 00:40:45 UTC
  • mfrom: (1003.2.7 mordred)
  • Revision ID: brian@gaz-20090505004045-yns4biv63thufj1b
Tags: 2009.05.1005
Merge Monty

Show diffs side-by-side

added added

removed removed

Lines of Context:
49
49
#include "drizzled/index_hint.h"
50
50
 
51
51
#include <string>
 
52
#include <bitset>
 
53
#include <iostream>
52
54
 
53
55
using namespace std;
54
56
 
798
800
static void save_index_subquery_explain_info(JOIN_TAB *join_tab, Item* where)
799
801
{
800
802
  join_tab->packed_info= TAB_INFO_HAVE_VALUE;
801
 
  if (join_tab->table->covering_keys.is_set(join_tab->ref.key))
 
803
  if (join_tab->table->covering_keys.test(join_tab->ref.key))
802
804
    join_tab->packed_info |= TAB_INFO_USING_INDEX;
803
805
  if (where)
804
806
    join_tab->packed_info |= TAB_INFO_USING_WHERE;
2031
2033
  @todo
2032
2034
    When can we have here session->net.report_error not zero?
2033
2035
*/
2034
 
void
2035
 
JOIN::exec()
 
2036
void JOIN::exec()
2036
2037
{
2037
2038
  List<Item> *columns_list= &fields_list;
2038
2039
  int      tmp_error;
2044
2045
  {                                           
2045
2046
    /* Only test of functions */
2046
2047
    if (select_options & SELECT_DESCRIBE)
2047
 
      select_describe(this, false, false, false,
2048
 
                      (zero_result_cause?zero_result_cause:"No tables used"));
 
2048
      select_describe(this, false, false, false, (zero_result_cause?zero_result_cause:"No tables used"));
2049
2049
    else
2050
2050
    {
2051
 
      result->send_fields(*columns_list,
2052
 
                          Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
 
2051
      result->send_fields(*columns_list, Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
2053
2052
      /*
2054
2053
        We have to test for 'conds' here as the WHERE may not be constant
2055
2054
        even if we don't have any tables for prepared statements or if
2097
2096
    return;
2098
2097
  }
2099
2098
 
2100
 
  if ((this->select_lex->options & OPTION_SCHEMA_TABLE) &&
2101
 
      get_schema_tables_result(this, PROCESSED_BY_JOIN_EXEC))
 
2099
  if ((this->select_lex->options & OPTION_SCHEMA_TABLE) && get_schema_tables_result(this, PROCESSED_BY_JOIN_EXEC))
2102
2100
    return;
2103
2101
 
2104
2102
  if (select_options & SELECT_DESCRIBE)
2110
2108
    */
2111
2109
    if (!order && !no_order && (!skip_sort_order || !need_tmp))
2112
2110
    {
2113
 
      /*
2114
 
        Reset 'order' to 'group_list' and reinit variables describing
2115
 
        'order'
2116
 
      */
 
2111
      /* Reset 'order' to 'group_list' and reinit variables describing 'order' */
2117
2112
      order= group_list;
2118
2113
      simple_order= simple_group;
2119
2114
      skip_sort_order= 0;
2120
2115
    }
2121
 
    if (order &&
2122
 
        (order != group_list || !(select_options & SELECT_BIG_RESULT)) &&
2123
 
        (const_tables == tables ||
2124
 
         ((simple_order || skip_sort_order) &&
2125
 
          test_if_skip_sort_order(&join_tab[const_tables], order,
2126
 
                                  select_limit, 0,
2127
 
                                  &join_tab[const_tables].table->
2128
 
                                    keys_in_use_for_query))))
2129
 
      order=0;
 
2116
    if (order && (order != group_list || !(select_options & SELECT_BIG_RESULT)))
 
2117
    {
 
2118
      if (const_tables == tables 
 
2119
        || ((simple_order || skip_sort_order) 
 
2120
          && test_if_skip_sort_order(&join_tab[const_tables], order, select_limit, 0, &join_tab[const_tables].table->keys_in_use_for_query)))
 
2121
      order= 0;
 
2122
    }
2130
2123
    having= tmp_having;
2131
 
    select_describe(this, need_tmp,
2132
 
                    order != 0 && !skip_sort_order,
2133
 
                    select_distinct,
2134
 
                    !tables ? "No tables used" : NULL);
 
2124
    select_describe(this, need_tmp, order != 0 && !skip_sort_order,  select_distinct, !tables ? "No tables used" : NULL);
2135
2125
    return;
2136
2126
  }
2137
2127
 
2163
2153
 
2164
2154
    /* Copy data to the temporary table */
2165
2155
    session->set_proc_info("Copying to tmp table");
2166
 
    if (!curr_join->sort_and_group &&
2167
 
        curr_join->const_tables != curr_join->tables)
 
2156
    if (! curr_join->sort_and_group && curr_join->const_tables != curr_join->tables)
2168
2157
      curr_join->join_tab[curr_join->const_tables].sorted= 0;
2169
2158
    if ((tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
2170
2159
    {
2183
2172
      items1= items0 + all_fields.elements;
2184
2173
      if (sort_and_group || curr_tmp_table->group)
2185
2174
      {
2186
 
        if (change_to_use_tmp_fields(session, items1,
2187
 
                                     tmp_fields_list1, tmp_all_fields1,
2188
 
                                     fields_list.elements, all_fields))
2189
 
          return;
 
2175
        if (change_to_use_tmp_fields(session, items1,
 
2176
                  tmp_fields_list1, tmp_all_fields1,
 
2177
                  fields_list.elements, all_fields))
 
2178
          return;
2190
2179
      }
2191
2180
      else
2192
2181
      {
2193
 
        if (change_refs_to_tmp_fields(session, items1,
2194
 
                                      tmp_fields_list1, tmp_all_fields1,
2195
 
                                      fields_list.elements, all_fields))
2196
 
          return;
 
2182
        if (change_refs_to_tmp_fields(session, items1,
 
2183
                    tmp_fields_list1, tmp_all_fields1,
 
2184
                    fields_list.elements, all_fields))
 
2185
          return;
2197
2186
      }
2198
2187
      curr_join->tmp_all_fields1= tmp_all_fields1;
2199
2188
      curr_join->tmp_fields_list1= tmp_fields_list1;
2205
2194
 
2206
2195
    if (sort_and_group || curr_tmp_table->group)
2207
2196
    {
2208
 
      curr_join->tmp_table_param.field_count+=
2209
 
        curr_join->tmp_table_param.sum_func_count+
2210
 
        curr_join->tmp_table_param.func_count;
2211
 
      curr_join->tmp_table_param.sum_func_count=
2212
 
        curr_join->tmp_table_param.func_count= 0;
 
2197
      curr_join->tmp_table_param.field_count+= curr_join->tmp_table_param.sum_func_count
 
2198
                                             + curr_join->tmp_table_param.func_count;
 
2199
      curr_join->tmp_table_param.sum_func_count= 0;
 
2200
      curr_join->tmp_table_param.func_count= 0;
2213
2201
    }
2214
2202
    else
2215
2203
    {
2216
 
      curr_join->tmp_table_param.field_count+=
2217
 
        curr_join->tmp_table_param.func_count;
 
2204
      curr_join->tmp_table_param.field_count+= curr_join->tmp_table_param.func_count;
2218
2205
      curr_join->tmp_table_param.func_count= 0;
2219
2206
    }
2220
2207
 
2221
2208
    if (curr_tmp_table->group)
2222
2209
    {                                           // Already grouped
2223
2210
      if (!curr_join->order && !curr_join->no_order && !skip_sort_order)
2224
 
        curr_join->order= curr_join->group_list;  /* order by group */
 
2211
        curr_join->order= curr_join->group_list;  /* order by group */
2225
2212
      curr_join->group_list= 0;
2226
2213
    }
2227
2214
 
2233
2220
      like SEC_TO_TIME(SUM(...)).
2234
2221
    */
2235
2222
 
2236
 
    if ((curr_join->group_list && (!test_if_subpart(curr_join->group_list, curr_join->order) || curr_join->select_distinct)) || (curr_join->select_distinct && curr_join->tmp_table_param.using_indirect_summary_function))
 
2223
    if ((curr_join->group_list && (!test_if_subpart(curr_join->group_list, curr_join->order) || curr_join->select_distinct)) 
 
2224
        || (curr_join->select_distinct && curr_join->tmp_table_param.using_indirect_summary_function))
2237
2225
    {                                   /* Must copy to another table */
2238
2226
      /* Free first data from old join */
2239
2227
      curr_join->join_free();
2240
2228
      if (make_simple_join(curr_join, curr_tmp_table))
2241
 
        return;
 
2229
        return;
2242
2230
      calc_group_buffer(curr_join, group_list);
2243
2231
      count_field_types(select_lex, &curr_join->tmp_table_param,
2244
2232
                        curr_join->tmp_all_fields1,
2245
2233
                        curr_join->select_distinct && !curr_join->group_list);
2246
 
      curr_join->tmp_table_param.hidden_field_count=
2247
 
        (curr_join->tmp_all_fields1.elements-
2248
 
         curr_join->tmp_fields_list1.elements);
2249
 
 
 
2234
      curr_join->tmp_table_param.hidden_field_count= curr_join->tmp_all_fields1.elements
 
2235
                                                   - curr_join->tmp_fields_list1.elements;
2250
2236
 
2251
2237
      if (exec_tmp_table2)
2252
 
        curr_tmp_table= exec_tmp_table2;
 
2238
        curr_tmp_table= exec_tmp_table2;
2253
2239
      else
2254
2240
      {
2255
 
        /* group data to new table */
 
2241
        /* group data to new table */
2256
2242
 
2257
2243
        /*
2258
2244
          If the access method is loose index scan then all MIN/MAX
2262
2248
        if (curr_join->join_tab->is_using_loose_index_scan())
2263
2249
          curr_join->tmp_table_param.precomputed_group_by= true;
2264
2250
 
2265
 
        if (!(curr_tmp_table=
2266
 
              exec_tmp_table2= create_tmp_table(session,
2267
 
                                                &curr_join->tmp_table_param,
2268
 
                                                *curr_all_fields,
2269
 
                                                (order_st*) 0,
2270
 
                                                curr_join->select_distinct &&
2271
 
                                                !curr_join->group_list,
2272
 
                                                1, curr_join->select_options,
2273
 
                                                HA_POS_ERROR,
2274
 
                                                (char *) "")))
2275
 
          return;
2276
 
        curr_join->exec_tmp_table2= exec_tmp_table2;
 
2251
        if (!(curr_tmp_table=
 
2252
              exec_tmp_table2= create_tmp_table(session,
 
2253
                                                &curr_join->tmp_table_param,
 
2254
                                                *curr_all_fields,
 
2255
                                                (order_st*) 0,
 
2256
                                                curr_join->select_distinct &&
 
2257
                                                !curr_join->group_list,
 
2258
                                                1, curr_join->select_options,
 
2259
                                                HA_POS_ERROR,
 
2260
                                                (char *) "")))
 
2261
          return;
 
2262
        curr_join->exec_tmp_table2= exec_tmp_table2;
2277
2263
      }
2278
2264
      if (curr_join->group_list)
2279
2265
      {
2280
 
        session->set_proc_info("Creating sort index");
2281
 
        if (curr_join->join_tab == join_tab && save_join_tab())
2282
 
        {
2283
 
          return;
2284
 
        }
2285
 
        if (create_sort_index(session, curr_join, curr_join->group_list,
2286
 
                              HA_POS_ERROR, HA_POS_ERROR, false) ||
2287
 
            make_group_fields(this, curr_join))
2288
 
        {
2289
 
          return;
2290
 
        }
 
2266
        session->set_proc_info("Creating sort index");
 
2267
        if (curr_join->join_tab == join_tab && save_join_tab())
 
2268
        {
 
2269
          return;
 
2270
        }
 
2271
        if (create_sort_index(session, curr_join, curr_join->group_list,
 
2272
                  HA_POS_ERROR, HA_POS_ERROR, false) ||
 
2273
            make_group_fields(this, curr_join))
 
2274
        {
 
2275
          return;
 
2276
        }
2291
2277
        sortorder= curr_join->sortorder;
2292
2278
      }
2293
2279
 
2295
2281
      tmp_error= -1;
2296
2282
      if (curr_join != this)
2297
2283
      {
2298
 
        if (sum_funcs2)
2299
 
        {
2300
 
          curr_join->sum_funcs= sum_funcs2;
2301
 
          curr_join->sum_funcs_end= sum_funcs_end2;
2302
 
        }
2303
 
        else
2304
 
        {
2305
 
          curr_join->alloc_func_list();
2306
 
          sum_funcs2= curr_join->sum_funcs;
2307
 
          sum_funcs_end2= curr_join->sum_funcs_end;
2308
 
        }
 
2284
        if (sum_funcs2)
 
2285
        {
 
2286
          curr_join->sum_funcs= sum_funcs2;
 
2287
          curr_join->sum_funcs_end= sum_funcs_end2;
 
2288
        }
 
2289
        else
 
2290
        {
 
2291
          curr_join->alloc_func_list();
 
2292
          sum_funcs2= curr_join->sum_funcs;
 
2293
          sum_funcs_end2= curr_join->sum_funcs_end;
 
2294
        }
2309
2295
      }
2310
 
      if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
2311
 
                                        1, true))
 
2296
      if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list, 1, true))
2312
2297
        return;
2313
2298
      curr_join->group_list= 0;
2314
 
      if (!curr_join->sort_and_group &&
2315
 
          curr_join->const_tables != curr_join->tables)
 
2299
 
 
2300
      if (!curr_join->sort_and_group && (curr_join->const_tables != curr_join->tables))
2316
2301
        curr_join->join_tab[curr_join->const_tables].sorted= 0;
2317
 
      if (setup_sum_funcs(curr_join->session, curr_join->sum_funcs) ||
2318
 
          (tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
 
2302
      
 
2303
      if (setup_sum_funcs(curr_join->session, curr_join->sum_funcs) 
 
2304
        || (tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
2319
2305
      {
2320
 
        error= tmp_error;
2321
 
        return;
 
2306
        error= tmp_error;
 
2307
        return;
2322
2308
      }
2323
2309
      end_read_record(&curr_join->join_tab->read_record);
2324
2310
      curr_join->const_tables= curr_join->tables; // Mark free for cleanup()
2327
2313
      // No sum funcs anymore
2328
2314
      if (!items2)
2329
2315
      {
2330
 
        items2= items1 + all_fields.elements;
2331
 
        if (change_to_use_tmp_fields(session, items2,
2332
 
                                     tmp_fields_list2, tmp_all_fields2,
2333
 
                                     fields_list.elements, tmp_all_fields1))
2334
 
          return;
2335
 
        curr_join->tmp_fields_list2= tmp_fields_list2;
2336
 
        curr_join->tmp_all_fields2= tmp_all_fields2;
 
2316
        items2= items1 + all_fields.elements;
 
2317
        if (change_to_use_tmp_fields(session, items2,
 
2318
                  tmp_fields_list2, tmp_all_fields2,
 
2319
                  fields_list.elements, tmp_all_fields1))
 
2320
          return;
 
2321
        curr_join->tmp_fields_list2= tmp_fields_list2;
 
2322
        curr_join->tmp_all_fields2= tmp_all_fields2;
2337
2323
      }
2338
2324
      curr_fields_list= &curr_join->tmp_fields_list2;
2339
2325
      curr_all_fields= &curr_join->tmp_all_fields2;
2340
2326
      curr_join->set_items_ref_array(items2);
2341
 
      curr_join->tmp_table_param.field_count+=
2342
 
        curr_join->tmp_table_param.sum_func_count;
 
2327
      curr_join->tmp_table_param.field_count+= curr_join->tmp_table_param.sum_func_count;
2343
2328
      curr_join->tmp_table_param.sum_func_count= 0;
2344
2329
    }
2345
2330
    if (curr_tmp_table->distinct)
2350
2335
    {
2351
2336
      session->set_proc_info("Removing duplicates");
2352
2337
      if (curr_join->tmp_having)
2353
 
        curr_join->tmp_having->update_used_tables();
 
2338
        curr_join->tmp_having->update_used_tables();
 
2339
 
2354
2340
      if (remove_duplicates(curr_join, curr_tmp_table,
2355
2341
                            *curr_fields_list, curr_join->tmp_having))
2356
 
        return;
 
2342
        return;
 
2343
      
2357
2344
      curr_join->tmp_having=0;
2358
2345
      curr_join->select_distinct=0;
2359
2346
    }
2361
2348
    if (make_simple_join(curr_join, curr_tmp_table))
2362
2349
      return;
2363
2350
    calc_group_buffer(curr_join, curr_join->group_list);
2364
 
    count_field_types(select_lex, &curr_join->tmp_table_param,
2365
 
                      *curr_all_fields, 0);
 
2351
    count_field_types(select_lex, &curr_join->tmp_table_param, *curr_all_fields, 0);
2366
2352
 
2367
2353
  }
2368
2354
 
2369
2355
  if (curr_join->group || curr_join->tmp_table_param.sum_func_count)
2370
2356
  {
2371
2357
    if (make_group_fields(this, curr_join))
2372
 
    {
2373
2358
      return;
2374
 
    }
2375
 
    if (!items3)
 
2359
 
 
2360
    if (! items3)
2376
2361
    {
2377
 
      if (!items0)
2378
 
        init_items_ref_array();
 
2362
      if (! items0)
 
2363
        init_items_ref_array();
2379
2364
      items3= ref_pointer_array + (all_fields.elements*4);
2380
2365
      setup_copy_fields(session, &curr_join->tmp_table_param,
2381
2366
                        items3, tmp_fields_list3, tmp_all_fields3,
2382
2367
                        curr_fields_list->elements, *curr_all_fields);
2383
2368
      tmp_table_param.save_copy_funcs= curr_join->tmp_table_param.copy_funcs;
2384
2369
      tmp_table_param.save_copy_field= curr_join->tmp_table_param.copy_field;
2385
 
      tmp_table_param.save_copy_field_end=
2386
 
        curr_join->tmp_table_param.copy_field_end;
 
2370
      tmp_table_param.save_copy_field_end= curr_join->tmp_table_param.copy_field_end;
2387
2371
      curr_join->tmp_all_fields3= tmp_all_fields3;
2388
2372
      curr_join->tmp_fields_list3= tmp_fields_list3;
2389
2373
    }
2391
2375
    {
2392
2376
      curr_join->tmp_table_param.copy_funcs= tmp_table_param.save_copy_funcs;
2393
2377
      curr_join->tmp_table_param.copy_field= tmp_table_param.save_copy_field;
2394
 
      curr_join->tmp_table_param.copy_field_end=
2395
 
        tmp_table_param.save_copy_field_end;
 
2378
      curr_join->tmp_table_param.copy_field_end= tmp_table_param.save_copy_field_end;
2396
2379
    }
2397
2380
    curr_fields_list= &tmp_fields_list3;
2398
2381
    curr_all_fields= &tmp_all_fields3;
2408
2391
  {
2409
2392
    session->set_proc_info("Sorting result");
2410
2393
    /* If we have already done the group, add HAVING to sorted table */
2411
 
    if (curr_join->tmp_having && ! curr_join->group_list &&
2412
 
        ! curr_join->sort_and_group)
 
2394
    if (curr_join->tmp_having && ! curr_join->group_list &&     ! curr_join->sort_and_group)
2413
2395
    {
2414
2396
      // Some tables may have been const
2415
2397
      curr_join->tmp_having->update_used_tables();
2417
2399
      table_map used_tables= (curr_join->const_table_map |
2418
2400
                              curr_table->table->map);
2419
2401
 
2420
 
      Item* sort_table_cond= make_cond_for_table(curr_join->tmp_having,
2421
 
                                                 used_tables,
2422
 
                                                 used_tables, 0);
 
2402
      Item* sort_table_cond= make_cond_for_table(curr_join->tmp_having, used_tables, used_tables, 0);
2423
2403
      if (sort_table_cond)
2424
2404
      {
2425
 
        if (!curr_table->select)
2426
 
          if (!(curr_table->select= new SQL_SELECT))
2427
 
            return;
2428
 
        if (!curr_table->select->cond)
2429
 
          curr_table->select->cond= sort_table_cond;
2430
 
        else                                    // This should never happen
2431
 
        {
2432
 
          if (!(curr_table->select->cond=
2433
 
                new Item_cond_and(curr_table->select->cond,
2434
 
                                  sort_table_cond)))
2435
 
            return;
2436
 
          /*
2437
 
            Item_cond_and do not need fix_fields for execution, its parameters
2438
 
            are fixed or do not need fix_fields, too
2439
 
          */
2440
 
          curr_table->select->cond->quick_fix_field();
2441
 
        }
2442
 
        curr_table->select_cond= curr_table->select->cond;
2443
 
        curr_table->select_cond->top_level_item();
2444
 
        curr_join->tmp_having= make_cond_for_table(curr_join->tmp_having,
2445
 
                                                   ~ (table_map) 0,
2446
 
                                                   ~used_tables, 0);
 
2405
        if (!curr_table->select)
 
2406
          if (!(curr_table->select= new SQL_SELECT))
 
2407
            return;
 
2408
        if (!curr_table->select->cond)
 
2409
          curr_table->select->cond= sort_table_cond;
 
2410
        else                                    // This should never happen
 
2411
        {
 
2412
          if (!(curr_table->select->cond=
 
2413
          new Item_cond_and(curr_table->select->cond,
 
2414
                sort_table_cond)))
 
2415
            return;
 
2416
          /*
 
2417
            Item_cond_and do not need fix_fields for execution, its parameters
 
2418
            are fixed or do not need fix_fields, too
 
2419
          */
 
2420
          curr_table->select->cond->quick_fix_field();
 
2421
        }
 
2422
        curr_table->select_cond= curr_table->select->cond;
 
2423
        curr_table->select_cond->top_level_item();
 
2424
        curr_join->tmp_having= make_cond_for_table(curr_join->tmp_having,
 
2425
                    ~ (table_map) 0,
 
2426
                    ~used_tables, 0);
2447
2427
      }
2448
2428
    }
2449
2429
    {
2450
2430
      if (group)
2451
 
        curr_join->select_limit= HA_POS_ERROR;
 
2431
        curr_join->select_limit= HA_POS_ERROR;
2452
2432
      else
2453
2433
      {
2454
 
        /*
2455
 
          We can abort sorting after session->select_limit rows if we there is no
2456
 
          WHERE clause for any tables after the sorted one.
2457
 
        */
2458
 
        JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables+1];
2459
 
        JOIN_TAB *end_table= &curr_join->join_tab[curr_join->tables];
2460
 
        for (; curr_table < end_table ; curr_table++)
2461
 
        {
2462
 
          /*
2463
 
            table->keyuse is set in the case there was an original WHERE clause
2464
 
            on the table that was optimized away.
2465
 
          */
2466
 
          if (curr_table->select_cond ||
2467
 
              (curr_table->keyuse && !curr_table->first_inner))
2468
 
          {
2469
 
            /* We have to sort all rows */
2470
 
            curr_join->select_limit= HA_POS_ERROR;
2471
 
            break;
2472
 
          }
2473
 
        }
 
2434
        /*
 
2435
          We can abort sorting after session->select_limit rows if we there is no
 
2436
          WHERE clause for any tables after the sorted one.
 
2437
        */
 
2438
        JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables+1];
 
2439
        JOIN_TAB *end_table= &curr_join->join_tab[curr_join->tables];
 
2440
        for (; curr_table < end_table ; curr_table++)
 
2441
        {
 
2442
          /*
 
2443
            table->keyuse is set in the case there was an original WHERE clause
 
2444
            on the table that was optimized away.
 
2445
          */
 
2446
          if (curr_table->select_cond ||
 
2447
              (curr_table->keyuse && !curr_table->first_inner))
 
2448
          {
 
2449
            /* We have to sort all rows */
 
2450
            curr_join->select_limit= HA_POS_ERROR;
 
2451
            break;
 
2452
          }
 
2453
        }
2474
2454
      }
2475
2455
      if (curr_join->join_tab == join_tab && save_join_tab())
2476
 
      {
2477
 
        return;
2478
 
      }
 
2456
        return;
2479
2457
      /*
2480
 
        Here we sort rows for order_st BY/GROUP BY clause, if the optimiser
2481
 
        chose FILESORT to be faster than INDEX SCAN or there is no
2482
 
        suitable index present.
2483
 
        Note, that create_sort_index calls test_if_skip_sort_order and may
2484
 
        finally replace sorting with index scan if there is a LIMIT clause in
2485
 
        the query. XXX: it's never shown in EXPLAIN!
2486
 
        OPTION_FOUND_ROWS supersedes LIMIT and is taken into account.
 
2458
        Here we sort rows for order_st BY/GROUP BY clause, if the optimiser
 
2459
        chose FILESORT to be faster than INDEX SCAN or there is no
 
2460
        suitable index present.
 
2461
        Note, that create_sort_index calls test_if_skip_sort_order and may
 
2462
        finally replace sorting with index scan if there is a LIMIT clause in
 
2463
        the query. XXX: it's never shown in EXPLAIN!
 
2464
        OPTION_FOUND_ROWS supersedes LIMIT and is taken into account.
2487
2465
      */
2488
2466
      if (create_sort_index(session, curr_join,
2489
2467
                            curr_join->group_list ?
2492
2470
                            (select_options & OPTION_FOUND_ROWS ?
2493
2471
                             HA_POS_ERROR : unit->select_limit_cnt),
2494
2472
                            curr_join->group_list ? true : false))
2495
 
        return;
 
2473
        return;
 
2474
 
2496
2475
      sortorder= curr_join->sortorder;
2497
2476
      if (curr_join->const_tables != curr_join->tables &&
2498
2477
          !curr_join->join_tab[curr_join->const_tables].table->sort.io_cache)
2515
2494
  curr_join->having= curr_join->tmp_having;
2516
2495
  curr_join->fields= curr_fields_list;
2517
2496
 
2518
 
  {
2519
 
    session->set_proc_info("Sending data");
2520
 
    result->send_fields(*curr_fields_list,
2521
 
                        Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
2522
 
    error= do_select(curr_join, curr_fields_list, NULL);
2523
 
    session->limit_found_rows= curr_join->send_records;
2524
 
  }
 
2497
  session->set_proc_info("Sending data");
 
2498
  result->send_fields(*curr_fields_list,
 
2499
                      Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
 
2500
  error= do_select(curr_join, curr_fields_list, NULL);
 
2501
  session->limit_found_rows= curr_join->send_records;
2525
2502
 
2526
2503
  /* Accumulate the counts from all join iterations of all join parts. */
2527
2504
  session->examined_row_count+= curr_join->examined_rows;
2531
2508
    for a derived table which is always materialized.
2532
2509
    Otherwise we would not be able to print the query  correctly.
2533
2510
  */
2534
 
  if (items0 &&
2535
 
      (session->lex->describe & DESCRIBE_EXTENDED) &&
2536
 
      select_lex->linkage == DERIVED_TABLE_TYPE)
 
2511
  if (items0 && (session->lex->describe & DESCRIBE_EXTENDED) && select_lex->linkage == DERIVED_TABLE_TYPE)
2537
2512
    set_items_ref_array(items0);
2538
2513
 
2539
2514
  return;
2540
2515
}
2541
2516
 
2542
 
 
2543
2517
/**
2544
2518
  Clean up join.
2545
2519
 
2546
2520
  @return
2547
2521
    Return error that hold JOIN.
2548
2522
*/
2549
 
 
2550
 
int
2551
 
JOIN::destroy()
 
2523
int JOIN::destroy()
2552
2524
{
2553
2525
  select_lex->join= 0;
2554
2526
 
2558
2530
    {
2559
2531
      JOIN_TAB *tab, *end;
2560
2532
      for (tab= join_tab, end= tab+tables ; tab != end ; tab++)
2561
 
        tab->cleanup();
 
2533
        tab->cleanup();
2562
2534
    }
2563
2535
    tmp_join->tmp_join= 0;
2564
2536
    tmp_table_param.copy_field=0;
2576
2548
  return(error);
2577
2549
}
2578
2550
 
2579
 
 
2580
 
 
2581
2551
/**
2582
2552
  An entry point to single-unit select (a select without UNION).
2583
2553
 
3163
3133
  return(false);
3164
3134
}
3165
3135
 
3166
 
 
3167
3136
/**
3168
3137
  Setup for execution all subqueries of a query, for which the optimizer
3169
3138
  chose hash semi-join.
3186
3155
  @retval false     success.
3187
3156
  @retval true      error occurred.
3188
3157
*/
3189
 
 
3190
3158
bool JOIN::setup_subquery_materialization()
3191
3159
{
3192
3160
  for (Select_Lex_Unit *un= select_lex->first_inner_unit(); un;
3478
3446
  {
3479
3447
    TableList *embedding= tables->embedding;
3480
3448
    stat_vector[i]=s;
3481
 
    s->keys.init();
3482
 
    s->const_keys.init();
3483
 
    s->checked_keys.init();
3484
 
    s->needed_reg.init();
 
3449
    s->keys.reset();
 
3450
    s->const_keys.reset();
 
3451
    s->checked_keys.reset();
 
3452
    s->needed_reg.reset();
3485
3453
    table_vector[i]=s->table=table=tables->table;
3486
3454
    table->pos_in_table_list= tables;
3487
3455
    error= table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
3490
3458
        table->file->print_error(error, MYF(0));
3491
3459
        return(1);
3492
3460
    }
3493
 
    table->quick_keys.clear_all();
 
3461
    table->quick_keys.reset();
3494
3462
    table->reginfo.join_tab=s;
3495
3463
    table->reginfo.not_exists_optimize=0;
3496
3464
    memset(table->const_key_parts, 0,
3687
3655
        {
3688
3656
          start_keyuse=keyuse;
3689
3657
          key=keyuse->key;
3690
 
          s->keys.set_bit(key);               // QQ: remove this ?
 
3658
          s->keys.set(key);               // QQ: remove this ?
3691
3659
 
3692
3660
          refs=0;
3693
 
          const_ref.clear_all();
3694
 
          eq_part.clear_all();
 
3661
          const_ref.reset();
 
3662
          eq_part.reset();
3695
3663
          do
3696
3664
          {
3697
3665
            if (keyuse->val->type() != Item::NULL_ITEM && !keyuse->optimize)
3698
3666
            {
3699
3667
              if (!((~found_const_table_map) & keyuse->used_tables))
3700
 
                const_ref.set_bit(keyuse->keypart);
 
3668
                const_ref.set(keyuse->keypart);
3701
3669
              else
3702
3670
                refs|=keyuse->used_tables;
3703
 
              eq_part.set_bit(keyuse->keypart);
 
3671
              eq_part.set(keyuse->keypart);
3704
3672
            }
3705
3673
            keyuse++;
3706
3674
          } while (keyuse->table == table && keyuse->key == key);
3707
3675
 
3708
 
          if (eq_part.is_prefix(table->key_info[key].key_parts) &&
 
3676
          if (is_keymap_prefix(eq_part, table->key_info[key].key_parts) &&
3709
3677
              !table->pos_in_table_list->embedding)
3710
3678
          {
3711
3679
            if ((table->key_info[key].flags & (HA_NOSAME))
3716
3684
                int tmp;
3717
3685
                ref_changed = 1;
3718
3686
                s->type= JT_CONST;
3719
 
                join->const_table_map|=table->map;
 
3687
                join->const_table_map|= table->map;
3720
3688
                set_position(join,const_count++,s,start_keyuse);
3721
3689
                if (create_ref_for_key(join, s, start_keyuse,
3722
3690
                                       found_const_table_map))
3735
3703
                found_ref|= refs;      // Table is const if all refs are const
3736
3704
            }
3737
3705
            else if (const_ref == eq_part)
3738
 
              s->const_keys.set_bit(key);
 
3706
              s->const_keys.set(key);
3739
3707
          }
3740
3708
        }
3741
3709
      }
3753
3721
      Field *field= sargables->field;
3754
3722
      JOIN_TAB *join_tab= field->table->reginfo.join_tab;
3755
3723
      key_map possible_keys= field->key_start;
3756
 
      possible_keys.intersect(field->table->keys_in_use_for_query);
 
3724
      possible_keys&= field->table->keys_in_use_for_query;
3757
3725
      bool is_const= 1;
3758
3726
      for (uint32_t j=0; j < sargables->num_values; j++)
3759
3727
        is_const&= sargables->arg_value[j]->const_item();
3760
3728
      if (is_const)
3761
 
        join_tab[0].const_keys.merge(possible_keys);
 
3729
        join_tab[0].const_keys|= possible_keys;
3762
3730
    }
3763
3731
  }
3764
3732
 
3795
3763
    */
3796
3764
    add_group_and_distinct_keys(join, s);
3797
3765
 
3798
 
    if (!s->const_keys.is_clear_all() &&
 
3766
    if (s->const_keys.any() &&
3799
3767
        !s->table->pos_in_table_list->embedding)
3800
3768
    {
3801
3769
      ha_rows records;
3813
3781
      select->quick=0;
3814
3782
      if (records == 0 && s->table->reginfo.impossible_range)
3815
3783
      {
3816
 
        /*
3817
 
          Impossible WHERE or ON expression
3818
 
          In case of ON, we mark that the we match one empty NULL row.
3819
 
          In case of WHERE, don't set found_const_table_map to get the
3820
 
          caller to abort with a zero row result.
3821
 
        */
3822
 
        join->const_table_map|= s->table->map;
3823
 
        set_position(join,const_count++,s,(KEYUSE*) 0);
3824
 
        s->type= JT_CONST;
3825
 
        if (*s->on_expr_ref)
3826
 
        {
3827
 
          /* Generate empty row */
3828
 
          s->info= "Impossible ON condition";
3829
 
          found_const_table_map|= s->table->map;
3830
 
          s->type= JT_CONST;
3831
 
          s->table->mark_as_null_row();         // All fields are NULL
3832
 
        }
 
3784
        /*
 
3785
          Impossible WHERE or ON expression
 
3786
          In case of ON, we mark that the we match one empty NULL row.
 
3787
          In case of WHERE, don't set found_const_table_map to get the
 
3788
          caller to abort with a zero row result.
 
3789
        */
 
3790
        join->const_table_map|= s->table->map;
 
3791
        set_position(join,const_count++,s,(KEYUSE*) 0);
 
3792
        s->type= JT_CONST;
 
3793
        if (*s->on_expr_ref)
 
3794
        {
 
3795
          /* Generate empty row */
 
3796
          s->info= "Impossible ON condition";
 
3797
          found_const_table_map|= s->table->map;
 
3798
          s->type= JT_CONST;
 
3799
          s->table->mark_as_null_row();         // All fields are NULL
 
3800
        }
3833
3801
      }
3834
3802
      if (records != HA_POS_ERROR)
3835
3803
      {
3836
 
        s->found_records=records;
3837
 
        s->read_time= (ha_rows) (s->quick ? s->quick->read_time : 0.0);
 
3804
        s->found_records=records;
 
3805
        s->read_time= (ha_rows) (s->quick ? s->quick->read_time : 0.0);
3838
3806
      }
3839
3807
      delete select;
3840
3808
    }
4077
4045
    else
4078
4046
    {
4079
4047
      JOIN_TAB *stat=field->table->reginfo.join_tab;
4080
 
      key_map possible_keys=field->key_start;
4081
 
      possible_keys.intersect(field->table->keys_in_use_for_query);
4082
 
      stat[0].keys.merge(possible_keys);             // Add possible keys
 
4048
      key_map possible_keys= field->key_start;
 
4049
      possible_keys&= field->table->keys_in_use_for_query;
 
4050
      stat[0].keys|= possible_keys;             // Add possible keys
4083
4051
 
4084
4052
      /*
4085
4053
        Save the following cases:
4092
4060
         Field BETWEEN ...
4093
4061
         Field IN ...
4094
4062
      */
4095
 
      stat[0].key_dependent|=used_tables;
 
4063
      stat[0].key_dependent|= used_tables;
4096
4064
 
4097
4065
      bool is_const=1;
4098
4066
      for (uint32_t i=0; i<num_values; i++)
4101
4069
          break;
4102
4070
      }
4103
4071
      if (is_const)
4104
 
        stat[0].const_keys.merge(possible_keys);
 
4072
        stat[0].const_keys|= possible_keys;
4105
4073
      else if (!eq_func)
4106
4074
      {
4107
4075
        /*
4464
4432
  {
4465
4433
    for (uint32_t key= 0 ; key < form->sizeKeys() ; key++)
4466
4434
    {
4467
 
      if (!(form->keys_in_use_for_query.is_set(key)))
 
4435
      if (!(form->keys_in_use_for_query.test(key)))
4468
4436
        continue;
4469
4437
 
4470
4438
      uint32_t key_parts= (uint32_t) form->key_info[key].key_parts;
4739
4707
      /* Save ptr to first use */
4740
4708
      if (!use->table->reginfo.join_tab->keyuse)
4741
4709
        use->table->reginfo.join_tab->keyuse=save_pos;
4742
 
      use->table->reginfo.join_tab->checked_keys.set_bit(use->key);
 
4710
      use->table->reginfo.join_tab->checked_keys.set(use->key);
4743
4711
      save_pos++;
4744
4712
    }
4745
4713
    i=(uint32_t) (save_pos-(KEYUSE*) keyuse->buffer);
4841
4809
 
4842
4810
  /* Intersect the keys of all group fields. */
4843
4811
  cur_item= indexed_fields_it++;
4844
 
  possible_keys.merge(cur_item->field->part_of_key);
 
4812
  possible_keys|= cur_item->field->part_of_key;
4845
4813
  while ((cur_item= indexed_fields_it++))
4846
4814
  {
4847
 
    possible_keys.intersect(cur_item->field->part_of_key);
 
4815
    possible_keys&= cur_item->field->part_of_key;
4848
4816
  }
4849
4817
 
4850
 
  if (!possible_keys.is_clear_all())
4851
 
    join_tab->const_keys.merge(possible_keys);
 
4818
  if (possible_keys.any())
 
4819
    join_tab->const_keys|= possible_keys;
4852
4820
}
4853
4821
 
4854
4822
 
5088
5056
             ...
5089
5057
        */
5090
5058
        if (try_sj_inside_out &&
5091
 
            table->covering_keys.is_set(key) &&
 
5059
            table->covering_keys.test(key) &&
5092
5060
            (handled_sj_equalities | bound_sj_equalities) ==     // (1)
5093
5061
            PREV_BITS(uint64_t, s->emb_sj_nest->sj_in_exprs)) // (1)
5094
5062
        {
5160
5128
                quick_cond is equivalent to ref_const_cond (if it was an
5161
5129
                empty interval we wouldn't have got here).
5162
5130
              */
5163
 
              if (table->quick_keys.is_set(key))
 
5131
              if (table->quick_keys.test(key))
5164
5132
                records= (double) table->quick_rows[key];
5165
5133
              else
5166
5134
              {
5190
5158
                can make an adjustment is a special case of the criteria used
5191
5159
                in ReuseRangeEstimateForRef-3.
5192
5160
              */
5193
 
              if (table->quick_keys.is_set(key) &&
 
5161
              if (table->quick_keys.test(key) &&
5194
5162
                  const_part & (1 << table->quick_key_parts[key]) &&
5195
5163
                  table->quick_n_ranges[key] == 1 &&
5196
5164
                  records > (double) table->quick_rows[key])
5201
5169
            /* Limit the number of matched rows */
5202
5170
            tmp= records;
5203
5171
            set_if_smaller(tmp, (double) session->variables.max_seeks_for_key);
5204
 
            if (table->covering_keys.is_set(key))
 
5172
            if (table->covering_keys.test(key))
5205
5173
            {
5206
5174
              /* we can use only index tree */
5207
5175
              tmp= record_count * table->file->index_only_read_time(key, tmp);
5261
5229
 
5262
5230
              (C3) "range optimizer used (have ref_or_null?2:1) intervals"
5263
5231
            */
5264
 
            if (table->quick_keys.is_set(key) && !found_ref &&          //(C1)
 
5232
            if (table->quick_keys.test(key) && !found_ref &&          //(C1)
5265
5233
                table->quick_key_parts[key] == max_key_part &&          //(C2)
5266
5234
                table->quick_n_ranges[key] == 1+((ref_or_null_part)?1:0)) //(C3)
5267
5235
            {
5288
5256
                  cheaper in some cases ?
5289
5257
                  TODO: figure this out and adjust the plan choice if needed.
5290
5258
                */
5291
 
                if (!found_ref && table->quick_keys.is_set(key) &&    // (1)
 
5259
                if (!found_ref && table->quick_keys.test(key) &&    // (1)
5292
5260
                    table->quick_key_parts[key] > max_key_part &&     // (2)
5293
5261
                    records < (double)table->quick_rows[key])         // (3)
5294
5262
                  records= (double)table->quick_rows[key];
5353
5321
                optimizer is the same as in ReuseRangeEstimateForRef-3,
5354
5322
                applied to first table->quick_key_parts[key] key parts.
5355
5323
              */
5356
 
              if (table->quick_keys.is_set(key) &&
 
5324
              if (table->quick_keys.test(key) &&
5357
5325
                  table->quick_key_parts[key] <= max_key_part &&
5358
5326
                  const_part & (1 << table->quick_key_parts[key]) &&
5359
5327
                  table->quick_n_ranges[key] == 1 + ((ref_or_null_part &
5366
5334
 
5367
5335
            /* Limit the number of matched rows */
5368
5336
            set_if_smaller(tmp, (double) session->variables.max_seeks_for_key);
5369
 
            if (table->covering_keys.is_set(key))
 
5337
            if (table->covering_keys.test(key))
5370
5338
            {
5371
5339
              /* we can use only index tree */
5372
5340
              tmp= record_count * table->file->index_only_read_time(key, tmp);
5432
5400
      !(s->quick && best_key && s->quick->index == best_key->key &&      // (2)
5433
5401
        best_max_key_part >= s->table->quick_key_parts[best_key->key]) &&// (2)
5434
5402
      !((s->table->file->ha_table_flags() & HA_TABLE_SCAN_ON_INDEX) &&   // (3)
5435
 
        ! s->table->covering_keys.is_clear_all() && best_key && !s->quick) &&// (3)
 
5403
        ! s->table->covering_keys.none() && best_key && !s->quick) &&// (3)
5436
5404
      !(s->table->force_index && best_key && !s->quick))                 // (4)
5437
5405
  {                                             // Check full join
5438
5406
    ha_rows rnd_records= s->found_records;
6262
6230
{
6263
6231
  uint32_t null_fields,blobs,fields,rec_length;
6264
6232
  Field **f_ptr,*field;
6265
 
  MY_BITMAP *read_set= join_tab->table->read_set;;
 
6233
  bitset<MAX_FIELDS> *read_set= join_tab->table->read_set;
6266
6234
 
6267
6235
  null_fields= blobs= fields= rec_length=0;
6268
6236
  for (f_ptr=join_tab->table->field ; (field= *f_ptr) ; f_ptr++)
6269
6237
  {
6270
 
    if (bitmap_is_set(read_set, field->field_index))
 
6238
    if (read_set->test(field->field_index))
6271
6239
    {
6272
6240
      uint32_t flags=field->flags;
6273
6241
      fields++;
6288
6256
                             (join_tab->table->getRecordLength()- rec_length));
6289
6257
    rec_length+=(uint32_t) cmax((uint32_t)4,blob_length);
6290
6258
  }
6291
 
  join_tab->used_fields=fields;
6292
 
  join_tab->used_fieldlength=rec_length;
6293
 
  join_tab->used_blobs=blobs;
 
6259
  join_tab->used_fields= fields;
 
6260
  join_tab->used_fieldlength= rec_length;
 
6261
  join_tab->used_blobs= blobs;
6294
6262
}
6295
6263
 
6296
6264
 
6438
6406
 
6439
6407
    if (j->type == JT_SYSTEM)
6440
6408
      continue;
6441
 
    if (j->keys.is_clear_all() || !(keyuse= join->best_positions[tablenr].key))
 
6409
    if (j->keys.none() || !(keyuse= join->best_positions[tablenr].key))
6442
6410
    {
6443
6411
      j->type=JT_ALL;
6444
6412
      if (tablenr != join->const_tables)
6693
6661
  join_tab->select_cond=0;
6694
6662
  join_tab->quick=0;
6695
6663
  join_tab->type= JT_ALL;                       /* Map through all records */
6696
 
  join_tab->keys.init();
6697
 
  join_tab->keys.set_all();                     /* test everything in quick */
 
6664
  join_tab->keys.set();                     /* test everything in quick */
6698
6665
  join_tab->info=0;
6699
6666
  join_tab->on_expr_ref=0;
6700
6667
  join_tab->last_inner= 0;
7115
7082
        {
7116
7083
          /* Use quick key read if it's a constant and it's not used
7117
7084
             with key reading */
7118
 
          if (tab->needed_reg.is_clear_all() && tab->type != JT_EQ_REF
 
7085
          if (tab->needed_reg.none() && tab->type != JT_EQ_REF
7119
7086
              && (tab->type != JT_REF || (uint32_t) tab->ref.key == tab->quick->index))
7120
7087
          {
7121
7088
            sel->quick=tab->quick;              // Use value from get_quick_...
7122
 
            sel->quick_keys.clear_all();
7123
 
            sel->needed_reg.clear_all();
 
7089
            sel->quick_keys.reset();
 
7090
            sel->needed_reg.reset();
7124
7091
          }
7125
7092
          else
7126
7093
          {
7131
7098
        uint32_t ref_key=(uint32_t) sel->head->reginfo.join_tab->ref.key+1;
7132
7099
        if (i == join->const_tables && ref_key)
7133
7100
        {
7134
 
          if (!tab->const_keys.is_clear_all() &&
 
7101
          if (tab->const_keys.any() &&
7135
7102
              tab->table->reginfo.impossible_range)
7136
7103
            return(1);
7137
7104
        }
7138
7105
        else if (tab->type == JT_ALL && ! use_quick_range)
7139
7106
        {
7140
 
          if (!tab->const_keys.is_clear_all() &&
 
7107
          if (tab->const_keys.any() &&
7141
7108
              tab->table->reginfo.impossible_range)
7142
7109
            return(1);                          // Impossible range
7143
7110
          /*
7147
7114
            the index if we are using limit and this is the first table
7148
7115
          */
7149
7116
 
7150
 
          if ((cond && (!tab->keys.is_subset(tab->const_keys) && i > 0)) ||
7151
 
              (!tab->const_keys.is_clear_all() && (i == join->const_tables) && (join->unit->select_limit_cnt < join->best_positions[i].records_read) && ((join->select_options & OPTION_FOUND_ROWS) == false)))
 
7117
          if ((cond && (!((tab->const_keys & tab->keys) == tab->keys) && i > 0)) ||
 
7118
              (!tab->const_keys.none() && (i == join->const_tables) && (join->unit->select_limit_cnt < join->best_positions[i].records_read) && ((join->select_options & OPTION_FOUND_ROWS) == false)))
7152
7119
          {
7153
7120
            /* Join with outer join condition */
7154
7121
            COND *orig_cond=sel->cond;
7197
7164
          else
7198
7165
          {
7199
7166
            sel->needed_reg=tab->needed_reg;
7200
 
            sel->quick_keys.clear_all();
 
7167
            sel->quick_keys.reset();
7201
7168
          }
7202
 
          if (!sel->quick_keys.is_subset(tab->checked_keys) ||
7203
 
              !sel->needed_reg.is_subset(tab->checked_keys))
 
7169
          if (!((tab->checked_keys & sel->quick_keys) == sel->quick_keys) ||
 
7170
              !((tab->checked_keys & sel->needed_reg) == sel->needed_reg))
7204
7171
          {
7205
 
            tab->keys=sel->quick_keys;
7206
 
            tab->keys.merge(sel->needed_reg);
7207
 
            tab->use_quick= (!sel->needed_reg.is_clear_all() &&
7208
 
                             (select->quick_keys.is_clear_all() ||
 
7172
            tab->keys= sel->quick_keys;
 
7173
            tab->keys|= sel->needed_reg;
 
7174
            tab->use_quick= (!sel->needed_reg.none() &&
 
7175
                             (select->quick_keys.none() ||
7209
7176
                              (select->quick &&
7210
7177
                               (select->quick->records >= 100L)))) ?
7211
7178
              2 : 1;
7392
7359
      Item_field *item_field= (Item_field*)item;
7393
7360
      if (item_field->field->table != tbl)
7394
7361
        return true;
7395
 
      return item_field->field->part_of_key.is_set(keyno);
 
7362
      return item_field->field->part_of_key.test(keyno);
7396
7363
    }
7397
7364
  case Item::REF_ITEM:
7398
7365
    return uses_index_fields_only(item->real_item(), tbl, keyno,
7715
7682
      table->status=STATUS_NO_RECORD;
7716
7683
      tab->read_first_record= join_read_const;
7717
7684
      tab->read_record.read_record= join_no_more_records;
7718
 
      if (table->covering_keys.is_set(tab->ref.key) &&
 
7685
      if (table->covering_keys.test(tab->ref.key) &&
7719
7686
          !table->no_keyread)
7720
7687
      {
7721
7688
        table->key_read=1;
7733
7700
      tab->quick=0;
7734
7701
      tab->read_first_record= join_read_key;
7735
7702
      tab->read_record.read_record= join_no_more_records;
7736
 
      if (table->covering_keys.is_set(tab->ref.key) &&
 
7703
      if (table->covering_keys.test(tab->ref.key) &&
7737
7704
          !table->no_keyread)
7738
7705
      {
7739
7706
        table->key_read=1;
7752
7719
      }
7753
7720
      delete tab->quick;
7754
7721
      tab->quick=0;
7755
 
      if (table->covering_keys.is_set(tab->ref.key) &&
 
7722
      if (table->covering_keys.test(tab->ref.key) &&
7756
7723
          !table->no_keyread)
7757
7724
      {
7758
7725
        table->key_read=1;
7834
7801
        {
7835
7802
          if (tab->select && tab->select->quick &&
7836
7803
              tab->select->quick->index != MAX_KEY && //not index_merge
7837
 
              table->covering_keys.is_set(tab->select->quick->index))
 
7804
              table->covering_keys.test(tab->select->quick->index))
7838
7805
          {
7839
7806
            table->key_read=1;
7840
7807
            table->file->extra(HA_EXTRA_KEYREAD);
7841
7808
          }
7842
 
          else if (!table->covering_keys.is_clear_all() &&
 
7809
          else if (!table->covering_keys.none() &&
7843
7810
                   !(tab->select && tab->select->quick))
7844
7811
          {                                     // Only read index tree
7845
7812
            if (!tab->insideout_match_tab)
9444
9411
        Field *field= item_field->field;
9445
9412
        JOIN_TAB *stat= field->table->reginfo.join_tab;
9446
9413
        key_map possible_keys= field->key_start;
9447
 
        possible_keys.intersect(field->table->keys_in_use_for_query);
9448
 
        stat[0].const_keys.merge(possible_keys);
 
9414
        possible_keys&= field->table->keys_in_use_for_query;
 
9415
        stat[0].const_keys|= possible_keys;
9449
9416
 
9450
9417
        /*
9451
9418
          For each field in the multiple equality (for which we know that it
9452
9419
          is a constant) we have to find its corresponding key part, and set
9453
9420
          that key part in const_key_parts.
9454
9421
        */
9455
 
        if (!possible_keys.is_clear_all())
 
9422
        if (possible_keys.any())
9456
9423
        {
9457
9424
          Table *field_tab= field->table;
9458
9425
          KEYUSE *use;
9459
9426
          for (use= stat->keyuse; use && use->table == field_tab; use++)
9460
 
            if (possible_keys.is_set(use->key) &&
 
9427
            if (possible_keys.test(use->key) &&
9461
9428
                field_tab->key_info[use->key].key_part[use->keypart].field ==
9462
9429
                field)
9463
9430
              field_tab->const_key_parts[use->key]|= use->keypart_map;
10622
10589
  if (table)
10623
10590
  {
10624
10591
    table->file->extra(HA_EXTRA_WRITE_CACHE);
10625
 
    empty_record(table);
 
10592
    table->emptyRecord();
10626
10593
    if (table->group && join->tmp_table_param.sum_func_count &&
10627
10594
        table->s->keys && !table->file->inited)
10628
10595
      table->file->ha_index_init(0, 0);
11187
11154
    join_tab->found= 1;
11188
11155
    join_tab->not_null_compl= 0;
11189
11156
    /* The outer row is complemented by nulls for each inner tables */
11190
 
    restore_record(join_tab->table,s->default_values);  // Make empty record
 
11157
    join_tab->table->restoreRecordAsDefault();  // Make empty record
11191
11158
    join_tab->table->mark_as_null_row();       // For group by without error
11192
11159
    select_cond= join_tab->select_cond;
11193
11160
    /* Check all attached conditions for inner table rows. */
11346
11313
  }
11347
11314
  else
11348
11315
  {
11349
 
    if (!table->key_read && table->covering_keys.is_set(tab->ref.key) &&
 
11316
    if (!table->key_read && table->covering_keys.test(tab->ref.key) &&
11350
11317
        !table->no_keyread &&
11351
11318
        (int) table->reginfo.lock_type <= (int) TL_READ_HIGH_PRIORITY)
11352
11319
    {
11415
11382
      if (error != HA_ERR_END_OF_FILE)
11416
11383
        return table->report_error(error);
11417
11384
      tab->table->mark_as_null_row();
11418
 
      empty_record(table);                      // Make empty record
 
11385
      table->emptyRecord();                     // Make empty record
11419
11386
      return -1;
11420
11387
    }
11421
 
    update_virtual_fields_marked_for_write(table);
11422
 
    store_record(table,record[1]);
 
11388
    table->storeRecord();
11423
11389
  }
11424
11390
  else if (!table->status)                      // Only happens with left join
11425
 
    restore_record(table,record[1]);                    // restore old record
 
11391
    table->restoreRecord();                     // restore old record
11426
11392
  table->null_row=0;
11427
11393
  return table->status ? -1 : 0;
11428
11394
}
11462
11428
    {
11463
11429
      table->status= STATUS_NOT_FOUND;
11464
11430
      tab->table->mark_as_null_row();
11465
 
      empty_record(table);
 
11431
      table->emptyRecord();
11466
11432
      if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
11467
11433
        return table->report_error(error);
11468
11434
      return -1;
11469
11435
    }
11470
 
    update_virtual_fields_marked_for_write(table);
11471
 
    store_record(table,record[1]);
 
11436
    table->storeRecord();
11472
11437
  }
11473
11438
  else if (!(table->status & ~STATUS_NULL_ROW)) // Only happens with left join
11474
11439
  {
11475
11440
    table->status=0;
11476
 
    restore_record(table,record[1]);                    // restore old record
 
11441
    table->restoreRecord();                     // restore old record
11477
11442
  }
11478
11443
  table->null_row=0;
11479
11444
  return table->status ? -1 : 0;
11576
11541
      return table->report_error(error);
11577
11542
    return -1; /* purecov: inspected */
11578
11543
  }
11579
 
  update_virtual_fields_marked_for_write(table);
 
11544
 
11580
11545
  return 0;
11581
11546
}
11582
11547
 
11663
11628
    table->status= STATUS_GARBAGE;
11664
11629
    return -1;
11665
11630
  }
11666
 
  update_virtual_fields_marked_for_write(table);
 
11631
 
11667
11632
  return 0;
11668
11633
}
11669
11634
 
11683
11648
    table->status=STATUS_NOT_FOUND;
11684
11649
    error= -1;
11685
11650
  }
11686
 
  update_virtual_fields_marked_for_write(table);
 
11651
 
11687
11652
  return error;
11688
11653
}
11689
11654
 
11733
11698
{
11734
11699
  int error;
11735
11700
  Table *table=tab->table;
11736
 
  if (!table->key_read && table->covering_keys.is_set(tab->index) &&
 
11701
  if (!table->key_read && table->covering_keys.test(tab->index) &&
11737
11702
      !table->no_keyread)
11738
11703
  {
11739
11704
    table->key_read=1;
11764
11729
      table->report_error(error);
11765
11730
    return -1;
11766
11731
  }
11767
 
  if (not error)
11768
 
    update_virtual_fields_marked_for_write(tab->table);
 
11732
 
11769
11733
  return 0;
11770
11734
}
11771
11735
 
11785
11749
 
11786
11750
      if ((error=info->file->index_next(info->record)))
11787
11751
        return info->table->report_error(error);
11788
 
      if (not error)
11789
 
        update_virtual_fields_marked_for_write(tab->table);
11790
11752
    } while (!key_cmp(tab->table->key_info[tab->index].key_part,
11791
11753
                      tab->insideout_buf, key->key_length));
11792
11754
    tab->insideout_match_tab->found_match= 0;
11803
11765
  int error;
11804
11766
  if ((error=info->file->index_next(info->record)))
11805
11767
    return info->table->report_error(error);
11806
 
  if (not error)
11807
 
    update_virtual_fields_marked_for_write(info->table);
11808
11768
  return 0;
11809
11769
}
11810
11770
 
11814
11774
{
11815
11775
  Table *table=tab->table;
11816
11776
  int error;
11817
 
  if (!table->key_read && table->covering_keys.is_set(tab->index) &&
 
11777
  if (!table->key_read && table->covering_keys.test(tab->index) &&
11818
11778
      !table->no_keyread)
11819
11779
  {
11820
11780
    table->key_read=1;
11830
11790
    table->file->ha_index_init(tab->index, 1);
11831
11791
  if ((error= tab->table->file->index_last(tab->table->record[0])))
11832
11792
    return table->report_error(error);
11833
 
  if (not error)
11834
 
    update_virtual_fields_marked_for_write(tab->table);
 
11793
 
11835
11794
  return 0;
11836
11795
}
11837
11796
 
11842
11801
  int error;
11843
11802
  if ((error= info->file->index_prev(info->record)))
11844
11803
    return info->table->report_error(error);
11845
 
  if (not error)
11846
 
    update_virtual_fields_marked_for_write(info->table);
 
11804
 
11847
11805
  return 0;
11848
11806
}
11849
11807
 
12148
12106
                                   HA_WHOLE_KEY,
12149
12107
                                   HA_READ_KEY_EXACT))
12150
12108
  {                                             /* Update old record */
12151
 
    restore_record(table,record[1]);
 
12109
    table->restoreRecord();
12152
12110
    update_tmptable_sum_func(join->sum_funcs,table);
12153
12111
    if ((error=table->file->ha_update_row(table->record[1],
12154
12112
                                          table->record[0])))
12225
12183
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
12226
12184
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
12227
12185
    }
12228
 
    restore_record(table,record[1]);
 
12186
    table->restoreRecord();
12229
12187
    update_tmptable_sum_func(join->sum_funcs,table);
12230
12188
    if ((error=table->file->ha_update_row(table->record[1],
12231
12189
                                          table->record[0])))
12721
12679
 
12722
12680
  for (nr= 0 ; nr < table->s->keys ; nr++)
12723
12681
  {
12724
 
    if (usable_keys->is_set(nr) &&
 
12682
    if (usable_keys->test(nr) &&
12725
12683
        table->key_info[nr].key_length < min_length &&
12726
12684
        table->key_info[nr].key_parts >= ref_key_parts &&
12727
12685
        is_subkey(table->key_info[nr].key_part, ref_key_part,
12915
12873
    Item *item= (*tmp_order->item)->real_item();
12916
12874
    if (item->type() != Item::FIELD_ITEM)
12917
12875
    {
12918
 
      usable_keys.clear_all();
 
12876
      usable_keys.reset();
12919
12877
      return(0);
12920
12878
    }
12921
 
    usable_keys.intersect(((Item_field*) item)->field->part_of_sortkey);
12922
 
    if (usable_keys.is_clear_all())
 
12879
    usable_keys&= ((Item_field*) item)->field->part_of_sortkey;
 
12880
    if (usable_keys.none())
12923
12881
      return(0);                                        // No usable keys
12924
12882
  }
12925
12883
 
12955
12913
    /*
12956
12914
      We come here when there is a REF key.
12957
12915
    */
12958
 
    if (!usable_keys.is_set(ref_key))
 
12916
    if (! usable_keys.test(ref_key))
12959
12917
    {
12960
12918
      /*
12961
12919
        We come here when ref_key is not among usable_keys
12965
12923
        If using index only read, only consider other possible index only
12966
12924
        keys
12967
12925
      */
12968
 
      if (table->covering_keys.is_set(ref_key))
12969
 
        usable_keys.intersect(table->covering_keys);
 
12926
      if (table->covering_keys.test(ref_key))
 
12927
        usable_keys&= table->covering_keys;
12970
12928
      if (tab->pre_idx_push_select_cond)
12971
12929
        tab->select_cond= tab->select->cond= tab->pre_idx_push_select_cond;
12972
12930
      if ((new_ref_key= test_if_subkey(order, table, ref_key, ref_key_parts,
13002
12960
            parameres are set correctly by the range optimizer.
13003
12961
           */
13004
12962
          key_map new_ref_key_map;
13005
 
          new_ref_key_map.clear_all();  // Force the creation of quick select
13006
 
          new_ref_key_map.set_bit(new_ref_key); // only for new_ref_key.
 
12963
          new_ref_key_map.reset();  // Force the creation of quick select
 
12964
          new_ref_key_map.set(new_ref_key); // only for new_ref_key.
13007
12965
 
13008
12966
          if (select->test_quick_select(tab->join->session, new_ref_key_map, 0,
13009
12967
                                        (tab->join->select_options &
13018
12976
      }
13019
12977
    }
13020
12978
    /* Check if we get the rows in requested sorted order by using the key */
13021
 
    if (usable_keys.is_set(ref_key) &&
 
12979
    if (usable_keys.test(ref_key) &&
13022
12980
        (order_direction= test_if_order_by_key(order,table,ref_key,
13023
12981
                                               &used_key_parts)))
13024
12982
      goto check_reverse_order;
13058
13016
      if (tab->type == JT_ALL && tab->join->tables > tab->join->const_tables + 1)
13059
13017
        return(0);
13060
13018
      keys= *table->file->keys_to_use_for_scanning();
13061
 
      keys.merge(table->covering_keys);
 
13019
      keys|= table->covering_keys;
13062
13020
 
13063
13021
      /*
13064
13022
        We are adding here also the index specified in FORCE INDEX clause,
13066
13024
        This is to allow users to use index in order_st BY.
13067
13025
      */
13068
13026
      if (table->force_index)
13069
 
        keys.merge(group ? table->keys_in_use_for_group_by :
 
13027
        keys|= (group ? table->keys_in_use_for_group_by :
13070
13028
                           table->keys_in_use_for_order_by);
13071
 
      keys.intersect(usable_keys);
 
13029
      keys&= usable_keys;
13072
13030
    }
13073
13031
    else
13074
13032
      keys= usable_keys;
13080
13038
    for (nr=0; nr < table->s->keys ; nr++)
13081
13039
    {
13082
13040
      int direction;
13083
 
      if (keys.is_set(nr) &&
 
13041
      if (keys.test(nr) &&
13084
13042
          (direction= test_if_order_by_key(order, table, nr, &used_key_parts)))
13085
13043
      {
13086
 
        bool is_covering= table->covering_keys.is_set(nr) || (nr == table->s->primary_key && table->file->primary_key_is_clustered());
 
13044
        bool is_covering= table->covering_keys.test(nr) || (nr == table->s->primary_key && table->file->primary_key_is_clustered());
13087
13045
 
13088
13046
        /*
13089
13047
          Don't use an index scan with order_st BY without limit.
13166
13124
            ha_rows quick_records= table_records;
13167
13125
            if (is_best_covering && !is_covering)
13168
13126
              continue;
13169
 
            if (table->quick_keys.is_set(nr))
 
13127
            if (table->quick_keys.test(nr))
13170
13128
              quick_records= table->quick_rows[nr];
13171
13129
            if (best_key < 0 ||
13172
13130
                (select_limit <= cmin(quick_records,best_records) ?
13186
13144
    if (best_key >= 0)
13187
13145
    {
13188
13146
      bool quick_created= false;
13189
 
      if (table->quick_keys.is_set(best_key) && best_key != ref_key)
 
13147
      if (table->quick_keys.test(best_key) && best_key != ref_key)
13190
13148
      {
13191
13149
        key_map test_map;
13192
 
        test_map.clear_all();       // Force the creation of quick select
13193
 
        test_map.set_bit(best_key); // only best_key.
 
13150
        test_map.reset();       // Force the creation of quick select
 
13151
        test_map.set(best_key); // only best_key.
13194
13152
        quick_created=
13195
13153
          select->test_quick_select(join->session, test_map, 0,
13196
13154
                                    join->select_options & OPTION_FOUND_ROWS ?
13211
13169
            delete select->quick;
13212
13170
            select->quick= 0;
13213
13171
          }
13214
 
          if (table->covering_keys.is_set(best_key))
 
13172
          if (table->covering_keys.test(best_key))
13215
13173
          {
13216
13174
            table->key_read=1;
13217
13175
            table->file->extra(HA_EXTRA_KEYREAD);
13492
13450
 
13493
13451
  free_io_cache(entry);                         // Safety
13494
13452
  entry->file->info(HA_STATUS_VARIABLE);
13495
 
  if (entry->s->db_type() == heap_hton ||
 
13453
  if (entry->s->db_type() == heap_engine ||
13496
13454
      (!entry->s->blob_fields &&
13497
13455
       ((ALIGN_SIZE(reclength) + HASH_OVERHEAD) * entry->file->stats.records <
13498
13456
        session->variables.sortbuff_size)))
13795
13753
  {
13796
13754
    uint32_t null_fields=0, used_fields;
13797
13755
    Field **f_ptr,*field;
13798
 
    MY_BITMAP *read_set= tables[i].table->read_set;
 
13756
    bitset<MAX_FIELDS> *read_set= tables[i].table->read_set;
13799
13757
    for (f_ptr=tables[i].table->field,used_fields=tables[i].used_fields ;
13800
13758
         used_fields ;
13801
13759
         f_ptr++)
13802
13760
    {
13803
13761
      field= *f_ptr;
13804
 
      if (bitmap_is_set(read_set, field->field_index))
 
13762
      if (read_set->test(field->field_index))
13805
13763
      {
13806
13764
        used_fields--;
13807
13765
        length+=field->fill_cache_field(copy);
13863
13821
            (size_t)cache->length);
13864
13822
  if (!(cache->buff=(unsigned char*) malloc(size)))
13865
13823
    return 1;                           /* Don't use cache */ /* purecov: inspected */
13866
 
  cache->end=cache->buff+size;
 
13824
  cache->end= cache->buff+size;
13867
13825
  reset_cache_write(cache);
13868
13826
  return 0;
13869
13827
}
15881
15839
                                          strlen(join_type_str[tab->type]),
15882
15840
                                          cs));
15883
15841
      /* Build "possible_keys" value and add it to item_list */
15884
 
      if (!tab->keys.is_clear_all())
 
15842
      if (tab->keys.any())
15885
15843
      {
15886
15844
        uint32_t j;
15887
15845
        for (j=0 ; j < table->s->keys ; j++)
15888
15846
        {
15889
 
          if (tab->keys.is_set(j))
 
15847
          if (tab->keys.test(j))
15890
15848
          {
15891
15849
            if (tmp1.length())
15892
15850
              tmp1.append(',');
16010
15968
      /* Build "Extra" field and add it to item_list. */
16011
15969
      bool key_read=table->key_read;
16012
15970
      if ((tab->type == JT_NEXT || tab->type == JT_CONST) &&
16013
 
          table->covering_keys.is_set(tab->index))
 
15971
          table->covering_keys.test(tab->index))
16014
15972
        key_read=1;
16015
15973
      if (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT &&
16016
15974
          !((QUICK_ROR_INTERSECT_SELECT*)tab->select->quick)->need_to_fetch_row)
16059
16017
        {
16060
16018
          if (tab->use_quick == 2)
16061
16019
          {
16062
 
            /* 4 bits per 1 hex digit + terminating '\0' */
16063
 
            char buf[MAX_KEY / 4 + 1];
 
16020
            /*
 
16021
             * To print out the bitset in tab->keys, we go through
 
16022
             * it 32 bits at a time. We need to do this to ensure
 
16023
             * that the to_ulong() method will not throw an
 
16024
             * out_of_range exception at runtime which would happen
 
16025
             * if the bitset we were working with was larger than 64
 
16026
             * bits on a 64-bit platform (for example).
 
16027
             */
 
16028
            stringstream s, w;
 
16029
            string str;
 
16030
            w << tab->keys;
 
16031
            w >> str;
 
16032
            for (uint32_t pos= 0; pos < tab->keys.size(); pos+= 32)
 
16033
            {
 
16034
              bitset<32> tmp(str, pos, 32);
 
16035
              if (tmp.any())
 
16036
                s << uppercase << hex << tmp.to_ulong();
 
16037
            }
16064
16038
            extra.append(STRING_WITH_LEN("; Range checked for each "
16065
16039
                                         "record (index map: 0x"));
16066
 
            extra.append(tab->keys.print(buf));
 
16040
            extra.append(s.str().c_str());
16067
16041
            extra.append(')');
16068
16042
          }
16069
16043
          else if (tab->select->cond)