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 */
24
21
classes to use when handling where clause
24
#ifdef USE_PRAGMA_INTERFACE
25
#pragma interface /* gcc class implementation */
28
28
/* PREV_BITS only used in sql_select.cc */
29
29
#define PREV_BITS(type,A) ((type) (((type) 1 << (A)) -1))
31
31
#include <storage/myisam/myisam.h>
32
#include <drizzled/sql_array.h>
34
33
/* Values in optimize */
35
34
#define KEY_OPTIMIZE_EXISTS 1
36
35
#define KEY_OPTIMIZE_REF_OR_NULL 2
38
37
typedef struct keyuse_t {
40
39
Item *val; /**< or value if no field */
41
40
table_map used_tables;
43
uint32_t optimize; // 0, or KEY_OPTIMIZE_*
42
uint optimize; // 0, or KEY_OPTIMIZE_*
44
43
key_part_map keypart_map;
45
44
ha_rows ref_table_rows;
71
70
typedef struct st_table_ref
74
uint32_t key_parts; ///< num of ...
75
uint32_t key_length; ///< length of key_buff
76
int32_t key; ///< key no
77
unsigned char *key_buff; ///< value to look for with key
78
unsigned char *key_buff2; ///< key_buff+key_length
73
uint key_parts; ///< num of ...
74
uint key_length; ///< length of key_buff
76
uchar *key_buff; ///< value to look for with key
77
uchar *key_buff2; ///< key_buff+key_length
79
78
store_key **key_copy; //
80
79
Item **items; ///< val()'s for each keypart
117
116
Where source data is located (i.e. this points to somewhere in
118
117
tableX->record[0])
121
uint32_t length; /* Length of data at *str, in bytes */
122
uint32_t blob_length; /* Valid IFF blob_field != 0 */
120
uint length; /* Length of data at *str, in bytes */
121
uint blob_length; /* Valid IFF blob_field != 0 */
123
122
Field_blob *blob_field;
124
123
bool strip; /* true <=> Strip endspaces ?? */
126
Table *get_rowid; /* _ != NULL <=> */
125
TABLE *get_rowid; /* _ != NULL <=> */
130
129
typedef struct st_join_cache
133
unsigned char *pos; /* Start of free space in the buffer */
135
uint32_t records; /* # of row cominations currently stored in the cache */
132
uchar *pos; /* Start of free space in the buffer */
134
uint records; /* # of row cominations currently stored in the cache */
139
138
Number of fields (i.e. cache_field objects). Those correspond to table
140
139
columns, and there are also special fields for
370
Describes use of one temporary table to weed out join duplicates.
374
- create a temp table
375
- when we reach the weed-out tab, walk through rowid-ed tabs and
377
For each table we need
382
class SJ_TMP_TABLE : public Sql_alloc
385
/* Array of pointers to tables that should be "used" */
403
MI_COLUMNDEF *start_recinfo;
404
MI_COLUMNDEF *recinfo;
406
/* Pointer to next table (next->start_idx > this->end_idx) */
370
411
class JOIN :public Sql_alloc
372
413
JOIN(const JOIN &rhs); /**< not implemented */
375
416
JOIN_TAB *join_tab,**best_ref;
376
417
JOIN_TAB **map2table; ///< mapping between table indexes and JOIN_TABs
377
418
JOIN_TAB *join_tab_save; ///< saved join_tab for subquery reexecution
378
Table **table,**all_tables;
419
TABLE **table,**all_tables;
380
421
The table which has an index that allows to produce the requried ordering.
381
422
A special value of 0x1 means that the ordering will be produced by
382
423
passing 1st non-const table to filesort(). NULL means no such table exists.
384
Table *sort_by_table;
425
TABLE *sort_by_table;
385
426
uint tables; /**< Number of tables in the join */
386
uint32_t outer_tables; /**< Number of tables that are not inside semijoin */
387
uint32_t const_tables;
427
uint outer_tables; /**< Number of tables that are not inside semijoin */
388
429
uint send_group_parts;
389
430
bool sort_and_group,first_record,full_join,group, no_field_update;
390
431
bool do_send_rows;
416
457
double best_read;
417
458
List<Item> *fields;
418
459
List<Cached_item> group_fields, group_fields_cache;
420
461
/// used to store 2 possible tmp table of SELECT
421
Table *exec_tmp_table1, *exec_tmp_table2;
462
TABLE *exec_tmp_table1, *exec_tmp_table2;
423
464
Item_sum **sum_funcs, ***sum_funcs_end;
424
465
/** second copy of sumfuncs (for queries with 2 temporary tables */
425
466
Item_sum **sum_funcs2, ***sum_funcs_end2;
457
498
bool group_optimized_away;
460
simple_xxxxx is set if order_st/GROUP BY doesn't include any references
501
simple_xxxxx is set if ORDER/GROUP BY doesn't include any references
461
502
to other tables than the first non-constant table in the JOIN.
462
It's also set if order_st/GROUP BY is empty.
503
It's also set if ORDER/GROUP BY is empty.
464
505
bool simple_order, simple_group;
466
507
Is set only in case if we have a GROUP BY clause
467
and no order_st BY after constant elimination of 'order'.
508
and no ORDER BY after constant elimination of 'order'.
470
/** Is set if we have a GROUP BY and we have order_st BY on a constant. */
511
/** Is set if we have a GROUP BY and we have ORDER BY on a constant. */
471
512
bool skip_sort_order;
473
514
bool need_tmp, hidden_group_fields;
481
522
List<Item> &fields_list; ///< hold field list passed to mysql_select
484
order_st *order, *group_list, *proc_param; //hold parameters of mysql_select
525
ORDER *order, *group_list, *proc_param; //hold parameters of mysql_select
485
526
COND *conds; // ---"---
486
527
Item *conds_history; // store WHERE for explain
487
TableList *tables_list; ///<hold 'tables' parameter of mysql_select
488
List<TableList> *join_list; ///< list of joined tables in reverse order
528
TABLE_LIST *tables_list; ///<hold 'tables' parameter of mysql_select
529
List<TABLE_LIST> *join_list; ///< list of joined tables in reverse order
489
530
COND_EQUAL *cond_equal;
490
531
SQL_SELECT *select; ///<created in optimisation phase
491
532
JOIN_TAB *return_tab; ///<used only for outer joins
492
533
Item **ref_pointer_array; ///<used pointer reference for this select
493
534
// Copy of above to be used with different lists
494
535
Item **items0, **items1, **items2, **items3, **current_ref_pointer_array;
495
uint32_t ref_pointer_array_size; ///< size of above in bytes
536
uint ref_pointer_array_size; ///< size of above in bytes
496
537
const char *zero_result_cause; ///< not 0 if exec must return zero result
498
539
bool union_part; ///< this subselect is part of union
513
554
excessive memory usage.
515
556
SORT_FIELD *sortorder; // make_unireg_sortorder()
516
Table **table_reexec; // make_simple_join()
557
TABLE **table_reexec; // make_simple_join()
517
558
JOIN_TAB *join_tab_reexec; // make_simple_join()
518
559
/* end of allocation caching storage */
520
JOIN(Session *session_arg, List<Item> &fields_arg, uint64_t select_options_arg,
561
JOIN(THD *thd_arg, List<Item> &fields_arg, uint64_t select_options_arg,
521
562
select_result *result_arg)
522
:fields_list(fields_arg), sj_subselects(session_arg->mem_root, 4)
563
:fields_list(fields_arg), sj_subselects(thd_arg->mem_root, 4)
524
init(session_arg, fields_arg, select_options_arg, result_arg);
565
init(thd_arg, fields_arg, select_options_arg, result_arg);
527
void init(Session *session_arg, List<Item> &fields_arg, uint64_t select_options_arg,
568
void init(THD *thd_arg, List<Item> &fields_arg, uint64_t select_options_arg,
528
569
select_result *result_arg)
530
571
join_tab= join_tab_save= 0;
581
622
no_const_tables= false;
584
int prepare(Item ***rref_pointer_array, TableList *tables, uint32_t wind_num,
585
COND *conds, uint32_t og_num, order_st *order, order_st *group,
586
Item *having, order_st *proc_param, SELECT_LEX *select,
625
int prepare(Item ***rref_pointer_array, TABLE_LIST *tables, uint wind_num,
626
COND *conds, uint og_num, ORDER *order, ORDER *group,
627
Item *having, ORDER *proc_param, SELECT_LEX *select,
587
628
SELECT_LEX_UNIT *unit);
611
652
bool rollup_init();
612
653
bool rollup_make_fields(List<Item> &all_fields, List<Item> &fields,
613
654
Item_sum ***func);
614
int rollup_send_data(uint32_t idx);
615
int rollup_write_data(uint32_t idx, Table *table);
655
int rollup_send_data(uint idx);
656
int rollup_write_data(uint idx, TABLE *table);
616
657
void remove_subq_pushed_predicates(Item **where);
618
659
Release memory and, if possible, the open tables held by this execution
650
691
/* Extern functions in sql_select.cc */
651
692
bool store_val_in_field(Field *field, Item *val, enum_check_fields check_flag);
652
Table *create_tmp_table(Session *session,TMP_TABLE_PARAM *param,List<Item> &fields,
653
order_st *group, bool distinct, bool save_sum_fields,
693
TABLE *create_tmp_table(THD *thd,TMP_TABLE_PARAM *param,List<Item> &fields,
694
ORDER *group, bool distinct, bool save_sum_fields,
654
695
uint64_t select_options, ha_rows rows_limit,
656
void free_tmp_table(Session *session, Table *entry);
697
void free_tmp_table(THD *thd, TABLE *entry);
657
698
void count_field_types(SELECT_LEX *select_lex, TMP_TABLE_PARAM *param,
658
699
List<Item> &fields, bool reset_with_sum_func);
659
bool setup_copy_fields(Session *session, TMP_TABLE_PARAM *param,
700
bool setup_copy_fields(THD *thd, TMP_TABLE_PARAM *param,
660
701
Item **ref_pointer_array,
661
702
List<Item> &new_list1, List<Item> &new_list2,
662
uint32_t elements, List<Item> &fields);
703
uint elements, List<Item> &fields);
663
704
void copy_fields(TMP_TABLE_PARAM *param);
664
705
void copy_funcs(Item **func_ptr);
665
Field* create_tmp_field_from_field(Session *session, Field* org_field,
666
const char *name, Table *table,
667
Item_field *item, uint32_t convert_blob_length);
706
bool create_myisam_from_heap(THD *thd, TABLE *table,
707
MI_COLUMNDEF *start_recinfo,
708
MI_COLUMNDEF **recinfo,
709
int error, bool ignore_last_dupp_key_error);
710
uint find_shortest_key(TABLE *table, const key_map *usable_keys);
711
Field* create_tmp_field_from_field(THD *thd, Field* org_field,
712
const char *name, TABLE *table,
713
Item_field *item, uint convert_blob_length);
669
715
/* functions from opt_sum.cc */
670
716
bool simple_pred(Item_func *func_item, Item **args, bool *inv_order);
671
int opt_sum_query(TableList *tables, List<Item> &all_fields,COND *conds);
717
int opt_sum_query(TABLE_LIST *tables, List<Item> &all_fields,COND *conds);
673
719
/* from sql_delete.cc, used by opt_range.cc */
674
720
extern "C" int refpos_order_cmp(void* arg, const void *a,const void *b);
681
727
bool null_key; /* true <=> the value of the key has a null part */
682
728
enum store_key_result { STORE_KEY_OK, STORE_KEY_FATAL, STORE_KEY_CONV };
683
store_key(Session *session, Field *field_arg, unsigned char *ptr, unsigned char *null, uint32_t length)
729
store_key(THD *thd, Field *field_arg, uchar *ptr, uchar *null, uint length)
684
730
:null_key(0), null_ptr(null), err(0)
686
732
if (field_arg->type() == DRIZZLE_TYPE_BLOB)
710
756
enum store_key_result copy()
712
758
enum store_key_result result;
713
Session *session= to_field->table->in_use;
714
enum_check_fields saved_count_cuted_fields= session->count_cuted_fields;
759
THD *thd= to_field->table->in_use;
760
enum_check_fields saved_count_cuted_fields= thd->count_cuted_fields;
716
session->count_cuted_fields= CHECK_FIELD_IGNORE;
762
thd->count_cuted_fields= CHECK_FIELD_IGNORE;
718
764
result= copy_inner();
720
session->count_cuted_fields= saved_count_cuted_fields;
766
thd->count_cuted_fields= saved_count_cuted_fields;
726
772
Field *to_field; // Store data here
727
unsigned char *null_ptr;
730
776
virtual enum store_key_result copy_inner()=0;
736
782
Copy_field copy_field;
737
783
const char *field_name;
739
store_key_field(Session *session, Field *to_field_arg, unsigned char *ptr,
740
unsigned char *null_ptr_arg,
741
uint32_t length, Field *from_field, const char *name_arg)
742
:store_key(session, to_field_arg,ptr,
785
store_key_field(THD *thd, Field *to_field_arg, uchar *ptr,
787
uint length, Field *from_field, const char *name_arg)
788
:store_key(thd, to_field_arg,ptr,
743
789
null_ptr_arg ? null_ptr_arg : from_field->maybe_null() ? &err
744
: (unsigned char*) 0, length), field_name(name_arg)
790
: (uchar*) 0, length), field_name(name_arg)
768
store_key_item(Session *session, Field *to_field_arg, unsigned char *ptr,
769
unsigned char *null_ptr_arg, uint32_t length, Item *item_arg)
770
:store_key(session, to_field_arg, ptr,
818
store_key_item(THD *thd, Field *to_field_arg, uchar *ptr,
819
uchar *null_ptr_arg, uint length, Item *item_arg)
820
:store_key(thd, to_field_arg, ptr,
771
821
null_ptr_arg ? null_ptr_arg : item_arg->maybe_null ?
772
&err : (unsigned char*) 0, length), item(item_arg)
822
&err : (uchar*) 0, length), item(item_arg)
774
824
const char *name() const { return "func"; }
777
827
enum store_key_result copy_inner()
829
TABLE *table= to_field->table;
830
my_bitmap_map *old_map= dbug_tmp_use_all_columns(table,
779
832
int res= item->save_in_field(to_field, 1);
833
dbug_tmp_restore_column_map(table->write_set, old_map);
780
834
null_key= to_field->is_null() || item->null_value;
781
835
return (err != 0 || res > 2 ? STORE_KEY_FATAL : (store_key_result) res);
790
store_key_const_item(Session *session, Field *to_field_arg, unsigned char *ptr,
791
unsigned char *null_ptr_arg, uint32_t length,
844
store_key_const_item(THD *thd, Field *to_field_arg, uchar *ptr,
845
uchar *null_ptr_arg, uint length,
793
:store_key_item(session, to_field_arg,ptr,
847
:store_key_item(thd, to_field_arg,ptr,
794
848
null_ptr_arg ? null_ptr_arg : item_arg->maybe_null ?
795
&err : (unsigned char*) 0, length, item_arg), inited(0)
849
&err : (uchar*) 0, length, item_arg), inited(0)
798
852
const char *name() const { return "const"; }
818
bool cp_buffer_from_ref(Session *session, TABLE_REF *ref);
872
bool cp_buffer_from_ref(THD *thd, TABLE *table, TABLE_REF *ref);
819
873
bool error_if_full_join(JOIN *join);
874
int report_error(TABLE *table, int error);
820
875
int safe_index_read(JOIN_TAB *tab);
821
COND *remove_eq_conds(Session *session, COND *cond, Item::cond_result *cond_value);
876
COND *remove_eq_conds(THD *thd, COND *cond, Item::cond_result *cond_value);
822
877
int test_if_item_cache_changed(List<Cached_item> &list);