1
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008-2009 Sun Microsystems, Inc.
4
* Copyright (C) 2008-2009 Sun Microsystems
6
6
* This program is free software; you can redistribute it and/or modify
7
7
* it under the terms of the GNU General Public License as published by
134
133
static bool add_ref_to_table_cond(Session *session, JoinTable *join_tab);
135
134
static void free_blobs(Field **ptr); /* Rename this method...conflicts with another in global namespace... */
137
Join::Join(Session *session_arg,
138
List<Item> &fields_arg,
139
uint64_t select_options_arg,
140
select_result *result_arg) :
152
sort_and_group(false),
156
no_field_update(false),
158
resume_nested_loop(false),
159
no_const_tables(false),
160
select_distinct(false),
161
group_optimized_away(false),
165
skip_sort_order(false),
169
hidden_group_fields(false),
171
found_const_table_map(0),
178
fetch_limit(HA_POS_ERROR),
179
session(session_arg),
180
fields_list(fields_arg),
185
exec_tmp_table1(NULL),
186
exec_tmp_table2(NULL),
191
having_history(NULL),
192
select_options(select_options_arg),
194
lock(session_arg->lock),
196
all_fields(fields_arg),
200
ref_pointer_array(NULL),
205
ref_pointer_array_size(0),
206
zero_result_cause(NULL),
209
join_tab_reexec(NULL)
211
select_distinct= test(select_options & SELECT_DISTINCT);
212
if (&fields_list != &fields_arg) /* only copy if not same*/
213
fields_list= fields_arg;
214
memset(&keyuse, 0, sizeof(keyuse));
215
tmp_table_param.init();
216
tmp_table_param.end_write_records= HA_POS_ERROR;
217
rollup.setState(Rollup::STATE_NONE);
221
* This method is currently only used when a subselect EXPLAIN is performed.
222
* I pulled out the init() method and have simply reset the values to what
223
* was previously in the init() method. See the note about the hack in
226
void Join::reset(Session *session_arg,
227
List<Item> &fields_arg,
228
uint64_t select_options_arg,
229
select_result *result_arg)
242
sort_and_group= false;
246
no_field_update= false;
248
resume_nested_loop= false;
249
no_const_tables= false;
250
select_distinct= false;
251
group_optimized_away= false;
255
skip_sort_order= false;
259
hidden_group_fields= false;
261
found_const_table_map= 0;
268
fetch_limit= HA_POS_ERROR;
269
session= session_arg;
270
fields_list= fields_arg;
275
exec_tmp_table1= NULL;
276
exec_tmp_table2= NULL;
281
having_history= NULL;
282
select_options= select_options_arg;
284
lock= session_arg->lock;
286
all_fields= fields_arg;
290
ref_pointer_array= NULL;
295
ref_pointer_array_size= 0;
296
zero_result_cause= NULL;
299
join_tab_reexec= NULL;
300
select_distinct= test(select_options & SELECT_DISTINCT);
301
if (&fields_list != &fields_arg) /* only copy if not same*/
302
fields_list= fields_arg;
303
memset(&keyuse, 0, sizeof(keyuse));
304
tmp_table_param.init();
305
tmp_table_param.end_write_records= HA_POS_ERROR;
306
rollup.setState(Rollup::STATE_NONE);
309
bool Join::is_top_level_join() const
311
return (unit == &session->lex->unit && (unit->fake_select_lex == 0 ||
312
select_lex == unit->fake_select_lex));
316
137
Prepare of whole select (including sub queries in future).
393
211
nesting_map save_allow_sum_func= session->lex->allow_sum_func;
394
session->setWhere("having clause");
212
session->where="having clause";
395
213
session->lex->allow_sum_func|= 1 << select_lex_arg->nest_level;
396
214
select_lex->having_fix_field= 1;
397
215
bool having_fix_rc= (!having->fixed &&
1164
982
tmp_table_param.hidden_field_count= (all_fields.elements -
1165
983
fields_list.elements);
1166
Order *tmp_group= (((not simple_group) or not (getDebug().test(debug::NO_KEY_GROUP))) ? group_list : (Order*) 0);
984
Order *tmp_group= ((!simple_group &&
985
! (test_flags.test(TEST_NO_KEY_GROUP))) ? group_list :
1169
988
Pushing LIMIT to the temporary table creation is not applicable
1170
989
when there is ORDER BY or GROUP BY or there is no GROUP BY, but
2012
1831
for (sl= tmp_unit->first_select(); sl; sl= sl->next_select())
2014
1833
Item_subselect *subselect= sl->master_unit()->item;
2015
bool full_local= full && (!subselect ||
2016
(subselect->is_evaluated() &&
2017
!subselect->is_uncacheable()));
1834
bool full_local= full && (!subselect || subselect->is_evaluated());
2019
1836
If this join is evaluated, we can fully clean it up and clean up all
2020
1837
its underlying joins even if they are correlated -- they will not be
2226
2036
((Item_sum *)item)->depended_from() == select_lex))
2227
2037
*func++= (Item_sum*) item;
2229
if (before_group_by && rollup.getState() == Rollup::STATE_INITED)
2039
if (before_group_by && rollup.state == ROLLUP::STATE_INITED)
2231
rollup.setState(Rollup::STATE_READY);
2041
rollup.state= ROLLUP::STATE_READY;
2232
2042
if (rollup_make_fields(field_list, send_fields, &func))
2233
return true; // Should never happen
2043
return(true); // Should never happen
2235
else if (rollup.getState() == Rollup::STATE_NONE)
2045
else if (rollup.state == ROLLUP::STATE_NONE)
2237
2047
for (uint32_t i=0 ; i <= send_group_parts ;i++)
2238
2048
sum_funcs_end[i]= func;
2240
else if (rollup.getState() == Rollup::STATE_READY)
2050
else if (rollup.state == ROLLUP::STATE_READY)
2241
2051
return(false); // Don't put end marker
2242
2052
*func=0; // End marker
2258
2069
tmp_table_param.group_parts= send_group_parts;
2260
rollup.setNullItems((Item_null_result**) session->getMemRoot()->allocate((sizeof(Item*) +
2262
sizeof(List<Item>) +
2263
ref_pointer_array_size)
2264
* send_group_parts ));
2265
if (! rollup.getNullItems())
2071
if (!(rollup.null_items= (Item_null_result**) session->alloc((sizeof(Item*) +
2073
sizeof(List<Item>) +
2074
ref_pointer_array_size)
2075
* send_group_parts )))
2270
rollup.setFields((List<Item>*) (rollup.getNullItems() + send_group_parts));
2271
rollup.setRefPointerArrays((Item***) (rollup.getFields() + send_group_parts));
2272
ref_array= (Item**) (rollup.getRefPointerArrays()+send_group_parts);
2078
rollup.fields= (List<Item>*) (rollup.null_items + send_group_parts);
2079
rollup.ref_pointer_arrays= (Item***) (rollup.fields + send_group_parts);
2080
ref_array= (Item**) (rollup.ref_pointer_arrays+send_group_parts);
2275
2083
Prepare space for field list for the different levels
2276
2084
These will be filled up in rollup_make_fields()
2278
for (uint32_t i= 0 ; i < send_group_parts ; i++)
2086
for (i= 0 ; i < send_group_parts ; i++)
2280
rollup.getNullItems()[i]= new (session->mem_root) Item_null_result();
2281
List<Item> *rollup_fields= &rollup.getFields()[i];
2088
rollup.null_items[i]= new (session->mem_root) Item_null_result();
2089
List<Item> *rollup_fields= &rollup.fields[i];
2282
2090
rollup_fields->empty();
2283
rollup.getRefPointerArrays()[i]= ref_array;
2091
rollup.ref_pointer_arrays[i]= ref_array;
2284
2092
ref_array+= all_fields.elements;
2287
for (uint32_t i= 0 ; i < send_group_parts; i++)
2094
for (i= 0 ; i < send_group_parts; i++)
2289
for (uint32_t j= 0 ; j < fields_list.elements ; j++)
2291
rollup.getFields()[i].push_back(rollup.getNullItems()[i]);
2096
for (j=0 ; j < fields_list.elements ; j++)
2097
rollup.fields[i].push_back(rollup.null_items[i]);
2295
2099
List_iterator<Item> it(all_fields);
2297
2101
while ((item= it++))
2398
2202
uint32_t pos= send_group_parts - level -1;
2399
2203
bool real_fields= 0;
2401
List_iterator<Item> new_it(rollup.getFields()[pos]);
2402
Item **ref_array_start= rollup.getRefPointerArrays()[pos];
2205
List_iterator<Item> new_it(rollup.fields[pos]);
2206
Item **ref_array_start= rollup.ref_pointer_arrays[pos];
2403
2207
Order *start_group;
2405
2209
/* Point to first hidden field */
2498
2302
int Join::rollup_send_data(uint32_t idx)
2500
for (uint32_t i= send_group_parts ; i-- > idx ; )
2305
for (i= send_group_parts ; i-- > idx ; )
2502
2307
/* Get reference pointers to sum functions in place */
2503
memcpy(ref_pointer_array, rollup.getRefPointerArrays()[i], ref_pointer_array_size);
2308
memcpy(ref_pointer_array, rollup.ref_pointer_arrays[i],
2309
ref_pointer_array_size);
2505
2310
if ((!having || having->val_int()))
2507
if (send_records < unit->select_limit_cnt && do_send_rows && result->send_data(rollup.getFields()[i]))
2312
if (send_records < unit->select_limit_cnt && do_send_rows &&
2313
result->send_data(rollup.fields[i]))
2511
2315
send_records++;
2514
2318
/* Restore ref_pointer_array */
2515
2319
set_items_ref_array(current_ref_pointer_array);
2539
2342
int Join::rollup_write_data(uint32_t idx, Table *table_arg)
2541
for (uint32_t i= send_group_parts ; i-- > idx ; )
2345
for (i= send_group_parts ; i-- > idx ; )
2543
2347
/* Get reference pointers to sum functions in place */
2544
memcpy(ref_pointer_array, rollup.getRefPointerArrays()[i],
2348
memcpy(ref_pointer_array, rollup.ref_pointer_arrays[i],
2545
2349
ref_pointer_array_size);
2546
2350
if ((!having || having->val_int()))
2548
2352
int write_error;
2550
List_iterator_fast<Item> it(rollup.getFields()[i]);
2354
List_iterator_fast<Item> it(rollup.fields[i]);
2551
2355
while ((item= it++))
2553
2357
if (item->type() == Item::NULL_ITEM && item->is_result_field())
3210
3013
case REAL_RESULT:
3211
3014
key_length+= sizeof(double);
3214
3016
case INT_RESULT:
3215
3017
key_length+= sizeof(int64_t);
3218
3019
case DECIMAL_RESULT:
3219
key_length+= class_decimal_get_binary_size(group_item->max_length -
3020
key_length+= my_decimal_get_binary_size(group_item->max_length -
3220
3021
(group_item->decimals ? 1 : 0),
3221
3022
group_item->decimals);
3224
3024
case STRING_RESULT:
3226
enum enum_field_types type= group_item->field_type();
3026
enum enum_field_types type= group_item->field_type();
3028
As items represented as DATE/TIME fields in the group buffer
3029
have STRING_RESULT result type, we increase the length
3030
by 8 as maximum pack length of such fields.
3032
if (type == DRIZZLE_TYPE_DATE ||
3033
type == DRIZZLE_TYPE_DATETIME ||
3034
type == DRIZZLE_TYPE_TIMESTAMP)
3228
As items represented as DATE/TIME fields in the group buffer
3229
have STRING_RESULT result type, we increase the length
3230
by 8 as maximum pack length of such fields.
3041
Group strings are taken as varstrings and require an length field.
3042
A field is not yet created by create_tmp_field()
3043
and the sizes should match up.
3232
if (type == DRIZZLE_TYPE_DATE ||
3233
type == DRIZZLE_TYPE_TIME ||
3234
type == DRIZZLE_TYPE_DATETIME ||
3235
type == DRIZZLE_TYPE_MICROTIME ||
3236
type == DRIZZLE_TYPE_TIMESTAMP)
3243
Group strings are taken as varstrings and require an length field.
3244
A field is not yet created by create_tmp_field()
3245
and the sizes should match up.
3247
key_length+= group_item->max_length + HA_KEY_BLOB_LENGTH;
3045
key_length+= group_item->max_length + HA_KEY_BLOB_LENGTH;
3254
3050
/* This case should never be choosen */
3256
3052
my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
3262
3056
if (group_item->maybe_null)
3266
3059
join->tmp_table_param.group_length=key_length+null_parts;
3267
3060
join->tmp_table_param.group_parts=parts;
3268
3061
join->tmp_table_param.group_null_parts=null_parts;
3406
3199
table_count=join->tables;
3407
3200
if (!(join->join_tab=join_tab=
3408
(JoinTable*) session->getMemRoot()->allocate(sizeof(JoinTable)*table_count)))
3201
(JoinTable*) session->alloc(sizeof(JoinTable)*table_count)))
3411
for (i= 0; i < table_count; i++)
3412
new (join_tab+i) JoinTable();
3414
3204
join->full_join=0;
3416
3206
used_tables= OUTER_REF_TABLE_BIT; // Outer row is already read
4571
4361
if (!join->join_tab_reexec)
4573
4363
if (!(join->join_tab_reexec=
4574
(JoinTable*) join->session->getMemRoot()->allocate(sizeof(JoinTable))))
4364
(JoinTable*) join->session->alloc(sizeof(JoinTable))))
4576
new (join->join_tab_reexec) JoinTable();
4577
4366
if (join->tmp_join)
4578
4367
join->tmp_join->join_tab_reexec= join->join_tab_reexec;
4976
4760
current_map, 0)))
4978
4762
tab->cache.select= (optimizer::SqlSelect*)
4979
session->getMemRoot()->duplicate((unsigned char*) sel, sizeof(optimizer::SqlSelect));
4763
session->memdup((unsigned char*) sel, sizeof(optimizer::SqlSelect));
4980
4764
tab->cache.select->cond= tmp;
4981
4765
tab->cache.select->read_tables= join->const_table_map;
5704
5488
table_count= join->tables;
5705
5489
stat= (JoinTable*) join->session->calloc(sizeof(JoinTable)*table_count);
5706
stat_ref= (JoinTable**) join->session->getMemRoot()->allocate(sizeof(JoinTable*)*MAX_TABLES);
5707
table_vector= (Table**) join->session->getMemRoot()->allocate(sizeof(Table*)*(table_count*2));
5490
stat_ref= (JoinTable**) join->session->alloc(sizeof(JoinTable*)*MAX_TABLES);
5491
table_vector= (Table**) join->session->alloc(sizeof(Table*)*(table_count*2));
5708
5492
if (! stat || ! stat_ref || ! table_vector)
5768
5552
s->embedding_map.reset();
5771
NestedJoin *nested_join= embedding->getNestedJoin();
5555
nested_join_st *nested_join= embedding->getNestedJoin();
5772
5556
s->embedding_map|= nested_join->nj_map;
5773
5557
s->dependent|= embedding->getDepTables();
5774
5558
embedding= embedding->getEmbedding();
5797
5581
As we use bitmaps to represent the relation the complexity
5798
5582
of the algorithm is O((number of tables)^2).
5800
for (i= 0; i < table_count; i++)
5584
for (i= 0, s= stat ; i < table_count ; i++, s++)
5803
table= stat[i].table;
5805
if (!table->reginfo.join_tab->dependent)
5808
for (j= 0, s= stat; j < table_count; j++, s++)
5586
for (uint32_t j= 0 ; j < table_count ; j++)
5588
table= stat[j].table;
5810
5589
if (s->dependent & table->map)
5812
table_map was_dependent= s->dependent;
5813
5590
s->dependent |= table->reginfo.join_tab->dependent;
5814
if (i > j && s->dependent != was_dependent)
5593
s->table->maybe_null= 1;
5822
5595
/* Catch illegal cross references for outer joins */
5823
5596
for (i= 0, s= stat ; i < table_count ; i++, s++)
5926
5696
join->const_table_map|=table->map;
5927
5697
set_position(join, const_count++, s, (optimizer::KeyUse*) 0);
5928
5698
partial_pos= join->getSpecificPosInPartialPlan(const_count - 1);
5929
if ((tmp= s->joinReadConstTable(partial_pos)))
5699
if ((tmp= join_read_const_table(s, partial_pos)))
5932
5702
return 1; // Fatal error
5978
5748
if (create_ref_for_key(join, s, start_keyuse, found_const_table_map))
5980
5750
partial_pos= join->getSpecificPosInPartialPlan(const_count - 1);
5981
if ((tmp=s->joinReadConstTable(partial_pos)))
5751
if ((tmp=join_read_const_table(s, partial_pos)))
5984
5754
return 1; // Fatal error