~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to sql/item_sum.h

  • Committer: Brian Aker
  • Date: 2008-07-08 16:17:31 UTC
  • Revision ID: brian@tangent.org-20080708161731-io36j7igglok79py
DATE cleanup.

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/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
 
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_st;
 
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
 
  Item_sum() :arg_count(0), quick_group(1), forced_const(false)
 
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),
279
 
    forced_const(false)
 
261
  Item_sum(Item *a) :args(tmp_args), arg_count(1), quick_group(1), 
 
262
    forced_const(FALSE)
280
263
  {
281
264
    args[0]=a;
282
265
    mark_as_sum_func();
283
266
  }
284
267
  Item_sum( Item *a, Item *b ) :args(tmp_args), arg_count(2), quick_group(1),
285
 
    forced_const(false)
 
268
    forced_const(FALSE)
286
269
  {
287
270
    args[0]=a; args[1]=b;
288
271
    mark_as_sum_func();
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
 
  void make_field(SendField *field);
 
352
  void make_field(Send_field *field);
370
353
  virtual void print(String *str, enum_query_type query_type);
371
354
  void fix_num_length_and_dec();
372
355
 
380
363
  */
381
364
  void no_rows_in_result() { clear(); }
382
365
 
383
 
  virtual bool setup(Session *) {return 0;}
 
366
  virtual bool setup(THD *thd __attribute__((__unused__))) {return 0;}
384
367
  virtual void make_unique(void) {}
385
 
  Item *get_tmp_table_item(Session *session);
386
 
  virtual Field *create_tmp_field(bool group, Table *table,
387
 
                                  uint32_t convert_blob_length);
388
 
  bool walk(Item_processor processor, bool walk_subquery, unsigned char *argument);
389
 
  bool init_sum_func_check(Session *session);
390
 
  bool check_sum_func(Session *session, Item **ref);
391
 
  bool register_sum_func(Session *session, Item **ref);
392
 
  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() 
393
376
    { return (nest_level == aggr_level ? 0 : aggr_sel); }
394
377
};
395
378
 
398
381
{
399
382
protected:
400
383
  /*
401
 
   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 
402
385
   query. Derived classes that require extensive calculation in val_xxx()
403
 
   maintain cache of aggregate value. This variable governs the validity of
 
386
   maintain cache of aggregate value. This variable governs the validity of 
404
387
   that cache.
405
388
  */
406
389
  bool is_evaluated;
407
390
public:
408
 
  Item_sum_num() :Item_sum(),is_evaluated(false) {}
409
 
  Item_sum_num(Item *item_par)
410
 
    :Item_sum(item_par), is_evaluated(false) {}
411
 
  Item_sum_num(Item *a, Item* b) :Item_sum(a,b),is_evaluated(false) {}
412
 
  Item_sum_num(List<Item> &list)
413
 
    :Item_sum(list), is_evaluated(false) {}
414
 
  Item_sum_num(Session *session, Item_sum_num *item)
415
 
    :Item_sum(session, item),is_evaluated(item->is_evaluated) {}
416
 
  bool fix_fields(Session *, Item **);
417
 
  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
  }
418
405
  String *val_str(String*str);
419
406
  my_decimal *val_decimal(my_decimal *);
420
407
  void reset_field();
426
413
public:
427
414
  Item_sum_int(Item *item_par) :Item_sum_num(item_par) {}
428
415
  Item_sum_int(List<Item> &list) :Item_sum_num(list) {}
429
 
  Item_sum_int(Session *session, Item_sum_int *item) :Item_sum_num(session, item) {}
430
 
  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(); }
431
418
  String *val_str(String*str);
432
419
  my_decimal *val_decimal(my_decimal *);
433
420
  enum Item_result result_type () const { return INT_RESULT; }
442
429
  Item_result hybrid_type;
443
430
  double sum;
444
431
  my_decimal dec_buffs[2];
445
 
  uint32_t curr_dec_buff;
 
432
  uint curr_dec_buff;
446
433
  void fix_length_and_dec();
447
434
 
448
435
public:
449
436
  Item_sum_sum(Item *item_par) :Item_sum_num(item_par) {}
450
 
  Item_sum_sum(Session *session, Item_sum_sum *item);
 
437
  Item_sum_sum(THD *thd, Item_sum_sum *item);
451
438
  enum Sumfunctype sum_func () const {return SUM_FUNC;}
452
439
  void clear();
453
440
  bool add();
454
441
  double val_real();
455
 
  int64_t val_int();
 
442
  longlong val_int();
456
443
  String *val_str(String*str);
457
444
  my_decimal *val_decimal(my_decimal *);
458
445
  enum Item_result result_type () const { return hybrid_type; }
460
447
  void update_field();
461
448
  void no_rows_in_result() {}
462
449
  const char *func_name() const { return "sum("; }
463
 
  Item *copy_or_same(Session* session);
 
450
  Item *copy_or_same(THD* thd);
464
451
};
465
452
 
466
453
 
473
460
{
474
461
protected:
475
462
  /* storage for the summation result */
476
 
  uint64_t count;
 
463
  ulonglong count;
477
464
  Hybrid_type val;
478
465
  /* storage for unique elements */
479
466
  Unique *tree;
480
 
  Table *table;
 
467
  TABLE *table;
481
468
  enum enum_field_types table_field_type;
482
 
  uint32_t tree_key_length;
 
469
  uint tree_key_length;
483
470
protected:
484
 
  Item_sum_distinct(Session *session, Item_sum_distinct *item);
 
471
  Item_sum_distinct(THD *thd, Item_sum_distinct *item);
485
472
public:
486
473
  Item_sum_distinct(Item *item_par);
487
474
  ~Item_sum_distinct();
488
475
 
489
 
  bool setup(Session *session);
 
476
  bool setup(THD *thd);
490
477
  void clear();
491
478
  void cleanup();
492
479
  bool add();
493
480
  double val_real();
494
481
  my_decimal *val_decimal(my_decimal *);
495
 
  int64_t val_int();
 
482
  longlong val_int();
496
483
  String *val_str(String *str);
497
484
 
498
485
  /* XXX: does it need make_unique? */
502
489
  void update_field() {} // not used
503
490
  virtual void no_rows_in_result() {}
504
491
  void fix_length_and_dec();
505
 
  enum Item_result result_type () const;
 
492
  enum Item_result result_type () const { return val.traits->type(); }
506
493
  virtual void calculate_val_and_count();
507
494
  virtual bool unique_walk_function(void *elem);
508
495
};
517
504
class Item_sum_sum_distinct :public Item_sum_distinct
518
505
{
519
506
private:
520
 
  Item_sum_sum_distinct(Session *session, Item_sum_sum_distinct *item)
521
 
    :Item_sum_distinct(session, item) {}
 
507
  Item_sum_sum_distinct(THD *thd, Item_sum_sum_distinct *item)
 
508
    :Item_sum_distinct(thd, item) {}
522
509
public:
523
510
  Item_sum_sum_distinct(Item *item_arg) :Item_sum_distinct(item_arg) {}
524
511
 
525
512
  enum Sumfunctype sum_func () const { return SUM_DISTINCT_FUNC; }
526
513
  const char *func_name() const { return "sum(distinct "; }
527
 
  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); }
528
515
};
529
516
 
530
517
 
533
520
class Item_sum_avg_distinct: public Item_sum_distinct
534
521
{
535
522
private:
536
 
  Item_sum_avg_distinct(Session *session, Item_sum_avg_distinct *original)
537
 
    :Item_sum_distinct(session, original) {}
 
523
  Item_sum_avg_distinct(THD *thd, Item_sum_avg_distinct *original)
 
524
    :Item_sum_distinct(thd, original) {}
538
525
public:
539
 
  uint32_t prec_increment;
 
526
  uint prec_increment;
540
527
  Item_sum_avg_distinct(Item *item_arg) : Item_sum_distinct(item_arg) {}
541
528
 
542
529
  void fix_length_and_dec();
543
530
  virtual void calculate_val_and_count();
544
531
  enum Sumfunctype sum_func () const { return AVG_DISTINCT_FUNC; }
545
532
  const char *func_name() const { return "avg(distinct "; }
546
 
  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); }
547
534
};
548
535
 
549
536
 
550
537
class Item_sum_count :public Item_sum_int
551
538
{
552
 
  int64_t count;
 
539
  longlong count;
553
540
 
554
541
  public:
555
542
  Item_sum_count(Item *item_par)
556
543
    :Item_sum_int(item_par),count(0)
557
544
  {}
558
 
  Item_sum_count(Session *session, Item_sum_count *item)
559
 
    :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)
560
547
  {}
561
548
  enum Sumfunctype sum_func () const { return COUNT_FUNC; }
562
549
  void clear();
563
550
  void no_rows_in_result() { count=0; }
564
551
  bool add();
565
 
  void make_const_count(int64_t count_arg)
566
 
  {
 
552
  void make_const(longlong count_arg) 
 
553
  { 
567
554
    count=count_arg;
568
555
    Item_sum::make_const();
569
556
  }
570
 
  int64_t val_int();
 
557
  longlong val_int();
571
558
  void reset_field();
572
559
  void cleanup();
573
560
  void update_field();
574
561
  const char *func_name() const { return "count("; }
575
 
  Item *copy_or_same(Session* session);
 
562
  Item *copy_or_same(THD* thd);
576
563
};
577
564
 
578
565
 
579
 
class Tmp_Table_Param;
 
566
class TMP_TABLE_PARAM;
580
567
 
581
568
class Item_sum_count_distinct :public Item_sum_int
582
569
{
583
 
  Table *table;
584
 
  uint32_t *field_lengths;
585
 
  Tmp_Table_Param *tmp_table_param;
 
570
  TABLE *table;
 
571
  uint32 *field_lengths;
 
572
  TMP_TABLE_PARAM *tmp_table_param;
586
573
  bool force_copy_fields;
587
574
  /*
588
575
    If there are no blobs, we can use a tree, which
595
582
   will not recalculate on each call. Validitiy of the value is stored in
596
583
   is_evaluated.
597
584
  */
598
 
  int64_t count;
 
585
  longlong count;
599
586
  /*
600
 
    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 
601
588
    (to correctly free resources)
602
589
  */
603
590
  Item_sum_count_distinct *original;
604
 
  uint32_t tree_key_length;
 
591
  uint tree_key_length;
605
592
 
606
593
 
607
594
  bool always_null;             // Set to 1 if the result is always NULL
608
595
 
609
596
 
610
 
  friend int composite_key_cmp(void* arg, unsigned char* key1, unsigned char* key2);
611
 
  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);
612
599
 
613
600
public:
614
601
  Item_sum_count_distinct(List<Item> &list)
615
602
    :Item_sum_int(list), table(0), field_lengths(0), tmp_table_param(0),
616
603
     force_copy_fields(0), tree(0), count(0),
617
 
     original(0), always_null(false)
 
604
     original(0), always_null(FALSE)
618
605
  { quick_group= 0; }
619
 
  Item_sum_count_distinct(Session *session, Item_sum_count_distinct *item)
620
 
    :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),
621
608
     field_lengths(item->field_lengths),
622
609
     tmp_table_param(item->tmp_table_param),
623
 
     force_copy_fields(0), tree(item->tree), count(item->count),
 
610
     force_copy_fields(0), tree(item->tree), count(item->count), 
624
611
     original(item), tree_key_length(item->tree_key_length),
625
612
     always_null(item->always_null)
626
613
  {}
631
618
  enum Sumfunctype sum_func () const { return COUNT_DISTINCT_FUNC; }
632
619
  void clear();
633
620
  bool add();
634
 
  int64_t val_int();
 
621
  longlong val_int();
635
622
  void reset_field() { return ;}                // Never called
636
623
  void update_field() { return ; }              // Never called
637
624
  const char *func_name() const { return "count(distinct "; }
638
 
  bool setup(Session *session);
 
625
  bool setup(THD *thd);
639
626
  void make_unique();
640
 
  Item *copy_or_same(Session* session);
 
627
  Item *copy_or_same(THD* thd);
641
628
  void no_rows_in_result() {}
642
629
};
643
630
 
651
638
public:
652
639
  Field *field;
653
640
  Item_result hybrid_type;
654
 
  uint32_t f_precision, f_scale, dec_bin_size;
655
 
  uint32_t prec_increment;
 
641
  uint f_precision, f_scale, dec_bin_size;
 
642
  uint prec_increment;
656
643
  Item_avg_field(Item_result res_type, Item_sum_avg *item);
657
644
  enum Type type() const { return FIELD_AVG_ITEM; }
658
645
  double val_real();
659
 
  int64_t val_int();
 
646
  longlong val_int();
660
647
  my_decimal *val_decimal(my_decimal *);
661
648
  bool is_null() { update_null_value(); return null_value; }
662
649
  String *val_str(String*);
663
650
  enum_field_types field_type() const
664
651
  {
665
652
    return hybrid_type == DECIMAL_RESULT ?
666
 
      DRIZZLE_TYPE_DECIMAL : DRIZZLE_TYPE_DOUBLE;
 
653
      MYSQL_TYPE_NEWDECIMAL : MYSQL_TYPE_DOUBLE;
667
654
  }
668
655
  void fix_length_and_dec() {}
669
656
  enum Item_result result_type () const { return hybrid_type; }
673
660
class Item_sum_avg :public Item_sum_sum
674
661
{
675
662
public:
676
 
  uint64_t count;
677
 
  uint32_t prec_increment;
678
 
  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;
679
666
 
680
667
  Item_sum_avg(Item *item_par) :Item_sum_sum(item_par), count(0) {}
681
 
  Item_sum_avg(Session *session, Item_sum_avg *item)
682
 
    :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),
683
670
    prec_increment(item->prec_increment) {}
684
671
 
685
672
  void fix_length_and_dec();
688
675
  bool add();
689
676
  double val_real();
690
677
  // In SPs we might force the "wrong" type with select into a declare variable
691
 
  int64_t val_int();
 
678
  longlong val_int() { return (longlong) rint(val_real()); }
692
679
  my_decimal *val_decimal(my_decimal *);
693
680
  String *val_str(String *str);
694
681
  void reset_field();
695
682
  void update_field();
696
 
  Item *result_item(Field *)
 
683
  Item *result_item(Field *field __attribute__((__unused__)))
697
684
  { return new Item_avg_field(hybrid_type, this); }
698
685
  void no_rows_in_result() {}
699
686
  const char *func_name() const { return "avg("; }
700
 
  Item *copy_or_same(Session* session);
701
 
  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);
702
689
  void cleanup()
703
690
  {
704
691
    count= 0;
713
700
public:
714
701
  Field *field;
715
702
  Item_result hybrid_type;
716
 
  uint32_t f_precision0, f_scale0;
717
 
  uint32_t f_precision1, f_scale1;
718
 
  uint32_t dec_bin_size0, dec_bin_size1;
719
 
  uint32_t sample;
720
 
  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;
721
708
  Item_variance_field(Item_sum_variance *item);
722
709
  enum Type type() const {return FIELD_VARIANCE_ITEM; }
723
710
  double val_real();
724
 
  int64_t val_int();
 
711
  longlong val_int()
 
712
  { /* can't be fix_fields()ed */ return (longlong) rint(val_real()); }
725
713
  String *val_str(String *str)
726
714
  { return val_string_from_real(str); }
727
715
  my_decimal *val_decimal(my_decimal *dec_buf)
730
718
  enum_field_types field_type() const
731
719
  {
732
720
    return hybrid_type == DECIMAL_RESULT ?
733
 
      DRIZZLE_TYPE_DECIMAL : DRIZZLE_TYPE_DOUBLE;
 
721
      MYSQL_TYPE_NEWDECIMAL : MYSQL_TYPE_DOUBLE;
734
722
  }
735
723
  void fix_length_and_dec() {}
736
724
  enum Item_result result_type () const { return hybrid_type; }
742
730
 
743
731
  =  sum (ai - avg(a))^2 / count(a) )
744
732
  =  sum (ai^2 - 2*ai*avg(a) + avg(a)^2) / count(a)
745
 
  =  (sum(ai^2) - sum(2*ai*avg(a)) + sum(avg(a)^2))/count(a) =
746
 
  =  (sum(ai^2) - 2*avg(a)*sum(a) + count(a)*avg(a)^2)/count(a) =
747
 
  =  (sum(ai^2) - 2*sum(a)*sum(a)/count(a) + count(a)*sum(a)^2/count(a)^2 )/count(a) =
748
 
  =  (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) = 
749
737
  =  (sum(ai^2) - sum(a)^2/count(a))/count(a)
750
738
 
751
739
But, this falls prey to catastrophic cancellation.  Instead, use the recurrence formulas
752
740
 
753
 
  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 
754
742
  S_{1} = 0, ~ S_{k} = S_{k-1} + (x_{k} - M_{k-1}) times (x_{k} - M_{k}) newline
755
743
  for 2 <= k <= n newline
756
744
  ital variance = S_{n} / (n-1)
765
753
  Item_result hybrid_type;
766
754
  int cur_dec;
767
755
  double recurrence_m, recurrence_s;    /* Used in recurrence relation. */
768
 
  uint64_t count;
769
 
  uint32_t f_precision0, f_scale0;
770
 
  uint32_t f_precision1, f_scale1;
771
 
  uint32_t dec_bin_size0, dec_bin_size1;
772
 
  uint32_t sample;
773
 
  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;
774
762
 
775
 
  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),
776
764
    hybrid_type(REAL_RESULT), count(0), sample(sample_arg)
777
765
    {}
778
 
  Item_sum_variance(Session *session, Item_sum_variance *item);
 
766
  Item_sum_variance(THD *thd, Item_sum_variance *item);
779
767
  enum Sumfunctype sum_func () const { return VARIANCE_FUNC; }
780
768
  void clear();
781
769
  bool add();
782
770
  double val_real();
783
 
  int64_t val_int();
784
771
  my_decimal *val_decimal(my_decimal *);
785
772
  void reset_field();
786
773
  void update_field();
787
 
  Item *result_item(Field *)
 
774
  Item *result_item(Field *field __attribute__((__unused__)))
788
775
  { return new Item_variance_field(this); }
789
776
  void no_rows_in_result() {}
790
777
  const char *func_name() const
791
778
    { return sample ? "var_samp(" : "variance("; }
792
 
  Item *copy_or_same(Session* session);
793
 
  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);
794
781
  enum Item_result result_type () const { return REAL_RESULT; }
795
782
  void cleanup()
796
783
  {
809
796
  double val_real();
810
797
  my_decimal *val_decimal(my_decimal *);
811
798
  enum Item_result result_type () const { return REAL_RESULT; }
812
 
  enum_field_types field_type() const { return DRIZZLE_TYPE_DOUBLE;}
 
799
  enum_field_types field_type() const { return MYSQL_TYPE_DOUBLE;}
813
800
};
814
801
 
815
802
/*
819
806
class Item_sum_std :public Item_sum_variance
820
807
{
821
808
  public:
822
 
  Item_sum_std(Item *item_par, uint32_t sample_arg)
 
809
  Item_sum_std(Item *item_par, uint sample_arg)
823
810
    :Item_sum_variance(item_par, sample_arg) {}
824
 
  Item_sum_std(Session *session, Item_sum_std *item)
825
 
    :Item_sum_variance(session, item)
 
811
  Item_sum_std(THD *thd, Item_sum_std *item)
 
812
    :Item_sum_variance(thd, item)
826
813
    {}
827
814
  enum Sumfunctype sum_func () const { return STD_FUNC; }
828
815
  double val_real();
829
 
  Item *result_item(Field *)
 
816
  Item *result_item(Field *field __attribute__((__unused__)))
830
817
    { return new Item_std_field(this); }
831
818
  const char *func_name() const { return "std("; }
832
 
  Item *copy_or_same(Session* session);
 
819
  Item *copy_or_same(THD* thd);
833
820
  enum Item_result result_type () const { return REAL_RESULT; }
834
 
  enum_field_types field_type() const { return DRIZZLE_TYPE_DOUBLE;}
 
821
  enum_field_types field_type() const { return MYSQL_TYPE_DOUBLE;}
835
822
};
836
823
 
837
824
// This class is a string or number function depending on num_func
841
828
protected:
842
829
  String value,tmp_value;
843
830
  double sum;
844
 
  int64_t sum_int;
 
831
  longlong sum_int;
845
832
  my_decimal sum_dec;
846
833
  Item_result hybrid_type;
847
834
  enum_field_types hybrid_field_type;
851
838
  public:
852
839
  Item_sum_hybrid(Item *item_par,int sign)
853
840
    :Item_sum(item_par), sum(0.0), sum_int(0),
854
 
    hybrid_type(INT_RESULT), hybrid_field_type(DRIZZLE_TYPE_LONGLONG),
855
 
    cmp_sign(sign), was_values(true)
 
841
    hybrid_type(INT_RESULT), hybrid_field_type(MYSQL_TYPE_LONGLONG),
 
842
    cmp_sign(sign), was_values(TRUE)
856
843
  { collation.set(&my_charset_bin); }
857
 
  Item_sum_hybrid(Session *session, Item_sum_hybrid *item);
858
 
  bool fix_fields(Session *, Item **);
 
844
  Item_sum_hybrid(THD *thd, Item_sum_hybrid *item);
 
845
  bool fix_fields(THD *, Item **);
859
846
  void clear();
860
847
  double val_real();
861
 
  int64_t val_int();
 
848
  longlong val_int();
862
849
  my_decimal *val_decimal(my_decimal *);
863
850
  void reset_field();
864
851
  String *val_str(String *);
873
860
  void cleanup();
874
861
  bool any_value() { return was_values; }
875
862
  void no_rows_in_result();
876
 
  Field *create_tmp_field(bool group, Table *table,
877
 
                          uint32_t convert_blob_length);
 
863
  Field *create_tmp_field(bool group, TABLE *table,
 
864
                          uint convert_blob_length);
878
865
};
879
866
 
880
867
 
882
869
{
883
870
public:
884
871
  Item_sum_min(Item *item_par) :Item_sum_hybrid(item_par,1) {}
885
 
  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) {}
886
873
  enum Sumfunctype sum_func () const {return MIN_FUNC;}
887
874
 
888
875
  bool add();
889
876
  const char *func_name() const { return "min("; }
890
 
  Item *copy_or_same(Session* session);
 
877
  Item *copy_or_same(THD* thd);
891
878
};
892
879
 
893
880
 
895
882
{
896
883
public:
897
884
  Item_sum_max(Item *item_par) :Item_sum_hybrid(item_par,-1) {}
898
 
  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) {}
899
886
  enum Sumfunctype sum_func () const {return MAX_FUNC;}
900
887
 
901
888
  bool add();
902
889
  const char *func_name() const { return "max("; }
903
 
  Item *copy_or_same(Session* session);
 
890
  Item *copy_or_same(THD* thd);
904
891
};
905
892
 
906
893
 
907
894
class Item_sum_bit :public Item_sum_int
908
895
{
909
896
protected:
910
 
  uint64_t reset_bits,bits;
 
897
  ulonglong reset_bits,bits;
911
898
 
912
899
public:
913
 
  Item_sum_bit(Item *item_par,uint64_t reset_arg)
 
900
  Item_sum_bit(Item *item_par,ulonglong reset_arg)
914
901
    :Item_sum_int(item_par),reset_bits(reset_arg),bits(reset_arg) {}
915
 
  Item_sum_bit(Session *session, Item_sum_bit *item):
916
 
    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) {}
917
904
  enum Sumfunctype sum_func () const {return SUM_BIT_FUNC;}
918
905
  void clear();
919
 
  int64_t val_int();
 
906
  longlong val_int();
920
907
  void reset_field();
921
908
  void update_field();
922
909
  void fix_length_and_dec()
932
919
class Item_sum_or :public Item_sum_bit
933
920
{
934
921
public:
935
 
  Item_sum_or(Item *item_par) :Item_sum_bit(item_par,0) {}
936
 
  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) {}
937
924
  bool add();
938
925
  const char *func_name() const { return "bit_or("; }
939
 
  Item *copy_or_same(Session* session);
 
926
  Item *copy_or_same(THD* thd);
940
927
};
941
928
 
942
929
 
943
930
class Item_sum_and :public Item_sum_bit
944
931
{
945
932
  public:
946
 
  Item_sum_and(Item *item_par) :Item_sum_bit(item_par, UINT64_MAX) {}
947
 
  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) {}
948
935
  bool add();
949
936
  const char *func_name() const { return "bit_and("; }
950
 
  Item *copy_or_same(Session* session);
 
937
  Item *copy_or_same(THD* thd);
951
938
};
952
939
 
953
940
class Item_sum_xor :public Item_sum_bit
954
941
{
955
942
  public:
956
 
  Item_sum_xor(Item *item_par) :Item_sum_bit(item_par,0) {}
957
 
  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) {}
958
945
  bool add();
959
946
  const char *func_name() const { return "bit_xor("; }
960
 
  Item *copy_or_same(Session* session);
961
 
};
962
 
 
963
 
 
964
 
 
965
 
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;
966
1174
 
967
1175
class Item_func_group_concat : public Item_sum
968
1176
{
969
 
  Tmp_Table_Param *tmp_table_param;
970
 
  DRIZZLE_ERROR *warning;
 
1177
  TMP_TABLE_PARAM *tmp_table_param;
 
1178
  MYSQL_ERROR *warning;
971
1179
  String result;
972
1180
  String *separator;
973
1181
  TREE tree_base;
975
1183
 
976
1184
  /**
977
1185
     If DISTINCT is used with this GROUP_CONCAT, this member is used to filter
978
 
     out duplicates.
 
1186
     out duplicates. 
979
1187
     @see Item_func_group_concat::setup
980
1188
     @see Item_func_group_concat::add
981
1189
     @see Item_func_group_concat::clear
982
1190
   */
983
1191
  Unique *unique_filter;
984
 
  Table *table;
985
 
  order_st **order;
 
1192
  TABLE *table;
 
1193
  ORDER **order;
986
1194
  Name_resolution_context *context;
987
1195
  /** The number of ORDER BY items. */
988
 
  uint32_t arg_count_order;
 
1196
  uint arg_count_order;
989
1197
  /** The number of selected items, aka the expr list. */
990
 
  uint32_t arg_count_field;
991
 
  uint32_t count_cut_values;
 
1198
  uint arg_count_field;
 
1199
  uint count_cut_values;
992
1200
  bool distinct;
993
1201
  bool warning_for_row;
994
1202
  bool always_null;
1004
1212
                                                const void* key2);
1005
1213
  friend int group_concat_key_cmp_with_order(void* arg, const void* key1,
1006
1214
                                             const void* key2);
1007
 
  friend int dump_leaf_key(unsigned char* key, uint32_t,
1008
 
                           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);
1009
1218
 
1010
1219
public:
1011
1220
  Item_func_group_concat(Name_resolution_context *context_arg,
1012
1221
                         bool is_distinct, List<Item> *is_select,
1013
1222
                         SQL_LIST *is_order, String *is_separator);
1014
1223
 
1015
 
  Item_func_group_concat(Session *session, Item_func_group_concat *item);
 
1224
  Item_func_group_concat(THD *thd, Item_func_group_concat *item);
1016
1225
  ~Item_func_group_concat();
1017
1226
  void cleanup();
1018
1227
 
1022
1231
  enum_field_types field_type() const
1023
1232
  {
1024
1233
    if (max_length/collation.collation->mbmaxlen > CONVERT_IF_BIGGER_TO_BLOB )
1025
 
      return DRIZZLE_TYPE_BLOB;
 
1234
      return MYSQL_TYPE_BLOB;
1026
1235
    else
1027
 
      return DRIZZLE_TYPE_VARCHAR;
 
1236
      return MYSQL_TYPE_VARCHAR;
1028
1237
  }
1029
1238
  void clear();
1030
1239
  bool add();
1031
 
  void reset_field() { assert(0); }        // not used
1032
 
  void update_field() { assert(0); }       // not used
1033
 
  bool fix_fields(Session *,Item **);
1034
 
  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);
1035
1244
  void make_unique();
1036
 
  double val_real();
1037
 
  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
  }
1038
1260
  my_decimal *val_decimal(my_decimal *decimal_value)
1039
1261
  {
1040
1262
    return val_decimal_from_string(decimal_value);
1041
1263
  }
1042
1264
  String* val_str(String* str);
1043
 
  Item *copy_or_same(Session* session);
 
1265
  Item *copy_or_same(THD* thd);
1044
1266
  void no_rows_in_result() {}
1045
1267
  virtual void print(String *str, enum_query_type query_type);
1046
 
  virtual bool change_context_processor(unsigned char *cntx)
1047
 
    { context= (Name_resolution_context *)cntx; return false; }
 
1268
  virtual bool change_context_processor(uchar *cntx)
 
1269
    { context= (Name_resolution_context *)cntx; return FALSE; }
1048
1270
};
1049
 
 
1050
 
} /* namespace drizzled */
1051
 
 
1052
 
#endif /* DRIZZLED_ITEM_SUM_H */