~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_select.cc

  • Committer: Jay Pipes
  • Date: 2009-03-16 15:20:27 UTC
  • mto: (934.3.7 mordred)
  • mto: This revision was merged to the branch mainline in revision 938.
  • Revision ID: jpipes@serialcoder-20090316152027-njlreaim8vxqta6c
Fixes ENUM field type to throw an error on bad data input.  0 is now not
allowed on insertion.  MySQL allows 0, in the manual it states 0 is "the
null string error index" whatever that means.  Drizzle doesn't allow it.

Corrected test cases.

Also cleans up indentation on JOIN::exec() which was bothering me.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2030
2030
  @todo
2031
2031
    When can we have here session->net.report_error not zero?
2032
2032
*/
2033
 
void
2034
 
JOIN::exec()
 
2033
void JOIN::exec()
2035
2034
{
2036
2035
  List<Item> *columns_list= &fields_list;
2037
2036
  int      tmp_error;
2043
2042
  {                                           
2044
2043
    /* Only test of functions */
2045
2044
    if (select_options & SELECT_DESCRIBE)
2046
 
      select_describe(this, false, false, false,
2047
 
                      (zero_result_cause?zero_result_cause:"No tables used"));
 
2045
      select_describe(this, false, false, false, (zero_result_cause?zero_result_cause:"No tables used"));
2048
2046
    else
2049
2047
    {
2050
 
      result->send_fields(*columns_list,
2051
 
                          Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
 
2048
      result->send_fields(*columns_list, Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
2052
2049
      /*
2053
2050
        We have to test for 'conds' here as the WHERE may not be constant
2054
2051
        even if we don't have any tables for prepared statements or if
2096
2093
    return;
2097
2094
  }
2098
2095
 
2099
 
  if ((this->select_lex->options & OPTION_SCHEMA_TABLE) &&
2100
 
      get_schema_tables_result(this, PROCESSED_BY_JOIN_EXEC))
 
2096
  if ((this->select_lex->options & OPTION_SCHEMA_TABLE) && get_schema_tables_result(this, PROCESSED_BY_JOIN_EXEC))
2101
2097
    return;
2102
2098
 
2103
2099
  if (select_options & SELECT_DESCRIBE)
2109
2105
    */
2110
2106
    if (!order && !no_order && (!skip_sort_order || !need_tmp))
2111
2107
    {
2112
 
      /*
2113
 
        Reset 'order' to 'group_list' and reinit variables describing
2114
 
        'order'
2115
 
      */
 
2108
      /* Reset 'order' to 'group_list' and reinit variables describing 'order' */
2116
2109
      order= group_list;
2117
2110
      simple_order= simple_group;
2118
2111
      skip_sort_order= 0;
2119
2112
    }
2120
 
    if (order &&
2121
 
        (order != group_list || !(select_options & SELECT_BIG_RESULT)) &&
2122
 
        (const_tables == tables ||
2123
 
         ((simple_order || skip_sort_order) &&
2124
 
          test_if_skip_sort_order(&join_tab[const_tables], order,
2125
 
                                  select_limit, 0,
2126
 
                                  &join_tab[const_tables].table->
2127
 
                                    keys_in_use_for_query))))
2128
 
      order=0;
 
2113
    if (order && (order != group_list || !(select_options & SELECT_BIG_RESULT)))
 
2114
    {
 
2115
      if (const_tables == tables 
 
2116
        || ((simple_order || skip_sort_order) 
 
2117
          && test_if_skip_sort_order(&join_tab[const_tables], order, select_limit, 0, &join_tab[const_tables].table->keys_in_use_for_query)))
 
2118
      order= 0;
 
2119
    }
2129
2120
    having= tmp_having;
2130
 
    select_describe(this, need_tmp,
2131
 
                    order != 0 && !skip_sort_order,
2132
 
                    select_distinct,
2133
 
                    !tables ? "No tables used" : NULL);
 
2121
    select_describe(this, need_tmp, order != 0 && !skip_sort_order,  select_distinct, !tables ? "No tables used" : NULL);
2134
2122
    return;
2135
2123
  }
2136
2124
 
2162
2150
 
2163
2151
    /* Copy data to the temporary table */
2164
2152
    session->set_proc_info("Copying to tmp table");
2165
 
    if (!curr_join->sort_and_group &&
2166
 
        curr_join->const_tables != curr_join->tables)
 
2153
    if (! curr_join->sort_and_group && curr_join->const_tables != curr_join->tables)
2167
2154
      curr_join->join_tab[curr_join->const_tables].sorted= 0;
2168
2155
    if ((tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
2169
2156
    {
2182
2169
      items1= items0 + all_fields.elements;
2183
2170
      if (sort_and_group || curr_tmp_table->group)
2184
2171
      {
2185
 
        if (change_to_use_tmp_fields(session, items1,
2186
 
                                     tmp_fields_list1, tmp_all_fields1,
2187
 
                                     fields_list.elements, all_fields))
2188
 
          return;
 
2172
        if (change_to_use_tmp_fields(session, items1,
 
2173
                  tmp_fields_list1, tmp_all_fields1,
 
2174
                  fields_list.elements, all_fields))
 
2175
          return;
2189
2176
      }
2190
2177
      else
2191
2178
      {
2192
 
        if (change_refs_to_tmp_fields(session, items1,
2193
 
                                      tmp_fields_list1, tmp_all_fields1,
2194
 
                                      fields_list.elements, all_fields))
2195
 
          return;
 
2179
        if (change_refs_to_tmp_fields(session, items1,
 
2180
                    tmp_fields_list1, tmp_all_fields1,
 
2181
                    fields_list.elements, all_fields))
 
2182
          return;
2196
2183
      }
2197
2184
      curr_join->tmp_all_fields1= tmp_all_fields1;
2198
2185
      curr_join->tmp_fields_list1= tmp_fields_list1;
2204
2191
 
2205
2192
    if (sort_and_group || curr_tmp_table->group)
2206
2193
    {
2207
 
      curr_join->tmp_table_param.field_count+=
2208
 
        curr_join->tmp_table_param.sum_func_count+
2209
 
        curr_join->tmp_table_param.func_count;
2210
 
      curr_join->tmp_table_param.sum_func_count=
2211
 
        curr_join->tmp_table_param.func_count= 0;
 
2194
      curr_join->tmp_table_param.field_count+= curr_join->tmp_table_param.sum_func_count
 
2195
                                             + curr_join->tmp_table_param.func_count;
 
2196
      curr_join->tmp_table_param.sum_func_count= 0;
 
2197
      curr_join->tmp_table_param.func_count= 0;
2212
2198
    }
2213
2199
    else
2214
2200
    {
2215
 
      curr_join->tmp_table_param.field_count+=
2216
 
        curr_join->tmp_table_param.func_count;
 
2201
      curr_join->tmp_table_param.field_count+= curr_join->tmp_table_param.func_count;
2217
2202
      curr_join->tmp_table_param.func_count= 0;
2218
2203
    }
2219
2204
 
2220
2205
    if (curr_tmp_table->group)
2221
2206
    {                                           // Already grouped
2222
2207
      if (!curr_join->order && !curr_join->no_order && !skip_sort_order)
2223
 
        curr_join->order= curr_join->group_list;  /* order by group */
 
2208
        curr_join->order= curr_join->group_list;  /* order by group */
2224
2209
      curr_join->group_list= 0;
2225
2210
    }
2226
2211
 
2232
2217
      like SEC_TO_TIME(SUM(...)).
2233
2218
    */
2234
2219
 
2235
 
    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))
 
2220
    if ((curr_join->group_list && (!test_if_subpart(curr_join->group_list, curr_join->order) || curr_join->select_distinct)) 
 
2221
        || (curr_join->select_distinct && curr_join->tmp_table_param.using_indirect_summary_function))
2236
2222
    {                                   /* Must copy to another table */
2237
2223
      /* Free first data from old join */
2238
2224
      curr_join->join_free();
2239
2225
      if (make_simple_join(curr_join, curr_tmp_table))
2240
 
        return;
 
2226
        return;
2241
2227
      calc_group_buffer(curr_join, group_list);
2242
2228
      count_field_types(select_lex, &curr_join->tmp_table_param,
2243
2229
                        curr_join->tmp_all_fields1,
2244
2230
                        curr_join->select_distinct && !curr_join->group_list);
2245
 
      curr_join->tmp_table_param.hidden_field_count=
2246
 
        (curr_join->tmp_all_fields1.elements-
2247
 
         curr_join->tmp_fields_list1.elements);
2248
 
 
 
2231
      curr_join->tmp_table_param.hidden_field_count= curr_join->tmp_all_fields1.elements
 
2232
                                                   - curr_join->tmp_fields_list1.elements;
2249
2233
 
2250
2234
      if (exec_tmp_table2)
2251
 
        curr_tmp_table= exec_tmp_table2;
 
2235
        curr_tmp_table= exec_tmp_table2;
2252
2236
      else
2253
2237
      {
2254
 
        /* group data to new table */
 
2238
        /* group data to new table */
2255
2239
 
2256
2240
        /*
2257
2241
          If the access method is loose index scan then all MIN/MAX
2261
2245
        if (curr_join->join_tab->is_using_loose_index_scan())
2262
2246
          curr_join->tmp_table_param.precomputed_group_by= true;
2263
2247
 
2264
 
        if (!(curr_tmp_table=
2265
 
              exec_tmp_table2= create_tmp_table(session,
2266
 
                                                &curr_join->tmp_table_param,
2267
 
                                                *curr_all_fields,
2268
 
                                                (order_st*) 0,
2269
 
                                                curr_join->select_distinct &&
2270
 
                                                !curr_join->group_list,
2271
 
                                                1, curr_join->select_options,
2272
 
                                                HA_POS_ERROR,
2273
 
                                                (char *) "")))
2274
 
          return;
2275
 
        curr_join->exec_tmp_table2= exec_tmp_table2;
 
2248
        if (!(curr_tmp_table=
 
2249
              exec_tmp_table2= create_tmp_table(session,
 
2250
                                                &curr_join->tmp_table_param,
 
2251
                                                *curr_all_fields,
 
2252
                                                (order_st*) 0,
 
2253
                                                curr_join->select_distinct &&
 
2254
                                                !curr_join->group_list,
 
2255
                                                1, curr_join->select_options,
 
2256
                                                HA_POS_ERROR,
 
2257
                                                (char *) "")))
 
2258
          return;
 
2259
        curr_join->exec_tmp_table2= exec_tmp_table2;
2276
2260
      }
2277
2261
      if (curr_join->group_list)
2278
2262
      {
2279
 
        session->set_proc_info("Creating sort index");
2280
 
        if (curr_join->join_tab == join_tab && save_join_tab())
2281
 
        {
2282
 
          return;
2283
 
        }
2284
 
        if (create_sort_index(session, curr_join, curr_join->group_list,
2285
 
                              HA_POS_ERROR, HA_POS_ERROR, false) ||
2286
 
            make_group_fields(this, curr_join))
2287
 
        {
2288
 
          return;
2289
 
        }
 
2263
        session->set_proc_info("Creating sort index");
 
2264
        if (curr_join->join_tab == join_tab && save_join_tab())
 
2265
        {
 
2266
          return;
 
2267
        }
 
2268
        if (create_sort_index(session, curr_join, curr_join->group_list,
 
2269
                  HA_POS_ERROR, HA_POS_ERROR, false) ||
 
2270
            make_group_fields(this, curr_join))
 
2271
        {
 
2272
          return;
 
2273
        }
2290
2274
        sortorder= curr_join->sortorder;
2291
2275
      }
2292
2276
 
2294
2278
      tmp_error= -1;
2295
2279
      if (curr_join != this)
2296
2280
      {
2297
 
        if (sum_funcs2)
2298
 
        {
2299
 
          curr_join->sum_funcs= sum_funcs2;
2300
 
          curr_join->sum_funcs_end= sum_funcs_end2;
2301
 
        }
2302
 
        else
2303
 
        {
2304
 
          curr_join->alloc_func_list();
2305
 
          sum_funcs2= curr_join->sum_funcs;
2306
 
          sum_funcs_end2= curr_join->sum_funcs_end;
2307
 
        }
 
2281
        if (sum_funcs2)
 
2282
        {
 
2283
          curr_join->sum_funcs= sum_funcs2;
 
2284
          curr_join->sum_funcs_end= sum_funcs_end2;
 
2285
        }
 
2286
        else
 
2287
        {
 
2288
          curr_join->alloc_func_list();
 
2289
          sum_funcs2= curr_join->sum_funcs;
 
2290
          sum_funcs_end2= curr_join->sum_funcs_end;
 
2291
        }
2308
2292
      }
2309
 
      if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
2310
 
                                        1, true))
 
2293
      if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list, 1, true))
2311
2294
        return;
2312
2295
      curr_join->group_list= 0;
2313
 
      if (!curr_join->sort_and_group &&
2314
 
          curr_join->const_tables != curr_join->tables)
 
2296
 
 
2297
      if (!curr_join->sort_and_group && (curr_join->const_tables != curr_join->tables))
2315
2298
        curr_join->join_tab[curr_join->const_tables].sorted= 0;
2316
 
      if (setup_sum_funcs(curr_join->session, curr_join->sum_funcs) ||
2317
 
          (tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
 
2299
      
 
2300
      if (setup_sum_funcs(curr_join->session, curr_join->sum_funcs) 
 
2301
        || (tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
2318
2302
      {
2319
 
        error= tmp_error;
2320
 
        return;
 
2303
        error= tmp_error;
 
2304
        return;
2321
2305
      }
2322
2306
      end_read_record(&curr_join->join_tab->read_record);
2323
2307
      curr_join->const_tables= curr_join->tables; // Mark free for cleanup()
2326
2310
      // No sum funcs anymore
2327
2311
      if (!items2)
2328
2312
      {
2329
 
        items2= items1 + all_fields.elements;
2330
 
        if (change_to_use_tmp_fields(session, items2,
2331
 
                                     tmp_fields_list2, tmp_all_fields2,
2332
 
                                     fields_list.elements, tmp_all_fields1))
2333
 
          return;
2334
 
        curr_join->tmp_fields_list2= tmp_fields_list2;
2335
 
        curr_join->tmp_all_fields2= tmp_all_fields2;
 
2313
        items2= items1 + all_fields.elements;
 
2314
        if (change_to_use_tmp_fields(session, items2,
 
2315
                  tmp_fields_list2, tmp_all_fields2,
 
2316
                  fields_list.elements, tmp_all_fields1))
 
2317
          return;
 
2318
        curr_join->tmp_fields_list2= tmp_fields_list2;
 
2319
        curr_join->tmp_all_fields2= tmp_all_fields2;
2336
2320
      }
2337
2321
      curr_fields_list= &curr_join->tmp_fields_list2;
2338
2322
      curr_all_fields= &curr_join->tmp_all_fields2;
2339
2323
      curr_join->set_items_ref_array(items2);
2340
 
      curr_join->tmp_table_param.field_count+=
2341
 
        curr_join->tmp_table_param.sum_func_count;
 
2324
      curr_join->tmp_table_param.field_count+= curr_join->tmp_table_param.sum_func_count;
2342
2325
      curr_join->tmp_table_param.sum_func_count= 0;
2343
2326
    }
2344
2327
    if (curr_tmp_table->distinct)
2349
2332
    {
2350
2333
      session->set_proc_info("Removing duplicates");
2351
2334
      if (curr_join->tmp_having)
2352
 
        curr_join->tmp_having->update_used_tables();
 
2335
        curr_join->tmp_having->update_used_tables();
 
2336
 
2353
2337
      if (remove_duplicates(curr_join, curr_tmp_table,
2354
2338
                            *curr_fields_list, curr_join->tmp_having))
2355
 
        return;
 
2339
        return;
 
2340
      
2356
2341
      curr_join->tmp_having=0;
2357
2342
      curr_join->select_distinct=0;
2358
2343
    }
2360
2345
    if (make_simple_join(curr_join, curr_tmp_table))
2361
2346
      return;
2362
2347
    calc_group_buffer(curr_join, curr_join->group_list);
2363
 
    count_field_types(select_lex, &curr_join->tmp_table_param,
2364
 
                      *curr_all_fields, 0);
 
2348
    count_field_types(select_lex, &curr_join->tmp_table_param, *curr_all_fields, 0);
2365
2349
 
2366
2350
  }
2367
2351
 
2368
2352
  if (curr_join->group || curr_join->tmp_table_param.sum_func_count)
2369
2353
  {
2370
2354
    if (make_group_fields(this, curr_join))
2371
 
    {
2372
2355
      return;
2373
 
    }
2374
 
    if (!items3)
 
2356
 
 
2357
    if (! items3)
2375
2358
    {
2376
 
      if (!items0)
2377
 
        init_items_ref_array();
 
2359
      if (! items0)
 
2360
        init_items_ref_array();
2378
2361
      items3= ref_pointer_array + (all_fields.elements*4);
2379
2362
      setup_copy_fields(session, &curr_join->tmp_table_param,
2380
2363
                        items3, tmp_fields_list3, tmp_all_fields3,
2381
2364
                        curr_fields_list->elements, *curr_all_fields);
2382
2365
      tmp_table_param.save_copy_funcs= curr_join->tmp_table_param.copy_funcs;
2383
2366
      tmp_table_param.save_copy_field= curr_join->tmp_table_param.copy_field;
2384
 
      tmp_table_param.save_copy_field_end=
2385
 
        curr_join->tmp_table_param.copy_field_end;
 
2367
      tmp_table_param.save_copy_field_end= curr_join->tmp_table_param.copy_field_end;
2386
2368
      curr_join->tmp_all_fields3= tmp_all_fields3;
2387
2369
      curr_join->tmp_fields_list3= tmp_fields_list3;
2388
2370
    }
2390
2372
    {
2391
2373
      curr_join->tmp_table_param.copy_funcs= tmp_table_param.save_copy_funcs;
2392
2374
      curr_join->tmp_table_param.copy_field= tmp_table_param.save_copy_field;
2393
 
      curr_join->tmp_table_param.copy_field_end=
2394
 
        tmp_table_param.save_copy_field_end;
 
2375
      curr_join->tmp_table_param.copy_field_end= tmp_table_param.save_copy_field_end;
2395
2376
    }
2396
2377
    curr_fields_list= &tmp_fields_list3;
2397
2378
    curr_all_fields= &tmp_all_fields3;
2407
2388
  {
2408
2389
    session->set_proc_info("Sorting result");
2409
2390
    /* If we have already done the group, add HAVING to sorted table */
2410
 
    if (curr_join->tmp_having && ! curr_join->group_list &&
2411
 
        ! curr_join->sort_and_group)
 
2391
    if (curr_join->tmp_having && ! curr_join->group_list &&     ! curr_join->sort_and_group)
2412
2392
    {
2413
2393
      // Some tables may have been const
2414
2394
      curr_join->tmp_having->update_used_tables();
2416
2396
      table_map used_tables= (curr_join->const_table_map |
2417
2397
                              curr_table->table->map);
2418
2398
 
2419
 
      Item* sort_table_cond= make_cond_for_table(curr_join->tmp_having,
2420
 
                                                 used_tables,
2421
 
                                                 used_tables, 0);
 
2399
      Item* sort_table_cond= make_cond_for_table(curr_join->tmp_having, used_tables, used_tables, 0);
2422
2400
      if (sort_table_cond)
2423
2401
      {
2424
 
        if (!curr_table->select)
2425
 
          if (!(curr_table->select= new SQL_SELECT))
2426
 
            return;
2427
 
        if (!curr_table->select->cond)
2428
 
          curr_table->select->cond= sort_table_cond;
2429
 
        else                                    // This should never happen
2430
 
        {
2431
 
          if (!(curr_table->select->cond=
2432
 
                new Item_cond_and(curr_table->select->cond,
2433
 
                                  sort_table_cond)))
2434
 
            return;
2435
 
          /*
2436
 
            Item_cond_and do not need fix_fields for execution, its parameters
2437
 
            are fixed or do not need fix_fields, too
2438
 
          */
2439
 
          curr_table->select->cond->quick_fix_field();
2440
 
        }
2441
 
        curr_table->select_cond= curr_table->select->cond;
2442
 
        curr_table->select_cond->top_level_item();
2443
 
        curr_join->tmp_having= make_cond_for_table(curr_join->tmp_having,
2444
 
                                                   ~ (table_map) 0,
2445
 
                                                   ~used_tables, 0);
 
2402
        if (!curr_table->select)
 
2403
          if (!(curr_table->select= new SQL_SELECT))
 
2404
            return;
 
2405
        if (!curr_table->select->cond)
 
2406
          curr_table->select->cond= sort_table_cond;
 
2407
        else                                    // This should never happen
 
2408
        {
 
2409
          if (!(curr_table->select->cond=
 
2410
          new Item_cond_and(curr_table->select->cond,
 
2411
                sort_table_cond)))
 
2412
            return;
 
2413
          /*
 
2414
            Item_cond_and do not need fix_fields for execution, its parameters
 
2415
            are fixed or do not need fix_fields, too
 
2416
          */
 
2417
          curr_table->select->cond->quick_fix_field();
 
2418
        }
 
2419
        curr_table->select_cond= curr_table->select->cond;
 
2420
        curr_table->select_cond->top_level_item();
 
2421
        curr_join->tmp_having= make_cond_for_table(curr_join->tmp_having,
 
2422
                    ~ (table_map) 0,
 
2423
                    ~used_tables, 0);
2446
2424
      }
2447
2425
    }
2448
2426
    {
2449
2427
      if (group)
2450
 
        curr_join->select_limit= HA_POS_ERROR;
 
2428
        curr_join->select_limit= HA_POS_ERROR;
2451
2429
      else
2452
2430
      {
2453
 
        /*
2454
 
          We can abort sorting after session->select_limit rows if we there is no
2455
 
          WHERE clause for any tables after the sorted one.
2456
 
        */
2457
 
        JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables+1];
2458
 
        JOIN_TAB *end_table= &curr_join->join_tab[curr_join->tables];
2459
 
        for (; curr_table < end_table ; curr_table++)
2460
 
        {
2461
 
          /*
2462
 
            table->keyuse is set in the case there was an original WHERE clause
2463
 
            on the table that was optimized away.
2464
 
          */
2465
 
          if (curr_table->select_cond ||
2466
 
              (curr_table->keyuse && !curr_table->first_inner))
2467
 
          {
2468
 
            /* We have to sort all rows */
2469
 
            curr_join->select_limit= HA_POS_ERROR;
2470
 
            break;
2471
 
          }
2472
 
        }
 
2431
        /*
 
2432
          We can abort sorting after session->select_limit rows if we there is no
 
2433
          WHERE clause for any tables after the sorted one.
 
2434
        */
 
2435
        JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables+1];
 
2436
        JOIN_TAB *end_table= &curr_join->join_tab[curr_join->tables];
 
2437
        for (; curr_table < end_table ; curr_table++)
 
2438
        {
 
2439
          /*
 
2440
            table->keyuse is set in the case there was an original WHERE clause
 
2441
            on the table that was optimized away.
 
2442
          */
 
2443
          if (curr_table->select_cond ||
 
2444
              (curr_table->keyuse && !curr_table->first_inner))
 
2445
          {
 
2446
            /* We have to sort all rows */
 
2447
            curr_join->select_limit= HA_POS_ERROR;
 
2448
            break;
 
2449
          }
 
2450
        }
2473
2451
      }
2474
2452
      if (curr_join->join_tab == join_tab && save_join_tab())
2475
 
      {
2476
 
        return;
2477
 
      }
 
2453
        return;
2478
2454
      /*
2479
 
        Here we sort rows for order_st BY/GROUP BY clause, if the optimiser
2480
 
        chose FILESORT to be faster than INDEX SCAN or there is no
2481
 
        suitable index present.
2482
 
        Note, that create_sort_index calls test_if_skip_sort_order and may
2483
 
        finally replace sorting with index scan if there is a LIMIT clause in
2484
 
        the query. XXX: it's never shown in EXPLAIN!
2485
 
        OPTION_FOUND_ROWS supersedes LIMIT and is taken into account.
 
2455
        Here we sort rows for order_st BY/GROUP BY clause, if the optimiser
 
2456
        chose FILESORT to be faster than INDEX SCAN or there is no
 
2457
        suitable index present.
 
2458
        Note, that create_sort_index calls test_if_skip_sort_order and may
 
2459
        finally replace sorting with index scan if there is a LIMIT clause in
 
2460
        the query. XXX: it's never shown in EXPLAIN!
 
2461
        OPTION_FOUND_ROWS supersedes LIMIT and is taken into account.
2486
2462
      */
2487
2463
      if (create_sort_index(session, curr_join,
2488
2464
                            curr_join->group_list ?
2491
2467
                            (select_options & OPTION_FOUND_ROWS ?
2492
2468
                             HA_POS_ERROR : unit->select_limit_cnt),
2493
2469
                            curr_join->group_list ? true : false))
2494
 
        return;
 
2470
        return;
 
2471
 
2495
2472
      sortorder= curr_join->sortorder;
2496
2473
      if (curr_join->const_tables != curr_join->tables &&
2497
2474
          !curr_join->join_tab[curr_join->const_tables].table->sort.io_cache)
2514
2491
  curr_join->having= curr_join->tmp_having;
2515
2492
  curr_join->fields= curr_fields_list;
2516
2493
 
2517
 
  {
2518
 
    session->set_proc_info("Sending data");
2519
 
    result->send_fields(*curr_fields_list,
2520
 
                        Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
2521
 
    error= do_select(curr_join, curr_fields_list, NULL);
2522
 
    session->limit_found_rows= curr_join->send_records;
2523
 
  }
 
2494
  session->set_proc_info("Sending data");
 
2495
  result->send_fields(*curr_fields_list,
 
2496
                      Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
 
2497
  error= do_select(curr_join, curr_fields_list, NULL);
 
2498
  session->limit_found_rows= curr_join->send_records;
2524
2499
 
2525
2500
  /* Accumulate the counts from all join iterations of all join parts. */
2526
2501
  session->examined_row_count+= curr_join->examined_rows;
2530
2505
    for a derived table which is always materialized.
2531
2506
    Otherwise we would not be able to print the query  correctly.
2532
2507
  */
2533
 
  if (items0 &&
2534
 
      (session->lex->describe & DESCRIBE_EXTENDED) &&
2535
 
      select_lex->linkage == DERIVED_TABLE_TYPE)
 
2508
  if (items0 && (session->lex->describe & DESCRIBE_EXTENDED) && select_lex->linkage == DERIVED_TABLE_TYPE)
2536
2509
    set_items_ref_array(items0);
2537
2510
 
2538
2511
  return;
2539
2512
}
2540
2513
 
2541
 
 
2542
2514
/**
2543
2515
  Clean up join.
2544
2516
 
2545
2517
  @return
2546
2518
    Return error that hold JOIN.
2547
2519
*/
2548
 
 
2549
 
int
2550
 
JOIN::destroy()
 
2520
int JOIN::destroy()
2551
2521
{
2552
2522
  select_lex->join= 0;
2553
2523
 
2557
2527
    {
2558
2528
      JOIN_TAB *tab, *end;
2559
2529
      for (tab= join_tab, end= tab+tables ; tab != end ; tab++)
2560
 
        tab->cleanup();
 
2530
        tab->cleanup();
2561
2531
    }
2562
2532
    tmp_join->tmp_join= 0;
2563
2533
    tmp_table_param.copy_field=0;
2575
2545
  return(error);
2576
2546
}
2577
2547
 
2578
 
 
2579
 
 
2580
2548
/**
2581
2549
  An entry point to single-unit select (a select without UNION).
2582
2550
 
3162
3130
  return(false);
3163
3131
}
3164
3132
 
3165
 
 
3166
3133
/**
3167
3134
  Setup for execution all subqueries of a query, for which the optimizer
3168
3135
  chose hash semi-join.
3185
3152
  @retval false     success.
3186
3153
  @retval true      error occurred.
3187
3154
*/
3188
 
 
3189
3155
bool JOIN::setup_subquery_materialization()
3190
3156
{
3191
3157
  for (Select_Lex_Unit *un= select_lex->first_inner_unit(); un;
3812
3778
      select->quick=0;
3813
3779
      if (records == 0 && s->table->reginfo.impossible_range)
3814
3780
      {
3815
 
        /*
3816
 
          Impossible WHERE or ON expression
3817
 
          In case of ON, we mark that the we match one empty NULL row.
3818
 
          In case of WHERE, don't set found_const_table_map to get the
3819
 
          caller to abort with a zero row result.
3820
 
        */
3821
 
        join->const_table_map|= s->table->map;
3822
 
        set_position(join,const_count++,s,(KEYUSE*) 0);
3823
 
        s->type= JT_CONST;
3824
 
        if (*s->on_expr_ref)
3825
 
        {
3826
 
          /* Generate empty row */
3827
 
          s->info= "Impossible ON condition";
3828
 
          found_const_table_map|= s->table->map;
3829
 
          s->type= JT_CONST;
3830
 
          mark_as_null_row(s->table);           // All fields are NULL
3831
 
        }
 
3781
        /*
 
3782
          Impossible WHERE or ON expression
 
3783
          In case of ON, we mark that the we match one empty NULL row.
 
3784
          In case of WHERE, don't set found_const_table_map to get the
 
3785
          caller to abort with a zero row result.
 
3786
        */
 
3787
        join->const_table_map|= s->table->map;
 
3788
        set_position(join,const_count++,s,(KEYUSE*) 0);
 
3789
        s->type= JT_CONST;
 
3790
        if (*s->on_expr_ref)
 
3791
        {
 
3792
          /* Generate empty row */
 
3793
          s->info= "Impossible ON condition";
 
3794
          found_const_table_map|= s->table->map;
 
3795
          s->type= JT_CONST;
 
3796
          mark_as_null_row(s->table);           // All fields are NULL
 
3797
        }
3832
3798
      }
3833
3799
      if (records != HA_POS_ERROR)
3834
3800
      {
3835
 
        s->found_records=records;
3836
 
        s->read_time= (ha_rows) (s->quick ? s->quick->read_time : 0.0);
 
3801
        s->found_records=records;
 
3802
        s->read_time= (ha_rows) (s->quick ? s->quick->read_time : 0.0);
3837
3803
      }
3838
3804
      delete select;
3839
3805
    }