~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to sql/item_sum.h

Merged in changes. 
Edited a the comment test case so deal with our version bump.

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