390.1.2
by Monty Taylor
Fixed copyright headers in drizzled/ |
1 |
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
|
2 |
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
|
|
3 |
*
|
|
4 |
* Copyright (C) 2008 Sun Microsystems
|
|
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 |
*/
|
|
1
by brian
clean slate |
19 |
|
20 |
||
21 |
/* classes for sum functions */
|
|
22 |
||
23 |
||
212.5.15
by Monty Taylor
Moved my_tree. |
24 |
#include <mysys/my_tree.h> |
1
by brian
clean slate |
25 |
|
26 |
/*
|
|
27 |
Class Item_sum is the base class used for special expressions that SQL calls
|
|
28 |
'set functions'. These expressions are formed with the help of aggregate
|
|
29 |
functions such as SUM, MAX, GROUP_CONCAT etc.
|
|
30 |
||
31 |
GENERAL NOTES
|
|
32 |
||
33 |
A set function cannot be used in certain positions where expressions are
|
|
34 |
accepted. There are some quite explicable restrictions for the usage of
|
|
35 |
set functions.
|
|
36 |
||
37 |
In the query:
|
|
38 |
SELECT AVG(b) FROM t1 WHERE SUM(b) > 20 GROUP by a
|
|
39 |
the usage of the set function AVG(b) is legal, while the usage of SUM(b)
|
|
40 |
is illegal. A WHERE condition must contain expressions that can be
|
|
41 |
evaluated for each row of the table. Yet the expression SUM(b) can be
|
|
42 |
evaluated only for each group of rows with the same value of column a.
|
|
43 |
In the query:
|
|
44 |
SELECT AVG(b) FROM t1 WHERE c > 30 GROUP BY a HAVING SUM(b) > 20
|
|
45 |
both set function expressions AVG(b) and SUM(b) are legal.
|
|
46 |
||
47 |
We can say that in a query without nested selects an occurrence of a
|
|
48 |
set function in an expression of the SELECT list or/and in the HAVING
|
|
49 |
clause is legal, while in the WHERE clause it's illegal.
|
|
50 |
||
51 |
The general rule to detect whether a set function is legal in a query with
|
|
52 |
nested subqueries is much more complicated.
|
|
53 |
||
54 |
Consider the the following query:
|
|
55 |
SELECT t1.a FROM t1 GROUP BY t1.a
|
|
56 |
HAVING t1.a > ALL (SELECT t2.c FROM t2 WHERE SUM(t1.b) < t2.c).
|
|
57 |
The set function SUM(b) is used here in the WHERE clause of the subquery.
|
|
58 |
Nevertheless it is legal since it is under the HAVING clause of the query
|
|
59 |
to which this function relates. The expression SUM(t1.b) is evaluated
|
|
60 |
for each group defined in the main query, not for groups of the subquery.
|
|
61 |
||
62 |
The problem of finding the query where to aggregate a particular
|
|
63 |
set function is not so simple as it seems to be.
|
|
64 |
||
65 |
In the query:
|
|
66 |
SELECT t1.a FROM t1 GROUP BY t1.a
|
|
67 |
HAVING t1.a > ALL(SELECT t2.c FROM t2 GROUP BY t2.c
|
|
68 |
HAVING SUM(t1.a) < t2.c)
|
|
69 |
the set function can be evaluated for both outer and inner selects.
|
|
70 |
If we evaluate SUM(t1.a) for the outer query then we get the value of t1.a
|
|
71 |
multiplied by the cardinality of a group in table t1. In this case
|
|
72 |
in each correlated subquery SUM(t1.a) is used as a constant. But we also
|
|
73 |
can evaluate SUM(t1.a) for the inner query. In this case t1.a will be a
|
|
74 |
constant for each correlated subquery and summation is performed
|
|
75 |
for each group of table t2.
|
|
76 |
(Here it makes sense to remind that the query
|
|
77 |
SELECT c FROM t GROUP BY a HAVING SUM(1) < a
|
|
78 |
is quite legal in our SQL).
|
|
79 |
||
80 |
So depending on what query we assign the set function to we
|
|
81 |
can get different result sets.
|
|
82 |
||
83 |
The general rule to detect the query where a set function is to be
|
|
84 |
evaluated can be formulated as follows.
|
|
85 |
Consider a set function S(E) where E is an expression with occurrences
|
|
86 |
of column references C1, ..., CN. Resolve these column references against
|
|
87 |
subqueries that contain the set function S(E). Let Q be the innermost
|
|
88 |
subquery of those subqueries. (It should be noted here that S(E)
|
|
89 |
in no way can be evaluated in the subquery embedding the subquery Q,
|
|
90 |
otherwise S(E) would refer to at least one unbound column reference)
|
|
91 |
If S(E) is used in a construct of Q where set functions are allowed then
|
|
92 |
we evaluate S(E) in Q.
|
|
93 |
Otherwise we look for a innermost subquery containing S(E) of those where
|
|
94 |
usage of S(E) is allowed.
|
|
95 |
||
96 |
Let's demonstrate how this rule is applied to the following queries.
|
|
97 |
||
98 |
1. SELECT t1.a FROM t1 GROUP BY t1.a
|
|
99 |
HAVING t1.a > ALL(SELECT t2.b FROM t2 GROUP BY t2.b
|
|
100 |
HAVING t2.b > ALL(SELECT t3.c FROM t3 GROUP BY t3.c
|
|
101 |
HAVING SUM(t1.a+t2.b) < t3.c))
|
|
102 |
For this query the set function SUM(t1.a+t2.b) depends on t1.a and t2.b
|
|
103 |
with t1.a defined in the outermost query, and t2.b defined for its
|
|
104 |
subquery. The set function is in the HAVING clause of the subquery and can
|
|
105 |
be evaluated in this subquery.
|
|
106 |
||
107 |
2. SELECT t1.a FROM t1 GROUP BY t1.a
|
|
108 |
HAVING t1.a > ALL(SELECT t2.b FROM t2
|
|
109 |
WHERE t2.b > ALL (SELECT t3.c FROM t3 GROUP BY t3.c
|
|
110 |
HAVING SUM(t1.a+t2.b) < t3.c))
|
|
111 |
Here the set function SUM(t1.a+t2.b)is in the WHERE clause of the second
|
|
112 |
subquery - the most upper subquery where t1.a and t2.b are defined.
|
|
113 |
If we evaluate the function in this subquery we violate the context rules.
|
|
114 |
So we evaluate the function in the third subquery (over table t3) where it
|
|
115 |
is used under the HAVING clause.
|
|
116 |
||
117 |
3. SELECT t1.a FROM t1 GROUP BY t1.a
|
|
118 |
HAVING t1.a > ALL(SELECT t2.b FROM t2
|
|
119 |
WHERE t2.b > ALL (SELECT t3.c FROM t3
|
|
120 |
WHERE SUM(t1.a+t2.b) < t3.c))
|
|
121 |
In this query evaluation of SUM(t1.a+t2.b) is not legal neither in the second
|
|
122 |
nor in the third subqueries. So this query is invalid.
|
|
123 |
||
124 |
Mostly set functions cannot be nested. In the query
|
|
125 |
SELECT t1.a from t1 GROUP BY t1.a HAVING AVG(SUM(t1.b)) > 20
|
|
126 |
the expression SUM(b) is not acceptable, though it is under a HAVING clause.
|
|
127 |
Yet it is acceptable in the query:
|
|
128 |
SELECT t.1 FROM t1 GROUP BY t1.a HAVING SUM(t1.b) > 20.
|
|
129 |
||
130 |
An argument of a set function does not have to be a reference to a table
|
|
131 |
column as we saw it in examples above. This can be a more complex expression
|
|
132 |
SELECT t1.a FROM t1 GROUP BY t1.a HAVING SUM(t1.b+1) > 20.
|
|
133 |
The expression SUM(t1.b+1) has a very clear semantics in this context:
|
|
134 |
we sum up the values of t1.b+1 where t1.b varies for all values within a
|
|
135 |
group of rows that contain the same t1.a value.
|
|
136 |
||
137 |
A set function for an outer query yields a constant within a subquery. So
|
|
138 |
the semantics of the query
|
|
139 |
SELECT t1.a FROM t1 GROUP BY t1.a
|
|
140 |
HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
|
|
141 |
HAVING AVG(t2.c+SUM(t1.b)) > 20)
|
|
142 |
is still clear. For a group of the rows with the same t1.a values we
|
|
143 |
calculate the value of SUM(t1.b). This value 's' is substituted in the
|
|
144 |
the subquery:
|
|
145 |
SELECT t2.c FROM t2 GROUP BY t2.c HAVING AVG(t2.c+s)
|
|
146 |
than returns some result set.
|
|
147 |
||
148 |
By the same reason the following query with a subquery
|
|
149 |
SELECT t1.a FROM t1 GROUP BY t1.a
|
|
150 |
HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
|
|
151 |
HAVING AVG(SUM(t1.b)) > 20)
|
|
152 |
is also acceptable.
|
|
153 |
||
154 |
IMPLEMENTATION NOTES
|
|
155 |
||
156 |
Three methods were added to the class to check the constraints specified
|
|
157 |
in the previous section. These methods utilize several new members.
|
|
158 |
||
159 |
The field 'nest_level' contains the number of the level for the subquery
|
|
160 |
containing the set function. The main SELECT is of level 0, its subqueries
|
|
161 |
are of levels 1, the subqueries of the latter are of level 2 and so on.
|
|
162 |
||
163 |
The field 'aggr_level' is to contain the nest level of the subquery
|
|
164 |
where the set function is aggregated.
|
|
165 |
||
166 |
The field 'max_arg_level' is for the maximun of the nest levels of the
|
|
167 |
unbound column references occurred in the set function. A column reference
|
|
168 |
is unbound within a set function if it is not bound by any subquery
|
|
169 |
used as a subexpression in this function. A column reference is bound by
|
|
170 |
a subquery if it is a reference to the column by which the aggregation
|
|
171 |
of some set function that is used in the subquery is calculated.
|
|
172 |
For the set function used in the query
|
|
173 |
SELECT t1.a FROM t1 GROUP BY t1.a
|
|
174 |
HAVING t1.a > ALL(SELECT t2.b FROM t2 GROUP BY t2.b
|
|
175 |
HAVING t2.b > ALL(SELECT t3.c FROM t3 GROUP BY t3.c
|
|
176 |
HAVING SUM(t1.a+t2.b) < t3.c))
|
|
177 |
the value of max_arg_level is equal to 1 since t1.a is bound in the main
|
|
178 |
query, and t2.b is bound by the first subquery whose nest level is 1.
|
|
179 |
Obviously a set function cannot be aggregated in the subquery whose
|
|
180 |
nest level is less than max_arg_level. (Yet it can be aggregated in the
|
|
181 |
subqueries whose nest level is greater than max_arg_level.)
|
|
182 |
In the query
|
|
183 |
SELECT t.a FROM t1 HAVING AVG(t1.a+(SELECT MIN(t2.c) FROM t2))
|
|
184 |
the value of the max_arg_level for the AVG set function is 0 since
|
|
185 |
the reference t2.c is bound in the subquery.
|
|
186 |
||
187 |
The field 'max_sum_func_level' is to contain the maximum of the
|
|
188 |
nest levels of the set functions that are used as subexpressions of
|
|
189 |
the arguments of the given set function, but not aggregated in any
|
|
190 |
subquery within this set function. A nested set function s1 can be
|
|
191 |
used within set function s0 only if s1.max_sum_func_level <
|
|
192 |
s0.max_sum_func_level. Set function s1 is considered as nested
|
|
193 |
for set function s0 if s1 is not calculated in any subquery
|
|
194 |
within s0.
|
|
195 |
||
196 |
A set function that is used as a subexpression in an argument of another
|
|
197 |
set function refers to the latter via the field 'in_sum_func'.
|
|
198 |
||
199 |
The condition imposed on the usage of set functions are checked when
|
|
200 |
we traverse query subexpressions with the help of the recursive method
|
|
201 |
fix_fields. When we apply this method to an object of the class
|
|
202 |
Item_sum, first, on the descent, we call the method init_sum_func_check
|
|
203 |
that initialize members used at checking. Then, on the ascent, we
|
|
204 |
call the method check_sum_func that validates the set function usage
|
|
205 |
and reports an error if it is illegal.
|
|
206 |
The method register_sum_func serves to link the items for the set functions
|
|
207 |
that are aggregated in the embedding (sub)queries. Circular chains of such
|
|
208 |
functions are attached to the corresponding st_select_lex structures
|
|
209 |
through the field inner_sum_func_list.
|
|
210 |
||
211 |
Exploiting the fact that the members mentioned above are used in one
|
|
212 |
recursive function we could have allocated them on the thread stack.
|
|
213 |
Yet we don't do it now.
|
|
214 |
|
|
215 |
We assume that the nesting level of subquries does not exceed 127.
|
|
216 |
TODO: to catch queries where the limit is exceeded to make the
|
|
217 |
code clean here.
|
|
218 |
|
|
219 |
*/
|
|
220 |
||
221 |
class st_select_lex; |
|
222 |
||
223 |
class Item_sum :public Item_result_field |
|
224 |
{
|
|
225 |
public: |
|
226 |
enum Sumfunctype |
|
227 |
{ COUNT_FUNC, COUNT_DISTINCT_FUNC, SUM_FUNC, SUM_DISTINCT_FUNC, AVG_FUNC, |
|
228 |
AVG_DISTINCT_FUNC, MIN_FUNC, MAX_FUNC, STD_FUNC, |
|
139.1.8
by Stewart Smith
UDFs are now normal Item_func objects. Simplifies handling them a lot. |
229 |
VARIANCE_FUNC, SUM_BIT_FUNC, GROUP_CONCAT_FUNC |
1
by brian
clean slate |
230 |
};
|
231 |
||
232 |
Item **args, *tmp_args[2]; |
|
233 |
Item **ref_by; /* pointer to a ref to the object used to register it */ |
|
234 |
Item_sum *next; /* next in the circular chain of registered objects */ |
|
482
by Brian Aker
Remove uint. |
235 |
uint32_t arg_count; |
1
by brian
clean slate |
236 |
Item_sum *in_sum_func; /* embedding set function if any */ |
237 |
st_select_lex * aggr_sel; /* select where the function is aggregated */ |
|
206
by Brian Aker
Removed final uint dead types. |
238 |
int8_t nest_level; /* number of the nesting level of the set function */ |
239 |
int8_t aggr_level; /* nesting level of the aggregating subquery */ |
|
240 |
int8_t max_arg_level; /* max level of unbound column references */ |
|
241 |
int8_t max_sum_func_level;/* max level of aggregation for embedded functions */ |
|
1
by brian
clean slate |
242 |
bool quick_group; /* If incremental update of fields */ |
243 |
/*
|
|
244 |
This list is used by the check for mixing non aggregated fields and
|
|
245 |
sum functions in the ONLY_FULL_GROUP_BY_MODE. We save all outer fields
|
|
246 |
directly or indirectly used under this function it as it's unclear
|
|
247 |
at the moment of fixing outer field whether it's aggregated or not.
|
|
248 |
*/
|
|
249 |
List<Item_field> outer_fields; |
|
250 |
||
251 |
protected: |
|
252 |
table_map used_tables_cache; |
|
253 |
bool forced_const; |
|
254 |
||
255 |
public: |
|
256 |
||
257 |
void mark_as_sum_func(); |
|
51.1.25
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
258 |
Item_sum() :arg_count(0), quick_group(1), forced_const(false) |
1
by brian
clean slate |
259 |
{
|
260 |
mark_as_sum_func(); |
|
261 |
}
|
|
262 |
Item_sum(Item *a) :args(tmp_args), arg_count(1), quick_group(1), |
|
51.1.25
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
263 |
forced_const(false) |
1
by brian
clean slate |
264 |
{
|
265 |
args[0]=a; |
|
266 |
mark_as_sum_func(); |
|
267 |
}
|
|
268 |
Item_sum( Item *a, Item *b ) :args(tmp_args), arg_count(2), quick_group(1), |
|
51.1.25
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
269 |
forced_const(false) |
1
by brian
clean slate |
270 |
{
|
271 |
args[0]=a; args[1]=b; |
|
272 |
mark_as_sum_func(); |
|
273 |
}
|
|
274 |
Item_sum(List<Item> &list); |
|
275 |
//Copy constructor, need to perform subselects with temporary tables
|
|
520.1.22
by Brian Aker
Second pass of thd cleanup |
276 |
Item_sum(Session *session, Item_sum *item); |
1
by brian
clean slate |
277 |
enum Type type() const { return SUM_FUNC_ITEM; } |
278 |
virtual enum Sumfunctype sum_func () const=0; |
|
279 |
||
280 |
/*
|
|
281 |
This method is similar to add(), but it is called when the current
|
|
282 |
aggregation group changes. Thus it performs a combination of
|
|
283 |
clear() and add().
|
|
284 |
*/
|
|
285 |
inline bool reset() { clear(); return add(); }; |
|
286 |
||
287 |
/*
|
|
288 |
Prepare this item for evaluation of an aggregate value. This is
|
|
289 |
called by reset() when a group changes, or, for correlated
|
|
290 |
subqueries, between subquery executions. E.g. for COUNT(), this
|
|
291 |
method should set count= 0;
|
|
292 |
*/
|
|
293 |
virtual void clear()= 0; |
|
294 |
||
295 |
/*
|
|
296 |
This method is called for the next row in the same group. Its
|
|
297 |
purpose is to aggregate the new value to the previous values in
|
|
298 |
the group (i.e. since clear() was called last time). For example,
|
|
299 |
for COUNT(), do count++.
|
|
300 |
*/
|
|
301 |
virtual bool add()=0; |
|
302 |
||
303 |
/*
|
|
304 |
Called when new group is started and results are being saved in
|
|
305 |
a temporary table. Similar to reset(), but must also store value in
|
|
306 |
result_field. Like reset() it is supposed to reset start value to
|
|
307 |
default.
|
|
308 |
This set of methods (reult_field(), reset_field, update_field()) of
|
|
309 |
Item_sum is used only if quick_group is not null. Otherwise
|
|
310 |
copy_or_same() is used to obtain a copy of this item.
|
|
311 |
*/
|
|
312 |
virtual void reset_field()=0; |
|
313 |
/*
|
|
314 |
Called for each new value in the group, when temporary table is in use.
|
|
315 |
Similar to add(), but uses temporary table field to obtain current value,
|
|
316 |
Updated value is then saved in the field.
|
|
317 |
*/
|
|
318 |
virtual void update_field()=0; |
|
319 |
virtual bool keep_field_type(void) const { return 0; } |
|
320 |
virtual void fix_length_and_dec() { maybe_null=1; null_value=1; } |
|
321 |
/*
|
|
322 |
This method is used for debug purposes to print the name of an
|
|
323 |
item to the debug log. The second use of this method is as
|
|
324 |
a helper function of print(), where it is applicable.
|
|
325 |
To suit both goals it should return a meaningful,
|
|
326 |
distinguishable and sintactically correct string. This method
|
|
327 |
should not be used for runtime type identification, use enum
|
|
328 |
{Sum}Functype and Item_func::functype()/Item_sum::sum_func()
|
|
329 |
instead.
|
|
330 |
||
331 |
NOTE: for Items inherited from Item_sum, func_name() return part of
|
|
332 |
function name till first argument (including '(') to make difference in
|
|
333 |
names for functions with 'distinct' clause and without 'distinct' and
|
|
334 |
also to make printing of items inherited from Item_sum uniform.
|
|
335 |
*/
|
|
336 |
virtual const char *func_name() const= 0; |
|
337 |
virtual Item *result_item(Field *field) |
|
338 |
{ return new Item_field(field); } |
|
339 |
table_map used_tables() const { return used_tables_cache; } |
|
340 |
void update_used_tables (); |
|
341 |
void cleanup() |
|
342 |
{
|
|
343 |
Item::cleanup(); |
|
51.1.25
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
344 |
forced_const= false; |
1
by brian
clean slate |
345 |
}
|
346 |
bool is_null() { return null_value; } |
|
347 |
void make_const () |
|
348 |
{
|
|
349 |
used_tables_cache= 0; |
|
51.1.25
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
350 |
forced_const= true; |
1
by brian
clean slate |
351 |
}
|
352 |
virtual bool const_item() const { return forced_const; } |
|
353 |
void make_field(Send_field *field); |
|
354 |
virtual void print(String *str, enum_query_type query_type); |
|
355 |
void fix_num_length_and_dec(); |
|
356 |
||
357 |
/*
|
|
358 |
This function is called by the execution engine to assign 'NO ROWS
|
|
359 |
FOUND' value to an aggregate item, when the underlying result set
|
|
360 |
has no rows. Such value, in a general case, may be different from
|
|
361 |
the default value of the item after 'clear()': e.g. a numeric item
|
|
362 |
may be initialized to 0 by clear() and to NULL by
|
|
363 |
no_rows_in_result().
|
|
364 |
*/
|
|
365 |
void no_rows_in_result() { clear(); } |
|
366 |
||
520.1.22
by Brian Aker
Second pass of thd cleanup |
367 |
virtual bool setup(Session *session __attribute__((unused))) {return 0;} |
77.1.7
by Monty Taylor
Heap builds clean. |
368 |
virtual void make_unique(void) {} |
520.1.22
by Brian Aker
Second pass of thd cleanup |
369 |
Item *get_tmp_table_item(Session *session); |
327.1.5
by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h |
370 |
virtual Field *create_tmp_field(bool group, Table *table, |
482
by Brian Aker
Remove uint. |
371 |
uint32_t convert_blob_length); |
481
by Brian Aker
Remove all of uchar. |
372 |
bool walk(Item_processor processor, bool walk_subquery, unsigned char *argument); |
520.1.22
by Brian Aker
Second pass of thd cleanup |
373 |
bool init_sum_func_check(Session *session); |
374 |
bool check_sum_func(Session *session, Item **ref); |
|
375 |
bool register_sum_func(Session *session, Item **ref); |
|
1
by brian
clean slate |
376 |
st_select_lex *depended_from() |
377 |
{ return (nest_level == aggr_level ? 0 : aggr_sel); } |
|
378 |
};
|
|
379 |
||
380 |
||
381 |
class Item_sum_num :public Item_sum |
|
382 |
{
|
|
383 |
protected: |
|
384 |
/*
|
|
385 |
val_xxx() functions may be called several times during the execution of a
|
|
386 |
query. Derived classes that require extensive calculation in val_xxx()
|
|
387 |
maintain cache of aggregate value. This variable governs the validity of
|
|
388 |
that cache.
|
|
389 |
*/
|
|
390 |
bool is_evaluated; |
|
391 |
public: |
|
51.1.25
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
392 |
Item_sum_num() :Item_sum(),is_evaluated(false) {} |
1
by brian
clean slate |
393 |
Item_sum_num(Item *item_par) |
51.1.25
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
394 |
:Item_sum(item_par), is_evaluated(false) {} |
395 |
Item_sum_num(Item *a, Item* b) :Item_sum(a,b),is_evaluated(false) {} |
|
1
by brian
clean slate |
396 |
Item_sum_num(List<Item> &list) |
51.1.25
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
397 |
:Item_sum(list), is_evaluated(false) {} |
520.1.22
by Brian Aker
Second pass of thd cleanup |
398 |
Item_sum_num(Session *session, Item_sum_num *item) |
399 |
:Item_sum(session, item),is_evaluated(item->is_evaluated) {} |
|
520.1.21
by Brian Aker
THD -> Session rename |
400 |
bool fix_fields(Session *, Item **); |
152
by Brian Aker
longlong replacement |
401 |
int64_t val_int() |
1
by brian
clean slate |
402 |
{
|
51.1.25
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
403 |
assert(fixed == 1); |
152
by Brian Aker
longlong replacement |
404 |
return (int64_t) rint(val_real()); /* Real as default */ |
1
by brian
clean slate |
405 |
}
|
406 |
String *val_str(String*str); |
|
407 |
my_decimal *val_decimal(my_decimal *); |
|
408 |
void reset_field(); |
|
409 |
};
|
|
410 |
||
411 |
||
412 |
class Item_sum_int :public Item_sum_num |
|
413 |
{
|
|
414 |
public: |
|
415 |
Item_sum_int(Item *item_par) :Item_sum_num(item_par) {} |
|
416 |
Item_sum_int(List<Item> &list) :Item_sum_num(list) {} |
|
520.1.22
by Brian Aker
Second pass of thd cleanup |
417 |
Item_sum_int(Session *session, Item_sum_int *item) :Item_sum_num(session, item) {} |
51.1.25
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
418 |
double val_real() { assert(fixed == 1); return (double) val_int(); } |
1
by brian
clean slate |
419 |
String *val_str(String*str); |
420 |
my_decimal *val_decimal(my_decimal *); |
|
421 |
enum Item_result result_type () const { return INT_RESULT; } |
|
422 |
void fix_length_and_dec() |
|
423 |
{ decimals=0; max_length=21; maybe_null=null_value=0; } |
|
424 |
};
|
|
425 |
||
426 |
||
427 |
class Item_sum_sum :public Item_sum_num |
|
428 |
{
|
|
429 |
protected: |
|
430 |
Item_result hybrid_type; |
|
431 |
double sum; |
|
432 |
my_decimal dec_buffs[2]; |
|
482
by Brian Aker
Remove uint. |
433 |
uint32_t curr_dec_buff; |
1
by brian
clean slate |
434 |
void fix_length_and_dec(); |
435 |
||
436 |
public: |
|
437 |
Item_sum_sum(Item *item_par) :Item_sum_num(item_par) {} |
|
520.1.22
by Brian Aker
Second pass of thd cleanup |
438 |
Item_sum_sum(Session *session, Item_sum_sum *item); |
1
by brian
clean slate |
439 |
enum Sumfunctype sum_func () const {return SUM_FUNC;} |
440 |
void clear(); |
|
441 |
bool add(); |
|
442 |
double val_real(); |
|
152
by Brian Aker
longlong replacement |
443 |
int64_t val_int(); |
1
by brian
clean slate |
444 |
String *val_str(String*str); |
445 |
my_decimal *val_decimal(my_decimal *); |
|
446 |
enum Item_result result_type () const { return hybrid_type; } |
|
447 |
void reset_field(); |
|
448 |
void update_field(); |
|
449 |
void no_rows_in_result() {} |
|
450 |
const char *func_name() const { return "sum("; } |
|
520.1.22
by Brian Aker
Second pass of thd cleanup |
451 |
Item *copy_or_same(Session* session); |
1
by brian
clean slate |
452 |
};
|
453 |
||
454 |
||
455 |
||
456 |
/* Common class for SUM(DISTINCT), AVG(DISTINCT) */
|
|
457 |
||
458 |
class Unique; |
|
459 |
||
460 |
class Item_sum_distinct :public Item_sum_num |
|
461 |
{
|
|
462 |
protected: |
|
463 |
/* storage for the summation result */
|
|
151
by Brian Aker
Ulonglong to uint64_t |
464 |
uint64_t count; |
1
by brian
clean slate |
465 |
Hybrid_type val; |
466 |
/* storage for unique elements */
|
|
467 |
Unique *tree; |
|
327.1.5
by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h |
468 |
Table *table; |
1
by brian
clean slate |
469 |
enum enum_field_types table_field_type; |
482
by Brian Aker
Remove uint. |
470 |
uint32_t tree_key_length; |
1
by brian
clean slate |
471 |
protected: |
520.1.22
by Brian Aker
Second pass of thd cleanup |
472 |
Item_sum_distinct(Session *session, Item_sum_distinct *item); |
1
by brian
clean slate |
473 |
public: |
474 |
Item_sum_distinct(Item *item_par); |
|
475 |
~Item_sum_distinct(); |
|
476 |
||
520.1.22
by Brian Aker
Second pass of thd cleanup |
477 |
bool setup(Session *session); |
1
by brian
clean slate |
478 |
void clear(); |
479 |
void cleanup(); |
|
480 |
bool add(); |
|
481 |
double val_real(); |
|
482 |
my_decimal *val_decimal(my_decimal *); |
|
152
by Brian Aker
longlong replacement |
483 |
int64_t val_int(); |
1
by brian
clean slate |
484 |
String *val_str(String *str); |
485 |
||
486 |
/* XXX: does it need make_unique? */
|
|
487 |
||
488 |
enum Sumfunctype sum_func () const { return SUM_DISTINCT_FUNC; } |
|
489 |
void reset_field() {} // not used |
|
490 |
void update_field() {} // not used |
|
491 |
virtual void no_rows_in_result() {} |
|
492 |
void fix_length_and_dec(); |
|
493 |
enum Item_result result_type () const { return val.traits->type(); } |
|
494 |
virtual void calculate_val_and_count(); |
|
495 |
virtual bool unique_walk_function(void *elem); |
|
496 |
};
|
|
497 |
||
498 |
||
499 |
/*
|
|
500 |
Item_sum_sum_distinct - implementation of SUM(DISTINCT expr).
|
|
501 |
See also: MySQL manual, chapter 'Adding New Functions To MySQL'
|
|
502 |
and comments in item_sum.cc.
|
|
503 |
*/
|
|
504 |
||
505 |
class Item_sum_sum_distinct :public Item_sum_distinct |
|
506 |
{
|
|
507 |
private: |
|
520.1.22
by Brian Aker
Second pass of thd cleanup |
508 |
Item_sum_sum_distinct(Session *session, Item_sum_sum_distinct *item) |
509 |
:Item_sum_distinct(session, item) {} |
|
1
by brian
clean slate |
510 |
public: |
511 |
Item_sum_sum_distinct(Item *item_arg) :Item_sum_distinct(item_arg) {} |
|
512 |
||
513 |
enum Sumfunctype sum_func () const { return SUM_DISTINCT_FUNC; } |
|
514 |
const char *func_name() const { return "sum(distinct "; } |
|
520.1.22
by Brian Aker
Second pass of thd cleanup |
515 |
Item *copy_or_same(Session* session) { return new Item_sum_sum_distinct(session, this); } |
1
by brian
clean slate |
516 |
};
|
517 |
||
518 |
||
519 |
/* Item_sum_avg_distinct - SELECT AVG(DISTINCT expr) FROM ... */
|
|
520 |
||
521 |
class Item_sum_avg_distinct: public Item_sum_distinct |
|
522 |
{
|
|
523 |
private: |
|
520.1.22
by Brian Aker
Second pass of thd cleanup |
524 |
Item_sum_avg_distinct(Session *session, Item_sum_avg_distinct *original) |
525 |
:Item_sum_distinct(session, original) {} |
|
1
by brian
clean slate |
526 |
public: |
482
by Brian Aker
Remove uint. |
527 |
uint32_t prec_increment; |
1
by brian
clean slate |
528 |
Item_sum_avg_distinct(Item *item_arg) : Item_sum_distinct(item_arg) {} |
529 |
||
530 |
void fix_length_and_dec(); |
|
531 |
virtual void calculate_val_and_count(); |
|
532 |
enum Sumfunctype sum_func () const { return AVG_DISTINCT_FUNC; } |
|
533 |
const char *func_name() const { return "avg(distinct "; } |
|
520.1.22
by Brian Aker
Second pass of thd cleanup |
534 |
Item *copy_or_same(Session* session) { return new Item_sum_avg_distinct(session, this); } |
1
by brian
clean slate |
535 |
};
|
536 |
||
537 |
||
538 |
class Item_sum_count :public Item_sum_int |
|
539 |
{
|
|
152
by Brian Aker
longlong replacement |
540 |
int64_t count; |
1
by brian
clean slate |
541 |
|
542 |
public: |
|
543 |
Item_sum_count(Item *item_par) |
|
544 |
:Item_sum_int(item_par),count(0) |
|
545 |
{}
|
|
520.1.22
by Brian Aker
Second pass of thd cleanup |
546 |
Item_sum_count(Session *session, Item_sum_count *item) |
547 |
:Item_sum_int(session, item), count(item->count) |
|
1
by brian
clean slate |
548 |
{}
|
549 |
enum Sumfunctype sum_func () const { return COUNT_FUNC; } |
|
550 |
void clear(); |
|
551 |
void no_rows_in_result() { count=0; } |
|
552 |
bool add(); |
|
152
by Brian Aker
longlong replacement |
553 |
void make_const(int64_t count_arg) |
1
by brian
clean slate |
554 |
{
|
555 |
count=count_arg; |
|
556 |
Item_sum::make_const(); |
|
557 |
}
|
|
152
by Brian Aker
longlong replacement |
558 |
int64_t val_int(); |
1
by brian
clean slate |
559 |
void reset_field(); |
560 |
void cleanup(); |
|
561 |
void update_field(); |
|
562 |
const char *func_name() const { return "count("; } |
|
520.1.22
by Brian Aker
Second pass of thd cleanup |
563 |
Item *copy_or_same(Session* session); |
1
by brian
clean slate |
564 |
};
|
565 |
||
566 |
||
567 |
class TMP_TABLE_PARAM; |
|
568 |
||
569 |
class Item_sum_count_distinct :public Item_sum_int |
|
570 |
{
|
|
327.1.5
by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h |
571 |
Table *table; |
205
by Brian Aker
uint32 -> uin32_t |
572 |
uint32_t *field_lengths; |
1
by brian
clean slate |
573 |
TMP_TABLE_PARAM *tmp_table_param; |
574 |
bool force_copy_fields; |
|
575 |
/*
|
|
576 |
If there are no blobs, we can use a tree, which
|
|
577 |
is faster than heap table. In that case, we still use the table
|
|
578 |
to help get things set up, but we insert nothing in it
|
|
579 |
*/
|
|
580 |
Unique *tree; |
|
581 |
/*
|
|
582 |
Storage for the value of count between calls to val_int() so val_int()
|
|
583 |
will not recalculate on each call. Validitiy of the value is stored in
|
|
584 |
is_evaluated.
|
|
585 |
*/
|
|
152
by Brian Aker
longlong replacement |
586 |
int64_t count; |
1
by brian
clean slate |
587 |
/*
|
588 |
Following is 0 normal object and pointer to original one for copy
|
|
589 |
(to correctly free resources)
|
|
590 |
*/
|
|
591 |
Item_sum_count_distinct *original; |
|
482
by Brian Aker
Remove uint. |
592 |
uint32_t tree_key_length; |
1
by brian
clean slate |
593 |
|
594 |
||
595 |
bool always_null; // Set to 1 if the result is always NULL |
|
596 |
||
597 |
||
481
by Brian Aker
Remove all of uchar. |
598 |
friend int composite_key_cmp(void* arg, unsigned char* key1, unsigned char* key2); |
599 |
friend int simple_str_key_cmp(void* arg, unsigned char* key1, unsigned char* key2); |
|
1
by brian
clean slate |
600 |
|
601 |
public: |
|
602 |
Item_sum_count_distinct(List<Item> &list) |
|
603 |
:Item_sum_int(list), table(0), field_lengths(0), tmp_table_param(0), |
|
604 |
force_copy_fields(0), tree(0), count(0), |
|
51.1.25
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
605 |
original(0), always_null(false) |
1
by brian
clean slate |
606 |
{ quick_group= 0; } |
520.1.22
by Brian Aker
Second pass of thd cleanup |
607 |
Item_sum_count_distinct(Session *session, Item_sum_count_distinct *item) |
608 |
:Item_sum_int(session, item), table(item->table), |
|
1
by brian
clean slate |
609 |
field_lengths(item->field_lengths), |
610 |
tmp_table_param(item->tmp_table_param), |
|
611 |
force_copy_fields(0), tree(item->tree), count(item->count), |
|
612 |
original(item), tree_key_length(item->tree_key_length), |
|
613 |
always_null(item->always_null) |
|
614 |
{}
|
|
615 |
~Item_sum_count_distinct(); |
|
616 |
||
617 |
void cleanup(); |
|
618 |
||
619 |
enum Sumfunctype sum_func () const { return COUNT_DISTINCT_FUNC; } |
|
620 |
void clear(); |
|
621 |
bool add(); |
|
152
by Brian Aker
longlong replacement |
622 |
int64_t val_int(); |
1
by brian
clean slate |
623 |
void reset_field() { return ;} // Never called |
624 |
void update_field() { return ; } // Never called |
|
625 |
const char *func_name() const { return "count(distinct "; } |
|
520.1.22
by Brian Aker
Second pass of thd cleanup |
626 |
bool setup(Session *session); |
1
by brian
clean slate |
627 |
void make_unique(); |
520.1.22
by Brian Aker
Second pass of thd cleanup |
628 |
Item *copy_or_same(Session* session); |
1
by brian
clean slate |
629 |
void no_rows_in_result() {} |
630 |
};
|
|
631 |
||
632 |
||
633 |
/* Item to get the value of a stored sum function */
|
|
634 |
||
635 |
class Item_sum_avg; |
|
636 |
||
637 |
class Item_avg_field :public Item_result_field |
|
638 |
{
|
|
639 |
public: |
|
640 |
Field *field; |
|
641 |
Item_result hybrid_type; |
|
482
by Brian Aker
Remove uint. |
642 |
uint32_t f_precision, f_scale, dec_bin_size; |
643 |
uint32_t prec_increment; |
|
1
by brian
clean slate |
644 |
Item_avg_field(Item_result res_type, Item_sum_avg *item); |
645 |
enum Type type() const { return FIELD_AVG_ITEM; } |
|
646 |
double val_real(); |
|
152
by Brian Aker
longlong replacement |
647 |
int64_t val_int(); |
1
by brian
clean slate |
648 |
my_decimal *val_decimal(my_decimal *); |
649 |
bool is_null() { update_null_value(); return null_value; } |
|
650 |
String *val_str(String*); |
|
651 |
enum_field_types field_type() const |
|
652 |
{
|
|
653 |
return hybrid_type == DECIMAL_RESULT ? |
|
212.2.2
by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE |
654 |
DRIZZLE_TYPE_NEWDECIMAL : DRIZZLE_TYPE_DOUBLE; |
1
by brian
clean slate |
655 |
}
|
656 |
void fix_length_and_dec() {} |
|
657 |
enum Item_result result_type () const { return hybrid_type; } |
|
658 |
};
|
|
659 |
||
660 |
||
661 |
class Item_sum_avg :public Item_sum_sum |
|
662 |
{
|
|
663 |
public: |
|
151
by Brian Aker
Ulonglong to uint64_t |
664 |
uint64_t count; |
482
by Brian Aker
Remove uint. |
665 |
uint32_t prec_increment; |
666 |
uint32_t f_precision, f_scale, dec_bin_size; |
|
1
by brian
clean slate |
667 |
|
668 |
Item_sum_avg(Item *item_par) :Item_sum_sum(item_par), count(0) {} |
|
520.1.22
by Brian Aker
Second pass of thd cleanup |
669 |
Item_sum_avg(Session *session, Item_sum_avg *item) |
670 |
:Item_sum_sum(session, item), count(item->count), |
|
1
by brian
clean slate |
671 |
prec_increment(item->prec_increment) {} |
672 |
||
673 |
void fix_length_and_dec(); |
|
674 |
enum Sumfunctype sum_func () const {return AVG_FUNC;} |
|
675 |
void clear(); |
|
676 |
bool add(); |
|
677 |
double val_real(); |
|
678 |
// In SPs we might force the "wrong" type with select into a declare variable
|
|
152
by Brian Aker
longlong replacement |
679 |
int64_t val_int() { return (int64_t) rint(val_real()); } |
1
by brian
clean slate |
680 |
my_decimal *val_decimal(my_decimal *); |
681 |
String *val_str(String *str); |
|
682 |
void reset_field(); |
|
683 |
void update_field(); |
|
212.1.3
by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)). |
684 |
Item *result_item(Field *field __attribute__((unused))) |
1
by brian
clean slate |
685 |
{ return new Item_avg_field(hybrid_type, this); } |
686 |
void no_rows_in_result() {} |
|
687 |
const char *func_name() const { return "avg("; } |
|
520.1.22
by Brian Aker
Second pass of thd cleanup |
688 |
Item *copy_or_same(Session* session); |
482
by Brian Aker
Remove uint. |
689 |
Field *create_tmp_field(bool group, Table *table, uint32_t convert_blob_length); |
1
by brian
clean slate |
690 |
void cleanup() |
691 |
{
|
|
692 |
count= 0; |
|
693 |
Item_sum_sum::cleanup(); |
|
694 |
}
|
|
695 |
};
|
|
696 |
||
697 |
class Item_sum_variance; |
|
698 |
||
699 |
class Item_variance_field :public Item_result_field |
|
700 |
{
|
|
701 |
public: |
|
702 |
Field *field; |
|
703 |
Item_result hybrid_type; |
|
482
by Brian Aker
Remove uint. |
704 |
uint32_t f_precision0, f_scale0; |
705 |
uint32_t f_precision1, f_scale1; |
|
706 |
uint32_t dec_bin_size0, dec_bin_size1; |
|
707 |
uint32_t sample; |
|
708 |
uint32_t prec_increment; |
|
1
by brian
clean slate |
709 |
Item_variance_field(Item_sum_variance *item); |
710 |
enum Type type() const {return FIELD_VARIANCE_ITEM; } |
|
711 |
double val_real(); |
|
152
by Brian Aker
longlong replacement |
712 |
int64_t val_int() |
713 |
{ /* can't be fix_fields()ed */ return (int64_t) rint(val_real()); } |
|
1
by brian
clean slate |
714 |
String *val_str(String *str) |
715 |
{ return val_string_from_real(str); } |
|
716 |
my_decimal *val_decimal(my_decimal *dec_buf) |
|
717 |
{ return val_decimal_from_real(dec_buf); } |
|
718 |
bool is_null() { update_null_value(); return null_value; } |
|
719 |
enum_field_types field_type() const |
|
720 |
{
|
|
721 |
return hybrid_type == DECIMAL_RESULT ? |
|
212.2.2
by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE |
722 |
DRIZZLE_TYPE_NEWDECIMAL : DRIZZLE_TYPE_DOUBLE; |
1
by brian
clean slate |
723 |
}
|
724 |
void fix_length_and_dec() {} |
|
725 |
enum Item_result result_type () const { return hybrid_type; } |
|
726 |
};
|
|
727 |
||
728 |
||
729 |
/*
|
|
730 |
variance(a) =
|
|
731 |
||
732 |
= sum (ai - avg(a))^2 / count(a) )
|
|
733 |
= sum (ai^2 - 2*ai*avg(a) + avg(a)^2) / count(a)
|
|
734 |
= (sum(ai^2) - sum(2*ai*avg(a)) + sum(avg(a)^2))/count(a) =
|
|
735 |
= (sum(ai^2) - 2*avg(a)*sum(a) + count(a)*avg(a)^2)/count(a) =
|
|
736 |
= (sum(ai^2) - 2*sum(a)*sum(a)/count(a) + count(a)*sum(a)^2/count(a)^2 )/count(a) =
|
|
737 |
= (sum(ai^2) - 2*sum(a)^2/count(a) + sum(a)^2/count(a) )/count(a) =
|
|
738 |
= (sum(ai^2) - sum(a)^2/count(a))/count(a)
|
|
739 |
||
740 |
But, this falls prey to catastrophic cancellation. Instead, use the recurrence formulas
|
|
741 |
||
742 |
M_{1} = x_{1}, ~ M_{k} = M_{k-1} + (x_{k} - M_{k-1}) / k newline
|
|
743 |
S_{1} = 0, ~ S_{k} = S_{k-1} + (x_{k} - M_{k-1}) times (x_{k} - M_{k}) newline
|
|
744 |
for 2 <= k <= n newline
|
|
745 |
ital variance = S_{n} / (n-1)
|
|
746 |
||
747 |
*/
|
|
748 |
||
749 |
class Item_sum_variance : public Item_sum_num |
|
750 |
{
|
|
751 |
void fix_length_and_dec(); |
|
752 |
||
753 |
public: |
|
754 |
Item_result hybrid_type; |
|
755 |
int cur_dec; |
|
756 |
double recurrence_m, recurrence_s; /* Used in recurrence relation. */ |
|
151
by Brian Aker
Ulonglong to uint64_t |
757 |
uint64_t count; |
482
by Brian Aker
Remove uint. |
758 |
uint32_t f_precision0, f_scale0; |
759 |
uint32_t f_precision1, f_scale1; |
|
760 |
uint32_t dec_bin_size0, dec_bin_size1; |
|
761 |
uint32_t sample; |
|
762 |
uint32_t prec_increment; |
|
1
by brian
clean slate |
763 |
|
482
by Brian Aker
Remove uint. |
764 |
Item_sum_variance(Item *item_par, uint32_t sample_arg) :Item_sum_num(item_par), |
1
by brian
clean slate |
765 |
hybrid_type(REAL_RESULT), count(0), sample(sample_arg) |
766 |
{}
|
|
520.1.22
by Brian Aker
Second pass of thd cleanup |
767 |
Item_sum_variance(Session *session, Item_sum_variance *item); |
1
by brian
clean slate |
768 |
enum Sumfunctype sum_func () const { return VARIANCE_FUNC; } |
769 |
void clear(); |
|
770 |
bool add(); |
|
771 |
double val_real(); |
|
772 |
my_decimal *val_decimal(my_decimal *); |
|
773 |
void reset_field(); |
|
774 |
void update_field(); |
|
212.1.3
by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)). |
775 |
Item *result_item(Field *field __attribute__((unused))) |
1
by brian
clean slate |
776 |
{ return new Item_variance_field(this); } |
777 |
void no_rows_in_result() {} |
|
778 |
const char *func_name() const |
|
779 |
{ return sample ? "var_samp(" : "variance("; } |
|
520.1.22
by Brian Aker
Second pass of thd cleanup |
780 |
Item *copy_or_same(Session* session); |
482
by Brian Aker
Remove uint. |
781 |
Field *create_tmp_field(bool group, Table *table, uint32_t convert_blob_length); |
1
by brian
clean slate |
782 |
enum Item_result result_type () const { return REAL_RESULT; } |
783 |
void cleanup() |
|
784 |
{
|
|
785 |
count= 0; |
|
786 |
Item_sum_num::cleanup(); |
|
787 |
}
|
|
788 |
};
|
|
789 |
||
790 |
class Item_sum_std; |
|
791 |
||
792 |
class Item_std_field :public Item_variance_field |
|
793 |
{
|
|
794 |
public: |
|
795 |
Item_std_field(Item_sum_std *item); |
|
796 |
enum Type type() const { return FIELD_STD_ITEM; } |
|
797 |
double val_real(); |
|
798 |
my_decimal *val_decimal(my_decimal *); |
|
799 |
enum Item_result result_type () const { return REAL_RESULT; } |
|
212.2.2
by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE |
800 |
enum_field_types field_type() const { return DRIZZLE_TYPE_DOUBLE;} |
1
by brian
clean slate |
801 |
};
|
802 |
||
803 |
/*
|
|
804 |
standard_deviation(a) = sqrt(variance(a))
|
|
805 |
*/
|
|
806 |
||
807 |
class Item_sum_std :public Item_sum_variance |
|
808 |
{
|
|
809 |
public: |
|
482
by Brian Aker
Remove uint. |
810 |
Item_sum_std(Item *item_par, uint32_t sample_arg) |
1
by brian
clean slate |
811 |
:Item_sum_variance(item_par, sample_arg) {} |
520.1.22
by Brian Aker
Second pass of thd cleanup |
812 |
Item_sum_std(Session *session, Item_sum_std *item) |
813 |
:Item_sum_variance(session, item) |
|
1
by brian
clean slate |
814 |
{}
|
815 |
enum Sumfunctype sum_func () const { return STD_FUNC; } |
|
816 |
double val_real(); |
|
212.1.3
by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)). |
817 |
Item *result_item(Field *field __attribute__((unused))) |
1
by brian
clean slate |
818 |
{ return new Item_std_field(this); } |
819 |
const char *func_name() const { return "std("; } |
|
520.1.22
by Brian Aker
Second pass of thd cleanup |
820 |
Item *copy_or_same(Session* session); |
1
by brian
clean slate |
821 |
enum Item_result result_type () const { return REAL_RESULT; } |
212.2.2
by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE |
822 |
enum_field_types field_type() const { return DRIZZLE_TYPE_DOUBLE;} |
1
by brian
clean slate |
823 |
};
|
824 |
||
825 |
// This class is a string or number function depending on num_func
|
|
826 |
||
827 |
class Item_sum_hybrid :public Item_sum |
|
828 |
{
|
|
829 |
protected: |
|
830 |
String value,tmp_value; |
|
831 |
double sum; |
|
152
by Brian Aker
longlong replacement |
832 |
int64_t sum_int; |
1
by brian
clean slate |
833 |
my_decimal sum_dec; |
834 |
Item_result hybrid_type; |
|
835 |
enum_field_types hybrid_field_type; |
|
836 |
int cmp_sign; |
|
837 |
bool was_values; // Set if we have found at least one row (for max/min only) |
|
838 |
||
839 |
public: |
|
840 |
Item_sum_hybrid(Item *item_par,int sign) |
|
841 |
:Item_sum(item_par), sum(0.0), sum_int(0), |
|
212.2.2
by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE |
842 |
hybrid_type(INT_RESULT), hybrid_field_type(DRIZZLE_TYPE_LONGLONG), |
51.1.25
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
843 |
cmp_sign(sign), was_values(true) |
1
by brian
clean slate |
844 |
{ collation.set(&my_charset_bin); } |
520.1.22
by Brian Aker
Second pass of thd cleanup |
845 |
Item_sum_hybrid(Session *session, Item_sum_hybrid *item); |
520.1.21
by Brian Aker
THD -> Session rename |
846 |
bool fix_fields(Session *, Item **); |
1
by brian
clean slate |
847 |
void clear(); |
848 |
double val_real(); |
|
152
by Brian Aker
longlong replacement |
849 |
int64_t val_int(); |
1
by brian
clean slate |
850 |
my_decimal *val_decimal(my_decimal *); |
851 |
void reset_field(); |
|
852 |
String *val_str(String *); |
|
853 |
bool keep_field_type(void) const { return 1; } |
|
854 |
enum Item_result result_type () const { return hybrid_type; } |
|
855 |
enum enum_field_types field_type() const { return hybrid_field_type; } |
|
856 |
void update_field(); |
|
857 |
void min_max_update_str_field(); |
|
858 |
void min_max_update_real_field(); |
|
859 |
void min_max_update_int_field(); |
|
860 |
void min_max_update_decimal_field(); |
|
861 |
void cleanup(); |
|
862 |
bool any_value() { return was_values; } |
|
863 |
void no_rows_in_result(); |
|
327.1.5
by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h |
864 |
Field *create_tmp_field(bool group, Table *table, |
482
by Brian Aker
Remove uint. |
865 |
uint32_t convert_blob_length); |
1
by brian
clean slate |
866 |
};
|
867 |
||
868 |
||
869 |
class Item_sum_min :public Item_sum_hybrid |
|
870 |
{
|
|
871 |
public: |
|
872 |
Item_sum_min(Item *item_par) :Item_sum_hybrid(item_par,1) {} |
|
520.1.22
by Brian Aker
Second pass of thd cleanup |
873 |
Item_sum_min(Session *session, Item_sum_min *item) :Item_sum_hybrid(session, item) {} |
1
by brian
clean slate |
874 |
enum Sumfunctype sum_func () const {return MIN_FUNC;} |
875 |
||
876 |
bool add(); |
|
877 |
const char *func_name() const { return "min("; } |
|
520.1.22
by Brian Aker
Second pass of thd cleanup |
878 |
Item *copy_or_same(Session* session); |
1
by brian
clean slate |
879 |
};
|
880 |
||
881 |
||
882 |
class Item_sum_max :public Item_sum_hybrid |
|
883 |
{
|
|
884 |
public: |
|
885 |
Item_sum_max(Item *item_par) :Item_sum_hybrid(item_par,-1) {} |
|
520.1.22
by Brian Aker
Second pass of thd cleanup |
886 |
Item_sum_max(Session *session, Item_sum_max *item) :Item_sum_hybrid(session, item) {} |
1
by brian
clean slate |
887 |
enum Sumfunctype sum_func () const {return MAX_FUNC;} |
888 |
||
889 |
bool add(); |
|
890 |
const char *func_name() const { return "max("; } |
|
520.1.22
by Brian Aker
Second pass of thd cleanup |
891 |
Item *copy_or_same(Session* session); |
1
by brian
clean slate |
892 |
};
|
893 |
||
894 |
||
895 |
class Item_sum_bit :public Item_sum_int |
|
896 |
{
|
|
897 |
protected: |
|
151
by Brian Aker
Ulonglong to uint64_t |
898 |
uint64_t reset_bits,bits; |
1
by brian
clean slate |
899 |
|
900 |
public: |
|
151
by Brian Aker
Ulonglong to uint64_t |
901 |
Item_sum_bit(Item *item_par,uint64_t reset_arg) |
1
by brian
clean slate |
902 |
:Item_sum_int(item_par),reset_bits(reset_arg),bits(reset_arg) {} |
520.1.22
by Brian Aker
Second pass of thd cleanup |
903 |
Item_sum_bit(Session *session, Item_sum_bit *item): |
904 |
Item_sum_int(session, item), reset_bits(item->reset_bits), bits(item->bits) {} |
|
1
by brian
clean slate |
905 |
enum Sumfunctype sum_func () const {return SUM_BIT_FUNC;} |
906 |
void clear(); |
|
152
by Brian Aker
longlong replacement |
907 |
int64_t val_int(); |
1
by brian
clean slate |
908 |
void reset_field(); |
909 |
void update_field(); |
|
910 |
void fix_length_and_dec() |
|
911 |
{ decimals= 0; max_length=21; unsigned_flag= 1; maybe_null= null_value= 0; } |
|
912 |
void cleanup() |
|
913 |
{
|
|
914 |
bits= reset_bits; |
|
915 |
Item_sum_int::cleanup(); |
|
916 |
}
|
|
917 |
};
|
|
918 |
||
919 |
||
920 |
class Item_sum_or :public Item_sum_bit |
|
921 |
{
|
|
922 |
public: |
|
398.1.8
by Monty Taylor
Enabled -Wlong-long. |
923 |
Item_sum_or(Item *item_par) :Item_sum_bit(item_par,0) {} |
520.1.22
by Brian Aker
Second pass of thd cleanup |
924 |
Item_sum_or(Session *session, Item_sum_or *item) :Item_sum_bit(session, item) {} |
1
by brian
clean slate |
925 |
bool add(); |
926 |
const char *func_name() const { return "bit_or("; } |
|
520.1.22
by Brian Aker
Second pass of thd cleanup |
927 |
Item *copy_or_same(Session* session); |
1
by brian
clean slate |
928 |
};
|
929 |
||
930 |
||
931 |
class Item_sum_and :public Item_sum_bit |
|
932 |
{
|
|
933 |
public: |
|
163
by Brian Aker
Merge Monty's code. |
934 |
Item_sum_and(Item *item_par) :Item_sum_bit(item_par, UINT64_MAX) {} |
520.1.22
by Brian Aker
Second pass of thd cleanup |
935 |
Item_sum_and(Session *session, Item_sum_and *item) :Item_sum_bit(session, item) {} |
1
by brian
clean slate |
936 |
bool add(); |
937 |
const char *func_name() const { return "bit_and("; } |
|
520.1.22
by Brian Aker
Second pass of thd cleanup |
938 |
Item *copy_or_same(Session* session); |
1
by brian
clean slate |
939 |
};
|
940 |
||
941 |
class Item_sum_xor :public Item_sum_bit |
|
942 |
{
|
|
943 |
public: |
|
398.1.8
by Monty Taylor
Enabled -Wlong-long. |
944 |
Item_sum_xor(Item *item_par) :Item_sum_bit(item_par,0) {} |
520.1.22
by Brian Aker
Second pass of thd cleanup |
945 |
Item_sum_xor(Session *session, Item_sum_xor *item) :Item_sum_bit(session, item) {} |
1
by brian
clean slate |
946 |
bool add(); |
947 |
const char *func_name() const { return "bit_xor("; } |
|
520.1.22
by Brian Aker
Second pass of thd cleanup |
948 |
Item *copy_or_same(Session* session); |
1
by brian
clean slate |
949 |
};
|
950 |
||
951 |
||
952 |
||
261.4.1
by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR. |
953 |
class DRIZZLE_ERROR; |
1
by brian
clean slate |
954 |
|
955 |
class Item_func_group_concat : public Item_sum |
|
956 |
{
|
|
957 |
TMP_TABLE_PARAM *tmp_table_param; |
|
261.4.1
by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR. |
958 |
DRIZZLE_ERROR *warning; |
1
by brian
clean slate |
959 |
String result; |
960 |
String *separator; |
|
961 |
TREE tree_base; |
|
962 |
TREE *tree; |
|
963 |
||
964 |
/**
|
|
965 |
If DISTINCT is used with this GROUP_CONCAT, this member is used to filter
|
|
966 |
out duplicates.
|
|
967 |
@see Item_func_group_concat::setup
|
|
968 |
@see Item_func_group_concat::add
|
|
969 |
@see Item_func_group_concat::clear
|
|
970 |
*/
|
|
971 |
Unique *unique_filter; |
|
327.1.5
by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h |
972 |
Table *table; |
327.2.3
by Brian Aker
Refactoring of class Table |
973 |
order_st **order; |
1
by brian
clean slate |
974 |
Name_resolution_context *context; |
975 |
/** The number of ORDER BY items. */
|
|
482
by Brian Aker
Remove uint. |
976 |
uint32_t arg_count_order; |
1
by brian
clean slate |
977 |
/** The number of selected items, aka the expr list. */
|
482
by Brian Aker
Remove uint. |
978 |
uint32_t arg_count_field; |
979 |
uint32_t count_cut_values; |
|
1
by brian
clean slate |
980 |
bool distinct; |
981 |
bool warning_for_row; |
|
982 |
bool always_null; |
|
983 |
bool force_copy_fields; |
|
984 |
bool no_appended; |
|
985 |
/*
|
|
986 |
Following is 0 normal object and pointer to original one for copy
|
|
987 |
(to correctly free resources)
|
|
988 |
*/
|
|
989 |
Item_func_group_concat *original; |
|
990 |
||
991 |
friend int group_concat_key_cmp_with_distinct(void* arg, const void* key1, |
|
992 |
const void* key2); |
|
993 |
friend int group_concat_key_cmp_with_order(void* arg, const void* key1, |
|
994 |
const void* key2); |
|
481
by Brian Aker
Remove all of uchar. |
995 |
friend int dump_leaf_key(unsigned char* key, |
1
by brian
clean slate |
996 |
element_count count __attribute__((unused)), |
997 |
Item_func_group_concat *group_concat_item); |
|
998 |
||
999 |
public: |
|
1000 |
Item_func_group_concat(Name_resolution_context *context_arg, |
|
1001 |
bool is_distinct, List<Item> *is_select, |
|
1002 |
SQL_LIST *is_order, String *is_separator); |
|
1003 |
||
520.1.22
by Brian Aker
Second pass of thd cleanup |
1004 |
Item_func_group_concat(Session *session, Item_func_group_concat *item); |
1
by brian
clean slate |
1005 |
~Item_func_group_concat(); |
1006 |
void cleanup(); |
|
1007 |
||
1008 |
enum Sumfunctype sum_func () const {return GROUP_CONCAT_FUNC;} |
|
1009 |
const char *func_name() const { return "group_concat"; } |
|
1010 |
virtual Item_result result_type () const { return STRING_RESULT; } |
|
1011 |
enum_field_types field_type() const |
|
1012 |
{
|
|
1013 |
if (max_length/collation.collation->mbmaxlen > CONVERT_IF_BIGGER_TO_BLOB ) |
|
212.2.2
by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE |
1014 |
return DRIZZLE_TYPE_BLOB; |
1
by brian
clean slate |
1015 |
else
|
212.2.2
by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE |
1016 |
return DRIZZLE_TYPE_VARCHAR; |
1
by brian
clean slate |
1017 |
}
|
1018 |
void clear(); |
|
1019 |
bool add(); |
|
51.1.25
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
1020 |
void reset_field() { assert(0); } // not used |
1021 |
void update_field() { assert(0); } // not used |
|
520.1.21
by Brian Aker
THD -> Session rename |
1022 |
bool fix_fields(Session *,Item **); |
520.1.22
by Brian Aker
Second pass of thd cleanup |
1023 |
bool setup(Session *session); |
1
by brian
clean slate |
1024 |
void make_unique(); |
1025 |
double val_real() |
|
1026 |
{
|
|
1027 |
String *res; res=val_str(&str_value); |
|
1028 |
return res ? my_atof(res->c_ptr()) : 0.0; |
|
1029 |
}
|
|
152
by Brian Aker
longlong replacement |
1030 |
int64_t val_int() |
1
by brian
clean slate |
1031 |
{
|
1032 |
String *res; |
|
1033 |
char *end_ptr; |
|
1034 |
int error; |
|
1035 |
if (!(res= val_str(&str_value))) |
|
152
by Brian Aker
longlong replacement |
1036 |
return (int64_t) 0; |
1
by brian
clean slate |
1037 |
end_ptr= (char*) res->ptr()+ res->length(); |
1038 |
return my_strtoll10(res->ptr(), &end_ptr, &error); |
|
1039 |
}
|
|
1040 |
my_decimal *val_decimal(my_decimal *decimal_value) |
|
1041 |
{
|
|
1042 |
return val_decimal_from_string(decimal_value); |
|
1043 |
}
|
|
1044 |
String* val_str(String* str); |
|
520.1.22
by Brian Aker
Second pass of thd cleanup |
1045 |
Item *copy_or_same(Session* session); |
1
by brian
clean slate |
1046 |
void no_rows_in_result() {} |
1047 |
virtual void print(String *str, enum_query_type query_type); |
|
481
by Brian Aker
Remove all of uchar. |
1048 |
virtual bool change_context_processor(unsigned char *cntx) |
51.1.25
by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE |
1049 |
{ context= (Name_resolution_context *)cntx; return false; } |
1
by brian
clean slate |
1050 |
};
|