~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item/sum.h

  • Committer: Brian Aker
  • Date: 2010-05-27 01:25:56 UTC
  • mfrom: (1567.1.4 new-staging)
  • Revision ID: brian@gaz-20100527012556-5zgkirkl7swbigd6
Merge of Brian, Paul. PBXT compile issue, and test framework cleanup. 

Show diffs side-by-side

added added

removed removed

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