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
28
/* PREV_BITS only used in sql_select.cc */
29
#define PREV_BITS(type,A) ((type) (((type) 1 << (A)) -1))
31
#include <storage/myisam/myisam.h>
24
#ifdef USE_PRAGMA_INTERFACE
25
#pragma interface /* gcc class implementation */
33
30
/* Values in optimize */
34
31
#define KEY_OPTIMIZE_EXISTS 1
35
32
#define KEY_OPTIMIZE_REF_OR_NULL 2
37
34
typedef struct keyuse_t {
39
36
Item *val; /**< or value if no field */
40
37
table_map used_tables;
42
uint32_t optimize; // 0, or KEY_OPTIMIZE_*
39
uint optimize; // 0, or KEY_OPTIMIZE_*
43
40
key_part_map keypart_map;
44
41
ha_rows ref_table_rows;
52
49
an Item_func_trig_cond. This means the equality (and validity of
53
50
this KEYUSE element) can be turned on and off. The on/off state
54
51
is indicted by the pointed value:
55
*cond_guard == true <=> equality condition is on
56
*cond_guard == false <=> equality condition is off
52
*cond_guard == TRUE <=> equality condition is on
53
*cond_guard == FALSE <=> equality condition is off
58
55
NULL - Otherwise (the source equality can't be turned off)
70
67
typedef struct st_table_ref
73
uint32_t key_parts; ///< num of ...
74
uint32_t key_length; ///< length of key_buff
75
int32_t key; ///< key no
76
unsigned char *key_buff; ///< value to look for with key
77
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
78
75
store_key **key_copy; //
79
76
Item **items; ///< val()'s for each keypart
116
113
Where source data is located (i.e. this points to somewhere in
117
114
tableX->record[0])
120
uint32_t length; /* Length of data at *str, in bytes */
121
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 */
122
119
Field_blob *blob_field;
123
bool strip; /* true <=> Strip endspaces ?? */
120
bool strip; /* TRUE <=> Strip endspaces ?? */
125
Table *get_rowid; /* _ != NULL <=> */
122
TABLE *get_rowid; /* _ != NULL <=> */
129
126
typedef struct st_join_cache
132
unsigned char *pos; /* Start of free space in the buffer */
134
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 */
138
135
Number of fields (i.e. cache_field objects). Those correspond to table
139
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) */
369
408
class JOIN :public Sql_alloc
371
410
JOIN(const JOIN &rhs); /**< not implemented */
374
413
JOIN_TAB *join_tab,**best_ref;
375
414
JOIN_TAB **map2table; ///< mapping between table indexes and JOIN_TABs
376
415
JOIN_TAB *join_tab_save; ///< saved join_tab for subquery reexecution
377
Table **table,**all_tables;
416
TABLE **table,**all_tables;
379
418
The table which has an index that allows to produce the requried ordering.
380
419
A special value of 0x1 means that the ordering will be produced by
381
420
passing 1st non-const table to filesort(). NULL means no such table exists.
383
Table *sort_by_table;
422
TABLE *sort_by_table;
384
423
uint tables; /**< Number of tables in the join */
385
uint32_t outer_tables; /**< Number of tables that are not inside semijoin */
386
uint32_t const_tables;
424
uint outer_tables; /**< Number of tables that are not inside semijoin */
387
426
uint send_group_parts;
388
427
bool sort_and_group,first_record,full_join,group, no_field_update;
389
428
bool do_send_rows;
391
true when we want to resume nested loop iterations when
430
TRUE when we want to resume nested loop iterations when
392
431
fetching data from a cursor
394
433
bool resume_nested_loop;
426
465
Item *tmp_having; ///< To store having when processed temporary table
427
466
Item *having_history; ///< Store having for explain
428
uint64_t select_options;
467
ulonglong select_options;
429
468
select_result *result;
430
469
TMP_TABLE_PARAM tmp_table_param;
432
471
/// unit structure (with global parameters) for this select
433
472
SELECT_LEX_UNIT *unit;
434
473
/// select that processed
435
474
SELECT_LEX *select_lex;
437
true <=> optimizer must not mark any table as a constant table.
476
TRUE <=> optimizer must not mark any table as a constant table.
438
477
This is needed for subqueries in form "a IN (SELECT .. UNION SELECT ..):
439
478
when we optimize the select that reads the results of the union from a
440
479
temporary table, we must not mark the temp. table as constant because
450
489
If we have the GROUP BY statement in the query,
451
490
but the group_list was emptied by optimizer, this
453
492
It happens when fields in the GROUP BY are from
456
495
bool group_optimized_away;
459
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
460
499
to other tables than the first non-constant table in the JOIN.
461
It's also set if order_st/GROUP BY is empty.
500
It's also set if ORDER/GROUP BY is empty.
463
502
bool simple_order, simple_group;
465
504
Is set only in case if we have a GROUP BY clause
466
and no order_st BY after constant elimination of 'order'.
505
and no ORDER BY after constant elimination of 'order'.
469
/** 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. */
470
509
bool skip_sort_order;
472
511
bool need_tmp, hidden_group_fields;
480
519
List<Item> &fields_list; ///< hold field list passed to mysql_select
483
order_st *order, *group_list, *proc_param; //hold parameters of mysql_select
522
ORDER *order, *group_list, *proc_param; //hold parameters of mysql_select
484
523
COND *conds; // ---"---
485
524
Item *conds_history; // store WHERE for explain
486
TableList *tables_list; ///<hold 'tables' parameter of mysql_select
487
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
488
527
COND_EQUAL *cond_equal;
489
528
SQL_SELECT *select; ///<created in optimisation phase
490
529
JOIN_TAB *return_tab; ///<used only for outer joins
491
530
Item **ref_pointer_array; ///<used pointer reference for this select
492
531
// Copy of above to be used with different lists
493
532
Item **items0, **items1, **items2, **items3, **current_ref_pointer_array;
494
uint32_t ref_pointer_array_size; ///< size of above in bytes
533
uint ref_pointer_array_size; ///< size of above in bytes
495
534
const char *zero_result_cause; ///< not 0 if exec must return zero result
497
536
bool union_part; ///< this subselect is part of union
512
551
excessive memory usage.
514
553
SORT_FIELD *sortorder; // make_unireg_sortorder()
515
Table **table_reexec; // make_simple_join()
554
TABLE **table_reexec; // make_simple_join()
516
555
JOIN_TAB *join_tab_reexec; // make_simple_join()
517
556
/* end of allocation caching storage */
519
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,
520
559
select_result *result_arg)
521
560
:fields_list(fields_arg), sj_subselects(thd_arg->mem_root, 4)
523
562
init(thd_arg, fields_arg, select_options_arg, result_arg);
526
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,
527
566
select_result *result_arg)
529
568
join_tab= join_tab_save= 0;
572
611
all_fields= fields_arg;
573
612
fields_list= fields_arg;
574
memset(&keyuse, 0, sizeof(keyuse));
613
bzero((char*) &keyuse,sizeof(keyuse));
575
614
tmp_table_param.init();
576
615
tmp_table_param.end_write_records= HA_POS_ERROR;
577
616
rollup.state= ROLLUP::STATE_NONE;
578
617
sj_tmp_tables= NULL;
580
no_const_tables= false;
619
no_const_tables= FALSE;
583
int prepare(Item ***rref_pointer_array, TableList *tables, uint32_t wind_num,
584
COND *conds, uint32_t og_num, order_st *order, order_st *group,
585
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,
586
625
SELECT_LEX_UNIT *unit);
593
632
bool flatten_subqueries();
594
633
bool setup_subquery_materialization();
595
634
bool make_sum_func_list(List<Item> &all_fields, List<Item> &send_fields,
596
bool before_group_by, bool recompute= false);
635
bool before_group_by, bool recompute= FALSE);
598
637
inline void set_items_ref_array(Item **ptr)
600
memcpy(ref_pointer_array, ptr, ref_pointer_array_size);
639
memcpy((char*) ref_pointer_array, (char*) ptr, ref_pointer_array_size);
601
640
current_ref_pointer_array= ptr;
603
642
inline void init_items_ref_array()
610
649
bool rollup_init();
611
650
bool rollup_make_fields(List<Item> &all_fields, List<Item> &fields,
612
651
Item_sum ***func);
613
int rollup_send_data(uint32_t idx);
614
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);
615
654
void remove_subq_pushed_predicates(Item **where);
617
656
Release memory and, if possible, the open tables held by this execution
649
688
/* Extern functions in sql_select.cc */
650
689
bool store_val_in_field(Field *field, Item *val, enum_check_fields check_flag);
651
Table *create_tmp_table(THD *thd,TMP_TABLE_PARAM *param,List<Item> &fields,
652
order_st *group, bool distinct, bool save_sum_fields,
653
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,
655
void free_tmp_table(THD *thd, Table *entry);
694
void free_tmp_table(THD *thd, TABLE *entry);
656
695
void count_field_types(SELECT_LEX *select_lex, TMP_TABLE_PARAM *param,
657
696
List<Item> &fields, bool reset_with_sum_func);
658
697
bool setup_copy_fields(THD *thd, TMP_TABLE_PARAM *param,
659
698
Item **ref_pointer_array,
660
699
List<Item> &new_list1, List<Item> &new_list2,
661
uint32_t elements, List<Item> &fields);
700
uint elements, List<Item> &fields);
662
701
void copy_fields(TMP_TABLE_PARAM *param);
663
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);
664
708
Field* create_tmp_field_from_field(THD *thd, Field* org_field,
665
const char *name, Table *table,
666
Item_field *item, uint32_t convert_blob_length);
709
const char *name, TABLE *table,
710
Item_field *item, uint convert_blob_length);
668
712
/* functions from opt_sum.cc */
669
713
bool simple_pred(Item_func *func_item, Item **args, bool *inv_order);
670
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);
672
716
/* from sql_delete.cc, used by opt_range.cc */
673
717
extern "C" int refpos_order_cmp(void* arg, const void *a,const void *b);
677
721
class store_key :public Sql_alloc
680
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 */
681
725
enum store_key_result { STORE_KEY_OK, STORE_KEY_FATAL, STORE_KEY_CONV };
682
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)
683
727
:null_key(0), null_ptr(null), err(0)
685
if (field_arg->type() == DRIZZLE_TYPE_BLOB)
729
if (field_arg->type() == MYSQL_TYPE_BLOB)
688
732
Key segments are always packed with a 2 byte length prefix.
735
779
Copy_field copy_field;
736
780
const char *field_name;
738
store_key_field(THD *thd, Field *to_field_arg, unsigned char *ptr,
739
unsigned char *null_ptr_arg,
740
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)
741
785
:store_key(thd, to_field_arg,ptr,
742
786
null_ptr_arg ? null_ptr_arg : from_field->maybe_null() ? &err
743
: (unsigned char*) 0, length), field_name(name_arg)
787
: (uchar*) 0, length), field_name(name_arg)
767
store_key_item(THD *thd, Field *to_field_arg, unsigned char *ptr,
768
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)
769
817
:store_key(thd, to_field_arg, ptr,
770
818
null_ptr_arg ? null_ptr_arg : item_arg->maybe_null ?
771
&err : (unsigned char*) 0, length), item(item_arg)
819
&err : (uchar*) 0, length), item(item_arg)
773
821
const char *name() const { return "func"; }
776
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,
778
829
int res= item->save_in_field(to_field, 1);
830
dbug_tmp_restore_column_map(table->write_set, old_map);
779
831
null_key= to_field->is_null() || item->null_value;
780
832
return (err != 0 || res > 2 ? STORE_KEY_FATAL : (store_key_result) res);
789
store_key_const_item(THD *thd, Field *to_field_arg, unsigned char *ptr,
790
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,
792
844
:store_key_item(thd, to_field_arg,ptr,
793
845
null_ptr_arg ? null_ptr_arg : item_arg->maybe_null ?
794
&err : (unsigned char*) 0, length, item_arg), inited(0)
846
&err : (uchar*) 0, length, item_arg), inited(0)
797
849
const char *name() const { return "const"; }
817
bool cp_buffer_from_ref(THD *thd, TABLE_REF *ref);
869
bool cp_buffer_from_ref(THD *thd, TABLE *table, TABLE_REF *ref);
818
870
bool error_if_full_join(JOIN *join);
871
int report_error(TABLE *table, int error);
819
872
int safe_index_read(JOIN_TAB *tab);
820
873
COND *remove_eq_conds(THD *thd, COND *cond, Item::cond_result *cond_value);
821
874
int test_if_item_cache_changed(List<Cached_item> &list);