~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000-2003 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
16
17
/**
18
  @file
19
20
  @brief
21
  Sum functions (COUNT, MIN...)
22
*/
23
24
#ifdef USE_PRAGMA_IMPLEMENTATION
25
#pragma implementation				// gcc: Class implementation
26
#endif
27
28
#include "mysql_priv.h"
29
#include "sql_select.h"
30
31
/**
32
  Prepare an aggregate function item for checking context conditions.
33
34
    The function initializes the members of the Item_sum object created
35
    for a set function that are used to check validity of the set function
36
    occurrence.
37
    If the set function is not allowed in any subquery where it occurs
38
    an error is reported immediately.
39
40
  @param thd      reference to the thread context info
41
42
  @note
43
    This function is to be called for any item created for a set function
44
    object when the traversal of trees built for expressions used in the query
45
    is performed at the phase of context analysis. This function is to
46
    be invoked at the descent of this traversal.
47
  @retval
48
    TRUE   if an error is reported
49
  @retval
50
    FALSE  otherwise
51
*/
52
 
53
bool Item_sum::init_sum_func_check(THD *thd)
54
{
55
  if (!thd->lex->allow_sum_func)
56
  {
57
    my_message(ER_INVALID_GROUP_FUNC_USE, ER(ER_INVALID_GROUP_FUNC_USE),
58
               MYF(0));
59
    return TRUE;
60
  }
61
  /* Set a reference to the nesting set function if there is  any */
62
  in_sum_func= thd->lex->in_sum_func;
63
  /* Save a pointer to object to be used in items for nested set functions */
64
  thd->lex->in_sum_func= this;
65
  nest_level= thd->lex->current_select->nest_level;
66
  ref_by= 0;
67
  aggr_level= -1;
68
  aggr_sel= NULL;
69
  max_arg_level= -1;
70
  max_sum_func_level= -1;
71
  outer_fields.empty();
72
  return FALSE;
73
}
74
75
/**
76
  Check constraints imposed on a usage of a set function.
77
78
    The method verifies whether context conditions imposed on a usage
79
    of any set function are met for this occurrence.
80
    It checks whether the set function occurs in the position where it
81
    can be aggregated and, when it happens to occur in argument of another
82
    set function, the method checks that these two functions are aggregated in
83
    different subqueries.
84
    If the context conditions are not met the method reports an error.
85
    If the set function is aggregated in some outer subquery the method
86
    adds it to the chain of items for such set functions that is attached
87
    to the the st_select_lex structure for this subquery.
88
89
    A number of designated members of the object are used to check the
90
    conditions. They are specified in the comment before the Item_sum
91
    class declaration.
92
    Additionally a bitmap variable called allow_sum_func is employed.
93
    It is included into the thd->lex structure.
94
    The bitmap contains 1 at n-th position if the set function happens
95
    to occur under a construct of the n-th level subquery where usage
96
    of set functions are allowed (i.e either in the SELECT list or
97
    in the HAVING clause of the corresponding subquery)
98
    Consider the query:
99
    @code
100
       SELECT SUM(t1.b) FROM t1 GROUP BY t1.a
101
         HAVING t1.a IN (SELECT t2.c FROM t2 WHERE AVG(t1.b) > 20) AND
102
                t1.a > (SELECT MIN(t2.d) FROM t2);
103
    @endcode
104
    allow_sum_func will contain: 
105
    - for SUM(t1.b) - 1 at the first position 
106
    - for AVG(t1.b) - 1 at the first position, 0 at the second position
107
    - for MIN(t2.d) - 1 at the first position, 1 at the second position.
108
109
  @param thd  reference to the thread context info
110
  @param ref  location of the pointer to this item in the embedding expression
111
112
  @note
113
    This function is to be called for any item created for a set function
114
    object when the traversal of trees built for expressions used in the query
115
    is performed at the phase of context analysis. This function is to
116
    be invoked at the ascent of this traversal.
117
118
  @retval
119
    TRUE   if an error is reported
120
  @retval
121
    FALSE  otherwise
122
*/
123
 
124
bool Item_sum::check_sum_func(THD *thd, Item **ref)
125
{
126
  bool invalid= FALSE;
127
  nesting_map allow_sum_func= thd->lex->allow_sum_func;
128
  /*  
129
    The value of max_arg_level is updated if an argument of the set function
130
    contains a column reference resolved  against a subquery whose level is
131
    greater than the current value of max_arg_level.
132
    max_arg_level cannot be greater than nest level.
133
    nest level is always >= 0  
134
  */ 
135
  if (nest_level == max_arg_level)
136
  {
137
    /*
138
      The function must be aggregated in the current subquery, 
139
      If it is there under a construct where it is not allowed 
140
      we report an error. 
141
    */ 
142
    invalid= !(allow_sum_func & (1 << max_arg_level));
143
  }
144
  else if (max_arg_level >= 0 || !(allow_sum_func & (1 << nest_level)))
145
  {
146
    /*
147
      The set function can be aggregated only in outer subqueries.
148
      Try to find a subquery where it can be aggregated;
149
      If we fail to find such a subquery report an error.
150
    */
151
    if (register_sum_func(thd, ref))
152
      return TRUE;
153
    invalid= aggr_level < 0 && !(allow_sum_func & (1 << nest_level));
154
    if (!invalid && thd->variables.sql_mode & MODE_ANSI)
155
      invalid= aggr_level < 0 && max_arg_level < nest_level;
156
  }
157
  if (!invalid && aggr_level < 0)
158
  {
159
    aggr_level= nest_level;
160
    aggr_sel= thd->lex->current_select;
161
  }
162
  /*
163
    By this moment we either found a subquery where the set function is
164
    to be aggregated  and assigned a value that is  >= 0 to aggr_level,
165
    or set the value of 'invalid' to TRUE to report later an error. 
166
  */
167
  /* 
168
    Additionally we have to check whether possible nested set functions
169
    are acceptable here: they are not, if the level of aggregation of
170
    some of them is less than aggr_level.
171
  */
172
  if (!invalid) 
173
    invalid= aggr_level <= max_sum_func_level;
174
  if (invalid)  
175
  {
176
    my_message(ER_INVALID_GROUP_FUNC_USE, ER(ER_INVALID_GROUP_FUNC_USE),
177
               MYF(0));
178
    return TRUE;
179
  }
180
181
  if (in_sum_func)
182
  {
183
    /*
184
      If the set function is nested adjust the value of
185
      max_sum_func_level for the nesting set function.
186
      We take into account only enclosed set functions that are to be 
187
      aggregated on the same level or above of the nest level of 
188
      the enclosing set function.
189
      But we must always pass up the max_sum_func_level because it is
190
      the maximum nested level of all directly and indirectly enclosed
191
      set functions. We must do that even for set functions that are
192
      aggregated inside of their enclosing set function's nest level
193
      because the enclosing function may contain another enclosing
194
      function that is to be aggregated outside or on the same level
195
      as its parent's nest level.
196
    */
197
    if (in_sum_func->nest_level >= aggr_level)
198
      set_if_bigger(in_sum_func->max_sum_func_level, aggr_level);
199
    set_if_bigger(in_sum_func->max_sum_func_level, max_sum_func_level);
200
  }
201
202
  /*
203
    Check that non-aggregated fields and sum functions aren't mixed in the
204
    same select in the ONLY_FULL_GROUP_BY mode.
205
  */
206
  if (outer_fields.elements)
207
  {
208
    Item_field *field;
209
    /*
210
      Here we compare the nesting level of the select to which an outer field
211
      belongs to with the aggregation level of the sum function. All fields in
212
      the outer_fields list are checked.
213
214
      If the nesting level is equal to the aggregation level then the field is
215
        aggregated by this sum function.
216
      If the nesting level is less than the aggregation level then the field
217
        belongs to an outer select. In this case if there is an embedding sum
218
        function add current field to functions outer_fields list. If there is
219
        no embedding function then the current field treated as non aggregated
220
        and the select it belongs to is marked accordingly.
221
      If the nesting level is greater than the aggregation level then it means
222
        that this field was added by an inner sum function.
223
        Consider an example:
224
225
          select avg ( <-- we are here, checking outer.f1
226
            select (
227
              select sum(outer.f1 + inner.f1) from inner
228
            ) from outer)
229
          from most_outer;
230
231
        In this case we check that no aggregate functions are used in the
232
        select the field belongs to. If there are some then an error is
233
        raised.
234
    */
235
    List_iterator<Item_field> of(outer_fields);
236
    while ((field= of++))
237
    {
238
      SELECT_LEX *sel= field->cached_table->select_lex;
239
      if (sel->nest_level < aggr_level)
240
      {
241
        if (in_sum_func)
242
        {
243
          /*
244
            Let upper function decide whether this field is a non
245
            aggregated one.
246
          */
247
          in_sum_func->outer_fields.push_back(field);
248
        }
249
        else
250
          sel->full_group_by_flag|= NON_AGG_FIELD_USED;
251
      }
252
      if (sel->nest_level > aggr_level &&
253
          (sel->full_group_by_flag & SUM_FUNC_USED) &&
254
          !sel->group_list.elements)
255
      {
256
        my_message(ER_MIX_OF_GROUP_FUNC_AND_FIELDS,
257
                   ER(ER_MIX_OF_GROUP_FUNC_AND_FIELDS), MYF(0));
258
        return TRUE;
259
      }
260
    }
261
  }
262
  aggr_sel->full_group_by_flag|= SUM_FUNC_USED;
263
  update_used_tables();
264
  thd->lex->in_sum_func= in_sum_func;
265
  return FALSE;
266
}
267
268
/**
269
  Attach a set function to the subquery where it must be aggregated.
270
271
    The function looks for an outer subquery where the set function must be
272
    aggregated. If it finds such a subquery then aggr_level is set to
273
    the nest level of this subquery and the item for the set function
274
    is added to the list of set functions used in nested subqueries
275
    inner_sum_func_list defined for each subquery. When the item is placed 
276
    there the field 'ref_by' is set to ref.
277
278
  @note
279
    Now we 'register' only set functions that are aggregated in outer
280
    subqueries. Actually it makes sense to link all set function for
281
    a subquery in one chain. It would simplify the process of 'splitting'
282
    for set functions.
283
284
  @param thd  reference to the thread context info
285
  @param ref  location of the pointer to this item in the embedding expression
286
287
  @retval
288
    FALSE  if the executes without failures (currently always)
289
  @retval
290
    TRUE   otherwise
291
*/  
292
293
bool Item_sum::register_sum_func(THD *thd, Item **ref)
294
{
295
  SELECT_LEX *sl;
296
  nesting_map allow_sum_func= thd->lex->allow_sum_func;
297
  for (sl= thd->lex->current_select->master_unit()->outer_select() ;
298
       sl && sl->nest_level > max_arg_level;
299
       sl= sl->master_unit()->outer_select() )
300
  {
301
    if (aggr_level < 0 && (allow_sum_func & (1 << sl->nest_level)))
302
    {
303
      /* Found the most nested subquery where the function can be aggregated */
304
      aggr_level= sl->nest_level;
305
      aggr_sel= sl;
306
    }
307
  }
308
  if (sl && (allow_sum_func & (1 << sl->nest_level)))
309
  {
310
    /* 
311
      We reached the subquery of level max_arg_level and checked
312
      that the function can be aggregated here. 
313
      The set function will be aggregated in this subquery.
314
    */   
315
    aggr_level= sl->nest_level;
316
    aggr_sel= sl;
317
318
  }
319
  if (aggr_level >= 0)
320
  {
321
    ref_by= ref;
322
    /* Add the object to the list of registered objects assigned to aggr_sel */
323
    if (!aggr_sel->inner_sum_func_list)
324
      next= this;
325
    else
326
    {
327
      next= aggr_sel->inner_sum_func_list->next;
328
      aggr_sel->inner_sum_func_list->next= this;
329
    }
330
    aggr_sel->inner_sum_func_list= this;
331
    aggr_sel->with_sum_func= 1;
332
333
    /* 
334
      Mark Item_subselect(s) as containing aggregate function all the way up
335
      to aggregate function's calculation context.
336
      Note that we must not mark the Item of calculation context itself
337
      because with_sum_func on the calculation context st_select_lex is
338
      already set above.
339
340
      with_sum_func being set for an Item means that this Item refers 
341
      (somewhere in it, e.g. one of its arguments if it's a function) directly
342
      or through intermediate items to an aggregate function that is calculated
343
      in a context "outside" of the Item (e.g. in the current or outer select).
344
345
      with_sum_func being set for an st_select_lex means that this st_select_lex
346
      has aggregate functions directly referenced (i.e. not through a sub-select).
347
    */
348
    for (sl= thd->lex->current_select; 
349
         sl && sl != aggr_sel && sl->master_unit()->item;
350
         sl= sl->master_unit()->outer_select() )
351
      sl->master_unit()->item->with_sum_func= 1;
352
  }
353
  thd->lex->current_select->mark_as_dependent(aggr_sel);
354
  return FALSE;
355
}
356
357
358
Item_sum::Item_sum(List<Item> &list) :arg_count(list.elements), 
359
  forced_const(FALSE)
360
{
361
  if ((args=(Item**) sql_alloc(sizeof(Item*)*arg_count)))
362
  {
363
    uint i=0;
364
    List_iterator_fast<Item> li(list);
365
    Item *item;
366
367
    while ((item=li++))
368
    {
369
      args[i++]= item;
370
    }
371
  }
372
  mark_as_sum_func();
373
  list.empty();					// Fields are used
374
}
375
376
377
/**
378
  Constructor used in processing select with temporary tebles.
379
*/
380
381
Item_sum::Item_sum(THD *thd, Item_sum *item):
382
  Item_result_field(thd, item), arg_count(item->arg_count),
383
  aggr_sel(item->aggr_sel),
384
  nest_level(item->nest_level), aggr_level(item->aggr_level),
385
  quick_group(item->quick_group), used_tables_cache(item->used_tables_cache),
386
  forced_const(item->forced_const) 
387
{
388
  if (arg_count <= 2)
389
    args=tmp_args;
390
  else
391
    if (!(args= (Item**) thd->alloc(sizeof(Item*)*arg_count)))
392
      return;
393
  memcpy(args, item->args, sizeof(Item*)*arg_count);
394
}
395
396
397
void Item_sum::mark_as_sum_func()
398
{
399
  SELECT_LEX *cur_select= current_thd->lex->current_select;
400
  cur_select->n_sum_items++;
401
  cur_select->with_sum_func= 1;
402
  with_sum_func= 1;
403
}
404
405
406
void Item_sum::make_field(Send_field *tmp_field)
407
{
408
  if (args[0]->type() == Item::FIELD_ITEM && keep_field_type())
409
  {
410
    ((Item_field*) args[0])->field->make_field(tmp_field);
411
    /* For expressions only col_name should be non-empty string. */
412
    char *empty_string= (char*)"";
413
    tmp_field->db_name= empty_string;
414
    tmp_field->org_table_name= empty_string;
415
    tmp_field->table_name= empty_string;
416
    tmp_field->org_col_name= empty_string;
417
    tmp_field->col_name= name;
418
    if (maybe_null)
419
      tmp_field->flags&= ~NOT_NULL_FLAG;
420
  }
421
  else
422
    init_make_field(tmp_field, field_type());
423
}
424
425
426
void Item_sum::print(String *str, enum_query_type query_type)
427
{
428
  str->append(func_name());
429
  for (uint i=0 ; i < arg_count ; i++)
430
  {
431
    if (i)
432
      str->append(',');
433
    args[i]->print(str, query_type);
434
  }
435
  str->append(')');
436
}
437
438
void Item_sum::fix_num_length_and_dec()
439
{
440
  decimals=0;
441
  for (uint i=0 ; i < arg_count ; i++)
442
    set_if_bigger(decimals,args[i]->decimals);
443
  max_length=float_length(decimals);
444
}
445
446
Item *Item_sum::get_tmp_table_item(THD *thd)
447
{
448
  Item_sum* sum_item= (Item_sum *) copy_or_same(thd);
449
  if (sum_item && sum_item->result_field)	   // If not a const sum func
450
  {
451
    Field *result_field_tmp= sum_item->result_field;
452
    for (uint i=0 ; i < sum_item->arg_count ; i++)
453
    {
454
      Item *arg= sum_item->args[i];
455
      if (!arg->const_item())
456
      {
457
	if (arg->type() == Item::FIELD_ITEM)
458
	  ((Item_field*) arg)->field= result_field_tmp++;
459
	else
460
	  sum_item->args[i]= new Item_field(result_field_tmp++);
461
      }
462
    }
463
  }
464
  return sum_item;
465
}
466
467
468
bool Item_sum::walk (Item_processor processor, bool walk_subquery,
469
                     uchar *argument)
470
{
471
  if (arg_count)
472
  {
473
    Item **arg,**arg_end;
474
    for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
475
    {
476
      if ((*arg)->walk(processor, walk_subquery, argument))
477
	return 1;
478
    }
479
  }
480
  return (this->*processor)(argument);
481
}
482
483
484
Field *Item_sum::create_tmp_field(bool group, TABLE *table,
485
                                  uint convert_blob_length)
486
{
487
  Field *field;
488
  switch (result_type()) {
489
  case REAL_RESULT:
490
    field= new Field_double(max_length, maybe_null, name, decimals, TRUE);
491
    break;
492
  case INT_RESULT:
493
    field= new Field_longlong(max_length, maybe_null, name, unsigned_flag);
494
    break;
495
  case STRING_RESULT:
496
    if (max_length/collation.collation->mbmaxlen <= 255 ||
497
        convert_blob_length > Field_varstring::MAX_SIZE ||
498
        !convert_blob_length)
499
      return make_string_field(table);
500
    field= new Field_varstring(convert_blob_length, maybe_null,
501
                               name, table->s, collation.collation);
502
    break;
503
  case DECIMAL_RESULT:
504
    field= new Field_new_decimal(max_length, maybe_null, name,
505
                                 decimals, unsigned_flag);
506
    break;
507
  case ROW_RESULT:
508
  default:
509
    // This case should never be choosen
510
    DBUG_ASSERT(0);
511
    return 0;
512
  }
513
  if (field)
514
    field->init(table);
515
  return field;
516
}
517
518
519
void Item_sum::update_used_tables ()
520
{
521
  if (!forced_const)
522
  {
523
    used_tables_cache= 0;
524
    for (uint i=0 ; i < arg_count ; i++)
525
    {
526
      args[i]->update_used_tables();
527
      used_tables_cache|= args[i]->used_tables();
528
    }
529
530
    used_tables_cache&= PSEUDO_TABLE_BITS;
531
532
    /* the aggregate function is aggregated into its local context */
533
    used_tables_cache |=  (1 << aggr_sel->join->tables) - 1;
534
  }
535
}
536
537
538
String *
539
Item_sum_num::val_str(String *str)
540
{
541
  return val_string_from_real(str);
542
}
543
544
545
my_decimal *Item_sum_num::val_decimal(my_decimal *decimal_value)
546
{
547
  return val_decimal_from_real(decimal_value);
548
}
549
550
551
String *
552
Item_sum_int::val_str(String *str)
553
{
554
  return val_string_from_int(str);
555
}
556
557
558
my_decimal *Item_sum_int::val_decimal(my_decimal *decimal_value)
559
{
560
  return val_decimal_from_int(decimal_value);
561
}
562
563
564
bool
565
Item_sum_num::fix_fields(THD *thd, Item **ref)
566
{
567
  DBUG_ASSERT(fixed == 0);
568
569
  if (init_sum_func_check(thd))
570
    return TRUE;
571
572
  decimals=0;
573
  maybe_null=0;
574
  for (uint i=0 ; i < arg_count ; i++)
575
  {
576
    if (args[i]->fix_fields(thd, args + i) || args[i]->check_cols(1))
577
      return TRUE;
578
    set_if_bigger(decimals, args[i]->decimals);
579
    maybe_null |= args[i]->maybe_null;
580
  }
581
  result_field=0;
582
  max_length=float_length(decimals);
583
  null_value=1;
584
  fix_length_and_dec();
585
586
  if (check_sum_func(thd, ref))
587
    return TRUE;
588
589
  fixed= 1;
590
  return FALSE;
591
}
592
593
594
Item_sum_hybrid::Item_sum_hybrid(THD *thd, Item_sum_hybrid *item)
595
  :Item_sum(thd, item), value(item->value), hybrid_type(item->hybrid_type),
596
  hybrid_field_type(item->hybrid_field_type), cmp_sign(item->cmp_sign),
597
  was_values(item->was_values)
598
{
599
  /* copy results from old value */
600
  switch (hybrid_type) {
601
  case INT_RESULT:
602
    sum_int= item->sum_int;
603
    break;
604
  case DECIMAL_RESULT:
605
    my_decimal2decimal(&item->sum_dec, &sum_dec);
606
    break;
607
  case REAL_RESULT:
608
    sum= item->sum;
609
    break;
610
  case STRING_RESULT:
611
    /*
612
      This can happen with ROLLUP. Note that the value is already
613
      copied at function call.
614
    */
615
    break;
616
  case ROW_RESULT:
617
  default:
618
    DBUG_ASSERT(0);
619
  }
620
  collation.set(item->collation);
621
}
622
623
bool
624
Item_sum_hybrid::fix_fields(THD *thd, Item **ref)
625
{
626
  DBUG_ASSERT(fixed == 0);
627
628
  Item *item= args[0];
629
630
  if (init_sum_func_check(thd))
631
    return TRUE;
632
633
  // 'item' can be changed during fix_fields
634
  if ((!item->fixed && item->fix_fields(thd, args)) ||
635
      (item= args[0])->check_cols(1))
636
    return TRUE;
637
  decimals=item->decimals;
638
639
  switch (hybrid_type= item->result_type()) {
640
  case INT_RESULT:
641
    max_length= 20;
642
    sum_int= 0;
643
    break;
644
  case DECIMAL_RESULT:
645
    max_length= item->max_length;
646
    my_decimal_set_zero(&sum_dec);
647
    break;
648
  case REAL_RESULT:
649
    max_length= float_length(decimals);
650
    sum= 0.0;
651
    break;
652
  case STRING_RESULT:
653
    max_length= item->max_length;
654
    break;
655
  case ROW_RESULT:
656
  default:
657
    DBUG_ASSERT(0);
658
  };
659
  /* MIN/MAX can return NULL for empty set indepedent of the used column */
660
  maybe_null= 1;
661
  unsigned_flag=item->unsigned_flag;
662
  collation.set(item->collation);
663
  result_field=0;
664
  null_value=1;
665
  fix_length_and_dec();
666
  item= item->real_item();
667
  if (item->type() == Item::FIELD_ITEM)
668
    hybrid_field_type= ((Item_field*) item)->field->type();
669
  else
670
    hybrid_field_type= Item::field_type();
671
672
  if (check_sum_func(thd, ref))
673
    return TRUE;
674
675
  fixed= 1;
676
  return FALSE;
677
}
678
679
Field *Item_sum_hybrid::create_tmp_field(bool group, TABLE *table,
680
					 uint convert_blob_length)
681
{
682
  Field *field;
683
  if (args[0]->type() == Item::FIELD_ITEM)
684
  {
685
    field= ((Item_field*) args[0])->field;
686
    
687
    if ((field= create_tmp_field_from_field(current_thd, field, name, table,
688
					    NULL, convert_blob_length)))
689
      field->flags&= ~NOT_NULL_FLAG;
690
    return field;
691
  }
692
  /*
693
    DATE/TIME fields have STRING_RESULT result types.
694
    In order to preserve field type, it's needed to handle DATE/TIME
695
    fields creations separately.
696
  */
697
  switch (args[0]->field_type()) {
698
  case MYSQL_TYPE_DATE:
699
    field= new Field_newdate(maybe_null, name, collation.collation);
700
    break;
701
  case MYSQL_TYPE_TIME:
702
    field= new Field_time(maybe_null, name, collation.collation);
703
    break;
704
  case MYSQL_TYPE_TIMESTAMP:
705
  case MYSQL_TYPE_DATETIME:
706
    field= new Field_datetime(maybe_null, name, collation.collation);
707
    break;
708
  default:
709
    return Item_sum::create_tmp_field(group, table, convert_blob_length);
710
  }
711
  if (field)
712
    field->init(table);
713
  return field;
714
}
715
716
717
/***********************************************************************
718
** reset and add of sum_func
719
***********************************************************************/
720
721
/**
722
  @todo
723
  check if the following assignments are really needed
724
*/
725
Item_sum_sum::Item_sum_sum(THD *thd, Item_sum_sum *item) 
726
  :Item_sum_num(thd, item), hybrid_type(item->hybrid_type),
727
   curr_dec_buff(item->curr_dec_buff)
728
{
729
  /* TODO: check if the following assignments are really needed */
730
  if (hybrid_type == DECIMAL_RESULT)
731
  {
732
    my_decimal2decimal(item->dec_buffs, dec_buffs);
733
    my_decimal2decimal(item->dec_buffs + 1, dec_buffs + 1);
734
  }
735
  else
736
    sum= item->sum;
737
}
738
739
Item *Item_sum_sum::copy_or_same(THD* thd)
740
{
741
  return new (thd->mem_root) Item_sum_sum(thd, this);
742
}
743
744
745
void Item_sum_sum::clear()
746
{
747
  DBUG_ENTER("Item_sum_sum::clear");
748
  null_value=1;
749
  if (hybrid_type == DECIMAL_RESULT)
750
  {
751
    curr_dec_buff= 0;
752
    my_decimal_set_zero(dec_buffs);
753
  }
754
  else
755
    sum= 0.0;
756
  DBUG_VOID_RETURN;
757
}
758
759
760
void Item_sum_sum::fix_length_and_dec()
761
{
762
  DBUG_ENTER("Item_sum_sum::fix_length_and_dec");
763
  maybe_null=null_value=1;
764
  decimals= args[0]->decimals;
765
  switch (args[0]->result_type()) {
766
  case REAL_RESULT:
767
  case STRING_RESULT:
768
    hybrid_type= REAL_RESULT;
769
    sum= 0.0;
770
    break;
771
  case INT_RESULT:
772
  case DECIMAL_RESULT:
773
  {
774
    /* SUM result can't be longer than length(arg) + length(MAX_ROWS) */
775
    int precision= args[0]->decimal_precision() + DECIMAL_LONGLONG_DIGITS;
776
    max_length= my_decimal_precision_to_length(precision, decimals,
777
                                               unsigned_flag);
778
    curr_dec_buff= 0;
779
    hybrid_type= DECIMAL_RESULT;
780
    my_decimal_set_zero(dec_buffs);
781
    break;
782
  }
783
  case ROW_RESULT:
784
  default:
785
    DBUG_ASSERT(0);
786
  }
787
  DBUG_PRINT("info", ("Type: %s (%d, %d)",
788
                      (hybrid_type == REAL_RESULT ? "REAL_RESULT" :
789
                       hybrid_type == DECIMAL_RESULT ? "DECIMAL_RESULT" :
790
                       hybrid_type == INT_RESULT ? "INT_RESULT" :
791
                       "--ILLEGAL!!!--"),
792
                      max_length,
793
                      (int)decimals));
794
  DBUG_VOID_RETURN;
795
}
796
797
798
bool Item_sum_sum::add()
799
{
800
  DBUG_ENTER("Item_sum_sum::add");
801
  if (hybrid_type == DECIMAL_RESULT)
802
  {
803
    my_decimal value, *val= args[0]->val_decimal(&value);
804
    if (!args[0]->null_value)
805
    {
806
      my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs + (curr_dec_buff^1),
807
                     val, dec_buffs + curr_dec_buff);
808
      curr_dec_buff^= 1;
809
      null_value= 0;
810
    }
811
  }
812
  else
813
  {
814
    sum+= args[0]->val_real();
815
    if (!args[0]->null_value)
816
      null_value= 0;
817
  }
818
  DBUG_RETURN(0);
819
}
820
821
822
longlong Item_sum_sum::val_int()
823
{
824
  DBUG_ASSERT(fixed == 1);
825
  if (hybrid_type == DECIMAL_RESULT)
826
  {
827
    longlong result;
828
    my_decimal2int(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, unsigned_flag,
829
                   &result);
830
    return result;
831
  }
832
  return (longlong) rint(val_real());
833
}
834
835
836
double Item_sum_sum::val_real()
837
{
838
  DBUG_ASSERT(fixed == 1);
839
  if (hybrid_type == DECIMAL_RESULT)
840
    my_decimal2double(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, &sum);
841
  return sum;
842
}
843
844
845
String *Item_sum_sum::val_str(String *str)
846
{
847
  if (hybrid_type == DECIMAL_RESULT)
848
    return val_string_from_decimal(str);
849
  return val_string_from_real(str);
850
}
851
852
853
my_decimal *Item_sum_sum::val_decimal(my_decimal *val)
854
{
855
  if (hybrid_type == DECIMAL_RESULT)
856
    return (dec_buffs + curr_dec_buff);
857
  return val_decimal_from_real(val);
858
}
859
860
/***************************************************************************/
861
862
C_MODE_START
863
864
/* Declarations for auxilary C-callbacks */
865
866
static int simple_raw_key_cmp(void* arg, const void* key1, const void* key2)
867
{
868
    return memcmp(key1, key2, *(uint *) arg);
869
}
870
871
872
static int item_sum_distinct_walk(void *element, element_count num_of_dups,
873
                                  void *item)
874
{
875
  return ((Item_sum_distinct*) (item))->unique_walk_function(element);
876
}
877
878
C_MODE_END
879
880
/* Item_sum_distinct */
881
882
Item_sum_distinct::Item_sum_distinct(Item *item_arg)
883
  :Item_sum_num(item_arg), tree(0)
884
{
885
  /*
886
    quick_group is an optimizer hint, which means that GROUP BY can be
887
    handled with help of index on grouped columns.
888
    By setting quick_group to zero we force creation of temporary table
889
    to perform GROUP BY.
890
  */
891
  quick_group= 0;
892
}
893
894
895
Item_sum_distinct::Item_sum_distinct(THD *thd, Item_sum_distinct *original)
896
  :Item_sum_num(thd, original), val(original->val), tree(0),
897
  table_field_type(original->table_field_type)
898
{
899
  quick_group= 0;
900
}
901
902
903
/**
904
  Behaves like an Integer except to fix_length_and_dec().
905
  Additionally div() converts val with this traits to a val with true
906
  decimal traits along with conversion of integer value to decimal value.
907
  This is to speedup SUM/AVG(DISTINCT) evaluation for 8-32 bit integer
908
  values.
909
*/
910
struct Hybrid_type_traits_fast_decimal: public
911
       Hybrid_type_traits_integer
912
{
913
  virtual Item_result type() const { return DECIMAL_RESULT; }
914
  virtual void fix_length_and_dec(Item *item, Item *arg) const
915
  { Hybrid_type_traits_decimal::instance()->fix_length_and_dec(item, arg); }
916
917
  virtual void div(Hybrid_type *val, ulonglong u) const
918
  {
919
    int2my_decimal(E_DEC_FATAL_ERROR, val->integer, 0, val->dec_buf);
920
    val->used_dec_buf_no= 0;
921
    val->traits= Hybrid_type_traits_decimal::instance();
922
    val->traits->div(val, u);
923
  }
924
  static const Hybrid_type_traits_fast_decimal *instance();
925
  Hybrid_type_traits_fast_decimal() {};
926
};
927
928
static const Hybrid_type_traits_fast_decimal fast_decimal_traits_instance;
929
930
const Hybrid_type_traits_fast_decimal
931
  *Hybrid_type_traits_fast_decimal::instance()
932
{
933
  return &fast_decimal_traits_instance;
934
}
935
936
void Item_sum_distinct::fix_length_and_dec()
937
{
938
  DBUG_ASSERT(args[0]->fixed);
939
940
  table_field_type= args[0]->field_type();
941
942
  /* Adjust tmp table type according to the chosen aggregation type */
943
  switch (args[0]->result_type()) {
944
  case STRING_RESULT:
945
  case REAL_RESULT:
946
    val.traits= Hybrid_type_traits::instance();
947
    if (table_field_type != MYSQL_TYPE_FLOAT)
948
      table_field_type= MYSQL_TYPE_DOUBLE;
949
    break;
950
  case INT_RESULT:
951
  /*
952
    Preserving int8, int16, int32 field types gives ~10% performance boost
953
    as the size of result tree becomes significantly smaller.
954
    Another speed up we gain by using longlong for intermediate
955
    calculations. The range of int64 is enough to hold sum 2^32 distinct
956
    integers each <= 2^32.
957
  */
958
  if (table_field_type == MYSQL_TYPE_INT24 ||
959
      (table_field_type >= MYSQL_TYPE_TINY && table_field_type <= MYSQL_TYPE_LONG))
960
  {
961
    val.traits= Hybrid_type_traits_fast_decimal::instance();
962
    break;
963
  }
964
  table_field_type= MYSQL_TYPE_LONGLONG;
965
  /* fallthrough */
966
  case DECIMAL_RESULT:
967
    val.traits= Hybrid_type_traits_decimal::instance();
968
    if (table_field_type != MYSQL_TYPE_LONGLONG)
969
      table_field_type= MYSQL_TYPE_NEWDECIMAL;
970
    break;
971
  case ROW_RESULT:
972
  default:
973
    DBUG_ASSERT(0);
974
  }
975
  val.traits->fix_length_and_dec(this, args[0]);
976
}
977
978
979
/**
980
  @todo
981
  check that the case of CHAR(0) works OK
982
*/
983
bool Item_sum_distinct::setup(THD *thd)
984
{
985
  List<Create_field> field_list;
986
  Create_field field_def;                              /* field definition */
987
  DBUG_ENTER("Item_sum_distinct::setup");
988
  /* It's legal to call setup() more than once when in a subquery */
989
  if (tree)
990
    DBUG_RETURN(FALSE);
991
992
  /*
993
    Virtual table and the tree are created anew on each re-execution of
994
    PS/SP. Hence all further allocations are performed in the runtime
995
    mem_root.
996
  */
997
  if (field_list.push_back(&field_def))
998
    DBUG_RETURN(TRUE);
999
1000
  null_value= maybe_null= 1;
1001
  quick_group= 0;
1002
1003
  DBUG_ASSERT(args[0]->fixed);
1004
1005
  field_def.init_for_tmp_table(table_field_type, args[0]->max_length,
1006
                               args[0]->decimals, args[0]->maybe_null,
1007
                               args[0]->unsigned_flag);
1008
1009
  if (! (table= create_virtual_tmp_table(thd, field_list)))
1010
    DBUG_RETURN(TRUE);
1011
1012
  /* XXX: check that the case of CHAR(0) works OK */
1013
  tree_key_length= table->s->reclength - table->s->null_bytes;
1014
1015
  /*
1016
    Unique handles all unique elements in a tree until they can't fit
1017
    in.  Then the tree is dumped to the temporary file. We can use
1018
    simple_raw_key_cmp because the table contains numbers only; decimals
1019
    are converted to binary representation as well.
1020
  */
1021
  tree= new Unique(simple_raw_key_cmp, &tree_key_length, tree_key_length,
1022
                   thd->variables.max_heap_table_size);
1023
1024
  is_evaluated= FALSE;
1025
  DBUG_RETURN(tree == 0);
1026
}
1027
1028
1029
bool Item_sum_distinct::add()
1030
{
1031
  args[0]->save_in_field(table->field[0], FALSE);
1032
  is_evaluated= FALSE;
1033
  if (!table->field[0]->is_null())
1034
  {
1035
    DBUG_ASSERT(tree);
1036
    null_value= 0;
1037
    /*
1038
      '0' values are also stored in the tree. This doesn't matter
1039
      for SUM(DISTINCT), but is important for AVG(DISTINCT)
1040
    */
1041
    return tree->unique_add(table->field[0]->ptr);
1042
  }
1043
  return 0;
1044
}
1045
1046
1047
bool Item_sum_distinct::unique_walk_function(void *element)
1048
{
1049
  memcpy(table->field[0]->ptr, element, tree_key_length);
1050
  ++count;
1051
  val.traits->add(&val, table->field[0]);
1052
  return 0;
1053
}
1054
1055
1056
void Item_sum_distinct::clear()
1057
{
1058
  DBUG_ENTER("Item_sum_distinct::clear");
1059
  DBUG_ASSERT(tree != 0);                        /* we always have a tree */
1060
  null_value= 1;
1061
  tree->reset();
1062
  is_evaluated= FALSE;
1063
  DBUG_VOID_RETURN;
1064
}
1065
1066
void Item_sum_distinct::cleanup()
1067
{
1068
  Item_sum_num::cleanup();
1069
  delete tree;
1070
  tree= 0;
1071
  table= 0;
1072
  is_evaluated= FALSE;
1073
}
1074
1075
Item_sum_distinct::~Item_sum_distinct()
1076
{
1077
  delete tree;
1078
  /* no need to free the table */
1079
}
1080
1081
1082
void Item_sum_distinct::calculate_val_and_count()
1083
{
1084
  if (!is_evaluated)
1085
  {
1086
    count= 0;
1087
    val.traits->set_zero(&val);
1088
    /*
1089
      We don't have a tree only if 'setup()' hasn't been called;
1090
      this is the case of sql_select.cc:return_zero_rows.
1091
     */
1092
    if (tree)
1093
    {
1094
      table->field[0]->set_notnull();
1095
      tree->walk(item_sum_distinct_walk, (void*) this);
1096
    }
1097
    is_evaluated= TRUE;
1098
  }
1099
}
1100
1101
1102
double Item_sum_distinct::val_real()
1103
{
1104
  calculate_val_and_count();
1105
  return val.traits->val_real(&val);
1106
}
1107
1108
1109
my_decimal *Item_sum_distinct::val_decimal(my_decimal *to)
1110
{
1111
  calculate_val_and_count();
1112
  if (null_value)
1113
    return 0;
1114
  return val.traits->val_decimal(&val, to);
1115
}
1116
1117
1118
longlong Item_sum_distinct::val_int()
1119
{
1120
  calculate_val_and_count();
1121
  return val.traits->val_int(&val, unsigned_flag);
1122
}
1123
1124
1125
String *Item_sum_distinct::val_str(String *str)
1126
{
1127
  calculate_val_and_count();
1128
  if (null_value)
1129
    return 0;
1130
  return val.traits->val_str(&val, str, decimals);
1131
}
1132
1133
/* end of Item_sum_distinct */
1134
1135
/* Item_sum_avg_distinct */
1136
1137
void
1138
Item_sum_avg_distinct::fix_length_and_dec()
1139
{
1140
  Item_sum_distinct::fix_length_and_dec();
1141
  prec_increment= current_thd->variables.div_precincrement;
1142
  /*
1143
    AVG() will divide val by count. We need to reserve digits
1144
    after decimal point as the result can be fractional.
1145
  */
1146
  decimals= min(decimals + prec_increment, NOT_FIXED_DEC);
1147
}
1148
1149
1150
void
1151
Item_sum_avg_distinct::calculate_val_and_count()
1152
{
1153
  if (!is_evaluated)
1154
  {
1155
    Item_sum_distinct::calculate_val_and_count();
1156
    if (count)
1157
      val.traits->div(&val, count);
1158
    is_evaluated= TRUE;
1159
  }
1160
}
1161
1162
1163
Item *Item_sum_count::copy_or_same(THD* thd)
1164
{
1165
  return new (thd->mem_root) Item_sum_count(thd, this);
1166
}
1167
1168
1169
void Item_sum_count::clear()
1170
{
1171
  count= 0;
1172
}
1173
1174
1175
bool Item_sum_count::add()
1176
{
1177
  if (!args[0]->maybe_null || !args[0]->is_null())
1178
    count++;
1179
  return 0;
1180
}
1181
1182
longlong Item_sum_count::val_int()
1183
{
1184
  DBUG_ASSERT(fixed == 1);
1185
  return (longlong) count;
1186
}
1187
1188
1189
void Item_sum_count::cleanup()
1190
{
1191
  DBUG_ENTER("Item_sum_count::cleanup");
1192
  count= 0;
1193
  Item_sum_int::cleanup();
1194
  DBUG_VOID_RETURN;
1195
}
1196
1197
1198
/*
1199
  Avgerage
1200
*/
1201
void Item_sum_avg::fix_length_and_dec()
1202
{
1203
  Item_sum_sum::fix_length_and_dec();
1204
  maybe_null=null_value=1;
1205
  prec_increment= current_thd->variables.div_precincrement;
1206
  if (hybrid_type == DECIMAL_RESULT)
1207
  {
1208
    int precision= args[0]->decimal_precision() + prec_increment;
1209
    decimals= min(args[0]->decimals + prec_increment, DECIMAL_MAX_SCALE);
1210
    max_length= my_decimal_precision_to_length(precision, decimals,
1211
                                               unsigned_flag);
1212
    f_precision= min(precision+DECIMAL_LONGLONG_DIGITS, DECIMAL_MAX_PRECISION);
1213
    f_scale=  args[0]->decimals;
1214
    dec_bin_size= my_decimal_get_binary_size(f_precision, f_scale);
1215
  }
1216
  else {
1217
    decimals= min(args[0]->decimals + prec_increment, NOT_FIXED_DEC);
1218
    max_length= args[0]->max_length + prec_increment;
1219
  }
1220
}
1221
1222
1223
Item *Item_sum_avg::copy_or_same(THD* thd)
1224
{
1225
  return new (thd->mem_root) Item_sum_avg(thd, this);
1226
}
1227
1228
1229
Field *Item_sum_avg::create_tmp_field(bool group, TABLE *table,
1230
                                      uint convert_blob_len)
1231
{
1232
  Field *field;
1233
  if (group)
1234
  {
1235
    /*
1236
      We must store both value and counter in the temporary table in one field.
1237
      The easiest way is to do this is to store both value in a string
1238
      and unpack on access.
1239
    */
1240
    field= new Field_string(((hybrid_type == DECIMAL_RESULT) ?
1241
                             dec_bin_size : sizeof(double)) + sizeof(longlong),
1242
                            0, name, &my_charset_bin);
1243
  }
1244
  else if (hybrid_type == DECIMAL_RESULT)
1245
    field= new Field_new_decimal(max_length, maybe_null, name,
1246
                                 decimals, unsigned_flag);
1247
  else
1248
    field= new Field_double(max_length, maybe_null, name, decimals, TRUE);
1249
  if (field)
1250
    field->init(table);
1251
  return field;
1252
}
1253
1254
1255
void Item_sum_avg::clear()
1256
{
1257
  Item_sum_sum::clear();
1258
  count=0;
1259
}
1260
1261
1262
bool Item_sum_avg::add()
1263
{
1264
  if (Item_sum_sum::add())
1265
    return TRUE;
1266
  if (!args[0]->null_value)
1267
    count++;
1268
  return FALSE;
1269
}
1270
1271
double Item_sum_avg::val_real()
1272
{
1273
  DBUG_ASSERT(fixed == 1);
1274
  if (!count)
1275
  {
1276
    null_value=1;
1277
    return 0.0;
1278
  }
1279
  return Item_sum_sum::val_real() / ulonglong2double(count);
1280
}
1281
1282
1283
my_decimal *Item_sum_avg::val_decimal(my_decimal *val)
1284
{
1285
  my_decimal sum_buff, cnt;
1286
  const my_decimal *sum_dec;
1287
  DBUG_ASSERT(fixed == 1);
1288
  if (!count)
1289
  {
1290
    null_value=1;
1291
    return NULL;
1292
  }
1293
1294
  /*
1295
    For non-DECIMAL hybrid_type the division will be done in
1296
    Item_sum_avg::val_real().
1297
  */
1298
  if (hybrid_type != DECIMAL_RESULT)
1299
    return val_decimal_from_real(val);
1300
1301
  sum_dec= dec_buffs + curr_dec_buff;
1302
  int2my_decimal(E_DEC_FATAL_ERROR, count, 0, &cnt);
1303
  my_decimal_div(E_DEC_FATAL_ERROR, val, sum_dec, &cnt, prec_increment);
1304
  return val;
1305
}
1306
1307
1308
String *Item_sum_avg::val_str(String *str)
1309
{
1310
  if (hybrid_type == DECIMAL_RESULT)
1311
    return val_string_from_decimal(str);
1312
  return val_string_from_real(str);
1313
}
1314
1315
1316
/*
1317
  Standard deviation
1318
*/
1319
1320
double Item_sum_std::val_real()
1321
{
1322
  DBUG_ASSERT(fixed == 1);
1323
  double nr= Item_sum_variance::val_real();
1324
  DBUG_ASSERT(nr >= 0.0);
1325
  return sqrt(nr);
1326
}
1327
1328
Item *Item_sum_std::copy_or_same(THD* thd)
1329
{
1330
  return new (thd->mem_root) Item_sum_std(thd, this);
1331
}
1332
1333
1334
/*
1335
  Variance
1336
*/
1337
1338
1339
/**
1340
  Variance implementation for floating-point implementations, without
1341
  catastrophic cancellation, from Knuth's _TAoCP_, 3rd ed, volume 2, pg232.
1342
  This alters the value at m, s, and increments count.
1343
*/
1344
1345
/*
1346
  These two functions are used by the Item_sum_variance and the
1347
  Item_variance_field classes, which are unrelated, and each need to calculate
1348
  variance.  The difference between the two classes is that the first is used
1349
  for a mundane SELECT, while the latter is used in a GROUPing SELECT.
1350
*/
1351
static void variance_fp_recurrence_next(double *m, double *s, ulonglong *count, double nr)
1352
{
1353
  *count += 1;
1354
1355
  if (*count == 1) 
1356
  {
1357
    *m= nr;
1358
    *s= 0;
1359
  }
1360
  else
1361
  {
1362
    double m_kminusone= *m;
1363
    *m= m_kminusone + (nr - m_kminusone) / (double) *count;
1364
    *s= *s + (nr - m_kminusone) * (nr - *m);
1365
  }
1366
}
1367
1368
1369
static double variance_fp_recurrence_result(double s, ulonglong count, bool is_sample_variance)
1370
{
1371
  if (count == 1)
1372
    return 0.0;
1373
1374
  if (is_sample_variance)
1375
    return s / (count - 1);
1376
1377
  /* else, is a population variance */
1378
  return s / count;
1379
}
1380
1381
1382
Item_sum_variance::Item_sum_variance(THD *thd, Item_sum_variance *item):
1383
  Item_sum_num(thd, item), hybrid_type(item->hybrid_type),
1384
    count(item->count), sample(item->sample),
1385
    prec_increment(item->prec_increment)
1386
{
1387
  recurrence_m= item->recurrence_m;
1388
  recurrence_s= item->recurrence_s;
1389
}
1390
1391
1392
void Item_sum_variance::fix_length_and_dec()
1393
{
1394
  DBUG_ENTER("Item_sum_variance::fix_length_and_dec");
1395
  maybe_null= null_value= 1;
1396
  prec_increment= current_thd->variables.div_precincrement;
1397
1398
  /*
1399
    According to the SQL2003 standard (Part 2, Foundations; sec 10.9,
1400
    aggregate function; paragraph 7h of Syntax Rules), "the declared 
1401
    type of the result is an implementation-defined aproximate numeric
1402
    type.
1403
  */
1404
  hybrid_type= REAL_RESULT;
1405
1406
  switch (args[0]->result_type()) {
1407
  case REAL_RESULT:
1408
  case STRING_RESULT:
1409
    decimals= min(args[0]->decimals + 4, NOT_FIXED_DEC);
1410
    break;
1411
  case INT_RESULT:
1412
  case DECIMAL_RESULT:
1413
  {
1414
    int precision= args[0]->decimal_precision()*2 + prec_increment;
1415
    decimals= min(args[0]->decimals + prec_increment, DECIMAL_MAX_SCALE);
1416
    max_length= my_decimal_precision_to_length(precision, decimals,
1417
                                               unsigned_flag);
1418
1419
    break;
1420
  }
1421
  case ROW_RESULT:
1422
  default:
1423
    DBUG_ASSERT(0);
1424
  }
1425
  DBUG_PRINT("info", ("Type: REAL_RESULT (%d, %d)", max_length, (int)decimals));
1426
  DBUG_VOID_RETURN;
1427
}
1428
1429
1430
Item *Item_sum_variance::copy_or_same(THD* thd)
1431
{
1432
  return new (thd->mem_root) Item_sum_variance(thd, this);
1433
}
1434
1435
1436
/**
1437
  Create a new field to match the type of value we're expected to yield.
1438
  If we're grouping, then we need some space to serialize variables into, to
1439
  pass around.
1440
*/
1441
Field *Item_sum_variance::create_tmp_field(bool group, TABLE *table,
1442
                                           uint convert_blob_len)
1443
{
1444
  Field *field;
1445
  if (group)
1446
  {
1447
    /*
1448
      We must store both value and counter in the temporary table in one field.
1449
      The easiest way is to do this is to store both value in a string
1450
      and unpack on access.
1451
    */
1452
    field= new Field_string(sizeof(double)*2 + sizeof(longlong), 0, name, &my_charset_bin);
1453
  }
1454
  else
1455
    field= new Field_double(max_length, maybe_null, name, decimals, TRUE);
1456
1457
  if (field != NULL)
1458
    field->init(table);
1459
1460
  return field;
1461
}
1462
1463
1464
void Item_sum_variance::clear()
1465
{
1466
  count= 0; 
1467
}
1468
1469
bool Item_sum_variance::add()
1470
{
1471
  /* 
1472
    Why use a temporary variable?  We don't know if it is null until we
1473
    evaluate it, which has the side-effect of setting null_value .
1474
  */
1475
  double nr= args[0]->val_real();
1476
  
1477
  if (!args[0]->null_value)
1478
    variance_fp_recurrence_next(&recurrence_m, &recurrence_s, &count, nr);
1479
  return 0;
1480
}
1481
1482
double Item_sum_variance::val_real()
1483
{
1484
  DBUG_ASSERT(fixed == 1);
1485
1486
  /*
1487
    'sample' is a 1/0 boolean value.  If it is 1/true, id est this is a sample
1488
    variance call, then we should set nullness when the count of the items
1489
    is one or zero.  If it's zero, i.e. a population variance, then we only
1490
    set nullness when the count is zero.
1491
1492
    Another way to read it is that 'sample' is the numerical threshhold, at and
1493
    below which a 'count' number of items is called NULL.
1494
  */
1495
  DBUG_ASSERT((sample == 0) || (sample == 1));
1496
  if (count <= sample)
1497
  {
1498
    null_value=1;
1499
    return 0.0;
1500
  }
1501
1502
  null_value=0;
1503
  return variance_fp_recurrence_result(recurrence_s, count, sample);
1504
}
1505
1506
1507
my_decimal *Item_sum_variance::val_decimal(my_decimal *dec_buf)
1508
{
1509
  DBUG_ASSERT(fixed == 1);
1510
  return val_decimal_from_real(dec_buf);
1511
}
1512
1513
1514
void Item_sum_variance::reset_field()
1515
{
1516
  double nr;
1517
  uchar *res= result_field->ptr;
1518
1519
  nr= args[0]->val_real();              /* sets null_value as side-effect */
1520
1521
  if (args[0]->null_value)
1522
    bzero(res,sizeof(double)*2+sizeof(longlong));
1523
  else
1524
  {
1525
    /* Serialize format is (double)m, (double)s, (longlong)count */
1526
    ulonglong tmp_count;
1527
    double tmp_s;
1528
    float8store(res, nr);               /* recurrence variable m */
1529
    tmp_s= 0.0;
1530
    float8store(res + sizeof(double), tmp_s);
1531
    tmp_count= 1;
1532
    int8store(res + sizeof(double)*2, tmp_count);
1533
  }
1534
}
1535
1536
1537
void Item_sum_variance::update_field()
1538
{
1539
  ulonglong field_count;
1540
  uchar *res=result_field->ptr;
1541
1542
  double nr= args[0]->val_real();       /* sets null_value as side-effect */
1543
1544
  if (args[0]->null_value)
1545
    return;
1546
1547
  /* Serialize format is (double)m, (double)s, (longlong)count */
1548
  double field_recurrence_m, field_recurrence_s;
1549
  float8get(field_recurrence_m, res);
1550
  float8get(field_recurrence_s, res + sizeof(double));
1551
  field_count=sint8korr(res+sizeof(double)*2);
1552
1553
  variance_fp_recurrence_next(&field_recurrence_m, &field_recurrence_s, &field_count, nr);
1554
1555
  float8store(res, field_recurrence_m);
1556
  float8store(res + sizeof(double), field_recurrence_s);
1557
  res+= sizeof(double)*2;
1558
  int8store(res,field_count);
1559
}
1560
1561
1562
/* min & max */
1563
1564
void Item_sum_hybrid::clear()
1565
{
1566
  switch (hybrid_type) {
1567
  case INT_RESULT:
1568
    sum_int= 0;
1569
    break;
1570
  case DECIMAL_RESULT:
1571
    my_decimal_set_zero(&sum_dec);
1572
    break;
1573
  case REAL_RESULT:
1574
    sum= 0.0;
1575
    break;
1576
  default:
1577
    value.length(0);
1578
  }
1579
  null_value= 1;
1580
}
1581
1582
double Item_sum_hybrid::val_real()
1583
{
1584
  DBUG_ASSERT(fixed == 1);
1585
  if (null_value)
1586
    return 0.0;
1587
  switch (hybrid_type) {
1588
  case STRING_RESULT:
1589
  {
1590
    char *end_not_used;
1591
    int err_not_used;
1592
    String *res;  res=val_str(&str_value);
1593
    return (res ? my_strntod(res->charset(), (char*) res->ptr(), res->length(),
1594
			     &end_not_used, &err_not_used) : 0.0);
1595
  }
1596
  case INT_RESULT:
1597
    if (unsigned_flag)
1598
      return ulonglong2double(sum_int);
1599
    return (double) sum_int;
1600
  case DECIMAL_RESULT:
1601
    my_decimal2double(E_DEC_FATAL_ERROR, &sum_dec, &sum);
1602
    return sum;
1603
  case REAL_RESULT:
1604
    return sum;
1605
  case ROW_RESULT:
1606
  default:
1607
    // This case should never be choosen
1608
    DBUG_ASSERT(0);
1609
    return 0;
1610
  }
1611
}
1612
1613
longlong Item_sum_hybrid::val_int()
1614
{
1615
  DBUG_ASSERT(fixed == 1);
1616
  if (null_value)
1617
    return 0;
1618
  switch (hybrid_type) {
1619
  case INT_RESULT:
1620
    return sum_int;
1621
  case DECIMAL_RESULT:
1622
  {
1623
    longlong result;
1624
    my_decimal2int(E_DEC_FATAL_ERROR, &sum_dec, unsigned_flag, &result);
1625
    return sum_int;
1626
  }
1627
  default:
1628
    return (longlong) rint(Item_sum_hybrid::val_real());
1629
  }
1630
}
1631
1632
1633
my_decimal *Item_sum_hybrid::val_decimal(my_decimal *val)
1634
{
1635
  DBUG_ASSERT(fixed == 1);
1636
  if (null_value)
1637
    return 0;
1638
  switch (hybrid_type) {
1639
  case STRING_RESULT:
1640
    string2my_decimal(E_DEC_FATAL_ERROR, &value, val);
1641
    break;
1642
  case REAL_RESULT:
1643
    double2my_decimal(E_DEC_FATAL_ERROR, sum, val);
1644
    break;
1645
  case DECIMAL_RESULT:
1646
    val= &sum_dec;
1647
    break;
1648
  case INT_RESULT:
1649
    int2my_decimal(E_DEC_FATAL_ERROR, sum_int, unsigned_flag, val);
1650
    break;
1651
  case ROW_RESULT:
1652
  default:
1653
    // This case should never be choosen
1654
    DBUG_ASSERT(0);
1655
    break;
1656
  }
1657
  return val;					// Keep compiler happy
1658
}
1659
1660
1661
String *
1662
Item_sum_hybrid::val_str(String *str)
1663
{
1664
  DBUG_ASSERT(fixed == 1);
1665
  if (null_value)
1666
    return 0;
1667
  switch (hybrid_type) {
1668
  case STRING_RESULT:
1669
    return &value;
1670
  case REAL_RESULT:
1671
    str->set_real(sum,decimals, &my_charset_bin);
1672
    break;
1673
  case DECIMAL_RESULT:
1674
    my_decimal2string(E_DEC_FATAL_ERROR, &sum_dec, 0, 0, 0, str);
1675
    return str;
1676
  case INT_RESULT:
1677
    str->set_int(sum_int, unsigned_flag, &my_charset_bin);
1678
    break;
1679
  case ROW_RESULT:
1680
  default:
1681
    // This case should never be choosen
1682
    DBUG_ASSERT(0);
1683
    break;
1684
  }
1685
  return str;					// Keep compiler happy
1686
}
1687
1688
1689
void Item_sum_hybrid::cleanup()
1690
{
1691
  DBUG_ENTER("Item_sum_hybrid::cleanup");
1692
  Item_sum::cleanup();
1693
  forced_const= FALSE;
1694
1695
  /*
1696
    by default it is TRUE to avoid TRUE reporting by
1697
    Item_func_not_all/Item_func_nop_all if this item was never called.
1698
1699
    no_rows_in_result() set it to FALSE if was not results found.
1700
    If some results found it will be left unchanged.
1701
  */
1702
  was_values= TRUE;
1703
  DBUG_VOID_RETURN;
1704
}
1705
1706
void Item_sum_hybrid::no_rows_in_result()
1707
{
1708
  was_values= FALSE;
1709
  clear();
1710
}
1711
1712
1713
Item *Item_sum_min::copy_or_same(THD* thd)
1714
{
1715
  return new (thd->mem_root) Item_sum_min(thd, this);
1716
}
1717
1718
1719
bool Item_sum_min::add()
1720
{
1721
  switch (hybrid_type) {
1722
  case STRING_RESULT:
1723
  {
1724
    String *result=args[0]->val_str(&tmp_value);
1725
    if (!args[0]->null_value &&
1726
	(null_value || sortcmp(&value,result,collation.collation) > 0))
1727
    {
1728
      value.copy(*result);
1729
      null_value=0;
1730
    }
1731
  }
1732
  break;
1733
  case INT_RESULT:
1734
  {
1735
    longlong nr=args[0]->val_int();
1736
    if (!args[0]->null_value && (null_value ||
1737
				 (unsigned_flag && 
1738
				  (ulonglong) nr < (ulonglong) sum_int) ||
1739
				 (!unsigned_flag && nr < sum_int)))
1740
    {
1741
      sum_int=nr;
1742
      null_value=0;
1743
    }
1744
  }
1745
  break;
1746
  case DECIMAL_RESULT:
1747
  {
1748
    my_decimal value_buff, *val= args[0]->val_decimal(&value_buff);
1749
    if (!args[0]->null_value &&
1750
        (null_value || (my_decimal_cmp(&sum_dec, val) > 0)))
1751
    {
1752
      my_decimal2decimal(val, &sum_dec);
1753
      null_value= 0;
1754
    }
1755
  }
1756
  break;
1757
  case REAL_RESULT:
1758
  {
1759
    double nr= args[0]->val_real();
1760
    if (!args[0]->null_value && (null_value || nr < sum))
1761
    {
1762
      sum=nr;
1763
      null_value=0;
1764
    }
1765
  }
1766
  break;
1767
  case ROW_RESULT:
1768
  default:
1769
    // This case should never be choosen
1770
    DBUG_ASSERT(0);
1771
    break;
1772
  }
1773
  return 0;
1774
}
1775
1776
1777
Item *Item_sum_max::copy_or_same(THD* thd)
1778
{
1779
  return new (thd->mem_root) Item_sum_max(thd, this);
1780
}
1781
1782
1783
bool Item_sum_max::add()
1784
{
1785
  switch (hybrid_type) {
1786
  case STRING_RESULT:
1787
  {
1788
    String *result=args[0]->val_str(&tmp_value);
1789
    if (!args[0]->null_value &&
1790
	(null_value || sortcmp(&value,result,collation.collation) < 0))
1791
    {
1792
      value.copy(*result);
1793
      null_value=0;
1794
    }
1795
  }
1796
  break;
1797
  case INT_RESULT:
1798
  {
1799
    longlong nr=args[0]->val_int();
1800
    if (!args[0]->null_value && (null_value ||
1801
				 (unsigned_flag && 
1802
				  (ulonglong) nr > (ulonglong) sum_int) ||
1803
				 (!unsigned_flag && nr > sum_int)))
1804
    {
1805
      sum_int=nr;
1806
      null_value=0;
1807
    }
1808
  }
1809
  break;
1810
  case DECIMAL_RESULT:
1811
  {
1812
    my_decimal value_buff, *val= args[0]->val_decimal(&value_buff);
1813
    if (!args[0]->null_value &&
1814
        (null_value || (my_decimal_cmp(val, &sum_dec) > 0)))
1815
    {
1816
      my_decimal2decimal(val, &sum_dec);
1817
      null_value= 0;
1818
    }
1819
  }
1820
  break;
1821
  case REAL_RESULT:
1822
  {
1823
    double nr= args[0]->val_real();
1824
    if (!args[0]->null_value && (null_value || nr > sum))
1825
    {
1826
      sum=nr;
1827
      null_value=0;
1828
    }
1829
  }
1830
  break;
1831
  case ROW_RESULT:
1832
  default:
1833
    // This case should never be choosen
1834
    DBUG_ASSERT(0);
1835
    break;
1836
  }
1837
  return 0;
1838
}
1839
1840
1841
/* bit_or and bit_and */
1842
1843
longlong Item_sum_bit::val_int()
1844
{
1845
  DBUG_ASSERT(fixed == 1);
1846
  return (longlong) bits;
1847
}
1848
1849
1850
void Item_sum_bit::clear()
1851
{
1852
  bits= reset_bits;
1853
}
1854
1855
Item *Item_sum_or::copy_or_same(THD* thd)
1856
{
1857
  return new (thd->mem_root) Item_sum_or(thd, this);
1858
}
1859
1860
1861
bool Item_sum_or::add()
1862
{
1863
  ulonglong value= (ulonglong) args[0]->val_int();
1864
  if (!args[0]->null_value)
1865
    bits|=value;
1866
  return 0;
1867
}
1868
1869
Item *Item_sum_xor::copy_or_same(THD* thd)
1870
{
1871
  return new (thd->mem_root) Item_sum_xor(thd, this);
1872
}
1873
1874
1875
bool Item_sum_xor::add()
1876
{
1877
  ulonglong value= (ulonglong) args[0]->val_int();
1878
  if (!args[0]->null_value)
1879
    bits^=value;
1880
  return 0;
1881
}
1882
1883
Item *Item_sum_and::copy_or_same(THD* thd)
1884
{
1885
  return new (thd->mem_root) Item_sum_and(thd, this);
1886
}
1887
1888
1889
bool Item_sum_and::add()
1890
{
1891
  ulonglong value= (ulonglong) args[0]->val_int();
1892
  if (!args[0]->null_value)
1893
    bits&=value;
1894
  return 0;
1895
}
1896
1897
/************************************************************************
1898
** reset result of a Item_sum with is saved in a tmp_table
1899
*************************************************************************/
1900
1901
void Item_sum_num::reset_field()
1902
{
1903
  double nr= args[0]->val_real();
1904
  uchar *res=result_field->ptr;
1905
1906
  if (maybe_null)
1907
  {
1908
    if (args[0]->null_value)
1909
    {
1910
      nr=0.0;
1911
      result_field->set_null();
1912
    }
1913
    else
1914
      result_field->set_notnull();
1915
  }
1916
  float8store(res,nr);
1917
}
1918
1919
1920
void Item_sum_hybrid::reset_field()
1921
{
1922
  switch(hybrid_type) {
1923
  case STRING_RESULT:
1924
  {
1925
    char buff[MAX_FIELD_WIDTH];
1926
    String tmp(buff,sizeof(buff),result_field->charset()),*res;
1927
1928
    res=args[0]->val_str(&tmp);
1929
    if (args[0]->null_value)
1930
    {
1931
      result_field->set_null();
1932
      result_field->reset();
1933
    }
1934
    else
1935
    {
1936
      result_field->set_notnull();
1937
      result_field->store(res->ptr(),res->length(),tmp.charset());
1938
    }
1939
    break;
1940
  }
1941
  case INT_RESULT:
1942
  {
1943
    longlong nr=args[0]->val_int();
1944
1945
    if (maybe_null)
1946
    {
1947
      if (args[0]->null_value)
1948
      {
1949
	nr=0;
1950
	result_field->set_null();
1951
      }
1952
      else
1953
	result_field->set_notnull();
1954
    }
1955
    result_field->store(nr, unsigned_flag);
1956
    break;
1957
  }
1958
  case REAL_RESULT:
1959
  {
1960
    double nr= args[0]->val_real();
1961
1962
    if (maybe_null)
1963
    {
1964
      if (args[0]->null_value)
1965
      {
1966
	nr=0.0;
1967
	result_field->set_null();
1968
      }
1969
      else
1970
	result_field->set_notnull();
1971
    }
1972
    result_field->store(nr);
1973
    break;
1974
  }
1975
  case DECIMAL_RESULT:
1976
  {
1977
    my_decimal value_buff, *arg_dec= args[0]->val_decimal(&value_buff);
1978
1979
    if (maybe_null)
1980
    {
1981
      if (args[0]->null_value)
1982
        result_field->set_null();
1983
      else
1984
        result_field->set_notnull();
1985
    }
1986
    /*
1987
      We must store zero in the field as we will use the field value in
1988
      add()
1989
    */
1990
    if (!arg_dec)                               // Null
1991
      arg_dec= &decimal_zero;
1992
    result_field->store_decimal(arg_dec);
1993
    break;
1994
  }
1995
  case ROW_RESULT:
1996
  default:
1997
    DBUG_ASSERT(0);
1998
  }
1999
}
2000
2001
2002
void Item_sum_sum::reset_field()
2003
{
2004
  if (hybrid_type == DECIMAL_RESULT)
2005
  {
2006
    my_decimal value, *arg_val= args[0]->val_decimal(&value);
2007
    if (!arg_val)                               // Null
2008
      arg_val= &decimal_zero;
2009
    result_field->store_decimal(arg_val);
2010
  }
2011
  else
2012
  {
2013
    DBUG_ASSERT(hybrid_type == REAL_RESULT);
2014
    double nr= args[0]->val_real();			// Nulls also return 0
2015
    float8store(result_field->ptr, nr);
2016
  }
2017
  if (args[0]->null_value)
2018
    result_field->set_null();
2019
  else
2020
    result_field->set_notnull();
2021
}
2022
2023
2024
void Item_sum_count::reset_field()
2025
{
2026
  uchar *res=result_field->ptr;
2027
  longlong nr=0;
2028
2029
  if (!args[0]->maybe_null || !args[0]->is_null())
2030
    nr=1;
2031
  int8store(res,nr);
2032
}
2033
2034
2035
void Item_sum_avg::reset_field()
2036
{
2037
  uchar *res=result_field->ptr;
2038
  if (hybrid_type == DECIMAL_RESULT)
2039
  {
2040
    longlong tmp;
2041
    my_decimal value, *arg_dec= args[0]->val_decimal(&value);
2042
    if (args[0]->null_value)
2043
    {
2044
      arg_dec= &decimal_zero;
2045
      tmp= 0;
2046
    }
2047
    else
2048
      tmp= 1;
2049
    my_decimal2binary(E_DEC_FATAL_ERROR, arg_dec, res, f_precision, f_scale);
2050
    res+= dec_bin_size;
2051
    int8store(res, tmp);
2052
  }
2053
  else
2054
  {
2055
    double nr= args[0]->val_real();
2056
2057
    if (args[0]->null_value)
2058
      bzero(res,sizeof(double)+sizeof(longlong));
2059
    else
2060
    {
2061
      longlong tmp= 1;
2062
      float8store(res,nr);
2063
      res+=sizeof(double);
2064
      int8store(res,tmp);
2065
    }
2066
  }
2067
}
2068
2069
2070
void Item_sum_bit::reset_field()
2071
{
2072
  reset();
2073
  int8store(result_field->ptr, bits);
2074
}
2075
2076
void Item_sum_bit::update_field()
2077
{
2078
  uchar *res=result_field->ptr;
2079
  bits= uint8korr(res);
2080
  add();
2081
  int8store(res, bits);
2082
}
2083
2084
2085
/**
2086
  calc next value and merge it with field_value.
2087
*/
2088
2089
void Item_sum_sum::update_field()
2090
{
2091
  if (hybrid_type == DECIMAL_RESULT)
2092
  {
2093
    my_decimal value, *arg_val= args[0]->val_decimal(&value);
2094
    if (!args[0]->null_value)
2095
    {
2096
      if (!result_field->is_null())
2097
      {
2098
        my_decimal field_value,
2099
                   *field_val= result_field->val_decimal(&field_value);
2100
        my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs, arg_val, field_val);
2101
        result_field->store_decimal(dec_buffs);
2102
      }
2103
      else
2104
      {
2105
        result_field->store_decimal(arg_val);
2106
        result_field->set_notnull();
2107
      }
2108
    }
2109
  }
2110
  else
2111
  {
2112
    double old_nr,nr;
2113
    uchar *res=result_field->ptr;
2114
2115
    float8get(old_nr,res);
2116
    nr= args[0]->val_real();
2117
    if (!args[0]->null_value)
2118
    {
2119
      old_nr+=nr;
2120
      result_field->set_notnull();
2121
    }
2122
    float8store(res,old_nr);
2123
  }
2124
}
2125
2126
2127
void Item_sum_count::update_field()
2128
{
2129
  longlong nr;
2130
  uchar *res=result_field->ptr;
2131
2132
  nr=sint8korr(res);
2133
  if (!args[0]->maybe_null || !args[0]->is_null())
2134
    nr++;
2135
  int8store(res,nr);
2136
}
2137
2138
2139
void Item_sum_avg::update_field()
2140
{
2141
  longlong field_count;
2142
  uchar *res=result_field->ptr;
2143
  if (hybrid_type == DECIMAL_RESULT)
2144
  {
2145
    my_decimal value, *arg_val= args[0]->val_decimal(&value);
2146
    if (!args[0]->null_value)
2147
    {
2148
      binary2my_decimal(E_DEC_FATAL_ERROR, res,
2149
                        dec_buffs + 1, f_precision, f_scale);
2150
      field_count= sint8korr(res + dec_bin_size);
2151
      my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs, arg_val, dec_buffs + 1);
2152
      my_decimal2binary(E_DEC_FATAL_ERROR, dec_buffs,
2153
                        res, f_precision, f_scale);
2154
      res+= dec_bin_size;
2155
      field_count++;
2156
      int8store(res, field_count);
2157
    }
2158
  }
2159
  else
2160
  {
2161
    double nr;
2162
2163
    nr= args[0]->val_real();
2164
    if (!args[0]->null_value)
2165
    {
2166
      double old_nr;
2167
      float8get(old_nr, res);
2168
      field_count= sint8korr(res + sizeof(double));
2169
      old_nr+= nr;
2170
      float8store(res,old_nr);
2171
      res+= sizeof(double);
2172
      field_count++;
2173
      int8store(res, field_count);
2174
    }
2175
  }
2176
}
2177
2178
2179
void Item_sum_hybrid::update_field()
2180
{
2181
  switch (hybrid_type) {
2182
  case STRING_RESULT:
2183
    min_max_update_str_field();
2184
    break;
2185
  case INT_RESULT:
2186
    min_max_update_int_field();
2187
    break;
2188
  case DECIMAL_RESULT:
2189
    min_max_update_decimal_field();
2190
    break;
2191
  default:
2192
    min_max_update_real_field();
2193
  }
2194
}
2195
2196
2197
void
2198
Item_sum_hybrid::min_max_update_str_field()
2199
{
2200
  String *res_str=args[0]->val_str(&value);
2201
2202
  if (!args[0]->null_value)
2203
  {
2204
    result_field->val_str(&tmp_value);
2205
2206
    if (result_field->is_null() ||
2207
	(cmp_sign * sortcmp(res_str,&tmp_value,collation.collation)) < 0)
2208
      result_field->store(res_str->ptr(),res_str->length(),res_str->charset());
2209
    result_field->set_notnull();
2210
  }
2211
}
2212
2213
2214
void
2215
Item_sum_hybrid::min_max_update_real_field()
2216
{
2217
  double nr,old_nr;
2218
2219
  old_nr=result_field->val_real();
2220
  nr= args[0]->val_real();
2221
  if (!args[0]->null_value)
2222
  {
2223
    if (result_field->is_null(0) ||
2224
	(cmp_sign > 0 ? old_nr > nr : old_nr < nr))
2225
      old_nr=nr;
2226
    result_field->set_notnull();
2227
  }
2228
  else if (result_field->is_null(0))
2229
    result_field->set_null();
2230
  result_field->store(old_nr);
2231
}
2232
2233
2234
void
2235
Item_sum_hybrid::min_max_update_int_field()
2236
{
2237
  longlong nr,old_nr;
2238
2239
  old_nr=result_field->val_int();
2240
  nr=args[0]->val_int();
2241
  if (!args[0]->null_value)
2242
  {
2243
    if (result_field->is_null(0))
2244
      old_nr=nr;
2245
    else
2246
    {
2247
      bool res=(unsigned_flag ?
2248
		(ulonglong) old_nr > (ulonglong) nr :
2249
		old_nr > nr);
2250
      /* (cmp_sign > 0 && res) || (!(cmp_sign > 0) && !res) */
2251
      if ((cmp_sign > 0) ^ (!res))
2252
	old_nr=nr;
2253
    }
2254
    result_field->set_notnull();
2255
  }
2256
  else if (result_field->is_null(0))
2257
    result_field->set_null();
2258
  result_field->store(old_nr, unsigned_flag);
2259
}
2260
2261
2262
/**
2263
  @todo
2264
  optimize: do not get result_field in case of args[0] is NULL
2265
*/
2266
void
2267
Item_sum_hybrid::min_max_update_decimal_field()
2268
{
2269
  /* TODO: optimize: do not get result_field in case of args[0] is NULL */
2270
  my_decimal old_val, nr_val;
2271
  const my_decimal *old_nr= result_field->val_decimal(&old_val);
2272
  const my_decimal *nr= args[0]->val_decimal(&nr_val);
2273
  if (!args[0]->null_value)
2274
  {
2275
    if (result_field->is_null(0))
2276
      old_nr=nr;
2277
    else
2278
    {
2279
      bool res= my_decimal_cmp(old_nr, nr) > 0;
2280
      /* (cmp_sign > 0 && res) || (!(cmp_sign > 0) && !res) */
2281
      if ((cmp_sign > 0) ^ (!res))
2282
        old_nr=nr;
2283
    }
2284
    result_field->set_notnull();
2285
  }
2286
  else if (result_field->is_null(0))
2287
    result_field->set_null();
2288
  result_field->store_decimal(old_nr);
2289
}
2290
2291
2292
Item_avg_field::Item_avg_field(Item_result res_type, Item_sum_avg *item)
2293
{
2294
  name=item->name;
2295
  decimals=item->decimals;
2296
  max_length= item->max_length;
2297
  unsigned_flag= item->unsigned_flag;
2298
  field=item->result_field;
2299
  maybe_null=1;
2300
  hybrid_type= res_type;
2301
  prec_increment= item->prec_increment;
2302
  if (hybrid_type == DECIMAL_RESULT)
2303
  {
2304
    f_scale= item->f_scale;
2305
    f_precision= item->f_precision;
2306
    dec_bin_size= item->dec_bin_size;
2307
  }
2308
}
2309
2310
double Item_avg_field::val_real()
2311
{
2312
  // fix_fields() never calls for this Item
2313
  double nr;
2314
  longlong count;
2315
  uchar *res;
2316
2317
  if (hybrid_type == DECIMAL_RESULT)
2318
    return val_real_from_decimal();
2319
2320
  float8get(nr,field->ptr);
2321
  res= (field->ptr+sizeof(double));
2322
  count= sint8korr(res);
2323
2324
  if ((null_value= !count))
2325
    return 0.0;
2326
  return nr/(double) count;
2327
}
2328
2329
2330
longlong Item_avg_field::val_int()
2331
{
2332
  return (longlong) rint(val_real());
2333
}
2334
2335
2336
my_decimal *Item_avg_field::val_decimal(my_decimal *dec_buf)
2337
{
2338
  // fix_fields() never calls for this Item
2339
  if (hybrid_type == REAL_RESULT)
2340
    return val_decimal_from_real(dec_buf);
2341
2342
  longlong count= sint8korr(field->ptr + dec_bin_size);
2343
  if ((null_value= !count))
2344
    return 0;
2345
2346
  my_decimal dec_count, dec_field;
2347
  binary2my_decimal(E_DEC_FATAL_ERROR,
2348
                    field->ptr, &dec_field, f_precision, f_scale);
2349
  int2my_decimal(E_DEC_FATAL_ERROR, count, 0, &dec_count);
2350
  my_decimal_div(E_DEC_FATAL_ERROR, dec_buf,
2351
                 &dec_field, &dec_count, prec_increment);
2352
  return dec_buf;
2353
}
2354
2355
2356
String *Item_avg_field::val_str(String *str)
2357
{
2358
  // fix_fields() never calls for this Item
2359
  if (hybrid_type == DECIMAL_RESULT)
2360
    return val_string_from_decimal(str);
2361
  return val_string_from_real(str);
2362
}
2363
2364
2365
Item_std_field::Item_std_field(Item_sum_std *item)
2366
  : Item_variance_field(item)
2367
{
2368
}
2369
2370
2371
double Item_std_field::val_real()
2372
{
2373
  double nr;
2374
  // fix_fields() never calls for this Item
2375
  nr= Item_variance_field::val_real();
2376
  DBUG_ASSERT(nr >= 0.0);
2377
  return sqrt(nr);
2378
}
2379
2380
2381
my_decimal *Item_std_field::val_decimal(my_decimal *dec_buf)
2382
{
2383
  /*
2384
    We can't call val_decimal_from_real() for DECIMAL_RESULT as
2385
    Item_variance_field::val_real() would cause an infinite loop
2386
  */
2387
  my_decimal tmp_dec, *dec;
2388
  double nr;
2389
  if (hybrid_type == REAL_RESULT)
2390
    return val_decimal_from_real(dec_buf);
2391
2392
  dec= Item_variance_field::val_decimal(dec_buf);
2393
  if (!dec)
2394
    return 0;
2395
  my_decimal2double(E_DEC_FATAL_ERROR, dec, &nr);
2396
  DBUG_ASSERT(nr >= 0.0);
2397
  nr= sqrt(nr);
2398
  double2my_decimal(E_DEC_FATAL_ERROR, nr, &tmp_dec);
2399
  my_decimal_round(E_DEC_FATAL_ERROR, &tmp_dec, decimals, FALSE, dec_buf);
2400
  return dec_buf;
2401
}
2402
2403
2404
Item_variance_field::Item_variance_field(Item_sum_variance *item)
2405
{
2406
  name=item->name;
2407
  decimals=item->decimals;
2408
  max_length=item->max_length;
2409
  unsigned_flag= item->unsigned_flag;
2410
  field=item->result_field;
2411
  maybe_null=1;
2412
  sample= item->sample;
2413
  prec_increment= item->prec_increment;
2414
  if ((hybrid_type= item->hybrid_type) == DECIMAL_RESULT)
2415
  {
2416
    f_scale0= item->f_scale0;
2417
    f_precision0= item->f_precision0;
2418
    dec_bin_size0= item->dec_bin_size0;
2419
    f_scale1= item->f_scale1;
2420
    f_precision1= item->f_precision1;
2421
    dec_bin_size1= item->dec_bin_size1;
2422
  }
2423
}
2424
2425
2426
double Item_variance_field::val_real()
2427
{
2428
  // fix_fields() never calls for this Item
2429
  if (hybrid_type == DECIMAL_RESULT)
2430
    return val_real_from_decimal();
2431
2432
  double recurrence_s;
2433
  ulonglong count;
2434
  float8get(recurrence_s, (field->ptr + sizeof(double)));
2435
  count=sint8korr(field->ptr+sizeof(double)*2);
2436
2437
  if ((null_value= (count <= sample)))
2438
    return 0.0;
2439
2440
  return variance_fp_recurrence_result(recurrence_s, count, sample);
2441
}
2442
2443
2444
/****************************************************************************
2445
** COUNT(DISTINCT ...)
2446
****************************************************************************/
2447
2448
int simple_str_key_cmp(void* arg, uchar* key1, uchar* key2)
2449
{
2450
  Field *f= (Field*) arg;
2451
  return f->cmp(key1, key2);
2452
}
2453
2454
/**
2455
  Did not make this one static - at least gcc gets confused when
2456
  I try to declare a static function as a friend. If you can figure
2457
  out the syntax to make a static function a friend, make this one
2458
  static
2459
*/
2460
2461
int composite_key_cmp(void* arg, uchar* key1, uchar* key2)
2462
{
2463
  Item_sum_count_distinct* item = (Item_sum_count_distinct*)arg;
2464
  Field **field    = item->table->field;
2465
  Field **field_end= field + item->table->s->fields;
2466
  uint32 *lengths=item->field_lengths;
2467
  for (; field < field_end; ++field)
2468
  {
2469
    Field* f = *field;
2470
    int len = *lengths++;
2471
    int res = f->cmp(key1, key2);
2472
    if (res)
2473
      return res;
2474
    key1 += len;
2475
    key2 += len;
2476
  }
2477
  return 0;
2478
}
2479
2480
2481
C_MODE_START
2482
2483
static int count_distinct_walk(void *elem, element_count count, void *arg)
2484
{
2485
  (*((ulonglong*)arg))++;
2486
  return 0;
2487
}
2488
2489
C_MODE_END
2490
2491
2492
void Item_sum_count_distinct::cleanup()
2493
{
2494
  DBUG_ENTER("Item_sum_count_distinct::cleanup");
2495
  Item_sum_int::cleanup();
2496
2497
  /* Free objects only if we own them. */
2498
  if (!original)
2499
  {
2500
    /*
2501
      We need to delete the table and the tree in cleanup() as
2502
      they were allocated in the runtime memroot. Using the runtime
2503
      memroot reduces memory footprint for PS/SP and simplifies setup().
2504
    */
2505
    delete tree;
2506
    tree= 0;
2507
    is_evaluated= FALSE;
2508
    if (table)
2509
    {
2510
      free_tmp_table(table->in_use, table);
2511
      table= 0;
2512
    }
2513
    delete tmp_table_param;
2514
    tmp_table_param= 0;
2515
  }
2516
  always_null= FALSE;
2517
  DBUG_VOID_RETURN;
2518
}
2519
2520
2521
/**
2522
  This is used by rollup to create a separate usable copy of
2523
  the function.
2524
*/
2525
2526
void Item_sum_count_distinct::make_unique()
2527
{
2528
  table=0;
2529
  original= 0;
2530
  force_copy_fields= 1;
2531
  tree= 0;
2532
  is_evaluated= FALSE;
2533
  tmp_table_param= 0;
2534
  always_null= FALSE;
2535
}
2536
2537
2538
Item_sum_count_distinct::~Item_sum_count_distinct()
2539
{
2540
  cleanup();
2541
}
2542
2543
2544
bool Item_sum_count_distinct::setup(THD *thd)
2545
{
2546
  List<Item> list;
2547
  SELECT_LEX *select_lex= thd->lex->current_select;
2548
2549
  /*
2550
    Setup can be called twice for ROLLUP items. This is a bug.
2551
    Please add DBUG_ASSERT(tree == 0) here when it's fixed.
2552
    It's legal to call setup() more than once when in a subquery
2553
  */
2554
  if (tree || table || tmp_table_param)
2555
    return FALSE;
2556
2557
  if (!(tmp_table_param= new TMP_TABLE_PARAM))
2558
    return TRUE;
2559
2560
  /* Create a table with an unique key over all parameters */
2561
  for (uint i=0; i < arg_count ; i++)
2562
  {
2563
    Item *item=args[i];
2564
    if (list.push_back(item))
2565
      return TRUE;                              // End of memory
2566
    if (item->const_item() && item->is_null())
2567
      always_null= 1;
2568
  }
2569
  if (always_null)
2570
    return FALSE;
2571
  count_field_types(select_lex, tmp_table_param, list, 0);
2572
  tmp_table_param->force_copy_fields= force_copy_fields;
2573
  DBUG_ASSERT(table == 0);
2574
  /*
2575
    Make create_tmp_table() convert BIT columns to BIGINT.
2576
    This is needed because BIT fields store parts of their data in table's
2577
    null bits, and we don't have methods to compare two table records, which
2578
    is needed by Unique which is used when HEAP table is used.
2579
  */
2580
  {
2581
    List_iterator_fast<Item> li(list);
2582
    Item *item;
2583
    while ((item= li++))
2584
    {
2585
      if (item->type() == Item::FIELD_ITEM &&
2586
          ((Item_field*)item)->field->type() == FIELD_TYPE_BIT)
2587
        item->marker=4;
2588
    }
2589
  }
2590
2591
  if (!(table= create_tmp_table(thd, tmp_table_param, list, (ORDER*) 0, 1,
2592
				0,
2593
				(select_lex->options | thd->options),
2594
				HA_POS_ERROR, (char*)"")))
2595
    return TRUE;
2596
  table->file->extra(HA_EXTRA_NO_ROWS);		// Don't update rows
2597
  table->no_rows=1;
2598
2599
  if (table->s->db_type() == heap_hton)
2600
  {
2601
    /*
2602
      No blobs, otherwise it would have been MyISAM: set up a compare
2603
      function and its arguments to use with Unique.
2604
    */
2605
    qsort_cmp2 compare_key;
2606
    void* cmp_arg;
2607
    Field **field= table->field;
2608
    Field **field_end= field + table->s->fields;
2609
    bool all_binary= TRUE;
2610
2611
    for (tree_key_length= 0; field < field_end; ++field)
2612
    {
2613
      Field *f= *field;
2614
      enum enum_field_types f_type= f->type();
2615
      tree_key_length+= f->pack_length();
2616
      if ((f_type == MYSQL_TYPE_VARCHAR) || (!f->binary() && (f_type == MYSQL_TYPE_STRING || f_type == MYSQL_TYPE_VAR_STRING)))
2617
      {
2618
        all_binary= FALSE;
2619
        break;
2620
      }
2621
    }
2622
    if (all_binary)
2623
    {
2624
      cmp_arg= (void*) &tree_key_length;
2625
      compare_key= (qsort_cmp2) simple_raw_key_cmp;
2626
    }
2627
    else
2628
    {
2629
      if (table->s->fields == 1)
2630
      {
2631
        /*
2632
          If we have only one field, which is the most common use of
2633
          count(distinct), it is much faster to use a simpler key
2634
          compare method that can take advantage of not having to worry
2635
          about other fields.
2636
        */
2637
        compare_key= (qsort_cmp2) simple_str_key_cmp;
2638
        cmp_arg= (void*) table->field[0];
2639
        /* tree_key_length has been set already */
2640
      }
2641
      else
2642
      {
2643
        uint32 *length;
2644
        compare_key= (qsort_cmp2) composite_key_cmp;
2645
        cmp_arg= (void*) this;
2646
        field_lengths= (uint32*) thd->alloc(table->s->fields * sizeof(uint32));
2647
        for (tree_key_length= 0, length= field_lengths, field= table->field;
2648
             field < field_end; ++field, ++length)
2649
        {
2650
          *length= (*field)->pack_length();
2651
          tree_key_length+= *length;
2652
        }
2653
      }
2654
    }
2655
    DBUG_ASSERT(tree == 0);
2656
    tree= new Unique(compare_key, cmp_arg, tree_key_length,
2657
                     thd->variables.max_heap_table_size);
2658
    /*
2659
      The only time tree_key_length could be 0 is if someone does
2660
      count(distinct) on a char(0) field - stupid thing to do,
2661
      but this has to be handled - otherwise someone can crash
2662
      the server with a DoS attack
2663
    */
2664
    is_evaluated= FALSE;
2665
    if (! tree)
2666
      return TRUE;
2667
  }
2668
  return FALSE;
2669
}
2670
2671
2672
Item *Item_sum_count_distinct::copy_or_same(THD* thd) 
2673
{
2674
  return new (thd->mem_root) Item_sum_count_distinct(thd, this);
2675
}
2676
2677
2678
void Item_sum_count_distinct::clear()
2679
{
2680
  /* tree and table can be both null only if always_null */
2681
  is_evaluated= FALSE;
2682
  if (tree)
2683
  {
2684
    tree->reset();
2685
  }
2686
  else if (table)
2687
  {
2688
    table->file->extra(HA_EXTRA_NO_CACHE);
2689
    table->file->ha_delete_all_rows();
2690
    table->file->extra(HA_EXTRA_WRITE_CACHE);
2691
  }
2692
}
2693
2694
bool Item_sum_count_distinct::add()
2695
{
2696
  int error;
2697
  if (always_null)
2698
    return 0;
2699
  copy_fields(tmp_table_param);
2700
  copy_funcs(tmp_table_param->items_to_copy);
2701
2702
  for (Field **field=table->field ; *field ; field++)
2703
    if ((*field)->is_real_null(0))
2704
      return 0;					// Don't count NULL
2705
2706
  is_evaluated= FALSE;
2707
  if (tree)
2708
  {
2709
    /*
2710
      The first few bytes of record (at least one) are just markers
2711
      for deleted and NULLs. We want to skip them since they will
2712
      bloat the tree without providing any valuable info. Besides,
2713
      key_length used to initialize the tree didn't include space for them.
2714
    */
2715
    return tree->unique_add(table->record[0] + table->s->null_bytes);
2716
  }
2717
  if ((error= table->file->ha_write_row(table->record[0])) &&
2718
      table->file->is_fatal_error(error, HA_CHECK_DUP))
2719
    return TRUE;
2720
  return FALSE;
2721
}
2722
2723
2724
longlong Item_sum_count_distinct::val_int()
2725
{
2726
  int error;
2727
  DBUG_ASSERT(fixed == 1);
2728
  if (!table)					// Empty query
2729
    return LL(0);
2730
  if (tree)
2731
  {
2732
    if (is_evaluated)
2733
      return count;
2734
2735
    if (tree->elements == 0)
2736
      return (longlong) tree->elements_in_tree(); // everything fits in memory
2737
    count= 0;
2738
    tree->walk(count_distinct_walk, (void*) &count);
2739
    is_evaluated= TRUE;
2740
    return (longlong) count;
2741
  }
2742
2743
  error= table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
2744
2745
  if(error)
2746
  {
2747
    table->file->print_error(error, MYF(0));
2748
  }
2749
2750
  return table->file->stats.records;
2751
}
2752
2753
2754
/****************************************************************************
2755
** Functions to handle dynamic loadable aggregates
2756
** Original source by: Alexis Mikhailov <root@medinf.chuvashia.su>
2757
** Adapted for UDAs by: Andreas F. Bobak <bobak@relog.ch>.
2758
** Rewritten by: Monty.
2759
****************************************************************************/
2760
2761
#ifdef HAVE_DLOPEN
2762
2763
void Item_udf_sum::clear()
2764
{
2765
  DBUG_ENTER("Item_udf_sum::clear");
2766
  udf.clear();
2767
  DBUG_VOID_RETURN;
2768
}
2769
2770
bool Item_udf_sum::add()
2771
{
2772
  DBUG_ENTER("Item_udf_sum::add");
2773
  udf.add(&null_value);
2774
  DBUG_RETURN(0);
2775
}
2776
2777
void Item_udf_sum::cleanup()
2778
{
2779
  /*
2780
    udf_handler::cleanup() nicely handles case when we have not
2781
    original item but one created by copy_or_same() method.
2782
  */
2783
  udf.cleanup();
2784
  Item_sum::cleanup();
2785
}
2786
2787
2788
void Item_udf_sum::print(String *str, enum_query_type query_type)
2789
{
2790
  str->append(func_name());
2791
  str->append('(');
2792
  for (uint i=0 ; i < arg_count ; i++)
2793
  {
2794
    if (i)
2795
      str->append(',');
2796
    args[i]->print(str, query_type);
2797
  }
2798
  str->append(')');
2799
}
2800
2801
2802
Item *Item_sum_udf_float::copy_or_same(THD* thd)
2803
{
2804
  return new (thd->mem_root) Item_sum_udf_float(thd, this);
2805
}
2806
2807
double Item_sum_udf_float::val_real()
2808
{
2809
  DBUG_ASSERT(fixed == 1);
2810
  DBUG_ENTER("Item_sum_udf_float::val");
2811
  DBUG_PRINT("info",("result_type: %d  arg_count: %d",
2812
		     args[0]->result_type(), arg_count));
2813
  DBUG_RETURN(udf.val(&null_value));
2814
}
2815
2816
2817
String *Item_sum_udf_float::val_str(String *str)
2818
{
2819
  return val_string_from_real(str);
2820
}
2821
2822
2823
my_decimal *Item_sum_udf_float::val_decimal(my_decimal *dec)
2824
{
2825
  return val_decimal_from_real(dec);
2826
}
2827
2828
2829
String *Item_sum_udf_decimal::val_str(String *str)
2830
{
2831
  return val_string_from_decimal(str);
2832
}
2833
2834
2835
double Item_sum_udf_decimal::val_real()
2836
{
2837
  return val_real_from_decimal();
2838
}
2839
2840
2841
longlong Item_sum_udf_decimal::val_int()
2842
{
2843
  return val_int_from_decimal();
2844
}
2845
2846
2847
my_decimal *Item_sum_udf_decimal::val_decimal(my_decimal *dec_buf)
2848
{
2849
  DBUG_ASSERT(fixed == 1);
2850
  DBUG_ENTER("Item_func_udf_decimal::val_decimal");
2851
  DBUG_PRINT("info",("result_type: %d  arg_count: %d",
2852
                     args[0]->result_type(), arg_count));
2853
2854
  DBUG_RETURN(udf.val_decimal(&null_value, dec_buf));
2855
}
2856
2857
2858
Item *Item_sum_udf_decimal::copy_or_same(THD* thd)
2859
{
2860
  return new (thd->mem_root) Item_sum_udf_decimal(thd, this);
2861
}
2862
2863
2864
Item *Item_sum_udf_int::copy_or_same(THD* thd)
2865
{
2866
  return new (thd->mem_root) Item_sum_udf_int(thd, this);
2867
}
2868
2869
longlong Item_sum_udf_int::val_int()
2870
{
2871
  DBUG_ASSERT(fixed == 1);
2872
  DBUG_ENTER("Item_sum_udf_int::val_int");
2873
  DBUG_PRINT("info",("result_type: %d  arg_count: %d",
2874
		     args[0]->result_type(), arg_count));
2875
  DBUG_RETURN(udf.val_int(&null_value));
2876
}
2877
2878
2879
String *Item_sum_udf_int::val_str(String *str)
2880
{
2881
  return val_string_from_int(str);
2882
}
2883
2884
my_decimal *Item_sum_udf_int::val_decimal(my_decimal *dec)
2885
{
2886
  return val_decimal_from_int(dec);
2887
}
2888
2889
2890
/** Default max_length is max argument length. */
2891
2892
void Item_sum_udf_str::fix_length_and_dec()
2893
{
2894
  DBUG_ENTER("Item_sum_udf_str::fix_length_and_dec");
2895
  max_length=0;
2896
  for (uint i = 0; i < arg_count; i++)
2897
    set_if_bigger(max_length,args[i]->max_length);
2898
  DBUG_VOID_RETURN;
2899
}
2900
2901
2902
Item *Item_sum_udf_str::copy_or_same(THD* thd)
2903
{
2904
  return new (thd->mem_root) Item_sum_udf_str(thd, this);
2905
}
2906
2907
2908
my_decimal *Item_sum_udf_str::val_decimal(my_decimal *dec)
2909
{
2910
  return val_decimal_from_string(dec);
2911
}
2912
2913
String *Item_sum_udf_str::val_str(String *str)
2914
{
2915
  DBUG_ASSERT(fixed == 1);
2916
  DBUG_ENTER("Item_sum_udf_str::str");
2917
  String *res=udf.val_str(str,&str_value);
2918
  null_value = !res;
2919
  DBUG_RETURN(res);
2920
}
2921
2922
#endif /* HAVE_DLOPEN */
2923
2924
2925
/*****************************************************************************
2926
 GROUP_CONCAT function
2927
2928
 SQL SYNTAX:
2929
  GROUP_CONCAT([DISTINCT] expr,... [ORDER BY col [ASC|DESC],...]
2930
    [SEPARATOR str_const])
2931
2932
 concat of values from "group by" operation
2933
2934
 BUGS
2935
   Blobs doesn't work with DISTINCT or ORDER BY
2936
*****************************************************************************/
2937
2938
2939
/** 
2940
  Compares the values for fields in expr list of GROUP_CONCAT.
2941
  @note
2942
       
2943
     GROUP_CONCAT([DISTINCT] expr [,expr ...]
2944
              [ORDER BY {unsigned_integer | col_name | expr}
2945
                  [ASC | DESC] [,col_name ...]]
2946
              [SEPARATOR str_val])
2947
 
2948
  @return
2949
  @retval -1 : key1 < key2 
2950
  @retval  0 : key1 = key2
2951
  @retval  1 : key1 > key2 
2952
*/
2953
2954
int group_concat_key_cmp_with_distinct(void* arg, const void* key1, 
2955
                                       const void* key2)
2956
{
2957
  Item_func_group_concat *item_func= (Item_func_group_concat*)arg;
2958
  TABLE *table= item_func->table;
2959
2960
  for (uint i= 0; i < item_func->arg_count_field; i++)
2961
  {
2962
    Item *item= item_func->args[i];
2963
    /* 
2964
      If field_item is a const item then either get_tp_table_field returns 0
2965
      or it is an item over a const table. 
2966
    */
2967
    if (item->const_item())
2968
      continue;
2969
    /*
2970
      We have to use get_tmp_table_field() instead of
2971
      real_item()->get_tmp_table_field() because we want the field in
2972
      the temporary table, not the original field
2973
    */
2974
    Field *field= item->get_tmp_table_field();
2975
    int res;
2976
    uint offset= field->offset(field->table->record[0])-table->s->null_bytes;
2977
    if((res= field->cmp((uchar*)key1 + offset, (uchar*)key2 + offset)))
2978
      return res;
2979
  }
2980
  return 0;
2981
}
2982
2983
2984
/**
2985
  function of sort for syntax: GROUP_CONCAT(expr,... ORDER BY col,... )
2986
*/
2987
2988
int group_concat_key_cmp_with_order(void* arg, const void* key1, 
2989
                                    const void* key2)
2990
{
2991
  Item_func_group_concat* grp_item= (Item_func_group_concat*) arg;
2992
  ORDER **order_item, **end;
2993
  TABLE *table= grp_item->table;
2994
2995
  for (order_item= grp_item->order, end=order_item+ grp_item->arg_count_order;
2996
       order_item < end;
2997
       order_item++)
2998
  {
2999
    Item *item= *(*order_item)->item;
3000
    /*
3001
      We have to use get_tmp_table_field() instead of
3002
      real_item()->get_tmp_table_field() because we want the field in
3003
      the temporary table, not the original field
3004
    */
3005
    Field *field= item->get_tmp_table_field();
3006
    /* 
3007
      If item is a const item then either get_tp_table_field returns 0
3008
      or it is an item over a const table. 
3009
    */
3010
    if (field && !item->const_item())
3011
    {
3012
      int res;
3013
      uint offset= (field->offset(field->table->record[0]) -
3014
                    table->s->null_bytes);
3015
      if ((res= field->cmp((uchar*)key1 + offset, (uchar*)key2 + offset)))
3016
        return (*order_item)->asc ? res : -res;
3017
    }
3018
  }
3019
  /*
3020
    We can't return 0 because in that case the tree class would remove this
3021
    item as double value. This would cause problems for case-changes and
3022
    if the returned values are not the same we do the sort on.
3023
  */
3024
  return 1;
3025
}
3026
3027
3028
/**
3029
  Append data from current leaf to item->result.
3030
*/
3031
3032
int dump_leaf_key(uchar* key, element_count count __attribute__((unused)),
3033
                  Item_func_group_concat *item)
3034
{
3035
  TABLE *table= item->table;
3036
  String tmp((char *)table->record[1], table->s->reclength,
3037
             default_charset_info);
3038
  String tmp2;
3039
  String *result= &item->result;
3040
  Item **arg= item->args, **arg_end= item->args + item->arg_count_field;
3041
  uint old_length= result->length();
3042
3043
  if (item->no_appended)
3044
    item->no_appended= FALSE;
3045
  else
3046
    result->append(*item->separator);
3047
3048
  tmp.length(0);
3049
3050
  for (; arg < arg_end; arg++)
3051
  {
3052
    String *res;
3053
    if (! (*arg)->const_item())
3054
    {
3055
      /*
3056
	We have to use get_tmp_table_field() instead of
3057
	real_item()->get_tmp_table_field() because we want the field in
3058
	the temporary table, not the original field
3059
        We also can't use table->field array to access the fields
3060
        because it contains both order and arg list fields.
3061
      */
3062
      Field *field= (*arg)->get_tmp_table_field();
3063
      uint offset= (field->offset(field->table->record[0]) -
3064
                    table->s->null_bytes);
3065
      DBUG_ASSERT(offset < table->s->reclength);
3066
      res= field->val_str(&tmp, key + offset);
3067
    }
3068
    else
3069
      res= (*arg)->val_str(&tmp);
3070
    if (res)
3071
      result->append(*res);
3072
  }
3073
3074
  /* stop if length of result more than max_length */
3075
  if (result->length() > item->max_length)
3076
  {
3077
    int well_formed_error;
3078
    CHARSET_INFO *cs= item->collation.collation;
3079
    const char *ptr= result->ptr();
3080
    uint add_length;
3081
    /*
3082
      It's ok to use item->result.length() as the fourth argument
3083
      as this is never used to limit the length of the data.
3084
      Cut is done with the third argument.
3085
    */
3086
    add_length= cs->cset->well_formed_len(cs,
3087
                                          ptr + old_length,
3088
                                          ptr + item->max_length,
3089
                                          result->length(),
3090
                                          &well_formed_error);
3091
    result->length(old_length + add_length);
3092
    item->count_cut_values++;
3093
    item->warning_for_row= TRUE;
3094
    return 1;
3095
  }
3096
  return 0;
3097
}
3098
3099
3100
/**
3101
  Constructor of Item_func_group_concat.
3102
3103
  @param distinct_arg   distinct
3104
  @param select_list    list of expression for show values
3105
  @param order_list     list of sort columns
3106
  @param separator_arg  string value of separator.
3107
*/
3108
3109
Item_func_group_concat::
3110
Item_func_group_concat(Name_resolution_context *context_arg,
3111
                       bool distinct_arg, List<Item> *select_list,
3112
                       SQL_LIST *order_list, String *separator_arg)
3113
  :tmp_table_param(0), warning(0),
3114
   separator(separator_arg), tree(0), unique_filter(NULL), table(0),
3115
   order(0), context(context_arg),
3116
   arg_count_order(order_list ? order_list->elements : 0),
3117
   arg_count_field(select_list->elements),
3118
   count_cut_values(0),
3119
   distinct(distinct_arg),
3120
   warning_for_row(FALSE),
3121
   force_copy_fields(0), original(0)
3122
{
3123
  Item *item_select;
3124
  Item **arg_ptr;
3125
3126
  quick_group= FALSE;
3127
  arg_count= arg_count_field + arg_count_order;
3128
3129
  /*
3130
    We need to allocate:
3131
    args - arg_count_field+arg_count_order
3132
           (for possible order items in temporare tables)
3133
    order - arg_count_order
3134
  */
3135
  if (!(args= (Item**) sql_alloc(sizeof(Item*) * arg_count +
3136
                                 sizeof(ORDER*)*arg_count_order)))
3137
    return;
3138
3139
  order= (ORDER**)(args + arg_count);
3140
3141
  /* fill args items of show and sort */
3142
  List_iterator_fast<Item> li(*select_list);
3143
3144
  for (arg_ptr=args ; (item_select= li++) ; arg_ptr++)
3145
    *arg_ptr= item_select;
3146
3147
  if (arg_count_order)
3148
  {
3149
    ORDER **order_ptr= order;
3150
    for (ORDER *order_item= (ORDER*) order_list->first;
3151
         order_item != NULL;
3152
         order_item= order_item->next)
3153
    {
3154
      (*order_ptr++)= order_item;
3155
      *arg_ptr= *order_item->item;
3156
      order_item->item= arg_ptr++;
3157
    }
3158
  }
3159
}
3160
3161
3162
Item_func_group_concat::Item_func_group_concat(THD *thd,
3163
                                               Item_func_group_concat *item)
3164
  :Item_sum(thd, item),
3165
  tmp_table_param(item->tmp_table_param),
3166
  warning(item->warning),
3167
  separator(item->separator),
3168
  tree(item->tree),
3169
  unique_filter(item->unique_filter),
3170
  table(item->table),
3171
  order(item->order),
3172
  context(item->context),
3173
  arg_count_order(item->arg_count_order),
3174
  arg_count_field(item->arg_count_field),
3175
  count_cut_values(item->count_cut_values),
3176
  distinct(item->distinct),
3177
  warning_for_row(item->warning_for_row),
3178
  always_null(item->always_null),
3179
  force_copy_fields(item->force_copy_fields),
3180
  original(item)
3181
{
3182
  quick_group= item->quick_group;
3183
  result.set_charset(collation.collation);
3184
}
3185
3186
3187
3188
void Item_func_group_concat::cleanup()
3189
{
3190
  DBUG_ENTER("Item_func_group_concat::cleanup");
3191
  Item_sum::cleanup();
3192
3193
  /* Adjust warning message to include total number of cut values */
3194
  if (warning)
3195
  {
3196
    char warn_buff[MYSQL_ERRMSG_SIZE];
3197
    sprintf(warn_buff, ER(ER_CUT_VALUE_GROUP_CONCAT), count_cut_values);
3198
    warning->set_msg(current_thd, warn_buff);
3199
    warning= 0;
3200
  }
3201
3202
  /*
3203
    Free table and tree if they belong to this item (if item have not pointer
3204
    to original item from which was made copy => it own its objects )
3205
  */
3206
  if (!original)
3207
  {
3208
    delete tmp_table_param;
3209
    tmp_table_param= 0;
3210
    if (table)
3211
    {
3212
      THD *thd= table->in_use;
3213
      free_tmp_table(thd, table);
3214
      table= 0;
3215
      if (tree)
3216
      {
3217
        delete_tree(tree);
3218
        tree= 0;
3219
      }
3220
      if (unique_filter)
3221
      {
3222
        delete unique_filter;
3223
        unique_filter= NULL;
3224
      }
3225
      if (warning)
3226
      {
3227
        char warn_buff[MYSQL_ERRMSG_SIZE];
3228
        sprintf(warn_buff, ER(ER_CUT_VALUE_GROUP_CONCAT), count_cut_values);
3229
        warning->set_msg(thd, warn_buff);
3230
        warning= 0;
3231
      }
3232
    }
3233
    DBUG_ASSERT(tree == 0 && warning == 0);
3234
  }
3235
  DBUG_VOID_RETURN;
3236
}
3237
3238
3239
Item *Item_func_group_concat::copy_or_same(THD* thd)
3240
{
3241
  return new (thd->mem_root) Item_func_group_concat(thd, this);
3242
}
3243
3244
3245
void Item_func_group_concat::clear()
3246
{
3247
  result.length(0);
3248
  result.copy();
3249
  null_value= TRUE;
3250
  warning_for_row= FALSE;
3251
  no_appended= TRUE;
3252
  if (tree)
3253
    reset_tree(tree);
3254
  if (distinct)
3255
    unique_filter->reset();
3256
  /* No need to reset the table as we never call write_row */
3257
}
3258
3259
3260
bool Item_func_group_concat::add()
3261
{
3262
  if (always_null)
3263
    return 0;
3264
  copy_fields(tmp_table_param);
3265
  copy_funcs(tmp_table_param->items_to_copy);
3266
3267
  for (uint i= 0; i < arg_count_field; i++)
3268
  {
3269
    Item *show_item= args[i];
3270
    if (!show_item->const_item())
3271
    {
3272
      Field *f= show_item->get_tmp_table_field();
3273
      if (f->is_null_in_record((const uchar*) table->record[0]))
3274
        return 0;                               // Skip row if it contains null
3275
    }
3276
  }
3277
3278
  null_value= FALSE;
3279
  bool row_eligible= TRUE;
3280
3281
  if (distinct) 
3282
  {
3283
    /* Filter out duplicate rows. */
3284
    uint count= unique_filter->elements_in_tree();
3285
    unique_filter->unique_add(table->record[0] + table->s->null_bytes);
3286
    if (count == unique_filter->elements_in_tree())
3287
      row_eligible= FALSE;
3288
  }
3289
3290
  TREE_ELEMENT *el= 0;                          // Only for safety
3291
  if (row_eligible && tree)
3292
    el= tree_insert(tree, table->record[0] + table->s->null_bytes, 0,
3293
                    tree->custom_arg);
3294
  /*
3295
    If the row is not a duplicate (el->count == 1)
3296
    we can dump the row here in case of GROUP_CONCAT(DISTINCT...)
3297
    instead of doing tree traverse later.
3298
  */
3299
  if (row_eligible && !warning_for_row &&
3300
      (!tree || (el->count == 1 && distinct && !arg_count_order)))
3301
    dump_leaf_key(table->record[0] + table->s->null_bytes, 1, this);
3302
3303
  return 0;
3304
}
3305
3306
3307
bool
3308
Item_func_group_concat::fix_fields(THD *thd, Item **ref)
3309
{
3310
  uint i;                       /* for loop variable */
3311
  DBUG_ASSERT(fixed == 0);
3312
3313
  if (init_sum_func_check(thd))
3314
    return TRUE;
3315
3316
  maybe_null= 1;
3317
3318
  /*
3319
    Fix fields for select list and ORDER clause
3320
  */
3321
3322
  for (i=0 ; i < arg_count ; i++)
3323
  {
3324
    if ((!args[i]->fixed &&
3325
         args[i]->fix_fields(thd, args + i)) ||
3326
        args[i]->check_cols(1))
3327
      return TRUE;
3328
  }
3329
3330
  if (agg_item_charsets(collation, func_name(),
3331
                        args,
3332
			/* skip charset aggregation for order columns */
3333
			arg_count - arg_count_order,
3334
			MY_COLL_ALLOW_CONV, 1))
3335
    return 1;
3336
3337
  result.set_charset(collation.collation);
3338
  result_field= 0;
3339
  null_value= 1;
3340
  max_length= thd->variables.group_concat_max_len;
3341
3342
  uint32 offset;
3343
  if (separator->needs_conversion(separator->length(), separator->charset(),
3344
                                  collation.collation, &offset))
3345
  {
3346
    uint32 buflen= collation.collation->mbmaxlen * separator->length();
3347
    uint errors, conv_length;
3348
    char *buf;
3349
    String *new_separator;
3350
3351
    if (!(buf= (char*) thd->stmt_arena->alloc(buflen)) ||
3352
        !(new_separator= new(thd->stmt_arena->mem_root)
3353
                           String(buf, buflen, collation.collation)))
3354
      return TRUE;
3355
    
3356
    conv_length= copy_and_convert(buf, buflen, collation.collation,
3357
                                  separator->ptr(), separator->length(),
3358
                                  separator->charset(), &errors);
3359
    new_separator->length(conv_length);
3360
    separator= new_separator;
3361
  }
3362
3363
  if (check_sum_func(thd, ref))
3364
    return TRUE;
3365
3366
  fixed= 1;
3367
  return FALSE;
3368
}
3369
3370
3371
bool Item_func_group_concat::setup(THD *thd)
3372
{
3373
  List<Item> list;
3374
  SELECT_LEX *select_lex= thd->lex->current_select;
3375
  DBUG_ENTER("Item_func_group_concat::setup");
3376
3377
  /*
3378
    Currently setup() can be called twice. Please add
3379
    assertion here when this is fixed.
3380
  */
3381
  if (table || tree)
3382
    DBUG_RETURN(FALSE);
3383
3384
  if (!(tmp_table_param= new TMP_TABLE_PARAM))
3385
    DBUG_RETURN(TRUE);
3386
3387
  /* We'll convert all blobs to varchar fields in the temporary table */
3388
  tmp_table_param->convert_blob_length= max_length *
3389
                                        collation.collation->mbmaxlen;
3390
  /* Push all not constant fields to the list and create a temp table */
3391
  always_null= 0;
3392
  for (uint i= 0; i < arg_count_field; i++)
3393
  {
3394
    Item *item= args[i];
3395
    if (list.push_back(item))
3396
      DBUG_RETURN(TRUE);
3397
    if (item->const_item())
3398
    {
3399
      if (item->is_null())
3400
      {
3401
        always_null= 1;
3402
        DBUG_RETURN(FALSE);
3403
      }
3404
    }
3405
  }
3406
3407
  List<Item> all_fields(list);
3408
  /*
3409
    Try to find every ORDER expression in the list of GROUP_CONCAT
3410
    arguments. If an expression is not found, prepend it to
3411
    "all_fields". The resulting field list is used as input to create
3412
    tmp table columns.
3413
  */
3414
  if (arg_count_order &&
3415
      setup_order(thd, args, context->table_list, list, all_fields, *order))
3416
    DBUG_RETURN(TRUE);
3417
3418
  count_field_types(select_lex, tmp_table_param, all_fields, 0);
3419
  tmp_table_param->force_copy_fields= force_copy_fields;
3420
  DBUG_ASSERT(table == 0);
3421
  if (arg_count_order > 0 || distinct)
3422
  {
3423
    /*
3424
      Currently we have to force conversion of BLOB values to VARCHAR's
3425
      if we are to store them in TREE objects used for ORDER BY and
3426
      DISTINCT. This leads to truncation if the BLOB's size exceeds
3427
      Field_varstring::MAX_SIZE.
3428
    */
3429
    set_if_smaller(tmp_table_param->convert_blob_length, 
3430
                   Field_varstring::MAX_SIZE);
3431
3432
    /*
3433
      Force the create_tmp_table() to convert BIT columns to INT
3434
      as we cannot compare two table records containg BIT fields
3435
      stored in the the tree used for distinct/order by.
3436
      Moreover we don't even save in the tree record null bits 
3437
      where BIT fields store parts of their data.
3438
    */
3439
    List_iterator_fast<Item> li(all_fields);
3440
    Item *item;
3441
    while ((item= li++))
3442
    {
3443
      if (item->type() == Item::FIELD_ITEM && 
3444
          ((Item_field*) item)->field->type() == FIELD_TYPE_BIT)
3445
        item->marker= 4;
3446
    }
3447
  }
3448
3449
  /*
3450
    We have to create a temporary table to get descriptions of fields
3451
    (types, sizes and so on).
3452
3453
    Note that in the table, we first have the ORDER BY fields, then the
3454
    field list.
3455
  */
3456
  if (!(table= create_tmp_table(thd, tmp_table_param, all_fields,
3457
                                (ORDER*) 0, 0, TRUE,
3458
                                (select_lex->options | thd->options),
3459
                                HA_POS_ERROR, (char*) "")))
3460
    DBUG_RETURN(TRUE);
3461
  table->file->extra(HA_EXTRA_NO_ROWS);
3462
  table->no_rows= 1;
3463
3464
  /*
3465
     Need sorting or uniqueness: init tree and choose a function to sort.
3466
     Don't reserve space for NULLs: if any of gconcat arguments is NULL,
3467
     the row is not added to the result.
3468
  */
3469
  uint tree_key_length= table->s->reclength - table->s->null_bytes;
3470
3471
  if (arg_count_order)
3472
  {
3473
    tree= &tree_base;
3474
    /*
3475
      Create a tree for sorting. The tree is used to sort (according to the
3476
      syntax of this function). If there is no ORDER BY clause, we don't
3477
      create this tree.
3478
    */
3479
    init_tree(tree, (uint) min(thd->variables.max_heap_table_size,
3480
                               thd->variables.sortbuff_size/16), 0,
3481
              tree_key_length, 
3482
              group_concat_key_cmp_with_order , 0, NULL, (void*) this);
3483
  }
3484
3485
  if (distinct)
3486
    unique_filter= new Unique(group_concat_key_cmp_with_distinct,
3487
                              (void*)this,
3488
                              tree_key_length,
3489
                              thd->variables.max_heap_table_size);
3490
  
3491
  DBUG_RETURN(FALSE);
3492
}
3493
3494
3495
/* This is used by rollup to create a separate usable copy of the function */
3496
3497
void Item_func_group_concat::make_unique()
3498
{
3499
  tmp_table_param= 0;
3500
  table=0;
3501
  original= 0;
3502
  force_copy_fields= 1;
3503
  tree= 0;
3504
}
3505
3506
3507
String* Item_func_group_concat::val_str(String* str)
3508
{
3509
  DBUG_ASSERT(fixed == 1);
3510
  if (null_value)
3511
    return 0;
3512
  if (no_appended && tree)
3513
    /* Tree is used for sorting as in ORDER BY */
3514
    tree_walk(tree, (tree_walk_action)&dump_leaf_key, (void*)this,
3515
              left_root_right);
3516
  if (count_cut_values && !warning)
3517
  {
3518
    /*
3519
      ER_CUT_VALUE_GROUP_CONCAT needs an argument, but this gets set in
3520
      Item_func_group_concat::cleanup().
3521
    */
3522
    DBUG_ASSERT(table);
3523
    warning= push_warning(table->in_use, MYSQL_ERROR::WARN_LEVEL_WARN,
3524
                          ER_CUT_VALUE_GROUP_CONCAT,
3525
                          ER(ER_CUT_VALUE_GROUP_CONCAT));
3526
  }
3527
  return &result;
3528
}
3529
3530
3531
void Item_func_group_concat::print(String *str, enum_query_type query_type)
3532
{
3533
  str->append(STRING_WITH_LEN("group_concat("));
3534
  if (distinct)
3535
    str->append(STRING_WITH_LEN("distinct "));
3536
  for (uint i= 0; i < arg_count_field; i++)
3537
  {
3538
    if (i)
3539
      str->append(',');
3540
    args[i]->print(str, query_type);
3541
  }
3542
  if (arg_count_order)
3543
  {
3544
    str->append(STRING_WITH_LEN(" order by "));
3545
    for (uint i= 0 ; i < arg_count_order ; i++)
3546
    {
3547
      if (i)
3548
        str->append(',');
3549
      (*order[i]->item)->print(str, query_type);
3550
      if (order[i]->asc)
3551
        str->append(STRING_WITH_LEN(" ASC"));
3552
      else
3553
        str->append(STRING_WITH_LEN(" DESC"));
3554
    }
3555
  }
3556
  str->append(STRING_WITH_LEN(" separator \'"));
3557
  str->append(*separator);
3558
  str->append(STRING_WITH_LEN("\')"));
3559
}
3560
3561
3562
Item_func_group_concat::~Item_func_group_concat()
3563
{
3564
  if (!original && unique_filter)
3565
    delete unique_filter;    
3566
}