1
/** Copyright (C) 2000-2003 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
21
This file defines all numerical functions
24
#include <drizzled/server_includes.h>
25
#include <drizzled/functions/func.h>
26
#include <drizzled/session.h>
27
#include <mysys/my_bit.h>
28
#include <drizzled/slave.h>
29
#include <drizzled/error.h>
31
bool check_reserved_words(LEX_STRING *name)
33
if (!my_strcasecmp(system_charset_info, name->str, "GLOBAL") ||
34
!my_strcasecmp(system_charset_info, name->str, "LOCAL") ||
35
!my_strcasecmp(system_charset_info, name->str, "SESSION"))
43
true if item is a constant
47
eval_const_cond(COND *cond)
49
return ((Item_func*) cond)->val_int() ? true : false;
53
void Item_func::fix_num_length_and_dec()
55
uint32_t fl_length= 0;
57
for (uint32_t i=0 ; i < arg_count ; i++)
59
set_if_bigger(decimals,args[i]->decimals);
60
set_if_bigger(fl_length, args[i]->max_length);
62
max_length=float_length(decimals);
63
if (fl_length > max_length)
65
decimals= NOT_FIXED_DEC;
66
max_length= float_length(NOT_FIXED_DEC);
71
Set max_length/decimals of function if function is fixed point and
72
result length/precision depends on argument ones.
75
void Item_func::count_decimal_length()
80
for (uint32_t i=0 ; i < arg_count ; i++)
82
set_if_bigger(decimals, args[i]->decimals);
83
set_if_bigger(max_int_part, args[i]->decimal_int_part());
84
set_if_smaller(unsigned_flag, args[i]->unsigned_flag);
86
int precision= cmin(max_int_part + decimals, DECIMAL_MAX_PRECISION);
87
max_length= my_decimal_precision_to_length(precision, decimals,
93
Set max_length of if it is maximum length of its arguments.
96
void Item_func::count_only_length()
100
for (uint32_t i=0 ; i < arg_count ; i++)
102
set_if_bigger(max_length, args[i]->max_length);
103
set_if_bigger(unsigned_flag, args[i]->unsigned_flag);
109
Set max_length/decimals of function if function is floating point and
110
result length/precision depends on argument ones.
113
void Item_func::count_real_length()
118
for (uint32_t i=0 ; i < arg_count ; i++)
120
if (decimals != NOT_FIXED_DEC)
122
set_if_bigger(decimals, args[i]->decimals);
123
set_if_bigger(length, (args[i]->max_length - args[i]->decimals));
125
set_if_bigger(max_length, args[i]->max_length);
127
if (decimals != NOT_FIXED_DEC)
131
if (length < max_length) // If previous operation gave overflow
132
max_length= UINT32_MAX;
140
void Item_func::signal_divide_by_null()
142
Session *session= current_session;
143
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_ERROR, ER_DIVISION_BY_ZERO, ER(ER_DIVISION_BY_ZERO));
148
Item *Item_func::get_tmp_table_item(Session *session)
150
if (!with_sum_func && !const_item() && functype() != SUSERVAR_FUNC)
151
return new Item_field(result_field);
152
return copy_or_same(session);
156
/** Get the value of a variable as a double. */
158
double user_var_entry::val_real(bool *null_value)
160
if ((*null_value= (value == 0)))
165
return *(double*) value;
167
return (double) *(int64_t*) value;
171
my_decimal2double(E_DEC_FATAL_ERROR, (my_decimal *)value, &result);
175
return my_atof(value); // This is null terminated
177
assert(1); // Impossible
180
return 0.0; // Impossible
184
/** Get the value of a variable as an integer. */
186
int64_t user_var_entry::val_int(bool *null_value) const
188
if ((*null_value= (value == 0)))
193
return (int64_t) *(double*) value;
195
return *(int64_t*) value;
199
my_decimal2int(E_DEC_FATAL_ERROR, (my_decimal *)value, 0, &result);
205
return my_strtoll10(value, (char**) 0, &error);// String is null terminated
208
assert(1); // Impossible
211
return 0L; // Impossible
215
/** Get the value of a variable as a string. */
217
String *user_var_entry::val_str(bool *null_value, String *str,
220
if ((*null_value= (value == 0)))
225
str->set_real(*(double*) value, decimals, &my_charset_bin);
229
str->set(*(int64_t*) value, &my_charset_bin);
231
str->set(*(uint64_t*) value, &my_charset_bin);
234
my_decimal2string(E_DEC_FATAL_ERROR, (my_decimal *)value, 0, 0, 0, str);
237
if (str->copy(value, length, collation.collation))
240
assert(1); // Impossible
246
/** Get the value of a variable as a decimal. */
248
my_decimal *user_var_entry::val_decimal(bool *null_value, my_decimal *val)
250
if ((*null_value= (value == 0)))
255
double2my_decimal(E_DEC_FATAL_ERROR, *(double*) value, val);
258
int2my_decimal(E_DEC_FATAL_ERROR, *(int64_t*) value, 0, val);
261
val= (my_decimal *)value;
264
str2my_decimal(E_DEC_FATAL_ERROR, value, length, collation.collation, val);
267
assert(1); // Impossible