~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
/* -*- 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
 */

#ifndef DRIZZLED_OPTIMIZER_SEL_IMERGE_H
#define DRIZZLED_OPTIMIZER_SEL_IMERGE_H

#include <drizzled/memory/sql_alloc.h>

namespace drizzled
{

namespace optimizer
{

class RangeParameter;
class SEL_TREE;
class SEL_ARG;

/*
  SEL_IMERGE is a list of possible ways to do index merge, i.e. it is
  a condition in the following form:
   (t_1||t_2||...||t_N) && (next)

  where all t_i are optimizer::SEL_TREEs, next is another SEL_IMERGE and no pair
  (t_i,t_j) contains SEL_ARGS for the same index.

  optimizer::SEL_TREE contained in SEL_IMERGE always has merges=NULL.

  This class relies on memory manager to do the cleanup.
*/
class SEL_IMERGE : public memory::SqlAlloc
{
  enum { PREALLOCED_TREES= 10};
public:
  SEL_TREE *trees_prealloced[PREALLOCED_TREES];
  SEL_TREE **trees;             /* trees used to do index_merge   */
  SEL_TREE **trees_next;        /* last of these trees            */
  SEL_TREE **trees_end;         /* end of allocated space         */

  SEL_ARG  ***best_keys;        /* best keys to read in optimizer::SEL_TREEs */

  SEL_IMERGE();

  /*
     Add optimizer::SEL_TREE to this index_merge without any checks,

     NOTES
     This function implements the following:
     (x_1||...||x_N) || t = (x_1||...||x_N||t), where x_i, t are optimizer::SEL_TREEs

     RETURN
     0 - OK
     -1 - Out of memory.
   */
  int or_sel_tree(RangeParameter *param, SEL_TREE *tree);

  /*
     Perform OR operation on this SEL_IMERGE and supplied optimizer::SEL_TREE new_tree,
     combining new_tree with one of the trees in this SEL_IMERGE if they both
     have SEL_ARGs for the same key.

     SYNOPSIS
     or_sel_tree_with_checks()
     param    Parameter from SqlSelect::test_quick_select
     new_tree optimizer::SEL_TREE with type KEY or KEY_SMALLER.

     NOTES
     This does the following:
     (t_1||...||t_k)||new_tree =
     either
     = (t_1||...||t_k||new_tree)
     or
     = (t_1||....||(t_j|| new_tree)||...||t_k),

     where t_i, y are optimizer::SEL_TREEs.
     new_tree is combined with the first t_j it has a SEL_ARG on common
     key with. As a consequence of this, choice of keys to do index_merge
     read may depend on the order of conditions in WHERE part of the query.

     RETURN
     0  OK
     1  One of the trees was combined with new_tree to optimizer::SEL_TREE::ALWAYS,
     and (*this) should be discarded.
     -1  An error occurred.
   */
  int or_sel_tree_with_checks(optimizer::RangeParameter *param, optimizer::SEL_TREE *new_tree);

  /*
     Perform OR operation on this index_merge and supplied index_merge list.

     RETURN
     0 - OK
     1 - One of conditions in result is always true and this SEL_IMERGE
     should be discarded.
     -1 - An error occurred
   */
  int or_sel_imerge_with_checks(optimizer::RangeParameter *param, SEL_IMERGE* imerge);

};


} /* namespace optimizer */

} /* namespace drizzled */

#endif /* DRIZZLED_OPTIMIZER_SEL_IMERGE_H */