~drizzle-trunk/drizzle/development

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