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