~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/functions/func.cc

  • Committer: Lee
  • Date: 2008-10-30 22:02:01 UTC
  • mto: (572.1.2 devel)
  • mto: This revision was merged to the branch mainline in revision 573.
  • Revision ID: lbieber@lbieber-desktop-20081030220201-elb6qprbzpn7c5a4
add my name to the AUTHORS file

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems, Inc.
 
4
 *  Copyright (C) 2008 Sun Microsystems
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
17
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
18
 */
19
19
 
20
 
#include "config.h"
 
20
using namespace std;
 
21
#include <drizzled/server_includes.h>
 
22
 
 
23
#include CSTDINT_H
 
24
#include <cassert>
21
25
 
22
26
#include <drizzled/sql_string.h>
23
27
#include <drizzled/sql_list.h>
24
28
 
25
 
#include <drizzled/function/math/int.h>
26
 
#include <drizzled/field/int32.h>
27
 
#include <drizzled/field/int64.h>
28
 
#include <drizzled/field/decimal.h>
29
 
#include <drizzled/field/double.h>
30
 
#include <drizzled/field/size.h>
31
 
#include <drizzled/session.h>
32
 
#include <drizzled/error.h>
33
 
#include <drizzled/check_stack_overrun.h>
34
 
#include <limits>
35
 
#include <algorithm>
36
 
 
37
 
using namespace std;
38
 
 
39
 
namespace drizzled
40
 
{
 
29
#include <drizzled/functions/int.h>
41
30
 
42
31
 
43
32
void Item_func::set_arguments(List<Item> &list)
45
34
  allowed_arg_cols= 1;
46
35
  arg_count=list.elements;
47
36
  args= tmp_arg;                                // If 2 arguments
48
 
  if (arg_count <= 2 || (args=(Item**) memory::sql_alloc(sizeof(Item*)*arg_count)))
 
37
  if (arg_count <= 2 || (args=(Item**) sql_alloc(sizeof(Item*)*arg_count)))
49
38
  {
50
39
    List_iterator_fast<Item> li(list);
51
40
    Item *item;
60
49
  list.empty();          // Fields are used
61
50
}
62
51
 
63
 
Item_func::Item_func(List<Item> &list) :
64
 
  _session(*current_session),
65
 
  allowed_arg_cols(1),
66
 
  const_item_cache(false)
 
52
Item_func::Item_func(List<Item> &list)
 
53
  :allowed_arg_cols(1)
67
54
{
68
 
  collation.set(DERIVATION_SYSCONST);
69
55
  set_arguments(list);
70
56
}
71
57
 
72
 
Item_func::Item_func(Session *session, Item_func *item) :
73
 
  Item_result_field(session, item),
74
 
  _session(*current_session),
75
 
  allowed_arg_cols(item->allowed_arg_cols),
76
 
  arg_count(item->arg_count),
77
 
  used_tables_cache(item->used_tables_cache),
78
 
  not_null_tables_cache(item->not_null_tables_cache),
79
 
  const_item_cache(item->const_item_cache)
 
58
Item_func::Item_func(Session *session, Item_func *item)
 
59
  :Item_result_field(session, item),
 
60
   allowed_arg_cols(item->allowed_arg_cols),
 
61
   arg_count(item->arg_count),
 
62
   used_tables_cache(item->used_tables_cache),
 
63
   not_null_tables_cache(item->not_null_tables_cache),
 
64
   const_item_cache(item->const_item_cache)
80
65
{
81
66
  if (arg_count)
82
67
  {
84
69
      args= tmp_arg;
85
70
    else
86
71
    {
87
 
      if (!(args=(Item**) session->getMemRoot()->allocate(sizeof(Item*)*arg_count)))
 
72
      if (!(args=(Item**) session->alloc(sizeof(Item*)*arg_count)))
88
73
        return;
89
74
    }
90
75
    memcpy(args, item->args, sizeof(Item*)*arg_count);
91
76
  }
92
 
  collation.set(DERIVATION_SYSCONST);
93
77
}
94
78
 
95
79
 
127
111
*/
128
112
 
129
113
bool
130
 
Item_func::fix_fields(Session *session, Item **)
 
114
Item_func::fix_fields(Session *session, Item **ref __attribute__((unused)))
131
115
{
132
116
  assert(fixed == 0);
133
117
  Item **arg,**arg_end;
135
119
  unsigned char buff[STACK_BUFF_ALLOC];      // Max argument in function
136
120
  session->session_marker= 0;
137
121
  used_tables_cache= not_null_tables_cache= 0;
138
 
  const_item_cache= true;
 
122
  const_item_cache=1;
139
123
 
140
124
  if (check_stack_overrun(session, STACK_MIN_SIZE, buff))
141
125
    return true;        // Fatal error if flag is set!
149
133
        We shouldn't call fix_fields() twice, so check 'fixed' field first
150
134
      */
151
135
      if ((!(*arg)->fixed && (*arg)->fix_fields(session, arg)))
152
 
        return true;
 
136
        return true;        /* purecov: inspected */
153
137
      item= *arg;
154
138
 
155
139
      if (allowed_arg_cols)
184
168
}
185
169
 
186
170
 
187
 
void Item_func::fix_after_pullout(Select_Lex *new_parent,
188
 
                                  Item **)
 
171
void Item_func::fix_after_pullout(st_select_lex *new_parent,
 
172
                                  Item **ref __attribute__((unused)))
189
173
{
190
174
  Item **arg,**arg_end;
191
175
 
192
176
  used_tables_cache= not_null_tables_cache= 0;
193
 
  const_item_cache= false;
 
177
  const_item_cache=1;
194
178
 
195
179
  if (arg_count)
196
180
  {
230
214
    Item **arg,**arg_end;
231
215
 
232
216
    switch (order) {
233
 
    case (T_PREFIX):
 
217
    case(PREFIX):
234
218
      (*traverser)(this, argument);
235
219
      for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
236
220
      {
237
221
        (*arg)->traverse_cond(traverser, argument, order);
238
222
      }
239
223
      break;
240
 
    case (T_POSTFIX):
 
224
    case (POSTFIX):
241
225
      for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
242
226
      {
243
227
        (*arg)->traverse_cond(traverser, argument, order);
285
269
        change records at each execution.
286
270
      */
287
271
      if (*arg != new_item)
288
 
        getSession().change_item_tree(arg, new_item);
 
272
        current_session->change_item_tree(arg, new_item);
289
273
    }
290
274
  }
291
275
  return (this->*transformer)(argument);
348
332
{
349
333
  Item **arg, **arg_end;
350
334
  for (arg= args, arg_end= args+arg_count; arg != arg_end ; arg++)
351
 
    (*arg)->split_sum_func(session, ref_pointer_array, fields, arg, true);
 
335
    (*arg)->split_sum_func2(session, ref_pointer_array, fields, arg, true);
352
336
}
353
337
 
354
338
 
355
339
void Item_func::update_used_tables()
356
340
{
357
341
  used_tables_cache=0;
358
 
  const_item_cache= true;
 
342
  const_item_cache=1;
359
343
  for (uint32_t i=0 ; i < arg_count ; i++)
360
344
  {
361
345
    args[i]->update_used_tables();
435
419
}
436
420
 
437
421
 
438
 
bool Item_func::get_arg0_date(type::Time &ltime, uint32_t fuzzy_date)
 
422
bool Item_func::get_arg0_date(DRIZZLE_TIME *ltime, uint32_t fuzzy_date)
439
423
{
440
424
  return (null_value=args[0]->get_date(ltime, fuzzy_date));
441
425
}
442
426
 
443
427
 
444
 
bool Item_func::get_arg0_time(type::Time &ltime)
 
428
bool Item_func::get_arg0_time(DRIZZLE_TIME *ltime)
445
429
{
446
 
  return (null_value= args[0]->get_time(ltime));
 
430
  return (null_value=args[0]->get_time(ltime));
447
431
}
448
432
 
449
433
 
456
440
 
457
441
Field *Item_func::tmp_table_field(Table *table)
458
442
{
459
 
  Field *field= NULL;
 
443
  Field *field;
460
444
 
461
445
  switch (result_type()) {
462
446
  case INT_RESULT:
463
 
    if (unsigned_flag)
464
 
    {
465
 
      field= new field::Size(max_length, maybe_null, name, true);
466
 
    } 
467
 
    else if (max_length > MY_INT32_NUM_DECIMAL_DIGITS)
468
 
    {
469
 
      field= new field::Int64(max_length, maybe_null, name, false);
470
 
    }
 
447
    if (max_length > MY_INT32_NUM_DECIMAL_DIGITS)
 
448
      field= new Field_int64_t(max_length, maybe_null, name, unsigned_flag);
471
449
    else
472
 
    {
473
 
      field= new field::Int32(max_length, maybe_null, name, false);
474
 
    }
475
 
 
 
450
      field= new Field_long(max_length, maybe_null, name, unsigned_flag);
476
451
    break;
477
 
 
478
452
  case REAL_RESULT:
479
453
    field= new Field_double(max_length, maybe_null, name, decimals);
480
454
    break;
481
 
 
482
455
  case STRING_RESULT:
483
456
    return make_string_field(table);
484
 
 
 
457
    break;
485
458
  case DECIMAL_RESULT:
486
 
    field= new Field_decimal(class_decimal_precision_to_length(decimal_precision(),
487
 
                                                            decimals,
488
 
                                                            unsigned_flag),
489
 
                             maybe_null,
490
 
                             name,
491
 
                             decimals,
492
 
                             unsigned_flag);
 
459
    field= new Field_new_decimal(
 
460
                       my_decimal_precision_to_length(decimal_precision(),
 
461
                                                      decimals,
 
462
                                                      unsigned_flag),
 
463
                       maybe_null, name, decimals, unsigned_flag);
493
464
    break;
494
465
  case ROW_RESULT:
 
466
  default:
495
467
    // This case should never be chosen
496
468
    assert(0);
 
469
    field= 0;
497
470
    break;
498
471
  }
499
 
 
500
472
  if (field)
501
473
    field->init(table);
502
 
 
503
474
  return field;
504
475
}
505
476
 
506
477
 
507
 
type::Decimal *Item_func::val_decimal(type::Decimal *decimal_value)
 
478
my_decimal *Item_func::val_decimal(my_decimal *decimal_value)
508
479
{
509
480
  assert(fixed);
510
 
  int2_class_decimal(E_DEC_FATAL_ERROR, val_int(), unsigned_flag, decimal_value);
 
481
  int2my_decimal(E_DEC_FATAL_ERROR, val_int(), unsigned_flag, decimal_value);
511
482
  return decimal_value;
512
483
}
513
484
 
538
509
 
539
510
double Item_func::fix_result(double value)
540
511
{
541
 
  static double fix_infinity= numeric_limits<double>::infinity();
542
 
 
543
 
  if (value != fix_infinity && value != -fix_infinity)
 
512
  if (isfinite(value))
544
513
    return value;
545
514
  null_value=1;
546
515
  return 0.0;
547
516
}
548
 
 
549
 
 
550
 
void Item_func::fix_num_length_and_dec()
551
 
{
552
 
  uint32_t fl_length= 0;
553
 
  decimals=0;
554
 
  for (uint32_t i=0 ; i < arg_count ; i++)
555
 
  {
556
 
    set_if_bigger(decimals,args[i]->decimals);
557
 
    set_if_bigger(fl_length, args[i]->max_length);
558
 
  }
559
 
  max_length=float_length(decimals);
560
 
  if (fl_length > max_length)
561
 
  {
562
 
    decimals= NOT_FIXED_DEC;
563
 
    max_length= float_length(NOT_FIXED_DEC);
564
 
  }
565
 
}
566
 
 
567
 
/**
568
 
  Set max_length/decimals of function if function is fixed point and
569
 
  result length/precision depends on argument ones.
570
 
*/
571
 
 
572
 
void Item_func::count_decimal_length()
573
 
{
574
 
  int max_int_part= 0;
575
 
  decimals= 0;
576
 
  unsigned_flag= 1;
577
 
  for (uint32_t i= 0 ; i < arg_count ; i++)
578
 
  {
579
 
    set_if_bigger(decimals, args[i]->decimals);
580
 
    set_if_bigger(max_int_part, args[i]->decimal_int_part());
581
 
    set_if_smaller(unsigned_flag, args[i]->unsigned_flag);
582
 
  }
583
 
  int precision= min(max_int_part + decimals, DECIMAL_MAX_PRECISION);
584
 
  max_length= class_decimal_precision_to_length(precision, decimals,
585
 
                                             unsigned_flag);
586
 
}
587
 
 
588
 
 
589
 
/**
590
 
  Set max_length of if it is maximum length of its arguments.
591
 
*/
592
 
 
593
 
void Item_func::count_only_length()
594
 
{
595
 
  max_length= 0;
596
 
  unsigned_flag= 0;
597
 
  for (uint32_t i=0 ; i < arg_count ; i++)
598
 
  {
599
 
    set_if_bigger(max_length, args[i]->max_length);
600
 
    set_if_bigger(unsigned_flag, args[i]->unsigned_flag);
601
 
  }
602
 
}
603
 
 
604
 
 
605
 
/**
606
 
  Set max_length/decimals of function if function is floating point and
607
 
  result length/precision depends on argument ones.
608
 
*/
609
 
 
610
 
void Item_func::count_real_length()
611
 
{
612
 
  uint32_t length= 0;
613
 
  decimals= 0;
614
 
  max_length= 0;
615
 
  for (uint32_t i=0 ; i < arg_count ; i++)
616
 
  {
617
 
    if (decimals != NOT_FIXED_DEC)
618
 
    {
619
 
      set_if_bigger(decimals, args[i]->decimals);
620
 
      set_if_bigger(length, (args[i]->max_length - args[i]->decimals));
621
 
    }
622
 
    set_if_bigger(max_length, args[i]->max_length);
623
 
  }
624
 
  if (decimals != NOT_FIXED_DEC)
625
 
  {
626
 
    max_length= length;
627
 
    length+= decimals;
628
 
    if (length < max_length)  // If previous operation gave overflow
629
 
      max_length= UINT32_MAX;
630
 
    else
631
 
      max_length= length;
632
 
  }
633
 
}
634
 
 
635
 
 
636
 
 
637
 
void Item_func::signal_divide_by_null()
638
 
{
639
 
  my_error(ER_DIVISION_BY_ZERO, MYF(0));
640
 
  null_value= 0;
641
 
}
642
 
 
643
 
 
644
 
Item *Item_func::get_tmp_table_item(Session *session)
645
 
{
646
 
  if (!with_sum_func && !const_item() && functype() != SUSERVAR_FUNC)
647
 
    return new Item_field(result_field);
648
 
  return copy_or_same(session);
649
 
}
650
 
 
651
 
 
652
 
} /* namespace drizzled */