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
#ifdef USE_PRAGMA_IMPLEMENTATION
25
#pragma implementation // gcc: Class implementation
28
#include "mysql_priv.h"
29
#include "slave.h" // for wait_for_master_pos
34
#include <ft_global.h>
37
bool check_reserved_words(LEX_STRING *name)
39
if (!my_strcasecmp(system_charset_info, name->str, "GLOBAL") ||
40
!my_strcasecmp(system_charset_info, name->str, "LOCAL") ||
41
!my_strcasecmp(system_charset_info, name->str, "SESSION"))
49
TRUE if item is a constant
53
eval_const_cond(COND *cond)
55
return ((Item_func*) cond)->val_int() ? TRUE : FALSE;
59
void Item_func::set_arguments(List<Item> &list)
62
arg_count=list.elements;
63
args= tmp_arg; // If 2 arguments
64
if (arg_count <= 2 || (args=(Item**) sql_alloc(sizeof(Item*)*arg_count)))
66
List_iterator_fast<Item> li(list);
68
Item **save_args= args;
73
with_sum_func|=item->with_sum_func;
76
list.empty(); // Fields are used
79
Item_func::Item_func(List<Item> &list)
85
Item_func::Item_func(THD *thd, Item_func *item)
86
:Item_result_field(thd, item),
87
allowed_arg_cols(item->allowed_arg_cols),
88
arg_count(item->arg_count),
89
used_tables_cache(item->used_tables_cache),
90
not_null_tables_cache(item->not_null_tables_cache),
91
const_item_cache(item->const_item_cache)
99
if (!(args=(Item**) thd->alloc(sizeof(Item*)*arg_count)))
102
memcpy((char*) args, (char*) item->args, sizeof(Item*)*arg_count);
108
Resolve references to table column for a function and its argument
113
ref Pointer to where this object is used. This reference
114
is used if we want to replace this object with another
115
one (for example in the summary functions).
118
Call fix_fields() for all arguments to the function. The main intention
119
is to allow all Item_field() objects to setup pointers to the table fields.
121
Sets as a side effect the following class variables:
122
maybe_null Set if any argument may return NULL
123
with_sum_func Set if any of the arguments contains a sum function
124
used_tables_cache Set to union of the tables used by arguments
126
str_value.charset If this is a string function, set this to the
127
character set for the first argument.
128
If any argument is binary, this is set to binary
130
If for any item any of the defaults are wrong, then this can
131
be fixed in the fix_length_and_dec() function that is called
132
after this one or by writing a specialized fix_fields() for the
137
TRUE Got error. Stored with my_error().
141
Item_func::fix_fields(THD *thd, Item **ref)
143
DBUG_ASSERT(fixed == 0);
144
Item **arg,**arg_end;
145
void *save_thd_marker= thd->thd_marker;
146
uchar buff[STACK_BUFF_ALLOC]; // Max argument in function
148
used_tables_cache= not_null_tables_cache= 0;
151
if (check_stack_overrun(thd, STACK_MIN_SIZE, buff))
152
return TRUE; // Fatal error if flag is set!
154
{ // Print purify happy
155
for (arg=args, arg_end=args+arg_count; arg != arg_end ; arg++)
159
We can't yet set item to *arg as fix_fields may change *arg
160
We shouldn't call fix_fields() twice, so check 'fixed' field first
162
if ((!(*arg)->fixed && (*arg)->fix_fields(thd, arg)))
163
return TRUE; /* purecov: inspected */
166
if (allowed_arg_cols)
168
if (item->check_cols(allowed_arg_cols))
173
/* we have to fetch allowed_arg_cols from first argument */
174
DBUG_ASSERT(arg == args); // it is first argument
175
allowed_arg_cols= item->cols();
176
DBUG_ASSERT(allowed_arg_cols); // Can't be 0 any more
179
if (item->maybe_null)
182
with_sum_func= with_sum_func || item->with_sum_func;
183
used_tables_cache|= item->used_tables();
184
not_null_tables_cache|= item->not_null_tables();
185
const_item_cache&= item->const_item();
186
with_subselect|= item->with_subselect;
189
fix_length_and_dec();
190
if (thd->is_error()) // An error inside fix_length_and_dec occured
193
thd->thd_marker= save_thd_marker;
198
void Item_func::fix_after_pullout(st_select_lex *new_parent, Item **ref)
200
Item **arg,**arg_end;
202
used_tables_cache= not_null_tables_cache= 0;
207
for (arg=args, arg_end=args+arg_count; arg != arg_end ; arg++)
209
(*arg)->fix_after_pullout(new_parent, arg);
212
used_tables_cache|= item->used_tables();
213
not_null_tables_cache|= item->not_null_tables();
214
const_item_cache&= item->const_item();
220
bool Item_func::walk(Item_processor processor, bool walk_subquery,
225
Item **arg,**arg_end;
226
for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
228
if ((*arg)->walk(processor, walk_subquery, argument))
232
return (this->*processor)(argument);
235
void Item_func::traverse_cond(Cond_traverser traverser,
236
void *argument, traverse_order order)
240
Item **arg,**arg_end;
244
(*traverser)(this, argument);
245
for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
247
(*arg)->traverse_cond(traverser, argument, order);
251
for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
253
(*arg)->traverse_cond(traverser, argument, order);
255
(*traverser)(this, argument);
259
(*traverser)(this, argument);
264
Transform an Item_func object with a transformer callback function.
266
The function recursively applies the transform method to each
267
argument of the Item_func node.
268
If the call of the method for an argument item returns a new item
269
the old item is substituted for a new one.
270
After this the transformer is applied to the root node
271
of the Item_func object.
272
@param transformer the transformer callback function to be applied to
273
the nodes of the tree of the object
274
@param argument parameter to be passed to the transformer
277
Item returned as the result of transformation of the root node
280
Item *Item_func::transform(Item_transformer transformer, uchar *argument)
284
Item **arg,**arg_end;
285
for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
287
Item *new_item= (*arg)->transform(transformer, argument);
292
THD::change_item_tree() should be called only if the tree was
293
really transformed, i.e. when a new item has been created.
294
Otherwise we'll be allocating a lot of unnecessary memory for
295
change records at each execution.
297
if (*arg != new_item)
298
current_thd->change_item_tree(arg, new_item);
301
return (this->*transformer)(argument);
306
Compile Item_func object with a processor and a transformer
309
First the function applies the analyzer to the root node of
310
the Item_func object. Then if the analizer succeeeds (returns TRUE)
311
the function recursively applies the compile method to each argument
312
of the Item_func node.
313
If the call of the method for an argument item returns a new item
314
the old item is substituted for a new one.
315
After this the transformer is applied to the root node
316
of the Item_func object.
318
@param analyzer the analyzer callback function to be applied to the
319
nodes of the tree of the object
320
@param[in,out] arg_p parameter to be passed to the processor
321
@param transformer the transformer callback function to be applied to the
322
nodes of the tree of the object
323
@param arg_t parameter to be passed to the transformer
326
Item returned as the result of transformation of the root node
329
Item *Item_func::compile(Item_analyzer analyzer, uchar **arg_p,
330
Item_transformer transformer, uchar *arg_t)
332
if (!(this->*analyzer)(arg_p))
336
Item **arg,**arg_end;
337
for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
340
The same parameter value of arg_p must be passed
341
to analyze any argument of the condition formula.
343
uchar *arg_v= *arg_p;
344
Item *new_item= (*arg)->compile(analyzer, &arg_v, transformer, arg_t);
345
if (new_item && *arg != new_item)
346
current_thd->change_item_tree(arg, new_item);
349
return (this->*transformer)(arg_t);
353
See comments in Item_cmp_func::split_sum_func()
356
void Item_func::split_sum_func(THD *thd, Item **ref_pointer_array,
359
Item **arg, **arg_end;
360
for (arg= args, arg_end= args+arg_count; arg != arg_end ; arg++)
361
(*arg)->split_sum_func2(thd, ref_pointer_array, fields, arg, TRUE);
365
void Item_func::update_used_tables()
369
for (uint i=0 ; i < arg_count ; i++)
371
args[i]->update_used_tables();
372
used_tables_cache|=args[i]->used_tables();
373
const_item_cache&=args[i]->const_item();
378
table_map Item_func::used_tables() const
380
return used_tables_cache;
384
table_map Item_func::not_null_tables() const
386
return not_null_tables_cache;
390
void Item_func::print(String *str, enum_query_type query_type)
392
str->append(func_name());
394
print_args(str, 0, query_type);
399
void Item_func::print_args(String *str, uint from, enum_query_type query_type)
401
for (uint i=from ; i < arg_count ; i++)
405
args[i]->print(str, query_type);
410
void Item_func::print_op(String *str, enum_query_type query_type)
413
for (uint i=0 ; i < arg_count-1 ; i++)
415
args[i]->print(str, query_type);
417
str->append(func_name());
420
args[arg_count-1]->print(str, query_type);
425
bool Item_func::eq(const Item *item, bool binary_cmp) const
427
/* Assume we don't have rtti */
430
if (item->type() != FUNC_ITEM)
432
Item_func *item_func=(Item_func*) item;
433
Item_func::Functype func_type;
434
if ((func_type= functype()) != item_func->functype() ||
435
arg_count != item_func->arg_count ||
436
(func_type != Item_func::FUNC_SP &&
437
func_name() != item_func->func_name()) ||
438
(func_type == Item_func::FUNC_SP &&
439
my_strcasecmp(system_charset_info, func_name(), item_func->func_name())))
441
for (uint i=0; i < arg_count ; i++)
442
if (!args[i]->eq(item_func->args[i], binary_cmp))
448
Field *Item_func::tmp_table_field(TABLE *table)
452
switch (result_type()) {
454
if (max_length > MY_INT32_NUM_DECIMAL_DIGITS)
455
field= new Field_longlong(max_length, maybe_null, name, unsigned_flag);
457
field= new Field_long(max_length, maybe_null, name, unsigned_flag);
460
field= new Field_double(max_length, maybe_null, name, decimals);
463
return make_string_field(table);
466
field= new Field_new_decimal(my_decimal_precision_to_length(decimal_precision(),
469
maybe_null, name, decimals, unsigned_flag);
473
// This case should never be chosen
484
my_decimal *Item_func::val_decimal(my_decimal *decimal_value)
487
int2my_decimal(E_DEC_FATAL_ERROR, val_int(), unsigned_flag, decimal_value);
488
return decimal_value;
492
String *Item_real_func::val_str(String *str)
494
DBUG_ASSERT(fixed == 1);
495
double nr= val_real();
497
return 0; /* purecov: inspected */
498
str->set_real(nr,decimals, &my_charset_bin);
503
my_decimal *Item_real_func::val_decimal(my_decimal *decimal_value)
506
double nr= val_real();
508
return 0; /* purecov: inspected */
509
double2my_decimal(E_DEC_FATAL_ERROR, nr, decimal_value);
510
return decimal_value;
514
void Item_func::fix_num_length_and_dec()
518
for (uint i=0 ; i < arg_count ; i++)
520
set_if_bigger(decimals,args[i]->decimals);
521
set_if_bigger(fl_length, args[i]->max_length);
523
max_length=float_length(decimals);
524
if (fl_length > max_length)
526
decimals= NOT_FIXED_DEC;
527
max_length= float_length(NOT_FIXED_DEC);
532
void Item_func_numhybrid::fix_num_length_and_dec()
537
Set max_length/decimals of function if function is fixed point and
538
result length/precision depends on argument ones.
541
void Item_func::count_decimal_length()
546
for (uint i=0 ; i < arg_count ; i++)
548
set_if_bigger(decimals, args[i]->decimals);
549
set_if_bigger(max_int_part, args[i]->decimal_int_part());
550
set_if_smaller(unsigned_flag, args[i]->unsigned_flag);
552
int precision= min(max_int_part + decimals, DECIMAL_MAX_PRECISION);
553
max_length= my_decimal_precision_to_length(precision, decimals,
559
Set max_length of if it is maximum length of its arguments.
562
void Item_func::count_only_length()
566
for (uint i=0 ; i < arg_count ; i++)
568
set_if_bigger(max_length, args[i]->max_length);
569
set_if_bigger(unsigned_flag, args[i]->unsigned_flag);
575
Set max_length/decimals of function if function is floating point and
576
result length/precision depends on argument ones.
579
void Item_func::count_real_length()
584
for (uint i=0 ; i < arg_count ; i++)
586
if (decimals != NOT_FIXED_DEC)
588
set_if_bigger(decimals, args[i]->decimals);
589
set_if_bigger(length, (args[i]->max_length - args[i]->decimals));
591
set_if_bigger(max_length, args[i]->max_length);
593
if (decimals != NOT_FIXED_DEC)
597
if (length < max_length) // If previous operation gave overflow
598
max_length= UINT_MAX32;
606
void Item_func::signal_divide_by_null()
608
THD *thd= current_thd;
609
if (thd->variables.sql_mode & MODE_ERROR_FOR_DIVISION_BY_ZERO)
610
push_warning(thd, MYSQL_ERROR::WARN_LEVEL_ERROR, ER_DIVISION_BY_ZERO,
611
ER(ER_DIVISION_BY_ZERO));
616
Item *Item_func::get_tmp_table_item(THD *thd)
618
if (!with_sum_func && !const_item() && functype() != SUSERVAR_FUNC)
619
return new Item_field(result_field);
620
return copy_or_same(thd);
623
double Item_int_func::val_real()
625
DBUG_ASSERT(fixed == 1);
627
return unsigned_flag ? (double) ((ulonglong) val_int()) : (double) val_int();
631
String *Item_int_func::val_str(String *str)
633
DBUG_ASSERT(fixed == 1);
634
longlong nr=val_int();
637
str->set_int(nr, unsigned_flag, &my_charset_bin);
642
void Item_func_connection_id::fix_length_and_dec()
644
Item_int_func::fix_length_and_dec();
649
bool Item_func_connection_id::fix_fields(THD *thd, Item **ref)
651
if (Item_int_func::fix_fields(thd, ref))
653
thd->thread_specific_used= TRUE;
654
value= thd->variables.pseudo_thread_id;
660
Check arguments here to determine result's type for a numeric
661
function of two arguments.
664
void Item_num_op::find_num_type(void)
666
DBUG_ENTER("Item_num_op::find_num_type");
667
DBUG_PRINT("info", ("name %s", func_name()));
668
DBUG_ASSERT(arg_count == 2);
669
Item_result r0= args[0]->result_type();
670
Item_result r1= args[1]->result_type();
672
if (r0 == REAL_RESULT || r1 == REAL_RESULT ||
673
r0 == STRING_RESULT || r1 ==STRING_RESULT)
676
max_length= float_length(decimals);
677
hybrid_type= REAL_RESULT;
679
else if (r0 == DECIMAL_RESULT || r1 == DECIMAL_RESULT)
681
hybrid_type= DECIMAL_RESULT;
686
DBUG_ASSERT(r0 == INT_RESULT && r1 == INT_RESULT);
688
hybrid_type=INT_RESULT;
691
DBUG_PRINT("info", ("Type: %s",
692
(hybrid_type == REAL_RESULT ? "REAL_RESULT" :
693
hybrid_type == DECIMAL_RESULT ? "DECIMAL_RESULT" :
694
hybrid_type == INT_RESULT ? "INT_RESULT" :
701
Set result type for a numeric function of one argument
702
(can be also used by a numeric function of many arguments, if the result
703
type depends only on the first argument)
706
void Item_func_num1::find_num_type()
708
DBUG_ENTER("Item_func_num1::find_num_type");
709
DBUG_PRINT("info", ("name %s", func_name()));
710
switch (hybrid_type= args[0]->result_type()) {
712
unsigned_flag= args[0]->unsigned_flag;
716
hybrid_type= REAL_RESULT;
717
max_length= float_length(decimals);
724
DBUG_PRINT("info", ("Type: %s",
725
(hybrid_type == REAL_RESULT ? "REAL_RESULT" :
726
hybrid_type == DECIMAL_RESULT ? "DECIMAL_RESULT" :
727
hybrid_type == INT_RESULT ? "INT_RESULT" :
733
void Item_func_num1::fix_num_length_and_dec()
735
decimals= args[0]->decimals;
736
max_length= args[0]->max_length;
740
void Item_func_numhybrid::fix_length_and_dec()
742
fix_num_length_and_dec();
747
String *Item_func_numhybrid::val_str(String *str)
749
DBUG_ASSERT(fixed == 1);
750
switch (hybrid_type) {
753
my_decimal decimal_value, *val;
754
if (!(val= decimal_op(&decimal_value)))
755
return 0; // null is set
756
my_decimal_round(E_DEC_FATAL_ERROR, val, decimals, FALSE, val);
757
my_decimal2string(E_DEC_FATAL_ERROR, val, 0, 0, 0, str);
762
longlong nr= int_op();
764
return 0; /* purecov: inspected */
765
str->set_int(nr, unsigned_flag, &my_charset_bin);
770
double nr= real_op();
772
return 0; /* purecov: inspected */
773
str->set_real(nr,decimals,&my_charset_bin);
777
return str_op(&str_value);
785
double Item_func_numhybrid::val_real()
787
DBUG_ASSERT(fixed == 1);
788
switch (hybrid_type) {
791
my_decimal decimal_value, *val;
793
if (!(val= decimal_op(&decimal_value)))
794
return 0.0; // null is set
795
my_decimal2double(E_DEC_FATAL_ERROR, val, &result);
800
longlong result= int_op();
801
return unsigned_flag ? (double) ((ulonglong) result) : (double) result;
809
String *res= str_op(&str_value);
810
return (res ? my_strntod(res->charset(), (char*) res->ptr(), res->length(),
811
&end_not_used, &err_not_used) : 0.0);
820
longlong Item_func_numhybrid::val_int()
822
DBUG_ASSERT(fixed == 1);
823
switch (hybrid_type) {
826
my_decimal decimal_value, *val;
827
if (!(val= decimal_op(&decimal_value)))
828
return 0; // null is set
830
my_decimal2int(E_DEC_FATAL_ERROR, val, unsigned_flag, &result);
836
return (longlong) rint(real_op());
841
if (!(res= str_op(&str_value)))
844
char *end= (char*) res->ptr() + res->length();
845
CHARSET_INFO *cs= str_value.charset();
846
return (*(cs->cset->strtoll10))(cs, res->ptr(), &end, &err_not_used);
855
my_decimal *Item_func_numhybrid::val_decimal(my_decimal *decimal_value)
857
my_decimal *val= decimal_value;
858
DBUG_ASSERT(fixed == 1);
859
switch (hybrid_type) {
861
val= decimal_op(decimal_value);
865
longlong result= int_op();
866
int2my_decimal(E_DEC_FATAL_ERROR, result, unsigned_flag, decimal_value);
871
double result= (double)real_op();
872
double2my_decimal(E_DEC_FATAL_ERROR, result, decimal_value);
878
if (!(res= str_op(&str_value)))
881
str2my_decimal(E_DEC_FATAL_ERROR, (char*) res->ptr(),
882
res->length(), res->charset(), decimal_value);
893
void Item_func_signed::print(String *str, enum_query_type query_type)
895
str->append(STRING_WITH_LEN("cast("));
896
args[0]->print(str, query_type);
897
str->append(STRING_WITH_LEN(" as signed)"));
902
longlong Item_func_signed::val_int_from_str(int *error)
904
char buff[MAX_FIELD_WIDTH], *end, *start;
906
String tmp(buff,sizeof(buff), &my_charset_bin), *res;
910
For a string result, we must first get the string and then convert it
914
if (!(res= args[0]->val_str(&tmp)))
921
start= (char *)res->ptr();
922
length= res->length();
925
value= my_strtoll10(start, &end, error);
926
if (*error > 0 || end != start+ length)
929
String err_tmp(err_buff,(uint32) sizeof(err_buff), system_charset_info);
930
err_tmp.copy(start, length, system_charset_info);
931
push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
932
ER_TRUNCATED_WRONG_VALUE,
933
ER(ER_TRUNCATED_WRONG_VALUE), "INTEGER",
940
longlong Item_func_signed::val_int()
945
if (args[0]->cast_to_int_type() != STRING_RESULT ||
946
args[0]->result_as_longlong())
948
value= args[0]->val_int();
949
null_value= args[0]->null_value;
953
value= val_int_from_str(&error);
954
if (value < 0 && error == 0)
956
push_warning(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN, ER_UNKNOWN_ERROR,
957
"Cast to signed converted positive out-of-range integer to "
958
"it's negative complement");
964
void Item_func_unsigned::print(String *str, enum_query_type query_type)
966
str->append(STRING_WITH_LEN("cast("));
967
args[0]->print(str, query_type);
968
str->append(STRING_WITH_LEN(" as unsigned)"));
973
longlong Item_func_unsigned::val_int()
978
if (args[0]->cast_to_int_type() == DECIMAL_RESULT)
980
my_decimal tmp, *dec= args[0]->val_decimal(&tmp);
981
if (!(null_value= args[0]->null_value))
982
my_decimal2int(E_DEC_FATAL_ERROR, dec, 1, &value);
987
else if (args[0]->cast_to_int_type() != STRING_RESULT ||
988
args[0]->result_as_longlong())
990
value= args[0]->val_int();
991
null_value= args[0]->null_value;
995
value= val_int_from_str(&error);
997
push_warning(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN, ER_UNKNOWN_ERROR,
998
"Cast to unsigned converted negative integer to it's "
999
"positive complement");
1004
String *Item_decimal_typecast::val_str(String *str)
1006
my_decimal tmp_buf, *tmp= val_decimal(&tmp_buf);
1009
my_decimal2string(E_DEC_FATAL_ERROR, tmp, 0, 0, 0, str);
1014
double Item_decimal_typecast::val_real()
1016
my_decimal tmp_buf, *tmp= val_decimal(&tmp_buf);
1020
my_decimal2double(E_DEC_FATAL_ERROR, tmp, &res);
1025
longlong Item_decimal_typecast::val_int()
1027
my_decimal tmp_buf, *tmp= val_decimal(&tmp_buf);
1031
my_decimal2int(E_DEC_FATAL_ERROR, tmp, unsigned_flag, &res);
1036
my_decimal *Item_decimal_typecast::val_decimal(my_decimal *dec)
1038
my_decimal tmp_buf, *tmp= args[0]->val_decimal(&tmp_buf);
1042
if ((null_value= args[0]->null_value))
1044
my_decimal_round(E_DEC_FATAL_ERROR, tmp, decimals, FALSE, dec);
1050
my_decimal_set_zero(dec);
1054
precision= my_decimal_length_to_precision(max_length,
1055
decimals, unsigned_flag);
1056
if (precision - decimals < (uint) my_decimal_intg(dec))
1058
max_my_decimal(dec, precision, decimals);
1065
push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
1066
ER_WARN_DATA_OUT_OF_RANGE,
1067
ER(ER_WARN_DATA_OUT_OF_RANGE),
1073
void Item_decimal_typecast::print(String *str, enum_query_type query_type)
1075
char len_buf[20*3 + 1];
1078
uint precision= my_decimal_length_to_precision(max_length, decimals,
1080
str->append(STRING_WITH_LEN("cast("));
1081
args[0]->print(str, query_type);
1082
str->append(STRING_WITH_LEN(" as decimal("));
1084
end=int10_to_str(precision, len_buf,10);
1085
str->append(len_buf, (uint32) (end - len_buf));
1089
end=int10_to_str(decimals, len_buf,10);
1090
str->append(len_buf, (uint32) (end - len_buf));
1097
double Item_func_plus::real_op()
1099
double value= args[0]->val_real() + args[1]->val_real();
1100
if ((null_value=args[0]->null_value || args[1]->null_value))
1102
return fix_result(value);
1106
longlong Item_func_plus::int_op()
1108
longlong value=args[0]->val_int()+args[1]->val_int();
1109
if ((null_value=args[0]->null_value || args[1]->null_value))
1116
Calculate plus of two decimals.
1118
@param decimal_value Buffer that can be used to store result
1121
0 Value was NULL; In this case null_value is set
1123
\# Value of operation as a decimal
1126
my_decimal *Item_func_plus::decimal_op(my_decimal *decimal_value)
1128
my_decimal value1, *val1;
1129
my_decimal value2, *val2;
1130
val1= args[0]->val_decimal(&value1);
1131
if ((null_value= args[0]->null_value))
1133
val2= args[1]->val_decimal(&value2);
1134
if (!(null_value= (args[1]->null_value ||
1135
(my_decimal_add(E_DEC_FATAL_ERROR, decimal_value, val1,
1137
return decimal_value;
1142
Set precision of results for additive operations (+ and -)
1144
void Item_func_additive_op::result_precision()
1146
decimals= max(args[0]->decimals, args[1]->decimals);
1147
int max_int_part= max(args[0]->decimal_precision() - args[0]->decimals,
1148
args[1]->decimal_precision() - args[1]->decimals);
1149
int precision= min(max_int_part + 1 + decimals, DECIMAL_MAX_PRECISION);
1151
/* Integer operations keep unsigned_flag if one of arguments is unsigned */
1152
if (result_type() == INT_RESULT)
1153
unsigned_flag= args[0]->unsigned_flag | args[1]->unsigned_flag;
1155
unsigned_flag= args[0]->unsigned_flag & args[1]->unsigned_flag;
1156
max_length= my_decimal_precision_to_length(precision, decimals,
1162
The following function is here to allow the user to force
1163
subtraction of UNSIGNED BIGINT to return negative values.
1166
void Item_func_minus::fix_length_and_dec()
1168
Item_num_op::fix_length_and_dec();
1169
if (unsigned_flag &&
1170
(current_thd->variables.sql_mode & MODE_NO_UNSIGNED_SUBTRACTION))
1175
double Item_func_minus::real_op()
1177
double value= args[0]->val_real() - args[1]->val_real();
1178
if ((null_value=args[0]->null_value || args[1]->null_value))
1180
return fix_result(value);
1184
longlong Item_func_minus::int_op()
1186
longlong value=args[0]->val_int() - args[1]->val_int();
1187
if ((null_value=args[0]->null_value || args[1]->null_value))
1194
See Item_func_plus::decimal_op for comments.
1197
my_decimal *Item_func_minus::decimal_op(my_decimal *decimal_value)
1199
my_decimal value1, *val1;
1200
my_decimal value2, *val2=
1202
val1= args[0]->val_decimal(&value1);
1203
if ((null_value= args[0]->null_value))
1205
val2= args[1]->val_decimal(&value2);
1206
if (!(null_value= (args[1]->null_value ||
1207
(my_decimal_sub(E_DEC_FATAL_ERROR, decimal_value, val1,
1209
return decimal_value;
1214
double Item_func_mul::real_op()
1216
DBUG_ASSERT(fixed == 1);
1217
double value= args[0]->val_real() * args[1]->val_real();
1218
if ((null_value=args[0]->null_value || args[1]->null_value))
1220
return fix_result(value);
1224
longlong Item_func_mul::int_op()
1226
DBUG_ASSERT(fixed == 1);
1227
longlong value=args[0]->val_int()*args[1]->val_int();
1228
if ((null_value=args[0]->null_value || args[1]->null_value))
1234
/** See Item_func_plus::decimal_op for comments. */
1236
my_decimal *Item_func_mul::decimal_op(my_decimal *decimal_value)
1238
my_decimal value1, *val1;
1239
my_decimal value2, *val2;
1240
val1= args[0]->val_decimal(&value1);
1241
if ((null_value= args[0]->null_value))
1243
val2= args[1]->val_decimal(&value2);
1244
if (!(null_value= (args[1]->null_value ||
1245
(my_decimal_mul(E_DEC_FATAL_ERROR, decimal_value, val1,
1247
return decimal_value;
1252
void Item_func_mul::result_precision()
1254
/* Integer operations keep unsigned_flag if one of arguments is unsigned */
1255
if (result_type() == INT_RESULT)
1256
unsigned_flag= args[0]->unsigned_flag | args[1]->unsigned_flag;
1258
unsigned_flag= args[0]->unsigned_flag & args[1]->unsigned_flag;
1259
decimals= min(args[0]->decimals + args[1]->decimals, DECIMAL_MAX_SCALE);
1260
int precision= min(args[0]->decimal_precision() + args[1]->decimal_precision(),
1261
DECIMAL_MAX_PRECISION);
1262
max_length= my_decimal_precision_to_length(precision, decimals,unsigned_flag);
1266
double Item_func_div::real_op()
1268
DBUG_ASSERT(fixed == 1);
1269
double value= args[0]->val_real();
1270
double val2= args[1]->val_real();
1271
if ((null_value= args[0]->null_value || args[1]->null_value))
1275
signal_divide_by_null();
1278
return fix_result(value/val2);
1282
my_decimal *Item_func_div::decimal_op(my_decimal *decimal_value)
1284
my_decimal value1, *val1;
1285
my_decimal value2, *val2;
1288
val1= args[0]->val_decimal(&value1);
1289
if ((null_value= args[0]->null_value))
1291
val2= args[1]->val_decimal(&value2);
1292
if ((null_value= args[1]->null_value))
1294
if ((err= my_decimal_div(E_DEC_FATAL_ERROR & ~E_DEC_DIV_ZERO, decimal_value,
1295
val1, val2, prec_increment)) > 3)
1297
if (err == E_DEC_DIV_ZERO)
1298
signal_divide_by_null();
1302
return decimal_value;
1306
void Item_func_div::result_precision()
1308
uint precision=min(args[0]->decimal_precision() + prec_increment,
1309
DECIMAL_MAX_PRECISION);
1310
/* Integer operations keep unsigned_flag if one of arguments is unsigned */
1311
if (result_type() == INT_RESULT)
1312
unsigned_flag= args[0]->unsigned_flag | args[1]->unsigned_flag;
1314
unsigned_flag= args[0]->unsigned_flag & args[1]->unsigned_flag;
1315
decimals= min(args[0]->decimals + prec_increment, DECIMAL_MAX_SCALE);
1316
max_length= my_decimal_precision_to_length(precision, decimals,
1321
void Item_func_div::fix_length_and_dec()
1323
DBUG_ENTER("Item_func_div::fix_length_and_dec");
1324
prec_increment= current_thd->variables.div_precincrement;
1325
Item_num_op::fix_length_and_dec();
1326
switch(hybrid_type) {
1329
decimals=max(args[0]->decimals,args[1]->decimals)+prec_increment;
1330
set_if_smaller(decimals, NOT_FIXED_DEC);
1331
max_length=args[0]->max_length - args[0]->decimals + decimals;
1332
uint tmp=float_length(decimals);
1333
set_if_smaller(max_length,tmp);
1337
hybrid_type= DECIMAL_RESULT;
1338
DBUG_PRINT("info", ("Type changed: DECIMAL_RESULT"));
1341
case DECIMAL_RESULT:
1347
maybe_null= 1; // devision by zero
1352
/* Integer division */
1353
longlong Item_func_int_div::val_int()
1355
DBUG_ASSERT(fixed == 1);
1356
longlong value=args[0]->val_int();
1357
longlong val2=args[1]->val_int();
1358
if ((null_value= (args[0]->null_value || args[1]->null_value)))
1362
signal_divide_by_null();
1365
return (unsigned_flag ?
1366
(ulonglong) value / (ulonglong) val2 :
1371
void Item_func_int_div::fix_length_and_dec()
1373
Item_result argtype= args[0]->result_type();
1374
/* use precision ony for the data type it is applicable for and valid */
1375
max_length=args[0]->max_length -
1376
(argtype == DECIMAL_RESULT || argtype == INT_RESULT ?
1377
args[0]->decimals : 0);
1379
unsigned_flag=args[0]->unsigned_flag | args[1]->unsigned_flag;
1383
longlong Item_func_mod::int_op()
1385
DBUG_ASSERT(fixed == 1);
1386
longlong value= args[0]->val_int();
1387
longlong val2= args[1]->val_int();
1390
if ((null_value= args[0]->null_value || args[1]->null_value))
1391
return 0; /* purecov: inspected */
1394
signal_divide_by_null();
1398
if (args[0]->unsigned_flag)
1399
result= args[1]->unsigned_flag ?
1400
((ulonglong) value) % ((ulonglong) val2) : ((ulonglong) value) % val2;
1402
result= args[1]->unsigned_flag ?
1403
value % ((ulonglong) val2) : value % val2;
1408
double Item_func_mod::real_op()
1410
DBUG_ASSERT(fixed == 1);
1411
double value= args[0]->val_real();
1412
double val2= args[1]->val_real();
1413
if ((null_value= args[0]->null_value || args[1]->null_value))
1414
return 0.0; /* purecov: inspected */
1417
signal_divide_by_null();
1420
return fmod(value,val2);
1424
my_decimal *Item_func_mod::decimal_op(my_decimal *decimal_value)
1426
my_decimal value1, *val1;
1427
my_decimal value2, *val2;
1429
val1= args[0]->val_decimal(&value1);
1430
if ((null_value= args[0]->null_value))
1432
val2= args[1]->val_decimal(&value2);
1433
if ((null_value= args[1]->null_value))
1435
switch (my_decimal_mod(E_DEC_FATAL_ERROR & ~E_DEC_DIV_ZERO, decimal_value,
1437
case E_DEC_TRUNCATED:
1439
return decimal_value;
1440
case E_DEC_DIV_ZERO:
1441
signal_divide_by_null();
1449
void Item_func_mod::result_precision()
1451
decimals= max(args[0]->decimals, args[1]->decimals);
1452
max_length= max(args[0]->max_length, args[1]->max_length);
1456
void Item_func_mod::fix_length_and_dec()
1458
Item_num_op::fix_length_and_dec();
1460
unsigned_flag= args[0]->unsigned_flag;
1464
double Item_func_neg::real_op()
1466
double value= args[0]->val_real();
1467
null_value= args[0]->null_value;
1472
longlong Item_func_neg::int_op()
1474
longlong value= args[0]->val_int();
1475
null_value= args[0]->null_value;
1480
my_decimal *Item_func_neg::decimal_op(my_decimal *decimal_value)
1482
my_decimal val, *value= args[0]->val_decimal(&val);
1483
if (!(null_value= args[0]->null_value))
1485
my_decimal2decimal(value, decimal_value);
1486
my_decimal_neg(decimal_value);
1487
return decimal_value;
1493
void Item_func_neg::fix_num_length_and_dec()
1495
decimals= args[0]->decimals;
1496
/* 1 add because sign can appear */
1497
max_length= args[0]->max_length + 1;
1501
void Item_func_neg::fix_length_and_dec()
1503
DBUG_ENTER("Item_func_neg::fix_length_and_dec");
1504
Item_func_num1::fix_length_and_dec();
1507
If this is in integer context keep the context as integer if possible
1508
(This is how multiplication and other integer functions works)
1509
Use val() to get value as arg_type doesn't mean that item is
1510
Item_int or Item_real due to existence of Item_param.
1512
if (hybrid_type == INT_RESULT && args[0]->const_item())
1514
longlong val= args[0]->val_int();
1515
if ((ulonglong) val >= (ulonglong) LONGLONG_MIN &&
1516
((ulonglong) val != (ulonglong) LONGLONG_MIN ||
1517
args[0]->type() != INT_ITEM))
1520
Ensure that result is converted to DECIMAL, as longlong can't hold
1523
hybrid_type= DECIMAL_RESULT;
1524
DBUG_PRINT("info", ("Type changed: DECIMAL_RESULT"));
1532
double Item_func_abs::real_op()
1534
double value= args[0]->val_real();
1535
null_value= args[0]->null_value;
1540
longlong Item_func_abs::int_op()
1542
longlong value= args[0]->val_int();
1543
if ((null_value= args[0]->null_value))
1545
return (value >= 0) || unsigned_flag ? value : -value;
1549
my_decimal *Item_func_abs::decimal_op(my_decimal *decimal_value)
1551
my_decimal val, *value= args[0]->val_decimal(&val);
1552
if (!(null_value= args[0]->null_value))
1554
my_decimal2decimal(value, decimal_value);
1555
if (decimal_value->sign())
1556
my_decimal_neg(decimal_value);
1557
return decimal_value;
1563
void Item_func_abs::fix_length_and_dec()
1565
Item_func_num1::fix_length_and_dec();
1566
unsigned_flag= args[0]->unsigned_flag;
1570
/** Gateway to natural LOG function. */
1571
double Item_func_ln::val_real()
1573
DBUG_ASSERT(fixed == 1);
1574
double value= args[0]->val_real();
1575
if ((null_value= args[0]->null_value))
1579
signal_divide_by_null();
1586
Extended but so slower LOG function.
1588
We have to check if all values are > zero and first one is not one
1589
as these are the cases then result is not a number.
1591
double Item_func_log::val_real()
1593
DBUG_ASSERT(fixed == 1);
1594
double value= args[0]->val_real();
1595
if ((null_value= args[0]->null_value))
1599
signal_divide_by_null();
1604
double value2= args[1]->val_real();
1605
if ((null_value= args[1]->null_value))
1607
if (value2 <= 0.0 || value == 1.0)
1609
signal_divide_by_null();
1612
return log(value2) / log(value);
1617
double Item_func_log2::val_real()
1619
DBUG_ASSERT(fixed == 1);
1620
double value= args[0]->val_real();
1622
if ((null_value=args[0]->null_value))
1626
signal_divide_by_null();
1629
return log(value) / M_LN2;
1632
double Item_func_log10::val_real()
1634
DBUG_ASSERT(fixed == 1);
1635
double value= args[0]->val_real();
1636
if ((null_value= args[0]->null_value))
1640
signal_divide_by_null();
1643
return log10(value);
1646
double Item_func_exp::val_real()
1648
DBUG_ASSERT(fixed == 1);
1649
double value= args[0]->val_real();
1650
if ((null_value=args[0]->null_value))
1651
return 0.0; /* purecov: inspected */
1652
return fix_result(exp(value));
1655
double Item_func_sqrt::val_real()
1657
DBUG_ASSERT(fixed == 1);
1658
double value= args[0]->val_real();
1659
if ((null_value=(args[0]->null_value || value < 0)))
1660
return 0.0; /* purecov: inspected */
1664
double Item_func_pow::val_real()
1666
DBUG_ASSERT(fixed == 1);
1667
double value= args[0]->val_real();
1668
double val2= args[1]->val_real();
1669
if ((null_value=(args[0]->null_value || args[1]->null_value)))
1670
return 0.0; /* purecov: inspected */
1671
return fix_result(pow(value,val2));
1674
// Trigonometric functions
1676
double Item_func_acos::val_real()
1678
DBUG_ASSERT(fixed == 1);
1679
// the volatile's for BUG #2338 to calm optimizer down (because of gcc's bug)
1680
volatile double value= args[0]->val_real();
1681
if ((null_value=(args[0]->null_value || (value < -1.0 || value > 1.0))))
1686
double Item_func_asin::val_real()
1688
DBUG_ASSERT(fixed == 1);
1689
// the volatile's for BUG #2338 to calm optimizer down (because of gcc's bug)
1690
volatile double value= args[0]->val_real();
1691
if ((null_value=(args[0]->null_value || (value < -1.0 || value > 1.0))))
1696
double Item_func_atan::val_real()
1698
DBUG_ASSERT(fixed == 1);
1699
double value= args[0]->val_real();
1700
if ((null_value=args[0]->null_value))
1704
double val2= args[1]->val_real();
1705
if ((null_value=args[1]->null_value))
1707
return fix_result(atan2(value,val2));
1712
double Item_func_cos::val_real()
1714
DBUG_ASSERT(fixed == 1);
1715
double value= args[0]->val_real();
1716
if ((null_value=args[0]->null_value))
1721
double Item_func_sin::val_real()
1723
DBUG_ASSERT(fixed == 1);
1724
double value= args[0]->val_real();
1725
if ((null_value=args[0]->null_value))
1730
double Item_func_tan::val_real()
1732
DBUG_ASSERT(fixed == 1);
1733
double value= args[0]->val_real();
1734
if ((null_value=args[0]->null_value))
1736
return fix_result(tan(value));
1740
// Shift-functions, same as << and >> in C/C++
1743
longlong Item_func_shift_left::val_int()
1745
DBUG_ASSERT(fixed == 1);
1747
ulonglong res= ((ulonglong) args[0]->val_int() <<
1748
(shift=(uint) args[1]->val_int()));
1749
if (args[0]->null_value || args[1]->null_value)
1755
return (shift < sizeof(longlong)*8 ? (longlong) res : LL(0));
1758
longlong Item_func_shift_right::val_int()
1760
DBUG_ASSERT(fixed == 1);
1762
ulonglong res= (ulonglong) args[0]->val_int() >>
1763
(shift=(uint) args[1]->val_int());
1764
if (args[0]->null_value || args[1]->null_value)
1770
return (shift < sizeof(longlong)*8 ? (longlong) res : LL(0));
1774
longlong Item_func_bit_neg::val_int()
1776
DBUG_ASSERT(fixed == 1);
1777
ulonglong res= (ulonglong) args[0]->val_int();
1778
if ((null_value=args[0]->null_value))
1784
// Conversion functions
1786
void Item_func_integer::fix_length_and_dec()
1788
max_length=args[0]->max_length - args[0]->decimals+1;
1789
uint tmp=float_length(decimals);
1790
set_if_smaller(max_length,tmp);
1794
void Item_func_int_val::fix_num_length_and_dec()
1796
max_length= args[0]->max_length - (args[0]->decimals ?
1797
args[0]->decimals + 1 :
1799
uint tmp= float_length(decimals);
1800
set_if_smaller(max_length,tmp);
1805
void Item_func_int_val::find_num_type()
1807
DBUG_ENTER("Item_func_int_val::find_num_type");
1808
DBUG_PRINT("info", ("name %s", func_name()));
1809
switch(hybrid_type= args[0]->result_type())
1813
hybrid_type= REAL_RESULT;
1814
max_length= float_length(decimals);
1817
case DECIMAL_RESULT:
1819
-2 because in most high position can't be used any digit for longlong
1820
and one position for increasing value during operation
1822
if ((args[0]->max_length - args[0]->decimals) >=
1823
(DECIMAL_LONGLONG_DIGITS - 2))
1825
hybrid_type= DECIMAL_RESULT;
1829
unsigned_flag= args[0]->unsigned_flag;
1830
hybrid_type= INT_RESULT;
1836
DBUG_PRINT("info", ("Type: %s",
1837
(hybrid_type == REAL_RESULT ? "REAL_RESULT" :
1838
hybrid_type == DECIMAL_RESULT ? "DECIMAL_RESULT" :
1839
hybrid_type == INT_RESULT ? "INT_RESULT" :
1840
"--ILLEGAL!!!--")));
1846
longlong Item_func_ceiling::int_op()
1849
switch (args[0]->result_type()) {
1851
result= args[0]->val_int();
1852
null_value= args[0]->null_value;
1854
case DECIMAL_RESULT:
1856
my_decimal dec_buf, *dec;
1857
if ((dec= Item_func_ceiling::decimal_op(&dec_buf)))
1858
my_decimal2int(E_DEC_FATAL_ERROR, dec, unsigned_flag, &result);
1864
result= (longlong)Item_func_ceiling::real_op();
1870
double Item_func_ceiling::real_op()
1873
the volatile's for BUG #3051 to calm optimizer down (because of gcc's
1876
volatile double value= args[0]->val_real();
1877
null_value= args[0]->null_value;
1882
my_decimal *Item_func_ceiling::decimal_op(my_decimal *decimal_value)
1884
my_decimal val, *value= args[0]->val_decimal(&val);
1885
if (!(null_value= (args[0]->null_value ||
1886
my_decimal_ceiling(E_DEC_FATAL_ERROR, value,
1887
decimal_value) > 1)))
1888
return decimal_value;
1893
longlong Item_func_floor::int_op()
1896
switch (args[0]->result_type()) {
1898
result= args[0]->val_int();
1899
null_value= args[0]->null_value;
1901
case DECIMAL_RESULT:
1903
my_decimal dec_buf, *dec;
1904
if ((dec= Item_func_floor::decimal_op(&dec_buf)))
1905
my_decimal2int(E_DEC_FATAL_ERROR, dec, unsigned_flag, &result);
1911
result= (longlong)Item_func_floor::real_op();
1917
double Item_func_floor::real_op()
1920
the volatile's for BUG #3051 to calm optimizer down (because of gcc's
1923
volatile double value= args[0]->val_real();
1924
null_value= args[0]->null_value;
1925
return floor(value);
1929
my_decimal *Item_func_floor::decimal_op(my_decimal *decimal_value)
1931
my_decimal val, *value= args[0]->val_decimal(&val);
1932
if (!(null_value= (args[0]->null_value ||
1933
my_decimal_floor(E_DEC_FATAL_ERROR, value,
1934
decimal_value) > 1)))
1935
return decimal_value;
1940
void Item_func_round::fix_length_and_dec()
1942
int decimals_to_set;
1946
unsigned_flag= args[0]->unsigned_flag;
1947
if (!args[1]->const_item())
1949
max_length= args[0]->max_length;
1950
decimals= args[0]->decimals;
1951
if (args[0]->result_type() == DECIMAL_RESULT)
1954
hybrid_type= DECIMAL_RESULT;
1957
hybrid_type= REAL_RESULT;
1961
val1= args[1]->val_int();
1962
val1_unsigned= args[1]->unsigned_flag;
1964
decimals_to_set= val1_unsigned ? INT_MAX : 0;
1966
decimals_to_set= (val1 > INT_MAX) ? INT_MAX : (int) val1;
1968
if (args[0]->decimals == NOT_FIXED_DEC)
1970
max_length= args[0]->max_length;
1971
decimals= min(decimals_to_set, NOT_FIXED_DEC);
1972
hybrid_type= REAL_RESULT;
1976
switch (args[0]->result_type()) {
1979
hybrid_type= REAL_RESULT;
1980
decimals= min(decimals_to_set, NOT_FIXED_DEC);
1981
max_length= float_length(decimals);
1984
if ((!decimals_to_set && truncate) || (args[0]->decimal_precision() < DECIMAL_LONGLONG_DIGITS))
1986
int length_can_increase= test(!truncate && (val1 < 0) && !val1_unsigned);
1987
max_length= args[0]->max_length + length_can_increase;
1988
/* Here we can keep INT_RESULT */
1989
hybrid_type= INT_RESULT;
1994
case DECIMAL_RESULT:
1996
hybrid_type= DECIMAL_RESULT;
1997
decimals_to_set= min(DECIMAL_MAX_SCALE, decimals_to_set);
1998
int decimals_delta= args[0]->decimals - decimals_to_set;
1999
int precision= args[0]->decimal_precision();
2000
int length_increase= ((decimals_delta <= 0) || truncate) ? 0:1;
2002
precision-= decimals_delta - length_increase;
2003
decimals= min(decimals_to_set, DECIMAL_MAX_SCALE);
2004
max_length= my_decimal_precision_to_length(precision, decimals,
2009
DBUG_ASSERT(0); /* This result type isn't handled */
2013
double my_double_round(double value, longlong dec, bool dec_unsigned,
2017
bool dec_negative= (dec < 0) && !dec_unsigned;
2018
ulonglong abs_dec= dec_negative ? -dec : dec;
2020
tmp2 is here to avoid return the value with 80 bit precision
2021
This will fix that the test round(0.1,1) = round(0.1,1) is true
2023
volatile double tmp2;
2025
tmp=(abs_dec < array_elements(log_10) ?
2026
log_10[abs_dec] : pow(10.0,(double) abs_dec));
2028
if (dec_negative && my_isinf(tmp))
2030
else if (!dec_negative && my_isinf(value * tmp))
2035
tmp2= dec < 0 ? floor(value/tmp)*tmp : floor(value*tmp)/tmp;
2037
tmp2= dec < 0 ? ceil(value/tmp)*tmp : ceil(value*tmp)/tmp;
2040
tmp2=dec < 0 ? rint(value/tmp)*tmp : rint(value*tmp)/tmp;
2045
double Item_func_round::real_op()
2047
double value= args[0]->val_real();
2049
if (!(null_value= args[0]->null_value || args[1]->null_value))
2050
return my_double_round(value, args[1]->val_int(), args[1]->unsigned_flag,
2057
Rounds a given value to a power of 10 specified as the 'to' argument,
2058
avoiding overflows when the value is close to the ulonglong range boundary.
2061
static inline ulonglong my_unsigned_round(ulonglong value, ulonglong to)
2063
ulonglong tmp= value / to * to;
2064
return (value - tmp < (to >> 1)) ? tmp : tmp + to;
2068
longlong Item_func_round::int_op()
2070
longlong value= args[0]->val_int();
2071
longlong dec= args[1]->val_int();
2074
if ((null_value= args[0]->null_value || args[1]->null_value))
2076
if ((dec >= 0) || args[1]->unsigned_flag)
2077
return value; // integer have not digits after point
2082
if(abs_dec >= array_elements(log_10_int))
2085
tmp= log_10_int[abs_dec];
2088
value= (unsigned_flag) ?
2089
((ulonglong) value / tmp) * tmp : (value / tmp) * tmp;
2091
value= (unsigned_flag || value >= 0) ?
2092
my_unsigned_round((ulonglong) value, tmp) :
2093
-(longlong) my_unsigned_round((ulonglong) -value, tmp);
2098
my_decimal *Item_func_round::decimal_op(my_decimal *decimal_value)
2100
my_decimal val, *value= args[0]->val_decimal(&val);
2101
longlong dec= args[1]->val_int();
2102
if (dec >= 0 || args[1]->unsigned_flag)
2103
dec= min((ulonglong) dec, decimals);
2104
else if (dec < INT_MIN)
2107
if (!(null_value= (args[0]->null_value || args[1]->null_value ||
2108
my_decimal_round(E_DEC_FATAL_ERROR, value, (int) dec,
2109
truncate, decimal_value) > 1)))
2111
decimal_value->frac= decimals;
2112
return decimal_value;
2118
void Item_func_rand::seed_random(Item *arg)
2121
TODO: do not do reinit 'rand' for every execute of PS/SP if
2122
args[0] is a constant.
2124
uint32 tmp= (uint32) arg->val_int();
2125
randominit(rand, (uint32) (tmp*0x10001L+55555555L),
2126
(uint32) (tmp*0x10000001L));
2130
bool Item_func_rand::fix_fields(THD *thd,Item **ref)
2132
if (Item_real_func::fix_fields(thd, ref))
2134
used_tables_cache|= RAND_TABLE_BIT;
2136
{ // Only use argument once in query
2138
Allocate rand structure once: we must use thd->stmt_arena
2139
to create rand in proper mem_root if it's a prepared statement or
2142
No need to send a Rand log event if seed was given eg: RAND(seed),
2143
as it will be replicated in the query as such.
2145
if (!rand && !(rand= (struct rand_struct*)
2146
thd->stmt_arena->alloc(sizeof(*rand))))
2149
if (args[0]->const_item())
2150
seed_random (args[0]);
2155
Save the seed only the first time RAND() is used in the query
2156
Once events are forwarded rather than recreated,
2157
the following can be skipped if inside the slave thread
2159
if (!thd->rand_used)
2162
thd->rand_saved_seed1= thd->rand.seed1;
2163
thd->rand_saved_seed2= thd->rand.seed2;
2170
void Item_func_rand::update_used_tables()
2172
Item_real_func::update_used_tables();
2173
used_tables_cache|= RAND_TABLE_BIT;
2177
double Item_func_rand::val_real()
2179
DBUG_ASSERT(fixed == 1);
2180
if (arg_count && !args[0]->const_item())
2181
seed_random (args[0]);
2182
return my_rnd(rand);
2185
longlong Item_func_sign::val_int()
2187
DBUG_ASSERT(fixed == 1);
2188
double value= args[0]->val_real();
2189
null_value=args[0]->null_value;
2190
return value < 0.0 ? -1 : (value > 0 ? 1 : 0);
2194
double Item_func_units::val_real()
2196
DBUG_ASSERT(fixed == 1);
2197
double value= args[0]->val_real();
2198
if ((null_value=args[0]->null_value))
2200
return value*mul+add;
2204
void Item_func_min_max::fix_length_and_dec()
2207
bool datetime_found= FALSE;
2211
cmp_type=args[0]->result_type();
2213
for (uint i=0 ; i < arg_count ; i++)
2215
set_if_bigger(max_length, args[i]->max_length);
2216
set_if_bigger(decimals, args[i]->decimals);
2217
set_if_bigger(max_int_part, args[i]->decimal_int_part());
2218
if (args[i]->maybe_null)
2220
cmp_type=item_cmp_type(cmp_type,args[i]->result_type());
2221
if (args[i]->result_type() != ROW_RESULT && args[i]->is_datetime())
2223
datetime_found= TRUE;
2224
if (!datetime_item || args[i]->field_type() == MYSQL_TYPE_DATETIME)
2225
datetime_item= args[i];
2228
if (cmp_type == STRING_RESULT)
2230
agg_arg_charsets(collation, args, arg_count, MY_COLL_CMP_CONV, 1);
2234
compare_as_dates= TRUE;
2237
else if ((cmp_type == DECIMAL_RESULT) || (cmp_type == INT_RESULT))
2238
max_length= my_decimal_precision_to_length(max_int_part+decimals, decimals,
2240
cached_field_type= agg_field_type(args, arg_count);
2245
Compare item arguments in the DATETIME context.
2249
value [out] found least/greatest DATE/DATETIME value
2252
Compare item arguments as DATETIME values and return the index of the
2253
least/greatest argument in the arguments array.
2254
The correct integer DATE/DATETIME value of the found argument is
2255
stored to the value pointer, if latter is provided.
2258
0 If one of arguments is NULL
2259
# index of the least/greatest argument
2262
uint Item_func_min_max::cmp_datetimes(ulonglong *value)
2264
ulonglong min_max= 0;
2265
uint min_max_idx= 0;
2267
for (uint i=0; i < arg_count ; i++)
2269
Item **arg= args + i;
2271
ulonglong res= get_datetime_value(thd, &arg, 0, datetime_item, &is_null);
2272
if ((null_value= args[i]->null_value))
2274
if (i == 0 || (res < min_max ? cmp_sign : -cmp_sign) > 0)
2283
if (datetime_item->field_type() == MYSQL_TYPE_DATE)
2290
String *Item_func_min_max::val_str(String *str)
2292
DBUG_ASSERT(fixed == 1);
2293
if (compare_as_dates)
2296
uint min_max_idx= cmp_datetimes(NULL);
2299
str_res= args[min_max_idx]->val_str(str);
2300
str_res->set_charset(collation.collation);
2306
longlong nr=val_int();
2309
str->set_int(nr, unsigned_flag, &my_charset_bin);
2312
case DECIMAL_RESULT:
2314
my_decimal dec_buf, *dec_val= val_decimal(&dec_buf);
2317
my_decimal2string(E_DEC_FATAL_ERROR, dec_val, 0, 0, 0, str);
2322
double nr= val_real();
2324
return 0; /* purecov: inspected */
2325
str->set_real(nr,decimals,&my_charset_bin);
2332
for (uint i=0; i < arg_count ; i++)
2335
res=args[i]->val_str(str);
2339
res2= args[i]->val_str(res == str ? &tmp_value : str);
2342
int cmp= sortcmp(res,res2,collation.collation);
2343
if ((cmp_sign < 0 ? cmp : -cmp) < 0)
2347
if ((null_value= args[i]->null_value))
2350
res->set_charset(collation.collation);
2355
// This case should never be chosen
2359
return 0; // Keep compiler happy
2363
double Item_func_min_max::val_real()
2365
DBUG_ASSERT(fixed == 1);
2367
if (compare_as_dates)
2369
ulonglong result= 0;
2370
(void)cmp_datetimes(&result);
2371
return (double)result;
2373
for (uint i=0; i < arg_count ; i++)
2376
value= args[i]->val_real();
2379
double tmp= args[i]->val_real();
2380
if (!args[i]->null_value && (tmp < value ? cmp_sign : -cmp_sign) > 0)
2383
if ((null_value= args[i]->null_value))
2390
longlong Item_func_min_max::val_int()
2392
DBUG_ASSERT(fixed == 1);
2394
if (compare_as_dates)
2396
ulonglong result= 0;
2397
(void)cmp_datetimes(&result);
2398
return (longlong)result;
2400
for (uint i=0; i < arg_count ; i++)
2403
value=args[i]->val_int();
2406
longlong tmp=args[i]->val_int();
2407
if (!args[i]->null_value && (tmp < value ? cmp_sign : -cmp_sign) > 0)
2410
if ((null_value= args[i]->null_value))
2417
my_decimal *Item_func_min_max::val_decimal(my_decimal *dec)
2419
DBUG_ASSERT(fixed == 1);
2420
my_decimal tmp_buf, *tmp, *res= NULL;
2422
if (compare_as_dates)
2425
(void)cmp_datetimes(&value);
2426
ulonglong2decimal(value, dec);
2429
for (uint i=0; i < arg_count ; i++)
2432
res= args[i]->val_decimal(dec);
2435
tmp= args[i]->val_decimal(&tmp_buf); // Zero if NULL
2436
if (tmp && (my_decimal_cmp(tmp, res) * cmp_sign) < 0)
2438
if (tmp == &tmp_buf)
2440
/* Move value out of tmp_buf as this will be reused on next loop */
2441
my_decimal2decimal(tmp, dec);
2448
if ((null_value= args[i]->null_value))
2458
longlong Item_func_length::val_int()
2460
DBUG_ASSERT(fixed == 1);
2461
String *res=args[0]->val_str(&value);
2465
return 0; /* purecov: inspected */
2468
return (longlong) res->length();
2472
longlong Item_func_char_length::val_int()
2474
DBUG_ASSERT(fixed == 1);
2475
String *res=args[0]->val_str(&value);
2479
return 0; /* purecov: inspected */
2482
return (longlong) res->numchars();
2486
longlong Item_func_coercibility::val_int()
2488
DBUG_ASSERT(fixed == 1);
2490
return (longlong) args[0]->collation.derivation;
2494
void Item_func_locate::fix_length_and_dec()
2496
max_length= MY_INT32_NUM_DECIMAL_DIGITS;
2497
agg_arg_charsets(cmp_collation, args, 2, MY_COLL_CMP_CONV, 1);
2501
longlong Item_func_locate::val_int()
2503
DBUG_ASSERT(fixed == 1);
2504
String *a=args[0]->val_str(&value1);
2505
String *b=args[1]->val_str(&value2);
2509
return 0; /* purecov: inspected */
2512
/* must be longlong to avoid truncation */
2519
start0= start= args[2]->val_int() - 1;
2521
if ((start < 0) || (start > a->length()))
2524
/* start is now sufficiently valid to pass to charpos function */
2525
start= a->charpos((int) start);
2527
if (start + b->length() > a->length())
2531
if (!b->length()) // Found empty string at start
2534
if (!cmp_collation.collation->coll->instr(cmp_collation.collation,
2536
(uint) (a->length()-start),
2537
b->ptr(), b->length(),
2540
return (longlong) match.mb_len + start0 + 1;
2544
void Item_func_locate::print(String *str, enum_query_type query_type)
2546
str->append(STRING_WITH_LEN("locate("));
2547
args[1]->print(str, query_type);
2549
args[0]->print(str, query_type);
2553
args[2]->print(str, query_type);
2559
longlong Item_func_field::val_int()
2561
DBUG_ASSERT(fixed == 1);
2563
if (cmp_type == STRING_RESULT)
2566
if (!(field= args[0]->val_str(&value)))
2568
for (uint i=1 ; i < arg_count ; i++)
2570
String *tmp_value=args[i]->val_str(&tmp);
2571
if (tmp_value && !sortcmp(field,tmp_value,cmp_collation.collation))
2572
return (longlong) (i);
2575
else if (cmp_type == INT_RESULT)
2577
longlong val= args[0]->val_int();
2578
if (args[0]->null_value)
2580
for (uint i=1; i < arg_count ; i++)
2582
if (val == args[i]->val_int() && !args[i]->null_value)
2583
return (longlong) (i);
2586
else if (cmp_type == DECIMAL_RESULT)
2588
my_decimal dec_arg_buf, *dec_arg,
2589
dec_buf, *dec= args[0]->val_decimal(&dec_buf);
2590
if (args[0]->null_value)
2592
for (uint i=1; i < arg_count; i++)
2594
dec_arg= args[i]->val_decimal(&dec_arg_buf);
2595
if (!args[i]->null_value && !my_decimal_cmp(dec_arg, dec))
2596
return (longlong) (i);
2601
double val= args[0]->val_real();
2602
if (args[0]->null_value)
2604
for (uint i=1; i < arg_count ; i++)
2606
if (val == args[i]->val_real() && !args[i]->null_value)
2607
return (longlong) (i);
2614
void Item_func_field::fix_length_and_dec()
2616
maybe_null=0; max_length=3;
2617
cmp_type= args[0]->result_type();
2618
for (uint i=1; i < arg_count ; i++)
2619
cmp_type= item_cmp_type(cmp_type, args[i]->result_type());
2620
if (cmp_type == STRING_RESULT)
2621
agg_arg_charsets(cmp_collation, args, arg_count, MY_COLL_CMP_CONV, 1);
2625
longlong Item_func_ascii::val_int()
2627
DBUG_ASSERT(fixed == 1);
2628
String *res=args[0]->val_str(&value);
2635
return (longlong) (res->length() ? (uchar) (*res)[0] : (uchar) 0);
2638
longlong Item_func_ord::val_int()
2640
DBUG_ASSERT(fixed == 1);
2641
String *res=args[0]->val_str(&value);
2648
if (!res->length()) return 0;
2650
if (use_mb(res->charset()))
2652
register const char *str=res->ptr();
2653
register uint32 n=0, l=my_ismbchar(res->charset(),str,str+res->length());
2655
return (longlong)((uchar) *str);
2657
n=(n<<8)|(uint32)((uchar) *str++);
2658
return (longlong) n;
2661
return (longlong) ((uchar) (*res)[0]);
2664
/* Search after a string in a string of strings separated by ',' */
2665
/* Returns number of found type >= 1 or 0 if not found */
2666
/* This optimizes searching in enums to bit testing! */
2668
void Item_func_find_in_set::fix_length_and_dec()
2671
max_length=3; // 1-999
2672
if (args[0]->const_item() && args[1]->type() == FIELD_ITEM)
2674
Field *field= ((Item_field*) args[1])->field;
2675
if (field->real_type() == MYSQL_TYPE_SET)
2677
String *find=args[0]->val_str(&value);
2680
enum_value= find_type(((Field_enum*) field)->typelib,find->ptr(),
2684
enum_bit=LL(1) << (enum_value-1);
2688
agg_arg_charsets(cmp_collation, args, 2, MY_COLL_CMP_CONV, 1);
2691
static const char separator=',';
2693
longlong Item_func_find_in_set::val_int()
2695
DBUG_ASSERT(fixed == 1);
2698
ulonglong tmp=(ulonglong) args[1]->val_int();
2699
if (!(null_value=args[1]->null_value || args[0]->null_value))
2707
String *find=args[0]->val_str(&value);
2708
String *buffer=args[1]->val_str(&value2);
2709
if (!find || !buffer)
2712
return 0; /* purecov: inspected */
2717
if ((diff=buffer->length() - find->length()) >= 0)
2720
CHARSET_INFO *cs= cmp_collation.collation;
2721
const char *str_begin= buffer->ptr();
2722
const char *str_end= buffer->ptr();
2723
const char *real_end= str_end+buffer->length();
2724
const uchar *find_str= (const uchar *) find->ptr();
2725
uint find_str_len= find->length();
2730
if ((symbol_len= cs->cset->mb_wc(cs, &wc, (uchar*) str_end,
2731
(uchar*) real_end)) > 0)
2733
const char *substr_end= str_end + symbol_len;
2734
bool is_last_item= (substr_end == real_end);
2735
bool is_separator= (wc == (my_wc_t) separator);
2736
if (is_separator || is_last_item)
2739
if (is_last_item && !is_separator)
2740
str_end= substr_end;
2741
if (!my_strnncoll(cs, (const uchar *) str_begin,
2742
str_end - str_begin,
2743
find_str, find_str_len))
2744
return (longlong) position;
2746
str_begin= substr_end;
2748
str_end= substr_end;
2750
else if (str_end - str_begin == 0 &&
2751
find_str_len == 0 &&
2752
wc == (my_wc_t) separator)
2753
return (longlong) ++position;
2761
longlong Item_func_bit_count::val_int()
2763
DBUG_ASSERT(fixed == 1);
2764
ulonglong value= (ulonglong) args[0]->val_int();
2765
if ((null_value= args[0]->null_value))
2766
return 0; /* purecov: inspected */
2767
return (longlong) my_count_bits(value);
2771
/****************************************************************************
2772
** Functions to handle dynamic loadable functions
2773
** Original source by: Alexis Mikhailov <root@medinf.chuvashia.su>
2774
** Rewritten by monty.
2775
****************************************************************************/
2779
void udf_handler::cleanup()
2785
if (u_d->func_deinit != NULL)
2787
Udf_func_deinit deinit= u_d->func_deinit;
2793
if (buffers) // Because of bug in ecc
2801
udf_handler::fix_fields(THD *thd, Item_result_field *func,
2802
uint arg_count, Item **arguments)
2804
uchar buff[STACK_BUFF_ALLOC]; // Max argument in function
2805
DBUG_ENTER("Item_udf_func::fix_fields");
2807
if (check_stack_overrun(thd, STACK_MIN_SIZE, buff))
2808
DBUG_RETURN(TRUE); // Fatal error flag is set!
2810
udf_func *tmp_udf=find_udf(u_d->name.str,(uint) u_d->name.length,1);
2814
my_error(ER_CANT_FIND_UDF, MYF(0), u_d->name.str, errno);
2820
/* Fix all arguments */
2822
used_tables_cache=0;
2825
if ((f_args.arg_count=arg_count))
2827
if (!(f_args.arg_type= (Item_result*)
2828
sql_alloc(f_args.arg_count*sizeof(Item_result))))
2835
Item **arg,**arg_end;
2836
for (i=0, arg=arguments, arg_end=arguments+arg_count;
2840
if (!(*arg)->fixed &&
2841
(*arg)->fix_fields(thd, arg))
2843
// we can't assign 'item' before, because fix_fields() can change arg
2845
if (item->check_cols(1))
2848
TODO: We should think about this. It is not always
2849
right way just to set an UDF result to return my_charset_bin
2850
if one argument has binary sorting order.
2851
The result collation should be calculated according to arguments
2852
derivations in some cases and should not in other cases.
2853
Moreover, some arguments can represent a numeric input
2854
which doesn't effect the result character set and collation.
2855
There is no a general rule for UDF. Everything depends on
2856
the particular user defined function.
2858
if (item->collation.collation->state & MY_CS_BINSORT)
2859
func->collation.set(&my_charset_bin);
2860
if (item->maybe_null)
2862
func->with_sum_func= func->with_sum_func || item->with_sum_func;
2863
used_tables_cache|=item->used_tables();
2864
const_item_cache&=item->const_item();
2865
f_args.arg_type[i]=item->result_type();
2867
//TODO: why all following memory is not allocated with 1 call of sql_alloc?
2868
if (!(buffers=new String[arg_count]) ||
2869
!(f_args.args= (char**) sql_alloc(arg_count * sizeof(char *))) ||
2870
!(f_args.lengths= (ulong*) sql_alloc(arg_count * sizeof(long))) ||
2871
!(f_args.maybe_null= (char*) sql_alloc(arg_count * sizeof(char))) ||
2872
!(num_buffer= (char*) sql_alloc(arg_count *
2873
ALIGN_SIZE(sizeof(double)))) ||
2874
!(f_args.attributes= (char**) sql_alloc(arg_count * sizeof(char *))) ||
2875
!(f_args.attribute_lengths= (ulong*) sql_alloc(arg_count *
2882
func->fix_length_and_dec();
2883
initid.max_length=func->max_length;
2884
initid.maybe_null=func->maybe_null;
2885
initid.const_item=const_item_cache;
2886
initid.decimals=func->decimals;
2891
char init_msg_buff[MYSQL_ERRMSG_SIZE];
2892
char *to=num_buffer;
2893
for (uint i=0; i < arg_count; i++)
2896
For a constant argument i, args->args[i] points to the argument value.
2897
For non-constant, args->args[i] is NULL.
2899
f_args.args[i]= NULL; /* Non-const unless updated below. */
2901
f_args.lengths[i]= arguments[i]->max_length;
2902
f_args.maybe_null[i]= (char) arguments[i]->maybe_null;
2903
f_args.attributes[i]= arguments[i]->name;
2904
f_args.attribute_lengths[i]= arguments[i]->name_length;
2906
if (arguments[i]->const_item())
2908
switch (arguments[i]->result_type())
2911
case DECIMAL_RESULT:
2913
String *res= arguments[i]->val_str(&buffers[i]);
2914
if (arguments[i]->null_value)
2916
f_args.args[i]= (char*) res->c_ptr();
2917
f_args.lengths[i]= res->length();
2921
*((longlong*) to)= arguments[i]->val_int();
2922
if (arguments[i]->null_value)
2925
to+= ALIGN_SIZE(sizeof(longlong));
2928
*((double*) to)= arguments[i]->val_real();
2929
if (arguments[i]->null_value)
2932
to+= ALIGN_SIZE(sizeof(double));
2936
// This case should never be chosen
2942
Udf_func_init init= u_d->func_init;
2943
if ((error=(uchar) init(&initid, &f_args, init_msg_buff)))
2945
my_error(ER_CANT_INITIALIZE_UDF, MYF(0),
2946
u_d->name.str, init_msg_buff);
2950
func->max_length=min(initid.max_length,MAX_BLOB_WIDTH);
2951
func->maybe_null=initid.maybe_null;
2952
const_item_cache=initid.const_item;
2954
Keep used_tables_cache in sync with const_item_cache.
2955
See the comment in Item_udf_func::update_used tables.
2957
if (!const_item_cache && !used_tables_cache)
2958
used_tables_cache= RAND_TABLE_BIT;
2959
func->decimals=min(initid.decimals,NOT_FIXED_DEC);
2964
my_error(ER_CANT_INITIALIZE_UDF, MYF(0),
2965
u_d->name.str, ER(ER_UNKNOWN_ERROR));
2972
bool udf_handler::get_arguments()
2975
return 1; // Got an error earlier
2976
char *to= num_buffer;
2978
for (uint i=0; i < f_args.arg_count; i++)
2981
switch (f_args.arg_type[i]) {
2983
case DECIMAL_RESULT:
2985
String *res=args[i]->val_str(&buffers[str_count++]);
2986
if (!(args[i]->null_value))
2988
f_args.args[i]= (char*) res->ptr();
2989
f_args.lengths[i]= res->length();
2994
*((longlong*) to) = args[i]->val_int();
2995
if (!args[i]->null_value)
2998
to+= ALIGN_SIZE(sizeof(longlong));
3002
*((double*) to)= args[i]->val_real();
3003
if (!args[i]->null_value)
3006
to+= ALIGN_SIZE(sizeof(double));
3011
// This case should never be chosen
3021
(String*)NULL in case of NULL values
3023
String *udf_handler::val_str(String *str,String *save_str)
3025
uchar is_null_tmp=0;
3027
DBUG_ENTER("udf_handler::val_str");
3029
if (get_arguments())
3031
char * (*func)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *)=
3032
(char* (*)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *))
3035
if ((res_length=str->alloced_length()) < MAX_FIELD_WIDTH)
3036
{ // This happens VERY seldom
3037
if (str->alloc(MAX_FIELD_WIDTH))
3043
char *res=func(&initid, &f_args, (char*) str->ptr(), &res_length,
3044
&is_null_tmp, &error);
3045
DBUG_PRINT("info", ("udf func returned, res_length: %lu", res_length));
3046
if (is_null_tmp || !res || error) // The !res is for safety
3048
DBUG_PRINT("info", ("Null or error"));
3051
if (res == str->ptr())
3053
str->length(res_length);
3054
DBUG_PRINT("exit", ("str: %s", str->ptr()));
3057
save_str->set(res, res_length, str->charset());
3058
DBUG_PRINT("exit", ("save_str: %s", save_str->ptr()));
3059
DBUG_RETURN(save_str);
3064
For the moment, UDF functions are returning DECIMAL values as strings
3067
my_decimal *udf_handler::val_decimal(my_bool *null_value, my_decimal *dec_buf)
3069
char buf[DECIMAL_MAX_STR_LENGTH+1], *end;
3070
ulong res_length= DECIMAL_MAX_STR_LENGTH;
3072
if (get_arguments())
3077
char *(*func)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *)=
3078
(char* (*)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *))
3081
char *res= func(&initid, &f_args, buf, &res_length, &is_null, &error);
3082
if (is_null || error)
3087
end= res+ res_length;
3088
str2my_decimal(E_DEC_FATAL_ERROR, res, dec_buf, &end);
3093
void Item_udf_func::cleanup()
3096
Item_func::cleanup();
3100
void Item_udf_func::print(String *str, enum_query_type query_type)
3102
str->append(func_name());
3104
for (uint i=0 ; i < arg_count ; i++)
3108
args[i]->print_item_w_name(str, query_type);
3114
double Item_func_udf_float::val_real()
3116
DBUG_ASSERT(fixed == 1);
3117
DBUG_ENTER("Item_func_udf_float::val");
3118
DBUG_PRINT("info",("result_type: %d arg_count: %d",
3119
args[0]->result_type(), arg_count));
3120
DBUG_RETURN(udf.val(&null_value));
3124
String *Item_func_udf_float::val_str(String *str)
3126
DBUG_ASSERT(fixed == 1);
3127
double nr= val_real();
3129
return 0; /* purecov: inspected */
3130
str->set_real(nr,decimals,&my_charset_bin);
3135
longlong Item_func_udf_int::val_int()
3137
DBUG_ASSERT(fixed == 1);
3138
DBUG_ENTER("Item_func_udf_int::val_int");
3139
DBUG_RETURN(udf.val_int(&null_value));
3143
String *Item_func_udf_int::val_str(String *str)
3145
DBUG_ASSERT(fixed == 1);
3146
longlong nr=val_int();
3149
str->set_int(nr, unsigned_flag, &my_charset_bin);
3154
longlong Item_func_udf_decimal::val_int()
3156
my_decimal dec_buf, *dec= udf.val_decimal(&null_value, &dec_buf);
3160
my_decimal2int(E_DEC_FATAL_ERROR, dec, unsigned_flag, &result);
3165
double Item_func_udf_decimal::val_real()
3167
my_decimal dec_buf, *dec= udf.val_decimal(&null_value, &dec_buf);
3171
my_decimal2double(E_DEC_FATAL_ERROR, dec, &result);
3176
my_decimal *Item_func_udf_decimal::val_decimal(my_decimal *dec_buf)
3178
DBUG_ASSERT(fixed == 1);
3179
DBUG_ENTER("Item_func_udf_decimal::val_decimal");
3180
DBUG_PRINT("info",("result_type: %d arg_count: %d",
3181
args[0]->result_type(), arg_count));
3183
DBUG_RETURN(udf.val_decimal(&null_value, dec_buf));
3187
String *Item_func_udf_decimal::val_str(String *str)
3189
my_decimal dec_buf, *dec= udf.val_decimal(&null_value, &dec_buf);
3192
if (str->length() < DECIMAL_MAX_STR_LENGTH)
3193
str->length(DECIMAL_MAX_STR_LENGTH);
3194
my_decimal_round(E_DEC_FATAL_ERROR, dec, decimals, FALSE, &dec_buf);
3195
my_decimal2string(E_DEC_FATAL_ERROR, &dec_buf, 0, 0, '0', str);
3200
void Item_func_udf_decimal::fix_length_and_dec()
3202
fix_num_length_and_dec();
3206
/* Default max_length is max argument length */
3208
void Item_func_udf_str::fix_length_and_dec()
3210
DBUG_ENTER("Item_func_udf_str::fix_length_and_dec");
3212
for (uint i = 0; i < arg_count; i++)
3213
set_if_bigger(max_length,args[i]->max_length);
3217
String *Item_func_udf_str::val_str(String *str)
3219
DBUG_ASSERT(fixed == 1);
3220
String *res=udf.val_str(str,&str_value);
3228
This has to come last in the udf_handler methods, or C for AIX
3229
version 6.0.0.0 fails to compile with debugging enabled. (Yes, really.)
3232
udf_handler::~udf_handler()
3234
/* Everything should be properly cleaned up by this moment. */
3235
DBUG_ASSERT(not_original || !(initialized || buffers));
3239
bool udf_handler::get_arguments() { return 0; }
3240
#endif /* HAVE_DLOPEN */
3246
pthread_mutex_t LOCK_user_locks;
3247
static HASH hash_user_locks;
3249
class User_level_lock
3257
pthread_cond_t cond;
3258
my_thread_id thread_id;
3259
void set_thread(THD *thd) { thread_id= thd->thread_id; }
3261
User_level_lock(const uchar *key_arg,uint length, ulong id)
3262
:key_length(length),count(1),locked(1), thread_id(id)
3264
key= (uchar*) my_memdup(key_arg,length,MYF(0));
3265
pthread_cond_init(&cond,NULL);
3268
if (my_hash_insert(&hash_user_locks,(uchar*) this))
3270
my_free(key,MYF(0));
3279
hash_delete(&hash_user_locks,(uchar*) this);
3280
my_free(key, MYF(0));
3282
pthread_cond_destroy(&cond);
3284
inline bool initialized() { return key != 0; }
3285
friend void item_user_lock_release(User_level_lock *ull);
3286
friend uchar *ull_get_key(const User_level_lock *ull, size_t *length,
3290
uchar *ull_get_key(const User_level_lock *ull, size_t *length,
3291
my_bool not_used __attribute__((unused)))
3293
*length= ull->key_length;
3298
static bool item_user_lock_inited= 0;
3300
void item_user_lock_init(void)
3302
pthread_mutex_init(&LOCK_user_locks,MY_MUTEX_INIT_SLOW);
3303
hash_init(&hash_user_locks,system_charset_info,
3304
16,0,0,(hash_get_key) ull_get_key,NULL,0);
3305
item_user_lock_inited= 1;
3308
void item_user_lock_free(void)
3310
if (item_user_lock_inited)
3312
item_user_lock_inited= 0;
3313
hash_free(&hash_user_locks);
3314
pthread_mutex_destroy(&LOCK_user_locks);
3318
void item_user_lock_release(User_level_lock *ull)
3323
pthread_cond_signal(&ull->cond);
3329
Wait until we are at or past the given position in the master binlog
3333
longlong Item_master_pos_wait::val_int()
3335
DBUG_ASSERT(fixed == 1);
3336
THD* thd = current_thd;
3337
String *log_name = args[0]->val_str(&value);
3341
if (thd->slave_thread || !log_name || !log_name->length())
3346
#ifdef HAVE_REPLICATION
3347
longlong pos = (ulong)args[1]->val_int();
3348
longlong timeout = (arg_count==3) ? args[2]->val_int() : 0 ;
3349
if ((event_count = active_mi->rli.wait_for_pos(thd, log_name, pos, timeout)) == -2)
3359
void debug_sync_point(const char* lock_name, uint lock_timeout)
3366
longlong Item_func_last_insert_id::val_int()
3368
THD *thd= current_thd;
3369
DBUG_ASSERT(fixed == 1);
3372
longlong value= args[0]->val_int();
3373
null_value= args[0]->null_value;
3375
LAST_INSERT_ID(X) must affect the client's mysql_insert_id() as
3376
documented in the manual. We don't want to touch
3377
first_successful_insert_id_in_cur_stmt because it would make
3378
LAST_INSERT_ID(X) take precedence over an generated auto_increment
3381
thd->arg_of_last_insert_id_function= TRUE;
3382
thd->first_successful_insert_id_in_prev_stmt= value;
3385
return thd->read_first_successful_insert_id_in_prev_stmt();
3389
bool Item_func_last_insert_id::fix_fields(THD *thd, Item **ref)
3391
return Item_int_func::fix_fields(thd, ref);
3395
/* This function is just used to test speed of different functions */
3397
longlong Item_func_benchmark::val_int()
3399
DBUG_ASSERT(fixed == 1);
3400
char buff[MAX_FIELD_WIDTH];
3401
String tmp(buff,sizeof(buff), &my_charset_bin);
3402
my_decimal tmp_decimal;
3403
THD *thd=current_thd;
3404
ulonglong loop_count;
3406
loop_count= (ulonglong) args[0]->val_int();
3408
if (args[0]->null_value ||
3409
(!args[0]->unsigned_flag && (((longlong) loop_count) < 0)))
3411
if (!args[0]->null_value)
3414
llstr(((longlong) loop_count), buff);
3415
push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
3416
ER_WRONG_VALUE_FOR_TYPE, ER(ER_WRONG_VALUE_FOR_TYPE),
3417
"count", buff, "benchmark");
3425
for (ulonglong loop=0 ; loop < loop_count && !thd->killed; loop++)
3427
switch (args[1]->result_type()) {
3429
(void) args[1]->val_real();
3432
(void) args[1]->val_int();
3435
(void) args[1]->val_str(&tmp);
3437
case DECIMAL_RESULT:
3438
(void) args[1]->val_decimal(&tmp_decimal);
3442
// This case should never be chosen
3451
void Item_func_benchmark::print(String *str, enum_query_type query_type)
3453
str->append(STRING_WITH_LEN("benchmark("));
3454
args[0]->print(str, query_type);
3456
args[1]->print(str, query_type);
3460
#define extra_size sizeof(double)
3462
static user_var_entry *get_variable(HASH *hash, LEX_STRING &name,
3463
bool create_if_not_exists)
3465
user_var_entry *entry;
3467
if (!(entry = (user_var_entry*) hash_search(hash, (uchar*) name.str,
3469
create_if_not_exists)
3471
uint size=ALIGN_SIZE(sizeof(user_var_entry))+name.length+1+extra_size;
3472
if (!hash_inited(hash))
3474
if (!(entry = (user_var_entry*) my_malloc(size,MYF(MY_WME | ME_FATALERROR))))
3476
entry->name.str=(char*) entry+ ALIGN_SIZE(sizeof(user_var_entry))+
3478
entry->name.length=name.length;
3481
entry->update_query_id=0;
3482
entry->collation.set(NULL, DERIVATION_IMPLICIT, 0);
3483
entry->unsigned_flag= 0;
3485
If we are here, we were called from a SET or a query which sets a
3486
variable. Imagine it is this:
3487
INSERT INTO t SELECT @a:=10, @a:=@a+1.
3488
Then when we have a Item_func_get_user_var (because of the @a+1) so we
3489
think we have to write the value of @a to the binlog. But before that,
3490
we have a Item_func_set_user_var to create @a (@a:=10), in this we mark
3491
the variable as "already logged" (line below) so that it won't be logged
3492
by Item_func_get_user_var (because that's not necessary).
3494
entry->used_query_id=current_thd->query_id;
3495
entry->type=STRING_RESULT;
3496
memcpy(entry->name.str, name.str, name.length+1);
3497
if (my_hash_insert(hash,(uchar*) entry))
3499
my_free((char*) entry,MYF(0));
3507
When a user variable is updated (in a SET command or a query like
3511
bool Item_func_set_user_var::fix_fields(THD *thd, Item **ref)
3513
DBUG_ASSERT(fixed == 0);
3514
/* fix_fields will call Item_func_set_user_var::fix_length_and_dec */
3515
if (Item_func::fix_fields(thd, ref) ||
3516
!(entry= get_variable(&thd->user_vars, name, 1)))
3519
Remember the last query which updated it, this way a query can later know
3520
if this variable is a constant item in the query (it is if update_query_id
3521
is different from query_id).
3523
entry->update_query_id= thd->query_id;
3525
As it is wrong and confusing to associate any
3526
character set with NULL, @a should be latin2
3527
after this query sequence:
3529
SET @a=_latin2'string';
3532
I.e. the second query should not change the charset
3533
to the current default value, but should keep the
3534
original value assigned during the first query.
3535
In order to do it, we don't copy charset
3536
from the argument if the argument is NULL
3537
and the variable has previously been initialized.
3539
null_item= (args[0]->type() == NULL_ITEM);
3540
if (!entry->collation.collation || !null_item)
3541
entry->collation.set(args[0]->collation.collation, DERIVATION_IMPLICIT);
3542
collation.set(entry->collation.collation, DERIVATION_IMPLICIT);
3543
cached_result_type= args[0]->result_type();
3549
Item_func_set_user_var::fix_length_and_dec()
3551
maybe_null=args[0]->maybe_null;
3552
max_length=args[0]->max_length;
3553
decimals=args[0]->decimals;
3554
collation.set(args[0]->collation.collation, DERIVATION_IMPLICIT);
3559
Mark field in read_map
3562
This is used by filesort to register used fields in a a temporary
3563
column read set or to register used fields in a view
3566
bool Item_func_set_user_var::register_field_in_read_map(uchar *arg)
3570
TABLE *table= (TABLE *) arg;
3571
if (result_field->table == table || !table)
3572
bitmap_set_bit(result_field->table->read_set, result_field->field_index);
3579
Set value to user variable.
3581
@param entry pointer to structure representing variable
3582
@param set_null should we set NULL value ?
3583
@param ptr pointer to buffer with new value
3584
@param length length of new value
3585
@param type type of new value
3586
@param cs charset info for new value
3587
@param dv derivation for new value
3588
@param unsigned_arg indiates if a value of type INT_RESULT is unsigned
3590
@note Sets error and fatal error if allocation fails.
3599
update_hash(user_var_entry *entry, bool set_null, void *ptr, uint length,
3600
Item_result type, CHARSET_INFO *cs, Derivation dv,
3605
char *pos= (char*) entry+ ALIGN_SIZE(sizeof(user_var_entry));
3606
if (entry->value && entry->value != pos)
3607
my_free(entry->value,MYF(0));
3613
if (type == STRING_RESULT)
3614
length++; // Store strings with end \0
3615
if (length <= extra_size)
3617
/* Save value in value struct */
3618
char *pos= (char*) entry+ ALIGN_SIZE(sizeof(user_var_entry));
3619
if (entry->value != pos)
3622
my_free(entry->value,MYF(0));
3628
/* Allocate variable */
3629
if (entry->length != length)
3631
char *pos= (char*) entry+ ALIGN_SIZE(sizeof(user_var_entry));
3632
if (entry->value == pos)
3634
entry->value= (char*) my_realloc(entry->value, length,
3635
MYF(MY_ALLOW_ZERO_PTR | MY_WME |
3641
if (type == STRING_RESULT)
3643
length--; // Fix length change above
3644
entry->value[length]= 0; // Store end \0
3646
memcpy(entry->value,ptr,length);
3647
if (type == DECIMAL_RESULT)
3648
((my_decimal*)entry->value)->fix_buffer_pointer();
3649
entry->length= length;
3650
entry->collation.set(cs, dv);
3651
entry->unsigned_flag= unsigned_arg;
3659
Item_func_set_user_var::update_hash(void *ptr, uint length,
3660
Item_result res_type,
3661
CHARSET_INFO *cs, Derivation dv,
3665
If we set a variable explicitely to NULL then keep the old
3666
result type of the variable
3668
if ((null_value= args[0]->null_value) && null_item)
3669
res_type= entry->type; // Don't change type of item
3670
if (::update_hash(entry, (null_value= args[0]->null_value),
3671
ptr, length, res_type, cs, dv, unsigned_arg))
3680
/** Get the value of a variable as a double. */
3682
double user_var_entry::val_real(my_bool *null_value)
3684
if ((*null_value= (value == 0)))
3689
return *(double*) value;
3691
return (double) *(longlong*) value;
3692
case DECIMAL_RESULT:
3695
my_decimal2double(E_DEC_FATAL_ERROR, (my_decimal *)value, &result);
3699
return my_atof(value); // This is null terminated
3701
DBUG_ASSERT(1); // Impossible
3704
return 0.0; // Impossible
3708
/** Get the value of a variable as an integer. */
3710
longlong user_var_entry::val_int(my_bool *null_value) const
3712
if ((*null_value= (value == 0)))
3717
return (longlong) *(double*) value;
3719
return *(longlong*) value;
3720
case DECIMAL_RESULT:
3723
my_decimal2int(E_DEC_FATAL_ERROR, (my_decimal *)value, 0, &result);
3729
return my_strtoll10(value, (char**) 0, &error);// String is null terminated
3732
DBUG_ASSERT(1); // Impossible
3735
return LL(0); // Impossible
3739
/** Get the value of a variable as a string. */
3741
String *user_var_entry::val_str(my_bool *null_value, String *str,
3744
if ((*null_value= (value == 0)))
3749
str->set_real(*(double*) value, decimals, &my_charset_bin);
3753
str->set(*(longlong*) value, &my_charset_bin);
3755
str->set(*(ulonglong*) value, &my_charset_bin);
3757
case DECIMAL_RESULT:
3758
my_decimal2string(E_DEC_FATAL_ERROR, (my_decimal *)value, 0, 0, 0, str);
3761
if (str->copy(value, length, collation.collation))
3762
str= 0; // EOM error
3764
DBUG_ASSERT(1); // Impossible
3770
/** Get the value of a variable as a decimal. */
3772
my_decimal *user_var_entry::val_decimal(my_bool *null_value, my_decimal *val)
3774
if ((*null_value= (value == 0)))
3779
double2my_decimal(E_DEC_FATAL_ERROR, *(double*) value, val);
3782
int2my_decimal(E_DEC_FATAL_ERROR, *(longlong*) value, 0, val);
3784
case DECIMAL_RESULT:
3785
val= (my_decimal *)value;
3788
str2my_decimal(E_DEC_FATAL_ERROR, value, length, collation.collation, val);
3791
DBUG_ASSERT(1); // Impossible
3798
This functions is invoked on SET \@variable or
3799
\@variable:= expression.
3801
Evaluate (and check expression), store results.
3804
For now it always return OK. All problem with value evaluating
3805
will be caught by thd->is_error() check in sql_set_variables().
3812
Item_func_set_user_var::check(bool use_result_field)
3814
DBUG_ENTER("Item_func_set_user_var::check");
3815
if (use_result_field && !result_field)
3816
use_result_field= FALSE;
3818
switch (cached_result_type) {
3821
save_result.vreal= use_result_field ? result_field->val_real() :
3822
args[0]->val_real();
3827
save_result.vint= use_result_field ? result_field->val_int() :
3829
unsigned_flag= use_result_field ? ((Field_num*)result_field)->unsigned_flag:
3830
args[0]->unsigned_flag;
3835
save_result.vstr= use_result_field ? result_field->val_str(&value) :
3836
args[0]->val_str(&value);
3839
case DECIMAL_RESULT:
3841
save_result.vdec= use_result_field ?
3842
result_field->val_decimal(&decimal_buff) :
3843
args[0]->val_decimal(&decimal_buff);
3848
// This case should never be chosen
3857
This functions is invoked on
3858
SET \@variable or \@variable:= expression.
3861
We have to store the expression as such in the variable, independent of
3862
the value method used by the user
3872
Item_func_set_user_var::update()
3875
DBUG_ENTER("Item_func_set_user_var::update");
3877
switch (cached_result_type) {
3880
res= update_hash((void*) &save_result.vreal,sizeof(save_result.vreal),
3881
REAL_RESULT, &my_charset_bin, DERIVATION_IMPLICIT, 0);
3886
res= update_hash((void*) &save_result.vint, sizeof(save_result.vint),
3887
INT_RESULT, &my_charset_bin, DERIVATION_IMPLICIT,
3893
if (!save_result.vstr) // Null value
3894
res= update_hash((void*) 0, 0, STRING_RESULT, &my_charset_bin,
3895
DERIVATION_IMPLICIT, 0);
3897
res= update_hash((void*) save_result.vstr->ptr(),
3898
save_result.vstr->length(), STRING_RESULT,
3899
save_result.vstr->charset(),
3900
DERIVATION_IMPLICIT, 0);
3903
case DECIMAL_RESULT:
3905
if (!save_result.vdec) // Null value
3906
res= update_hash((void*) 0, 0, DECIMAL_RESULT, &my_charset_bin,
3907
DERIVATION_IMPLICIT, 0);
3909
res= update_hash((void*) save_result.vdec,
3910
sizeof(my_decimal), DECIMAL_RESULT,
3911
&my_charset_bin, DERIVATION_IMPLICIT, 0);
3916
// This case should never be chosen
3924
double Item_func_set_user_var::val_real()
3926
DBUG_ASSERT(fixed == 1);
3928
update(); // Store expression
3929
return entry->val_real(&null_value);
3932
longlong Item_func_set_user_var::val_int()
3934
DBUG_ASSERT(fixed == 1);
3936
update(); // Store expression
3937
return entry->val_int(&null_value);
3940
String *Item_func_set_user_var::val_str(String *str)
3942
DBUG_ASSERT(fixed == 1);
3944
update(); // Store expression
3945
return entry->val_str(&null_value, str, decimals);
3949
my_decimal *Item_func_set_user_var::val_decimal(my_decimal *val)
3951
DBUG_ASSERT(fixed == 1);
3953
update(); // Store expression
3954
return entry->val_decimal(&null_value, val);
3958
double Item_func_set_user_var::val_result()
3960
DBUG_ASSERT(fixed == 1);
3962
update(); // Store expression
3963
return entry->val_real(&null_value);
3966
longlong Item_func_set_user_var::val_int_result()
3968
DBUG_ASSERT(fixed == 1);
3970
update(); // Store expression
3971
return entry->val_int(&null_value);
3974
String *Item_func_set_user_var::str_result(String *str)
3976
DBUG_ASSERT(fixed == 1);
3978
update(); // Store expression
3979
return entry->val_str(&null_value, str, decimals);
3983
my_decimal *Item_func_set_user_var::val_decimal_result(my_decimal *val)
3985
DBUG_ASSERT(fixed == 1);
3987
update(); // Store expression
3988
return entry->val_decimal(&null_value, val);
3992
void Item_func_set_user_var::print(String *str, enum_query_type query_type)
3994
str->append(STRING_WITH_LEN("(@"));
3995
str->append(name.str, name.length);
3996
str->append(STRING_WITH_LEN(":="));
3997
args[0]->print(str, query_type);
4002
void Item_func_set_user_var::print_as_stmt(String *str,
4003
enum_query_type query_type)
4005
str->append(STRING_WITH_LEN("set @"));
4006
str->append(name.str, name.length);
4007
str->append(STRING_WITH_LEN(":="));
4008
args[0]->print(str, query_type);
4012
bool Item_func_set_user_var::send(Protocol *protocol, String *str_arg)
4018
return protocol->store(result_field);
4020
return Item::send(protocol, str_arg);
4023
void Item_func_set_user_var::make_field(Send_field *tmp_field)
4027
result_field->make_field(tmp_field);
4028
DBUG_ASSERT(tmp_field->table_name != 0);
4030
tmp_field->col_name=Item::name; // Use user supplied name
4033
Item::make_field(tmp_field);
4038
Save the value of a user variable into a field
4042
field target field to save the value to
4043
no_conversion flag indicating whether conversions are allowed
4046
Save the function value into a field and update the user variable
4047
accordingly. If a result field is defined and the target field doesn't
4048
coincide with it then the value from the result field will be used as
4049
the new value of the user variable.
4051
The reason to have this method rather than simply using the result
4052
field in the val_xxx() methods is that the value from the result field
4053
not always can be used when the result field is defined.
4054
Let's consider the following cases:
4055
1) when filling a tmp table the result field is defined but the value of it
4056
is undefined because it has to be produced yet. Thus we can't use it.
4057
2) on execution of an INSERT ... SELECT statement the save_in_field()
4058
function will be called to fill the data in the new record. If the SELECT
4059
part uses a tmp table then the result field is defined and should be
4060
used in order to get the correct result.
4062
The difference between the SET_USER_VAR function and regular functions
4063
like CONCAT is that the Item_func objects for the regular functions are
4064
replaced by Item_field objects after the values of these functions have
4065
been stored in a tmp table. Yet an object of the Item_field class cannot
4066
be used to update a user variable.
4067
Due to this we have to handle the result field in a special way here and
4068
in the Item_func_set_user_var::send() function.
4075
int Item_func_set_user_var::save_in_field(Field *field, bool no_conversions,
4076
bool can_use_result_field)
4078
bool use_result_field= (!can_use_result_field ? 0 :
4079
(result_field && result_field != field));
4082
/* Update the value of the user variable */
4083
check(use_result_field);
4086
if (result_type() == STRING_RESULT ||
4087
(result_type() == REAL_RESULT && field->result_type() == STRING_RESULT))
4090
CHARSET_INFO *cs= collation.collation;
4091
char buff[MAX_FIELD_WIDTH]; // Alloc buffer for small columns
4092
str_value.set_quick(buff, sizeof(buff), cs);
4093
result= entry->val_str(&null_value, &str_value, decimals);
4097
str_value.set_quick(0, 0, cs);
4098
return set_field_to_null_with_conversions(field, no_conversions);
4101
/* NOTE: If null_value == FALSE, "result" must be not NULL. */
4103
field->set_notnull();
4104
error=field->store(result->ptr(),result->length(),cs);
4105
str_value.set_quick(0, 0, cs);
4107
else if (result_type() == REAL_RESULT)
4109
double nr= entry->val_real(&null_value);
4111
return set_field_to_null(field);
4112
field->set_notnull();
4113
error=field->store(nr);
4115
else if (result_type() == DECIMAL_RESULT)
4117
my_decimal decimal_value;
4118
my_decimal *val= entry->val_decimal(&null_value, &decimal_value);
4120
return set_field_to_null(field);
4121
field->set_notnull();
4122
error=field->store_decimal(val);
4126
longlong nr= entry->val_int(&null_value);
4128
return set_field_to_null_with_conversions(field, no_conversions);
4129
field->set_notnull();
4130
error=field->store(nr, unsigned_flag);
4137
Item_func_get_user_var::val_str(String *str)
4139
DBUG_ASSERT(fixed == 1);
4140
DBUG_ENTER("Item_func_get_user_var::val_str");
4142
DBUG_RETURN((String*) 0); // No such variable
4143
DBUG_RETURN(var_entry->val_str(&null_value, str, decimals));
4147
double Item_func_get_user_var::val_real()
4149
DBUG_ASSERT(fixed == 1);
4151
return 0.0; // No such variable
4152
return (var_entry->val_real(&null_value));
4156
my_decimal *Item_func_get_user_var::val_decimal(my_decimal *dec)
4158
DBUG_ASSERT(fixed == 1);
4161
return var_entry->val_decimal(&null_value, dec);
4165
longlong Item_func_get_user_var::val_int()
4167
DBUG_ASSERT(fixed == 1);
4169
return LL(0); // No such variable
4170
return (var_entry->val_int(&null_value));
4175
Get variable by name and, if necessary, put the record of variable
4176
use into the binary log.
4178
When a user variable is invoked from an update query (INSERT, UPDATE etc),
4179
stores this variable and its value in thd->user_var_events, so that it can be
4180
written to the binlog (will be written just before the query is written, see
4183
@param thd Current thread
4184
@param name Variable name
4185
@param[out] out_entry variable structure or NULL. The pointer is set
4186
regardless of whether function succeeded or not.
4191
1 Failed to put appropriate record into binary log
4195
int get_var_with_binlog(THD *thd, enum_sql_command sql_command,
4196
LEX_STRING &name, user_var_entry **out_entry)
4198
BINLOG_USER_VAR_EVENT *user_var_event;
4199
user_var_entry *var_entry;
4200
var_entry= get_variable(&thd->user_vars, name, 0);
4203
Any reference to user-defined variable which is done from stored
4204
function or trigger affects their execution and the execution of the
4205
calling statement. We must log all such variables even if they are
4206
not involved in table-updating statements.
4208
if (!(opt_bin_log &&
4209
(is_update_query(sql_command) || thd->in_sub_stmt)))
4211
*out_entry= var_entry;
4218
If the variable does not exist, it's NULL, but we want to create it so
4219
that it gets into the binlog (if it didn't, the slave could be
4220
influenced by a variable of the same name previously set by another
4222
We create it like if it had been explicitly set with SET before.
4223
The 'new' mimics what sql_yacc.yy does when 'SET @a=10;'.
4224
sql_set_variables() is what is called from 'case SQLCOM_SET_OPTION'
4225
in dispatch_command()). Instead of building a one-element list to pass to
4226
sql_set_variables(), we could instead manually call check() and update();
4227
this would save memory and time; but calling sql_set_variables() makes
4228
one unique place to maintain (sql_set_variables()).
4230
Manipulation with lex is necessary since free_underlaid_joins
4231
is going to release memory belonging to the main query.
4234
List<set_var_base> tmp_var_list;
4235
LEX *sav_lex= thd->lex, lex_tmp;
4238
tmp_var_list.push_back(new set_var_user(new Item_func_set_user_var(name,
4240
/* Create the variable */
4241
if (sql_set_variables(thd, &tmp_var_list))
4247
if (!(var_entry= get_variable(&thd->user_vars, name, 0)))
4250
else if (var_entry->used_query_id == thd->query_id ||
4251
mysql_bin_log.is_query_in_union(thd, var_entry->used_query_id))
4254
If this variable was already stored in user_var_events by this query
4255
(because it's used in more than one place in the query), don't store
4258
*out_entry= var_entry;
4264
First we need to store value of var_entry, when the next situation
4267
> insert into t1 values (@a), (@a:=@a+1), (@a:=@a+1);
4268
We have to write to binlog value @a= 1.
4270
We allocate the user_var_event on user_var_events_alloc pool, not on
4271
the this-statement-execution pool because in SPs user_var_event objects
4272
may need to be valid after current [SP] statement execution pool is
4275
size= ALIGN_SIZE(sizeof(BINLOG_USER_VAR_EVENT)) + var_entry->length;
4276
if (!(user_var_event= (BINLOG_USER_VAR_EVENT *)
4277
alloc_root(thd->user_var_events_alloc, size)))
4280
user_var_event->value= (char*) user_var_event +
4281
ALIGN_SIZE(sizeof(BINLOG_USER_VAR_EVENT));
4282
user_var_event->user_var_event= var_entry;
4283
user_var_event->type= var_entry->type;
4284
user_var_event->charset_number= var_entry->collation.collation->number;
4285
if (!var_entry->value)
4288
user_var_event->length= 0;
4289
user_var_event->value= 0;
4293
user_var_event->length= var_entry->length;
4294
memcpy(user_var_event->value, var_entry->value,
4297
/* Mark that this variable has been used by this query */
4298
var_entry->used_query_id= thd->query_id;
4299
if (insert_dynamic(&thd->user_var_events, (uchar*) &user_var_event))
4302
*out_entry= var_entry;
4306
*out_entry= var_entry;
4310
void Item_func_get_user_var::fix_length_and_dec()
4312
THD *thd=current_thd;
4315
decimals=NOT_FIXED_DEC;
4316
max_length=MAX_BLOB_WIDTH;
4318
error= get_var_with_binlog(thd, thd->lex->sql_command, name, &var_entry);
4321
If the variable didn't exist it has been created as a STRING-type.
4322
'var_entry' is NULL only if there occured an error during the call to
4323
get_var_with_binlog.
4327
m_cached_result_type= var_entry->type;
4328
unsigned_flag= var_entry->unsigned_flag;
4329
max_length= var_entry->length;
4331
collation.set(var_entry->collation);
4332
switch(m_cached_result_type) {
4334
max_length= DBL_DIG + 8;
4337
max_length= MAX_BIGINT_WIDTH;
4341
max_length= MAX_BLOB_WIDTH;
4343
case DECIMAL_RESULT:
4344
max_length= DECIMAL_MAX_STR_LENGTH;
4345
decimals= DECIMAL_MAX_SCALE;
4347
case ROW_RESULT: // Keep compiler happy
4355
collation.set(&my_charset_bin, DERIVATION_IMPLICIT);
4357
m_cached_result_type= STRING_RESULT;
4358
max_length= MAX_BLOB_WIDTH;
4363
bool Item_func_get_user_var::const_item() const
4365
return (!var_entry || current_thd->query_id != var_entry->update_query_id);
4369
enum Item_result Item_func_get_user_var::result_type() const
4371
return m_cached_result_type;
4375
void Item_func_get_user_var::print(String *str, enum_query_type query_type)
4377
str->append(STRING_WITH_LEN("(@"));
4378
str->append(name.str,name.length);
4383
bool Item_func_get_user_var::eq(const Item *item, bool binary_cmp) const
4385
/* Assume we don't have rtti */
4387
return 1; // Same item is same.
4388
/* Check if other type is also a get_user_var() object */
4389
if (item->type() != FUNC_ITEM ||
4390
((Item_func*) item)->functype() != functype())
4392
Item_func_get_user_var *other=(Item_func_get_user_var*) item;
4393
return (name.length == other->name.length &&
4394
!memcmp(name.str, other->name.str, name.length));
4398
bool Item_user_var_as_out_param::fix_fields(THD *thd, Item **ref)
4400
DBUG_ASSERT(fixed == 0);
4401
if (Item::fix_fields(thd, ref) ||
4402
!(entry= get_variable(&thd->user_vars, name, 1)))
4404
entry->type= STRING_RESULT;
4406
Let us set the same collation which is used for loading
4407
of fields in LOAD DATA INFILE.
4408
(Since Item_user_var_as_out_param is used only there).
4410
entry->collation.set(thd->variables.collation_database);
4411
entry->update_query_id= thd->query_id;
4416
void Item_user_var_as_out_param::set_null_value(CHARSET_INFO* cs)
4418
::update_hash(entry, TRUE, 0, 0, STRING_RESULT, cs,
4419
DERIVATION_IMPLICIT, 0 /* unsigned_arg */);
4423
void Item_user_var_as_out_param::set_value(const char *str, uint length,
4426
::update_hash(entry, FALSE, (void*)str, length, STRING_RESULT, cs,
4427
DERIVATION_IMPLICIT, 0 /* unsigned_arg */);
4431
double Item_user_var_as_out_param::val_real()
4438
longlong Item_user_var_as_out_param::val_int()
4445
String* Item_user_var_as_out_param::val_str(String *str)
4452
my_decimal* Item_user_var_as_out_param::val_decimal(my_decimal *decimal_buffer)
4459
void Item_user_var_as_out_param::print(String *str, enum_query_type query_type)
4462
str->append(name.str,name.length);
4466
Item_func_get_system_var::
4467
Item_func_get_system_var(sys_var *var_arg, enum_var_type var_type_arg,
4468
LEX_STRING *component_arg, const char *name_arg,
4469
size_t name_len_arg)
4470
:var(var_arg), var_type(var_type_arg), component(*component_arg)
4472
/* set_name() will allocate the name */
4473
set_name(name_arg, name_len_arg, system_charset_info);
4478
Item_func_get_system_var::fix_fields(THD *thd, Item **ref)
4481
DBUG_ENTER("Item_func_get_system_var::fix_fields");
4484
Evaluate the system variable and substitute the result (a basic constant)
4485
instead of this item. If the variable can not be evaluated,
4486
the error is reported in sys_var::item().
4488
if (!(item= var->item(thd, var_type, &component)))
4489
DBUG_RETURN(1); // Impossible
4490
item->set_name(name, 0, system_charset_info); // don't allocate a new name
4491
thd->change_item_tree(ref, item);
4497
bool Item_func_get_system_var::is_written_to_binlog()
4499
return var->is_written_to_binlog(var_type);
4502
longlong Item_func_bit_xor::val_int()
4504
DBUG_ASSERT(fixed == 1);
4505
ulonglong arg1= (ulonglong) args[0]->val_int();
4506
ulonglong arg2= (ulonglong) args[1]->val_int();
4507
if ((null_value= (args[0]->null_value || args[1]->null_value)))
4509
return (longlong) (arg1 ^ arg2);
4513
/***************************************************************************
4515
****************************************************************************/
4518
Return value of an system variable base[.name] as a constant item.
4520
@param thd Thread handler
4521
@param var_type global / session
4522
@param name Name of base or system variable
4523
@param component Component.
4526
If component.str = 0 then the variable name is in 'name'
4534
Item *get_system_var(THD *thd, enum_var_type var_type, LEX_STRING name,
4535
LEX_STRING component)
4538
LEX_STRING *base_name, *component_name;
4542
base_name= &component;
4543
component_name= &name;
4548
component_name= &component; // Empty string
4551
if (!(var= find_sys_var(thd, base_name->str, base_name->length)))
4555
if (!var->is_struct())
4557
my_error(ER_VARIABLE_IS_NOT_STRUCT, MYF(0), base_name->str);
4562
set_if_smaller(component_name->length, MAX_SYS_VAR_LENGTH);
4564
return new Item_func_get_system_var(var, var_type, component_name,
4570
Check a user level lock.
4572
Sets null_value=TRUE on error.
4577
0 Already taken, or error
4580
longlong Item_func_is_free_lock::val_int()
4582
DBUG_ASSERT(fixed == 1);
4583
String *res=args[0]->val_str(&value);
4584
User_level_lock *ull;
4587
if (!res || !res->length())
4593
pthread_mutex_lock(&LOCK_user_locks);
4594
ull= (User_level_lock *) hash_search(&hash_user_locks, (uchar*) res->ptr(),
4595
(size_t) res->length());
4596
pthread_mutex_unlock(&LOCK_user_locks);
4597
if (!ull || !ull->locked)
4602
longlong Item_func_is_used_lock::val_int()
4604
DBUG_ASSERT(fixed == 1);
4605
String *res=args[0]->val_str(&value);
4606
User_level_lock *ull;
4609
if (!res || !res->length())
4612
pthread_mutex_lock(&LOCK_user_locks);
4613
ull= (User_level_lock *) hash_search(&hash_user_locks, (uchar*) res->ptr(),
4614
(size_t) res->length());
4615
pthread_mutex_unlock(&LOCK_user_locks);
4616
if (!ull || !ull->locked)
4620
return ull->thread_id;
4624
longlong Item_func_row_count::val_int()
4626
DBUG_ASSERT(fixed == 1);
4627
THD *thd= current_thd;
4629
return thd->row_count_func;
4632
longlong Item_func_found_rows::val_int()
4634
DBUG_ASSERT(fixed == 1);
4635
THD *thd= current_thd;
4637
return thd->found_rows();
4643
uuid_short handling.
4645
The short uuid is defined as a longlong that contains the following bytes:
4649
4 Startup time of server in seconds
4652
This means that an uuid is guaranteed to be unique
4653
even in a replication environment if the following holds:
4655
- The last byte of the server id is unique
4656
- If you between two shutdown of the server don't get more than
4657
an average of 2^24 = 16M calls to uuid_short() per second.
4660
ulonglong uuid_value;
4662
void uuid_short_init()
4664
uuid_value= ((((ulonglong) server_id) << 56) +
4665
(((ulonglong) server_start_time) << 24));
4669
longlong Item_func_uuid_short::val_int()
4672
pthread_mutex_lock(&LOCK_uuid_generator);
4674
pthread_mutex_unlock(&LOCK_uuid_generator);
4675
return (longlong) val;