~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_select.h

  • Committer: Brian Aker
  • Date: 2009-05-15 17:06:35 UTC
  • mto: This revision was merged to the branch mainline in revision 1023.
  • Revision ID: brian@gaz-20090515170635-croy1u63a3gqdn9n
Dead convert functions for character sets.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
18
 */
19
19
 
 
20
#ifndef DRIZZLED_SQL_SELECT_H
 
21
#define DRIZZLED_SQL_SELECT_H
 
22
 
 
23
#include <drizzled/cached_item.h>
 
24
#include <drizzled/session.h>
 
25
#include <drizzled/field/varstring.h>
 
26
#include <drizzled/item/null.h>
 
27
 
 
28
class select_result;
 
29
 
20
30
/**
21
31
  @file
22
32
 
24
34
  classes to use when handling where clause
25
35
*/
26
36
 
27
 
#ifdef USE_PRAGMA_INTERFACE
28
 
#pragma interface                       /* gcc class implementation */
29
 
#endif
30
37
 
31
38
/* PREV_BITS only used in sql_select.cc */
32
39
#define PREV_BITS(type,A)       ((type) (((type) 1 << (A)) -1))
33
40
 
34
 
#include <storage/myisam/myisam.h>
 
41
#include <plugin/myisam/myisam.h>
 
42
#include <drizzled/sql_array.h>
35
43
 
36
44
/* Values in optimize */
37
45
#define KEY_OPTIMIZE_EXISTS             1
52
60
  bool null_rejecting;
53
61
  /*
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
83
 
  /*  
 
91
  /*
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
86
 
    
87
 
      for each used key part i, (!cond_guards[i] || *cond_guards[i]) 
 
94
 
 
95
      for each used key part i, (!cond_guards[i] || *cond_guards[i])
88
96
 
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)
93
101
  */
94
102
  bool          **cond_guards;
115
123
*/
116
124
 
117
125
typedef struct st_cache_field {
118
 
  /* 
119
 
    Where source data is located (i.e. this points to somewhere in 
 
126
  /*
 
127
    Where source data is located (i.e. this points to somewhere in
120
128
    tableX->record[0])
121
129
  */
122
130
  unsigned char *str;
129
137
} CACHE_FIELD;
130
138
 
131
139
 
132
 
typedef struct st_join_cache 
 
140
typedef struct st_join_cache
133
141
{
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;
139
 
  uint32_t ptr_record; 
140
 
  /* 
 
147
  uint32_t ptr_record;
 
148
  /*
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.
146
154
  */
147
 
  uint32_t fields; 
148
 
  uint32_t length; 
 
155
  uint32_t fields;
 
156
  uint32_t length;
149
157
  uint32_t blobs;
150
158
  CACHE_FIELD *field;
151
159
  CACHE_FIELD **blob_ptr;
191
199
  SQL_SELECT    *select;
192
200
  COND          *select_cond;
193
201
  QUICK_SELECT_I *quick;
194
 
  /* 
 
202
  /*
195
203
    The value of select_cond before we've attempted to do Index Condition
196
204
    Pushdown. We may need to restore everything back if we first choose one
197
205
    index but then reconsider (see test_if_skip_sort_order() for such
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     */
210
 
  
 
218
 
211
219
  /* Special content for EXPLAIN 'Extra' column or NULL if none */
212
220
  const char    *info;
213
 
  /* 
 
221
  /*
214
222
    Bitmap of TAB_INFO_* bits that encodes special line for EXPLAIN 'Extra'
215
223
    column, or 0 if there is no info.
216
224
  */
219
227
  Read_record_func read_first_record;
220
228
  Next_select_func next_select;
221
229
  READ_RECORD   read_record;
222
 
  /* 
 
230
  /*
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.
226
 
  */  
227
 
  Read_record_func save_read_first_record;/* to save read_first_record */ 
 
234
  */
 
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 */
245
253
    E(#records) is in found_records.
246
254
  */
247
255
  ha_rows       read_time;
248
 
  
 
256
 
249
257
  table_map     dependent,key_dependent;
250
258
  uint          use_quick,index;
251
259
  uint          status;                         ///< Save status for cache
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 */
256
264
  bool          sorted;
257
 
  /* 
 
265
  /*
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.
261
 
  */ 
262
 
  ha_rows       limit; 
 
269
  */
 
270
  ha_rows       limit;
263
271
  TABLE_REF     ref;
264
272
  JOIN_CACHE    cache;
265
273
  JOIN          *join;
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;
279
 
 
280
 
  /* 
281
 
     ptr  - this join tab should do an InsideOut scan. Points 
 
287
 
 
288
  /*
 
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.
283
291
 
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;
291
299
 
292
 
  enum { 
 
300
  enum {
293
301
    /* If set, the rowid of this table must be put into the temptable. */
294
 
    KEEP_ROWID=1, 
295
 
    /* 
 
302
    KEEP_ROWID=1,
 
303
    /*
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
338
346
  */
339
347
  double records_read;
340
348
 
341
 
  /* 
 
349
  /*
342
350
    Cost accessing the table in course of the entire complete join execution,
343
 
    i.e. cost of one access method use (e.g. 'range' or 'ref' scan ) times 
 
351
    i.e. cost of one access method use (e.g. 'range' or 'ref' scan ) times
344
352
    number the access method will be invoked.
345
353
  */
346
354
  double read_time;
408
416
  */
409
417
  ha_rows  fetch_limit;
410
418
  POSITION positions[MAX_TABLES+1],best_positions[MAX_TABLES+1];
411
 
  
 
419
 
412
420
  /* *
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).
415
423
  */
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;
424
 
  THD      *thd;
 
432
  Session          *session;
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;
439
 
  /** 
 
446
  Select_Lex *select_lex;
 
447
  /**
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.
445
453
  */
446
 
  bool no_const_tables; 
447
 
  
 
454
  bool no_const_tables;
 
455
 
448
456
  JOIN *tmp_join; ///< copy of this JOIN to be used with temporary tables
449
457
  ROLLUP rollup;                                ///< Used with rollup
450
458
 
483
491
  List<Item> &fields_list; ///< hold field list passed to mysql_select
484
492
  int error;
485
493
 
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
499
 
  
500
 
  bool union_part; ///< this subselect is part of union 
 
507
 
 
508
  bool union_part; ///< this subselect is part of union
501
509
  bool optimized; ///< flag to avoid double optimization in EXPLAIN
502
 
  
 
510
 
503
511
  Array<Item_in_subselect> sj_subselects;
504
512
 
505
513
  /* Descriptions of temporary tables used to weed-out semi-join duplicates */
507
515
 
508
516
  table_map cur_emb_sj_nests;
509
517
 
510
 
  /* 
511
 
    storage for caching buffers allocated during query execution. 
 
518
  /*
 
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.
516
 
  */  
 
524
  */
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 */
521
529
 
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)
525
533
  {
526
 
    init(thd_arg, fields_arg, select_options_arg, result_arg);
 
534
    init(session_arg, fields_arg, select_options_arg, result_arg);
527
535
  }
528
536
 
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)
531
539
  {
532
540
    join_tab= join_tab_save= 0;
547
555
    sortorder= 0;
548
556
    table_reexec= 0;
549
557
    join_tab_reexec= 0;
550
 
    thd= thd_arg;
 
558
    session= session_arg;
551
559
    sum_funcs= sum_funcs2= 0;
552
560
    having= tmp_having= having_history= 0;
553
561
    select_options= select_options_arg;
554
562
    result= result_arg;
555
 
    lock= thd_arg->lock;
 
563
    lock= session_arg->lock;
556
564
    select_lex= 0; //for safety
557
565
    tmp_join= 0;
558
566
    select_distinct= test(select_options & SELECT_DISTINCT);
573
581
    group_optimized_away= 0;
574
582
 
575
583
    all_fields= fields_arg;
576
 
    fields_list= fields_arg;
 
584
    if (&fields_list != &fields_arg) /* only copy if not same*/
 
585
      fields_list= fields_arg;
577
586
    memset(&keyuse, 0, sizeof(keyuse));
578
587
    tmp_table_param.init();
579
588
    tmp_table_param.end_write_records= HA_POS_ERROR;
585
594
 
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);
590
598
  int optimize();
591
599
  int reinit();
592
600
  void exec();
636
644
  bool change_result(select_result *result);
637
645
  bool is_top_level_join() const
638
646
  {
639
 
    return (unit == &thd->lex->unit && (unit->fake_select_lex == 0 ||
 
647
    return (unit == &session->lex->unit && (unit->fake_select_lex == 0 ||
640
648
                                        select_lex == unit->fake_select_lex));
641
649
  }
642
650
};
651
659
 
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,
657
665
                        char* alias);
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);
670
 
                                                                      
 
678
 
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);
682
690
public:
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)
687
695
  {
688
696
    if (field_arg->type() == DRIZZLE_TYPE_BLOB)
689
697
    {
690
 
      /* 
 
698
      /*
691
699
        Key segments are always packed with a 2 byte length prefix.
692
700
        See mi_rkey for details.
693
701
      */
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);
698
706
    }
699
707
    else
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,
701
709
                                        ptr, null, 1);
702
710
  }
703
711
  virtual ~store_key() {}                       /** Not actually needed */
712
720
  enum store_key_result copy()
713
721
  {
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;
717
725
 
718
 
    thd->count_cuted_fields= CHECK_FIELD_IGNORE;
 
726
    session->count_cuted_fields= CHECK_FIELD_IGNORE;
719
727
 
720
728
    result= copy_inner();
721
729
 
722
 
    thd->count_cuted_fields= saved_count_cuted_fields;
 
730
    session->count_cuted_fields= saved_count_cuted_fields;
723
731
 
724
732
    return result;
725
733
  }
738
746
  Copy_field copy_field;
739
747
  const char *field_name;
740
748
 public:
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)
747
755
  {
752
760
  }
753
761
  const char *name() const { return field_name; }
754
762
 
755
 
 protected: 
 
763
 protected:
756
764
  enum store_key_result copy_inner()
757
765
  {
758
766
    copy_field.do_copy(&copy_field);
767
775
 protected:
768
776
  Item *item;
769
777
public:
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)
775
783
  {}
776
784
  const char *name() const { return "func"; }
777
785
 
778
 
 protected:  
 
786
 protected:
779
787
  enum store_key_result copy_inner()
780
788
  {
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);
784
792
  }
785
793
};
786
794
 
789
797
{
790
798
  bool inited;
791
799
public:
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,
794
802
                       Item *item_arg)
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)
798
806
  {
799
807
  }
800
808
  const char *name() const { return "const"; }
801
809
 
802
 
protected:  
 
810
protected:
803
811
  enum store_key_result copy_inner()
804
812
  {
805
813
    int res;
807
815
    {
808
816
      inited=1;
809
817
      if ((res= item->save_in_field(to_field, 1)))
810
 
      {       
 
818
      {
811
819
        if (!err)
812
820
          err= res;
813
821
      }
817
825
  }
818
826
};
819
827
 
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);
 
833
 
 
834
#endif /* DRIZZLED_SQL_SELECT_H */