~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_select.h

  • Committer: Brian Aker
  • Date: 2008-11-04 15:39:09 UTC
  • mfrom: (575.1.2 devel)
  • Revision ID: brian@tangent.org-20081104153909-c72hn65udxs1ccal
Merge of Monty's work

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2000-2006 MySQL AB
2
 
 
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.
6
 
 
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.
11
 
 
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 */
15
 
 
 
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2008 Sun Microsystems
 
5
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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
 
18
 */
16
19
 
17
20
/**
18
21
  @file
21
24
  classes to use when handling where clause
22
25
*/
23
26
 
24
 
#ifdef USE_PRAGMA_INTERFACE
25
 
#pragma interface                       /* gcc class implementation */
26
 
#endif
27
27
 
28
28
/* PREV_BITS only used in sql_select.cc */
29
29
#define PREV_BITS(type,A)       ((type) (((type) 1 << (A)) -1))
30
30
 
31
31
#include <storage/myisam/myisam.h>
 
32
#include <drizzled/sql_array.h>
32
33
 
33
34
/* Values in optimize */
34
35
#define KEY_OPTIMIZE_EXISTS             1
39
40
  Item  *val;                           /**< or value if no field */
40
41
  table_map used_tables;
41
42
  uint  key, keypart;
42
 
  uint optimize; // 0, or KEY_OPTIMIZE_*
 
43
  uint32_t optimize; // 0, or KEY_OPTIMIZE_*
43
44
  key_part_map keypart_map;
44
45
  ha_rows      ref_table_rows;
45
46
  /**
62
63
     0..64    <=> This was created from semi-join IN-equality # sj_pred_no.
63
64
     MAX_UINT  Otherwise
64
65
  */
65
 
  uint         sj_pred_no;
 
66
  uint32_t         sj_pred_no;
66
67
} KEYUSE;
67
68
 
68
69
class store_key;
211
212
    Bitmap of TAB_INFO_* bits that encodes special line for EXPLAIN 'Extra'
212
213
    column, or 0 if there is no info.
213
214
  */
214
 
  uint          packed_info;
 
215
  uint32_t          packed_info;
215
216
 
216
217
  Read_record_func read_first_record;
217
218
  Next_select_func next_select;
281
282
     NULL - Not an insideout scan.
282
283
  */
283
284
  struct st_join_table *insideout_match_tab;
284
 
  uchar *insideout_buf; // Buffer to save index tuple to be able to skip dups
 
285
  unsigned char *insideout_buf; // Buffer to save index tuple to be able to skip dups
285
286
 
286
287
  /* Used by InsideOut scan. Just set to true when have found a row. */
287
288
  bool found_match;
382
383
  */
383
384
  Table    *sort_by_table;
384
385
  uint     tables;        /**< Number of tables in the join */
385
 
  uint     outer_tables;  /**< Number of tables that are not inside semijoin */
386
 
  uint     const_tables;
 
386
  uint32_t     outer_tables;  /**< Number of tables that are not inside semijoin */
 
387
  uint32_t     const_tables;
387
388
  uint     send_group_parts;
388
389
  bool     sort_and_group,first_record,full_join,group, no_field_update;
389
390
  bool     do_send_rows;
418
419
  Table    *tmp_table;
419
420
  /// used to store 2 possible tmp table of SELECT
420
421
  Table    *exec_tmp_table1, *exec_tmp_table2;
421
 
  THD      *thd;
 
422
  Session          *session;
422
423
  Item_sum  **sum_funcs, ***sum_funcs_end;
423
424
  /** second copy of sumfuncs (for queries with 2 temporary tables */
424
425
  Item_sum  **sum_funcs2, ***sum_funcs_end2;
491
492
  Item **ref_pointer_array; ///<used pointer reference for this select
492
493
  // Copy of above to be used with different lists
493
494
  Item **items0, **items1, **items2, **items3, **current_ref_pointer_array;
494
 
  uint ref_pointer_array_size; ///< size of above in bytes
 
495
  uint32_t ref_pointer_array_size; ///< size of above in bytes
495
496
  const char *zero_result_cause; ///< not 0 if exec must return zero result
496
497
  
497
498
  bool union_part; ///< this subselect is part of union 
516
517
  JOIN_TAB *join_tab_reexec;                    // make_simple_join()
517
518
  /* end of allocation caching storage */
518
519
 
519
 
  JOIN(THD *thd_arg, List<Item> &fields_arg, uint64_t select_options_arg,
 
520
  JOIN(Session *session_arg, List<Item> &fields_arg, uint64_t select_options_arg,
520
521
       select_result *result_arg)
521
 
    :fields_list(fields_arg), sj_subselects(thd_arg->mem_root, 4)
 
522
    :fields_list(fields_arg), sj_subselects(session_arg->mem_root, 4)
522
523
  {
523
 
    init(thd_arg, fields_arg, select_options_arg, result_arg);
 
524
    init(session_arg, fields_arg, select_options_arg, result_arg);
524
525
  }
525
526
 
526
 
  void init(THD *thd_arg, List<Item> &fields_arg, uint64_t select_options_arg,
 
527
  void init(Session *session_arg, List<Item> &fields_arg, uint64_t select_options_arg,
527
528
       select_result *result_arg)
528
529
  {
529
530
    join_tab= join_tab_save= 0;
544
545
    sortorder= 0;
545
546
    table_reexec= 0;
546
547
    join_tab_reexec= 0;
547
 
    thd= thd_arg;
 
548
    session= session_arg;
548
549
    sum_funcs= sum_funcs2= 0;
549
550
    having= tmp_having= having_history= 0;
550
551
    select_options= select_options_arg;
551
552
    result= result_arg;
552
 
    lock= thd_arg->lock;
 
553
    lock= session_arg->lock;
553
554
    select_lex= 0; //for safety
554
555
    tmp_join= 0;
555
556
    select_distinct= test(select_options & SELECT_DISTINCT);
580
581
    no_const_tables= false;
581
582
  }
582
583
 
583
 
  int prepare(Item ***rref_pointer_array, TableList *tables, uint wind_num,
584
 
              COND *conds, uint og_num, order_st *order, order_st *group,
 
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,
585
586
              Item *having, order_st *proc_param, SELECT_LEX *select,
586
587
              SELECT_LEX_UNIT *unit);
587
588
  int optimize();
610
611
  bool rollup_init();
611
612
  bool rollup_make_fields(List<Item> &all_fields, List<Item> &fields,
612
613
                          Item_sum ***func);
613
 
  int rollup_send_data(uint idx);
614
 
  int rollup_write_data(uint idx, Table *table);
 
614
  int rollup_send_data(uint32_t idx);
 
615
  int rollup_write_data(uint32_t idx, Table *table);
615
616
  void remove_subq_pushed_predicates(Item **where);
616
617
  /**
617
618
    Release memory and, if possible, the open tables held by this execution
633
634
  bool change_result(select_result *result);
634
635
  bool is_top_level_join() const
635
636
  {
636
 
    return (unit == &thd->lex->unit && (unit->fake_select_lex == 0 ||
 
637
    return (unit == &session->lex->unit && (unit->fake_select_lex == 0 ||
637
638
                                        select_lex == unit->fake_select_lex));
638
639
  }
639
640
};
640
641
 
641
642
 
642
643
typedef struct st_select_check {
643
 
  uint const_ref,reg_ref;
 
644
  uint32_t const_ref,reg_ref;
644
645
} SELECT_CHECK;
645
646
 
646
647
extern const char *join_type_str[];
648
649
 
649
650
/* Extern functions in sql_select.cc */
650
651
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
Table *create_tmp_table(Session *session,TMP_TABLE_PARAM *param,List<Item> &fields,
652
653
                        order_st *group, bool distinct, bool save_sum_fields,
653
654
                        uint64_t select_options, ha_rows rows_limit,
654
655
                        char* alias);
655
 
void free_tmp_table(THD *thd, Table *entry);
 
656
void free_tmp_table(Session *session, Table *entry);
656
657
void count_field_types(SELECT_LEX *select_lex, TMP_TABLE_PARAM *param, 
657
658
                       List<Item> &fields, bool reset_with_sum_func);
658
 
bool setup_copy_fields(THD *thd, TMP_TABLE_PARAM *param,
 
659
bool setup_copy_fields(Session *session, TMP_TABLE_PARAM *param,
659
660
                       Item **ref_pointer_array,
660
661
                       List<Item> &new_list1, List<Item> &new_list2,
661
 
                       uint elements, List<Item> &fields);
 
662
                       uint32_t elements, List<Item> &fields);
662
663
void copy_fields(TMP_TABLE_PARAM *param);
663
664
void copy_funcs(Item **func_ptr);
664
 
Field* create_tmp_field_from_field(THD *thd, Field* org_field,
 
665
Field* create_tmp_field_from_field(Session *session, Field* org_field,
665
666
                                   const char *name, Table *table,
666
 
                                   Item_field *item, uint convert_blob_length);
 
667
                                   Item_field *item, uint32_t convert_blob_length);
667
668
                                                                      
668
669
/* functions from opt_sum.cc */
669
670
bool simple_pred(Item_func *func_item, Item **args, bool *inv_order);
679
680
public:
680
681
  bool null_key; /* true <=> the value of the key has a null part */
681
682
  enum store_key_result { STORE_KEY_OK, STORE_KEY_FATAL, STORE_KEY_CONV };
682
 
  store_key(THD *thd, Field *field_arg, uchar *ptr, uchar *null, uint length)
 
683
  store_key(Session *session, Field *field_arg, unsigned char *ptr, unsigned char *null, uint32_t length)
683
684
    :null_key(0), null_ptr(null), err(0)
684
685
  {
685
686
    if (field_arg->type() == DRIZZLE_TYPE_BLOB)
694
695
      to_field->init(field_arg->table);
695
696
    }
696
697
    else
697
 
      to_field=field_arg->new_key_field(thd->mem_root, field_arg->table,
 
698
      to_field=field_arg->new_key_field(session->mem_root, field_arg->table,
698
699
                                        ptr, null, 1);
699
700
  }
700
701
  virtual ~store_key() {}                       /** Not actually needed */
709
710
  enum store_key_result copy()
710
711
  {
711
712
    enum store_key_result result;
712
 
    THD *thd= to_field->table->in_use;
713
 
    enum_check_fields saved_count_cuted_fields= thd->count_cuted_fields;
 
713
    Session *session= to_field->table->in_use;
 
714
    enum_check_fields saved_count_cuted_fields= session->count_cuted_fields;
714
715
 
715
 
    thd->count_cuted_fields= CHECK_FIELD_IGNORE;
 
716
    session->count_cuted_fields= CHECK_FIELD_IGNORE;
716
717
 
717
718
    result= copy_inner();
718
719
 
719
 
    thd->count_cuted_fields= saved_count_cuted_fields;
 
720
    session->count_cuted_fields= saved_count_cuted_fields;
720
721
 
721
722
    return result;
722
723
  }
723
724
 
724
725
 protected:
725
726
  Field *to_field;                              // Store data here
726
 
  uchar *null_ptr;
727
 
  uchar err;
 
727
  unsigned char *null_ptr;
 
728
  unsigned char err;
728
729
 
729
730
  virtual enum store_key_result copy_inner()=0;
730
731
};
735
736
  Copy_field copy_field;
736
737
  const char *field_name;
737
738
 public:
738
 
  store_key_field(THD *thd, Field *to_field_arg, uchar *ptr,
739
 
                  uchar *null_ptr_arg,
740
 
                  uint length, Field *from_field, const char *name_arg)
741
 
    :store_key(thd, to_field_arg,ptr,
 
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,
742
743
               null_ptr_arg ? null_ptr_arg : from_field->maybe_null() ? &err
743
 
               : (uchar*) 0, length), field_name(name_arg)
 
744
               : (unsigned char*) 0, length), field_name(name_arg)
744
745
  {
745
746
    if (to_field)
746
747
    {
764
765
 protected:
765
766
  Item *item;
766
767
public:
767
 
  store_key_item(THD *thd, Field *to_field_arg, uchar *ptr,
768
 
                 uchar *null_ptr_arg, uint length, Item *item_arg)
769
 
    :store_key(thd, to_field_arg, ptr,
 
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,
770
771
               null_ptr_arg ? null_ptr_arg : item_arg->maybe_null ?
771
 
               &err : (uchar*) 0, length), item(item_arg)
 
772
               &err : (unsigned char*) 0, length), item(item_arg)
772
773
  {}
773
774
  const char *name() const { return "func"; }
774
775
 
786
787
{
787
788
  bool inited;
788
789
public:
789
 
  store_key_const_item(THD *thd, Field *to_field_arg, uchar *ptr,
790
 
                       uchar *null_ptr_arg, uint length,
 
790
  store_key_const_item(Session *session, Field *to_field_arg, unsigned char *ptr,
 
791
                       unsigned char *null_ptr_arg, uint32_t length,
791
792
                       Item *item_arg)
792
 
    :store_key_item(thd, to_field_arg,ptr,
 
793
    :store_key_item(session, to_field_arg,ptr,
793
794
                    null_ptr_arg ? null_ptr_arg : item_arg->maybe_null ?
794
 
                    &err : (uchar*) 0, length, item_arg), inited(0)
 
795
                    &err : (unsigned char*) 0, length, item_arg), inited(0)
795
796
  {
796
797
  }
797
798
  const char *name() const { return "const"; }
814
815
  }
815
816
};
816
817
 
817
 
bool cp_buffer_from_ref(THD *thd, TABLE_REF *ref);
 
818
bool cp_buffer_from_ref(Session *session, TABLE_REF *ref);
818
819
bool error_if_full_join(JOIN *join);
819
820
int safe_index_read(JOIN_TAB *tab);
820
 
COND *remove_eq_conds(THD *thd, COND *cond, Item::cond_result *cond_value);
 
821
COND *remove_eq_conds(Session *session, COND *cond, Item::cond_result *cond_value);
821
822
int test_if_item_cache_changed(List<Cached_item> &list);