Drizzled Public API Documentation

table_read_plan.h
00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2008-2009 Sun Microsystems, Inc.
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; version 2 of the License.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  */
00019 
00020 #pragma once
00021 
00022 #include <drizzled/util/functors.h>
00023 #include <algorithm>
00024 
00025 namespace drizzled
00026 {
00027 
00028 namespace optimizer
00029 {
00030 
00031 class Parameter;
00032 class SEL_ARG;
00033 class SEL_TREE;
00034 class RorScanInfo;
00035 
00036 /*
00037   Table rows retrieval plan. Range optimizer creates QuickSelectInterface-derived
00038   objects from table read plans.
00039 */
00040 class TableReadPlan
00041 {
00042 public:
00043   /*
00044     Plan read cost, with or without cost of full row retrieval, depending
00045     on plan creation parameters.
00046   */
00047   double read_cost;
00048   ha_rows records; /* estimate of #rows to be examined */
00049 
00050   /*
00051     If true, the scan returns rows in rowid order. This is used only for
00052     scans that can be both ROR and non-ROR.
00053   */
00054   bool is_ror;
00055 
00056   /*
00057     Create quick select for this plan.
00058     SYNOPSIS
00059      make_quick()
00060        param               Parameter from test_quick_select
00061        retrieve_full_rows  If true, created quick select will do full record
00062                            retrieval.
00063        parent_alloc        Memory pool to use, if any.
00064 
00065     NOTES
00066       retrieve_full_rows is ignored by some implementations.
00067 
00068     RETURN
00069       created quick select
00070       NULL on any error.
00071   */
00072   virtual QuickSelectInterface *make_quick(Parameter *param,
00073                                            bool retrieve_full_rows,
00074                                            memory::Root *parent_alloc= NULL) = 0;
00075 
00076   /* Table read plans are allocated on memory::Root and are never deleted */
00077   static void *operator new(size_t size, memory::Root *mem_root)
00078   { 
00079     return (void*) mem_root->alloc_root((uint32_t) size); 
00080   }
00081 
00082   static void operator delete(void *, size_t)
00083   { }
00084 
00085   static void operator delete(void *, memory::Root *)
00086     { /* Never called */ }
00087 
00088   virtual ~TableReadPlan() {} /* Remove gcc warning */
00089 
00090 };
00091 
00092 
00093 /*
00094   Plan for a QuickRangeSelect scan.
00095   RangeReadPlan::make_quick ignores retrieve_full_rows parameter because
00096   QuickRangeSelect doesn't distinguish between 'index only' scans and full
00097   record retrieval scans.
00098 */
00099 class RangeReadPlan : public TableReadPlan
00100 {
00101 
00102 public:
00103 
00104   SEL_ARG *key; /* set of intervals to be used in "range" method retrieval */
00105   uint32_t     key_idx; /* key number in Parameter::key */
00106   uint32_t     mrr_flags;
00107   uint32_t     mrr_buf_size;
00108 
00109   RangeReadPlan(SEL_ARG *key_arg, uint32_t idx_arg, uint32_t mrr_flags_arg)
00110     :
00111       key(key_arg),
00112       key_idx(idx_arg),
00113       mrr_flags(mrr_flags_arg)
00114   {}
00115   virtual ~RangeReadPlan() {}                     /* Remove gcc warning */
00116 
00117   QuickSelectInterface *make_quick(Parameter *param, bool, memory::Root *parent_alloc);
00118 
00119 };
00120 
00121 
00122 /* Plan for QuickRorIntersectSelect scan. */
00123 class RorIntersectReadPlan : public TableReadPlan
00124 {
00125 public:
00126 
00127   RorIntersectReadPlan() {}                      /* Remove gcc warning */
00128   virtual ~RorIntersectReadPlan() {}             /* Remove gcc warning */
00129 
00130   QuickSelectInterface *make_quick(Parameter *param,
00131                                    bool retrieve_full_rows,
00132                                    memory::Root *parent_alloc);
00133 
00134   /* Array of pointers to ROR range scans used in this intersection */
00135   RorScanInfo **first_scan;
00136   RorScanInfo **last_scan; /* End of the above array */
00137   RorScanInfo *cpk_scan;  /* Clustered PK scan, if there is one */
00138 
00139   bool is_covering; /* true if no row retrieval phase is necessary */
00140   double index_scan_costs; /* SUM(cost(index_scan)) */
00141 
00142 };
00143 
00144 
00145 /*
00146   Plan for QuickRorUnionSelect scan.
00147   QuickRorUnionSelect always retrieves full rows, so retrieve_full_rows
00148   is ignored by make_quick.
00149 */
00150 
00151 class RorUnionReadPlan : public TableReadPlan
00152 {
00153 public:
00154   RorUnionReadPlan() {}                          /* Remove gcc warning */
00155   virtual ~RorUnionReadPlan() {}                 /* Remove gcc warning */
00156   QuickSelectInterface *make_quick(Parameter *param,
00157                                    bool retrieve_full_rows,
00158                                    memory::Root *parent_alloc);
00159   TableReadPlan **first_ror; /* array of ptrs to plans for merged scans */
00160   TableReadPlan **last_ror;  /* end of the above array */
00161 };
00162 
00163 
00164 /*
00165   Plan for QuickIndexMergeSelect scan.
00166   QuickRorIntersectSelect always retrieves full rows, so retrieve_full_rows
00167   is ignored by make_quick.
00168 */
00169 
00170 class IndexMergeReadPlan : public TableReadPlan
00171 {
00172 public:
00173   IndexMergeReadPlan() {}                        /* Remove gcc warning */
00174   virtual ~IndexMergeReadPlan() {}               /* Remove gcc warning */
00175   QuickSelectInterface *make_quick(Parameter *param,
00176                                    bool retrieve_full_rows,
00177                                    memory::Root *parent_alloc);
00178   RangeReadPlan **range_scans; /* array of ptrs to plans of merged scans */
00179   RangeReadPlan **range_scans_end; /* end of the array */
00180 };
00181 
00182 
00183 /*
00184   Plan for a QuickGroupMinMaxSelect scan.
00185 */
00186 
00187 class GroupMinMaxReadPlan : public TableReadPlan
00188 {
00189 private:
00190   bool have_min;
00191   bool have_max;
00192   KeyPartInfo *min_max_arg_part;
00193   uint32_t group_prefix_len;
00194   uint32_t used_key_parts;
00195   uint32_t group_key_parts;
00196   KeyInfo *index_info;
00197   uint32_t index;
00198   uint32_t key_infix_len;
00199   unsigned char key_infix[MAX_KEY_LENGTH];
00200   SEL_TREE *range_tree; /* Represents all range predicates in the query. */
00201   SEL_ARG *index_tree; /* The SEL_ARG sub-tree corresponding to index_info. */
00202   uint32_t param_idx; /* Index of used key in param->key. */
00203   /* Number of records selected by the ranges in index_tree. */
00204 public:
00205   ha_rows quick_prefix_records;
00206 
00207 public:
00208   GroupMinMaxReadPlan(bool have_min_arg, 
00209                       bool have_max_arg,
00210                       KeyPartInfo *min_max_arg_part_arg,
00211                       uint32_t group_prefix_len_arg, 
00212                       uint32_t used_key_parts_arg,
00213                       uint32_t group_key_parts_arg, 
00214                       KeyInfo *index_info_arg,
00215                       uint32_t index_arg, 
00216                       uint32_t key_infix_len_arg,
00217                       unsigned char *key_infix_arg,
00218                       SEL_TREE *tree_arg, 
00219                       SEL_ARG *index_tree_arg,
00220                       uint32_t param_idx_arg, 
00221                       ha_rows quick_prefix_records_arg)
00222     :
00223       have_min(have_min_arg),
00224       have_max(have_max_arg),
00225       min_max_arg_part(min_max_arg_part_arg),
00226       group_prefix_len(group_prefix_len_arg),
00227       used_key_parts(used_key_parts_arg),
00228       group_key_parts(group_key_parts_arg),
00229       index_info(index_info_arg),
00230       index(index_arg),
00231       key_infix_len(key_infix_len_arg),
00232       range_tree(tree_arg),
00233       index_tree(index_tree_arg),
00234       param_idx(param_idx_arg),
00235       quick_prefix_records(quick_prefix_records_arg)
00236     {
00237       if (key_infix_len)
00238         memcpy(this->key_infix, key_infix_arg, key_infix_len);
00239     }
00240   virtual ~GroupMinMaxReadPlan() {}             /* Remove gcc warning */
00241 
00242   QuickSelectInterface *make_quick(Parameter *param,
00243                                    bool retrieve_full_rows,
00244                                    memory::Root *parent_alloc);
00245 };
00246 
00247 
00248 } /* namespace optimizer */
00249 
00250 } /* namespace drizzled */
00251