52
52
#include "drizzled/records.h"
53
53
#include "drizzled/internal/iocache.h"
54
54
#include "drizzled/drizzled.h"
55
#include "drizzled/plugin/storage_engine.h"
57
56
#include "drizzled/sql_union.h"
58
57
#include "drizzled/optimizer/key_field.h"
79
78
static Item* part_of_refkey(Table *form,Field *field);
80
79
static bool cmp_buffer_with_ref(JoinTable *tab);
81
80
static void change_cond_ref_to_const(Session *session,
82
list<COND_CMP>& save_list,
81
vector<COND_CMP>& save_list,
132
131
unit->set_limit(unit->global_parameters);
133
132
session->session_marker= 0;
135
'options' of select_query will be set in JOIN, as far as JOIN for
134
'options' of mysql_select will be set in JOIN, as far as JOIN for
136
135
every PS/SP execution new, we will not need reset this flag if
137
136
setup_tables_done_option changed for next rexecution
139
res= select_query(session,
140
&select_lex->ref_pointer_array,
138
res= mysql_select(session, &select_lex->ref_pointer_array,
141
139
(TableList*) select_lex->table_list.first,
142
select_lex->with_wild,
143
select_lex->item_list,
140
select_lex->with_wild, select_lex->item_list,
144
141
select_lex->where,
145
142
select_lex->order_list.elements +
146
143
select_lex->group_list.elements,
272
269
/*****************************************************************************
273
270
Check fields, find best join, do the select and output fields.
274
select_query assumes that all tables are already opened
271
mysql_select assumes that all tables are already opened
275
272
*****************************************************************************/
573
570
sz= sizeof(optimizer::KeyField) *
574
571
(((session->lex->current_select->cond_count+1)*2 +
575
572
session->lex->current_select->between_count)*m+1);
576
if (! (key_fields= (optimizer::KeyField*) session->getMemRoot()->allocate(sz)))
573
if (! (key_fields= (optimizer::KeyField*) session->alloc(sz)))
579
576
field= end= key_fields;
934
931
we should restore old value of count_cuted_fields because
935
store_val_in_field can be called from insert_query
932
store_val_in_field can be called from mysql_insert
936
933
with select_insert, which make count_cuted_fields= 1
938
935
enum_check_fields old_count_cuted_fields= session->count_cuted_fields;
1005
1002
j->ref.key_length=length;
1006
1003
j->ref.key=(int) key;
1007
1004
if (!(j->ref.key_buff= (unsigned char*) session->calloc(ALIGN_SIZE(length)*2)) ||
1008
!(j->ref.key_copy= (StoredKey**) session->getMemRoot()->allocate((sizeof(StoredKey*) *
1005
!(j->ref.key_copy= (StoredKey**) session->alloc((sizeof(StoredKey*) *
1009
1006
(keyparts+1)))) ||
1010
!(j->ref.items= (Item**) session->getMemRoot()->allocate(sizeof(Item*)*keyparts)) ||
1011
!(j->ref.cond_guards= (bool**) session->getMemRoot()->allocate(sizeof(uint*)*keyparts)))
1007
!(j->ref.items= (Item**) session->alloc(sizeof(Item*)*keyparts)) ||
1008
!(j->ref.cond_guards= (bool**) session->alloc(sizeof(uint*)*keyparts)))
2308
2305
@param cond condition whose multiple equalities are to be checked
2309
2306
@param table constant table that has been read
2311
void update_const_equal_items(COND *cond, JoinTable *tab)
2308
static void update_const_equal_items(COND *cond, JoinTable *tab)
2313
2310
if (!(cond->used_tables() & tab->table->map))
2490
2486
// Handle other found items
2491
for (list<COND_CMP>::iterator iter= save.begin(); iter != save.end(); ++iter)
2487
for (vector<COND_CMP>::iterator iter= save.begin(); iter != save.end(); ++iter)
2493
Item **args= iter->second->arguments();
2494
if (not args[0]->const_item())
2489
Item **args= iter->cmp_func->arguments();
2490
if (!args[0]->const_item())
2496
change_cond_ref_to_const(session, save, iter->first,
2497
iter->first, args[0], args[1] );
2492
change_cond_ref_to_const( session, save, iter->and_level,
2493
iter->and_level, args[0], args[1] );
2608
2604
1. join->cur_embedding_map - bitmap of pairs of brackets (aka nested
2609
2605
joins) we've opened but didn't close.
2610
2. {each NestedJoin class not simplified away}->counter - number
2606
2. {each nested_join_st structure not simplified away}->counter - number
2611
2607
of this nested join's children that have already been added to to
2612
2608
the partial join order.
3054
3050
table->emptyRecord();
3055
3051
if (table->group && join->tmp_table_param.sum_func_count &&
3056
3052
table->getShare()->sizeKeys() && !table->cursor->inited)
3059
tmp_error= table->cursor->startIndexScan(0, 0);
3062
table->print_error(tmp_error, MYF(0));
3053
table->cursor->startIndexScan(0, 0);
3067
3055
/* Set up select_end */
3068
3056
Next_select_func end_select= setup_end_select_func(join);
3359
int join_read_const_table(JoinTable *tab, optimizer::Position *pos)
3362
Table *table=tab->table;
3363
table->const_table=1;
3365
table->status=STATUS_NO_RECORD;
3367
if (tab->type == AM_SYSTEM)
3369
if ((error=join_read_system(tab)))
3370
{ // Info for DESCRIBE
3371
tab->info="const row not found";
3372
/* Mark for EXPLAIN that the row was not found */
3373
pos->setFanout(0.0);
3374
pos->clearRefDependMap();
3375
if (! table->maybe_null || error > 0)
3381
if (! table->key_read &&
3382
table->covering_keys.test(tab->ref.key) &&
3383
! table->no_keyread &&
3384
(int) table->reginfo.lock_type <= (int) TL_READ_WITH_SHARED_LOCKS)
3387
table->cursor->extra(HA_EXTRA_KEYREAD);
3388
tab->index= tab->ref.key;
3390
error=join_read_const(tab);
3391
if (table->key_read)
3394
table->cursor->extra(HA_EXTRA_NO_KEYREAD);
3398
tab->info="unique row not found";
3399
/* Mark for EXPLAIN that the row was not found */
3400
pos->setFanout(0.0);
3401
pos->clearRefDependMap();
3402
if (!table->maybe_null || error > 0)
3406
if (*tab->on_expr_ref && !table->null_row)
3408
if ((table->null_row= test((*tab->on_expr_ref)->val_int() == 0)))
3409
table->mark_as_null_row();
3411
if (!table->null_row)
3412
table->maybe_null=0;
3414
/* Check appearance of new constant items in Item_equal objects */
3415
Join *join= tab->join;
3417
update_const_equal_items(join->conds, tab);
3419
for (tbl= join->select_lex->leaf_tables; tbl; tbl= tbl->next_leaf)
3421
TableList *embedded;
3422
TableList *embedding= tbl;
3425
embedded= embedding;
3426
if (embedded->on_expr)
3427
update_const_equal_items(embedded->on_expr, tab);
3428
embedding= embedded->getEmbedding();
3431
embedding->getNestedJoin()->join_list.head() == embedded);
3437
int join_read_system(JoinTable *tab)
3439
Table *table= tab->table;
3441
if (table->status & STATUS_GARBAGE) // If first read
3443
if ((error=table->cursor->read_first_row(table->getInsertRecord(),
3444
table->getShare()->getPrimaryKey())))
3446
if (error != HA_ERR_END_OF_FILE)
3447
return table->report_error(error);
3448
tab->table->mark_as_null_row();
3449
table->emptyRecord(); // Make empty record
3452
table->storeRecord();
3454
else if (!table->status) // Only happens with left join
3455
table->restoreRecord(); // restore old record
3457
return table->status ? -1 : 0;
3372
3461
Read a (constant) table when there is at most one matching row.
3440
3529
if (!table->cursor->inited)
3442
error= table->cursor->startIndexScan(tab->ref.key, tab->sorted);
3445
table->print_error(error, MYF(0));
3531
table->cursor->startIndexScan(tab->ref.key, tab->sorted);
3449
3534
/* TODO: Why don't we do "Late NULLs Filtering" here? */
3492
3577
/* Initialize the index first */
3493
3578
if (!table->cursor->inited)
3495
error= table->cursor->startIndexScan(tab->ref.key, tab->sorted);
3497
return table->report_error(error);
3579
table->cursor->startIndexScan(tab->ref.key, tab->sorted);
3500
3581
/* Perform "Late NULLs Filtering" (see internals manual for explanations) */
3501
3582
for (uint32_t i= 0 ; i < tab->ref.key_parts ; i++)
3529
3610
Table *table= tab->table;
3531
3612
if (!table->cursor->inited)
3533
error= table->cursor->startIndexScan(tab->ref.key, tab->sorted);
3535
return table->report_error(error);
3613
table->cursor->startIndexScan(tab->ref.key, tab->sorted);
3537
3614
if (cp_buffer_from_ref(tab->join->session, &tab->ref))
3539
3616
if ((error=table->cursor->index_read_last_map(table->getInsertRecord(),
3648
3725
if (tab->select && tab->select->quick && tab->select->quick->reset())
3651
if (tab->read_record.init_read_record(tab->join->session, tab->table, tab->select, 1, true))
3728
tab->read_record.init_read_record(tab->join->session, tab->table, tab->select, 1, true);
3654
3730
return (*tab->read_record.read_record)(&tab->read_record);
3684
3760
if (!table->cursor->inited)
3686
error= table->cursor->startIndexScan(tab->index, tab->sorted);
3689
table->report_error(error);
3761
table->cursor->startIndexScan(tab->index, tab->sorted);
3693
3762
if ((error=tab->table->cursor->index_first(tab->table->getInsertRecord())))
3695
3764
if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3748
3817
tab->read_record.index=tab->index;
3749
3818
tab->read_record.record=table->getInsertRecord();
3750
3819
if (!table->cursor->inited)
3752
error= table->cursor->startIndexScan(tab->index, 1);
3754
return table->report_error(error);
3820
table->cursor->startIndexScan(tab->index, 1);
3756
3821
if ((error= tab->table->cursor->index_last(tab->table->getInsertRecord())))
3757
3822
return table->report_error(error);
3922
3987
return NESTED_LOOP_ERROR;
3925
if (join->rollup.getState() != Rollup::STATE_NONE)
3990
if (join->rollup.state != ROLLUP::STATE_NONE)
3927
3992
if (join->rollup_write_data((uint32_t) (idx+1), table))
3928
3993
return NESTED_LOOP_ERROR;
5364
5425
if (!count || count > fields.elements)
5366
5427
my_error(ER_BAD_FIELD_ERROR, MYF(0),
5367
order_item->full_name(), session->where());
5428
order_item->full_name(), session->where);
5370
5431
order->item= ref_pointer_array + count - 1;
5483
5544
List<Item> &all_fields,
5486
session->setWhere("order clause");
5547
session->where="order clause";
5487
5548
for (; order; order=order->next)
5489
5550
if (find_order_in_list(session, ref_pointer_array, tables, order, fields,
5535
5596
uint32_t org_fields=all_fields.elements;
5537
session->setWhere("group statement");
5598
session->where="group statement";
5538
5599
for (ord= order; ord; ord= ord->next)
5540
5601
if (find_order_in_list(session, ref_pointer_array, tables, ord, fields,