~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to server/item_sum.h

Merge/fix in FAQ.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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, Inc.
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
 
 */
19
 
 
20
 
#ifndef DRIZZLED_ITEM_SUM_H
21
 
#define DRIZZLED_ITEM_SUM_H
 
1
/* Copyright (C) 2000-2006 MySQL AB
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
22
16
 
23
17
/* classes for sum functions */
24
18
 
25
 
#include <drizzled/tree.h>
26
 
#include <drizzled/hybrid_type.h>
27
 
#include <drizzled/item.h>
28
 
#include <drizzled/item/field.h>
29
 
#include <drizzled/item/bin_string.h>
30
 
#include <drizzled/charset_info.h>
31
 
 
32
 
namespace drizzled
33
 
{
34
 
 
35
 
int group_concat_key_cmp_with_distinct(void* arg, const void* key1,
36
 
                                       const void* key2);
37
 
 
38
 
int group_concat_key_cmp_with_order(void* arg, const void* key1,
39
 
                                    const void* key2);
40
 
 
41
 
class Select_Lex;
42
 
struct Order;
 
19
#ifdef USE_PRAGMA_INTERFACE
 
20
#pragma interface                       /* gcc class implementation */
 
21
#endif
 
22
 
 
23
#include <my_tree.h>
43
24
 
44
25
/*
45
26
  Class Item_sum is the base class used for special expressions that SQL calls
49
30
 GENERAL NOTES
50
31
 
51
32
  A set function cannot be used in certain positions where expressions are
52
 
  accepted. There are some quite explicable restrictions for the usage of
 
33
  accepted. There are some quite explicable restrictions for the usage of 
53
34
  set functions.
54
35
 
55
36
  In the query:
56
37
    SELECT AVG(b) FROM t1 WHERE SUM(b) > 20 GROUP by a
57
38
  the usage of the set function AVG(b) is legal, while the usage of SUM(b)
58
 
  is illegal. A WHERE condition must contain expressions that can be
 
39
  is illegal. A WHERE condition must contain expressions that can be 
59
40
  evaluated for each row of the table. Yet the expression SUM(b) can be
60
41
  evaluated only for each group of rows with the same value of column a.
61
42
  In the query:
80
61
  The problem of finding the query where to aggregate a particular
81
62
  set function is not so simple as it seems to be.
82
63
 
83
 
  In the query:
 
64
  In the query: 
84
65
    SELECT t1.a FROM t1 GROUP BY t1.a
85
66
     HAVING t1.a > ALL(SELECT t2.c FROM t2 GROUP BY t2.c
86
67
                         HAVING SUM(t1.a) < t2.c)
87
68
  the set function can be evaluated for both outer and inner selects.
88
69
  If we evaluate SUM(t1.a) for the outer query then we get the value of t1.a
89
 
  multiplied by the cardinality of a group in table t1. In this case
 
70
  multiplied by the cardinality of a group in table t1. In this case 
90
71
  in each correlated subquery SUM(t1.a) is used as a constant. But we also
91
72
  can evaluate SUM(t1.a) for the inner query. In this case t1.a will be a
92
73
  constant for each correlated subquery and summation is performed
93
74
  for each group of table t2.
94
75
  (Here it makes sense to remind that the query
95
 
    SELECT c FROM t GROUP BY a HAVING SUM(1) < a
 
76
    SELECT c FROM t GROUP BY a HAVING SUM(1) < a 
96
77
  is quite legal in our SQL).
97
78
 
98
79
  So depending on what query we assign the set function to we
134
115
 
135
116
  3. SELECT t1.a FROM t1 GROUP BY t1.a
136
117
       HAVING t1.a > ALL(SELECT t2.b FROM t2
137
 
                           WHERE t2.b > ALL (SELECT t3.c FROM t3
 
118
                           WHERE t2.b > ALL (SELECT t3.c FROM t3 
138
119
                                               WHERE SUM(t1.a+t2.b) < t3.c))
139
120
  In this query evaluation of SUM(t1.a+t2.b) is not legal neither in the second
140
121
  nor in the third subqueries. So this query is invalid.
163
144
    SELECT t2.c FROM t2 GROUP BY t2.c HAVING AVG(t2.c+s)
164
145
  than returns some result set.
165
146
 
166
 
  By the same reason the following query with a subquery
 
147
  By the same reason the following query with a subquery 
167
148
    SELECT t1.a FROM t1 GROUP BY t1.a
168
149
      HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
169
150
                        HAVING AVG(SUM(t1.b)) > 20)
223
204
  and reports an error if it is illegal.
224
205
  The method register_sum_func serves to link the items for the set functions
225
206
  that are aggregated in the embedding (sub)queries. Circular chains of such
226
 
  functions are attached to the corresponding Select_Lex structures
 
207
  functions are attached to the corresponding st_select_lex structures
227
208
  through the field inner_sum_func_list.
228
209
 
229
210
  Exploiting the fact that the members mentioned above are used in one
230
211
  recursive function we could have allocated them on the thread stack.
231
212
  Yet we don't do it now.
232
 
 
 
213
  
233
214
  We assume that the nesting level of subquries does not exceed 127.
234
215
  TODO: to catch queries where the limit is exceeded to make the
235
 
  code clean here.
 
216
  code clean here.  
 
217
    
 
218
*/ 
236
219
 
237
 
*/
 
220
class st_select_lex;
238
221
 
239
222
class Item_sum :public Item_result_field
240
223
{
242
225
  enum Sumfunctype
243
226
  { COUNT_FUNC, COUNT_DISTINCT_FUNC, SUM_FUNC, SUM_DISTINCT_FUNC, AVG_FUNC,
244
227
    AVG_DISTINCT_FUNC, MIN_FUNC, MAX_FUNC, STD_FUNC,
245
 
    VARIANCE_FUNC, SUM_BIT_FUNC, GROUP_CONCAT_FUNC
 
228
    VARIANCE_FUNC, SUM_BIT_FUNC, UDF_SUM_FUNC, GROUP_CONCAT_FUNC
246
229
  };
247
230
 
248
231
  Item **args, *tmp_args[2];
249
232
  Item **ref_by; /* pointer to a ref to the object used to register it */
250
233
  Item_sum *next; /* next in the circular chain of registered objects  */
251
 
  uint32_t arg_count;
252
 
  Item_sum *in_sum_func;  /* embedding set function if any */
253
 
  Select_Lex * aggr_sel; /* select where the function is aggregated       */
254
 
  int8_t nest_level;        /* number of the nesting level of the set function */
255
 
  int8_t aggr_level;        /* nesting level of the aggregating subquery       */
256
 
  int8_t max_arg_level;     /* max level of unbound column references          */
257
 
  int8_t max_sum_func_level;/* max level of aggregation for embedded functions */
 
234
  uint arg_count;
 
235
  Item_sum *in_sum_func;  /* embedding set function if any */ 
 
236
  st_select_lex * aggr_sel; /* select where the function is aggregated       */ 
 
237
  int8 nest_level;        /* number of the nesting level of the set function */
 
238
  int8 aggr_level;        /* nesting level of the aggregating subquery       */
 
239
  int8 max_arg_level;     /* max level of unbound column references          */
 
240
  int8 max_sum_func_level;/* max level of aggregation for embedded functions */
258
241
  bool quick_group;                     /* If incremental update of fields */
259
242
  /*
260
243
    This list is used by the check for mixing non aggregated fields and
264
247
  */
265
248
  List<Item_field> outer_fields;
266
249
 
267
 
protected:
 
250
protected:  
268
251
  table_map used_tables_cache;
269
252
  bool forced_const;
270
253
 
271
 
public:
 
254
public:  
272
255
 
273
256
  void mark_as_sum_func();
274
257
  Item_sum() :arg_count(0), quick_group(1), forced_const(false)
275
258
  {
276
259
    mark_as_sum_func();
277
260
  }
278
 
  Item_sum(Item *a) :args(tmp_args), arg_count(1), quick_group(1),
 
261
  Item_sum(Item *a) :args(tmp_args), arg_count(1), quick_group(1), 
279
262
    forced_const(false)
280
263
  {
281
264
    args[0]=a;
289
272
  }
290
273
  Item_sum(List<Item> &list);
291
274
  //Copy constructor, need to perform subselects with temporary tables
292
 
  Item_sum(Session *session, Item_sum *item);
 
275
  Item_sum(THD *thd, Item_sum *item);
293
276
  enum Type type() const { return SUM_FUNC_ITEM; }
294
277
  virtual enum Sumfunctype sum_func () const=0;
295
278
 
354
337
    { return new Item_field(field); }
355
338
  table_map used_tables() const { return used_tables_cache; }
356
339
  void update_used_tables ();
357
 
  void cleanup()
358
 
  {
 
340
  void cleanup() 
 
341
  { 
359
342
    Item::cleanup();
360
 
    forced_const= false;
 
343
    forced_const= false; 
361
344
  }
362
345
  bool is_null() { return null_value; }
363
 
  void make_const ()
364
 
  {
365
 
    used_tables_cache= 0;
366
 
    forced_const= true;
 
346
  void make_const () 
 
347
  { 
 
348
    used_tables_cache= 0; 
 
349
    forced_const= true; 
367
350
  }
368
351
  virtual bool const_item() const { return forced_const; }
369
 
  virtual bool const_during_execution() const { return false; }
370
 
  void make_field(SendField *field);
 
352
  void make_field(Send_field *field);
371
353
  virtual void print(String *str, enum_query_type query_type);
372
354
  void fix_num_length_and_dec();
373
355
 
381
363
  */
382
364
  void no_rows_in_result() { clear(); }
383
365
 
384
 
  virtual bool setup(Session *) {return 0;}
 
366
  virtual bool setup(THD *thd __attribute__((__unused__))) {return 0;}
385
367
  virtual void make_unique(void) {}
386
 
  Item *get_tmp_table_item(Session *session);
387
 
  virtual Field *create_tmp_field(bool group, Table *table,
388
 
                                  uint32_t convert_blob_length);
389
 
  bool walk(Item_processor processor, bool walk_subquery, unsigned char *argument);
390
 
  bool init_sum_func_check(Session *session);
391
 
  bool check_sum_func(Session *session, Item **ref);
392
 
  bool register_sum_func(Session *session, Item **ref);
393
 
  Select_Lex *depended_from()
 
368
  Item *get_tmp_table_item(THD *thd);
 
369
  virtual Field *create_tmp_field(bool group, TABLE *table,
 
370
                                  uint convert_blob_length);
 
371
  bool walk(Item_processor processor, bool walk_subquery, uchar *argument);
 
372
  bool init_sum_func_check(THD *thd);
 
373
  bool check_sum_func(THD *thd, Item **ref);
 
374
  bool register_sum_func(THD *thd, Item **ref);
 
375
  st_select_lex *depended_from() 
394
376
    { return (nest_level == aggr_level ? 0 : aggr_sel); }
395
377
};
396
378
 
399
381
{
400
382
protected:
401
383
  /*
402
 
   val_xxx() functions may be called several times during the execution of a
 
384
   val_xxx() functions may be called several times during the execution of a 
403
385
   query. Derived classes that require extensive calculation in val_xxx()
404
 
   maintain cache of aggregate value. This variable governs the validity of
 
386
   maintain cache of aggregate value. This variable governs the validity of 
405
387
   that cache.
406
388
  */
407
389
  bool is_evaluated;
408
390
public:
409
391
  Item_sum_num() :Item_sum(),is_evaluated(false) {}
410
 
  Item_sum_num(Item *item_par)
 
392
  Item_sum_num(Item *item_par) 
411
393
    :Item_sum(item_par), is_evaluated(false) {}
412
394
  Item_sum_num(Item *a, Item* b) :Item_sum(a,b),is_evaluated(false) {}
413
 
  Item_sum_num(List<Item> &list)
 
395
  Item_sum_num(List<Item> &list) 
414
396
    :Item_sum(list), is_evaluated(false) {}
415
 
  Item_sum_num(Session *session, Item_sum_num *item)
416
 
    :Item_sum(session, item),is_evaluated(item->is_evaluated) {}
417
 
  bool fix_fields(Session *, Item **);
418
 
  int64_t val_int();
 
397
  Item_sum_num(THD *thd, Item_sum_num *item) 
 
398
    :Item_sum(thd, item),is_evaluated(item->is_evaluated) {}
 
399
  bool fix_fields(THD *, Item **);
 
400
  int64_t val_int()
 
401
  {
 
402
    assert(fixed == 1);
 
403
    return (int64_t) rint(val_real());             /* Real as default */
 
404
  }
419
405
  String *val_str(String*str);
420
 
  type::Decimal *val_decimal(type::Decimal *);
 
406
  my_decimal *val_decimal(my_decimal *);
421
407
  void reset_field();
422
408
};
423
409
 
427
413
public:
428
414
  Item_sum_int(Item *item_par) :Item_sum_num(item_par) {}
429
415
  Item_sum_int(List<Item> &list) :Item_sum_num(list) {}
430
 
  Item_sum_int(Session *session, Item_sum_int *item) :Item_sum_num(session, item) {}
 
416
  Item_sum_int(THD *thd, Item_sum_int *item) :Item_sum_num(thd, item) {}
431
417
  double val_real() { assert(fixed == 1); return (double) val_int(); }
432
418
  String *val_str(String*str);
433
 
  type::Decimal *val_decimal(type::Decimal *);
 
419
  my_decimal *val_decimal(my_decimal *);
434
420
  enum Item_result result_type () const { return INT_RESULT; }
435
421
  void fix_length_and_dec()
436
422
  { decimals=0; max_length=21; maybe_null=null_value=0; }
442
428
protected:
443
429
  Item_result hybrid_type;
444
430
  double sum;
445
 
  type::Decimal dec_buffs[2];
446
 
  uint32_t curr_dec_buff;
 
431
  my_decimal dec_buffs[2];
 
432
  uint curr_dec_buff;
447
433
  void fix_length_and_dec();
448
434
 
449
435
public:
450
436
  Item_sum_sum(Item *item_par) :Item_sum_num(item_par) {}
451
 
  Item_sum_sum(Session *session, Item_sum_sum *item);
 
437
  Item_sum_sum(THD *thd, Item_sum_sum *item);
452
438
  enum Sumfunctype sum_func () const {return SUM_FUNC;}
453
439
  void clear();
454
440
  bool add();
455
441
  double val_real();
456
442
  int64_t val_int();
457
443
  String *val_str(String*str);
458
 
  type::Decimal *val_decimal(type::Decimal *);
 
444
  my_decimal *val_decimal(my_decimal *);
459
445
  enum Item_result result_type () const { return hybrid_type; }
460
446
  void reset_field();
461
447
  void update_field();
462
448
  void no_rows_in_result() {}
463
449
  const char *func_name() const { return "sum("; }
464
 
  Item *copy_or_same(Session* session);
 
450
  Item *copy_or_same(THD* thd);
465
451
};
466
452
 
467
453
 
478
464
  Hybrid_type val;
479
465
  /* storage for unique elements */
480
466
  Unique *tree;
481
 
  Table *table;
 
467
  TABLE *table;
482
468
  enum enum_field_types table_field_type;
483
 
  uint32_t tree_key_length;
 
469
  uint tree_key_length;
484
470
protected:
485
 
  Item_sum_distinct(Session *session, Item_sum_distinct *item);
 
471
  Item_sum_distinct(THD *thd, Item_sum_distinct *item);
486
472
public:
487
473
  Item_sum_distinct(Item *item_par);
488
474
  ~Item_sum_distinct();
489
475
 
490
 
  bool setup(Session *session);
 
476
  bool setup(THD *thd);
491
477
  void clear();
492
478
  void cleanup();
493
479
  bool add();
494
480
  double val_real();
495
 
  type::Decimal *val_decimal(type::Decimal *);
 
481
  my_decimal *val_decimal(my_decimal *);
496
482
  int64_t val_int();
497
483
  String *val_str(String *str);
498
484
 
503
489
  void update_field() {} // not used
504
490
  virtual void no_rows_in_result() {}
505
491
  void fix_length_and_dec();
506
 
  enum Item_result result_type () const;
 
492
  enum Item_result result_type () const { return val.traits->type(); }
507
493
  virtual void calculate_val_and_count();
508
494
  virtual bool unique_walk_function(void *elem);
509
495
};
518
504
class Item_sum_sum_distinct :public Item_sum_distinct
519
505
{
520
506
private:
521
 
  Item_sum_sum_distinct(Session *session, Item_sum_sum_distinct *item)
522
 
    :Item_sum_distinct(session, item) {}
 
507
  Item_sum_sum_distinct(THD *thd, Item_sum_sum_distinct *item)
 
508
    :Item_sum_distinct(thd, item) {}
523
509
public:
524
510
  Item_sum_sum_distinct(Item *item_arg) :Item_sum_distinct(item_arg) {}
525
511
 
526
512
  enum Sumfunctype sum_func () const { return SUM_DISTINCT_FUNC; }
527
513
  const char *func_name() const { return "sum(distinct "; }
528
 
  Item *copy_or_same(Session* session) { return new Item_sum_sum_distinct(session, this); }
 
514
  Item *copy_or_same(THD* thd) { return new Item_sum_sum_distinct(thd, this); }
529
515
};
530
516
 
531
517
 
534
520
class Item_sum_avg_distinct: public Item_sum_distinct
535
521
{
536
522
private:
537
 
  Item_sum_avg_distinct(Session *session, Item_sum_avg_distinct *original)
538
 
    :Item_sum_distinct(session, original) {}
 
523
  Item_sum_avg_distinct(THD *thd, Item_sum_avg_distinct *original)
 
524
    :Item_sum_distinct(thd, original) {}
539
525
public:
540
 
  uint32_t prec_increment;
 
526
  uint prec_increment;
541
527
  Item_sum_avg_distinct(Item *item_arg) : Item_sum_distinct(item_arg) {}
542
528
 
543
529
  void fix_length_and_dec();
544
530
  virtual void calculate_val_and_count();
545
531
  enum Sumfunctype sum_func () const { return AVG_DISTINCT_FUNC; }
546
532
  const char *func_name() const { return "avg(distinct "; }
547
 
  Item *copy_or_same(Session* session) { return new Item_sum_avg_distinct(session, this); }
 
533
  Item *copy_or_same(THD* thd) { return new Item_sum_avg_distinct(thd, this); }
548
534
};
549
535
 
550
536
 
556
542
  Item_sum_count(Item *item_par)
557
543
    :Item_sum_int(item_par),count(0)
558
544
  {}
559
 
  Item_sum_count(Session *session, Item_sum_count *item)
560
 
    :Item_sum_int(session, item), count(item->count)
 
545
  Item_sum_count(THD *thd, Item_sum_count *item)
 
546
    :Item_sum_int(thd, item), count(item->count)
561
547
  {}
562
548
  enum Sumfunctype sum_func () const { return COUNT_FUNC; }
563
549
  void clear();
564
550
  void no_rows_in_result() { count=0; }
565
551
  bool add();
566
 
  void make_const_count(int64_t count_arg)
567
 
  {
 
552
  void make_const(int64_t count_arg) 
 
553
  { 
568
554
    count=count_arg;
569
555
    Item_sum::make_const();
570
556
  }
573
559
  void cleanup();
574
560
  void update_field();
575
561
  const char *func_name() const { return "count("; }
576
 
  Item *copy_or_same(Session* session);
 
562
  Item *copy_or_same(THD* thd);
577
563
};
578
564
 
579
565
 
580
 
class Tmp_Table_Param;
 
566
class TMP_TABLE_PARAM;
581
567
 
582
568
class Item_sum_count_distinct :public Item_sum_int
583
569
{
584
 
  Table *table;
585
 
  uint32_t *field_lengths;
586
 
  Tmp_Table_Param *tmp_table_param;
 
570
  TABLE *table;
 
571
  uint32 *field_lengths;
 
572
  TMP_TABLE_PARAM *tmp_table_param;
587
573
  bool force_copy_fields;
588
574
  /*
589
575
    If there are no blobs, we can use a tree, which
598
584
  */
599
585
  int64_t count;
600
586
  /*
601
 
    Following is 0 normal object and pointer to original one for copy
 
587
    Following is 0 normal object and pointer to original one for copy 
602
588
    (to correctly free resources)
603
589
  */
604
590
  Item_sum_count_distinct *original;
605
 
  uint32_t tree_key_length;
 
591
  uint tree_key_length;
606
592
 
607
593
 
608
594
  bool always_null;             // Set to 1 if the result is always NULL
609
595
 
610
596
 
611
 
  friend int composite_key_cmp(void* arg, unsigned char* key1, unsigned char* key2);
612
 
  friend int simple_str_key_cmp(void* arg, unsigned char* key1, unsigned char* key2);
 
597
  friend int composite_key_cmp(void* arg, uchar* key1, uchar* key2);
 
598
  friend int simple_str_key_cmp(void* arg, uchar* key1, uchar* key2);
613
599
 
614
600
public:
615
601
  Item_sum_count_distinct(List<Item> &list)
617
603
     force_copy_fields(0), tree(0), count(0),
618
604
     original(0), always_null(false)
619
605
  { quick_group= 0; }
620
 
  Item_sum_count_distinct(Session *session, Item_sum_count_distinct *item)
621
 
    :Item_sum_int(session, item), table(item->table),
 
606
  Item_sum_count_distinct(THD *thd, Item_sum_count_distinct *item)
 
607
    :Item_sum_int(thd, item), table(item->table),
622
608
     field_lengths(item->field_lengths),
623
609
     tmp_table_param(item->tmp_table_param),
624
 
     force_copy_fields(0), tree(item->tree), count(item->count),
 
610
     force_copy_fields(0), tree(item->tree), count(item->count), 
625
611
     original(item), tree_key_length(item->tree_key_length),
626
612
     always_null(item->always_null)
627
613
  {}
636
622
  void reset_field() { return ;}                // Never called
637
623
  void update_field() { return ; }              // Never called
638
624
  const char *func_name() const { return "count(distinct "; }
639
 
  bool setup(Session *session);
 
625
  bool setup(THD *thd);
640
626
  void make_unique();
641
 
  Item *copy_or_same(Session* session);
 
627
  Item *copy_or_same(THD* thd);
642
628
  void no_rows_in_result() {}
643
629
};
644
630
 
652
638
public:
653
639
  Field *field;
654
640
  Item_result hybrid_type;
655
 
  uint32_t f_precision, f_scale, dec_bin_size;
656
 
  uint32_t prec_increment;
 
641
  uint f_precision, f_scale, dec_bin_size;
 
642
  uint prec_increment;
657
643
  Item_avg_field(Item_result res_type, Item_sum_avg *item);
658
644
  enum Type type() const { return FIELD_AVG_ITEM; }
659
645
  double val_real();
660
646
  int64_t val_int();
661
 
  type::Decimal *val_decimal(type::Decimal *);
 
647
  my_decimal *val_decimal(my_decimal *);
662
648
  bool is_null() { update_null_value(); return null_value; }
663
649
  String *val_str(String*);
664
650
  enum_field_types field_type() const
665
651
  {
666
652
    return hybrid_type == DECIMAL_RESULT ?
667
 
      DRIZZLE_TYPE_DECIMAL : DRIZZLE_TYPE_DOUBLE;
 
653
      MYSQL_TYPE_NEWDECIMAL : MYSQL_TYPE_DOUBLE;
668
654
  }
669
655
  void fix_length_and_dec() {}
670
656
  enum Item_result result_type () const { return hybrid_type; }
675
661
{
676
662
public:
677
663
  uint64_t count;
678
 
  uint32_t prec_increment;
679
 
  uint32_t f_precision, f_scale, dec_bin_size;
 
664
  uint prec_increment;
 
665
  uint f_precision, f_scale, dec_bin_size;
680
666
 
681
667
  Item_sum_avg(Item *item_par) :Item_sum_sum(item_par), count(0) {}
682
 
  Item_sum_avg(Session *session, Item_sum_avg *item)
683
 
    :Item_sum_sum(session, item), count(item->count),
 
668
  Item_sum_avg(THD *thd, Item_sum_avg *item)
 
669
    :Item_sum_sum(thd, item), count(item->count),
684
670
    prec_increment(item->prec_increment) {}
685
671
 
686
672
  void fix_length_and_dec();
689
675
  bool add();
690
676
  double val_real();
691
677
  // In SPs we might force the "wrong" type with select into a declare variable
692
 
  int64_t val_int();
693
 
  type::Decimal *val_decimal(type::Decimal *);
 
678
  int64_t val_int() { return (int64_t) rint(val_real()); }
 
679
  my_decimal *val_decimal(my_decimal *);
694
680
  String *val_str(String *str);
695
681
  void reset_field();
696
682
  void update_field();
697
 
  Item *result_item(Field *)
 
683
  Item *result_item(Field *field __attribute__((__unused__)))
698
684
  { return new Item_avg_field(hybrid_type, this); }
699
685
  void no_rows_in_result() {}
700
686
  const char *func_name() const { return "avg("; }
701
 
  Item *copy_or_same(Session* session);
702
 
  Field *create_tmp_field(bool group, Table *table, uint32_t convert_blob_length);
 
687
  Item *copy_or_same(THD* thd);
 
688
  Field *create_tmp_field(bool group, TABLE *table, uint convert_blob_length);
703
689
  void cleanup()
704
690
  {
705
691
    count= 0;
714
700
public:
715
701
  Field *field;
716
702
  Item_result hybrid_type;
717
 
  uint32_t f_precision0, f_scale0;
718
 
  uint32_t f_precision1, f_scale1;
719
 
  uint32_t dec_bin_size0, dec_bin_size1;
720
 
  uint32_t sample;
721
 
  uint32_t prec_increment;
 
703
  uint f_precision0, f_scale0;
 
704
  uint f_precision1, f_scale1;
 
705
  uint dec_bin_size0, dec_bin_size1;
 
706
  uint sample;
 
707
  uint prec_increment;
722
708
  Item_variance_field(Item_sum_variance *item);
723
709
  enum Type type() const {return FIELD_VARIANCE_ITEM; }
724
710
  double val_real();
725
 
  int64_t val_int();
 
711
  int64_t val_int()
 
712
  { /* can't be fix_fields()ed */ return (int64_t) rint(val_real()); }
726
713
  String *val_str(String *str)
727
714
  { return val_string_from_real(str); }
728
 
  type::Decimal *val_decimal(type::Decimal *dec_buf)
 
715
  my_decimal *val_decimal(my_decimal *dec_buf)
729
716
  { return val_decimal_from_real(dec_buf); }
730
717
  bool is_null() { update_null_value(); return null_value; }
731
718
  enum_field_types field_type() const
732
719
  {
733
720
    return hybrid_type == DECIMAL_RESULT ?
734
 
      DRIZZLE_TYPE_DECIMAL : DRIZZLE_TYPE_DOUBLE;
 
721
      MYSQL_TYPE_NEWDECIMAL : MYSQL_TYPE_DOUBLE;
735
722
  }
736
723
  void fix_length_and_dec() {}
737
724
  enum Item_result result_type () const { return hybrid_type; }
743
730
 
744
731
  =  sum (ai - avg(a))^2 / count(a) )
745
732
  =  sum (ai^2 - 2*ai*avg(a) + avg(a)^2) / count(a)
746
 
  =  (sum(ai^2) - sum(2*ai*avg(a)) + sum(avg(a)^2))/count(a) =
747
 
  =  (sum(ai^2) - 2*avg(a)*sum(a) + count(a)*avg(a)^2)/count(a) =
748
 
  =  (sum(ai^2) - 2*sum(a)*sum(a)/count(a) + count(a)*sum(a)^2/count(a)^2 )/count(a) =
749
 
  =  (sum(ai^2) - 2*sum(a)^2/count(a) + sum(a)^2/count(a) )/count(a) =
 
733
  =  (sum(ai^2) - sum(2*ai*avg(a)) + sum(avg(a)^2))/count(a) = 
 
734
  =  (sum(ai^2) - 2*avg(a)*sum(a) + count(a)*avg(a)^2)/count(a) = 
 
735
  =  (sum(ai^2) - 2*sum(a)*sum(a)/count(a) + count(a)*sum(a)^2/count(a)^2 )/count(a) = 
 
736
  =  (sum(ai^2) - 2*sum(a)^2/count(a) + sum(a)^2/count(a) )/count(a) = 
750
737
  =  (sum(ai^2) - sum(a)^2/count(a))/count(a)
751
738
 
752
739
But, this falls prey to catastrophic cancellation.  Instead, use the recurrence formulas
753
740
 
754
 
  M_{1} = x_{1}, ~ M_{k} = M_{k-1} + (x_{k} - M_{k-1}) / k newline
 
741
  M_{1} = x_{1}, ~ M_{k} = M_{k-1} + (x_{k} - M_{k-1}) / k newline 
755
742
  S_{1} = 0, ~ S_{k} = S_{k-1} + (x_{k} - M_{k-1}) times (x_{k} - M_{k}) newline
756
743
  for 2 <= k <= n newline
757
744
  ital variance = S_{n} / (n-1)
767
754
  int cur_dec;
768
755
  double recurrence_m, recurrence_s;    /* Used in recurrence relation. */
769
756
  uint64_t count;
770
 
  uint32_t f_precision0, f_scale0;
771
 
  uint32_t f_precision1, f_scale1;
772
 
  uint32_t dec_bin_size0, dec_bin_size1;
773
 
  uint32_t sample;
774
 
  uint32_t prec_increment;
 
757
  uint f_precision0, f_scale0;
 
758
  uint f_precision1, f_scale1;
 
759
  uint dec_bin_size0, dec_bin_size1;
 
760
  uint sample;
 
761
  uint prec_increment;
775
762
 
776
 
  Item_sum_variance(Item *item_par, uint32_t sample_arg) :Item_sum_num(item_par),
 
763
  Item_sum_variance(Item *item_par, uint sample_arg) :Item_sum_num(item_par),
777
764
    hybrid_type(REAL_RESULT), count(0), sample(sample_arg)
778
765
    {}
779
 
  Item_sum_variance(Session *session, Item_sum_variance *item);
 
766
  Item_sum_variance(THD *thd, Item_sum_variance *item);
780
767
  enum Sumfunctype sum_func () const { return VARIANCE_FUNC; }
781
768
  void clear();
782
769
  bool add();
783
770
  double val_real();
784
 
  int64_t val_int();
785
 
  type::Decimal *val_decimal(type::Decimal *);
 
771
  my_decimal *val_decimal(my_decimal *);
786
772
  void reset_field();
787
773
  void update_field();
788
 
  Item *result_item(Field *)
 
774
  Item *result_item(Field *field __attribute__((__unused__)))
789
775
  { return new Item_variance_field(this); }
790
776
  void no_rows_in_result() {}
791
777
  const char *func_name() const
792
778
    { return sample ? "var_samp(" : "variance("; }
793
 
  Item *copy_or_same(Session* session);
794
 
  Field *create_tmp_field(bool group, Table *table, uint32_t convert_blob_length);
 
779
  Item *copy_or_same(THD* thd);
 
780
  Field *create_tmp_field(bool group, TABLE *table, uint convert_blob_length);
795
781
  enum Item_result result_type () const { return REAL_RESULT; }
796
782
  void cleanup()
797
783
  {
808
794
  Item_std_field(Item_sum_std *item);
809
795
  enum Type type() const { return FIELD_STD_ITEM; }
810
796
  double val_real();
811
 
  type::Decimal *val_decimal(type::Decimal *);
 
797
  my_decimal *val_decimal(my_decimal *);
812
798
  enum Item_result result_type () const { return REAL_RESULT; }
813
 
  enum_field_types field_type() const { return DRIZZLE_TYPE_DOUBLE;}
 
799
  enum_field_types field_type() const { return MYSQL_TYPE_DOUBLE;}
814
800
};
815
801
 
816
802
/*
820
806
class Item_sum_std :public Item_sum_variance
821
807
{
822
808
  public:
823
 
  Item_sum_std(Item *item_par, uint32_t sample_arg)
 
809
  Item_sum_std(Item *item_par, uint sample_arg)
824
810
    :Item_sum_variance(item_par, sample_arg) {}
825
 
  Item_sum_std(Session *session, Item_sum_std *item)
826
 
    :Item_sum_variance(session, item)
 
811
  Item_sum_std(THD *thd, Item_sum_std *item)
 
812
    :Item_sum_variance(thd, item)
827
813
    {}
828
814
  enum Sumfunctype sum_func () const { return STD_FUNC; }
829
815
  double val_real();
830
 
  Item *result_item(Field *)
 
816
  Item *result_item(Field *field __attribute__((__unused__)))
831
817
    { return new Item_std_field(this); }
832
818
  const char *func_name() const { return "std("; }
833
 
  Item *copy_or_same(Session* session);
 
819
  Item *copy_or_same(THD* thd);
834
820
  enum Item_result result_type () const { return REAL_RESULT; }
835
 
  enum_field_types field_type() const { return DRIZZLE_TYPE_DOUBLE;}
 
821
  enum_field_types field_type() const { return MYSQL_TYPE_DOUBLE;}
836
822
};
837
823
 
838
824
// This class is a string or number function depending on num_func
843
829
  String value,tmp_value;
844
830
  double sum;
845
831
  int64_t sum_int;
846
 
  type::Decimal sum_dec;
 
832
  my_decimal sum_dec;
847
833
  Item_result hybrid_type;
848
834
  enum_field_types hybrid_field_type;
849
835
  int cmp_sign;
852
838
  public:
853
839
  Item_sum_hybrid(Item *item_par,int sign)
854
840
    :Item_sum(item_par), sum(0.0), sum_int(0),
855
 
    hybrid_type(INT_RESULT), hybrid_field_type(DRIZZLE_TYPE_LONGLONG),
 
841
    hybrid_type(INT_RESULT), hybrid_field_type(MYSQL_TYPE_LONGLONG),
856
842
    cmp_sign(sign), was_values(true)
857
843
  { collation.set(&my_charset_bin); }
858
 
  Item_sum_hybrid(Session *session, Item_sum_hybrid *item);
859
 
  bool fix_fields(Session *, Item **);
 
844
  Item_sum_hybrid(THD *thd, Item_sum_hybrid *item);
 
845
  bool fix_fields(THD *, Item **);
860
846
  void clear();
861
847
  double val_real();
862
848
  int64_t val_int();
863
 
  type::Decimal *val_decimal(type::Decimal *);
 
849
  my_decimal *val_decimal(my_decimal *);
864
850
  void reset_field();
865
851
  String *val_str(String *);
866
852
  bool keep_field_type(void) const { return 1; }
874
860
  void cleanup();
875
861
  bool any_value() { return was_values; }
876
862
  void no_rows_in_result();
877
 
  Field *create_tmp_field(bool group, Table *table,
878
 
                          uint32_t convert_blob_length);
 
863
  Field *create_tmp_field(bool group, TABLE *table,
 
864
                          uint convert_blob_length);
879
865
};
880
866
 
881
867
 
883
869
{
884
870
public:
885
871
  Item_sum_min(Item *item_par) :Item_sum_hybrid(item_par,1) {}
886
 
  Item_sum_min(Session *session, Item_sum_min *item) :Item_sum_hybrid(session, item) {}
 
872
  Item_sum_min(THD *thd, Item_sum_min *item) :Item_sum_hybrid(thd, item) {}
887
873
  enum Sumfunctype sum_func () const {return MIN_FUNC;}
888
874
 
889
875
  bool add();
890
876
  const char *func_name() const { return "min("; }
891
 
  Item *copy_or_same(Session* session);
 
877
  Item *copy_or_same(THD* thd);
892
878
};
893
879
 
894
880
 
896
882
{
897
883
public:
898
884
  Item_sum_max(Item *item_par) :Item_sum_hybrid(item_par,-1) {}
899
 
  Item_sum_max(Session *session, Item_sum_max *item) :Item_sum_hybrid(session, item) {}
 
885
  Item_sum_max(THD *thd, Item_sum_max *item) :Item_sum_hybrid(thd, item) {}
900
886
  enum Sumfunctype sum_func () const {return MAX_FUNC;}
901
887
 
902
888
  bool add();
903
889
  const char *func_name() const { return "max("; }
904
 
  Item *copy_or_same(Session* session);
 
890
  Item *copy_or_same(THD* thd);
905
891
};
906
892
 
907
893
 
913
899
public:
914
900
  Item_sum_bit(Item *item_par,uint64_t reset_arg)
915
901
    :Item_sum_int(item_par),reset_bits(reset_arg),bits(reset_arg) {}
916
 
  Item_sum_bit(Session *session, Item_sum_bit *item):
917
 
    Item_sum_int(session, item), reset_bits(item->reset_bits), bits(item->bits) {}
 
902
  Item_sum_bit(THD *thd, Item_sum_bit *item):
 
903
    Item_sum_int(thd, item), reset_bits(item->reset_bits), bits(item->bits) {}
918
904
  enum Sumfunctype sum_func () const {return SUM_BIT_FUNC;}
919
905
  void clear();
920
906
  int64_t val_int();
933
919
class Item_sum_or :public Item_sum_bit
934
920
{
935
921
public:
936
 
  Item_sum_or(Item *item_par) :Item_sum_bit(item_par,0) {}
937
 
  Item_sum_or(Session *session, Item_sum_or *item) :Item_sum_bit(session, item) {}
 
922
  Item_sum_or(Item *item_par) :Item_sum_bit(item_par,0LL) {}
 
923
  Item_sum_or(THD *thd, Item_sum_or *item) :Item_sum_bit(thd, item) {}
938
924
  bool add();
939
925
  const char *func_name() const { return "bit_or("; }
940
 
  Item *copy_or_same(Session* session);
 
926
  Item *copy_or_same(THD* thd);
941
927
};
942
928
 
943
929
 
945
931
{
946
932
  public:
947
933
  Item_sum_and(Item *item_par) :Item_sum_bit(item_par, UINT64_MAX) {}
948
 
  Item_sum_and(Session *session, Item_sum_and *item) :Item_sum_bit(session, item) {}
 
934
  Item_sum_and(THD *thd, Item_sum_and *item) :Item_sum_bit(thd, item) {}
949
935
  bool add();
950
936
  const char *func_name() const { return "bit_and("; }
951
 
  Item *copy_or_same(Session* session);
 
937
  Item *copy_or_same(THD* thd);
952
938
};
953
939
 
954
940
class Item_sum_xor :public Item_sum_bit
955
941
{
956
942
  public:
957
 
  Item_sum_xor(Item *item_par) :Item_sum_bit(item_par,0) {}
958
 
  Item_sum_xor(Session *session, Item_sum_xor *item) :Item_sum_bit(session, item) {}
 
943
  Item_sum_xor(Item *item_par) :Item_sum_bit(item_par,0LL) {}
 
944
  Item_sum_xor(THD *thd, Item_sum_xor *item) :Item_sum_bit(thd, item) {}
959
945
  bool add();
960
946
  const char *func_name() const { return "bit_xor("; }
961
 
  Item *copy_or_same(Session* session);
962
 
};
963
 
 
964
 
 
965
 
 
966
 
class DRIZZLE_ERROR;
 
947
  Item *copy_or_same(THD* thd);
 
948
};
 
949
 
 
950
 
 
951
/*
 
952
  User defined aggregates
 
953
*/
 
954
 
 
955
class Item_udf_sum : public Item_sum
 
956
{
 
957
protected:
 
958
  udf_handler udf;
 
959
 
 
960
public:
 
961
  Item_udf_sum(udf_func *udf_arg)
 
962
    :Item_sum(), udf(udf_arg)
 
963
  { quick_group=0; }
 
964
  Item_udf_sum(udf_func *udf_arg, List<Item> &list)
 
965
    :Item_sum(list), udf(udf_arg)
 
966
  { quick_group=0;}
 
967
  Item_udf_sum(THD *thd, Item_udf_sum *item)
 
968
    :Item_sum(thd, item), udf(item->udf)
 
969
  { udf.not_original= true; }
 
970
  const char *func_name() const { return udf.name(); }
 
971
  bool fix_fields(THD *thd, Item **ref)
 
972
  {
 
973
    assert(fixed == 0);
 
974
 
 
975
    if (init_sum_func_check(thd))
 
976
      return true;
 
977
 
 
978
    fixed= 1;
 
979
    if (udf.fix_fields(thd, this, this->arg_count, this->args))
 
980
      return true;
 
981
 
 
982
    return check_sum_func(thd, ref);
 
983
  }
 
984
  enum Sumfunctype sum_func () const { return UDF_SUM_FUNC; }
 
985
  virtual bool have_field_update(void) const { return 0; }
 
986
 
 
987
  void clear();
 
988
  bool add();
 
989
  void reset_field() {};
 
990
  void update_field() {};
 
991
  void cleanup();
 
992
  virtual void print(String *str, enum_query_type query_type);
 
993
};
 
994
 
 
995
 
 
996
class Item_sum_udf_float :public Item_udf_sum
 
997
{
 
998
 public:
 
999
  Item_sum_udf_float(udf_func *udf_arg)
 
1000
    :Item_udf_sum(udf_arg) {}
 
1001
  Item_sum_udf_float(udf_func *udf_arg, List<Item> &list)
 
1002
    :Item_udf_sum(udf_arg, list) {}
 
1003
  Item_sum_udf_float(THD *thd, Item_sum_udf_float *item)
 
1004
    :Item_udf_sum(thd, item) {}
 
1005
  int64_t val_int()
 
1006
  {
 
1007
    assert(fixed == 1);
 
1008
    return (int64_t) rint(Item_sum_udf_float::val_real());
 
1009
  }
 
1010
  double val_real();
 
1011
  String *val_str(String*str);
 
1012
  my_decimal *val_decimal(my_decimal *);
 
1013
  void fix_length_and_dec() { fix_num_length_and_dec(); }
 
1014
  Item *copy_or_same(THD* thd);
 
1015
};
 
1016
 
 
1017
 
 
1018
class Item_sum_udf_int :public Item_udf_sum
 
1019
{
 
1020
public:
 
1021
  Item_sum_udf_int(udf_func *udf_arg)
 
1022
    :Item_udf_sum(udf_arg) {}
 
1023
  Item_sum_udf_int(udf_func *udf_arg, List<Item> &list)
 
1024
    :Item_udf_sum(udf_arg, list) {}
 
1025
  Item_sum_udf_int(THD *thd, Item_sum_udf_int *item)
 
1026
    :Item_udf_sum(thd, item) {}
 
1027
  int64_t val_int();
 
1028
  double val_real()
 
1029
    { assert(fixed == 1); return (double) Item_sum_udf_int::val_int(); }
 
1030
  String *val_str(String*str);
 
1031
  my_decimal *val_decimal(my_decimal *);
 
1032
  enum Item_result result_type () const { return INT_RESULT; }
 
1033
  void fix_length_and_dec() { decimals=0; max_length=21; }
 
1034
  Item *copy_or_same(THD* thd);
 
1035
};
 
1036
 
 
1037
 
 
1038
class Item_sum_udf_str :public Item_udf_sum
 
1039
{
 
1040
public:
 
1041
  Item_sum_udf_str(udf_func *udf_arg)
 
1042
    :Item_udf_sum(udf_arg) {}
 
1043
  Item_sum_udf_str(udf_func *udf_arg, List<Item> &list)
 
1044
    :Item_udf_sum(udf_arg,list) {}
 
1045
  Item_sum_udf_str(THD *thd, Item_sum_udf_str *item)
 
1046
    :Item_udf_sum(thd, item) {}
 
1047
  String *val_str(String *);
 
1048
  double val_real()
 
1049
  {
 
1050
    int err_not_used;
 
1051
    char *end_not_used;
 
1052
    String *res;
 
1053
    res=val_str(&str_value);
 
1054
    return res ? my_strntod(res->charset(),(char*) res->ptr(),res->length(),
 
1055
                            &end_not_used, &err_not_used) : 0.0;
 
1056
  }
 
1057
  int64_t val_int()
 
1058
  {
 
1059
    int err_not_used;
 
1060
    char *end;
 
1061
    String *res;
 
1062
    CHARSET_INFO *cs;
 
1063
 
 
1064
    if (!(res= val_str(&str_value)))
 
1065
      return 0;                                 /* Null value */
 
1066
    cs= res->charset();
 
1067
    end= (char*) res->ptr()+res->length();
 
1068
    return cs->cset->strtoll10(cs, res->ptr(), &end, &err_not_used);
 
1069
  }
 
1070
  my_decimal *val_decimal(my_decimal *dec);
 
1071
  enum Item_result result_type () const { return STRING_RESULT; }
 
1072
  void fix_length_and_dec();
 
1073
  Item *copy_or_same(THD* thd);
 
1074
};
 
1075
 
 
1076
 
 
1077
class Item_sum_udf_decimal :public Item_udf_sum
 
1078
{
 
1079
public:
 
1080
  Item_sum_udf_decimal(udf_func *udf_arg)
 
1081
    :Item_udf_sum(udf_arg) {}
 
1082
  Item_sum_udf_decimal(udf_func *udf_arg, List<Item> &list)
 
1083
    :Item_udf_sum(udf_arg, list) {}
 
1084
  Item_sum_udf_decimal(THD *thd, Item_sum_udf_decimal *item)
 
1085
    :Item_udf_sum(thd, item) {}
 
1086
  String *val_str(String *);
 
1087
  double val_real();
 
1088
  int64_t val_int();
 
1089
  my_decimal *val_decimal(my_decimal *);
 
1090
  enum Item_result result_type () const { return DECIMAL_RESULT; }
 
1091
  void fix_length_and_dec() { fix_num_length_and_dec(); }
 
1092
  Item *copy_or_same(THD* thd);
 
1093
};
 
1094
 
 
1095
class MYSQL_ERROR;
967
1096
 
968
1097
class Item_func_group_concat : public Item_sum
969
1098
{
970
 
  Tmp_Table_Param *tmp_table_param;
971
 
  DRIZZLE_ERROR *warning;
 
1099
  TMP_TABLE_PARAM *tmp_table_param;
 
1100
  MYSQL_ERROR *warning;
972
1101
  String result;
973
1102
  String *separator;
974
1103
  TREE tree_base;
976
1105
 
977
1106
  /**
978
1107
     If DISTINCT is used with this GROUP_CONCAT, this member is used to filter
979
 
     out duplicates.
 
1108
     out duplicates. 
980
1109
     @see Item_func_group_concat::setup
981
1110
     @see Item_func_group_concat::add
982
1111
     @see Item_func_group_concat::clear
983
1112
   */
984
1113
  Unique *unique_filter;
985
 
  Table *table;
986
 
  Order **order;
 
1114
  TABLE *table;
 
1115
  ORDER **order;
987
1116
  Name_resolution_context *context;
988
1117
  /** The number of ORDER BY items. */
989
 
  uint32_t arg_count_order;
 
1118
  uint arg_count_order;
990
1119
  /** The number of selected items, aka the expr list. */
991
 
  uint32_t arg_count_field;
992
 
  uint32_t count_cut_values;
 
1120
  uint arg_count_field;
 
1121
  uint count_cut_values;
993
1122
  bool distinct;
994
1123
  bool warning_for_row;
995
1124
  bool always_null;
1005
1134
                                                const void* key2);
1006
1135
  friend int group_concat_key_cmp_with_order(void* arg, const void* key1,
1007
1136
                                             const void* key2);
1008
 
  friend int dump_leaf_key(unsigned char* key, uint32_t,
1009
 
                           Item_func_group_concat *group_concat_item);
 
1137
  friend int dump_leaf_key(uchar* key,
 
1138
                           element_count count __attribute__((unused)),
 
1139
                           Item_func_group_concat *group_concat_item);
1010
1140
 
1011
1141
public:
1012
1142
  Item_func_group_concat(Name_resolution_context *context_arg,
1013
1143
                         bool is_distinct, List<Item> *is_select,
1014
1144
                         SQL_LIST *is_order, String *is_separator);
1015
1145
 
1016
 
  Item_func_group_concat(Session *session, Item_func_group_concat *item);
 
1146
  Item_func_group_concat(THD *thd, Item_func_group_concat *item);
1017
1147
  ~Item_func_group_concat();
1018
1148
  void cleanup();
1019
1149
 
1023
1153
  enum_field_types field_type() const
1024
1154
  {
1025
1155
    if (max_length/collation.collation->mbmaxlen > CONVERT_IF_BIGGER_TO_BLOB )
1026
 
      return DRIZZLE_TYPE_BLOB;
 
1156
      return MYSQL_TYPE_BLOB;
1027
1157
    else
1028
 
      return DRIZZLE_TYPE_VARCHAR;
 
1158
      return MYSQL_TYPE_VARCHAR;
1029
1159
  }
1030
1160
  void clear();
1031
1161
  bool add();
1032
1162
  void reset_field() { assert(0); }        // not used
1033
1163
  void update_field() { assert(0); }       // not used
1034
 
  bool fix_fields(Session *,Item **);
1035
 
  bool setup(Session *session);
 
1164
  bool fix_fields(THD *,Item **);
 
1165
  bool setup(THD *thd);
1036
1166
  void make_unique();
1037
 
  double val_real();
1038
 
  int64_t val_int();
1039
 
  type::Decimal *val_decimal(type::Decimal *decimal_value)
 
1167
  double val_real()
 
1168
  {
 
1169
    String *res;  res=val_str(&str_value);
 
1170
    return res ? my_atof(res->c_ptr()) : 0.0;
 
1171
  }
 
1172
  int64_t val_int()
 
1173
  {
 
1174
    String *res;
 
1175
    char *end_ptr;
 
1176
    int error;
 
1177
    if (!(res= val_str(&str_value)))
 
1178
      return (int64_t) 0;
 
1179
    end_ptr= (char*) res->ptr()+ res->length();
 
1180
    return my_strtoll10(res->ptr(), &end_ptr, &error);
 
1181
  }
 
1182
  my_decimal *val_decimal(my_decimal *decimal_value)
1040
1183
  {
1041
1184
    return val_decimal_from_string(decimal_value);
1042
1185
  }
1043
1186
  String* val_str(String* str);
1044
 
  Item *copy_or_same(Session* session);
 
1187
  Item *copy_or_same(THD* thd);
1045
1188
  void no_rows_in_result() {}
1046
1189
  virtual void print(String *str, enum_query_type query_type);
1047
 
  virtual bool change_context_processor(unsigned char *cntx)
 
1190
  virtual bool change_context_processor(uchar *cntx)
1048
1191
    { context= (Name_resolution_context *)cntx; return false; }
1049
1192
};
1050
 
 
1051
 
} /* namespace drizzled */
1052
 
 
1053
 
#endif /* DRIZZLED_ITEM_SUM_H */