17
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
#ifndef DRIZZLED_SQL_SELECT_H
21
#define DRIZZLED_SQL_SELECT_H
23
#include <drizzled/cached_item.h>
24
#include <drizzled/session.h>
25
#include <drizzled/field/varstring.h>
26
#include <drizzled/item/null.h>
24
34
classes to use when handling where clause
27
#ifdef USE_PRAGMA_INTERFACE
28
#pragma interface /* gcc class implementation */
31
38
/* PREV_BITS only used in sql_select.cc */
32
39
#define PREV_BITS(type,A) ((type) (((type) 1 << (A)) -1))
34
#include <storage/myisam/myisam.h>
41
#include <plugin/myisam/myisam.h>
42
#include <drizzled/sql_array.h>
36
44
/* Values in optimize */
37
45
#define KEY_OPTIMIZE_EXISTS 1
52
60
bool null_rejecting;
54
62
!NULL - This KEYUSE was created from an equality that was wrapped into
55
an Item_func_trig_cond. This means the equality (and validity of
56
this KEYUSE element) can be turned on and off. The on/off state
63
an Item_func_trig_cond. This means the equality (and validity of
64
this KEYUSE element) can be turned on and off. The on/off state
57
65
is indicted by the pointed value:
58
66
*cond_guard == true <=> equality condition is on
59
67
*cond_guard == false <=> equality condition is off
80
88
unsigned char *key_buff2; ///< key_buff+key_length
81
89
store_key **key_copy; //
82
90
Item **items; ///< val()'s for each keypart
84
92
Array of pointers to trigger variables. Some/all of the pointers may be
85
93
NULL. The ref access can be used iff
87
for each used key part i, (!cond_guards[i] || *cond_guards[i])
95
for each used key part i, (!cond_guards[i] || *cond_guards[i])
89
97
This array is used by subquery code. The subquery code may inject
90
triggered conditions, i.e. conditions that can be 'switched off'. A ref
91
access created from such condition is not valid when at least one of the
98
triggered conditions, i.e. conditions that can be 'switched off'. A ref
99
access created from such condition is not valid when at least one of the
92
100
underlying conditions is switched off (see subquery code for more details)
94
102
bool **cond_guards;
132
typedef struct st_join_cache
140
typedef struct st_join_cache
134
142
unsigned char *buff;
135
143
unsigned char *pos; /* Start of free space in the buffer */
136
144
unsigned char *end;
137
145
uint32_t records; /* # of row cominations currently stored in the cache */
138
146
uint32_t record_nr;
141
149
Number of fields (i.e. cache_field objects). Those correspond to table
142
150
columns, and there are also special fields for
143
151
- table's column null bits
144
152
- table's null-complementation byte
145
153
- [new] table's rowid.
150
158
CACHE_FIELD *field;
151
159
CACHE_FIELD **blob_ptr;
207
215
st_join_table *last_inner; /**< last table table for embedding outer join */
208
216
st_join_table *first_upper; /**< first inner table for embedding outer join */
209
217
st_join_table *first_unmatched; /**< used for optimization purposes only */
211
219
/* Special content for EXPLAIN 'Extra' column or NULL if none */
212
220
const char *info;
214
222
Bitmap of TAB_INFO_* bits that encodes special line for EXPLAIN 'Extra'
215
223
column, or 0 if there is no info.
219
227
Read_record_func read_first_record;
220
228
Next_select_func next_select;
221
229
READ_RECORD read_record;
223
231
Currently the following two fields are used only for a [NOT] IN subquery
224
232
if it is executed by an alternative full table scan when the left operand of
225
233
the subquery predicate is evaluated to NULL.
227
Read_record_func save_read_first_record;/* to save read_first_record */
235
Read_record_func save_read_first_record;/* to save read_first_record */
228
236
int (*save_read_record) (READ_RECORD *);/* to save read_record.read_record */
229
237
double worst_seeks;
230
238
key_map const_keys; /**< Keys with constant part */
254
262
bool cached_eq_ref_table,eq_ref_table,not_used_in_distinct;
255
263
/* true <=> index-based access method must return records in order */
258
266
If it's not 0 the number stored this field indicates that the index
259
scan has been chosen to access the table data and we expect to scan
267
scan has been chosen to access the table data and we expect to scan
260
268
this number of rows for the table.
264
272
JOIN_CACHE cache;
276
284
SJ_TMP_TABLE *flush_weedout_table;
277
285
SJ_TMP_TABLE *check_weed_out_table;
278
286
struct st_join_table *do_firstmatch;
281
ptr - this join tab should do an InsideOut scan. Points
289
ptr - this join tab should do an InsideOut scan. Points
282
290
to the tab for which we'll need to check tab->found_match.
284
292
NULL - Not an insideout scan.
289
297
/* Used by InsideOut scan. Just set to true when have found a row. */
290
298
bool found_match;
293
301
/* If set, the rowid of this table must be put into the temptable. */
296
304
If set, one should call h->position() to obtain the rowid,
297
305
otherwise, the rowid is assumed to already be in h->ref
298
306
(this is because join caching and filesort() save the rowid and then
409
417
ha_rows fetch_limit;
410
418
POSITION positions[MAX_TABLES+1],best_positions[MAX_TABLES+1];
413
Bitmap of nested joins embedding the position at the end of the current
421
Bitmap of nested joins embedding the position at the end of the current
414
422
partial join (valid only during join optimizer run).
416
424
nested_join_map cur_embedding_map;
421
429
Table *tmp_table;
422
430
/// used to store 2 possible tmp table of SELECT
423
431
Table *exec_tmp_table1, *exec_tmp_table2;
425
433
Item_sum **sum_funcs, ***sum_funcs_end;
426
434
/** second copy of sumfuncs (for queries with 2 temporary tables */
427
435
Item_sum **sum_funcs2, ***sum_funcs_end2;
430
438
Item *having_history; ///< Store having for explain
431
439
uint64_t select_options;
432
440
select_result *result;
433
TMP_TABLE_PARAM tmp_table_param;
441
Tmp_Table_Param tmp_table_param;
434
442
DRIZZLE_LOCK *lock;
435
443
/// unit structure (with global parameters) for this select
436
SELECT_LEX_UNIT *unit;
444
Select_Lex_Unit *unit;
437
445
/// select that processed
438
SELECT_LEX *select_lex;
446
Select_Lex *select_lex;
440
448
true <=> optimizer must not mark any table as a constant table.
441
449
This is needed for subqueries in form "a IN (SELECT .. UNION SELECT ..):
442
450
when we optimize the select that reads the results of the union from a
443
451
temporary table, we must not mark the temp. table as constant because
444
452
the number of rows in it may vary from one subquery execution to another.
446
bool no_const_tables;
454
bool no_const_tables;
448
456
JOIN *tmp_join; ///< copy of this JOIN to be used with temporary tables
449
457
ROLLUP rollup; ///< Used with rollup
483
491
List<Item> &fields_list; ///< hold field list passed to mysql_select
486
order_st *order, *group_list, *proc_param; //hold parameters of mysql_select
494
order_st *order, *group_list; //hold parameters of mysql_select
487
495
COND *conds; // ---"---
488
496
Item *conds_history; // store WHERE for explain
489
497
TableList *tables_list; ///<hold 'tables' parameter of mysql_select
496
504
Item **items0, **items1, **items2, **items3, **current_ref_pointer_array;
497
505
uint32_t ref_pointer_array_size; ///< size of above in bytes
498
506
const char *zero_result_cause; ///< not 0 if exec must return zero result
500
bool union_part; ///< this subselect is part of union
508
bool union_part; ///< this subselect is part of union
501
509
bool optimized; ///< flag to avoid double optimization in EXPLAIN
503
511
Array<Item_in_subselect> sj_subselects;
505
513
/* Descriptions of temporary tables used to weed-out semi-join duplicates */
508
516
table_map cur_emb_sj_nests;
511
storage for caching buffers allocated during query execution.
519
storage for caching buffers allocated during query execution.
512
520
These buffers allocations need to be cached as the thread memory pool is
513
521
cleared only at the end of the execution of the whole query and not caching
514
allocations that occur in repetition at execution time will result in
522
allocations that occur in repetition at execution time will result in
515
523
excessive memory usage.
517
525
SORT_FIELD *sortorder; // make_unireg_sortorder()
518
526
Table **table_reexec; // make_simple_join()
519
527
JOIN_TAB *join_tab_reexec; // make_simple_join()
520
528
/* end of allocation caching storage */
522
JOIN(THD *thd_arg, List<Item> &fields_arg, uint64_t select_options_arg,
530
JOIN(Session *session_arg, List<Item> &fields_arg, uint64_t select_options_arg,
523
531
select_result *result_arg)
524
:fields_list(fields_arg), sj_subselects(thd_arg->mem_root, 4)
532
:fields_list(fields_arg), sj_subselects(session_arg->mem_root, 4)
526
init(thd_arg, fields_arg, select_options_arg, result_arg);
534
init(session_arg, fields_arg, select_options_arg, result_arg);
529
void init(THD *thd_arg, List<Item> &fields_arg, uint64_t select_options_arg,
537
void init(Session *session_arg, List<Item> &fields_arg, uint64_t select_options_arg,
530
538
select_result *result_arg)
532
540
join_tab= join_tab_save= 0;
586
595
int prepare(Item ***rref_pointer_array, TableList *tables, uint32_t wind_num,
587
596
COND *conds, uint32_t og_num, order_st *order, order_st *group,
588
Item *having, order_st *proc_param, SELECT_LEX *select,
589
SELECT_LEX_UNIT *unit);
597
Item *having, Select_Lex *select, Select_Lex_Unit *unit);
652
660
/* Extern functions in sql_select.cc */
653
661
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,
662
Table *create_tmp_table(Session *session,Tmp_Table_Param *param,List<Item> &fields,
655
663
order_st *group, bool distinct, bool save_sum_fields,
656
664
uint64_t select_options, ha_rows rows_limit,
658
void free_tmp_table(THD *thd, Table *entry);
659
void count_field_types(SELECT_LEX *select_lex, TMP_TABLE_PARAM *param,
666
void free_tmp_table(Session *session, Table *entry);
667
void count_field_types(Select_Lex *select_lex, Tmp_Table_Param *param,
660
668
List<Item> &fields, bool reset_with_sum_func);
661
bool setup_copy_fields(THD *thd, TMP_TABLE_PARAM *param,
669
bool setup_copy_fields(Session *session, Tmp_Table_Param *param,
662
670
Item **ref_pointer_array,
663
671
List<Item> &new_list1, List<Item> &new_list2,
664
672
uint32_t elements, List<Item> &fields);
665
void copy_fields(TMP_TABLE_PARAM *param);
673
void copy_fields(Tmp_Table_Param *param);
666
674
void copy_funcs(Item **func_ptr);
667
Field* create_tmp_field_from_field(THD *thd, Field* org_field,
675
Field* create_tmp_field_from_field(Session *session, Field* org_field,
668
676
const char *name, Table *table,
669
677
Item_field *item, uint32_t convert_blob_length);
671
679
/* functions from opt_sum.cc */
672
680
bool simple_pred(Item_func *func_item, Item **args, bool *inv_order);
673
681
int opt_sum_query(TableList *tables, List<Item> &all_fields,COND *conds);
683
691
bool null_key; /* true <=> the value of the key has a null part */
684
692
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)
693
store_key(Session *session, Field *field_arg, unsigned char *ptr, unsigned char *null, uint32_t length)
686
694
:null_key(0), null_ptr(null), err(0)
688
696
if (field_arg->type() == DRIZZLE_TYPE_BLOB)
691
699
Key segments are always packed with a 2 byte length prefix.
692
700
See mi_rkey for details.
694
to_field= new Field_varstring(ptr, length, 2, null, 1,
702
to_field= new Field_varstring(ptr, length, 2, null, 1,
695
703
Field::NONE, field_arg->field_name,
696
704
field_arg->table->s, field_arg->charset());
697
705
to_field->init(field_arg->table);
700
to_field=field_arg->new_key_field(thd->mem_root, field_arg->table,
708
to_field=field_arg->new_key_field(session->mem_root, field_arg->table,
703
711
virtual ~store_key() {} /** Not actually needed */
712
720
enum store_key_result copy()
714
722
enum store_key_result result;
715
THD *thd= to_field->table->in_use;
716
enum_check_fields saved_count_cuted_fields= thd->count_cuted_fields;
723
Session *session= to_field->table->in_use;
724
enum_check_fields saved_count_cuted_fields= session->count_cuted_fields;
718
thd->count_cuted_fields= CHECK_FIELD_IGNORE;
726
session->count_cuted_fields= CHECK_FIELD_IGNORE;
720
728
result= copy_inner();
722
thd->count_cuted_fields= saved_count_cuted_fields;
730
session->count_cuted_fields= saved_count_cuted_fields;
738
746
Copy_field copy_field;
739
747
const char *field_name;
741
store_key_field(THD *thd, Field *to_field_arg, unsigned char *ptr,
749
store_key_field(Session *session, Field *to_field_arg, unsigned char *ptr,
742
750
unsigned char *null_ptr_arg,
743
751
uint32_t length, Field *from_field, const char *name_arg)
744
:store_key(thd, to_field_arg,ptr,
752
:store_key(session, to_field_arg,ptr,
745
753
null_ptr_arg ? null_ptr_arg : from_field->maybe_null() ? &err
746
754
: (unsigned char*) 0, length), field_name(name_arg)
770
store_key_item(THD *thd, Field *to_field_arg, unsigned char *ptr,
778
store_key_item(Session *session, Field *to_field_arg, unsigned char *ptr,
771
779
unsigned char *null_ptr_arg, uint32_t length, Item *item_arg)
772
:store_key(thd, to_field_arg, ptr,
780
:store_key(session, to_field_arg, ptr,
773
781
null_ptr_arg ? null_ptr_arg : item_arg->maybe_null ?
774
782
&err : (unsigned char*) 0, length), item(item_arg)
776
784
const char *name() const { return "func"; }
779
787
enum store_key_result copy_inner()
781
789
int res= item->save_in_field(to_field, 1);
782
790
null_key= to_field->is_null() || item->null_value;
783
return (err != 0 || res > 2 ? STORE_KEY_FATAL : (store_key_result) res);
791
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,
800
store_key_const_item(Session *session, Field *to_field_arg, unsigned char *ptr,
793
801
unsigned char *null_ptr_arg, uint32_t length,
795
:store_key_item(thd, to_field_arg,ptr,
803
:store_key_item(session, to_field_arg,ptr,
796
804
null_ptr_arg ? null_ptr_arg : item_arg->maybe_null ?
797
805
&err : (unsigned char*) 0, length, item_arg), inited(0)
800
808
const char *name() const { return "const"; }
803
811
enum store_key_result copy_inner()
820
bool cp_buffer_from_ref(THD *thd, TABLE_REF *ref);
828
bool cp_buffer_from_ref(Session *session, TABLE_REF *ref);
821
829
bool error_if_full_join(JOIN *join);
822
830
int safe_index_read(JOIN_TAB *tab);
823
COND *remove_eq_conds(THD *thd, COND *cond, Item::cond_result *cond_value);
831
COND *remove_eq_conds(Session *session, COND *cond, Item::cond_result *cond_value);
824
832
int test_if_item_cache_changed(List<Cached_item> &list);
834
#endif /* DRIZZLED_SQL_SELECT_H */