~drizzle-developers/ubuntu/natty/drizzle/natty

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 *
 *  Copyright (C) 2009 Sun Microsystems
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifndef DRIZZLED_OPTIMIZER_POSITION_H
#define DRIZZLED_OPTIMIZER_POSITION_H

#include <drizzled/join_table.h>

namespace drizzled
{
namespace optimizer
{

/**
 * Information about a position of table within a join order. Used in join
 * optimization.
 */
class Position
{
public:

  Position()
    :
      records_read(0),
      read_time(0),
      table(NULL),
      key(NULL),
      ref_depend_map(0)
  {}

  Position(double in_records_read,
           double in_read_time,
           JoinTable *in_table,
           KeyUse *in_key,
           table_map in_ref_depend_map)
    :
      records_read(in_records_read),
      read_time(in_read_time),
      table(in_table),
      key(in_key),
      ref_depend_map(in_ref_depend_map)
  {}

  Position(const Position &rhs)
    :
      records_read(rhs.records_read),
      read_time(rhs.read_time),
      table(rhs.table),
      key(rhs.key),
      ref_depend_map(rhs.ref_depend_map)
  {}

  Position &operator=(const Position &rhs)
  {
    if (this == &rhs)
    {
      return *this;
    }
    records_read= rhs.records_read;
    read_time= rhs.read_time;
    table= rhs.table;
    key= rhs.key;
    ref_depend_map= rhs.ref_depend_map;
    return *this;
  }

  /**
   * Determine whether the table this particular position is representing in
   * the query plan is a const table or not. A constant table is defined as
   * (taken from the MySQL optimizer internals document on MySQL forge):
   *
   * 1) A table with zero rows, or with only one row
   * 2) A table expression that is restricted with a WHERE condition
   *
   * Based on the definition above, when records_read is set to 1.0 in the
   * Position class, it infers that this position in the partial plan
   * represents a const table.
   *
   * @return true if this position represents a const table; false otherwise
   */
  bool isConstTable() const
  {
    return (records_read < 2.0);
  }

  double getFanout() const
  {
    return records_read;
  }

  void setFanout(double in_records_read)
  {
    records_read= in_records_read;
  }

  double getCost() const
  {
    return read_time;
  }

  JoinTable *getJoinTable()
  {
    return table;
  }

  /**
   * Check to see if the table attached to the JoinTable for this position
   * has an index that can produce an ordering. 
   *
   * @return true if the table attached to the JoinTable for this position
   * does not have an index that can produce an ordering; false otherwise
   */
  bool hasTableForSorting(Table *cmp_table) const
  {
    return (cmp_table != table->table);
  }

  bool examinePosition(table_map found_ref)
  {
    if (table->table->map & found_ref)
    {
      return true;
    }
    return false;
  }

  KeyUse *getKeyUse()
  {
    return key;
  }

  table_map getRefDependMap()
  {
    return ref_depend_map;
  }

  void clearRefDependMap()
  {
    ref_depend_map= 0;
  }

private:

  /**
    The "fanout": number of output rows that will be produced (after
    pushed down selection condition is applied) per each row combination of
    previous tables. The value is an in-precise estimate.
  */
  double records_read;

  /**
    Cost accessing the table in course of the entire complete join execution,
    i.e. cost of one access method use (e.g. 'range' or 'ref' scan ) times
    number the access method will be invoked.
  */
  double read_time;

  JoinTable *table;

  /**
    NULL  -  'index' or 'range' or 'index_merge' or 'ALL' access is used.
    Other - [eq_]ref[_or_null] access is used. Pointer to {t.keypart1 = expr}
  */
  KeyUse *key;

  /** If ref-based access is used: bitmap of tables this table depends on  */
  table_map ref_depend_map;

};

} /* end namespace optimizer */

} /* end namespace drizzled */

#endif /* DRIZZLED_OPTIMIZER_POSITION_H */