~drizzle-trunk/drizzle/development

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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 *
 *  Copyright (C) 2008-2009 Sun Microsystems, Inc.
 *
 *  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
 */

#include <config.h>
#include <drizzled/session.h>
#include <drizzled/records.h>
#include <drizzled/util/functors.h>
#include <drizzled/optimizer/quick_range_select.h>
#include <drizzled/optimizer/quick_index_merge_select.h>
#include <drizzled/internal/m_string.h>
#include <drizzled/unique.h>
#include <drizzled/key.h>
#include <drizzled/table.h>
#include <drizzled/system_variables.h>

#include <vector>

using namespace std;

namespace drizzled {

static int refpos_order_cmp(void *arg, const void *a, const void *b)
{
  Cursor *cursor= (Cursor*)arg;
  return cursor->cmp_ref((const unsigned char *) a, (const unsigned char *) b);
}

optimizer::QuickIndexMergeSelect::QuickIndexMergeSelect(Session *session_param,
                                                        Table *table)
  :
    pk_quick_select(NULL),
    session(session_param)
{
  index= MAX_KEY;
  head= table;
  memset(&read_record, 0, sizeof(read_record));
  alloc.init(session->variables.range_alloc_block_size);
}

int optimizer::QuickIndexMergeSelect::init()
{
  return 0;
}

int optimizer::QuickIndexMergeSelect::reset()
{
  return (read_keys_and_merge());
}

void optimizer::QuickIndexMergeSelect::push_quick_back(optimizer::QuickRangeSelect *quick_sel_range)
{
  /*
    Save quick_select that does scan on clustered primary key as it will be
    processed separately.
  */
  if (head->cursor->primary_key_is_clustered() &&
      quick_sel_range->index == head->getShare()->getPrimaryKey())
  {
    pk_quick_select= quick_sel_range;
  }
  else
  {
    quick_selects.push_back(quick_sel_range);
  }
}

optimizer::QuickIndexMergeSelect::~QuickIndexMergeSelect()
{
  BOOST_FOREACH(QuickRangeSelect* it, quick_selects)
    it->cursor= NULL;
  for_each(quick_selects.begin(), quick_selects.end(), DeletePtr());
  quick_selects.clear();
  delete pk_quick_select;
  alloc.free_root(MYF(0));
}


int optimizer::QuickIndexMergeSelect::read_keys_and_merge()
{
  vector<optimizer::QuickRangeSelect *>::iterator it= quick_selects.begin();
  optimizer::QuickRangeSelect *cur_quick= NULL;
  int result;
  Unique *unique= NULL;
  Cursor *cursor= head->cursor;

  cursor->extra(HA_EXTRA_KEYREAD);
  head->prepare_for_position();

  cur_quick= *it;
  ++it;
  assert(cur_quick != 0);

  /*
    We reuse the same instance of Cursor so we need to call both init and
    reset here.
  */
  if (cur_quick->init() || cur_quick->reset())
    return 0;

  unique= new Unique(refpos_order_cmp,
                     (void *) cursor,
                     cursor->ref_length,
                     session->variables.sortbuff_size);
  if (! unique)
    return 0;
  for (;;)
  {
    while ((result= cur_quick->get_next()) == HA_ERR_END_OF_FILE)
    {
      cur_quick->range_end();
      if (it == quick_selects.end())
      {
        break;
      }
      cur_quick= *it;
      ++it;
      if (! cur_quick)
        break;

      if (cur_quick->cursor->inited != Cursor::NONE)
        cur_quick->cursor->endIndexScan();
      if (cur_quick->init() || cur_quick->reset())
        return 0;
    }

    if (result)
    {
      if (result != HA_ERR_END_OF_FILE)
      {
        cur_quick->range_end();
        return result;
      }
      break;
    }

    if (session->getKilled())
      return 0;

    /* skip row if it will be retrieved by clustered PK scan */
    if (pk_quick_select && pk_quick_select->row_in_ranges())
      continue;

    cur_quick->cursor->position(cur_quick->record);
    result= unique->unique_add((char*) cur_quick->cursor->ref);
    if (result)
      return 0;

  }

  /* ok, all row ids are in Unique */
  result= unique->get(head);
  delete unique;
  doing_pk_scan= false;
  /* index_merge currently doesn't support "using index" at all */
  cursor->extra(HA_EXTRA_NO_KEYREAD);
  /* start table scan */
  if ((result= read_record.init_read_record(session, head, (optimizer::SqlSelect*) 0, 1, 1)))
  {
    head->print_error(result, MYF(0));
    return 0;
  }
  return result;
}


int optimizer::QuickIndexMergeSelect::get_next()
{
  int result;

  if (doing_pk_scan)
    return(pk_quick_select->get_next());

  if ((result= read_record.read_record(&read_record)) == -1)
  {
    result= HA_ERR_END_OF_FILE;
    read_record.end_read_record();
    /* All rows from Unique have been retrieved, do a clustered PK scan */
    if (pk_quick_select)
    {
      doing_pk_scan= true;
      if ((result= pk_quick_select->init()) ||
          (result= pk_quick_select->reset()))
        return result;
      return(pk_quick_select->get_next());
    }
  }

  return result;
}

bool optimizer::QuickIndexMergeSelect::is_keys_used(const boost::dynamic_bitset<>& fields)
{
  BOOST_FOREACH(QuickRangeSelect* it, quick_selects)
  {
    if (is_key_used(head, it->index, fields))
      return 1;
  }
  return 0;
}


void optimizer::QuickIndexMergeSelect::add_info_string(string *str)
{
  bool first= true;
  str->append("sort_union(");
  BOOST_FOREACH(QuickRangeSelect* it, quick_selects)
  {
    if (! first)
      str->append(",");
    else
      first= false;
    it->add_info_string(str);
  }
  if (pk_quick_select)
  {
    str->append(",");
    pk_quick_select->add_info_string(str);
  }
  str->append(")");
}


void optimizer::QuickIndexMergeSelect::add_keys_and_lengths(string *key_names,
                                                            string *used_lengths)
{
  char buf[64];
  uint32_t length= 0;
  bool first= true;

  BOOST_FOREACH(QuickRangeSelect* it, quick_selects)
  {
    if (first)
      first= false;
    else
    {
      key_names->append(",");
      used_lengths->append(",");
    }

    KeyInfo *key_info= head->key_info + it->index;
    key_names->append(key_info->name);
    length= internal::int64_t2str(it->max_used_key_length, buf, 10) - buf;
    used_lengths->append(buf, length);
  }
  if (pk_quick_select)
  {
    KeyInfo *key_info= head->key_info + pk_quick_select->index;
    key_names->append(",");
    key_names->append(key_info->name);
    length= internal::int64_t2str(pk_quick_select->max_used_key_length, buf, 10) - buf;
    used_lengths->append(",");
    used_lengths->append(buf, length);
  }
}


} /* namespace drizzled */