~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item_sum.cc

Merged vcol stuff.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
13
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
16
 
17
17
/**
20
20
  @brief
21
21
  Sum functions (COUNT, MIN...)
22
22
*/
23
 
#include "config.h"
24
 
#include <cstdio>
25
 
#include <math.h>
 
23
#include <drizzled/server_includes.h>
26
24
#include <drizzled/sql_select.h>
27
 
#include <drizzled/error.h>
28
 
#include <drizzled/hybrid_type_traits.h>
29
 
#include <drizzled/hybrid_type_traits_integer.h>
30
 
#include <drizzled/hybrid_type_traits_decimal.h>
31
 
#include <drizzled/sql_base.h>
32
 
 
33
 
#include <drizzled/item/sum.h>
34
 
#include <drizzled/field/decimal.h>
35
 
#include <drizzled/field/double.h>
36
 
#include <drizzled/field/int64.h>
37
 
#include <drizzled/field/date.h>
38
 
#include <drizzled/field/datetime.h>
39
 
 
40
 
#include <drizzled/type/decimal.h>
41
 
 
42
 
#include "drizzled/internal/m_string.h"
43
 
 
44
 
#include <algorithm>
45
 
 
46
 
using namespace std;
47
 
 
48
 
namespace drizzled
49
 
{
50
 
 
51
 
extern plugin::StorageEngine *heap_engine;
 
25
#include <drizzled/drizzled_error_messages.h>
52
26
 
53
27
/**
54
28
  Prepare an aggregate function item for checking context conditions.
59
33
    If the set function is not allowed in any subquery where it occurs
60
34
    an error is reported immediately.
61
35
 
62
 
  @param session      reference to the thread context info
 
36
  @param thd      reference to the thread context info
63
37
 
64
38
  @note
65
39
    This function is to be called for any item created for a set function
71
45
  @retval
72
46
    FALSE  otherwise
73
47
*/
74
 
 
75
 
bool Item_sum::init_sum_func_check(Session *session)
 
48
 
 
49
bool Item_sum::init_sum_func_check(THD *thd)
76
50
{
77
 
  if (!session->lex->allow_sum_func)
 
51
  if (!thd->lex->allow_sum_func)
78
52
  {
79
53
    my_message(ER_INVALID_GROUP_FUNC_USE, ER(ER_INVALID_GROUP_FUNC_USE),
80
54
               MYF(0));
81
55
    return true;
82
56
  }
83
57
  /* Set a reference to the nesting set function if there is  any */
84
 
  in_sum_func= session->lex->in_sum_func;
 
58
  in_sum_func= thd->lex->in_sum_func;
85
59
  /* Save a pointer to object to be used in items for nested set functions */
86
 
  session->lex->in_sum_func= this;
87
 
  nest_level= session->lex->current_select->nest_level;
 
60
  thd->lex->in_sum_func= this;
 
61
  nest_level= thd->lex->current_select->nest_level;
88
62
  ref_by= 0;
89
63
  aggr_level= -1;
90
64
  aggr_sel= NULL;
106
80
    If the context conditions are not met the method reports an error.
107
81
    If the set function is aggregated in some outer subquery the method
108
82
    adds it to the chain of items for such set functions that is attached
109
 
    to the the Select_Lex structure for this subquery.
 
83
    to the the st_select_lex structure for this subquery.
110
84
 
111
85
    A number of designated members of the object are used to check the
112
86
    conditions. They are specified in the comment before the Item_sum
113
87
    class declaration.
114
88
    Additionally a bitmap variable called allow_sum_func is employed.
115
 
    It is included into the session->lex structure.
 
89
    It is included into the thd->lex structure.
116
90
    The bitmap contains 1 at n-th position if the set function happens
117
91
    to occur under a construct of the n-th level subquery where usage
118
92
    of set functions are allowed (i.e either in the SELECT list or
123
97
         HAVING t1.a IN (SELECT t2.c FROM t2 WHERE AVG(t1.b) > 20) AND
124
98
                t1.a > (SELECT MIN(t2.d) FROM t2);
125
99
    @endcode
126
 
    allow_sum_func will contain:
127
 
    - for SUM(t1.b) - 1 at the first position
 
100
    allow_sum_func will contain: 
 
101
    - for SUM(t1.b) - 1 at the first position 
128
102
    - for AVG(t1.b) - 1 at the first position, 0 at the second position
129
103
    - for MIN(t2.d) - 1 at the first position, 1 at the second position.
130
104
 
131
 
  @param session  reference to the thread context info
 
105
  @param thd  reference to the thread context info
132
106
  @param ref  location of the pointer to this item in the embedding expression
133
107
 
134
108
  @note
142
116
  @retval
143
117
    FALSE  otherwise
144
118
*/
145
 
 
146
 
bool Item_sum::check_sum_func(Session *session, Item **ref)
 
119
 
 
120
bool Item_sum::check_sum_func(THD *thd, Item **ref)
147
121
{
148
122
  bool invalid= false;
149
 
  nesting_map allow_sum_func= session->lex->allow_sum_func;
150
 
  /*
 
123
  nesting_map allow_sum_func= thd->lex->allow_sum_func;
 
124
  /*  
151
125
    The value of max_arg_level is updated if an argument of the set function
152
126
    contains a column reference resolved  against a subquery whose level is
153
127
    greater than the current value of max_arg_level.
154
128
    max_arg_level cannot be greater than nest level.
155
 
    nest level is always >= 0
156
 
  */
 
129
    nest level is always >= 0  
 
130
  */ 
157
131
  if (nest_level == max_arg_level)
158
132
  {
159
133
    /*
160
 
      The function must be aggregated in the current subquery,
161
 
      If it is there under a construct where it is not allowed
162
 
      we report an error.
163
 
    */
 
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
    */ 
164
138
    invalid= !(allow_sum_func & (1 << max_arg_level));
165
139
  }
166
140
  else if (max_arg_level >= 0 || !(allow_sum_func & (1 << nest_level)))
170
144
      Try to find a subquery where it can be aggregated;
171
145
      If we fail to find such a subquery report an error.
172
146
    */
173
 
    if (register_sum_func(session, ref))
 
147
    if (register_sum_func(thd, ref))
174
148
      return true;
175
149
    invalid= aggr_level < 0 && !(allow_sum_func & (1 << nest_level));
176
150
    if (!invalid && false)
179
153
  if (!invalid && aggr_level < 0)
180
154
  {
181
155
    aggr_level= nest_level;
182
 
    aggr_sel= session->lex->current_select;
 
156
    aggr_sel= thd->lex->current_select;
183
157
  }
184
158
  /*
185
159
    By this moment we either found a subquery where the set function is
186
160
    to be aggregated  and assigned a value that is  >= 0 to aggr_level,
187
 
    or set the value of 'invalid' to TRUE to report later an error.
 
161
    or set the value of 'invalid' to TRUE to report later an error. 
188
162
  */
189
 
  /*
 
163
  /* 
190
164
    Additionally we have to check whether possible nested set functions
191
165
    are acceptable here: they are not, if the level of aggregation of
192
166
    some of them is less than aggr_level.
193
167
  */
194
 
  if (!invalid)
 
168
  if (!invalid) 
195
169
    invalid= aggr_level <= max_sum_func_level;
196
 
  if (invalid)
 
170
  if (invalid)  
197
171
  {
198
172
    my_message(ER_INVALID_GROUP_FUNC_USE, ER(ER_INVALID_GROUP_FUNC_USE),
199
173
               MYF(0));
205
179
    /*
206
180
      If the set function is nested adjust the value of
207
181
      max_sum_func_level for the nesting set function.
208
 
      We take into account only enclosed set functions that are to be
209
 
      aggregated on the same level or above of the nest level of
 
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 
210
184
      the enclosing set function.
211
185
      But we must always pass up the max_sum_func_level because it is
212
186
      the maximum nested level of all directly and indirectly enclosed
257
231
    List_iterator<Item_field> of(outer_fields);
258
232
    while ((field= of++))
259
233
    {
260
 
      Select_Lex *sel= field->cached_table->select_lex;
 
234
      SELECT_LEX *sel= field->cached_table->select_lex;
261
235
      if (sel->nest_level < aggr_level)
262
236
      {
263
237
        if (in_sum_func)
269
243
          in_sum_func->outer_fields.push_back(field);
270
244
        }
271
245
        else
272
 
        {
273
 
          sel->full_group_by_flag.set(NON_AGG_FIELD_USED);
274
 
        }
 
246
          sel->full_group_by_flag|= NON_AGG_FIELD_USED;
275
247
      }
276
248
      if (sel->nest_level > aggr_level &&
277
 
          (sel->full_group_by_flag.test(SUM_FUNC_USED)) &&
278
 
          ! sel->group_list.elements)
 
249
          (sel->full_group_by_flag & SUM_FUNC_USED) &&
 
250
          !sel->group_list.elements)
279
251
      {
280
252
        my_message(ER_MIX_OF_GROUP_FUNC_AND_FIELDS,
281
253
                   ER(ER_MIX_OF_GROUP_FUNC_AND_FIELDS), MYF(0));
283
255
      }
284
256
    }
285
257
  }
286
 
  aggr_sel->full_group_by_flag.set(SUM_FUNC_USED);
 
258
  aggr_sel->full_group_by_flag|= SUM_FUNC_USED;
287
259
  update_used_tables();
288
 
  session->lex->in_sum_func= in_sum_func;
 
260
  thd->lex->in_sum_func= in_sum_func;
289
261
  return false;
290
262
}
291
263
 
296
268
    aggregated. If it finds such a subquery then aggr_level is set to
297
269
    the nest level of this subquery and the item for the set function
298
270
    is added to the list of set functions used in nested subqueries
299
 
    inner_sum_func_list defined for each subquery. When the item is placed
 
271
    inner_sum_func_list defined for each subquery. When the item is placed 
300
272
    there the field 'ref_by' is set to ref.
301
273
 
302
274
  @note
305
277
    a subquery in one chain. It would simplify the process of 'splitting'
306
278
    for set functions.
307
279
 
308
 
  @param session  reference to the thread context info
 
280
  @param thd  reference to the thread context info
309
281
  @param ref  location of the pointer to this item in the embedding expression
310
282
 
311
283
  @retval
312
284
    FALSE  if the executes without failures (currently always)
313
285
  @retval
314
286
    TRUE   otherwise
315
 
*/
 
287
*/  
316
288
 
317
 
bool Item_sum::register_sum_func(Session *session, Item **ref)
 
289
bool Item_sum::register_sum_func(THD *thd, Item **ref)
318
290
{
319
 
  Select_Lex *sl;
320
 
  nesting_map allow_sum_func= session->lex->allow_sum_func;
321
 
  for (sl= session->lex->current_select->master_unit()->outer_select() ;
 
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() ;
322
294
       sl && sl->nest_level > max_arg_level;
323
295
       sl= sl->master_unit()->outer_select() )
324
296
  {
331
303
  }
332
304
  if (sl && (allow_sum_func & (1 << sl->nest_level)))
333
305
  {
334
 
    /*
 
306
    /* 
335
307
      We reached the subquery of level max_arg_level and checked
336
 
      that the function can be aggregated here.
 
308
      that the function can be aggregated here. 
337
309
      The set function will be aggregated in this subquery.
338
 
    */
 
310
    */   
339
311
    aggr_level= sl->nest_level;
340
312
    aggr_sel= sl;
341
313
 
354
326
    aggr_sel->inner_sum_func_list= this;
355
327
    aggr_sel->with_sum_func= 1;
356
328
 
357
 
    /*
 
329
    /* 
358
330
      Mark Item_subselect(s) as containing aggregate function all the way up
359
331
      to aggregate function's calculation context.
360
332
      Note that we must not mark the Item of calculation context itself
361
 
      because with_sum_func on the calculation context Select_Lex is
 
333
      because with_sum_func on the calculation context st_select_lex is
362
334
      already set above.
363
335
 
364
 
      with_sum_func being set for an Item means that this Item refers
 
336
      with_sum_func being set for an Item means that this Item refers 
365
337
      (somewhere in it, e.g. one of its arguments if it's a function) directly
366
338
      or through intermediate items to an aggregate function that is calculated
367
339
      in a context "outside" of the Item (e.g. in the current or outer select).
368
340
 
369
 
      with_sum_func being set for an Select_Lex means that this Select_Lex
 
341
      with_sum_func being set for an st_select_lex means that this st_select_lex
370
342
      has aggregate functions directly referenced (i.e. not through a sub-select).
371
343
    */
372
 
    for (sl= session->lex->current_select;
 
344
    for (sl= thd->lex->current_select; 
373
345
         sl && sl != aggr_sel && sl->master_unit()->item;
374
346
         sl= sl->master_unit()->outer_select() )
375
347
      sl->master_unit()->item->with_sum_func= 1;
376
348
  }
377
 
  session->lex->current_select->mark_as_dependent(aggr_sel);
 
349
  thd->lex->current_select->mark_as_dependent(aggr_sel);
378
350
  return false;
379
351
}
380
352
 
381
353
 
382
 
Item_sum::Item_sum(List<Item> &list) :arg_count(list.elements),
 
354
Item_sum::Item_sum(List<Item> &list) :arg_count(list.elements), 
383
355
  forced_const(false)
384
356
{
385
 
  if ((args=(Item**) memory::sql_alloc(sizeof(Item*)*arg_count)))
 
357
  if ((args=(Item**) sql_alloc(sizeof(Item*)*arg_count)))
386
358
  {
387
359
    uint32_t i=0;
388
360
    List_iterator_fast<Item> li(list);
402
374
  Constructor used in processing select with temporary tebles.
403
375
*/
404
376
 
405
 
Item_sum::Item_sum(Session *session, Item_sum *item):
406
 
  Item_result_field(session, item), arg_count(item->arg_count),
 
377
Item_sum::Item_sum(THD *thd, Item_sum *item):
 
378
  Item_result_field(thd, item), arg_count(item->arg_count),
407
379
  aggr_sel(item->aggr_sel),
408
380
  nest_level(item->nest_level), aggr_level(item->aggr_level),
409
381
  quick_group(item->quick_group), used_tables_cache(item->used_tables_cache),
410
 
  forced_const(item->forced_const)
 
382
  forced_const(item->forced_const) 
411
383
{
412
384
  if (arg_count <= 2)
413
385
    args=tmp_args;
414
386
  else
415
 
    if (!(args= (Item**) session->getMemRoot()->allocate(sizeof(Item*)*arg_count)))
 
387
    if (!(args= (Item**) thd->alloc(sizeof(Item*)*arg_count)))
416
388
      return;
417
389
  memcpy(args, item->args, sizeof(Item*)*arg_count);
418
390
}
420
392
 
421
393
void Item_sum::mark_as_sum_func()
422
394
{
423
 
  Select_Lex *cur_select= getSession().getLex()->current_select;
 
395
  SELECT_LEX *cur_select= current_thd->lex->current_select;
424
396
  cur_select->n_sum_items++;
425
397
  cur_select->with_sum_func= 1;
426
398
  with_sum_func= 1;
427
399
}
428
400
 
429
401
 
430
 
void Item_sum::make_field(SendField *tmp_field)
 
402
void Item_sum::make_field(Send_field *tmp_field)
431
403
{
432
404
  if (args[0]->type() == Item::FIELD_ITEM && keep_field_type())
433
405
  {
467
439
  max_length=float_length(decimals);
468
440
}
469
441
 
470
 
Item *Item_sum::get_tmp_table_item(Session *session)
 
442
Item *Item_sum::get_tmp_table_item(THD *thd)
471
443
{
472
 
  Item_sum* sum_item= (Item_sum *) copy_or_same(session);
 
444
  Item_sum* sum_item= (Item_sum *) copy_or_same(thd);
473
445
  if (sum_item && sum_item->result_field)          // If not a const sum func
474
446
  {
475
447
    Field *result_field_tmp= sum_item->result_field;
505
477
}
506
478
 
507
479
 
508
 
Field *Item_sum::create_tmp_field(bool ,
 
480
Field *Item_sum::create_tmp_field(bool group __attribute__((unused)),
509
481
                                  Table *table,
510
482
                                  uint32_t convert_blob_length)
511
483
{
512
 
  Field *field= NULL;
513
 
 
 
484
  Field *field;
514
485
  switch (result_type()) {
515
486
  case REAL_RESULT:
516
487
    field= new Field_double(max_length, maybe_null, name, decimals, true);
517
488
    break;
518
 
 
519
489
  case INT_RESULT:
520
 
    field= new field::Int64(max_length, maybe_null, name, unsigned_flag);
 
490
    field= new Field_int64_t(max_length, maybe_null, name, unsigned_flag);
521
491
    break;
522
 
 
523
492
  case STRING_RESULT:
524
493
    if (max_length/collation.collation->mbmaxlen <= 255 ||
525
494
        convert_blob_length > Field_varstring::MAX_SIZE ||
526
495
        !convert_blob_length)
527
 
    {
528
496
      return make_string_field(table);
529
 
    }
530
 
 
531
 
    table->setVariableWidth();
532
497
    field= new Field_varstring(convert_blob_length, maybe_null,
533
 
                               name, collation.collation);
 
498
                               name, table->s, collation.collation);
534
499
    break;
535
 
 
536
500
  case DECIMAL_RESULT:
537
 
    field= new Field_decimal(max_length, maybe_null, name,
538
 
                             decimals, unsigned_flag);
 
501
    field= new Field_new_decimal(max_length, maybe_null, name,
 
502
                                 decimals, unsigned_flag);
539
503
    break;
540
 
 
541
504
  case ROW_RESULT:
 
505
  default:
542
506
    // This case should never be choosen
543
507
    assert(0);
544
508
    return 0;
545
509
  }
546
 
 
547
510
  if (field)
548
511
    field->init(table);
549
 
 
550
512
  return field;
551
513
}
552
514
 
577
539
}
578
540
 
579
541
 
580
 
int64_t Item_sum_num::val_int()
581
 
{
582
 
  assert(fixed == 1);
583
 
  return (int64_t) rint(val_real());             /* Real as default */
584
 
}
585
 
 
586
 
 
587
 
type::Decimal *Item_sum_num::val_decimal(type::Decimal *decimal_value)
 
542
my_decimal *Item_sum_num::val_decimal(my_decimal *decimal_value)
588
543
{
589
544
  return val_decimal_from_real(decimal_value);
590
545
}
597
552
}
598
553
 
599
554
 
600
 
type::Decimal *Item_sum_int::val_decimal(type::Decimal *decimal_value)
 
555
my_decimal *Item_sum_int::val_decimal(my_decimal *decimal_value)
601
556
{
602
557
  return val_decimal_from_int(decimal_value);
603
558
}
604
559
 
605
560
 
606
561
bool
607
 
Item_sum_num::fix_fields(Session *session, Item **ref)
 
562
Item_sum_num::fix_fields(THD *thd, Item **ref)
608
563
{
609
564
  assert(fixed == 0);
610
565
 
611
 
  if (init_sum_func_check(session))
 
566
  if (init_sum_func_check(thd))
612
567
    return true;
613
568
 
614
569
  decimals=0;
615
570
  maybe_null=0;
616
571
  for (uint32_t i=0 ; i < arg_count ; i++)
617
572
  {
618
 
    if (args[i]->fix_fields(session, args + i) || args[i]->check_cols(1))
 
573
    if (args[i]->fix_fields(thd, args + i) || args[i]->check_cols(1))
619
574
      return true;
620
575
    set_if_bigger(decimals, args[i]->decimals);
621
576
    maybe_null |= args[i]->maybe_null;
625
580
  null_value=1;
626
581
  fix_length_and_dec();
627
582
 
628
 
  if (check_sum_func(session, ref))
 
583
  if (check_sum_func(thd, ref))
629
584
    return true;
630
585
 
631
586
  fixed= 1;
633
588
}
634
589
 
635
590
 
636
 
Item_sum_hybrid::Item_sum_hybrid(Session *session, Item_sum_hybrid *item)
637
 
  :Item_sum(session, item), value(item->value), hybrid_type(item->hybrid_type),
 
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),
638
593
  hybrid_field_type(item->hybrid_field_type), cmp_sign(item->cmp_sign),
639
594
  was_values(item->was_values)
640
595
{
644
599
    sum_int= item->sum_int;
645
600
    break;
646
601
  case DECIMAL_RESULT:
647
 
    class_decimal2decimal(&item->sum_dec, &sum_dec);
 
602
    my_decimal2decimal(&item->sum_dec, &sum_dec);
648
603
    break;
649
604
  case REAL_RESULT:
650
605
    sum= item->sum;
656
611
    */
657
612
    break;
658
613
  case ROW_RESULT:
 
614
  default:
659
615
    assert(0);
660
616
  }
661
617
  collation.set(item->collation);
662
618
}
663
619
 
664
620
bool
665
 
Item_sum_hybrid::fix_fields(Session *session, Item **ref)
 
621
Item_sum_hybrid::fix_fields(THD *thd, Item **ref)
666
622
{
667
623
  assert(fixed == 0);
668
624
 
669
625
  Item *item= args[0];
670
626
 
671
 
  if (init_sum_func_check(session))
 
627
  if (init_sum_func_check(thd))
672
628
    return true;
673
629
 
674
630
  // 'item' can be changed during fix_fields
675
 
  if ((!item->fixed && item->fix_fields(session, args)) ||
 
631
  if ((!item->fixed && item->fix_fields(thd, args)) ||
676
632
      (item= args[0])->check_cols(1))
677
633
    return true;
678
634
  decimals=item->decimals;
684
640
    break;
685
641
  case DECIMAL_RESULT:
686
642
    max_length= item->max_length;
687
 
    sum_dec.set_zero();
 
643
    my_decimal_set_zero(&sum_dec);
688
644
    break;
689
645
  case REAL_RESULT:
690
646
    max_length= float_length(decimals);
694
650
    max_length= item->max_length;
695
651
    break;
696
652
  case ROW_RESULT:
 
653
  default:
697
654
    assert(0);
698
655
  };
699
656
  /* MIN/MAX can return NULL for empty set indepedent of the used column */
709
666
  else
710
667
    hybrid_field_type= Item::field_type();
711
668
 
712
 
  if (check_sum_func(session, ref))
 
669
  if (check_sum_func(thd, ref))
713
670
    return true;
714
671
 
715
672
  fixed= 1;
723
680
  if (args[0]->type() == Item::FIELD_ITEM)
724
681
  {
725
682
    field= ((Item_field*) args[0])->field;
726
 
 
727
 
    if ((field= create_tmp_field_from_field(&getSession(), field, name, table,
 
683
    
 
684
    if ((field= create_tmp_field_from_field(current_thd, field, name, table,
728
685
                                            NULL, convert_blob_length)))
729
686
      field->flags&= ~NOT_NULL_FLAG;
730
687
    return field;
735
692
    fields creations separately.
736
693
  */
737
694
  switch (args[0]->field_type()) {
738
 
  case DRIZZLE_TYPE_DATE:
739
 
    field= new Field_date(maybe_null, name, collation.collation);
 
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);
740
700
    break;
741
701
  case DRIZZLE_TYPE_TIMESTAMP:
742
702
  case DRIZZLE_TYPE_DATETIME:
745
705
  default:
746
706
    return Item_sum::create_tmp_field(group, table, convert_blob_length);
747
707
  }
748
 
 
749
708
  if (field)
750
709
    field->init(table);
751
 
 
752
710
  return field;
753
711
}
754
712
 
761
719
  @todo
762
720
  check if the following assignments are really needed
763
721
*/
764
 
Item_sum_sum::Item_sum_sum(Session *session, Item_sum_sum *item)
765
 
  :Item_sum_num(session, item), hybrid_type(item->hybrid_type),
 
722
Item_sum_sum::Item_sum_sum(THD *thd, Item_sum_sum *item) 
 
723
  :Item_sum_num(thd, item), hybrid_type(item->hybrid_type),
766
724
   curr_dec_buff(item->curr_dec_buff)
767
725
{
768
726
  /* TODO: check if the following assignments are really needed */
769
727
  if (hybrid_type == DECIMAL_RESULT)
770
728
  {
771
 
    class_decimal2decimal(item->dec_buffs, dec_buffs);
772
 
    class_decimal2decimal(item->dec_buffs + 1, dec_buffs + 1);
 
729
    my_decimal2decimal(item->dec_buffs, dec_buffs);
 
730
    my_decimal2decimal(item->dec_buffs + 1, dec_buffs + 1);
773
731
  }
774
732
  else
775
733
    sum= item->sum;
776
734
}
777
735
 
778
 
Item *Item_sum_sum::copy_or_same(Session* session)
 
736
Item *Item_sum_sum::copy_or_same(THD* thd)
779
737
{
780
 
  return new (session->mem_root) Item_sum_sum(session, this);
 
738
  return new (thd->mem_root) Item_sum_sum(thd, this);
781
739
}
782
740
 
783
741
 
787
745
  if (hybrid_type == DECIMAL_RESULT)
788
746
  {
789
747
    curr_dec_buff= 0;
790
 
    dec_buffs->set_zero();
 
748
    my_decimal_set_zero(dec_buffs);
791
749
  }
792
750
  else
793
751
    sum= 0.0;
807
765
    break;
808
766
  case INT_RESULT:
809
767
  case DECIMAL_RESULT:
810
 
    {
811
 
      /* SUM result can't be longer than length(arg) + length(MAX_ROWS) */
812
 
      int precision= args[0]->decimal_precision() + DECIMAL_LONGLONG_DIGITS;
813
 
      max_length= class_decimal_precision_to_length(precision, decimals,
814
 
                                                 unsigned_flag);
815
 
      curr_dec_buff= 0;
816
 
      hybrid_type= DECIMAL_RESULT;
817
 
      dec_buffs->set_zero();
818
 
      break;
819
 
    }
 
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
  }
820
778
  case ROW_RESULT:
 
779
  default:
821
780
    assert(0);
822
781
  }
 
782
  return;
823
783
}
824
784
 
825
785
 
827
787
{
828
788
  if (hybrid_type == DECIMAL_RESULT)
829
789
  {
830
 
    type::Decimal value, *val= args[0]->val_decimal(&value);
 
790
    my_decimal value, *val= args[0]->val_decimal(&value);
831
791
    if (!args[0]->null_value)
832
792
    {
833
 
      class_decimal_add(E_DEC_FATAL_ERROR, dec_buffs + (curr_dec_buff^1),
 
793
      my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs + (curr_dec_buff^1),
834
794
                     val, dec_buffs + curr_dec_buff);
835
795
      curr_dec_buff^= 1;
836
796
      null_value= 0;
852
812
  if (hybrid_type == DECIMAL_RESULT)
853
813
  {
854
814
    int64_t result;
855
 
    (dec_buffs + curr_dec_buff)->val_int32(E_DEC_FATAL_ERROR, unsigned_flag, &result);
 
815
    my_decimal2int(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, unsigned_flag,
 
816
                   &result);
856
817
    return result;
857
818
  }
858
819
  return (int64_t) rint(val_real());
863
824
{
864
825
  assert(fixed == 1);
865
826
  if (hybrid_type == DECIMAL_RESULT)
866
 
    class_decimal2double(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, &sum);
 
827
    my_decimal2double(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, &sum);
867
828
  return sum;
868
829
}
869
830
 
876
837
}
877
838
 
878
839
 
879
 
type::Decimal *Item_sum_sum::val_decimal(type::Decimal *val)
 
840
my_decimal *Item_sum_sum::val_decimal(my_decimal *val)
880
841
{
881
842
  if (hybrid_type == DECIMAL_RESULT)
882
843
    return (dec_buffs + curr_dec_buff);
885
846
 
886
847
/***************************************************************************/
887
848
 
 
849
#ifdef __cplusplus
 
850
extern "C" {
 
851
#endif
 
852
 
888
853
/* Declarations for auxilary C-callbacks */
889
854
 
890
855
static int simple_raw_key_cmp(void* arg, const void* key1, const void* key2)
894
859
 
895
860
 
896
861
static int item_sum_distinct_walk(void *element,
897
 
                                  uint32_t ,
 
862
                                  element_count num_of_dups __attribute__((unused)),
898
863
                                  void *item)
899
864
{
900
865
  return ((Item_sum_distinct*) (item))->unique_walk_function(element);
901
866
}
902
867
 
 
868
#ifdef __cplusplus
 
869
}
 
870
#endif
 
871
 
903
872
/* Item_sum_distinct */
904
873
 
905
874
Item_sum_distinct::Item_sum_distinct(Item *item_arg)
915
884
}
916
885
 
917
886
 
918
 
Item_sum_distinct::Item_sum_distinct(Session *session, Item_sum_distinct *original)
919
 
  :Item_sum_num(session, original), val(original->val), tree(0),
 
887
Item_sum_distinct::Item_sum_distinct(THD *thd, Item_sum_distinct *original)
 
888
  :Item_sum_num(thd, original), val(original->val), tree(0),
920
889
  table_field_type(original->table_field_type)
921
890
{
922
891
  quick_group= 0;
939
908
 
940
909
  virtual void div(Hybrid_type *val, uint64_t u) const
941
910
  {
942
 
    int2_class_decimal(E_DEC_FATAL_ERROR, val->integer, 0, val->dec_buf);
 
911
    int2my_decimal(E_DEC_FATAL_ERROR, val->integer, 0, val->dec_buf);
943
912
    val->used_dec_buf_no= 0;
944
913
    val->traits= Hybrid_type_traits_decimal::instance();
945
914
    val->traits->div(val, u);
956
925
  return &fast_decimal_traits_instance;
957
926
}
958
927
 
959
 
 
960
928
void Item_sum_distinct::fix_length_and_dec()
961
929
{
962
930
  assert(args[0]->fixed);
963
931
 
964
 
  null_value= maybe_null= true;
965
932
  table_field_type= args[0]->field_type();
966
933
 
967
934
  /* Adjust tmp table type according to the chosen aggregation type */
972
939
    table_field_type= DRIZZLE_TYPE_DOUBLE;
973
940
    break;
974
941
  case INT_RESULT:
975
 
    /*
976
 
      Preserving int8, int16, int32 field types gives ~10% performance boost
977
 
      as the size of result tree becomes significantly smaller.
978
 
      Another speed up we gain by using int64_t for intermediate
979
 
      calculations. The range of int64 is enough to hold sum 2^32 distinct
980
 
      integers each <= 2^32.
981
 
    */
982
 
    if (table_field_type == DRIZZLE_TYPE_LONG)
983
 
    {
984
 
      val.traits= Hybrid_type_traits_fast_decimal::instance();
985
 
      break;
986
 
    }
987
 
    table_field_type= DRIZZLE_TYPE_LONGLONG;
988
 
    /* fallthrough */
 
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 */
989
956
  case DECIMAL_RESULT:
990
957
    val.traits= Hybrid_type_traits_decimal::instance();
991
958
    if (table_field_type != DRIZZLE_TYPE_LONGLONG)
992
 
      table_field_type= DRIZZLE_TYPE_DECIMAL;
 
959
      table_field_type= DRIZZLE_TYPE_NEWDECIMAL;
993
960
    break;
994
961
  case ROW_RESULT:
 
962
  default:
995
963
    assert(0);
996
964
  }
997
 
 
998
965
  val.traits->fix_length_and_dec(this, args[0]);
999
966
}
1000
967
 
1001
968
 
1002
 
enum Item_result Item_sum_distinct::result_type () const
1003
 
{
1004
 
  return val.traits->type();
1005
 
}
1006
 
 
1007
 
 
1008
969
/**
1009
970
  @todo
1010
971
  check that the case of CHAR(0) works OK
1011
972
*/
1012
 
bool Item_sum_distinct::setup(Session *session)
 
973
bool Item_sum_distinct::setup(THD *thd)
1013
974
{
1014
 
  List<CreateField> field_list;
1015
 
  CreateField field_def;                              /* field definition */
 
975
  List<Create_field> field_list;
 
976
  Create_field field_def;                              /* field definition */
1016
977
  /* It's legal to call setup() more than once when in a subquery */
1017
978
  if (tree)
1018
979
    return(false);
1031
992
  assert(args[0]->fixed);
1032
993
 
1033
994
  field_def.init_for_tmp_table(table_field_type, args[0]->max_length,
1034
 
                               args[0]->decimals, args[0]->maybe_null);
 
995
                               args[0]->decimals, args[0]->maybe_null,
 
996
                               args[0]->unsigned_flag);
1035
997
 
1036
 
  if (! (table= session->getInstanceTable(field_list)))
 
998
  if (! (table= create_virtual_tmp_table(thd, field_list)))
1037
999
    return(true);
1038
1000
 
1039
1001
  /* XXX: check that the case of CHAR(0) works OK */
1040
 
  tree_key_length= table->getShare()->getRecordLength() - table->getShare()->null_bytes;
 
1002
  tree_key_length= table->s->reclength - table->s->null_bytes;
1041
1003
 
1042
1004
  /*
1043
1005
    Unique handles all unique elements in a tree until they can't fit
1045
1007
    simple_raw_key_cmp because the table contains numbers only; decimals
1046
1008
    are converted to binary representation as well.
1047
1009
  */
1048
 
  tree= new Unique(simple_raw_key_cmp, &tree_key_length,
1049
 
                   tree_key_length,
1050
 
                   (size_t)session->variables.max_heap_table_size);
 
1010
  tree= new Unique(simple_raw_key_cmp, &tree_key_length, tree_key_length,
 
1011
                   thd->variables.max_heap_table_size);
1051
1012
 
1052
1013
  is_evaluated= false;
1053
1014
  return(tree == 0);
1056
1017
 
1057
1018
bool Item_sum_distinct::add()
1058
1019
{
1059
 
  args[0]->save_in_field(table->getField(0), false);
 
1020
  args[0]->save_in_field(table->field[0], false);
1060
1021
  is_evaluated= false;
1061
 
  if (!table->getField(0)->is_null())
 
1022
  if (!table->field[0]->is_null())
1062
1023
  {
1063
1024
    assert(tree);
1064
1025
    null_value= 0;
1066
1027
      '0' values are also stored in the tree. This doesn't matter
1067
1028
      for SUM(DISTINCT), but is important for AVG(DISTINCT)
1068
1029
    */
1069
 
    return tree->unique_add(table->getField(0)->ptr);
 
1030
    return tree->unique_add(table->field[0]->ptr);
1070
1031
  }
1071
1032
  return 0;
1072
1033
}
1074
1035
 
1075
1036
bool Item_sum_distinct::unique_walk_function(void *element)
1076
1037
{
1077
 
  memcpy(table->getField(0)->ptr, element, tree_key_length);
 
1038
  memcpy(table->field[0]->ptr, element, tree_key_length);
1078
1039
  ++count;
1079
 
  val.traits->add(&val, table->getField(0));
 
1040
  val.traits->add(&val, table->field[0]);
1080
1041
  return 0;
1081
1042
}
1082
1043
 
1118
1079
     */
1119
1080
    if (tree)
1120
1081
    {
1121
 
      table->getField(0)->set_notnull();
 
1082
      table->field[0]->set_notnull();
1122
1083
      tree->walk(item_sum_distinct_walk, (void*) this);
1123
1084
    }
1124
1085
    is_evaluated= true;
1133
1094
}
1134
1095
 
1135
1096
 
1136
 
type::Decimal *Item_sum_distinct::val_decimal(type::Decimal *to)
 
1097
my_decimal *Item_sum_distinct::val_decimal(my_decimal *to)
1137
1098
{
1138
1099
  calculate_val_and_count();
1139
1100
  if (null_value)
1165
1126
Item_sum_avg_distinct::fix_length_and_dec()
1166
1127
{
1167
1128
  Item_sum_distinct::fix_length_and_dec();
1168
 
  prec_increment= getSession().variables.div_precincrement;
 
1129
  prec_increment= current_thd->variables.div_precincrement;
1169
1130
  /*
1170
1131
    AVG() will divide val by count. We need to reserve digits
1171
1132
    after decimal point as the result can be fractional.
1172
1133
  */
1173
 
  decimals= min(decimals + prec_increment, (unsigned int)NOT_FIXED_DEC);
 
1134
  decimals= cmin(decimals + prec_increment, (unsigned int)NOT_FIXED_DEC);
1174
1135
}
1175
1136
 
1176
1137
 
1187
1148
}
1188
1149
 
1189
1150
 
1190
 
Item *Item_sum_count::copy_or_same(Session* session)
 
1151
Item *Item_sum_count::copy_or_same(THD* thd)
1191
1152
{
1192
 
  return new (session->mem_root) Item_sum_count(session, this);
 
1153
  return new (thd->mem_root) Item_sum_count(thd, this);
1193
1154
}
1194
1155
 
1195
1156
 
1228
1189
{
1229
1190
  Item_sum_sum::fix_length_and_dec();
1230
1191
  maybe_null=null_value=1;
1231
 
  prec_increment= getSession().variables.div_precincrement;
1232
 
 
 
1192
  prec_increment= current_thd->variables.div_precincrement;
1233
1193
  if (hybrid_type == DECIMAL_RESULT)
1234
1194
  {
1235
1195
    int precision= args[0]->decimal_precision() + prec_increment;
1236
 
    decimals= min(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
1237
 
    max_length= class_decimal_precision_to_length(precision, decimals,
 
1196
    decimals= cmin(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
 
1197
    max_length= my_decimal_precision_to_length(precision, decimals,
1238
1198
                                               unsigned_flag);
1239
 
    f_precision= min(precision+DECIMAL_LONGLONG_DIGITS, DECIMAL_MAX_PRECISION);
 
1199
    f_precision= cmin(precision+DECIMAL_LONGLONG_DIGITS, DECIMAL_MAX_PRECISION);
1240
1200
    f_scale=  args[0]->decimals;
1241
 
    dec_bin_size= class_decimal_get_binary_size(f_precision, f_scale);
 
1201
    dec_bin_size= my_decimal_get_binary_size(f_precision, f_scale);
1242
1202
  }
1243
1203
  else {
1244
 
    decimals= min(args[0]->decimals + prec_increment, (unsigned int) NOT_FIXED_DEC);
 
1204
    decimals= cmin(args[0]->decimals + prec_increment, (unsigned int) NOT_FIXED_DEC);
1245
1205
    max_length= args[0]->max_length + prec_increment;
1246
1206
  }
1247
1207
}
1248
1208
 
1249
1209
 
1250
 
Item *Item_sum_avg::copy_or_same(Session* session)
 
1210
Item *Item_sum_avg::copy_or_same(THD* thd)
1251
1211
{
1252
 
  return new (session->mem_root) Item_sum_avg(session, this);
 
1212
  return new (thd->mem_root) Item_sum_avg(thd, this);
1253
1213
}
1254
1214
 
1255
1215
 
1256
1216
Field *Item_sum_avg::create_tmp_field(bool group, Table *table,
1257
 
                                      uint32_t )
 
1217
                                      uint32_t convert_blob_len __attribute__((unused)))
1258
1218
{
1259
1219
  Field *field;
1260
1220
  if (group)
1264
1224
      The easiest way is to do this is to store both value in a string
1265
1225
      and unpack on access.
1266
1226
    */
1267
 
    table->setVariableWidth();
1268
1227
    field= new Field_varstring(((hybrid_type == DECIMAL_RESULT) ?
1269
1228
                                dec_bin_size : sizeof(double)) + sizeof(int64_t),
1270
 
                               0, name, &my_charset_bin);
 
1229
                               0, name, table->s, &my_charset_bin);
1271
1230
  }
1272
1231
  else if (hybrid_type == DECIMAL_RESULT)
1273
 
    field= new Field_decimal(max_length, maybe_null, name,
1274
 
                             decimals, unsigned_flag);
 
1232
    field= new Field_new_decimal(max_length, maybe_null, name,
 
1233
                                 decimals, unsigned_flag);
1275
1234
  else
1276
1235
    field= new Field_double(max_length, maybe_null, name, decimals, true);
1277
1236
  if (field)
1308
1267
}
1309
1268
 
1310
1269
 
1311
 
int64_t Item_sum_avg::val_int()
1312
 
{
1313
 
  return (int64_t) rint(val_real());
1314
 
}
1315
 
 
1316
 
 
1317
 
type::Decimal *Item_sum_avg::val_decimal(type::Decimal *val)
1318
 
{
1319
 
  type::Decimal sum_buff, cnt;
1320
 
  const type::Decimal *sum_dec;
 
1270
my_decimal *Item_sum_avg::val_decimal(my_decimal *val)
 
1271
{
 
1272
  my_decimal sum_buff, cnt;
 
1273
  const my_decimal *sum_dec;
1321
1274
  assert(fixed == 1);
1322
1275
  if (!count)
1323
1276
  {
1333
1286
    return val_decimal_from_real(val);
1334
1287
 
1335
1288
  sum_dec= dec_buffs + curr_dec_buff;
1336
 
  int2_class_decimal(E_DEC_FATAL_ERROR, count, 0, &cnt);
1337
 
  class_decimal_div(E_DEC_FATAL_ERROR, val, sum_dec, &cnt, prec_increment);
 
1289
  int2my_decimal(E_DEC_FATAL_ERROR, count, 0, &cnt);
 
1290
  my_decimal_div(E_DEC_FATAL_ERROR, val, sum_dec, &cnt, prec_increment);
1338
1291
  return val;
1339
1292
}
1340
1293
 
1359
1312
  return sqrt(nr);
1360
1313
}
1361
1314
 
1362
 
Item *Item_sum_std::copy_or_same(Session* session)
 
1315
Item *Item_sum_std::copy_or_same(THD* thd)
1363
1316
{
1364
 
  return new (session->mem_root) Item_sum_std(session, this);
 
1317
  return new (thd->mem_root) Item_sum_std(thd, this);
1365
1318
}
1366
1319
 
1367
1320
 
1386
1339
{
1387
1340
  *count += 1;
1388
1341
 
1389
 
  if (*count == 1)
 
1342
  if (*count == 1) 
1390
1343
  {
1391
1344
    *m= nr;
1392
1345
    *s= 0;
1413
1366
}
1414
1367
 
1415
1368
 
1416
 
Item_sum_variance::Item_sum_variance(Session *session, Item_sum_variance *item):
1417
 
  Item_sum_num(session, item), hybrid_type(item->hybrid_type),
 
1369
Item_sum_variance::Item_sum_variance(THD *thd, Item_sum_variance *item):
 
1370
  Item_sum_num(thd, item), hybrid_type(item->hybrid_type),
1418
1371
    count(item->count), sample(item->sample),
1419
1372
    prec_increment(item->prec_increment)
1420
1373
{
1426
1379
void Item_sum_variance::fix_length_and_dec()
1427
1380
{
1428
1381
  maybe_null= null_value= 1;
1429
 
  prec_increment= getSession().variables.div_precincrement;
 
1382
  prec_increment= current_thd->variables.div_precincrement;
1430
1383
 
1431
1384
  /*
1432
1385
    According to the SQL2003 standard (Part 2, Foundations; sec 10.9,
1433
 
    aggregate function; paragraph 7h of Syntax Rules), "the declared
 
1386
    aggregate function; paragraph 7h of Syntax Rules), "the declared 
1434
1387
    type of the result is an implementation-defined aproximate numeric
1435
1388
    type.
1436
1389
  */
1439
1392
  switch (args[0]->result_type()) {
1440
1393
  case REAL_RESULT:
1441
1394
  case STRING_RESULT:
1442
 
    decimals= min(args[0]->decimals + 4, (int)NOT_FIXED_DEC);
 
1395
    decimals= cmin(args[0]->decimals + 4, NOT_FIXED_DEC);
1443
1396
    break;
1444
1397
  case INT_RESULT:
1445
1398
  case DECIMAL_RESULT:
1446
 
    {
1447
 
      int precision= args[0]->decimal_precision()*2 + prec_increment;
1448
 
      decimals= min(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
1449
 
      max_length= class_decimal_precision_to_length(precision, decimals,
1450
 
                                                 unsigned_flag);
 
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);
1451
1404
 
1452
 
      break;
1453
 
    }
 
1405
    break;
 
1406
  }
1454
1407
  case ROW_RESULT:
 
1408
  default:
1455
1409
    assert(0);
1456
1410
  }
 
1411
  return;
1457
1412
}
1458
1413
 
1459
1414
 
1460
 
Item *Item_sum_variance::copy_or_same(Session* session)
 
1415
Item *Item_sum_variance::copy_or_same(THD* thd)
1461
1416
{
1462
 
  return new (session->mem_root) Item_sum_variance(session, this);
 
1417
  return new (thd->mem_root) Item_sum_variance(thd, this);
1463
1418
}
1464
1419
 
1465
1420
 
1469
1424
  pass around.
1470
1425
*/
1471
1426
Field *Item_sum_variance::create_tmp_field(bool group, Table *table,
1472
 
                                           uint32_t )
 
1427
                                           uint32_t convert_blob_len __attribute__((unused)))
1473
1428
{
1474
1429
  Field *field;
1475
1430
  if (group)
1479
1434
      The easiest way is to do this is to store both value in a string
1480
1435
      and unpack on access.
1481
1436
    */
1482
 
    table->setVariableWidth();
1483
 
    field= new Field_varstring(sizeof(double)*2 + sizeof(int64_t), 0, name, &my_charset_bin);
 
1437
    field= new Field_varstring(sizeof(double)*2 + sizeof(int64_t), 0, name, table->s, &my_charset_bin);
1484
1438
  }
1485
1439
  else
1486
1440
    field= new Field_double(max_length, maybe_null, name, decimals, true);
1494
1448
 
1495
1449
void Item_sum_variance::clear()
1496
1450
{
1497
 
  count= 0;
 
1451
  count= 0; 
1498
1452
}
1499
1453
 
1500
1454
bool Item_sum_variance::add()
1501
1455
{
1502
 
  /*
 
1456
  /* 
1503
1457
    Why use a temporary variable?  We don't know if it is null until we
1504
1458
    evaluate it, which has the side-effect of setting null_value .
1505
1459
  */
1506
1460
  double nr= args[0]->val_real();
1507
 
 
 
1461
  
1508
1462
  if (!args[0]->null_value)
1509
1463
    variance_fp_recurrence_next(&recurrence_m, &recurrence_s, &count, nr);
1510
1464
  return 0;
1535
1489
}
1536
1490
 
1537
1491
 
1538
 
int64_t Item_sum_variance::val_int()
1539
 
{
1540
 
  /* can't be fix_fields()ed */
1541
 
  return (int64_t) rint(val_real());
1542
 
}
1543
 
 
1544
 
 
1545
 
type::Decimal *Item_sum_variance::val_decimal(type::Decimal *dec_buf)
 
1492
my_decimal *Item_sum_variance::val_decimal(my_decimal *dec_buf)
1546
1493
{
1547
1494
  assert(fixed == 1);
1548
1495
  return val_decimal_from_real(dec_buf);
1606
1553
    sum_int= 0;
1607
1554
    break;
1608
1555
  case DECIMAL_RESULT:
1609
 
    sum_dec.set_zero();
 
1556
    my_decimal_set_zero(&sum_dec);
1610
1557
    break;
1611
1558
  case REAL_RESULT:
1612
1559
    sum= 0.0;
1622
1569
  assert(fixed == 1);
1623
1570
  if (null_value)
1624
1571
    return 0.0;
1625
 
 
1626
1572
  switch (hybrid_type) {
1627
1573
  case STRING_RESULT:
1628
 
    {
1629
 
      char *end_not_used;
1630
 
      int err_not_used;
1631
 
      String *res;  res=val_str(&str_value);
1632
 
      return (res ? my_strntod(res->charset(), (char*) res->ptr(), res->length(),
1633
 
                               &end_not_used, &err_not_used) : 0.0);
1634
 
    }
 
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
  }
1635
1581
  case INT_RESULT:
 
1582
    if (unsigned_flag)
 
1583
      return uint64_t2double(sum_int);
1636
1584
    return (double) sum_int;
1637
1585
  case DECIMAL_RESULT:
1638
 
    class_decimal2double(E_DEC_FATAL_ERROR, &sum_dec, &sum);
 
1586
    my_decimal2double(E_DEC_FATAL_ERROR, &sum_dec, &sum);
1639
1587
    return sum;
1640
1588
  case REAL_RESULT:
1641
1589
    return sum;
1642
1590
  case ROW_RESULT:
 
1591
  default:
1643
1592
    // This case should never be choosen
1644
 
    break;
 
1593
    assert(0);
 
1594
    return 0;
1645
1595
  }
1646
 
 
1647
 
  assert(0);
1648
 
  return 0;
1649
1596
}
1650
1597
 
1651
1598
int64_t Item_sum_hybrid::val_int()
1659
1606
  case DECIMAL_RESULT:
1660
1607
  {
1661
1608
    int64_t result;
1662
 
    sum_dec.val_int32(E_DEC_FATAL_ERROR, unsigned_flag, &result);
 
1609
    my_decimal2int(E_DEC_FATAL_ERROR, &sum_dec, unsigned_flag, &result);
1663
1610
    return sum_int;
1664
1611
  }
1665
1612
  default:
1668
1615
}
1669
1616
 
1670
1617
 
1671
 
type::Decimal *Item_sum_hybrid::val_decimal(type::Decimal *val)
 
1618
my_decimal *Item_sum_hybrid::val_decimal(my_decimal *val)
1672
1619
{
1673
1620
  assert(fixed == 1);
1674
1621
  if (null_value)
1675
1622
    return 0;
1676
 
 
1677
1623
  switch (hybrid_type) {
1678
1624
  case STRING_RESULT:
1679
 
    val->store(E_DEC_FATAL_ERROR, &value);
 
1625
    string2my_decimal(E_DEC_FATAL_ERROR, &value, val);
1680
1626
    break;
1681
1627
  case REAL_RESULT:
1682
 
    double2_class_decimal(E_DEC_FATAL_ERROR, sum, val);
 
1628
    double2my_decimal(E_DEC_FATAL_ERROR, sum, val);
1683
1629
    break;
1684
1630
  case DECIMAL_RESULT:
1685
1631
    val= &sum_dec;
1686
1632
    break;
1687
1633
  case INT_RESULT:
1688
 
    int2_class_decimal(E_DEC_FATAL_ERROR, sum_int, unsigned_flag, val);
 
1634
    int2my_decimal(E_DEC_FATAL_ERROR, sum_int, unsigned_flag, val);
1689
1635
    break;
1690
1636
  case ROW_RESULT:
 
1637
  default:
1691
1638
    // This case should never be choosen
1692
1639
    assert(0);
1693
1640
    break;
1694
1641
  }
1695
 
 
1696
1642
  return val;                                   // Keep compiler happy
1697
1643
}
1698
1644
 
1703
1649
  assert(fixed == 1);
1704
1650
  if (null_value)
1705
1651
    return 0;
1706
 
 
1707
1652
  switch (hybrid_type) {
1708
1653
  case STRING_RESULT:
1709
1654
    return &value;
1711
1656
    str->set_real(sum,decimals, &my_charset_bin);
1712
1657
    break;
1713
1658
  case DECIMAL_RESULT:
1714
 
    class_decimal2string(&sum_dec, 0, str);
 
1659
    my_decimal2string(E_DEC_FATAL_ERROR, &sum_dec, 0, 0, 0, str);
1715
1660
    return str;
1716
1661
  case INT_RESULT:
1717
1662
    str->set_int(sum_int, unsigned_flag, &my_charset_bin);
1719
1664
  case ROW_RESULT:
1720
1665
  default:
1721
1666
    // This case should never be choosen
 
1667
    assert(0);
1722
1668
    break;
1723
1669
  }
1724
 
 
1725
1670
  return str;                                   // Keep compiler happy
1726
1671
}
1727
1672
 
1749
1694
}
1750
1695
 
1751
1696
 
1752
 
Item *Item_sum_min::copy_or_same(Session* session)
 
1697
Item *Item_sum_min::copy_or_same(THD* thd)
1753
1698
{
1754
 
  return new (session->mem_root) Item_sum_min(session, this);
 
1699
  return new (thd->mem_root) Item_sum_min(thd, this);
1755
1700
}
1756
1701
 
1757
1702
 
1759
1704
{
1760
1705
  switch (hybrid_type) {
1761
1706
  case STRING_RESULT:
 
1707
  {
 
1708
    String *result=args[0]->val_str(&tmp_value);
 
1709
    if (!args[0]->null_value &&
 
1710
        (null_value || sortcmp(&value,result,collation.collation) > 0))
1762
1711
    {
1763
 
      String *result=args[0]->val_str(&tmp_value);
1764
 
      if (!args[0]->null_value &&
1765
 
          (null_value || sortcmp(&value,result,collation.collation) > 0))
1766
 
      {
1767
 
        value.copy(*result);
1768
 
        null_value=0;
1769
 
      }
 
1712
      value.copy(*result);
 
1713
      null_value=0;
1770
1714
    }
1771
 
    break;
 
1715
  }
 
1716
  break;
1772
1717
  case INT_RESULT:
 
1718
  {
 
1719
    int64_t nr=args[0]->val_int();
 
1720
    if (!args[0]->null_value && (null_value ||
 
1721
                                 (unsigned_flag && 
 
1722
                                  (uint64_t) nr < (uint64_t) sum_int) ||
 
1723
                                 (!unsigned_flag && nr < sum_int)))
1773
1724
    {
1774
 
      int64_t nr=args[0]->val_int();
1775
 
      if (!args[0]->null_value && (null_value ||
1776
 
                                   (unsigned_flag &&
1777
 
                                    (uint64_t) nr < (uint64_t) sum_int) ||
1778
 
                                   (!unsigned_flag && nr < sum_int)))
1779
 
      {
1780
 
        sum_int=nr;
1781
 
        null_value=0;
1782
 
      }
 
1725
      sum_int=nr;
 
1726
      null_value=0;
1783
1727
    }
1784
 
    break;
 
1728
  }
 
1729
  break;
1785
1730
  case DECIMAL_RESULT:
 
1731
  {
 
1732
    my_decimal value_buff, *val= args[0]->val_decimal(&value_buff);
 
1733
    if (!args[0]->null_value &&
 
1734
        (null_value || (my_decimal_cmp(&sum_dec, val) > 0)))
1786
1735
    {
1787
 
      type::Decimal value_buff, *val= args[0]->val_decimal(&value_buff);
1788
 
      if (!args[0]->null_value &&
1789
 
          (null_value || (class_decimal_cmp(&sum_dec, val) > 0)))
1790
 
      {
1791
 
        class_decimal2decimal(val, &sum_dec);
1792
 
        null_value= 0;
1793
 
      }
 
1736
      my_decimal2decimal(val, &sum_dec);
 
1737
      null_value= 0;
1794
1738
    }
1795
 
    break;
 
1739
  }
 
1740
  break;
1796
1741
  case REAL_RESULT:
 
1742
  {
 
1743
    double nr= args[0]->val_real();
 
1744
    if (!args[0]->null_value && (null_value || nr < sum))
1797
1745
    {
1798
 
      double nr= args[0]->val_real();
1799
 
      if (!args[0]->null_value && (null_value || nr < sum))
1800
 
      {
1801
 
        sum=nr;
1802
 
        null_value=0;
1803
 
      }
 
1746
      sum=nr;
 
1747
      null_value=0;
1804
1748
    }
1805
 
    break;
 
1749
  }
 
1750
  break;
1806
1751
  case ROW_RESULT:
 
1752
  default:
1807
1753
    // This case should never be choosen
1808
1754
    assert(0);
1809
1755
    break;
1812
1758
}
1813
1759
 
1814
1760
 
1815
 
Item *Item_sum_max::copy_or_same(Session* session)
 
1761
Item *Item_sum_max::copy_or_same(THD* thd)
1816
1762
{
1817
 
  return new (session->mem_root) Item_sum_max(session, this);
 
1763
  return new (thd->mem_root) Item_sum_max(thd, this);
1818
1764
}
1819
1765
 
1820
1766
 
1822
1768
{
1823
1769
  switch (hybrid_type) {
1824
1770
  case STRING_RESULT:
 
1771
  {
 
1772
    String *result=args[0]->val_str(&tmp_value);
 
1773
    if (!args[0]->null_value &&
 
1774
        (null_value || sortcmp(&value,result,collation.collation) < 0))
1825
1775
    {
1826
 
      String *result=args[0]->val_str(&tmp_value);
1827
 
      if (!args[0]->null_value &&
1828
 
          (null_value || sortcmp(&value,result,collation.collation) < 0))
1829
 
      {
1830
 
        value.copy(*result);
1831
 
        null_value=0;
1832
 
      }
 
1776
      value.copy(*result);
 
1777
      null_value=0;
1833
1778
    }
1834
 
    break;
 
1779
  }
 
1780
  break;
1835
1781
  case INT_RESULT:
 
1782
  {
 
1783
    int64_t nr=args[0]->val_int();
 
1784
    if (!args[0]->null_value && (null_value ||
 
1785
                                 (unsigned_flag && 
 
1786
                                  (uint64_t) nr > (uint64_t) sum_int) ||
 
1787
                                 (!unsigned_flag && nr > sum_int)))
1836
1788
    {
1837
 
      int64_t nr=args[0]->val_int();
1838
 
      if (!args[0]->null_value && (null_value ||
1839
 
                                   (unsigned_flag &&
1840
 
                                    (uint64_t) nr > (uint64_t) sum_int) ||
1841
 
                                   (!unsigned_flag && nr > sum_int)))
1842
 
      {
1843
 
        sum_int=nr;
1844
 
        null_value=0;
1845
 
      }
 
1789
      sum_int=nr;
 
1790
      null_value=0;
1846
1791
    }
1847
 
    break;
 
1792
  }
 
1793
  break;
1848
1794
  case DECIMAL_RESULT:
 
1795
  {
 
1796
    my_decimal value_buff, *val= args[0]->val_decimal(&value_buff);
 
1797
    if (!args[0]->null_value &&
 
1798
        (null_value || (my_decimal_cmp(val, &sum_dec) > 0)))
1849
1799
    {
1850
 
      type::Decimal value_buff, *val= args[0]->val_decimal(&value_buff);
1851
 
      if (!args[0]->null_value &&
1852
 
          (null_value || (class_decimal_cmp(val, &sum_dec) > 0)))
1853
 
      {
1854
 
        class_decimal2decimal(val, &sum_dec);
1855
 
        null_value= 0;
1856
 
      }
 
1800
      my_decimal2decimal(val, &sum_dec);
 
1801
      null_value= 0;
1857
1802
    }
1858
 
    break;
 
1803
  }
 
1804
  break;
1859
1805
  case REAL_RESULT:
 
1806
  {
 
1807
    double nr= args[0]->val_real();
 
1808
    if (!args[0]->null_value && (null_value || nr > sum))
1860
1809
    {
1861
 
      double nr= args[0]->val_real();
1862
 
      if (!args[0]->null_value && (null_value || nr > sum))
1863
 
      {
1864
 
        sum=nr;
1865
 
        null_value=0;
1866
 
      }
 
1810
      sum=nr;
 
1811
      null_value=0;
1867
1812
    }
1868
 
    break;
 
1813
  }
 
1814
  break;
1869
1815
  case ROW_RESULT:
 
1816
  default:
1870
1817
    // This case should never be choosen
1871
1818
    assert(0);
1872
1819
    break;
1873
1820
  }
1874
 
 
1875
1821
  return 0;
1876
1822
}
1877
1823
 
1890
1836
  bits= reset_bits;
1891
1837
}
1892
1838
 
1893
 
Item *Item_sum_or::copy_or_same(Session* session)
 
1839
Item *Item_sum_or::copy_or_same(THD* thd)
1894
1840
{
1895
 
  return new (session->mem_root) Item_sum_or(session, this);
 
1841
  return new (thd->mem_root) Item_sum_or(thd, this);
1896
1842
}
1897
1843
 
1898
1844
 
1904
1850
  return 0;
1905
1851
}
1906
1852
 
1907
 
Item *Item_sum_xor::copy_or_same(Session* session)
 
1853
Item *Item_sum_xor::copy_or_same(THD* thd)
1908
1854
{
1909
 
  return new (session->mem_root) Item_sum_xor(session, this);
 
1855
  return new (thd->mem_root) Item_sum_xor(thd, this);
1910
1856
}
1911
1857
 
1912
1858
 
1918
1864
  return 0;
1919
1865
}
1920
1866
 
1921
 
Item *Item_sum_and::copy_or_same(Session* session)
 
1867
Item *Item_sum_and::copy_or_same(THD* thd)
1922
1868
{
1923
 
  return new (session->mem_root) Item_sum_and(session, this);
 
1869
  return new (thd->mem_root) Item_sum_and(thd, this);
1924
1870
}
1925
1871
 
1926
1872
 
1959
1905
{
1960
1906
  switch(hybrid_type) {
1961
1907
  case STRING_RESULT:
1962
 
    {
1963
 
      char buff[MAX_FIELD_WIDTH];
1964
 
      String tmp(buff,sizeof(buff),result_field->charset()),*res;
1965
 
 
1966
 
      res=args[0]->val_str(&tmp);
1967
 
      if (args[0]->null_value)
1968
 
      {
 
1908
  {
 
1909
    char buff[MAX_FIELD_WIDTH];
 
1910
    String tmp(buff,sizeof(buff),result_field->charset()),*res;
 
1911
 
 
1912
    res=args[0]->val_str(&tmp);
 
1913
    if (args[0]->null_value)
 
1914
    {
 
1915
      result_field->set_null();
 
1916
      result_field->reset();
 
1917
    }
 
1918
    else
 
1919
    {
 
1920
      result_field->set_notnull();
 
1921
      result_field->store(res->ptr(),res->length(),tmp.charset());
 
1922
    }
 
1923
    break;
 
1924
  }
 
1925
  case INT_RESULT:
 
1926
  {
 
1927
    int64_t nr=args[0]->val_int();
 
1928
 
 
1929
    if (maybe_null)
 
1930
    {
 
1931
      if (args[0]->null_value)
 
1932
      {
 
1933
        nr=0;
 
1934
        result_field->set_null();
 
1935
      }
 
1936
      else
 
1937
        result_field->set_notnull();
 
1938
    }
 
1939
    result_field->store(nr, unsigned_flag);
 
1940
    break;
 
1941
  }
 
1942
  case REAL_RESULT:
 
1943
  {
 
1944
    double nr= args[0]->val_real();
 
1945
 
 
1946
    if (maybe_null)
 
1947
    {
 
1948
      if (args[0]->null_value)
 
1949
      {
 
1950
        nr=0.0;
 
1951
        result_field->set_null();
 
1952
      }
 
1953
      else
 
1954
        result_field->set_notnull();
 
1955
    }
 
1956
    result_field->store(nr);
 
1957
    break;
 
1958
  }
 
1959
  case DECIMAL_RESULT:
 
1960
  {
 
1961
    my_decimal value_buff, *arg_dec= args[0]->val_decimal(&value_buff);
 
1962
 
 
1963
    if (maybe_null)
 
1964
    {
 
1965
      if (args[0]->null_value)
1969
1966
        result_field->set_null();
1970
 
        result_field->reset();
1971
 
      }
1972
1967
      else
1973
 
      {
1974
1968
        result_field->set_notnull();
1975
 
        result_field->store(res->ptr(),res->length(),tmp.charset());
1976
 
      }
1977
 
      break;
1978
 
    }
1979
 
  case INT_RESULT:
1980
 
    {
1981
 
      int64_t nr=args[0]->val_int();
1982
 
 
1983
 
      if (maybe_null)
1984
 
      {
1985
 
        if (args[0]->null_value)
1986
 
        {
1987
 
          nr=0;
1988
 
          result_field->set_null();
1989
 
        }
1990
 
        else
1991
 
          result_field->set_notnull();
1992
 
      }
1993
 
      result_field->store(nr, unsigned_flag);
1994
 
      break;
1995
 
    }
1996
 
  case REAL_RESULT:
1997
 
    {
1998
 
      double nr= args[0]->val_real();
1999
 
 
2000
 
      if (maybe_null)
2001
 
      {
2002
 
        if (args[0]->null_value)
2003
 
        {
2004
 
          nr=0.0;
2005
 
          result_field->set_null();
2006
 
        }
2007
 
        else
2008
 
          result_field->set_notnull();
2009
 
      }
2010
 
      result_field->store(nr);
2011
 
      break;
2012
 
    }
2013
 
  case DECIMAL_RESULT:
2014
 
    {
2015
 
      type::Decimal value_buff, *arg_dec= args[0]->val_decimal(&value_buff);
2016
 
 
2017
 
      if (maybe_null)
2018
 
      {
2019
 
        if (args[0]->null_value)
2020
 
          result_field->set_null();
2021
 
        else
2022
 
          result_field->set_notnull();
2023
 
      }
2024
 
      /*
2025
 
        We must store zero in the field as we will use the field value in
2026
 
        add()
2027
 
      */
2028
 
      if (!arg_dec)                               // Null
2029
 
        arg_dec= &decimal_zero;
2030
 
      result_field->store_decimal(arg_dec);
2031
 
      break;
2032
 
    }
 
1969
    }
 
1970
    /*
 
1971
      We must store zero in the field as we will use the field value in
 
1972
      add()
 
1973
    */
 
1974
    if (!arg_dec)                               // Null
 
1975
      arg_dec= &decimal_zero;
 
1976
    result_field->store_decimal(arg_dec);
 
1977
    break;
 
1978
  }
2033
1979
  case ROW_RESULT:
 
1980
  default:
2034
1981
    assert(0);
2035
1982
  }
2036
1983
}
2040
1987
{
2041
1988
  if (hybrid_type == DECIMAL_RESULT)
2042
1989
  {
2043
 
    type::Decimal value, *arg_val= args[0]->val_decimal(&value);
 
1990
    my_decimal value, *arg_val= args[0]->val_decimal(&value);
2044
1991
    if (!arg_val)                               // Null
2045
1992
      arg_val= &decimal_zero;
2046
1993
    result_field->store_decimal(arg_val);
2075
2022
  if (hybrid_type == DECIMAL_RESULT)
2076
2023
  {
2077
2024
    int64_t tmp;
2078
 
    type::Decimal value, *arg_dec= args[0]->val_decimal(&value);
 
2025
    my_decimal value, *arg_dec= args[0]->val_decimal(&value);
2079
2026
    if (args[0]->null_value)
2080
2027
    {
2081
2028
      arg_dec= &decimal_zero;
2083
2030
    }
2084
2031
    else
2085
2032
      tmp= 1;
2086
 
    arg_dec->val_binary(E_DEC_FATAL_ERROR, res, f_precision, f_scale);
 
2033
    my_decimal2binary(E_DEC_FATAL_ERROR, arg_dec, res, f_precision, f_scale);
2087
2034
    res+= dec_bin_size;
2088
2035
    int8store(res, tmp);
2089
2036
  }
2127
2074
{
2128
2075
  if (hybrid_type == DECIMAL_RESULT)
2129
2076
  {
2130
 
    type::Decimal value, *arg_val= args[0]->val_decimal(&value);
 
2077
    my_decimal value, *arg_val= args[0]->val_decimal(&value);
2131
2078
    if (!args[0]->null_value)
2132
2079
    {
2133
2080
      if (!result_field->is_null())
2134
2081
      {
2135
 
        type::Decimal field_value,
 
2082
        my_decimal field_value,
2136
2083
                   *field_val= result_field->val_decimal(&field_value);
2137
 
        class_decimal_add(E_DEC_FATAL_ERROR, dec_buffs, arg_val, field_val);
 
2084
        my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs, arg_val, field_val);
2138
2085
        result_field->store_decimal(dec_buffs);
2139
2086
      }
2140
2087
      else
2179
2126
  unsigned char *res=result_field->ptr;
2180
2127
  if (hybrid_type == DECIMAL_RESULT)
2181
2128
  {
2182
 
    type::Decimal value, *arg_val= args[0]->val_decimal(&value);
 
2129
    my_decimal value, *arg_val= args[0]->val_decimal(&value);
2183
2130
    if (!args[0]->null_value)
2184
2131
    {
2185
 
      binary2_class_decimal(E_DEC_FATAL_ERROR, res,
 
2132
      binary2my_decimal(E_DEC_FATAL_ERROR, res,
2186
2133
                        dec_buffs + 1, f_precision, f_scale);
2187
2134
      field_count= sint8korr(res + dec_bin_size);
2188
 
      class_decimal_add(E_DEC_FATAL_ERROR, dec_buffs, arg_val, dec_buffs + 1);
2189
 
      dec_buffs->val_binary(E_DEC_FATAL_ERROR, res, f_precision, f_scale);
 
2135
      my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs, arg_val, dec_buffs + 1);
 
2136
      my_decimal2binary(E_DEC_FATAL_ERROR, dec_buffs,
 
2137
                        res, f_precision, f_scale);
2190
2138
      res+= dec_bin_size;
2191
2139
      field_count++;
2192
2140
      int8store(res, field_count);
2224
2172
  case DECIMAL_RESULT:
2225
2173
    min_max_update_decimal_field();
2226
2174
    break;
2227
 
  case REAL_RESULT:
2228
 
  case ROW_RESULT:
 
2175
  default:
2229
2176
    min_max_update_real_field();
2230
2177
  }
2231
2178
}
2238
2185
 
2239
2186
  if (!args[0]->null_value)
2240
2187
  {
2241
 
    result_field->val_str_internal(&tmp_value);
 
2188
    result_field->val_str(&tmp_value);
2242
2189
 
2243
2190
    if (result_field->is_null() ||
2244
2191
        (cmp_sign * sortcmp(res_str,&tmp_value,collation.collation)) < 0)
2304
2251
Item_sum_hybrid::min_max_update_decimal_field()
2305
2252
{
2306
2253
  /* TODO: optimize: do not get result_field in case of args[0] is NULL */
2307
 
  type::Decimal old_val, nr_val;
2308
 
  const type::Decimal *old_nr= result_field->val_decimal(&old_val);
2309
 
  const type::Decimal *nr= args[0]->val_decimal(&nr_val);
 
2254
  my_decimal old_val, nr_val;
 
2255
  const my_decimal *old_nr= result_field->val_decimal(&old_val);
 
2256
  const my_decimal *nr= args[0]->val_decimal(&nr_val);
2310
2257
  if (!args[0]->null_value)
2311
2258
  {
2312
2259
    if (result_field->is_null(0))
2313
2260
      old_nr=nr;
2314
2261
    else
2315
2262
    {
2316
 
      bool res= class_decimal_cmp(old_nr, nr) > 0;
 
2263
      bool res= my_decimal_cmp(old_nr, nr) > 0;
2317
2264
      /* (cmp_sign > 0 && res) || (!(cmp_sign > 0) && !res) */
2318
2265
      if ((cmp_sign > 0) ^ (!res))
2319
2266
        old_nr=nr;
2370
2317
}
2371
2318
 
2372
2319
 
2373
 
type::Decimal *Item_avg_field::val_decimal(type::Decimal *dec_buf)
 
2320
my_decimal *Item_avg_field::val_decimal(my_decimal *dec_buf)
2374
2321
{
2375
2322
  // fix_fields() never calls for this Item
2376
2323
  if (hybrid_type == REAL_RESULT)
2380
2327
  if ((null_value= !count))
2381
2328
    return 0;
2382
2329
 
2383
 
  type::Decimal dec_count, dec_field;
2384
 
  binary2_class_decimal(E_DEC_FATAL_ERROR,
 
2330
  my_decimal dec_count, dec_field;
 
2331
  binary2my_decimal(E_DEC_FATAL_ERROR,
2385
2332
                    field->ptr, &dec_field, f_precision, f_scale);
2386
 
  int2_class_decimal(E_DEC_FATAL_ERROR, count, 0, &dec_count);
2387
 
  class_decimal_div(E_DEC_FATAL_ERROR, dec_buf,
 
2333
  int2my_decimal(E_DEC_FATAL_ERROR, count, 0, &dec_count);
 
2334
  my_decimal_div(E_DEC_FATAL_ERROR, dec_buf,
2388
2335
                 &dec_field, &dec_count, prec_increment);
2389
2336
  return dec_buf;
2390
2337
}
2415
2362
}
2416
2363
 
2417
2364
 
2418
 
type::Decimal *Item_std_field::val_decimal(type::Decimal *dec_buf)
 
2365
my_decimal *Item_std_field::val_decimal(my_decimal *dec_buf)
2419
2366
{
2420
2367
  /*
2421
2368
    We can't call val_decimal_from_real() for DECIMAL_RESULT as
2422
2369
    Item_variance_field::val_real() would cause an infinite loop
2423
2370
  */
2424
 
  type::Decimal tmp_dec, *dec;
 
2371
  my_decimal tmp_dec, *dec;
2425
2372
  double nr;
2426
2373
  if (hybrid_type == REAL_RESULT)
2427
2374
    return val_decimal_from_real(dec_buf);
2429
2376
  dec= Item_variance_field::val_decimal(dec_buf);
2430
2377
  if (!dec)
2431
2378
    return 0;
2432
 
  class_decimal2double(E_DEC_FATAL_ERROR, dec, &nr);
 
2379
  my_decimal2double(E_DEC_FATAL_ERROR, dec, &nr);
2433
2380
  assert(nr >= 0.0);
2434
2381
  nr= sqrt(nr);
2435
 
  double2_class_decimal(E_DEC_FATAL_ERROR, nr, &tmp_dec);
2436
 
  class_decimal_round(E_DEC_FATAL_ERROR, &tmp_dec, decimals, false, dec_buf);
 
2382
  double2my_decimal(E_DEC_FATAL_ERROR, nr, &tmp_dec);
 
2383
  my_decimal_round(E_DEC_FATAL_ERROR, &tmp_dec, decimals, false, dec_buf);
2437
2384
  return dec_buf;
2438
2385
}
2439
2386
 
2460
2407
}
2461
2408
 
2462
2409
 
2463
 
int64_t Item_variance_field::val_int()
2464
 
{
2465
 
  /* can't be fix_fields()ed */
2466
 
  return (int64_t) rint(val_real());
2467
 
}
2468
 
 
2469
 
 
2470
2410
double Item_variance_field::val_real()
2471
2411
{
2472
2412
  // fix_fields() never calls for this Item
2505
2445
int composite_key_cmp(void* arg, unsigned char* key1, unsigned char* key2)
2506
2446
{
2507
2447
  Item_sum_count_distinct* item = (Item_sum_count_distinct*)arg;
2508
 
  Field **field    = item->table->getFields();
2509
 
  Field **field_end= field + item->table->getShare()->sizeFields();
 
2448
  Field **field    = item->table->field;
 
2449
  Field **field_end= field + item->table->s->fields;
2510
2450
  uint32_t *lengths=item->field_lengths;
2511
2451
  for (; field < field_end; ++field)
2512
2452
  {
2521
2461
  return 0;
2522
2462
}
2523
2463
 
2524
 
static int count_distinct_walk(void *,
2525
 
                               uint32_t ,
 
2464
#ifdef __cplusplus
 
2465
extern "C" {
 
2466
#endif
 
2467
 
 
2468
static int count_distinct_walk(void *elem __attribute__((unused)),
 
2469
                               element_count count __attribute__((unused)),
2526
2470
                               void *arg)
2527
2471
{
2528
2472
  (*((uint64_t*)arg))++;
2529
2473
  return 0;
2530
2474
}
2531
2475
 
 
2476
#ifdef __cplusplus
 
2477
}
 
2478
#endif
 
2479
 
 
2480
 
 
2481
 
2532
2482
void Item_sum_count_distinct::cleanup()
2533
2483
{
2534
2484
  Item_sum_int::cleanup();
2546
2496
    is_evaluated= false;
2547
2497
    if (table)
2548
2498
    {
 
2499
      table->free_tmp_table(table->in_use);
2549
2500
      table= 0;
2550
2501
    }
2551
2502
    delete tmp_table_param;
2579
2530
}
2580
2531
 
2581
2532
 
2582
 
bool Item_sum_count_distinct::setup(Session *session)
 
2533
bool Item_sum_count_distinct::setup(THD *thd)
2583
2534
{
2584
2535
  List<Item> list;
2585
 
  Select_Lex *select_lex= session->lex->current_select;
 
2536
  SELECT_LEX *select_lex= thd->lex->current_select;
2586
2537
 
2587
2538
  /*
2588
2539
    Setup can be called twice for ROLLUP items. This is a bug.
2592
2543
  if (tree || table || tmp_table_param)
2593
2544
    return false;
2594
2545
 
2595
 
  if (!(tmp_table_param= new Tmp_Table_Param))
 
2546
  if (!(tmp_table_param= new TMP_TABLE_PARAM))
2596
2547
    return true;
2597
2548
 
2598
2549
  /* Create a table with an unique key over all parameters */
2610
2561
  tmp_table_param->force_copy_fields= force_copy_fields;
2611
2562
  assert(table == 0);
2612
2563
 
2613
 
  if (!(table= create_tmp_table(session, tmp_table_param, list, (Order*) 0, 1,
 
2564
  if (!(table= create_tmp_table(thd, tmp_table_param, list, (order_st*) 0, 1,
2614
2565
                                0,
2615
 
                                (select_lex->options | session->options),
 
2566
                                (select_lex->options | thd->options),
2616
2567
                                HA_POS_ERROR, (char*)"")))
2617
 
  {
2618
2568
    return true;
2619
 
  }
2620
 
  table->cursor->extra(HA_EXTRA_NO_ROWS);               // Don't update rows
 
2569
  table->file->extra(HA_EXTRA_NO_ROWS);         // Don't update rows
2621
2570
  table->no_rows=1;
2622
2571
 
2623
 
  if (table->getShare()->db_type() == heap_engine)
 
2572
  if (table->s->db_type() == heap_hton)
2624
2573
  {
2625
2574
    /*
2626
2575
      No blobs, otherwise it would have been MyISAM: set up a compare
2628
2577
    */
2629
2578
    qsort_cmp2 compare_key;
2630
2579
    void* cmp_arg;
2631
 
    Field **field= table->getFields();
2632
 
    Field **field_end= field + table->getShare()->sizeFields();
 
2580
    Field **field= table->field;
 
2581
    Field **field_end= field + table->s->fields;
2633
2582
    bool all_binary= true;
2634
2583
 
2635
2584
    for (tree_key_length= 0; field < field_end; ++field)
2650
2599
    }
2651
2600
    else
2652
2601
    {
2653
 
      if (table->getShare()->sizeFields() == 1)
 
2602
      if (table->s->fields == 1)
2654
2603
      {
2655
2604
        /*
2656
2605
          If we have only one field, which is the most common use of
2659
2608
          about other fields.
2660
2609
        */
2661
2610
        compare_key= (qsort_cmp2) simple_str_key_cmp;
2662
 
        cmp_arg= (void*) table->getField(0);
 
2611
        cmp_arg= (void*) table->field[0];
2663
2612
        /* tree_key_length has been set already */
2664
2613
      }
2665
2614
      else
2667
2616
        uint32_t *length;
2668
2617
        compare_key= (qsort_cmp2) composite_key_cmp;
2669
2618
        cmp_arg= (void*) this;
2670
 
        field_lengths= (uint32_t*) session->getMemRoot()->allocate(table->getShare()->sizeFields() * sizeof(uint32_t));
2671
 
        for (tree_key_length= 0, length= field_lengths, field= table->getFields();
 
2619
        field_lengths= (uint32_t*) thd->alloc(table->s->fields * sizeof(uint32_t));
 
2620
        for (tree_key_length= 0, length= field_lengths, field= table->field;
2672
2621
             field < field_end; ++field, ++length)
2673
2622
        {
2674
2623
          *length= (*field)->pack_length();
2678
2627
    }
2679
2628
    assert(tree == 0);
2680
2629
    tree= new Unique(compare_key, cmp_arg, tree_key_length,
2681
 
                     (size_t)session->variables.max_heap_table_size);
 
2630
                     thd->variables.max_heap_table_size);
2682
2631
    /*
2683
2632
      The only time tree_key_length could be 0 is if someone does
2684
2633
      count(distinct) on a char(0) field - stupid thing to do,
2693
2642
}
2694
2643
 
2695
2644
 
2696
 
Item *Item_sum_count_distinct::copy_or_same(Session* session)
 
2645
Item *Item_sum_count_distinct::copy_or_same(THD* thd) 
2697
2646
{
2698
 
  return new (session->mem_root) Item_sum_count_distinct(session, this);
 
2647
  return new (thd->mem_root) Item_sum_count_distinct(thd, this);
2699
2648
}
2700
2649
 
2701
2650
 
2709
2658
  }
2710
2659
  else if (table)
2711
2660
  {
2712
 
    table->cursor->extra(HA_EXTRA_NO_CACHE);
2713
 
    table->cursor->ha_delete_all_rows();
2714
 
    table->cursor->extra(HA_EXTRA_WRITE_CACHE);
 
2661
    table->file->extra(HA_EXTRA_NO_CACHE);
 
2662
    table->file->ha_delete_all_rows();
 
2663
    table->file->extra(HA_EXTRA_WRITE_CACHE);
2715
2664
  }
2716
2665
}
2717
2666
 
2721
2670
  if (always_null)
2722
2671
    return 0;
2723
2672
  copy_fields(tmp_table_param);
2724
 
  if (copy_funcs(tmp_table_param->items_to_copy, table->in_use))
2725
 
    return true;
 
2673
  copy_funcs(tmp_table_param->items_to_copy);
2726
2674
 
2727
 
  for (Field **field= table->getFields() ; *field ; field++)
2728
 
  {
 
2675
  for (Field **field=table->field ; *field ; field++)
2729
2676
    if ((*field)->is_real_null(0))
2730
 
    {
2731
2677
      return 0;                                 // Don't count NULL
2732
 
    }
2733
 
  }
2734
2678
 
2735
2679
  is_evaluated= false;
2736
2680
  if (tree)
2741
2685
      bloat the tree without providing any valuable info. Besides,
2742
2686
      key_length used to initialize the tree didn't include space for them.
2743
2687
    */
2744
 
    return tree->unique_add(table->record[0] + table->getShare()->null_bytes);
 
2688
    return tree->unique_add(table->record[0] + table->s->null_bytes);
2745
2689
  }
2746
 
  if ((error= table->cursor->insertRecord(table->record[0])) &&
2747
 
      table->cursor->is_fatal_error(error, HA_CHECK_DUP))
 
2690
  if ((error= table->file->ha_write_row(table->record[0])) &&
 
2691
      table->file->is_fatal_error(error, HA_CHECK_DUP))
2748
2692
    return true;
2749
2693
  return false;
2750
2694
}
2769
2713
    return (int64_t) count;
2770
2714
  }
2771
2715
 
2772
 
  error= table->cursor->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
 
2716
  error= table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
2773
2717
 
2774
2718
  if(error)
2775
2719
  {
2776
 
    table->print_error(error, MYF(0));
 
2720
    table->file->print_error(error, MYF(0));
2777
2721
  }
2778
2722
 
2779
 
  return table->cursor->stats.records;
 
2723
  return table->file->stats.records;
2780
2724
}
2781
2725
 
2782
2726
/*****************************************************************************
2793
2737
*****************************************************************************/
2794
2738
 
2795
2739
 
2796
 
/**
 
2740
/** 
2797
2741
  Compares the values for fields in expr list of GROUP_CONCAT.
2798
2742
  @note
2799
 
 
 
2743
       
2800
2744
     GROUP_CONCAT([DISTINCT] expr [,expr ...]
2801
2745
              [order_st BY {unsigned_integer | col_name | expr}
2802
2746
                  [ASC | DESC] [,col_name ...]]
2803
2747
              [SEPARATOR str_val])
2804
 
 
 
2748
 
2805
2749
  @return
2806
 
  @retval -1 : key1 < key2
 
2750
  @retval -1 : key1 < key2 
2807
2751
  @retval  0 : key1 = key2
2808
 
  @retval  1 : key1 > key2
 
2752
  @retval  1 : key1 > key2 
2809
2753
*/
2810
2754
 
2811
 
int group_concat_key_cmp_with_distinct(void* arg, const void* key1,
 
2755
int group_concat_key_cmp_with_distinct(void* arg, const void* key1, 
2812
2756
                                       const void* key2)
2813
2757
{
2814
2758
  Item_func_group_concat *item_func= (Item_func_group_concat*)arg;
2817
2761
  for (uint32_t i= 0; i < item_func->arg_count_field; i++)
2818
2762
  {
2819
2763
    Item *item= item_func->args[i];
2820
 
    /*
 
2764
    /* 
2821
2765
      If field_item is a const item then either get_tp_table_field returns 0
2822
 
      or it is an item over a const table.
 
2766
      or it is an item over a const table. 
2823
2767
    */
2824
2768
    if (item->const_item())
2825
2769
      continue;
2830
2774
    */
2831
2775
    Field *field= item->get_tmp_table_field();
2832
2776
    int res;
2833
 
    uint32_t offset= field->offset(field->getTable()->record[0])-table->getShare()->null_bytes;
 
2777
    uint32_t offset= field->offset(field->table->record[0])-table->s->null_bytes;
2834
2778
    if((res= field->cmp((unsigned char*)key1 + offset, (unsigned char*)key2 + offset)))
2835
2779
      return res;
2836
2780
  }
2839
2783
 
2840
2784
 
2841
2785
/**
2842
 
  function of sort for syntax: GROUP_CONCAT(expr,... ORDER BY col,... )
 
2786
  function of sort for syntax: GROUP_CONCAT(expr,... order_st BY col,... )
2843
2787
*/
2844
2788
 
2845
 
int group_concat_key_cmp_with_order(void* arg, const void* key1,
 
2789
int group_concat_key_cmp_with_order(void* arg, const void* key1, 
2846
2790
                                    const void* key2)
2847
2791
{
2848
2792
  Item_func_group_concat* grp_item= (Item_func_group_concat*) arg;
2849
 
  Order **order_item, **end;
 
2793
  order_st **order_item, **end;
2850
2794
  Table *table= grp_item->table;
2851
2795
 
2852
2796
  for (order_item= grp_item->order, end=order_item+ grp_item->arg_count_order;
2860
2804
      the temporary table, not the original field
2861
2805
    */
2862
2806
    Field *field= item->get_tmp_table_field();
2863
 
    /*
 
2807
    /* 
2864
2808
      If item is a const item then either get_tp_table_field returns 0
2865
 
      or it is an item over a const table.
 
2809
      or it is an item over a const table. 
2866
2810
    */
2867
2811
    if (field && !item->const_item())
2868
2812
    {
2869
2813
      int res;
2870
 
      uint32_t offset= (field->offset(field->getTable()->record[0]) -
2871
 
                    table->getShare()->null_bytes);
 
2814
      uint32_t offset= (field->offset(field->table->record[0]) -
 
2815
                    table->s->null_bytes);
2872
2816
      if ((res= field->cmp((unsigned char*)key1 + offset, (unsigned char*)key2 + offset)))
2873
2817
        return (*order_item)->asc ? res : -res;
2874
2818
    }
2886
2830
  Append data from current leaf to item->result.
2887
2831
*/
2888
2832
 
2889
 
int dump_leaf_key(unsigned char* key, uint32_t ,
 
2833
int dump_leaf_key(unsigned char* key, element_count count __attribute__((unused)),
2890
2834
                  Item_func_group_concat *item)
2891
2835
{
2892
2836
  Table *table= item->table;
2893
 
  String tmp((char *)table->getUpdateRecord(), table->getShare()->getRecordLength(),
 
2837
  String tmp((char *)table->record[1], table->s->reclength,
2894
2838
             default_charset_info);
2895
2839
  String tmp2;
2896
2840
  String *result= &item->result;
2917
2861
        because it contains both order and arg list fields.
2918
2862
      */
2919
2863
      Field *field= (*arg)->get_tmp_table_field();
2920
 
      uint32_t offset= (field->offset(field->getTable()->record[0]) -
2921
 
                    table->getShare()->null_bytes);
2922
 
      assert(offset < table->getShare()->getRecordLength());
2923
 
      res= field->val_str_internal(&tmp, key + offset);
 
2864
      uint32_t offset= (field->offset(field->table->record[0]) -
 
2865
                    table->s->null_bytes);
 
2866
      assert(offset < table->s->reclength);
 
2867
      res= field->val_str(&tmp, key + offset);
2924
2868
    }
2925
2869
    else
2926
2870
      res= (*arg)->val_str(&tmp);
2968
2912
                       bool distinct_arg, List<Item> *select_list,
2969
2913
                       SQL_LIST *order_list, String *separator_arg)
2970
2914
  :tmp_table_param(0), warning(0),
2971
 
   separator(separator_arg), tree(NULL), unique_filter(NULL), table(0),
 
2915
   separator(separator_arg), tree(0), unique_filter(NULL), table(0),
2972
2916
   order(0), context(context_arg),
2973
2917
   arg_count_order(order_list ? order_list->elements : 0),
2974
2918
   arg_count_field(select_list->elements),
2989
2933
           (for possible order items in temporare tables)
2990
2934
    order - arg_count_order
2991
2935
  */
2992
 
  if (!(args= (Item**) memory::sql_alloc(sizeof(Item*) * arg_count +
2993
 
                                 sizeof(Order*)*arg_count_order)))
 
2936
  if (!(args= (Item**) sql_alloc(sizeof(Item*) * arg_count +
 
2937
                                 sizeof(order_st*)*arg_count_order)))
2994
2938
    return;
2995
2939
 
2996
 
  order= (Order**)(args + arg_count);
 
2940
  order= (order_st**)(args + arg_count);
2997
2941
 
2998
2942
  /* fill args items of show and sort */
2999
2943
  List_iterator_fast<Item> li(*select_list);
3003
2947
 
3004
2948
  if (arg_count_order)
3005
2949
  {
3006
 
    Order **order_ptr= order;
3007
 
    for (Order *order_item= (Order*) order_list->first;
 
2950
    order_st **order_ptr= order;
 
2951
    for (order_st *order_item= (order_st*) order_list->first;
3008
2952
         order_item != NULL;
3009
2953
         order_item= order_item->next)
3010
2954
    {
3016
2960
}
3017
2961
 
3018
2962
 
3019
 
Item_func_group_concat::Item_func_group_concat(Session *session,
 
2963
Item_func_group_concat::Item_func_group_concat(THD *thd,
3020
2964
                                               Item_func_group_concat *item)
3021
 
  :Item_sum(session, item),
 
2965
  :Item_sum(thd, item),
3022
2966
  tmp_table_param(item->tmp_table_param),
3023
2967
  warning(item->warning),
3024
2968
  separator(item->separator),
3050
2994
  if (warning)
3051
2995
  {
3052
2996
    char warn_buff[DRIZZLE_ERRMSG_SIZE];
3053
 
    snprintf(warn_buff, sizeof(warn_buff), ER(ER_CUT_VALUE_GROUP_CONCAT), count_cut_values);
3054
 
    warning->set_msg(&getSession(), warn_buff);
 
2997
    sprintf(warn_buff, ER(ER_CUT_VALUE_GROUP_CONCAT), count_cut_values);
 
2998
    warning->set_msg(current_thd, warn_buff);
3055
2999
    warning= 0;
3056
3000
  }
3057
3001
 
3065
3009
    tmp_table_param= 0;
3066
3010
    if (table)
3067
3011
    {
3068
 
      Session *session= table->in_use;
 
3012
      THD *thd= table->in_use;
 
3013
      table->free_tmp_table(thd);
3069
3014
      table= 0;
3070
3015
      if (tree)
3071
3016
      {
3080
3025
      if (warning)
3081
3026
      {
3082
3027
        char warn_buff[DRIZZLE_ERRMSG_SIZE];
3083
 
        snprintf(warn_buff, sizeof(warn_buff), ER(ER_CUT_VALUE_GROUP_CONCAT), count_cut_values);
3084
 
        warning->set_msg(session, warn_buff);
 
3028
        sprintf(warn_buff, ER(ER_CUT_VALUE_GROUP_CONCAT), count_cut_values);
 
3029
        warning->set_msg(thd, warn_buff);
3085
3030
        warning= 0;
3086
3031
      }
3087
3032
    }
3091
3036
}
3092
3037
 
3093
3038
 
3094
 
Item *Item_func_group_concat::copy_or_same(Session* session)
 
3039
Item *Item_func_group_concat::copy_or_same(THD* thd)
3095
3040
{
3096
 
  return new (session->mem_root) Item_func_group_concat(session, this);
 
3041
  return new (thd->mem_root) Item_func_group_concat(thd, this);
3097
3042
}
3098
3043
 
3099
3044
 
3117
3062
  if (always_null)
3118
3063
    return 0;
3119
3064
  copy_fields(tmp_table_param);
3120
 
  if (copy_funcs(tmp_table_param->items_to_copy, table->in_use))
3121
 
    return true;
 
3065
  copy_funcs(tmp_table_param->items_to_copy);
3122
3066
 
3123
3067
  for (uint32_t i= 0; i < arg_count_field; i++)
3124
3068
  {
3134
3078
  null_value= false;
3135
3079
  bool row_eligible= true;
3136
3080
 
3137
 
  if (distinct)
 
3081
  if (distinct) 
3138
3082
  {
3139
3083
    /* Filter out duplicate rows. */
3140
3084
    uint32_t count= unique_filter->elements_in_tree();
3141
 
    unique_filter->unique_add(table->record[0] + table->getShare()->null_bytes);
 
3085
    unique_filter->unique_add(table->record[0] + table->s->null_bytes);
3142
3086
    if (count == unique_filter->elements_in_tree())
3143
3087
      row_eligible= false;
3144
3088
  }
3145
3089
 
3146
3090
  TREE_ELEMENT *el= 0;                          // Only for safety
3147
3091
  if (row_eligible && tree)
3148
 
    el= tree_insert(tree, table->record[0] + table->getShare()->null_bytes, 0,
 
3092
    el= tree_insert(tree, table->record[0] + table->s->null_bytes, 0,
3149
3093
                    tree->custom_arg);
3150
3094
  /*
3151
3095
    If the row is not a duplicate (el->count == 1)
3154
3098
  */
3155
3099
  if (row_eligible && !warning_for_row &&
3156
3100
      (!tree || (el->count == 1 && distinct && !arg_count_order)))
3157
 
    dump_leaf_key(table->record[0] + table->getShare()->null_bytes, 1, this);
 
3101
    dump_leaf_key(table->record[0] + table->s->null_bytes, 1, this);
3158
3102
 
3159
3103
  return 0;
3160
3104
}
3161
3105
 
3162
3106
 
3163
3107
bool
3164
 
Item_func_group_concat::fix_fields(Session *session, Item **ref)
 
3108
Item_func_group_concat::fix_fields(THD *thd, Item **ref)
3165
3109
{
3166
3110
  uint32_t i;                       /* for loop variable */
3167
3111
  assert(fixed == 0);
3168
3112
 
3169
 
  if (init_sum_func_check(session))
 
3113
  if (init_sum_func_check(thd))
3170
3114
    return true;
3171
3115
 
3172
3116
  maybe_null= 1;
3178
3122
  for (i=0 ; i < arg_count ; i++)
3179
3123
  {
3180
3124
    if ((!args[i]->fixed &&
3181
 
         args[i]->fix_fields(session, args + i)) ||
 
3125
         args[i]->fix_fields(thd, args + i)) ||
3182
3126
        args[i]->check_cols(1))
3183
3127
      return true;
3184
3128
  }
3193
3137
  result.set_charset(collation.collation);
3194
3138
  result_field= 0;
3195
3139
  null_value= 1;
3196
 
  max_length= (size_t)session->variables.group_concat_max_len;
3197
 
 
3198
 
  if (check_sum_func(session, ref))
 
3140
  max_length= thd->variables.group_concat_max_len;
 
3141
 
 
3142
  uint32_t offset;
 
3143
  if (separator->needs_conversion(separator->length(), separator->charset(),
 
3144
                                  collation.collation, &offset))
 
3145
  {
 
3146
    uint32_t buflen= collation.collation->mbmaxlen * separator->length();
 
3147
    uint32_t errors, conv_length;
 
3148
    char *buf;
 
3149
    String *new_separator;
 
3150
 
 
3151
    if (!(buf= (char*) thd->alloc(buflen)) ||
 
3152
        !(new_separator= new(thd->mem_root)
 
3153
                           String(buf, buflen, collation.collation)))
 
3154
      return true;
 
3155
    
 
3156
    conv_length= copy_and_convert(buf, buflen, collation.collation,
 
3157
                                  separator->ptr(), separator->length(),
 
3158
                                  separator->charset(), &errors);
 
3159
    new_separator->length(conv_length);
 
3160
    separator= new_separator;
 
3161
  }
 
3162
 
 
3163
  if (check_sum_func(thd, ref))
3199
3164
    return true;
3200
3165
 
3201
3166
  fixed= 1;
3203
3168
}
3204
3169
 
3205
3170
 
3206
 
bool Item_func_group_concat::setup(Session *session)
 
3171
bool Item_func_group_concat::setup(THD *thd)
3207
3172
{
3208
3173
  List<Item> list;
3209
 
  Select_Lex *select_lex= session->lex->current_select;
 
3174
  SELECT_LEX *select_lex= thd->lex->current_select;
3210
3175
 
3211
3176
  /*
3212
3177
    Currently setup() can be called twice. Please add
3215
3180
  if (table || tree)
3216
3181
    return(false);
3217
3182
 
3218
 
  if (!(tmp_table_param= new Tmp_Table_Param))
 
3183
  if (!(tmp_table_param= new TMP_TABLE_PARAM))
3219
3184
    return(true);
3220
3185
 
3221
3186
  /* We'll convert all blobs to varchar fields in the temporary table */
3246
3211
    tmp table columns.
3247
3212
  */
3248
3213
  if (arg_count_order &&
3249
 
      setup_order(session, args, context->table_list, list, all_fields, *order))
 
3214
      setup_order(thd, args, context->table_list, list, all_fields, *order))
3250
3215
    return(true);
3251
3216
 
3252
3217
  count_field_types(select_lex, tmp_table_param, all_fields, 0);
3256
3221
  {
3257
3222
    /*
3258
3223
      Currently we have to force conversion of BLOB values to VARCHAR's
3259
 
      if we are to store them in TREE objects used for ORDER BY and
 
3224
      if we are to store them in TREE objects used for order_st BY and
3260
3225
      DISTINCT. This leads to truncation if the BLOB's size exceeds
3261
3226
      Field_varstring::MAX_SIZE.
3262
3227
    */
3263
 
    set_if_smaller(tmp_table_param->convert_blob_length,
 
3228
    set_if_smaller(tmp_table_param->convert_blob_length, 
3264
3229
                   Field_varstring::MAX_SIZE);
3265
3230
  }
3266
3231
 
3268
3233
    We have to create a temporary table to get descriptions of fields
3269
3234
    (types, sizes and so on).
3270
3235
 
3271
 
    Note that in the table, we first have the ORDER BY fields, then the
 
3236
    Note that in the table, we first have the order_st BY fields, then the
3272
3237
    field list.
3273
3238
  */
3274
 
  if (!(table= create_tmp_table(session, tmp_table_param, all_fields,
3275
 
                                (Order*) 0, 0, true,
3276
 
                                (select_lex->options | session->options),
 
3239
  if (!(table= create_tmp_table(thd, tmp_table_param, all_fields,
 
3240
                                (order_st*) 0, 0, true,
 
3241
                                (select_lex->options | thd->options),
3277
3242
                                HA_POS_ERROR, (char*) "")))
3278
 
  {
3279
3243
    return(true);
3280
 
  }
3281
 
 
3282
 
  table->cursor->extra(HA_EXTRA_NO_ROWS);
 
3244
  table->file->extra(HA_EXTRA_NO_ROWS);
3283
3245
  table->no_rows= 1;
3284
3246
 
3285
3247
  /*
3287
3249
     Don't reserve space for NULLs: if any of gconcat arguments is NULL,
3288
3250
     the row is not added to the result.
3289
3251
  */
3290
 
  uint32_t tree_key_length= table->getShare()->getRecordLength() - table->getShare()->null_bytes;
 
3252
  uint32_t tree_key_length= table->s->reclength - table->s->null_bytes;
3291
3253
 
3292
3254
  if (arg_count_order)
3293
3255
  {
3294
3256
    tree= &tree_base;
3295
3257
    /*
3296
3258
      Create a tree for sorting. The tree is used to sort (according to the
3297
 
      syntax of this function). If there is no ORDER BY clause, we don't
 
3259
      syntax of this function). If there is no order_st BY clause, we don't
3298
3260
      create this tree.
3299
3261
    */
3300
 
    init_tree(tree, (uint32_t) min(session->variables.max_heap_table_size,
3301
 
                                   (uint64_t)(session->variables.sortbuff_size/16)), 
3302
 
              0,
3303
 
              tree_key_length,
3304
 
              group_concat_key_cmp_with_order , false, NULL, (void*) this);
 
3262
    init_tree(tree, (uint) cmin(thd->variables.max_heap_table_size,
 
3263
                               thd->variables.sortbuff_size/16), 0,
 
3264
              tree_key_length, 
 
3265
              group_concat_key_cmp_with_order , 0, NULL, (void*) this);
3305
3266
  }
3306
3267
 
3307
3268
  if (distinct)
3308
3269
    unique_filter= new Unique(group_concat_key_cmp_with_distinct,
3309
3270
                              (void*)this,
3310
3271
                              tree_key_length,
3311
 
                              (size_t)session->variables.max_heap_table_size);
3312
 
 
 
3272
                              thd->variables.max_heap_table_size);
 
3273
  
3313
3274
  return(false);
3314
3275
}
3315
3276
 
3325
3286
  tree= 0;
3326
3287
}
3327
3288
 
3328
 
double Item_func_group_concat::val_real()
3329
 
{
3330
 
  String *res;  res=val_str(&str_value);
3331
 
  return res ? internal::my_atof(res->c_ptr()) : 0.0;
3332
 
}
3333
 
 
3334
 
int64_t Item_func_group_concat::val_int()
3335
 
{
3336
 
  String *res;
3337
 
  char *end_ptr;
3338
 
  int error;
3339
 
  if (!(res= val_str(&str_value)))
3340
 
    return (int64_t) 0;
3341
 
  end_ptr= (char*) res->ptr()+ res->length();
3342
 
  return internal::my_strtoll10(res->ptr(), &end_ptr, &error);
3343
 
}
3344
 
 
3345
 
String* Item_func_group_concat::val_str(String* )
 
3289
 
 
3290
String* Item_func_group_concat::val_str(String* str __attribute__((unused)))
3346
3291
{
3347
3292
  assert(fixed == 1);
3348
3293
  if (null_value)
3400
3345
Item_func_group_concat::~Item_func_group_concat()
3401
3346
{
3402
3347
  if (!original && unique_filter)
3403
 
    delete unique_filter;
 
3348
    delete unique_filter;    
3404
3349
}
3405
 
 
3406
 
} /* namespace drizzled */