~drizzle-trunk/drizzle/development

1237.13.5 by Padraig O'Sullivan
Split some classes from the range optimizer out in to their 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.5 by Padraig O'Sullivan
Split some classes from the range optimizer out in to their 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
1237.13.8 by Padraig O'Sullivan
Updated an include guard thanks to a nice catch during code review from Jay. Thanks Jay!
20
#ifndef DRIZZLED_OPTIMIZER_QUICK_INDEX_MERGE_SELECT_H
21
#define DRIZZLED_OPTIMIZER_QUICK_INDEX_MERGE_SELECT_H
1237.13.5 by Padraig O'Sullivan
Split some classes from the range optimizer out in to their own header and implementation files.
22
23
#include "drizzled/optimizer/range.h"
24
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.
25
#include <boost/dynamic_bitset.hpp>
1237.13.17 by Padraig O'Sullivan
Replaced List with std::vector in the QuickIndexMergeSelect class.
26
#include <vector>
27
1237.13.5 by Padraig O'Sullivan
Split some classes from the range optimizer out in to their own header and implementation files.
28
namespace drizzled
29
{
30
31
namespace optimizer
32
{
33
34
/**
35
  @class QuickIndexMergeSelect - index_merge access method quick select.
36
37
    QuickIndexMergeSelect uses
38
     * QuickRangeSelects to get rows
39
     * Unique class to remove duplicate rows
40
41
  INDEX MERGE OPTIMIZER
42
    Current implementation doesn't detect all cases where index_merge could
43
    be used, in particular:
44
     * index_merge will never be used if range scan is possible (even if
45
       range scan is more expensive)
46
47
     * index_merge+'using index' is not supported (this the consequence of
48
       the above restriction)
49
50
     * If WHERE part contains complex nested AND and OR conditions, some ways
51
       to retrieve rows using index_merge will not be considered. The choice
52
       of read plan may depend on the order of conjuncts/disjuncts in WHERE
53
       part of the query, see comments near imerge_list_or_list and
54
       SEL_IMERGE::or_sel_tree_with_checks functions for details.
55
56
     * There is no "index_merge_ref" method (but index_merge on non-first
57
       table in join is possible with 'range checked for each record').
58
59
    See comments around SEL_IMERGE class and test_quick_select for more
60
    details.
61
62
  ROW RETRIEVAL ALGORITHM
63
64
    index_merge uses Unique class for duplicates removal.  index_merge takes
65
    advantage of Clustered Primary Key (CPK) if the table has one.
66
    The index_merge algorithm consists of two phases:
67
68
    Phase 1 (implemented in QuickIndexMergeSelect::prepare_unique):
69
    prepare()
70
    {
71
      activate 'index only';
72
      while(retrieve next row for non-CPK scan)
73
      {
74
        if (there is a CPK scan and row will be retrieved by it)
75
          skip this row;
76
        else
77
          put its rowid into Unique;
78
      }
79
      deactivate 'index only';
80
    }
81
82
    Phase 2 (implemented as sequence of QuickIndexMergeSelect::get_next
83
    calls):
84
85
    fetch()
86
    {
87
      retrieve all rows from row pointers stored in Unique;
88
      free Unique;
89
      retrieve all rows for CPK scan;
90
    }
91
*/
92
class QuickIndexMergeSelect : public QuickSelectInterface
93
{
94
public:
95
96
  QuickIndexMergeSelect(Session *session, Table *table);
97
98
  ~QuickIndexMergeSelect();
99
100
  int init();
101
  int reset(void);
102
103
  /**
104
   * Get next row for index_merge.
105
   * NOTES
106
   * The rows are read from
107
   * 1. rowids stored in Unique.
108
   * 2. QuickRangeSelect with clustered primary key (if any).
109
   * The sets of rows retrieved in 1) and 2) are guaranteed to be disjoint.
110
   */
111
  int get_next();
112
1237.13.11 by Padraig O'Sullivan
Split the QUICK_ROR_INTERSECT_SELECT class out into its own header and implementation files.
113
  bool reverse_sorted() const
114
  {
115
    return false;
116
  }
117
118
  bool unique_key_range() const
119
  {
120
    return false;
121
  }
122
123
  int get_type() const
1237.13.5 by Padraig O'Sullivan
Split some classes from the range optimizer out in to their own header and implementation files.
124
  {
125
    return QS_TYPE_INDEX_MERGE;
126
  }
127
128
  void add_keys_and_lengths(String *key_names, String *used_lengths);
129
  void add_info_string(String *str);
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.
130
  bool is_keys_used(const boost::dynamic_bitset<>& fields);
1237.13.5 by Padraig O'Sullivan
Split some classes from the range optimizer out in to their own header and implementation files.
131
132
  bool push_quick_back(QuickRangeSelect *quick_sel_range);
133
134
  /* range quick selects this index_merge read consists of */
1237.13.17 by Padraig O'Sullivan
Replaced List with std::vector in the QuickIndexMergeSelect class.
135
  std::vector<QuickRangeSelect *> quick_selects;
1237.13.5 by Padraig O'Sullivan
Split some classes from the range optimizer out in to their own header and implementation files.
136
137
  /* quick select that uses clustered primary key (NULL if none) */
1237.13.15 by Padraig O'Sullivan
Used std::vector instead of List in the QuickRorUnionSelect class.
138
  QuickRangeSelect *pk_quick_select;
1237.13.5 by Padraig O'Sullivan
Split some classes from the range optimizer out in to their own header and implementation files.
139
140
  /* true if this select is currently doing a clustered PK scan */
141
  bool  doing_pk_scan;
142
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
143
  memory::Root alloc;
1237.13.5 by Padraig O'Sullivan
Split some classes from the range optimizer out in to their own header and implementation files.
144
  Session *session;
145
146
  /**
147
   * Perform key scans for all used indexes (except CPK), get rowids and merge
148
   * them into an ordered non-recurrent sequence of rowids.
149
   *
150
   * The merge/duplicate removal is performed using Unique class. We put all
151
   * rowids into Unique, get the sorted sequence and destroy the Unique.
152
   *
153
   * If table has a clustered primary key that covers all rows (true for bdb
154
   * and innodb currently) and one of the index_merge scans is a scan on PK,
155
   * then rows that will be retrieved by PK scan are not put into Unique and
156
   * primary key scan is not performed here, it is performed later separately.
157
   *
158
   * RETURN
159
   * @retval 0     OK
160
   * @retval other error
161
   */
162
  int read_keys_and_merge();
163
164
  /* used to get rows collected in Unique */
1538 by Brian Aker
Code shuffle on ReadRecord
165
  ReadRecord read_record;
1237.13.5 by Padraig O'Sullivan
Split some classes from the range optimizer out in to their own header and implementation files.
166
};
167
168
} /* namespace optimizer */
169
170
} /* namespace drizzled */
171
1237.13.8 by Padraig O'Sullivan
Updated an include guard thanks to a nice catch during code review from Jay. Thanks Jay!
172
#endif /* DRIZZLED_OPTIMIZER_QUICK_INDEX_MERGE_SELECT_H */