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"));
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);
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
2111
2109
if (!order && !no_order && (!skip_sort_order || !need_tmp))
2114
Reset 'order' to 'group_list' and reinit variables describing
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;
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,
2127
&join_tab[const_tables].table->
2128
keys_in_use_for_query))))
2116
if (order && (order != group_list || !(select_options & SELECT_BIG_RESULT)))
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)))
2130
2123
having= tmp_having;
2131
select_describe(this, need_tmp,
2132
order != 0 && !skip_sort_order,
2134
!tables ? "No tables used" : NULL);
2124
select_describe(this, need_tmp, order != 0 && !skip_sort_order, select_distinct, !tables ? "No tables used" : NULL);
2183
2172
items1= items0 + all_fields.elements;
2184
2173
if (sort_and_group || curr_tmp_table->group)
2186
if (change_to_use_tmp_fields(session, items1,
2187
tmp_fields_list1, tmp_all_fields1,
2188
fields_list.elements, all_fields))
2175
if (change_to_use_tmp_fields(session, items1,
2176
tmp_fields_list1, tmp_all_fields1,
2177
fields_list.elements, all_fields))
2193
if (change_refs_to_tmp_fields(session, items1,
2194
tmp_fields_list1, tmp_all_fields1,
2195
fields_list.elements, all_fields))
2182
if (change_refs_to_tmp_fields(session, items1,
2183
tmp_fields_list1, tmp_all_fields1,
2184
fields_list.elements, all_fields))
2198
2187
curr_join->tmp_all_fields1= tmp_all_fields1;
2199
2188
curr_join->tmp_fields_list1= tmp_fields_list1;
2206
2195
if (sort_and_group || curr_tmp_table->group)
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;
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;
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;
2233
2220
like SEC_TO_TIME(SUM(...)).
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))
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);
2234
curr_join->tmp_table_param.hidden_field_count= curr_join->tmp_all_fields1.elements
2235
- curr_join->tmp_fields_list1.elements;
2251
2237
if (exec_tmp_table2)
2252
curr_tmp_table= exec_tmp_table2;
2238
curr_tmp_table= exec_tmp_table2;
2255
/* group data to new table */
2241
/* group data to new table */
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;
2265
if (!(curr_tmp_table=
2266
exec_tmp_table2= create_tmp_table(session,
2267
&curr_join->tmp_table_param,
2270
curr_join->select_distinct &&
2271
!curr_join->group_list,
2272
1, curr_join->select_options,
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,
2256
curr_join->select_distinct &&
2257
!curr_join->group_list,
2258
1, curr_join->select_options,
2262
curr_join->exec_tmp_table2= exec_tmp_table2;
2278
2264
if (curr_join->group_list)
2280
session->set_proc_info("Creating sort index");
2281
if (curr_join->join_tab == join_tab && save_join_tab())
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))
2266
session->set_proc_info("Creating sort index");
2267
if (curr_join->join_tab == join_tab && save_join_tab())
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))
2291
2277
sortorder= curr_join->sortorder;
2296
2282
if (curr_join != this)
2300
curr_join->sum_funcs= sum_funcs2;
2301
curr_join->sum_funcs_end= sum_funcs_end2;
2305
curr_join->alloc_func_list();
2306
sum_funcs2= curr_join->sum_funcs;
2307
sum_funcs_end2= curr_join->sum_funcs_end;
2286
curr_join->sum_funcs= sum_funcs2;
2287
curr_join->sum_funcs_end= sum_funcs_end2;
2291
curr_join->alloc_func_list();
2292
sum_funcs2= curr_join->sum_funcs;
2293
sum_funcs_end2= curr_join->sum_funcs_end;
2310
if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
2296
if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list, 1, true))
2313
2298
curr_join->group_list= 0;
2314
if (!curr_join->sort_and_group &&
2315
curr_join->const_tables != curr_join->tables)
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)))
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)))
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
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))
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))
2321
curr_join->tmp_fields_list2= tmp_fields_list2;
2322
curr_join->tmp_all_fields2= tmp_all_fields2;
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;
2345
2330
if (curr_tmp_table->distinct)
2361
2348
if (make_simple_join(curr_join, curr_tmp_table))
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);
2369
2355
if (curr_join->group || curr_join->tmp_table_param.sum_func_count)
2371
2357
if (make_group_fields(this, curr_join))
2378
init_items_ref_array();
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;
2417
2399
table_map used_tables= (curr_join->const_table_map |
2418
2400
curr_table->table->map);
2420
Item* sort_table_cond= make_cond_for_table(curr_join->tmp_having,
2402
Item* sort_table_cond= make_cond_for_table(curr_join->tmp_having, used_tables, used_tables, 0);
2423
2403
if (sort_table_cond)
2425
if (!curr_table->select)
2426
if (!(curr_table->select= new SQL_SELECT))
2428
if (!curr_table->select->cond)
2429
curr_table->select->cond= sort_table_cond;
2430
else // This should never happen
2432
if (!(curr_table->select->cond=
2433
new Item_cond_and(curr_table->select->cond,
2437
Item_cond_and do not need fix_fields for execution, its parameters
2438
are fixed or do not need fix_fields, too
2440
curr_table->select->cond->quick_fix_field();
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,
2405
if (!curr_table->select)
2406
if (!(curr_table->select= new SQL_SELECT))
2408
if (!curr_table->select->cond)
2409
curr_table->select->cond= sort_table_cond;
2410
else // This should never happen
2412
if (!(curr_table->select->cond=
2413
new Item_cond_and(curr_table->select->cond,
2417
Item_cond_and do not need fix_fields for execution, its parameters
2418
are fixed or do not need fix_fields, too
2420
curr_table->select->cond->quick_fix_field();
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,
2451
curr_join->select_limit= HA_POS_ERROR;
2431
curr_join->select_limit= HA_POS_ERROR;
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.
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++)
2463
table->keyuse is set in the case there was an original WHERE clause
2464
on the table that was optimized away.
2466
if (curr_table->select_cond ||
2467
(curr_table->keyuse && !curr_table->first_inner))
2469
/* We have to sort all rows */
2470
curr_join->select_limit= HA_POS_ERROR;
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.
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++)
2443
table->keyuse is set in the case there was an original WHERE clause
2444
on the table that was optimized away.
2446
if (curr_table->select_cond ||
2447
(curr_table->keyuse && !curr_table->first_inner))
2449
/* We have to sort all rows */
2450
curr_join->select_limit= HA_POS_ERROR;
2475
2455
if (curr_join->join_tab == join_tab && save_join_tab())
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.
2488
2466
if (create_sort_index(session, curr_join,
2489
2467
curr_join->group_list ?
2515
2494
curr_join->having= curr_join->tmp_having;
2516
2495
curr_join->fields= curr_fields_list;
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;
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;
2526
2503
/* Accumulate the counts from all join iterations of all join parts. */
2527
2504
session->examined_row_count+= curr_join->examined_rows;
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 ?
3693
const_ref.clear_all();
3694
eq_part.clear_all();
3697
3665
if (keyuse->val->type() != Item::NULL_ITEM && !keyuse->optimize)
3699
3667
if (!((~found_const_table_map) & keyuse->used_tables))
3700
const_ref.set_bit(keyuse->keypart);
3668
const_ref.set(keyuse->keypart);
3702
3670
refs|=keyuse->used_tables;
3703
eq_part.set_bit(keyuse->keypart);
3671
eq_part.set(keyuse->keypart);
3706
3674
} while (keyuse->table == table && keyuse->key == key);
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)
3711
3679
if ((table->key_info[key].flags & (HA_NOSAME))
3813
3781
select->quick=0;
3814
3782
if (records == 0 && s->table->reginfo.impossible_range)
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.
3822
join->const_table_map|= s->table->map;
3823
set_position(join,const_count++,s,(KEYUSE*) 0);
3825
if (*s->on_expr_ref)
3827
/* Generate empty row */
3828
s->info= "Impossible ON condition";
3829
found_const_table_map|= s->table->map;
3831
s->table->mark_as_null_row(); // All fields are NULL
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.
3790
join->const_table_map|= s->table->map;
3791
set_position(join,const_count++,s,(KEYUSE*) 0);
3793
if (*s->on_expr_ref)
3795
/* Generate empty row */
3796
s->info= "Impossible ON condition";
3797
found_const_table_map|= s->table->map;
3799
s->table->mark_as_null_row(); // All fields are NULL
3834
3802
if (records != HA_POS_ERROR)
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);
7199
7166
sel->needed_reg=tab->needed_reg;
7200
sel->quick_keys.clear_all();
7167
sel->quick_keys.reset();
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))
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)))) ?
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;
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.
9455
if (!possible_keys.is_clear_all())
9422
if (possible_keys.any())
9457
9424
Table *field_tab= field->table;
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 ==
9463
9430
field_tab->const_key_parts[use->key]|= use->keypart_map;
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
11421
update_virtual_fields_marked_for_write(table);
11422
store_record(table,record[1]);
11388
table->storeRecord();
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;
16060
16018
if (tab->use_quick == 2)
16062
/* 4 bits per 1 hex digit + terminating '\0' */
16063
char buf[MAX_KEY / 4 + 1];
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).
16032
for (uint32_t pos= 0; pos < tab->keys.size(); pos+= 32)
16034
bitset<32> tmp(str, pos, 32);
16036
s << uppercase << hex << tmp.to_ulong();
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(')');
16069
16043
else if (tab->select->cond)