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