~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"
202.3.6 by Monty Taylor
First pass at gettexizing the error messages.
30
#include <drizzled/drizzled_error_messages.h>
1 by brian
clean slate
31
32
/**
33
  Prepare an aggregate function item for checking context conditions.
34
35
    The function initializes the members of the Item_sum object created
36
    for a set function that are used to check validity of the set function
37
    occurrence.
38
    If the set function is not allowed in any subquery where it occurs
39
    an error is reported immediately.
40
41
  @param thd      reference to the thread context info
42
43
  @note
44
    This function is to be called for any item created for a set function
45
    object when the traversal of trees built for expressions used in the query
46
    is performed at the phase of context analysis. This function is to
47
    be invoked at the descent of this traversal.
48
  @retval
49
    TRUE   if an error is reported
50
  @retval
51
    FALSE  otherwise
52
*/
53
 
54
bool Item_sum::init_sum_func_check(THD *thd)
55
{
56
  if (!thd->lex->allow_sum_func)
57
  {
58
    my_message(ER_INVALID_GROUP_FUNC_USE, ER(ER_INVALID_GROUP_FUNC_USE),
59
               MYF(0));
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
60
    return true;
1 by brian
clean slate
61
  }
62
  /* Set a reference to the nesting set function if there is  any */
63
  in_sum_func= thd->lex->in_sum_func;
64
  /* Save a pointer to object to be used in items for nested set functions */
65
  thd->lex->in_sum_func= this;
66
  nest_level= thd->lex->current_select->nest_level;
67
  ref_by= 0;
68
  aggr_level= -1;
69
  aggr_sel= NULL;
70
  max_arg_level= -1;
71
  max_sum_func_level= -1;
72
  outer_fields.empty();
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
73
  return false;
1 by brian
clean slate
74
}
75
76
/**
77
  Check constraints imposed on a usage of a set function.
78
79
    The method verifies whether context conditions imposed on a usage
80
    of any set function are met for this occurrence.
81
    It checks whether the set function occurs in the position where it
82
    can be aggregated and, when it happens to occur in argument of another
83
    set function, the method checks that these two functions are aggregated in
84
    different subqueries.
85
    If the context conditions are not met the method reports an error.
86
    If the set function is aggregated in some outer subquery the method
87
    adds it to the chain of items for such set functions that is attached
88
    to the the st_select_lex structure for this subquery.
89
90
    A number of designated members of the object are used to check the
91
    conditions. They are specified in the comment before the Item_sum
92
    class declaration.
93
    Additionally a bitmap variable called allow_sum_func is employed.
94
    It is included into the thd->lex structure.
95
    The bitmap contains 1 at n-th position if the set function happens
96
    to occur under a construct of the n-th level subquery where usage
97
    of set functions are allowed (i.e either in the SELECT list or
98
    in the HAVING clause of the corresponding subquery)
99
    Consider the query:
100
    @code
101
       SELECT SUM(t1.b) FROM t1 GROUP BY t1.a
102
         HAVING t1.a IN (SELECT t2.c FROM t2 WHERE AVG(t1.b) > 20) AND
103
                t1.a > (SELECT MIN(t2.d) FROM t2);
104
    @endcode
105
    allow_sum_func will contain: 
106
    - for SUM(t1.b) - 1 at the first position 
107
    - for AVG(t1.b) - 1 at the first position, 0 at the second position
108
    - for MIN(t2.d) - 1 at the first position, 1 at the second position.
109
110
  @param thd  reference to the thread context info
111
  @param ref  location of the pointer to this item in the embedding expression
112
113
  @note
114
    This function is to be called for any item created for a set function
115
    object when the traversal of trees built for expressions used in the query
116
    is performed at the phase of context analysis. This function is to
117
    be invoked at the ascent of this traversal.
118
119
  @retval
120
    TRUE   if an error is reported
121
  @retval
122
    FALSE  otherwise
123
*/
124
 
125
bool Item_sum::check_sum_func(THD *thd, Item **ref)
126
{
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
127
  bool invalid= false;
1 by brian
clean slate
128
  nesting_map allow_sum_func= thd->lex->allow_sum_func;
129
  /*  
130
    The value of max_arg_level is updated if an argument of the set function
131
    contains a column reference resolved  against a subquery whose level is
132
    greater than the current value of max_arg_level.
133
    max_arg_level cannot be greater than nest level.
134
    nest level is always >= 0  
135
  */ 
136
  if (nest_level == max_arg_level)
137
  {
138
    /*
139
      The function must be aggregated in the current subquery, 
140
      If it is there under a construct where it is not allowed 
141
      we report an error. 
142
    */ 
143
    invalid= !(allow_sum_func & (1 << max_arg_level));
144
  }
145
  else if (max_arg_level >= 0 || !(allow_sum_func & (1 << nest_level)))
146
  {
147
    /*
148
      The set function can be aggregated only in outer subqueries.
149
      Try to find a subquery where it can be aggregated;
150
      If we fail to find such a subquery report an error.
151
    */
152
    if (register_sum_func(thd, ref))
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
153
      return true;
1 by brian
clean slate
154
    invalid= aggr_level < 0 && !(allow_sum_func & (1 << nest_level));
155
    if (!invalid && thd->variables.sql_mode & MODE_ANSI)
156
      invalid= aggr_level < 0 && max_arg_level < nest_level;
157
  }
158
  if (!invalid && aggr_level < 0)
159
  {
160
    aggr_level= nest_level;
161
    aggr_sel= thd->lex->current_select;
162
  }
163
  /*
164
    By this moment we either found a subquery where the set function is
165
    to be aggregated  and assigned a value that is  >= 0 to aggr_level,
166
    or set the value of 'invalid' to TRUE to report later an error. 
167
  */
168
  /* 
169
    Additionally we have to check whether possible nested set functions
170
    are acceptable here: they are not, if the level of aggregation of
171
    some of them is less than aggr_level.
172
  */
173
  if (!invalid) 
174
    invalid= aggr_level <= max_sum_func_level;
175
  if (invalid)  
176
  {
177
    my_message(ER_INVALID_GROUP_FUNC_USE, ER(ER_INVALID_GROUP_FUNC_USE),
178
               MYF(0));
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
179
    return true;
1 by brian
clean slate
180
  }
181
182
  if (in_sum_func)
183
  {
184
    /*
185
      If the set function is nested adjust the value of
186
      max_sum_func_level for the nesting set function.
187
      We take into account only enclosed set functions that are to be 
188
      aggregated on the same level or above of the nest level of 
189
      the enclosing set function.
190
      But we must always pass up the max_sum_func_level because it is
191
      the maximum nested level of all directly and indirectly enclosed
192
      set functions. We must do that even for set functions that are
193
      aggregated inside of their enclosing set function's nest level
194
      because the enclosing function may contain another enclosing
195
      function that is to be aggregated outside or on the same level
196
      as its parent's nest level.
197
    */
198
    if (in_sum_func->nest_level >= aggr_level)
199
      set_if_bigger(in_sum_func->max_sum_func_level, aggr_level);
200
    set_if_bigger(in_sum_func->max_sum_func_level, max_sum_func_level);
201
  }
202
203
  /*
204
    Check that non-aggregated fields and sum functions aren't mixed in the
205
    same select in the ONLY_FULL_GROUP_BY mode.
206
  */
207
  if (outer_fields.elements)
208
  {
209
    Item_field *field;
210
    /*
211
      Here we compare the nesting level of the select to which an outer field
212
      belongs to with the aggregation level of the sum function. All fields in
213
      the outer_fields list are checked.
214
215
      If the nesting level is equal to the aggregation level then the field is
216
        aggregated by this sum function.
217
      If the nesting level is less than the aggregation level then the field
218
        belongs to an outer select. In this case if there is an embedding sum
219
        function add current field to functions outer_fields list. If there is
220
        no embedding function then the current field treated as non aggregated
221
        and the select it belongs to is marked accordingly.
222
      If the nesting level is greater than the aggregation level then it means
223
        that this field was added by an inner sum function.
224
        Consider an example:
225
226
          select avg ( <-- we are here, checking outer.f1
227
            select (
228
              select sum(outer.f1 + inner.f1) from inner
229
            ) from outer)
230
          from most_outer;
231
232
        In this case we check that no aggregate functions are used in the
233
        select the field belongs to. If there are some then an error is
234
        raised.
235
    */
236
    List_iterator<Item_field> of(outer_fields);
237
    while ((field= of++))
238
    {
239
      SELECT_LEX *sel= field->cached_table->select_lex;
240
      if (sel->nest_level < aggr_level)
241
      {
242
        if (in_sum_func)
243
        {
244
          /*
245
            Let upper function decide whether this field is a non
246
            aggregated one.
247
          */
248
          in_sum_func->outer_fields.push_back(field);
249
        }
250
        else
251
          sel->full_group_by_flag|= NON_AGG_FIELD_USED;
252
      }
253
      if (sel->nest_level > aggr_level &&
254
          (sel->full_group_by_flag & SUM_FUNC_USED) &&
255
          !sel->group_list.elements)
256
      {
257
        my_message(ER_MIX_OF_GROUP_FUNC_AND_FIELDS,
258
                   ER(ER_MIX_OF_GROUP_FUNC_AND_FIELDS), MYF(0));
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
259
        return true;
1 by brian
clean slate
260
      }
261
    }
262
  }
263
  aggr_sel->full_group_by_flag|= SUM_FUNC_USED;
264
  update_used_tables();
265
  thd->lex->in_sum_func= in_sum_func;
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
266
  return false;
1 by brian
clean slate
267
}
268
269
/**
270
  Attach a set function to the subquery where it must be aggregated.
271
272
    The function looks for an outer subquery where the set function must be
273
    aggregated. If it finds such a subquery then aggr_level is set to
274
    the nest level of this subquery and the item for the set function
275
    is added to the list of set functions used in nested subqueries
276
    inner_sum_func_list defined for each subquery. When the item is placed 
277
    there the field 'ref_by' is set to ref.
278
279
  @note
280
    Now we 'register' only set functions that are aggregated in outer
281
    subqueries. Actually it makes sense to link all set function for
282
    a subquery in one chain. It would simplify the process of 'splitting'
283
    for set functions.
284
285
  @param thd  reference to the thread context info
286
  @param ref  location of the pointer to this item in the embedding expression
287
288
  @retval
289
    FALSE  if the executes without failures (currently always)
290
  @retval
291
    TRUE   otherwise
292
*/  
293
294
bool Item_sum::register_sum_func(THD *thd, Item **ref)
295
{
296
  SELECT_LEX *sl;
297
  nesting_map allow_sum_func= thd->lex->allow_sum_func;
298
  for (sl= thd->lex->current_select->master_unit()->outer_select() ;
299
       sl && sl->nest_level > max_arg_level;
300
       sl= sl->master_unit()->outer_select() )
301
  {
302
    if (aggr_level < 0 && (allow_sum_func & (1 << sl->nest_level)))
303
    {
304
      /* Found the most nested subquery where the function can be aggregated */
305
      aggr_level= sl->nest_level;
306
      aggr_sel= sl;
307
    }
308
  }
309
  if (sl && (allow_sum_func & (1 << sl->nest_level)))
310
  {
311
    /* 
312
      We reached the subquery of level max_arg_level and checked
313
      that the function can be aggregated here. 
314
      The set function will be aggregated in this subquery.
315
    */   
316
    aggr_level= sl->nest_level;
317
    aggr_sel= sl;
318
319
  }
320
  if (aggr_level >= 0)
321
  {
322
    ref_by= ref;
323
    /* Add the object to the list of registered objects assigned to aggr_sel */
324
    if (!aggr_sel->inner_sum_func_list)
325
      next= this;
326
    else
327
    {
328
      next= aggr_sel->inner_sum_func_list->next;
329
      aggr_sel->inner_sum_func_list->next= this;
330
    }
331
    aggr_sel->inner_sum_func_list= this;
332
    aggr_sel->with_sum_func= 1;
333
334
    /* 
335
      Mark Item_subselect(s) as containing aggregate function all the way up
336
      to aggregate function's calculation context.
337
      Note that we must not mark the Item of calculation context itself
338
      because with_sum_func on the calculation context st_select_lex is
339
      already set above.
340
341
      with_sum_func being set for an Item means that this Item refers 
342
      (somewhere in it, e.g. one of its arguments if it's a function) directly
343
      or through intermediate items to an aggregate function that is calculated
344
      in a context "outside" of the Item (e.g. in the current or outer select).
345
346
      with_sum_func being set for an st_select_lex means that this st_select_lex
347
      has aggregate functions directly referenced (i.e. not through a sub-select).
348
    */
349
    for (sl= thd->lex->current_select; 
350
         sl && sl != aggr_sel && sl->master_unit()->item;
351
         sl= sl->master_unit()->outer_select() )
352
      sl->master_unit()->item->with_sum_func= 1;
353
  }
354
  thd->lex->current_select->mark_as_dependent(aggr_sel);
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
355
  return false;
1 by brian
clean slate
356
}
357
358
359
Item_sum::Item_sum(List<Item> &list) :arg_count(list.elements), 
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
360
  forced_const(false)
1 by brian
clean slate
361
{
362
  if ((args=(Item**) sql_alloc(sizeof(Item*)*arg_count)))
363
  {
364
    uint i=0;
365
    List_iterator_fast<Item> li(list);
366
    Item *item;
367
368
    while ((item=li++))
369
    {
370
      args[i++]= item;
371
    }
372
  }
373
  mark_as_sum_func();
374
  list.empty();					// Fields are used
375
}
376
377
378
/**
379
  Constructor used in processing select with temporary tebles.
380
*/
381
382
Item_sum::Item_sum(THD *thd, Item_sum *item):
383
  Item_result_field(thd, item), arg_count(item->arg_count),
384
  aggr_sel(item->aggr_sel),
385
  nest_level(item->nest_level), aggr_level(item->aggr_level),
386
  quick_group(item->quick_group), used_tables_cache(item->used_tables_cache),
387
  forced_const(item->forced_const) 
388
{
389
  if (arg_count <= 2)
390
    args=tmp_args;
391
  else
392
    if (!(args= (Item**) thd->alloc(sizeof(Item*)*arg_count)))
393
      return;
394
  memcpy(args, item->args, sizeof(Item*)*arg_count);
395
}
396
397
398
void Item_sum::mark_as_sum_func()
399
{
400
  SELECT_LEX *cur_select= current_thd->lex->current_select;
401
  cur_select->n_sum_items++;
402
  cur_select->with_sum_func= 1;
403
  with_sum_func= 1;
404
}
405
406
407
void Item_sum::make_field(Send_field *tmp_field)
408
{
409
  if (args[0]->type() == Item::FIELD_ITEM && keep_field_type())
410
  {
411
    ((Item_field*) args[0])->field->make_field(tmp_field);
412
    /* For expressions only col_name should be non-empty string. */
413
    char *empty_string= (char*)"";
414
    tmp_field->db_name= empty_string;
415
    tmp_field->org_table_name= empty_string;
416
    tmp_field->table_name= empty_string;
417
    tmp_field->org_col_name= empty_string;
418
    tmp_field->col_name= name;
419
    if (maybe_null)
420
      tmp_field->flags&= ~NOT_NULL_FLAG;
421
  }
422
  else
423
    init_make_field(tmp_field, field_type());
424
}
425
426
427
void Item_sum::print(String *str, enum_query_type query_type)
428
{
429
  str->append(func_name());
430
  for (uint i=0 ; i < arg_count ; i++)
431
  {
432
    if (i)
433
      str->append(',');
434
    args[i]->print(str, query_type);
435
  }
436
  str->append(')');
437
}
438
439
void Item_sum::fix_num_length_and_dec()
440
{
441
  decimals=0;
442
  for (uint i=0 ; i < arg_count ; i++)
443
    set_if_bigger(decimals,args[i]->decimals);
444
  max_length=float_length(decimals);
445
}
446
447
Item *Item_sum::get_tmp_table_item(THD *thd)
448
{
449
  Item_sum* sum_item= (Item_sum *) copy_or_same(thd);
450
  if (sum_item && sum_item->result_field)	   // If not a const sum func
451
  {
452
    Field *result_field_tmp= sum_item->result_field;
453
    for (uint i=0 ; i < sum_item->arg_count ; i++)
454
    {
455
      Item *arg= sum_item->args[i];
456
      if (!arg->const_item())
457
      {
458
	if (arg->type() == Item::FIELD_ITEM)
459
	  ((Item_field*) arg)->field= result_field_tmp++;
460
	else
461
	  sum_item->args[i]= new Item_field(result_field_tmp++);
462
      }
463
    }
464
  }
465
  return sum_item;
466
}
467
468
469
bool Item_sum::walk (Item_processor processor, bool walk_subquery,
470
                     uchar *argument)
471
{
472
  if (arg_count)
473
  {
474
    Item **arg,**arg_end;
475
    for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
476
    {
477
      if ((*arg)->walk(processor, walk_subquery, argument))
478
	return 1;
479
    }
480
  }
481
  return (this->*processor)(argument);
482
}
483
484
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
485
Field *Item_sum::create_tmp_field(bool group __attribute__((unused)),
77.1.15 by Monty Taylor
Bunch of warning cleanups.
486
                                  TABLE *table,
1 by brian
clean slate
487
                                  uint convert_blob_length)
488
{
489
  Field *field;
490
  switch (result_type()) {
491
  case REAL_RESULT:
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
492
    field= new Field_double(max_length, maybe_null, name, decimals, true);
1 by brian
clean slate
493
    break;
494
  case INT_RESULT:
152 by Brian Aker
longlong replacement
495
    field= new Field_int64_t(max_length, maybe_null, name, unsigned_flag);
1 by brian
clean slate
496
    break;
497
  case STRING_RESULT:
498
    if (max_length/collation.collation->mbmaxlen <= 255 ||
499
        convert_blob_length > Field_varstring::MAX_SIZE ||
500
        !convert_blob_length)
501
      return make_string_field(table);
502
    field= new Field_varstring(convert_blob_length, maybe_null,
503
                               name, table->s, collation.collation);
504
    break;
505
  case DECIMAL_RESULT:
506
    field= new Field_new_decimal(max_length, maybe_null, name,
507
                                 decimals, unsigned_flag);
508
    break;
509
  case ROW_RESULT:
510
  default:
511
    // This case should never be choosen
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
512
    assert(0);
1 by brian
clean slate
513
    return 0;
514
  }
515
  if (field)
516
    field->init(table);
517
  return field;
518
}
519
520
521
void Item_sum::update_used_tables ()
522
{
523
  if (!forced_const)
524
  {
525
    used_tables_cache= 0;
526
    for (uint i=0 ; i < arg_count ; i++)
527
    {
528
      args[i]->update_used_tables();
529
      used_tables_cache|= args[i]->used_tables();
530
    }
531
532
    used_tables_cache&= PSEUDO_TABLE_BITS;
533
534
    /* the aggregate function is aggregated into its local context */
535
    used_tables_cache |=  (1 << aggr_sel->join->tables) - 1;
536
  }
537
}
538
539
540
String *
541
Item_sum_num::val_str(String *str)
542
{
543
  return val_string_from_real(str);
544
}
545
546
547
my_decimal *Item_sum_num::val_decimal(my_decimal *decimal_value)
548
{
549
  return val_decimal_from_real(decimal_value);
550
}
551
552
553
String *
554
Item_sum_int::val_str(String *str)
555
{
556
  return val_string_from_int(str);
557
}
558
559
560
my_decimal *Item_sum_int::val_decimal(my_decimal *decimal_value)
561
{
562
  return val_decimal_from_int(decimal_value);
563
}
564
565
566
bool
567
Item_sum_num::fix_fields(THD *thd, Item **ref)
568
{
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
569
  assert(fixed == 0);
1 by brian
clean slate
570
571
  if (init_sum_func_check(thd))
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
572
    return true;
1 by brian
clean slate
573
574
  decimals=0;
575
  maybe_null=0;
576
  for (uint i=0 ; i < arg_count ; i++)
577
  {
578
    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
579
      return true;
1 by brian
clean slate
580
    set_if_bigger(decimals, args[i]->decimals);
581
    maybe_null |= args[i]->maybe_null;
582
  }
583
  result_field=0;
584
  max_length=float_length(decimals);
585
  null_value=1;
586
  fix_length_and_dec();
587
588
  if (check_sum_func(thd, ref))
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
589
    return true;
1 by brian
clean slate
590
591
  fixed= 1;
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
592
  return false;
1 by brian
clean slate
593
}
594
595
596
Item_sum_hybrid::Item_sum_hybrid(THD *thd, Item_sum_hybrid *item)
597
  :Item_sum(thd, item), value(item->value), hybrid_type(item->hybrid_type),
598
  hybrid_field_type(item->hybrid_field_type), cmp_sign(item->cmp_sign),
599
  was_values(item->was_values)
600
{
601
  /* copy results from old value */
602
  switch (hybrid_type) {
603
  case INT_RESULT:
604
    sum_int= item->sum_int;
605
    break;
606
  case DECIMAL_RESULT:
607
    my_decimal2decimal(&item->sum_dec, &sum_dec);
608
    break;
609
  case REAL_RESULT:
610
    sum= item->sum;
611
    break;
612
  case STRING_RESULT:
613
    /*
614
      This can happen with ROLLUP. Note that the value is already
615
      copied at function call.
616
    */
617
    break;
618
  case ROW_RESULT:
619
  default:
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
620
    assert(0);
1 by brian
clean slate
621
  }
622
  collation.set(item->collation);
623
}
624
625
bool
626
Item_sum_hybrid::fix_fields(THD *thd, Item **ref)
627
{
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
628
  assert(fixed == 0);
1 by brian
clean slate
629
630
  Item *item= args[0];
631
632
  if (init_sum_func_check(thd))
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
633
    return true;
1 by brian
clean slate
634
635
  // 'item' can be changed during fix_fields
636
  if ((!item->fixed && item->fix_fields(thd, args)) ||
637
      (item= args[0])->check_cols(1))
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
638
    return true;
1 by brian
clean slate
639
  decimals=item->decimals;
640
641
  switch (hybrid_type= item->result_type()) {
642
  case INT_RESULT:
643
    max_length= 20;
644
    sum_int= 0;
645
    break;
646
  case DECIMAL_RESULT:
647
    max_length= item->max_length;
648
    my_decimal_set_zero(&sum_dec);
649
    break;
650
  case REAL_RESULT:
651
    max_length= float_length(decimals);
652
    sum= 0.0;
653
    break;
654
  case STRING_RESULT:
655
    max_length= item->max_length;
656
    break;
657
  case ROW_RESULT:
658
  default:
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
659
    assert(0);
1 by brian
clean slate
660
  };
661
  /* MIN/MAX can return NULL for empty set indepedent of the used column */
662
  maybe_null= 1;
663
  unsigned_flag=item->unsigned_flag;
664
  collation.set(item->collation);
665
  result_field=0;
666
  null_value=1;
667
  fix_length_and_dec();
668
  item= item->real_item();
669
  if (item->type() == Item::FIELD_ITEM)
670
    hybrid_field_type= ((Item_field*) item)->field->type();
671
  else
672
    hybrid_field_type= Item::field_type();
673
674
  if (check_sum_func(thd, ref))
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
675
    return true;
1 by brian
clean slate
676
677
  fixed= 1;
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
678
  return false;
1 by brian
clean slate
679
}
680
681
Field *Item_sum_hybrid::create_tmp_field(bool group, TABLE *table,
682
					 uint convert_blob_length)
683
{
684
  Field *field;
685
  if (args[0]->type() == Item::FIELD_ITEM)
686
  {
687
    field= ((Item_field*) args[0])->field;
688
    
689
    if ((field= create_tmp_field_from_field(current_thd, field, name, table,
690
					    NULL, convert_blob_length)))
691
      field->flags&= ~NOT_NULL_FLAG;
692
    return field;
693
  }
694
  /*
695
    DATE/TIME fields have STRING_RESULT result types.
696
    In order to preserve field type, it's needed to handle DATE/TIME
697
    fields creations separately.
698
  */
699
  switch (args[0]->field_type()) {
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
700
  case DRIZZLE_TYPE_NEWDATE:
1 by brian
clean slate
701
    field= new Field_newdate(maybe_null, name, collation.collation);
702
    break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
703
  case DRIZZLE_TYPE_TIME:
1 by brian
clean slate
704
    field= new Field_time(maybe_null, name, collation.collation);
705
    break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
706
  case DRIZZLE_TYPE_TIMESTAMP:
707
  case DRIZZLE_TYPE_DATETIME:
1 by brian
clean slate
708
    field= new Field_datetime(maybe_null, name, collation.collation);
709
    break;
710
  default:
711
    return Item_sum::create_tmp_field(group, table, convert_blob_length);
712
  }
713
  if (field)
714
    field->init(table);
715
  return field;
716
}
717
718
719
/***********************************************************************
720
** reset and add of sum_func
721
***********************************************************************/
722
723
/**
724
  @todo
725
  check if the following assignments are really needed
726
*/
727
Item_sum_sum::Item_sum_sum(THD *thd, Item_sum_sum *item) 
728
  :Item_sum_num(thd, item), hybrid_type(item->hybrid_type),
729
   curr_dec_buff(item->curr_dec_buff)
730
{
731
  /* TODO: check if the following assignments are really needed */
732
  if (hybrid_type == DECIMAL_RESULT)
733
  {
734
    my_decimal2decimal(item->dec_buffs, dec_buffs);
735
    my_decimal2decimal(item->dec_buffs + 1, dec_buffs + 1);
736
  }
737
  else
738
    sum= item->sum;
739
}
740
741
Item *Item_sum_sum::copy_or_same(THD* thd)
742
{
743
  return new (thd->mem_root) Item_sum_sum(thd, this);
744
}
745
746
747
void Item_sum_sum::clear()
748
{
749
  null_value=1;
750
  if (hybrid_type == DECIMAL_RESULT)
751
  {
752
    curr_dec_buff= 0;
753
    my_decimal_set_zero(dec_buffs);
754
  }
755
  else
756
    sum= 0.0;
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
757
  return;
1 by brian
clean slate
758
}
759
760
761
void Item_sum_sum::fix_length_and_dec()
762
{
763
  maybe_null=null_value=1;
764
  decimals= args[0]->decimals;
765
  switch (args[0]->result_type()) {
766
  case REAL_RESULT:
767
  case STRING_RESULT:
768
    hybrid_type= REAL_RESULT;
769
    sum= 0.0;
770
    break;
771
  case INT_RESULT:
772
  case DECIMAL_RESULT:
773
  {
774
    /* SUM result can't be longer than length(arg) + length(MAX_ROWS) */
775
    int precision= args[0]->decimal_precision() + DECIMAL_LONGLONG_DIGITS;
776
    max_length= my_decimal_precision_to_length(precision, decimals,
777
                                               unsigned_flag);
778
    curr_dec_buff= 0;
779
    hybrid_type= DECIMAL_RESULT;
780
    my_decimal_set_zero(dec_buffs);
781
    break;
782
  }
783
  case ROW_RESULT:
784
  default:
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
785
    assert(0);
1 by brian
clean slate
786
  }
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
787
  return;
1 by brian
clean slate
788
}
789
790
791
bool Item_sum_sum::add()
792
{
793
  if (hybrid_type == DECIMAL_RESULT)
794
  {
795
    my_decimal value, *val= args[0]->val_decimal(&value);
796
    if (!args[0]->null_value)
797
    {
798
      my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs + (curr_dec_buff^1),
799
                     val, dec_buffs + curr_dec_buff);
800
      curr_dec_buff^= 1;
801
      null_value= 0;
802
    }
803
  }
804
  else
805
  {
806
    sum+= args[0]->val_real();
807
    if (!args[0]->null_value)
808
      null_value= 0;
809
  }
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
810
  return(0);
1 by brian
clean slate
811
}
812
813
152 by Brian Aker
longlong replacement
814
int64_t Item_sum_sum::val_int()
1 by brian
clean slate
815
{
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
816
  assert(fixed == 1);
1 by brian
clean slate
817
  if (hybrid_type == DECIMAL_RESULT)
818
  {
152 by Brian Aker
longlong replacement
819
    int64_t result;
1 by brian
clean slate
820
    my_decimal2int(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, unsigned_flag,
821
                   &result);
822
    return result;
823
  }
152 by Brian Aker
longlong replacement
824
  return (int64_t) rint(val_real());
1 by brian
clean slate
825
}
826
827
828
double Item_sum_sum::val_real()
829
{
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
830
  assert(fixed == 1);
1 by brian
clean slate
831
  if (hybrid_type == DECIMAL_RESULT)
832
    my_decimal2double(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, &sum);
833
  return sum;
834
}
835
836
837
String *Item_sum_sum::val_str(String *str)
838
{
839
  if (hybrid_type == DECIMAL_RESULT)
840
    return val_string_from_decimal(str);
841
  return val_string_from_real(str);
842
}
843
844
845
my_decimal *Item_sum_sum::val_decimal(my_decimal *val)
846
{
847
  if (hybrid_type == DECIMAL_RESULT)
848
    return (dec_buffs + curr_dec_buff);
849
  return val_decimal_from_real(val);
850
}
851
852
/***************************************************************************/
853
854
C_MODE_START
855
856
/* Declarations for auxilary C-callbacks */
857
858
static int simple_raw_key_cmp(void* arg, const void* key1, const void* key2)
859
{
860
    return memcmp(key1, key2, *(uint *) arg);
861
}
862
863
77.1.15 by Monty Taylor
Bunch of warning cleanups.
864
static int item_sum_distinct_walk(void *element,
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
865
                                  element_count num_of_dups __attribute__((unused)),
1 by brian
clean slate
866
                                  void *item)
867
{
868
  return ((Item_sum_distinct*) (item))->unique_walk_function(element);
869
}
870
871
C_MODE_END
872
873
/* Item_sum_distinct */
874
875
Item_sum_distinct::Item_sum_distinct(Item *item_arg)
876
  :Item_sum_num(item_arg), tree(0)
877
{
878
  /*
879
    quick_group is an optimizer hint, which means that GROUP BY can be
880
    handled with help of index on grouped columns.
881
    By setting quick_group to zero we force creation of temporary table
882
    to perform GROUP BY.
883
  */
884
  quick_group= 0;
885
}
886
887
888
Item_sum_distinct::Item_sum_distinct(THD *thd, Item_sum_distinct *original)
889
  :Item_sum_num(thd, original), val(original->val), tree(0),
890
  table_field_type(original->table_field_type)
891
{
892
  quick_group= 0;
893
}
894
895
896
/**
897
  Behaves like an Integer except to fix_length_and_dec().
898
  Additionally div() converts val with this traits to a val with true
899
  decimal traits along with conversion of integer value to decimal value.
900
  This is to speedup SUM/AVG(DISTINCT) evaluation for 8-32 bit integer
901
  values.
902
*/
903
struct Hybrid_type_traits_fast_decimal: public
904
       Hybrid_type_traits_integer
905
{
906
  virtual Item_result type() const { return DECIMAL_RESULT; }
907
  virtual void fix_length_and_dec(Item *item, Item *arg) const
908
  { Hybrid_type_traits_decimal::instance()->fix_length_and_dec(item, arg); }
909
151 by Brian Aker
Ulonglong to uint64_t
910
  virtual void div(Hybrid_type *val, uint64_t u) const
1 by brian
clean slate
911
  {
912
    int2my_decimal(E_DEC_FATAL_ERROR, val->integer, 0, val->dec_buf);
913
    val->used_dec_buf_no= 0;
914
    val->traits= Hybrid_type_traits_decimal::instance();
915
    val->traits->div(val, u);
916
  }
917
  static const Hybrid_type_traits_fast_decimal *instance();
918
  Hybrid_type_traits_fast_decimal() {};
919
};
920
921
static const Hybrid_type_traits_fast_decimal fast_decimal_traits_instance;
922
923
const Hybrid_type_traits_fast_decimal
924
  *Hybrid_type_traits_fast_decimal::instance()
925
{
926
  return &fast_decimal_traits_instance;
927
}
928
929
void Item_sum_distinct::fix_length_and_dec()
930
{
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
931
  assert(args[0]->fixed);
1 by brian
clean slate
932
933
  table_field_type= args[0]->field_type();
934
935
  /* Adjust tmp table type according to the chosen aggregation type */
936
  switch (args[0]->result_type()) {
937
  case STRING_RESULT:
938
  case REAL_RESULT:
939
    val.traits= Hybrid_type_traits::instance();
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
940
    table_field_type= DRIZZLE_TYPE_DOUBLE;
1 by brian
clean slate
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
  */
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
950
  if (table_field_type >= DRIZZLE_TYPE_TINY && table_field_type <= DRIZZLE_TYPE_LONG)
1 by brian
clean slate
951
  {
952
    val.traits= Hybrid_type_traits_fast_decimal::instance();
953
    break;
954
  }
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
955
  table_field_type= DRIZZLE_TYPE_LONGLONG;
1 by brian
clean slate
956
  /* fallthrough */
957
  case DECIMAL_RESULT:
958
    val.traits= Hybrid_type_traits_decimal::instance();
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
959
    if (table_field_type != DRIZZLE_TYPE_LONGLONG)
960
      table_field_type= DRIZZLE_TYPE_NEWDECIMAL;
1 by brian
clean slate
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,
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
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
    */
241 by Brian Aker
First pass of CHAR removal.
1228
    field= new Field_varstring(((hybrid_type == DECIMAL_RESULT) ?
1229
                                dec_bin_size : sizeof(double)) + sizeof(int64_t),
1230
                               0, name, table->s, &my_charset_bin);
1 by brian
clean slate
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,
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
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
    */
241 by Brian Aker
First pass of CHAR removal.
1438
    field= new Field_varstring(sizeof(double)*2 + sizeof(int64_t), 0, name, table->s, &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)
212.6.1 by Mats Kindahl
Replacing all bzero() calls with memset() calls and removing the bzero.c file.
1508
    memset(res, 0, 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)
212.6.1 by Mats Kindahl
Replacing all bzero() calls with memset() calls and removing the bzero.c file.
2043
      memset(res, 0, 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;
205 by Brian Aker
uint32 -> uin32_t
2451
  uint32_t *lengths=item->field_lengths;
1 by brian
clean slate
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
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
2468
static int count_distinct_walk(void *elem __attribute__((unused)),
2469
                               element_count count __attribute__((unused)),
77.1.15 by Monty Taylor
Bunch of warning cleanups.
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();
241 by Brian Aker
First pass of CHAR removal.
2586
      if (f_type == DRIZZLE_TYPE_VARCHAR)
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
      {
205 by Brian Aker
uint32 -> uin32_t
2613
        uint32_t *length;
1 by brian
clean slate
2614
        compare_key= (qsort_cmp2) composite_key_cmp;
2615
        cmp_arg= (void*) this;
205 by Brian Aker
uint32 -> uin32_t
2616
        field_lengths= (uint32_t*) thd->alloc(table->s->fields * sizeof(uint32_t));
1 by brian
clean slate
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
void Item_udf_sum::clear()
2732
{
2733
  udf.clear();
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
2734
  return;
1 by brian
clean slate
2735
}
2736
2737
bool Item_udf_sum::add()
2738
{
2739
  udf.add(&null_value);
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
2740
  return(0);
1 by brian
clean slate
2741
}
2742
2743
void Item_udf_sum::cleanup()
2744
{
2745
  /*
2746
    udf_handler::cleanup() nicely handles case when we have not
2747
    original item but one created by copy_or_same() method.
2748
  */
2749
  udf.cleanup();
2750
  Item_sum::cleanup();
2751
}
2752
2753
2754
void Item_udf_sum::print(String *str, enum_query_type query_type)
2755
{
2756
  str->append(func_name());
2757
  str->append('(');
2758
  for (uint i=0 ; i < arg_count ; i++)
2759
  {
2760
    if (i)
2761
      str->append(',');
2762
    args[i]->print(str, query_type);
2763
  }
2764
  str->append(')');
2765
}
2766
2767
2768
Item *Item_sum_udf_float::copy_or_same(THD* thd)
2769
{
2770
  return new (thd->mem_root) Item_sum_udf_float(thd, this);
2771
}
2772
2773
double Item_sum_udf_float::val_real()
2774
{
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
2775
  assert(fixed == 1);
2776
  return(udf.val(&null_value));
1 by brian
clean slate
2777
}
2778
2779
2780
String *Item_sum_udf_float::val_str(String *str)
2781
{
2782
  return val_string_from_real(str);
2783
}
2784
2785
2786
my_decimal *Item_sum_udf_float::val_decimal(my_decimal *dec)
2787
{
2788
  return val_decimal_from_real(dec);
2789
}
2790
2791
2792
String *Item_sum_udf_decimal::val_str(String *str)
2793
{
2794
  return val_string_from_decimal(str);
2795
}
2796
2797
2798
double Item_sum_udf_decimal::val_real()
2799
{
2800
  return val_real_from_decimal();
2801
}
2802
2803
152 by Brian Aker
longlong replacement
2804
int64_t Item_sum_udf_decimal::val_int()
1 by brian
clean slate
2805
{
2806
  return val_int_from_decimal();
2807
}
2808
2809
2810
my_decimal *Item_sum_udf_decimal::val_decimal(my_decimal *dec_buf)
2811
{
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
2812
  assert(fixed == 1);
2813
  return(udf.val_decimal(&null_value, dec_buf));
1 by brian
clean slate
2814
}
2815
2816
2817
Item *Item_sum_udf_decimal::copy_or_same(THD* thd)
2818
{
2819
  return new (thd->mem_root) Item_sum_udf_decimal(thd, this);
2820
}
2821
2822
2823
Item *Item_sum_udf_int::copy_or_same(THD* thd)
2824
{
2825
  return new (thd->mem_root) Item_sum_udf_int(thd, this);
2826
}
2827
152 by Brian Aker
longlong replacement
2828
int64_t Item_sum_udf_int::val_int()
1 by brian
clean slate
2829
{
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
2830
  assert(fixed == 1);
2831
  return(udf.val_int(&null_value));
1 by brian
clean slate
2832
}
2833
2834
2835
String *Item_sum_udf_int::val_str(String *str)
2836
{
2837
  return val_string_from_int(str);
2838
}
2839
2840
my_decimal *Item_sum_udf_int::val_decimal(my_decimal *dec)
2841
{
2842
  return val_decimal_from_int(dec);
2843
}
2844
2845
2846
/** Default max_length is max argument length. */
2847
2848
void Item_sum_udf_str::fix_length_and_dec()
2849
{
2850
  max_length=0;
2851
  for (uint i = 0; i < arg_count; i++)
2852
    set_if_bigger(max_length,args[i]->max_length);
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
2853
  return;
1 by brian
clean slate
2854
}
2855
2856
2857
Item *Item_sum_udf_str::copy_or_same(THD* thd)
2858
{
2859
  return new (thd->mem_root) Item_sum_udf_str(thd, this);
2860
}
2861
2862
2863
my_decimal *Item_sum_udf_str::val_decimal(my_decimal *dec)
2864
{
2865
  return val_decimal_from_string(dec);
2866
}
2867
2868
String *Item_sum_udf_str::val_str(String *str)
2869
{
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
2870
  assert(fixed == 1);
1 by brian
clean slate
2871
  String *res=udf.val_str(str,&str_value);
2872
  null_value = !res;
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
2873
  return(res);
1 by brian
clean slate
2874
}
2875
2876
/*****************************************************************************
2877
 GROUP_CONCAT function
2878
2879
 SQL SYNTAX:
2880
  GROUP_CONCAT([DISTINCT] expr,... [ORDER BY col [ASC|DESC],...]
2881
    [SEPARATOR str_const])
2882
2883
 concat of values from "group by" operation
2884
2885
 BUGS
2886
   Blobs doesn't work with DISTINCT or ORDER BY
2887
*****************************************************************************/
2888
2889
2890
/** 
2891
  Compares the values for fields in expr list of GROUP_CONCAT.
2892
  @note
2893
       
2894
     GROUP_CONCAT([DISTINCT] expr [,expr ...]
2895
              [ORDER BY {unsigned_integer | col_name | expr}
2896
                  [ASC | DESC] [,col_name ...]]
2897
              [SEPARATOR str_val])
2898
 
2899
  @return
2900
  @retval -1 : key1 < key2 
2901
  @retval  0 : key1 = key2
2902
  @retval  1 : key1 > key2 
2903
*/
2904
2905
int group_concat_key_cmp_with_distinct(void* arg, const void* key1, 
2906
                                       const void* key2)
2907
{
2908
  Item_func_group_concat *item_func= (Item_func_group_concat*)arg;
2909
  TABLE *table= item_func->table;
2910
2911
  for (uint i= 0; i < item_func->arg_count_field; i++)
2912
  {
2913
    Item *item= item_func->args[i];
2914
    /* 
2915
      If field_item is a const item then either get_tp_table_field returns 0
2916
      or it is an item over a const table. 
2917
    */
2918
    if (item->const_item())
2919
      continue;
2920
    /*
2921
      We have to use get_tmp_table_field() instead of
2922
      real_item()->get_tmp_table_field() because we want the field in
2923
      the temporary table, not the original field
2924
    */
2925
    Field *field= item->get_tmp_table_field();
2926
    int res;
2927
    uint offset= field->offset(field->table->record[0])-table->s->null_bytes;
2928
    if((res= field->cmp((uchar*)key1 + offset, (uchar*)key2 + offset)))
2929
      return res;
2930
  }
2931
  return 0;
2932
}
2933
2934
2935
/**
2936
  function of sort for syntax: GROUP_CONCAT(expr,... ORDER BY col,... )
2937
*/
2938
2939
int group_concat_key_cmp_with_order(void* arg, const void* key1, 
2940
                                    const void* key2)
2941
{
2942
  Item_func_group_concat* grp_item= (Item_func_group_concat*) arg;
2943
  ORDER **order_item, **end;
2944
  TABLE *table= grp_item->table;
2945
2946
  for (order_item= grp_item->order, end=order_item+ grp_item->arg_count_order;
2947
       order_item < end;
2948
       order_item++)
2949
  {
2950
    Item *item= *(*order_item)->item;
2951
    /*
2952
      We have to use get_tmp_table_field() instead of
2953
      real_item()->get_tmp_table_field() because we want the field in
2954
      the temporary table, not the original field
2955
    */
2956
    Field *field= item->get_tmp_table_field();
2957
    /* 
2958
      If item is a const item then either get_tp_table_field returns 0
2959
      or it is an item over a const table. 
2960
    */
2961
    if (field && !item->const_item())
2962
    {
2963
      int res;
2964
      uint offset= (field->offset(field->table->record[0]) -
2965
                    table->s->null_bytes);
2966
      if ((res= field->cmp((uchar*)key1 + offset, (uchar*)key2 + offset)))
2967
        return (*order_item)->asc ? res : -res;
2968
    }
2969
  }
2970
  /*
2971
    We can't return 0 because in that case the tree class would remove this
2972
    item as double value. This would cause problems for case-changes and
2973
    if the returned values are not the same we do the sort on.
2974
  */
2975
  return 1;
2976
}
2977
2978
2979
/**
2980
  Append data from current leaf to item->result.
2981
*/
2982
2983
int dump_leaf_key(uchar* key, element_count count __attribute__((unused)),
2984
                  Item_func_group_concat *item)
2985
{
2986
  TABLE *table= item->table;
2987
  String tmp((char *)table->record[1], table->s->reclength,
2988
             default_charset_info);
2989
  String tmp2;
2990
  String *result= &item->result;
2991
  Item **arg= item->args, **arg_end= item->args + item->arg_count_field;
2992
  uint old_length= result->length();
2993
2994
  if (item->no_appended)
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
2995
    item->no_appended= false;
1 by brian
clean slate
2996
  else
2997
    result->append(*item->separator);
2998
2999
  tmp.length(0);
3000
3001
  for (; arg < arg_end; arg++)
3002
  {
3003
    String *res;
3004
    if (! (*arg)->const_item())
3005
    {
3006
      /*
3007
	We have to use get_tmp_table_field() instead of
3008
	real_item()->get_tmp_table_field() because we want the field in
3009
	the temporary table, not the original field
3010
        We also can't use table->field array to access the fields
3011
        because it contains both order and arg list fields.
3012
      */
3013
      Field *field= (*arg)->get_tmp_table_field();
3014
      uint offset= (field->offset(field->table->record[0]) -
3015
                    table->s->null_bytes);
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
3016
      assert(offset < table->s->reclength);
1 by brian
clean slate
3017
      res= field->val_str(&tmp, key + offset);
3018
    }
3019
    else
3020
      res= (*arg)->val_str(&tmp);
3021
    if (res)
3022
      result->append(*res);
3023
  }
3024
3025
  /* stop if length of result more than max_length */
3026
  if (result->length() > item->max_length)
3027
  {
3028
    int well_formed_error;
3029
    CHARSET_INFO *cs= item->collation.collation;
3030
    const char *ptr= result->ptr();
3031
    uint add_length;
3032
    /*
3033
      It's ok to use item->result.length() as the fourth argument
3034
      as this is never used to limit the length of the data.
3035
      Cut is done with the third argument.
3036
    */
3037
    add_length= cs->cset->well_formed_len(cs,
3038
                                          ptr + old_length,
3039
                                          ptr + item->max_length,
3040
                                          result->length(),
3041
                                          &well_formed_error);
3042
    result->length(old_length + add_length);
3043
    item->count_cut_values++;
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
3044
    item->warning_for_row= true;
1 by brian
clean slate
3045
    return 1;
3046
  }
3047
  return 0;
3048
}
3049
3050
3051
/**
3052
  Constructor of Item_func_group_concat.
3053
3054
  @param distinct_arg   distinct
3055
  @param select_list    list of expression for show values
3056
  @param order_list     list of sort columns
3057
  @param separator_arg  string value of separator.
3058
*/
3059
3060
Item_func_group_concat::
3061
Item_func_group_concat(Name_resolution_context *context_arg,
3062
                       bool distinct_arg, List<Item> *select_list,
3063
                       SQL_LIST *order_list, String *separator_arg)
3064
  :tmp_table_param(0), warning(0),
3065
   separator(separator_arg), tree(0), unique_filter(NULL), table(0),
3066
   order(0), context(context_arg),
3067
   arg_count_order(order_list ? order_list->elements : 0),
3068
   arg_count_field(select_list->elements),
3069
   count_cut_values(0),
3070
   distinct(distinct_arg),
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
3071
   warning_for_row(false),
1 by brian
clean slate
3072
   force_copy_fields(0), original(0)
3073
{
3074
  Item *item_select;
3075
  Item **arg_ptr;
3076
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
3077
  quick_group= false;
1 by brian
clean slate
3078
  arg_count= arg_count_field + arg_count_order;
3079
3080
  /*
3081
    We need to allocate:
3082
    args - arg_count_field+arg_count_order
3083
           (for possible order items in temporare tables)
3084
    order - arg_count_order
3085
  */
3086
  if (!(args= (Item**) sql_alloc(sizeof(Item*) * arg_count +
3087
                                 sizeof(ORDER*)*arg_count_order)))
3088
    return;
3089
3090
  order= (ORDER**)(args + arg_count);
3091
3092
  /* fill args items of show and sort */
3093
  List_iterator_fast<Item> li(*select_list);
3094
3095
  for (arg_ptr=args ; (item_select= li++) ; arg_ptr++)
3096
    *arg_ptr= item_select;
3097
3098
  if (arg_count_order)
3099
  {
3100
    ORDER **order_ptr= order;
3101
    for (ORDER *order_item= (ORDER*) order_list->first;
3102
         order_item != NULL;
3103
         order_item= order_item->next)
3104
    {
3105
      (*order_ptr++)= order_item;
3106
      *arg_ptr= *order_item->item;
3107
      order_item->item= arg_ptr++;
3108
    }
3109
  }
3110
}
3111
3112
3113
Item_func_group_concat::Item_func_group_concat(THD *thd,
3114
                                               Item_func_group_concat *item)
3115
  :Item_sum(thd, item),
3116
  tmp_table_param(item->tmp_table_param),
3117
  warning(item->warning),
3118
  separator(item->separator),
3119
  tree(item->tree),
3120
  unique_filter(item->unique_filter),
3121
  table(item->table),
3122
  order(item->order),
3123
  context(item->context),
3124
  arg_count_order(item->arg_count_order),
3125
  arg_count_field(item->arg_count_field),
3126
  count_cut_values(item->count_cut_values),
3127
  distinct(item->distinct),
3128
  warning_for_row(item->warning_for_row),
3129
  always_null(item->always_null),
3130
  force_copy_fields(item->force_copy_fields),
3131
  original(item)
3132
{
3133
  quick_group= item->quick_group;
3134
  result.set_charset(collation.collation);
3135
}
3136
3137
3138
3139
void Item_func_group_concat::cleanup()
3140
{
3141
  Item_sum::cleanup();
3142
3143
  /* Adjust warning message to include total number of cut values */
3144
  if (warning)
3145
  {
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
3146
    char warn_buff[DRIZZLE_ERRMSG_SIZE];
1 by brian
clean slate
3147
    sprintf(warn_buff, ER(ER_CUT_VALUE_GROUP_CONCAT), count_cut_values);
3148
    warning->set_msg(current_thd, warn_buff);
3149
    warning= 0;
3150
  }
3151
3152
  /*
3153
    Free table and tree if they belong to this item (if item have not pointer
3154
    to original item from which was made copy => it own its objects )
3155
  */
3156
  if (!original)
3157
  {
3158
    delete tmp_table_param;
3159
    tmp_table_param= 0;
3160
    if (table)
3161
    {
3162
      THD *thd= table->in_use;
3163
      free_tmp_table(thd, table);
3164
      table= 0;
3165
      if (tree)
3166
      {
3167
        delete_tree(tree);
3168
        tree= 0;
3169
      }
3170
      if (unique_filter)
3171
      {
3172
        delete unique_filter;
3173
        unique_filter= NULL;
3174
      }
3175
      if (warning)
3176
      {
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
3177
        char warn_buff[DRIZZLE_ERRMSG_SIZE];
1 by brian
clean slate
3178
        sprintf(warn_buff, ER(ER_CUT_VALUE_GROUP_CONCAT), count_cut_values);
3179
        warning->set_msg(thd, warn_buff);
3180
        warning= 0;
3181
      }
3182
    }
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
3183
    assert(tree == 0 && warning == 0);
1 by brian
clean slate
3184
  }
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
3185
  return;
1 by brian
clean slate
3186
}
3187
3188
3189
Item *Item_func_group_concat::copy_or_same(THD* thd)
3190
{
3191
  return new (thd->mem_root) Item_func_group_concat(thd, this);
3192
}
3193
3194
3195
void Item_func_group_concat::clear()
3196
{
3197
  result.length(0);
3198
  result.copy();
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
3199
  null_value= true;
3200
  warning_for_row= false;
3201
  no_appended= true;
1 by brian
clean slate
3202
  if (tree)
3203
    reset_tree(tree);
3204
  if (distinct)
3205
    unique_filter->reset();
3206
  /* No need to reset the table as we never call write_row */
3207
}
3208
3209
3210
bool Item_func_group_concat::add()
3211
{
3212
  if (always_null)
3213
    return 0;
3214
  copy_fields(tmp_table_param);
3215
  copy_funcs(tmp_table_param->items_to_copy);
3216
3217
  for (uint i= 0; i < arg_count_field; i++)
3218
  {
3219
    Item *show_item= args[i];
3220
    if (!show_item->const_item())
3221
    {
3222
      Field *f= show_item->get_tmp_table_field();
3223
      if (f->is_null_in_record((const uchar*) table->record[0]))
3224
        return 0;                               // Skip row if it contains null
3225
    }
3226
  }
3227
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
3228
  null_value= false;
3229
  bool row_eligible= true;
1 by brian
clean slate
3230
3231
  if (distinct) 
3232
  {
3233
    /* Filter out duplicate rows. */
3234
    uint count= unique_filter->elements_in_tree();
3235
    unique_filter->unique_add(table->record[0] + table->s->null_bytes);
3236
    if (count == unique_filter->elements_in_tree())
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
3237
      row_eligible= false;
1 by brian
clean slate
3238
  }
3239
3240
  TREE_ELEMENT *el= 0;                          // Only for safety
3241
  if (row_eligible && tree)
3242
    el= tree_insert(tree, table->record[0] + table->s->null_bytes, 0,
3243
                    tree->custom_arg);
3244
  /*
3245
    If the row is not a duplicate (el->count == 1)
3246
    we can dump the row here in case of GROUP_CONCAT(DISTINCT...)
3247
    instead of doing tree traverse later.
3248
  */
3249
  if (row_eligible && !warning_for_row &&
3250
      (!tree || (el->count == 1 && distinct && !arg_count_order)))
3251
    dump_leaf_key(table->record[0] + table->s->null_bytes, 1, this);
3252
3253
  return 0;
3254
}
3255
3256
3257
bool
3258
Item_func_group_concat::fix_fields(THD *thd, Item **ref)
3259
{
3260
  uint i;                       /* for loop variable */
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
3261
  assert(fixed == 0);
1 by brian
clean slate
3262
3263
  if (init_sum_func_check(thd))
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
3264
    return true;
1 by brian
clean slate
3265
3266
  maybe_null= 1;
3267
3268
  /*
3269
    Fix fields for select list and ORDER clause
3270
  */
3271
3272
  for (i=0 ; i < arg_count ; i++)
3273
  {
3274
    if ((!args[i]->fixed &&
3275
         args[i]->fix_fields(thd, args + i)) ||
3276
        args[i]->check_cols(1))
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
3277
      return true;
1 by brian
clean slate
3278
  }
3279
3280
  if (agg_item_charsets(collation, func_name(),
3281
                        args,
3282
			/* skip charset aggregation for order columns */
3283
			arg_count - arg_count_order,
3284
			MY_COLL_ALLOW_CONV, 1))
3285
    return 1;
3286
3287
  result.set_charset(collation.collation);
3288
  result_field= 0;
3289
  null_value= 1;
3290
  max_length= thd->variables.group_concat_max_len;
3291
205 by Brian Aker
uint32 -> uin32_t
3292
  uint32_t offset;
1 by brian
clean slate
3293
  if (separator->needs_conversion(separator->length(), separator->charset(),
3294
                                  collation.collation, &offset))
3295
  {
205 by Brian Aker
uint32 -> uin32_t
3296
    uint32_t buflen= collation.collation->mbmaxlen * separator->length();
1 by brian
clean slate
3297
    uint errors, conv_length;
3298
    char *buf;
3299
    String *new_separator;
3300
3301
    if (!(buf= (char*) thd->stmt_arena->alloc(buflen)) ||
3302
        !(new_separator= new(thd->stmt_arena->mem_root)
3303
                           String(buf, buflen, collation.collation)))
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
3304
      return true;
1 by brian
clean slate
3305
    
3306
    conv_length= copy_and_convert(buf, buflen, collation.collation,
3307
                                  separator->ptr(), separator->length(),
3308
                                  separator->charset(), &errors);
3309
    new_separator->length(conv_length);
3310
    separator= new_separator;
3311
  }
3312
3313
  if (check_sum_func(thd, ref))
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
3314
    return true;
1 by brian
clean slate
3315
3316
  fixed= 1;
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
3317
  return false;
1 by brian
clean slate
3318
}
3319
3320
3321
bool Item_func_group_concat::setup(THD *thd)
3322
{
3323
  List<Item> list;
3324
  SELECT_LEX *select_lex= thd->lex->current_select;
3325
3326
  /*
3327
    Currently setup() can be called twice. Please add
3328
    assertion here when this is fixed.
3329
  */
3330
  if (table || tree)
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
3331
    return(false);
1 by brian
clean slate
3332
3333
  if (!(tmp_table_param= new TMP_TABLE_PARAM))
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
3334
    return(true);
1 by brian
clean slate
3335
3336
  /* We'll convert all blobs to varchar fields in the temporary table */
3337
  tmp_table_param->convert_blob_length= max_length *
3338
                                        collation.collation->mbmaxlen;
3339
  /* Push all not constant fields to the list and create a temp table */
3340
  always_null= 0;
3341
  for (uint i= 0; i < arg_count_field; i++)
3342
  {
3343
    Item *item= args[i];
3344
    if (list.push_back(item))
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
3345
      return(true);
1 by brian
clean slate
3346
    if (item->const_item())
3347
    {
3348
      if (item->is_null())
3349
      {
3350
        always_null= 1;
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
3351
        return(false);
1 by brian
clean slate
3352
      }
3353
    }
3354
  }
3355
3356
  List<Item> all_fields(list);
3357
  /*
3358
    Try to find every ORDER expression in the list of GROUP_CONCAT
3359
    arguments. If an expression is not found, prepend it to
3360
    "all_fields". The resulting field list is used as input to create
3361
    tmp table columns.
3362
  */
3363
  if (arg_count_order &&
3364
      setup_order(thd, args, context->table_list, list, all_fields, *order))
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
3365
    return(true);
1 by brian
clean slate
3366
3367
  count_field_types(select_lex, tmp_table_param, all_fields, 0);
3368
  tmp_table_param->force_copy_fields= force_copy_fields;
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
3369
  assert(table == 0);
1 by brian
clean slate
3370
  if (arg_count_order > 0 || distinct)
3371
  {
3372
    /*
3373
      Currently we have to force conversion of BLOB values to VARCHAR's
3374
      if we are to store them in TREE objects used for ORDER BY and
3375
      DISTINCT. This leads to truncation if the BLOB's size exceeds
3376
      Field_varstring::MAX_SIZE.
3377
    */
3378
    set_if_smaller(tmp_table_param->convert_blob_length, 
3379
                   Field_varstring::MAX_SIZE);
3380
  }
3381
3382
  /*
3383
    We have to create a temporary table to get descriptions of fields
3384
    (types, sizes and so on).
3385
3386
    Note that in the table, we first have the ORDER BY fields, then the
3387
    field list.
3388
  */
3389
  if (!(table= create_tmp_table(thd, tmp_table_param, all_fields,
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
3390
                                (ORDER*) 0, 0, true,
1 by brian
clean slate
3391
                                (select_lex->options | thd->options),
3392
                                HA_POS_ERROR, (char*) "")))
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
3393
    return(true);
1 by brian
clean slate
3394
  table->file->extra(HA_EXTRA_NO_ROWS);
3395
  table->no_rows= 1;
3396
3397
  /*
3398
     Need sorting or uniqueness: init tree and choose a function to sort.
3399
     Don't reserve space for NULLs: if any of gconcat arguments is NULL,
3400
     the row is not added to the result.
3401
  */
3402
  uint tree_key_length= table->s->reclength - table->s->null_bytes;
3403
3404
  if (arg_count_order)
3405
  {
3406
    tree= &tree_base;
3407
    /*
3408
      Create a tree for sorting. The tree is used to sort (according to the
3409
      syntax of this function). If there is no ORDER BY clause, we don't
3410
      create this tree.
3411
    */
3412
    init_tree(tree, (uint) min(thd->variables.max_heap_table_size,
3413
                               thd->variables.sortbuff_size/16), 0,
3414
              tree_key_length, 
3415
              group_concat_key_cmp_with_order , 0, NULL, (void*) this);
3416
  }
3417
3418
  if (distinct)
3419
    unique_filter= new Unique(group_concat_key_cmp_with_distinct,
3420
                              (void*)this,
3421
                              tree_key_length,
3422
                              thd->variables.max_heap_table_size);
3423
  
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
3424
  return(false);
1 by brian
clean slate
3425
}
3426
3427
3428
/* This is used by rollup to create a separate usable copy of the function */
3429
3430
void Item_func_group_concat::make_unique()
3431
{
3432
  tmp_table_param= 0;
3433
  table=0;
3434
  original= 0;
3435
  force_copy_fields= 1;
3436
  tree= 0;
3437
}
3438
3439
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
3440
String* Item_func_group_concat::val_str(String* str __attribute__((unused)))
1 by brian
clean slate
3441
{
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
3442
  assert(fixed == 1);
1 by brian
clean slate
3443
  if (null_value)
3444
    return 0;
3445
  if (no_appended && tree)
3446
    /* Tree is used for sorting as in ORDER BY */
3447
    tree_walk(tree, (tree_walk_action)&dump_leaf_key, (void*)this,
3448
              left_root_right);
3449
  if (count_cut_values && !warning)
3450
  {
3451
    /*
3452
      ER_CUT_VALUE_GROUP_CONCAT needs an argument, but this gets set in
3453
      Item_func_group_concat::cleanup().
3454
    */
51.1.25 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
3455
    assert(table);
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
3456
    warning= push_warning(table->in_use, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1 by brian
clean slate
3457
                          ER_CUT_VALUE_GROUP_CONCAT,
3458
                          ER(ER_CUT_VALUE_GROUP_CONCAT));
3459
  }
3460
  return &result;
3461
}
3462
3463
3464
void Item_func_group_concat::print(String *str, enum_query_type query_type)
3465
{
3466
  str->append(STRING_WITH_LEN("group_concat("));
3467
  if (distinct)
3468
    str->append(STRING_WITH_LEN("distinct "));
3469
  for (uint i= 0; i < arg_count_field; i++)
3470
  {
3471
    if (i)
3472
      str->append(',');
3473
    args[i]->print(str, query_type);
3474
  }
3475
  if (arg_count_order)
3476
  {
3477
    str->append(STRING_WITH_LEN(" order by "));
3478
    for (uint i= 0 ; i < arg_count_order ; i++)
3479
    {
3480
      if (i)
3481
        str->append(',');
3482
      (*order[i]->item)->print(str, query_type);
3483
      if (order[i]->asc)
3484
        str->append(STRING_WITH_LEN(" ASC"));
3485
      else
3486
        str->append(STRING_WITH_LEN(" DESC"));
3487
    }
3488
  }
3489
  str->append(STRING_WITH_LEN(" separator \'"));
3490
  str->append(*separator);
3491
  str->append(STRING_WITH_LEN("\')"));
3492
}
3493
3494
3495
Item_func_group_concat::~Item_func_group_concat()
3496
{
3497
  if (!original && unique_filter)
3498
    delete unique_filter;    
3499
}