1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; version 2 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1
/* Copyright (C) 2000-2006 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
28
25
#pragma interface /* gcc class implementation */
31
/* PREV_BITS only used in sql_select.cc */
32
#define PREV_BITS(type,A) ((type) (((type) 1 << (A)) -1))
34
#include <storage/myisam/myisam.h>
36
30
/* Values in optimize */
37
31
#define KEY_OPTIMIZE_EXISTS 1
38
32
#define KEY_OPTIMIZE_REF_OR_NULL 2
40
34
typedef struct keyuse_t {
42
36
Item *val; /**< or value if no field */
43
37
table_map used_tables;
45
uint32_t optimize; // 0, or KEY_OPTIMIZE_*
39
uint optimize; // 0, or KEY_OPTIMIZE_*
46
40
key_part_map keypart_map;
47
41
ha_rows ref_table_rows;
55
49
an Item_func_trig_cond. This means the equality (and validity of
56
50
this KEYUSE element) can be turned on and off. The on/off state
57
51
is indicted by the pointed value:
58
*cond_guard == true <=> equality condition is on
59
*cond_guard == false <=> equality condition is off
52
*cond_guard == TRUE <=> equality condition is on
53
*cond_guard == FALSE <=> equality condition is off
61
55
NULL - Otherwise (the source equality can't be turned off)
73
67
typedef struct st_table_ref
76
uint32_t key_parts; ///< num of ...
77
uint32_t key_length; ///< length of key_buff
78
int32_t key; ///< key no
79
unsigned char *key_buff; ///< value to look for with key
80
unsigned char *key_buff2; ///< key_buff+key_length
70
uint key_parts; ///< num of ...
71
uint key_length; ///< length of key_buff
73
uchar *key_buff; ///< value to look for with key
74
uchar *key_buff2; ///< key_buff+key_length
81
75
store_key **key_copy; //
82
76
Item **items; ///< val()'s for each keypart
119
113
Where source data is located (i.e. this points to somewhere in
120
114
tableX->record[0])
123
uint32_t length; /* Length of data at *str, in bytes */
124
uint32_t blob_length; /* Valid IFF blob_field != 0 */
117
uint length; /* Length of data at *str, in bytes */
118
uint blob_length; /* Valid IFF blob_field != 0 */
125
119
Field_blob *blob_field;
126
bool strip; /* true <=> Strip endspaces ?? */
120
bool strip; /* TRUE <=> Strip endspaces ?? */
128
Table *get_rowid; /* _ != NULL <=> */
122
TABLE *get_rowid; /* _ != NULL <=> */
132
126
typedef struct st_join_cache
135
unsigned char *pos; /* Start of free space in the buffer */
137
uint32_t records; /* # of row cominations currently stored in the cache */
129
uchar *pos; /* Start of free space in the buffer */
131
uint records; /* # of row cominations currently stored in the cache */
141
135
Number of fields (i.e. cache_field objects). Those correspond to table
142
136
columns, and there are also special fields for
367
Describes use of one temporary table to weed out join duplicates.
371
- create a temp table
372
- when we reach the weed-out tab, walk through rowid-ed tabs and
374
For each table we need
379
class SJ_TMP_TABLE : public Sql_alloc
382
/* Array of pointers to tables that should be "used" */
400
MI_COLUMNDEF *start_recinfo;
401
MI_COLUMNDEF *recinfo;
403
/* Pointer to next table (next->start_idx > this->end_idx) */
372
408
class JOIN :public Sql_alloc
374
410
JOIN(const JOIN &rhs); /**< not implemented */
377
413
JOIN_TAB *join_tab,**best_ref;
378
414
JOIN_TAB **map2table; ///< mapping between table indexes and JOIN_TABs
379
415
JOIN_TAB *join_tab_save; ///< saved join_tab for subquery reexecution
380
Table **table,**all_tables;
416
TABLE **table,**all_tables;
382
418
The table which has an index that allows to produce the requried ordering.
383
419
A special value of 0x1 means that the ordering will be produced by
384
420
passing 1st non-const table to filesort(). NULL means no such table exists.
386
Table *sort_by_table;
422
TABLE *sort_by_table;
387
423
uint tables; /**< Number of tables in the join */
388
uint32_t outer_tables; /**< Number of tables that are not inside semijoin */
389
uint32_t const_tables;
424
uint outer_tables; /**< Number of tables that are not inside semijoin */
390
426
uint send_group_parts;
391
427
bool sort_and_group,first_record,full_join,group, no_field_update;
392
428
bool do_send_rows;
394
true when we want to resume nested loop iterations when
430
TRUE when we want to resume nested loop iterations when
395
431
fetching data from a cursor
397
433
bool resume_nested_loop;
429
465
Item *tmp_having; ///< To store having when processed temporary table
430
466
Item *having_history; ///< Store having for explain
431
uint64_t select_options;
467
ulonglong select_options;
432
468
select_result *result;
433
469
TMP_TABLE_PARAM tmp_table_param;
435
471
/// unit structure (with global parameters) for this select
436
472
SELECT_LEX_UNIT *unit;
437
473
/// select that processed
438
474
SELECT_LEX *select_lex;
440
true <=> optimizer must not mark any table as a constant table.
476
TRUE <=> optimizer must not mark any table as a constant table.
441
477
This is needed for subqueries in form "a IN (SELECT .. UNION SELECT ..):
442
478
when we optimize the select that reads the results of the union from a
443
479
temporary table, we must not mark the temp. table as constant because
453
489
If we have the GROUP BY statement in the query,
454
490
but the group_list was emptied by optimizer, this
456
492
It happens when fields in the GROUP BY are from
459
495
bool group_optimized_away;
462
simple_xxxxx is set if order_st/GROUP BY doesn't include any references
498
simple_xxxxx is set if ORDER/GROUP BY doesn't include any references
463
499
to other tables than the first non-constant table in the JOIN.
464
It's also set if order_st/GROUP BY is empty.
500
It's also set if ORDER/GROUP BY is empty.
466
502
bool simple_order, simple_group;
468
504
Is set only in case if we have a GROUP BY clause
469
and no order_st BY after constant elimination of 'order'.
505
and no ORDER BY after constant elimination of 'order'.
472
/** Is set if we have a GROUP BY and we have order_st BY on a constant. */
508
/** Is set if we have a GROUP BY and we have ORDER BY on a constant. */
473
509
bool skip_sort_order;
475
511
bool need_tmp, hidden_group_fields;
483
519
List<Item> &fields_list; ///< hold field list passed to mysql_select
486
order_st *order, *group_list, *proc_param; //hold parameters of mysql_select
522
ORDER *order, *group_list, *proc_param; //hold parameters of mysql_select
487
523
COND *conds; // ---"---
488
524
Item *conds_history; // store WHERE for explain
489
TableList *tables_list; ///<hold 'tables' parameter of mysql_select
490
List<TableList> *join_list; ///< list of joined tables in reverse order
525
TABLE_LIST *tables_list; ///<hold 'tables' parameter of mysql_select
526
List<TABLE_LIST> *join_list; ///< list of joined tables in reverse order
491
527
COND_EQUAL *cond_equal;
492
528
SQL_SELECT *select; ///<created in optimisation phase
493
529
JOIN_TAB *return_tab; ///<used only for outer joins
494
530
Item **ref_pointer_array; ///<used pointer reference for this select
495
531
// Copy of above to be used with different lists
496
532
Item **items0, **items1, **items2, **items3, **current_ref_pointer_array;
497
uint32_t ref_pointer_array_size; ///< size of above in bytes
533
uint ref_pointer_array_size; ///< size of above in bytes
498
534
const char *zero_result_cause; ///< not 0 if exec must return zero result
500
536
bool union_part; ///< this subselect is part of union
515
551
excessive memory usage.
517
553
SORT_FIELD *sortorder; // make_unireg_sortorder()
518
Table **table_reexec; // make_simple_join()
554
TABLE **table_reexec; // make_simple_join()
519
555
JOIN_TAB *join_tab_reexec; // make_simple_join()
520
556
/* end of allocation caching storage */
522
JOIN(THD *thd_arg, List<Item> &fields_arg, uint64_t select_options_arg,
558
JOIN(THD *thd_arg, List<Item> &fields_arg, ulonglong select_options_arg,
523
559
select_result *result_arg)
524
560
:fields_list(fields_arg), sj_subselects(thd_arg->mem_root, 4)
526
562
init(thd_arg, fields_arg, select_options_arg, result_arg);
529
void init(THD *thd_arg, List<Item> &fields_arg, uint64_t select_options_arg,
565
void init(THD *thd_arg, List<Item> &fields_arg, ulonglong select_options_arg,
530
566
select_result *result_arg)
532
568
join_tab= join_tab_save= 0;
575
611
all_fields= fields_arg;
576
612
fields_list= fields_arg;
577
memset(&keyuse, 0, sizeof(keyuse));
613
bzero((char*) &keyuse,sizeof(keyuse));
578
614
tmp_table_param.init();
579
615
tmp_table_param.end_write_records= HA_POS_ERROR;
580
616
rollup.state= ROLLUP::STATE_NONE;
581
617
sj_tmp_tables= NULL;
583
no_const_tables= false;
619
no_const_tables= FALSE;
586
int prepare(Item ***rref_pointer_array, TableList *tables, uint32_t wind_num,
587
COND *conds, uint32_t og_num, order_st *order, order_st *group,
588
Item *having, order_st *proc_param, SELECT_LEX *select,
622
int prepare(Item ***rref_pointer_array, TABLE_LIST *tables, uint wind_num,
623
COND *conds, uint og_num, ORDER *order, ORDER *group,
624
Item *having, ORDER *proc_param, SELECT_LEX *select,
589
625
SELECT_LEX_UNIT *unit);
596
632
bool flatten_subqueries();
597
633
bool setup_subquery_materialization();
598
634
bool make_sum_func_list(List<Item> &all_fields, List<Item> &send_fields,
599
bool before_group_by, bool recompute= false);
635
bool before_group_by, bool recompute= FALSE);
601
637
inline void set_items_ref_array(Item **ptr)
603
memcpy(ref_pointer_array, ptr, ref_pointer_array_size);
639
memcpy((char*) ref_pointer_array, (char*) ptr, ref_pointer_array_size);
604
640
current_ref_pointer_array= ptr;
606
642
inline void init_items_ref_array()
613
649
bool rollup_init();
614
650
bool rollup_make_fields(List<Item> &all_fields, List<Item> &fields,
615
651
Item_sum ***func);
616
int rollup_send_data(uint32_t idx);
617
int rollup_write_data(uint32_t idx, Table *table);
652
int rollup_send_data(uint idx);
653
int rollup_write_data(uint idx, TABLE *table);
618
654
void remove_subq_pushed_predicates(Item **where);
620
656
Release memory and, if possible, the open tables held by this execution
652
688
/* Extern functions in sql_select.cc */
653
689
bool store_val_in_field(Field *field, Item *val, enum_check_fields check_flag);
654
Table *create_tmp_table(THD *thd,TMP_TABLE_PARAM *param,List<Item> &fields,
655
order_st *group, bool distinct, bool save_sum_fields,
656
uint64_t select_options, ha_rows rows_limit,
690
TABLE *create_tmp_table(THD *thd,TMP_TABLE_PARAM *param,List<Item> &fields,
691
ORDER *group, bool distinct, bool save_sum_fields,
692
ulonglong select_options, ha_rows rows_limit,
658
void free_tmp_table(THD *thd, Table *entry);
694
void free_tmp_table(THD *thd, TABLE *entry);
659
695
void count_field_types(SELECT_LEX *select_lex, TMP_TABLE_PARAM *param,
660
696
List<Item> &fields, bool reset_with_sum_func);
661
697
bool setup_copy_fields(THD *thd, TMP_TABLE_PARAM *param,
662
698
Item **ref_pointer_array,
663
699
List<Item> &new_list1, List<Item> &new_list2,
664
uint32_t elements, List<Item> &fields);
700
uint elements, List<Item> &fields);
665
701
void copy_fields(TMP_TABLE_PARAM *param);
666
702
void copy_funcs(Item **func_ptr);
703
bool create_myisam_from_heap(THD *thd, TABLE *table,
704
MI_COLUMNDEF *start_recinfo,
705
MI_COLUMNDEF **recinfo,
706
int error, bool ignore_last_dupp_key_error);
707
uint find_shortest_key(TABLE *table, const key_map *usable_keys);
667
708
Field* create_tmp_field_from_field(THD *thd, Field* org_field,
668
const char *name, Table *table,
669
Item_field *item, uint32_t convert_blob_length);
709
const char *name, TABLE *table,
710
Item_field *item, uint convert_blob_length);
671
712
/* functions from opt_sum.cc */
672
713
bool simple_pred(Item_func *func_item, Item **args, bool *inv_order);
673
int opt_sum_query(TableList *tables, List<Item> &all_fields,COND *conds);
714
int opt_sum_query(TABLE_LIST *tables, List<Item> &all_fields,COND *conds);
675
716
/* from sql_delete.cc, used by opt_range.cc */
676
717
extern "C" int refpos_order_cmp(void* arg, const void *a,const void *b);
680
721
class store_key :public Sql_alloc
683
bool null_key; /* true <=> the value of the key has a null part */
724
bool null_key; /* TRUE <=> the value of the key has a null part */
684
725
enum store_key_result { STORE_KEY_OK, STORE_KEY_FATAL, STORE_KEY_CONV };
685
store_key(THD *thd, Field *field_arg, unsigned char *ptr, unsigned char *null, uint32_t length)
726
store_key(THD *thd, Field *field_arg, uchar *ptr, uchar *null, uint length)
686
727
:null_key(0), null_ptr(null), err(0)
688
if (field_arg->type() == DRIZZLE_TYPE_BLOB)
729
if (field_arg->type() == MYSQL_TYPE_BLOB)
691
732
Key segments are always packed with a 2 byte length prefix.
738
779
Copy_field copy_field;
739
780
const char *field_name;
741
store_key_field(THD *thd, Field *to_field_arg, unsigned char *ptr,
742
unsigned char *null_ptr_arg,
743
uint32_t length, Field *from_field, const char *name_arg)
782
store_key_field(THD *thd, Field *to_field_arg, uchar *ptr,
784
uint length, Field *from_field, const char *name_arg)
744
785
:store_key(thd, to_field_arg,ptr,
745
786
null_ptr_arg ? null_ptr_arg : from_field->maybe_null() ? &err
746
: (unsigned char*) 0, length), field_name(name_arg)
787
: (uchar*) 0, length), field_name(name_arg)
770
store_key_item(THD *thd, Field *to_field_arg, unsigned char *ptr,
771
unsigned char *null_ptr_arg, uint32_t length, Item *item_arg)
815
store_key_item(THD *thd, Field *to_field_arg, uchar *ptr,
816
uchar *null_ptr_arg, uint length, Item *item_arg)
772
817
:store_key(thd, to_field_arg, ptr,
773
818
null_ptr_arg ? null_ptr_arg : item_arg->maybe_null ?
774
&err : (unsigned char*) 0, length), item(item_arg)
819
&err : (uchar*) 0, length), item(item_arg)
776
821
const char *name() const { return "func"; }
779
824
enum store_key_result copy_inner()
826
TABLE *table= to_field->table;
827
my_bitmap_map *old_map= dbug_tmp_use_all_columns(table,
781
829
int res= item->save_in_field(to_field, 1);
830
dbug_tmp_restore_column_map(table->write_set, old_map);
782
831
null_key= to_field->is_null() || item->null_value;
783
832
return (err != 0 || res > 2 ? STORE_KEY_FATAL : (store_key_result) res);
792
store_key_const_item(THD *thd, Field *to_field_arg, unsigned char *ptr,
793
unsigned char *null_ptr_arg, uint32_t length,
841
store_key_const_item(THD *thd, Field *to_field_arg, uchar *ptr,
842
uchar *null_ptr_arg, uint length,
795
844
:store_key_item(thd, to_field_arg,ptr,
796
845
null_ptr_arg ? null_ptr_arg : item_arg->maybe_null ?
797
&err : (unsigned char*) 0, length, item_arg), inited(0)
846
&err : (uchar*) 0, length, item_arg), inited(0)
800
849
const char *name() const { return "const"; }
820
bool cp_buffer_from_ref(THD *thd, TABLE_REF *ref);
869
bool cp_buffer_from_ref(THD *thd, TABLE *table, TABLE_REF *ref);
821
870
bool error_if_full_join(JOIN *join);
871
int report_error(TABLE *table, int error);
822
872
int safe_index_read(JOIN_TAB *tab);
823
873
COND *remove_eq_conds(THD *thd, COND *cond, Item::cond_result *cond_value);
824
874
int test_if_item_cache_changed(List<Cached_item> &list);