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