~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/function/func.cc

  • Committer: Olaf van der Spek
  • Date: 2011-03-28 14:32:36 UTC
  • mto: (2257.1.1 build) (2276.1.2 build)
  • mto: This revision was merged to the branch mainline in revision 2258.
  • Revision ID: olafvdspek@gmail.com-20110328143236-4ge1d793iqaktfq0
Common fwd

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
18
 */
19
19
 
20
 
#include "config.h"
21
 
 
22
 
#include <drizzled/sql_string.h>
23
 
#include <drizzled/sql_list.h>
24
 
 
25
 
#include <drizzled/function/math/int.h>
 
20
#include <config.h>
 
21
 
 
22
#include <drizzled/check_stack_overrun.h>
 
23
#include <drizzled/current_session.h>
 
24
#include <drizzled/error.h>
 
25
#include <drizzled/field/decimal.h>
 
26
#include <drizzled/field/double.h>
26
27
#include <drizzled/field/int32.h>
27
28
#include <drizzled/field/int64.h>
28
 
#include <drizzled/field/double.h>
29
 
#include <drizzled/field/decimal.h>
 
29
#include <drizzled/field/size.h>
 
30
#include <drizzled/function/math/int.h>
 
31
#include <drizzled/item/field.h>
30
32
#include <drizzled/session.h>
31
 
#include <drizzled/error.h>
32
 
#include <drizzled/check_stack_overrun.h>
 
33
#include <drizzled/sql_list.h>
 
34
#include <drizzled/sql_string.h>
 
35
 
33
36
#include <limits>
34
37
#include <algorithm>
35
38
 
39
42
{
40
43
 
41
44
 
 
45
Item_func::Item_func(void):
 
46
  _session(*current_session),
 
47
  allowed_arg_cols(1), arg_count(0),
 
48
  const_item_cache(false)
 
49
  {
 
50
    with_sum_func= 0;
 
51
    collation.set(DERIVATION_SYSCONST);
 
52
  }
 
53
 
 
54
Item_func::Item_func(Item *a):
 
55
  _session(*current_session),
 
56
  allowed_arg_cols(1), arg_count(1),
 
57
  const_item_cache(false)
 
58
  {
 
59
    args= tmp_arg;
 
60
    args[0]= a;
 
61
    with_sum_func= a->with_sum_func;
 
62
    collation.set(DERIVATION_SYSCONST);
 
63
  }
 
64
 
 
65
Item_func::Item_func(Item *a,Item *b):
 
66
  _session(*current_session),
 
67
  allowed_arg_cols(1), arg_count(2),
 
68
  const_item_cache(false)
 
69
  {
 
70
    args= tmp_arg;
 
71
    args[0]= a; args[1]= b;
 
72
    with_sum_func= a->with_sum_func || b->with_sum_func;
 
73
    collation.set(DERIVATION_SYSCONST);
 
74
  }
 
75
 
 
76
Item_func::Item_func(Item *a,Item *b,Item *c):
 
77
  _session(*current_session),
 
78
  allowed_arg_cols(1),
 
79
  const_item_cache(false)
 
80
  {
 
81
    arg_count= 0;
 
82
    if ((args= (Item**) memory::sql_alloc(sizeof(Item*)*3)))
 
83
    {
 
84
      arg_count= 3;
 
85
      args[0]= a; args[1]= b; args[2]= c;
 
86
      with_sum_func= a->with_sum_func || b->with_sum_func || c->with_sum_func;
 
87
    }
 
88
    collation.set(DERIVATION_SYSCONST);
 
89
  }
 
90
 
 
91
Item_func::Item_func(Item *a,Item *b,Item *c,Item *d):
 
92
  _session(*current_session),
 
93
  allowed_arg_cols(1),
 
94
  const_item_cache(false)
 
95
  {
 
96
    arg_count= 0;
 
97
    if ((args= (Item**) memory::sql_alloc(sizeof(Item*)*4)))
 
98
    {
 
99
      arg_count= 4;
 
100
      args[0]= a; args[1]= b; args[2]= c; args[3]= d;
 
101
      with_sum_func= a->with_sum_func || b->with_sum_func ||
 
102
        c->with_sum_func || d->with_sum_func;
 
103
    }
 
104
    collation.set(DERIVATION_SYSCONST);
 
105
  }
 
106
 
 
107
Item_func::Item_func(Item *a,Item *b,Item *c,Item *d,Item* e):
 
108
  _session(*current_session),
 
109
  allowed_arg_cols(1),
 
110
  const_item_cache(false)
 
111
  {
 
112
    arg_count= 5;
 
113
    if ((args= (Item**) memory::sql_alloc(sizeof(Item*)*5)))
 
114
    {
 
115
      args[0]= a; args[1]= b; args[2]= c; args[3]= d; args[4]= e;
 
116
      with_sum_func= a->with_sum_func || b->with_sum_func ||
 
117
        c->with_sum_func || d->with_sum_func || e->with_sum_func ;
 
118
    }
 
119
    collation.set(DERIVATION_SYSCONST);
 
120
  }
 
121
 
 
122
 
42
123
void Item_func::set_arguments(List<Item> &list)
43
124
{
44
125
  allowed_arg_cols= 1;
45
 
  arg_count=list.elements;
 
126
  arg_count=list.size();
46
127
  args= tmp_arg;                                // If 2 arguments
47
128
  if (arg_count <= 2 || (args=(Item**) memory::sql_alloc(sizeof(Item*)*arg_count)))
48
129
  {
49
 
    List_iterator_fast<Item> li(list);
 
130
    List<Item>::iterator li(list.begin());
50
131
    Item *item;
51
132
    Item **save_args= args;
52
133
 
56
137
      with_sum_func|=item->with_sum_func;
57
138
    }
58
139
  }
59
 
  list.empty();          // Fields are used
 
140
  list.clear();          // Fields are used
60
141
}
61
142
 
62
143
Item_func::Item_func(List<Item> &list) :
63
144
  _session(*current_session),
64
 
  allowed_arg_cols(1)
 
145
  allowed_arg_cols(1),
 
146
  const_item_cache(false)
65
147
{
66
148
  collation.set(DERIVATION_SYSCONST);
67
149
  set_arguments(list);
82
164
      args= tmp_arg;
83
165
    else
84
166
    {
85
 
      if (!(args=(Item**) session->alloc(sizeof(Item*)*arg_count)))
 
167
      if (!(args=(Item**) session->getMemRoot()->allocate(sizeof(Item*)*arg_count)))
86
168
        return;
87
169
    }
88
170
    memcpy(args, item->args, sizeof(Item*)*arg_count);
133
215
  unsigned char buff[STACK_BUFF_ALLOC];      // Max argument in function
134
216
  session->session_marker= 0;
135
217
  used_tables_cache= not_null_tables_cache= 0;
136
 
  const_item_cache=1;
 
218
  const_item_cache= true;
137
219
 
138
220
  if (check_stack_overrun(session, STACK_MIN_SIZE, buff))
139
221
    return true;        // Fatal error if flag is set!
188
270
  Item **arg,**arg_end;
189
271
 
190
272
  used_tables_cache= not_null_tables_cache= 0;
191
 
  const_item_cache=1;
 
273
  const_item_cache= false;
192
274
 
193
275
  if (arg_count)
194
276
  {
275
357
      Item *new_item= (*arg)->transform(transformer, argument);
276
358
      if (!new_item)
277
359
        return 0;
278
 
 
279
 
      /*
280
 
        Session::change_item_tree() should be called only if the tree was
281
 
        really transformed, i.e. when a new item has been created.
282
 
        Otherwise we'll be allocating a lot of unnecessary memory for
283
 
        change records at each execution.
284
 
      */
285
 
      if (*arg != new_item)
286
 
        getSession().change_item_tree(arg, new_item);
 
360
      *arg= new_item;
287
361
    }
288
362
  }
289
363
  return (this->*transformer)(argument);
331
405
      unsigned char *arg_v= *arg_p;
332
406
      Item *new_item= (*arg)->compile(analyzer, &arg_v, transformer, arg_t);
333
407
      if (new_item && *arg != new_item)
334
 
        current_session->change_item_tree(arg, new_item);
 
408
        *arg= new_item;
335
409
    }
336
410
  }
337
411
  return (this->*transformer)(arg_t);
353
427
void Item_func::update_used_tables()
354
428
{
355
429
  used_tables_cache=0;
356
 
  const_item_cache=1;
 
430
  const_item_cache= true;
357
431
  for (uint32_t i=0 ; i < arg_count ; i++)
358
432
  {
359
433
    args[i]->update_used_tables();
375
449
}
376
450
 
377
451
 
378
 
void Item_func::print(String *str, enum_query_type query_type)
 
452
void Item_func::print(String *str)
379
453
{
380
454
  str->append(func_name());
381
455
  str->append('(');
382
 
  print_args(str, 0, query_type);
 
456
  print_args(str, 0);
383
457
  str->append(')');
384
458
}
385
459
 
386
460
 
387
 
void Item_func::print_args(String *str, uint32_t from, enum_query_type query_type)
 
461
void Item_func::print_args(String *str, uint32_t from)
388
462
{
389
463
  for (uint32_t i=from ; i < arg_count ; i++)
390
464
  {
391
465
    if (i != from)
392
466
      str->append(',');
393
 
    args[i]->print(str, query_type);
 
467
    args[i]->print(str);
394
468
  }
395
469
}
396
470
 
397
471
 
398
 
void Item_func::print_op(String *str, enum_query_type query_type)
 
472
void Item_func::print_op(String *str)
399
473
{
400
474
  str->append('(');
401
475
  for (uint32_t i=0 ; i < arg_count-1 ; i++)
402
476
  {
403
 
    args[i]->print(str, query_type);
 
477
    args[i]->print(str);
404
478
    str->append(' ');
405
479
    str->append(func_name());
406
480
    str->append(' ');
407
481
  }
408
 
  args[arg_count-1]->print(str, query_type);
 
482
  args[arg_count-1]->print(str);
409
483
  str->append(')');
410
484
}
411
485
 
433
507
}
434
508
 
435
509
 
436
 
bool Item_func::get_arg0_date(DRIZZLE_TIME *ltime, uint32_t fuzzy_date)
 
510
bool Item_func::get_arg0_date(type::Time &ltime, uint32_t fuzzy_date)
437
511
{
438
512
  return (null_value=args[0]->get_date(ltime, fuzzy_date));
439
513
}
440
514
 
441
515
 
442
 
bool Item_func::get_arg0_time(DRIZZLE_TIME *ltime)
 
516
bool Item_func::get_arg0_time(type::Time &ltime)
443
517
{
444
 
  return (null_value=args[0]->get_time(ltime));
 
518
  return (null_value= args[0]->get_time(ltime));
445
519
}
446
520
 
447
521
 
458
532
 
459
533
  switch (result_type()) {
460
534
  case INT_RESULT:
461
 
    if (max_length > MY_INT32_NUM_DECIMAL_DIGITS)
462
 
      field= new field::Int64(max_length, maybe_null, name, unsigned_flag);
 
535
    if (unsigned_flag)
 
536
    {
 
537
      field= new field::Size(max_length, maybe_null, name, true);
 
538
    } 
 
539
    else if (max_length > MY_INT32_NUM_DECIMAL_DIGITS)
 
540
    {
 
541
      field= new field::Int64(max_length, maybe_null, name, false);
 
542
    }
463
543
    else
464
 
      field= new field::Int32(max_length, maybe_null, name, unsigned_flag);
 
544
    {
 
545
      field= new field::Int32(max_length, maybe_null, name, false);
 
546
    }
 
547
 
465
548
    break;
466
549
 
467
550
  case REAL_RESULT:
472
555
    return make_string_field(table);
473
556
 
474
557
  case DECIMAL_RESULT:
475
 
    field= new Field_decimal(my_decimal_precision_to_length(decimal_precision(),
 
558
    field= new Field_decimal(class_decimal_precision_to_length(decimal_precision(),
476
559
                                                            decimals,
477
560
                                                            unsigned_flag),
478
561
                             maybe_null,
493
576
}
494
577
 
495
578
 
496
 
my_decimal *Item_func::val_decimal(my_decimal *decimal_value)
 
579
type::Decimal *Item_func::val_decimal(type::Decimal *decimal_value)
497
580
{
498
581
  assert(fixed);
499
 
  int2my_decimal(E_DEC_FATAL_ERROR, val_int(), unsigned_flag, decimal_value);
 
582
  int2_class_decimal(E_DEC_FATAL_ERROR, val_int(), unsigned_flag, decimal_value);
500
583
  return decimal_value;
501
584
}
502
585
 
570
653
    set_if_smaller(unsigned_flag, args[i]->unsigned_flag);
571
654
  }
572
655
  int precision= min(max_int_part + decimals, DECIMAL_MAX_PRECISION);
573
 
  max_length= my_decimal_precision_to_length(precision, decimals,
 
656
  max_length= class_decimal_precision_to_length(precision, decimals,
574
657
                                             unsigned_flag);
575
658
}
576
659