~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item_sum.cc

  • Committer: Monty Taylor
  • Date: 2008-07-28 02:47:38 UTC
  • mto: (212.5.1 codestyle)
  • mto: This revision was merged to the branch mainline in revision 219.
  • Revision ID: monty@inaugust.com-20080728024738-366ikqnoqv7d8g6l
Fixed the includes in places to make the myisam header file move work.

Show diffs side-by-side

added added

removed removed

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