~drizzle-trunk/drizzle/development

1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
4
 *  Copyright (C) 2008-2009 Sun Microsystems, Inc.
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
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
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
20
#include <config.h>
21
#include <drizzled/session.h>
22
#include <drizzled/util/functors.h>
23
#include <drizzled/optimizer/range.h>
24
#include <drizzled/optimizer/quick_range_select.h>
25
#include <drizzled/optimizer/quick_ror_union_select.h>
2234.1.3 by Olaf van der Spek
Refactor includes
26
#include <drizzled/table.h>
2241.3.2 by Olaf van der Spek
Refactor Session::variables
27
#include <drizzled/system_variables.h>
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
28
1237.13.15 by Padraig O'Sullivan
Used std::vector instead of List in the QuickRorUnionSelect class.
29
#include <vector>
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
30
#include <algorithm>
31
32
using namespace std;
33
2241.3.2 by Olaf van der Spek
Refactor Session::variables
34
namespace drizzled {
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
35
1237.13.14 by Padraig O'Sullivan
Corrected some style issues in quick_ror_union_select.[cc,h] and range.cc
36
optimizer::QuickRorUnionSelect::QuickRorUnionSelect(Session *session_param,
37
                                                    Table *table)
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
38
  :
39
    session(session_param),
40
    scans_inited(false)
41
{
42
  index= MAX_KEY;
43
  head= table;
44
  rowid_length= table->cursor->ref_length;
45
  record= head->record[0];
2318.6.23 by Olaf van der Spek
Refactor
46
  alloc.init(session->variables.range_alloc_block_size);
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
47
  session_param->mem_root= &alloc;
48
}
49
50
/*
51
 * Function object that is used as the comparison function
1237.13.14 by Padraig O'Sullivan
Corrected some style issues in quick_ror_union_select.[cc,h] and range.cc
52
 * for the priority queue in the QuickRorUnionSelect
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
53
 * class.
54
 */
55
class optimizer::compare_functor
56
{
1237.13.14 by Padraig O'Sullivan
Corrected some style issues in quick_ror_union_select.[cc,h] and range.cc
57
  optimizer::QuickRorUnionSelect *self;
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
58
  public:
1237.13.14 by Padraig O'Sullivan
Corrected some style issues in quick_ror_union_select.[cc,h] and range.cc
59
  compare_functor(optimizer::QuickRorUnionSelect *in_arg)
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
60
    : self(in_arg) { }
61
  inline bool operator()(const optimizer::QuickSelectInterface *i, const optimizer::QuickSelectInterface *j) const
62
  {
63
    int val= self->head->cursor->cmp_ref(i->last_rowid,
64
                                         j->last_rowid);
65
    return (val >= 0);
66
  }
67
};
68
69
1237.13.14 by Padraig O'Sullivan
Corrected some style issues in quick_ror_union_select.[cc,h] and range.cc
70
int optimizer::QuickRorUnionSelect::init()
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
71
{
2318.6.44 by Olaf van der Spek
Refactor
72
  queue= new priority_queue<QuickSelectInterface*, vector<QuickSelectInterface*>, compare_functor>(compare_functor(this));
73
  cur_rowid= alloc.alloc(2 * head->cursor->ref_length);
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
74
  prev_rowid= cur_rowid + head->cursor->ref_length;
2318.6.46 by Olaf van der Spek
Refactor
75
  return 0;
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
76
}
77
78
1237.13.14 by Padraig O'Sullivan
Corrected some style issues in quick_ror_union_select.[cc,h] and range.cc
79
int optimizer::QuickRorUnionSelect::reset()
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
80
{
81
  have_prev_rowid= false;
2385.3.7 by Olaf van der Spek
Refactor DYNAMIC_ARRAY
82
  if (not scans_inited)
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
83
  {
2318.6.64 by Olaf van der Spek
Refactor
84
    BOOST_FOREACH(QuickSelectInterface* it, quick_selects)
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
85
    {
2318.6.64 by Olaf van der Spek
Refactor
86
      if (it->init_ror_merged_scan(false))
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
87
      {
88
        return 0;
89
      }
90
    }
91
    scans_inited= true;
92
  }
2385.3.7 by Olaf van der Spek
Refactor DYNAMIC_ARRAY
93
  while (not queue->empty())
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
94
  {
95
    queue->pop();
96
  }
97
  /*
98
    Initialize scans for merged quick selects and put all merged quick
99
    selects into the queue.
100
  */
2318.6.64 by Olaf van der Spek
Refactor
101
  BOOST_FOREACH(QuickSelectInterface* it, quick_selects)
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
102
  {
2318.6.64 by Olaf van der Spek
Refactor
103
    if (it->reset())
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
104
      return 0;
2318.6.64 by Olaf van der Spek
Refactor
105
    if (it->get_next() == HA_ERR_END_OF_FILE)
106
      continue;
107
    it->save_last_pos();
108
    queue->push(it);
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
109
  }
1491.1.10 by Jay Pipes
ha_rnd_init -> startTableScan, rnd_init -> doStartTableScan, ha_rnd_end -> endTableScan, rnd_end -> doEndTableScan
110
  if (head->cursor->startTableScan(1))
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
111
    return 0;
112
  return 0;
113
}
114
115
2318.6.52 by Olaf van der Spek
Refactor
116
void optimizer::QuickRorUnionSelect::push_quick_back(QuickSelectInterface *quick_sel_range)
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
117
{
1237.13.15 by Padraig O'Sullivan
Used std::vector instead of List in the QuickRorUnionSelect class.
118
  quick_selects.push_back(quick_sel_range);
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
119
}
120
121
1237.13.14 by Padraig O'Sullivan
Corrected some style issues in quick_ror_union_select.[cc,h] and range.cc
122
optimizer::QuickRorUnionSelect::~QuickRorUnionSelect()
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
123
{
124
  while (! queue->empty())
125
  {
126
    queue->pop();
127
  }
128
  delete queue;
1237.13.15 by Padraig O'Sullivan
Used std::vector instead of List in the QuickRorUnionSelect class.
129
  for_each(quick_selects.begin(),
130
           quick_selects.end(),
131
           DeletePtr());
132
  quick_selects.clear();
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
133
  if (head->cursor->inited != Cursor::NONE)
134
  {
1491.1.10 by Jay Pipes
ha_rnd_init -> startTableScan, rnd_init -> doStartTableScan, ha_rnd_end -> endTableScan, rnd_end -> doEndTableScan
135
    head->cursor->endTableScan();
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
136
  }
1487 by Brian Aker
More updates for memory::Root
137
  alloc.free_root(MYF(0));
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
138
}
139
140
1802.16.6 by Padraig O'Sullivan
Added temporary conversion of a bitmap to dynamic_bitset in order to remove references to MyBitmap within optimizer code.
141
bool optimizer::QuickRorUnionSelect::is_keys_used(const boost::dynamic_bitset<>& fields)
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
142
{
2318.6.64 by Olaf van der Spek
Refactor
143
  BOOST_FOREACH(QuickSelectInterface* it, quick_selects)
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
144
  {
2318.6.64 by Olaf van der Spek
Refactor
145
    if (it->is_keys_used(fields))
1237.13.15 by Padraig O'Sullivan
Used std::vector instead of List in the QuickRorUnionSelect class.
146
      return true;
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
147
  }
1237.13.15 by Padraig O'Sullivan
Used std::vector instead of List in the QuickRorUnionSelect class.
148
  return false;
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
149
}
150
151
1237.13.14 by Padraig O'Sullivan
Corrected some style issues in quick_ror_union_select.[cc,h] and range.cc
152
int optimizer::QuickRorUnionSelect::get_next()
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
153
{
1237.13.14 by Padraig O'Sullivan
Corrected some style issues in quick_ror_union_select.[cc,h] and range.cc
154
  int error;
155
  int dup_row;
156
  optimizer::QuickSelectInterface *quick= NULL;
157
  unsigned char *tmp= NULL;
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
158
159
  do
160
  {
161
    do
162
    {
163
      if (queue->empty())
1237.13.14 by Padraig O'Sullivan
Corrected some style issues in quick_ror_union_select.[cc,h] and range.cc
164
        return HA_ERR_END_OF_FILE;
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
165
      /* Ok, we have a queue with >= 1 scans */
166
167
      quick= queue->top();
168
      memcpy(cur_rowid, quick->last_rowid, rowid_length);
169
170
      /* put into queue rowid from the same stream as top element */
171
      if ((error= quick->get_next()))
172
      {
173
        if (error != HA_ERR_END_OF_FILE)
174
          return(error);
175
        queue->pop();
176
      }
177
      else
178
      {
179
        quick->save_last_pos();
180
        queue->pop();
181
        queue->push(quick);
182
      }
183
184
      if (!have_prev_rowid)
185
      {
186
        /* No rows have been returned yet */
187
        dup_row= false;
188
        have_prev_rowid= true;
189
      }
190
      else
1237.13.14 by Padraig O'Sullivan
Corrected some style issues in quick_ror_union_select.[cc,h] and range.cc
191
        dup_row= ! head->cursor->cmp_ref(cur_rowid, prev_rowid);
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
192
    } while (dup_row);
193
194
    tmp= cur_rowid;
195
    cur_rowid= prev_rowid;
196
    prev_rowid= tmp;
197
198
    error= head->cursor->rnd_pos(quick->record, prev_rowid);
199
  } while (error == HA_ERR_RECORD_DELETED);
1237.13.14 by Padraig O'Sullivan
Corrected some style issues in quick_ror_union_select.[cc,h] and range.cc
200
  return error;
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
201
}
202
203
2170.4.5 by Stewart Smith
convert explain_plan extra String to std::string. This means that add_info_string() also gets converted to std::string
204
void optimizer::QuickRorUnionSelect::add_info_string(string *str)
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
205
{
206
  bool first= true;
2170.4.5 by Stewart Smith
convert explain_plan extra String to std::string. This means that add_info_string() also gets converted to std::string
207
  str->append("union(");
2318.6.64 by Olaf van der Spek
Refactor
208
  BOOST_FOREACH(QuickSelectInterface* it, quick_selects)
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
209
  {
2318.6.64 by Olaf van der Spek
Refactor
210
    if (first)
211
      first= false;
212
    else
2170.4.5 by Stewart Smith
convert explain_plan extra String to std::string. This means that add_info_string() also gets converted to std::string
213
      str->append(",");
2318.6.64 by Olaf van der Spek
Refactor
214
    it->add_info_string(str);
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
215
  }
2170.4.5 by Stewart Smith
convert explain_plan extra String to std::string. This means that add_info_string() also gets converted to std::string
216
  str->append(")");
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
217
}
218
219
2318.6.64 by Olaf van der Spek
Refactor
220
void optimizer::QuickRorUnionSelect::add_keys_and_lengths(string *key_names, string *used_lengths)
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
221
{
222
  bool first= true;
2318.6.64 by Olaf van der Spek
Refactor
223
  BOOST_FOREACH(QuickSelectInterface* it, quick_selects)
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
224
  {
225
    if (first)
226
      first= false;
227
    else
228
    {
2170.4.4 by Stewart Smith
tmp3 String to std::string in explain_plan (and used_lengths parameter to add_keys_and_lengths
229
      used_lengths->append(",");
2170.4.3 by Stewart Smith
convert tmp2 in explain_plan to std::string instead of char[] buf and String, consequently also convert add_keys_and_length to deal with std::string for key_names parameter
230
      key_names->append(",");
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
231
    }
2318.6.64 by Olaf van der Spek
Refactor
232
    it->add_keys_and_lengths(key_names, used_lengths);
1237.13.13 by Padraig O'Sullivan
Moved the QUICK_ROR_UNION_SELECT class into its own header and implementation files.
233
  }
234
}
235
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
236
} /* namespace drizzled */