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"));
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);
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
2110
2106
if (!order && !no_order && (!skip_sort_order || !need_tmp))
2113
Reset 'order' to 'group_list' and reinit variables describing
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;
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,
2126
&join_tab[const_tables].table->
2127
keys_in_use_for_query))))
2113
if (order && (order != group_list || !(select_options & SELECT_BIG_RESULT)))
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)))
2129
2120
having= tmp_having;
2130
select_describe(this, need_tmp,
2131
order != 0 && !skip_sort_order,
2133
!tables ? "No tables used" : NULL);
2121
select_describe(this, need_tmp, order != 0 && !skip_sort_order, select_distinct, !tables ? "No tables used" : NULL);
2182
2169
items1= items0 + all_fields.elements;
2183
2170
if (sort_and_group || curr_tmp_table->group)
2185
if (change_to_use_tmp_fields(session, items1,
2186
tmp_fields_list1, tmp_all_fields1,
2187
fields_list.elements, all_fields))
2172
if (change_to_use_tmp_fields(session, items1,
2173
tmp_fields_list1, tmp_all_fields1,
2174
fields_list.elements, all_fields))
2192
if (change_refs_to_tmp_fields(session, items1,
2193
tmp_fields_list1, tmp_all_fields1,
2194
fields_list.elements, all_fields))
2179
if (change_refs_to_tmp_fields(session, items1,
2180
tmp_fields_list1, tmp_all_fields1,
2181
fields_list.elements, all_fields))
2197
2184
curr_join->tmp_all_fields1= tmp_all_fields1;
2198
2185
curr_join->tmp_fields_list1= tmp_fields_list1;
2205
2192
if (sort_and_group || curr_tmp_table->group)
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;
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;
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;
2232
2217
like SEC_TO_TIME(SUM(...)).
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))
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);
2231
curr_join->tmp_table_param.hidden_field_count= curr_join->tmp_all_fields1.elements
2232
- curr_join->tmp_fields_list1.elements;
2250
2234
if (exec_tmp_table2)
2251
curr_tmp_table= exec_tmp_table2;
2235
curr_tmp_table= exec_tmp_table2;
2254
/* group data to new table */
2238
/* group data to new table */
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;
2264
if (!(curr_tmp_table=
2265
exec_tmp_table2= create_tmp_table(session,
2266
&curr_join->tmp_table_param,
2269
curr_join->select_distinct &&
2270
!curr_join->group_list,
2271
1, curr_join->select_options,
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,
2253
curr_join->select_distinct &&
2254
!curr_join->group_list,
2255
1, curr_join->select_options,
2259
curr_join->exec_tmp_table2= exec_tmp_table2;
2277
2261
if (curr_join->group_list)
2279
session->set_proc_info("Creating sort index");
2280
if (curr_join->join_tab == join_tab && save_join_tab())
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))
2263
session->set_proc_info("Creating sort index");
2264
if (curr_join->join_tab == join_tab && save_join_tab())
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))
2290
2274
sortorder= curr_join->sortorder;
2295
2279
if (curr_join != this)
2299
curr_join->sum_funcs= sum_funcs2;
2300
curr_join->sum_funcs_end= sum_funcs_end2;
2304
curr_join->alloc_func_list();
2305
sum_funcs2= curr_join->sum_funcs;
2306
sum_funcs_end2= curr_join->sum_funcs_end;
2283
curr_join->sum_funcs= sum_funcs2;
2284
curr_join->sum_funcs_end= sum_funcs_end2;
2288
curr_join->alloc_func_list();
2289
sum_funcs2= curr_join->sum_funcs;
2290
sum_funcs_end2= curr_join->sum_funcs_end;
2309
if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
2293
if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list, 1, true))
2312
2295
curr_join->group_list= 0;
2313
if (!curr_join->sort_and_group &&
2314
curr_join->const_tables != curr_join->tables)
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)))
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)))
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
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))
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))
2318
curr_join->tmp_fields_list2= tmp_fields_list2;
2319
curr_join->tmp_all_fields2= tmp_all_fields2;
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;
2344
2327
if (curr_tmp_table->distinct)
2360
2345
if (make_simple_join(curr_join, curr_tmp_table))
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);
2368
2352
if (curr_join->group || curr_join->tmp_table_param.sum_func_count)
2370
2354
if (make_group_fields(this, curr_join))
2377
init_items_ref_array();
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;
2416
2396
table_map used_tables= (curr_join->const_table_map |
2417
2397
curr_table->table->map);
2419
Item* sort_table_cond= make_cond_for_table(curr_join->tmp_having,
2399
Item* sort_table_cond= make_cond_for_table(curr_join->tmp_having, used_tables, used_tables, 0);
2422
2400
if (sort_table_cond)
2424
if (!curr_table->select)
2425
if (!(curr_table->select= new SQL_SELECT))
2427
if (!curr_table->select->cond)
2428
curr_table->select->cond= sort_table_cond;
2429
else // This should never happen
2431
if (!(curr_table->select->cond=
2432
new Item_cond_and(curr_table->select->cond,
2436
Item_cond_and do not need fix_fields for execution, its parameters
2437
are fixed or do not need fix_fields, too
2439
curr_table->select->cond->quick_fix_field();
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,
2402
if (!curr_table->select)
2403
if (!(curr_table->select= new SQL_SELECT))
2405
if (!curr_table->select->cond)
2406
curr_table->select->cond= sort_table_cond;
2407
else // This should never happen
2409
if (!(curr_table->select->cond=
2410
new Item_cond_and(curr_table->select->cond,
2414
Item_cond_and do not need fix_fields for execution, its parameters
2415
are fixed or do not need fix_fields, too
2417
curr_table->select->cond->quick_fix_field();
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,
2450
curr_join->select_limit= HA_POS_ERROR;
2428
curr_join->select_limit= HA_POS_ERROR;
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.
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++)
2462
table->keyuse is set in the case there was an original WHERE clause
2463
on the table that was optimized away.
2465
if (curr_table->select_cond ||
2466
(curr_table->keyuse && !curr_table->first_inner))
2468
/* We have to sort all rows */
2469
curr_join->select_limit= HA_POS_ERROR;
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.
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++)
2440
table->keyuse is set in the case there was an original WHERE clause
2441
on the table that was optimized away.
2443
if (curr_table->select_cond ||
2444
(curr_table->keyuse && !curr_table->first_inner))
2446
/* We have to sort all rows */
2447
curr_join->select_limit= HA_POS_ERROR;
2474
2452
if (curr_join->join_tab == join_tab && save_join_tab())
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.
2487
2463
if (create_sort_index(session, curr_join,
2488
2464
curr_join->group_list ?
2514
2491
curr_join->having= curr_join->tmp_having;
2515
2492
curr_join->fields= curr_fields_list;
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;
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;
2525
2500
/* Accumulate the counts from all join iterations of all join parts. */
2526
2501
session->examined_row_count+= curr_join->examined_rows;
3812
3778
select->quick=0;
3813
3779
if (records == 0 && s->table->reginfo.impossible_range)
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.
3821
join->const_table_map|= s->table->map;
3822
set_position(join,const_count++,s,(KEYUSE*) 0);
3824
if (*s->on_expr_ref)
3826
/* Generate empty row */
3827
s->info= "Impossible ON condition";
3828
found_const_table_map|= s->table->map;
3830
mark_as_null_row(s->table); // All fields are NULL
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.
3787
join->const_table_map|= s->table->map;
3788
set_position(join,const_count++,s,(KEYUSE*) 0);
3790
if (*s->on_expr_ref)
3792
/* Generate empty row */
3793
s->info= "Impossible ON condition";
3794
found_const_table_map|= s->table->map;
3796
mark_as_null_row(s->table); // All fields are NULL
3833
3799
if (records != HA_POS_ERROR)
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);