~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_select.h

  • Committer: Stewart Smith
  • Date: 2008-09-15 07:13:59 UTC
  • mfrom: (383.1.21 drizzle)
  • mto: This revision was merged to the branch mainline in revision 408.
  • Revision ID: stewart@flamingspork.com-20080915071359-f8bznznyaiqrtqxa
merged

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
#pragma interface                       /* gcc class implementation */
26
26
#endif
27
27
 
 
28
/* PREV_BITS only used in sql_select.cc */
 
29
#define PREV_BITS(type,A)       ((type) (((type) 1 << (A)) -1))
 
30
 
28
31
#include <storage/myisam/myisam.h>
29
32
 
30
33
/* Values in optimize */
32
35
#define KEY_OPTIMIZE_REF_OR_NULL        2
33
36
 
34
37
typedef struct keyuse_t {
35
 
  TABLE *table;
 
38
  Table *table;
36
39
  Item  *val;                           /**< or value if no field */
37
40
  table_map used_tables;
38
41
  uint  key, keypart;
67
70
typedef struct st_table_ref
68
71
{
69
72
  bool          key_err;
70
 
  uint          key_parts;                ///< num of ...
71
 
  uint          key_length;               ///< length of key_buff
72
 
  int           key;                      ///< key no
73
 
  uchar         *key_buff;                ///< value to look for with key
74
 
  uchar         *key_buff2;               ///< key_buff+key_length
 
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
75
78
  store_key     **key_copy;               //
76
79
  Item          **items;                  ///< val()'s for each keypart
77
80
  /*  
93
96
  key_part_map  null_rejecting;
94
97
  table_map     depend_map;               ///< Table depends on these tables.
95
98
  /* null byte position in the key_buf. Used for REF_OR_NULL optimization */
96
 
  uchar          *null_ref_key;
 
99
  unsigned char *null_ref_key;
97
100
 
98
101
  /*
99
102
    true <=> disable the "cache" as doing lookup with the same key value may
113
116
    Where source data is located (i.e. this points to somewhere in 
114
117
    tableX->record[0])
115
118
  */
116
 
  uchar *str;
117
 
  uint length; /* Length of data at *str, in bytes */
118
 
  uint blob_length; /* Valid IFF blob_field != 0 */
 
119
  unsigned char *str;
 
120
  uint32_t length; /* Length of data at *str, in bytes */
 
121
  uint32_t blob_length; /* Valid IFF blob_field != 0 */
119
122
  Field_blob *blob_field;
120
123
  bool strip; /* true <=> Strip endspaces ?? */
121
124
 
122
 
  TABLE *get_rowid; /* _ != NULL <=> */
 
125
  Table *get_rowid; /* _ != NULL <=> */
123
126
} CACHE_FIELD;
124
127
 
125
128
 
126
129
typedef struct st_join_cache 
127
130
{
128
 
  uchar *buff;
129
 
  uchar *pos;    /* Start of free space in the buffer */
130
 
  uchar *end;
131
 
  uint records;  /* # of row cominations currently stored in the cache */
132
 
  uint record_nr;
133
 
  uint ptr_record; 
 
131
  unsigned char *buff;
 
132
  unsigned char *pos;    /* Start of free space in the buffer */
 
133
  unsigned char *end;
 
134
  uint32_t records;  /* # of row cominations currently stored in the cache */
 
135
  uint32_t record_nr;
 
136
  uint32_t ptr_record; 
134
137
  /* 
135
138
    Number of fields (i.e. cache_field objects). Those correspond to table
136
139
    columns, and there are also special fields for
138
141
     - table's null-complementation byte
139
142
     - [new] table's rowid.
140
143
  */
141
 
  uint fields; 
142
 
  uint length; 
143
 
  uint blobs;
 
144
  uint32_t fields; 
 
145
  uint32_t length; 
 
146
  uint32_t blobs;
144
147
  CACHE_FIELD *field;
145
148
  CACHE_FIELD **blob_ptr;
146
149
  SQL_SELECT *select;
180
183
 
181
184
typedef struct st_join_table {
182
185
  st_join_table() {}                          /* Remove gcc warning */
183
 
  TABLE         *table;
 
186
  Table         *table;
184
187
  KEYUSE        *keyuse;                        /**< pointer to first used key */
185
188
  SQL_SELECT    *select;
186
189
  COND          *select_cond;
264
267
    Embedding SJ-nest (may be not the direct parent), or NULL if none.
265
268
    This variable holds the result of table pullout.
266
269
  */
267
 
  TABLE_LIST    *emb_sj_nest;
 
270
  TableList    *emb_sj_nest;
268
271
 
269
272
  /* Variables for semi-join duplicate elimination */
270
273
  SJ_TMP_TABLE  *flush_weedout_table;
363
366
} ROLLUP;
364
367
 
365
368
 
366
 
/*
367
 
  Describes use of one temporary table to weed out join duplicates.
368
 
  The temporar
369
 
 
370
 
  Used to
371
 
    - create a temp table
372
 
    - when we reach the weed-out tab, walk through rowid-ed tabs and
373
 
      and copy rowids.
374
 
      For each table we need
375
 
       - rowid offset
376
 
       - null bit address.
377
 
*/
378
 
 
379
 
class SJ_TMP_TABLE : public Sql_alloc
380
 
{
381
 
public:
382
 
  /* Array of pointers to tables that should be "used" */
383
 
  class TAB
384
 
  {
385
 
  public:
386
 
    JOIN_TAB *join_tab;
387
 
    uint rowid_offset;
388
 
    ushort null_byte;
389
 
    uchar null_bit;
390
 
  };
391
 
  TAB *tabs;
392
 
  TAB *tabs_end;
393
 
 
394
 
  uint null_bits;
395
 
  uint null_bytes;
396
 
  uint rowid_len;
397
 
 
398
 
  TABLE *tmp_table;
399
 
 
400
 
  MI_COLUMNDEF *start_recinfo;
401
 
  MI_COLUMNDEF *recinfo;
402
 
 
403
 
  /* Pointer to next table (next->start_idx > this->end_idx) */
404
 
  SJ_TMP_TABLE *next; 
405
 
};
406
 
 
407
 
 
408
369
class JOIN :public Sql_alloc
409
370
{
410
371
  JOIN(const JOIN &rhs);                        /**< not implemented */
413
374
  JOIN_TAB *join_tab,**best_ref;
414
375
  JOIN_TAB **map2table;    ///< mapping between table indexes and JOIN_TABs
415
376
  JOIN_TAB *join_tab_save; ///< saved join_tab for subquery reexecution
416
 
  TABLE    **table,**all_tables;
 
377
  Table    **table,**all_tables;
417
378
  /**
418
379
    The table which has an index that allows to produce the requried ordering.
419
380
    A special value of 0x1 means that the ordering will be produced by
420
381
    passing 1st non-const table to filesort(). NULL means no such table exists.
421
382
  */
422
 
  TABLE    *sort_by_table;
 
383
  Table    *sort_by_table;
423
384
  uint     tables;        /**< Number of tables in the join */
424
385
  uint     outer_tables;  /**< Number of tables that are not inside semijoin */
425
386
  uint     const_tables;
454
415
  double   best_read;
455
416
  List<Item> *fields;
456
417
  List<Cached_item> group_fields, group_fields_cache;
457
 
  TABLE    *tmp_table;
 
418
  Table    *tmp_table;
458
419
  /// used to store 2 possible tmp table of SELECT
459
 
  TABLE    *exec_tmp_table1, *exec_tmp_table2;
 
420
  Table    *exec_tmp_table1, *exec_tmp_table2;
460
421
  THD      *thd;
461
422
  Item_sum  **sum_funcs, ***sum_funcs_end;
462
423
  /** second copy of sumfuncs (for queries with 2 temporary tables */
467
428
  uint64_t  select_options;
468
429
  select_result *result;
469
430
  TMP_TABLE_PARAM tmp_table_param;
470
 
  MYSQL_LOCK *lock;
 
431
  DRIZZLE_LOCK *lock;
471
432
  /// unit structure (with global parameters) for this select
472
433
  SELECT_LEX_UNIT *unit;
473
434
  /// select that processed
495
456
  bool group_optimized_away;
496
457
 
497
458
  /*
498
 
    simple_xxxxx is set if ORDER/GROUP BY doesn't include any references
 
459
    simple_xxxxx is set if order_st/GROUP BY doesn't include any references
499
460
    to other tables than the first non-constant table in the JOIN.
500
 
    It's also set if ORDER/GROUP BY is empty.
 
461
    It's also set if order_st/GROUP BY is empty.
501
462
  */
502
463
  bool simple_order, simple_group;
503
464
  /**
504
465
    Is set only in case if we have a GROUP BY clause
505
 
    and no ORDER BY after constant elimination of 'order'.
 
466
    and no order_st BY after constant elimination of 'order'.
506
467
  */
507
468
  bool no_order;
508
 
  /** Is set if we have a GROUP BY and we have ORDER BY on a constant. */
 
469
  /** Is set if we have a GROUP BY and we have order_st BY on a constant. */
509
470
  bool          skip_sort_order;
510
471
 
511
472
  bool need_tmp, hidden_group_fields;
519
480
  List<Item> &fields_list; ///< hold field list passed to mysql_select
520
481
  int error;
521
482
 
522
 
  ORDER *order, *group_list, *proc_param; //hold parameters of mysql_select
 
483
  order_st *order, *group_list, *proc_param; //hold parameters of mysql_select
523
484
  COND *conds;                            // ---"---
524
485
  Item *conds_history;                    // store WHERE for explain
525
 
  TABLE_LIST *tables_list;           ///<hold 'tables' parameter of mysql_select
526
 
  List<TABLE_LIST> *join_list;       ///< list of joined tables in reverse order
 
486
  TableList *tables_list;           ///<hold 'tables' parameter of mysql_select
 
487
  List<TableList> *join_list;       ///< list of joined tables in reverse order
527
488
  COND_EQUAL *cond_equal;
528
489
  SQL_SELECT *select;                ///<created in optimisation phase
529
490
  JOIN_TAB *return_tab;              ///<used only for outer joins
551
512
    excessive memory usage.
552
513
  */  
553
514
  SORT_FIELD *sortorder;                        // make_unireg_sortorder()
554
 
  TABLE **table_reexec;                         // make_simple_join()
 
515
  Table **table_reexec;                         // make_simple_join()
555
516
  JOIN_TAB *join_tab_reexec;                    // make_simple_join()
556
517
  /* end of allocation caching storage */
557
518
 
610
571
 
611
572
    all_fields= fields_arg;
612
573
    fields_list= fields_arg;
613
 
    memset((char*) &keyuse, 0, sizeof(keyuse));
 
574
    memset(&keyuse, 0, sizeof(keyuse));
614
575
    tmp_table_param.init();
615
576
    tmp_table_param.end_write_records= HA_POS_ERROR;
616
577
    rollup.state= ROLLUP::STATE_NONE;
619
580
    no_const_tables= false;
620
581
  }
621
582
 
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,
 
583
  int prepare(Item ***rref_pointer_array, TableList *tables, uint wind_num,
 
584
              COND *conds, uint og_num, order_st *order, order_st *group,
 
585
              Item *having, order_st *proc_param, SELECT_LEX *select,
625
586
              SELECT_LEX_UNIT *unit);
626
587
  int optimize();
627
588
  int reinit();
636
597
 
637
598
  inline void set_items_ref_array(Item **ptr)
638
599
  {
639
 
    memcpy((char*) ref_pointer_array, (char*) ptr, ref_pointer_array_size);
 
600
    memcpy(ref_pointer_array, ptr, ref_pointer_array_size);
640
601
    current_ref_pointer_array= ptr;
641
602
  }
642
603
  inline void init_items_ref_array()
650
611
  bool rollup_make_fields(List<Item> &all_fields, List<Item> &fields,
651
612
                          Item_sum ***func);
652
613
  int rollup_send_data(uint idx);
653
 
  int rollup_write_data(uint idx, TABLE *table);
 
614
  int rollup_write_data(uint idx, Table *table);
654
615
  void remove_subq_pushed_predicates(Item **where);
655
616
  /**
656
617
    Release memory and, if possible, the open tables held by this execution
687
648
 
688
649
/* Extern functions in sql_select.cc */
689
650
bool store_val_in_field(Field *field, Item *val, enum_check_fields check_flag);
690
 
TABLE *create_tmp_table(THD *thd,TMP_TABLE_PARAM *param,List<Item> &fields,
691
 
                        ORDER *group, bool distinct, bool save_sum_fields,
 
651
Table *create_tmp_table(THD *thd,TMP_TABLE_PARAM *param,List<Item> &fields,
 
652
                        order_st *group, bool distinct, bool save_sum_fields,
692
653
                        uint64_t select_options, ha_rows rows_limit,
693
654
                        char* alias);
694
 
void free_tmp_table(THD *thd, TABLE *entry);
 
655
void free_tmp_table(THD *thd, Table *entry);
695
656
void count_field_types(SELECT_LEX *select_lex, TMP_TABLE_PARAM *param, 
696
657
                       List<Item> &fields, bool reset_with_sum_func);
697
658
bool setup_copy_fields(THD *thd, TMP_TABLE_PARAM *param,
700
661
                       uint elements, List<Item> &fields);
701
662
void copy_fields(TMP_TABLE_PARAM *param);
702
663
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);
708
664
Field* create_tmp_field_from_field(THD *thd, Field* org_field,
709
 
                                   const char *name, TABLE *table,
 
665
                                   const char *name, Table *table,
710
666
                                   Item_field *item, uint convert_blob_length);
711
667
                                                                      
712
668
/* functions from opt_sum.cc */
713
669
bool simple_pred(Item_func *func_item, Item **args, bool *inv_order);
714
 
int opt_sum_query(TABLE_LIST *tables, List<Item> &all_fields,COND *conds);
 
670
int opt_sum_query(TableList *tables, List<Item> &all_fields,COND *conds);
715
671
 
716
672
/* from sql_delete.cc, used by opt_range.cc */
717
673
extern "C" int refpos_order_cmp(void* arg, const void *a,const void *b);
796
752
 protected: 
797
753
  enum store_key_result copy_inner()
798
754
  {
799
 
    TABLE *table= copy_field.to_field->table;
800
 
    my_bitmap_map *old_map= dbug_tmp_use_all_columns(table,
801
 
                                                     table->write_set);
802
755
    copy_field.do_copy(&copy_field);
803
 
    dbug_tmp_restore_column_map(table->write_set, old_map);
804
756
    null_key= to_field->is_null();
805
757
    return err != 0 ? STORE_KEY_FATAL : STORE_KEY_OK;
806
758
  }
823
775
 protected:  
824
776
  enum store_key_result copy_inner()
825
777
  {
826
 
    TABLE *table= to_field->table;
827
 
    my_bitmap_map *old_map= dbug_tmp_use_all_columns(table,
828
 
                                                     table->write_set);
829
778
    int res= item->save_in_field(to_field, 1);
830
 
    dbug_tmp_restore_column_map(table->write_set, old_map);
831
779
    null_key= to_field->is_null() || item->null_value;
832
780
    return (err != 0 || res > 2 ? STORE_KEY_FATAL : (store_key_result) res); 
833
781
  }
866
814
  }
867
815
};
868
816
 
869
 
bool cp_buffer_from_ref(THD *thd, TABLE *table, TABLE_REF *ref);
 
817
bool cp_buffer_from_ref(THD *thd, TABLE_REF *ref);
870
818
bool error_if_full_join(JOIN *join);
871
 
int report_error(TABLE *table, int error);
872
819
int safe_index_read(JOIN_TAB *tab);
873
820
COND *remove_eq_conds(THD *thd, COND *cond, Item::cond_result *cond_value);
874
821
int test_if_item_cache_changed(List<Cached_item> &list);