~drizzle-trunk/drizzle/development

1108.6.30 by Padraig O'Sullivan
Forgot to add the new sub-dir I created for my last commit.
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
4
 *  Copyright (C) 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; 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
 */
19
20
#ifndef DRIZZLED_OPTIMIZER_POSITION_H
21
#define DRIZZLED_OPTIMIZER_POSITION_H
22
23
#include <drizzled/join_table.h>
24
25
namespace drizzled
26
{
27
namespace optimizer
28
{
29
30
/**
31
 * Information about a position of table within a join order. Used in join
32
 * optimization.
33
 */
34
class Position
35
{
36
public:
37
38
  Position()
39
    :
40
      records_read(0),
41
      read_time(0),
42
      table(NULL),
43
      key(NULL),
44
      ref_depend_map(0)
45
  {}
46
47
  Position(double in_records_read,
48
           double in_read_time,
49
           JoinTable *in_table,
50
           KeyUse *in_key,
51
           table_map in_ref_depend_map)
52
    :
53
      records_read(in_records_read),
54
      read_time(in_read_time),
55
      table(in_table),
56
      key(in_key),
57
      ref_depend_map(in_ref_depend_map)
58
  {}
59
1108.6.52 by Padraig O'Sullivan
Added a copy constructor and assignment operator to the Position class.
60
  Position(const Position &rhs)
61
    :
62
      records_read(rhs.records_read),
63
      read_time(rhs.read_time),
64
      table(rhs.table),
65
      key(rhs.key),
66
      ref_depend_map(rhs.ref_depend_map)
67
  {}
68
69
  Position &operator=(const Position &rhs)
70
  {
71
    if (this == &rhs)
72
    {
73
      return *this;
74
    }
75
    records_read= rhs.records_read;
76
    read_time= rhs.read_time;
77
    table= rhs.table;
78
    key= rhs.key;
79
    ref_depend_map= rhs.ref_depend_map;
80
    return *this;
81
  }
82
1108.6.30 by Padraig O'Sullivan
Forgot to add the new sub-dir I created for my last commit.
83
  /**
84
   * Determine whether the table this particular position is representing in
85
   * the query plan is a const table or not. A constant table is defined as
86
   * (taken from the MySQL optimizer internals document on MySQL forge):
87
   *
88
   * 1) A table with zero rows, or with only one row
89
   * 2) A table expression that is restricted with a WHERE condition
90
   *
91
   * Based on the definition above, when records_read is set to 1.0 in the
92
   * Position class, it infers that this position in the partial plan
93
   * represents a const table.
94
   *
95
   * @return true if this position represents a const table; false otherwise
96
   */
97
  bool isConstTable() const
98
  {
99
    return (records_read < 2.0);
100
  }
101
102
  double getFanout() const
103
  {
104
    return records_read;
105
  }
106
107
  void setFanout(double in_records_read)
108
  {
109
    records_read= in_records_read;
110
  }
111
112
  double getCost() const
113
  {
114
    return read_time;
115
  }
116
117
  JoinTable *getJoinTable()
118
  {
119
    return table;
120
  }
121
122
  /**
123
   * Check to see if the table attached to the JoinTable for this position
124
   * has an index that can produce an ordering. 
125
   *
126
   * @return true if the table attached to the JoinTable for this position
127
   * does not have an index that can produce an ordering; false otherwise
128
   */
129
  bool hasTableForSorting(Table *cmp_table) const
130
  {
131
    return (cmp_table != table->table);
132
  }
133
134
  bool examinePosition(table_map found_ref)
135
  {
136
    if (table->table->map & found_ref)
137
    {
138
      return true;
139
    }
140
    return false;
141
  }
142
143
  KeyUse *getKeyUse()
144
  {
145
    return key;
146
  }
147
148
  table_map getRefDependMap()
149
  {
150
    return ref_depend_map;
151
  }
152
153
  void clearRefDependMap()
154
  {
155
    ref_depend_map= 0;
156
  }
157
158
private:
159
160
  /**
161
    The "fanout": number of output rows that will be produced (after
162
    pushed down selection condition is applied) per each row combination of
163
    previous tables. The value is an in-precise estimate.
164
  */
165
  double records_read;
166
167
  /**
168
    Cost accessing the table in course of the entire complete join execution,
169
    i.e. cost of one access method use (e.g. 'range' or 'ref' scan ) times
170
    number the access method will be invoked.
171
  */
172
  double read_time;
173
174
  JoinTable *table;
175
176
  /**
177
    NULL  -  'index' or 'range' or 'index_merge' or 'ALL' access is used.
178
    Other - [eq_]ref[_or_null] access is used. Pointer to {t.keypart1 = expr}
179
  */
180
  KeyUse *key;
181
182
  /** If ref-based access is used: bitmap of tables this table depends on  */
183
  table_map ref_depend_map;
184
185
};
186
187
} /* end namespace optimizer */
188
189
} /* end namespace drizzled */
190
191
#endif /* DRIZZLED_OPTIMIZER_POSITION_H */