~ubuntu-branches/ubuntu/trusty/drizzle/trusty

« back to all changes in this revision

Viewing changes to drizzled/join_table.h

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2010-03-18 12:12:31 UTC
  • Revision ID: james.westby@ubuntu.com-20100318121231-k6g1xe6cshbwa0f8
Tags: upstream-2010.03.1347
ImportĀ upstreamĀ versionĀ 2010.03.1347

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2008-2009 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; either version 2 of the License, or
 
9
 *  (at your option) any later version.
 
10
 *
 
11
 *  This program is distributed in the hope that it will be useful,
 
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *  GNU General Public License for more details.
 
15
 *
 
16
 *  You should have received a copy of the GNU General Public License
 
17
 *  along with this program; if not, write to the Free Software
 
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
19
 */
 
20
 
 
21
/**
 
22
 * @file
 
23
 *
 
24
 * Defines the JoinTable class which is the primary class
 
25
 * used in the nested loops join implementation.
 
26
 */
 
27
 
 
28
#ifndef DRIZZLED_JOIN_TABLE_H
 
29
#define DRIZZLED_JOIN_TABLE_H
 
30
 
 
31
#include "drizzled/enum_nested_loop_state.h"
 
32
#include "drizzled/table_reference.h"
 
33
#include "drizzled/optimizer/range.h"
 
34
#include "drizzled/join_cache.h"
 
35
#include "drizzled/optimizer/key_use.h"
 
36
 
 
37
#include <bitset>
 
38
 
 
39
namespace drizzled
 
40
{
 
41
 
 
42
/* Values for JoinTable::packed_info */
 
43
#define TAB_INFO_HAVE_VALUE 1
 
44
#define TAB_INFO_USING_INDEX 2
 
45
#define TAB_INFO_USING_WHERE 4
 
46
#define TAB_INFO_FULL_SCAN_ON_NULL 8
 
47
 
 
48
class Table;
 
49
 
 
50
 
 
51
/** Description of an access method */
 
52
enum access_method
 
53
 
54
  AM_UNKNOWN,
 
55
  AM_SYSTEM,
 
56
  AM_CONST,
 
57
  AM_EQ_REF,
 
58
  AM_REF,
 
59
  AM_MAYBE_REF,
 
60
        AM_ALL,
 
61
  AM_RANGE,
 
62
  AM_NEXT,
 
63
  AM_REF_OR_NULL,
 
64
  AM_UNIQUE_SUBQUERY,
 
65
  AM_INDEX_SUBQUERY,
 
66
  AM_INDEX_MERGE
 
67
};
 
68
 
 
69
 
 
70
class JoinTable 
 
71
{
 
72
public:
 
73
  JoinTable() :
 
74
    table(NULL),
 
75
    keyuse(NULL),
 
76
    select(NULL),
 
77
    select_cond(NULL),
 
78
    quick(NULL),
 
79
    pre_idx_push_select_cond(NULL),
 
80
    on_expr_ref(NULL),
 
81
    cond_equal(NULL),
 
82
    first_inner(NULL),
 
83
    found(false),
 
84
    not_null_compl(false),
 
85
    last_inner(NULL),
 
86
    first_upper(NULL),
 
87
    first_unmatched(NULL),
 
88
    packed_info(0),
 
89
    read_first_record(NULL),
 
90
    next_select(NULL),
 
91
    worst_seeks(0.0),
 
92
    const_keys(0),
 
93
    checked_keys(0),
 
94
    needed_reg(0),
 
95
    keys(0),
 
96
    records(0),
 
97
    found_records(0),
 
98
    read_time(0),
 
99
    dependent(0),
 
100
    key_dependent(0),
 
101
    use_quick(0),
 
102
    index(0),
 
103
    status(0),
 
104
    used_fields(0),
 
105
    used_fieldlength(0),
 
106
    used_blobs(0),
 
107
    type(AM_UNKNOWN),
 
108
    cached_eq_ref_table(0),
 
109
    eq_ref_table(0),
 
110
    not_used_in_distinct(0),
 
111
    sorted(0),
 
112
    limit(0),
 
113
    join(NULL),
 
114
    insideout_match_tab(NULL),
 
115
    insideout_buf(NULL),
 
116
    found_match(false),
 
117
    rowid_keep_flags(0),
 
118
    embedding_map(0)
 
119
  {}
 
120
  Table *table;
 
121
  optimizer::KeyUse *keyuse; /**< pointer to first used key */
 
122
  optimizer::SqlSelect *select;
 
123
  COND *select_cond;
 
124
  optimizer::QuickSelectInterface *quick;
 
125
  /**
 
126
    The value of select_cond before we've attempted to do Index Condition
 
127
    Pushdown. We may need to restore everything back if we first choose one
 
128
    index but then reconsider (see test_if_skip_sort_order() for such
 
129
    scenarios).
 
130
    NULL means no index condition pushdown was performed.
 
131
  */
 
132
  Item *pre_idx_push_select_cond;
 
133
  Item **on_expr_ref;   /**< pointer to the associated on expression   */
 
134
  COND_EQUAL *cond_equal;    /**< multiple equalities for the on expression */
 
135
  JoinTable *first_inner;   /**< first inner table for including outerjoin */
 
136
  bool found;         /**< true after all matches or null complement */
 
137
  bool not_null_compl;/**< true before null complement is added      */
 
138
  JoinTable *last_inner;    /**< last table table for embedding outer join */
 
139
  JoinTable *first_upper;  /**< first inner table for embedding outer join */
 
140
  JoinTable *first_unmatched; /**< used for optimization purposes only     */
 
141
 
 
142
  /* Special content for EXPLAIN 'Extra' column or NULL if none */
 
143
  const char *info;
 
144
  /*
 
145
    Bitmap of TAB_INFO_* bits that encodes special line for EXPLAIN 'Extra'
 
146
    column, or 0 if there is no info.
 
147
  */
 
148
  uint32_t packed_info;
 
149
 
 
150
  Read_record_func read_first_record;
 
151
  Next_select_func next_select;
 
152
  READ_RECORD   read_record;
 
153
  /*
 
154
    Currently the following two fields are used only for a [NOT] IN subquery
 
155
    if it is executed by an alternative full table scan when the left operand of
 
156
    the subquery predicate is evaluated to NULL.
 
157
  */
 
158
  Read_record_func save_read_first_record; /**< to save read_first_record */
 
159
  int (*save_read_record) (READ_RECORD *); /**< to save read_record.read_record */
 
160
  double worst_seeks;
 
161
  key_map       const_keys; /**< Keys with constant part */
 
162
  key_map       checked_keys; /**< Keys checked in find_best */
 
163
  key_map       needed_reg;
 
164
  key_map keys; /**< all keys with can be used */
 
165
 
 
166
  /** Either #rows in the table or 1 for const table.  */
 
167
  ha_rows       records;
 
168
  /**
 
169
    Number of records that will be scanned (yes scanned, not returned) by the
 
170
    best 'independent' access method, i.e. table scan or QUICK_*_SELECT)
 
171
  */
 
172
  ha_rows found_records;
 
173
  /**
 
174
    Cost of accessing the table using "ALL" or range/index_merge access
 
175
    method (but not 'index' for some reason), i.e. this matches method which
 
176
    E(#records) is in found_records.
 
177
  */
 
178
  ha_rows read_time;
 
179
 
 
180
  table_map     dependent;
 
181
  table_map key_dependent;
 
182
  uint32_t use_quick;
 
183
  uint32_t index;
 
184
  uint32_t status; /**< Save status for cache */
 
185
  uint32_t used_fields; /**< Number of used fields in join set */
 
186
  uint32_t used_fieldlength; /**< Not sure... */
 
187
  uint32_t used_blobs; /**< Number of BLOB fields in join set */
 
188
  enum access_method type; /**< Access method. */
 
189
  bool cached_eq_ref_table;
 
190
  bool eq_ref_table;
 
191
  bool not_used_in_distinct;
 
192
  /** True if index-based access method must return records in order */
 
193
  bool sorted;
 
194
  /**
 
195
    If it's not 0 the number stored this field indicates that the index
 
196
    scan has been chosen to access the table data and we expect to scan
 
197
    this number of rows for the table.
 
198
  */
 
199
  ha_rows limit;
 
200
  table_reference_st    ref;
 
201
  JOIN_CACHE cache;
 
202
  JOIN *join;
 
203
 
 
204
  /**
 
205
     ptr  - this join tab should do an InsideOut scan. Points
 
206
            to the tab for which we'll need to check tab->found_match.
 
207
 
 
208
     NULL - Not an insideout scan.
 
209
  */
 
210
  JoinTable *insideout_match_tab;
 
211
  unsigned char *insideout_buf; /**< Buffer to save index tuple to be able to skip dups */
 
212
 
 
213
  /** Used by InsideOut scan. Just set to true when have found a row. */
 
214
  bool found_match;
 
215
 
 
216
  enum 
 
217
  {
 
218
    /* If set, the rowid of this table must be put into the temptable. */
 
219
    KEEP_ROWID=1,
 
220
    /*
 
221
      If set, one should call h->position() to obtain the rowid,
 
222
      otherwise, the rowid is assumed to already be in h->ref
 
223
      (this is because join caching and filesort() save the rowid and then
 
224
      put it back into h->ref)
 
225
    */
 
226
    CALL_POSITION=2
 
227
  };
 
228
  /** A set of flags from the above enum */
 
229
  int rowid_keep_flags;
 
230
 
 
231
  /** Bitmap of nested joins this table is part of */
 
232
  std::bitset<64> embedding_map;
 
233
 
 
234
  void cleanup();
 
235
 
 
236
  inline bool is_using_loose_index_scan()
 
237
  {
 
238
    return (select && select->quick &&
 
239
            (select->quick->get_type() ==
 
240
             optimizer::QuickSelectInterface::QS_TYPE_GROUP_MIN_MAX));
 
241
  }
 
242
 
 
243
  void readCachedRecord();
 
244
};
 
245
 
 
246
} /* namespace drizzled */
 
247
 
 
248
#endif /* DRIZZLED_JOIN_TABLE_H */