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
36
bool check_reserved_words(LEX_STRING *name)
38
if (!my_strcasecmp(system_charset_info, name->str, "GLOBAL") ||
39
!my_strcasecmp(system_charset_info, name->str, "LOCAL") ||
40
!my_strcasecmp(system_charset_info, name->str, "SESSION"))
48
true if item is a constant
52
eval_const_cond(COND *cond)
54
return ((Item_func*) cond)->val_int() ? true : false;
58
void Item_func::set_arguments(List<Item> &list)
61
arg_count=list.elements;
62
args= tmp_arg; // If 2 arguments
63
if (arg_count <= 2 || (args=(Item**) sql_alloc(sizeof(Item*)*arg_count)))
65
List_iterator_fast<Item> li(list);
67
Item **save_args= args;
72
with_sum_func|=item->with_sum_func;
75
list.empty(); // Fields are used
78
Item_func::Item_func(List<Item> &list)
84
Item_func::Item_func(THD *thd, Item_func *item)
85
:Item_result_field(thd, item),
86
allowed_arg_cols(item->allowed_arg_cols),
87
arg_count(item->arg_count),
88
used_tables_cache(item->used_tables_cache),
89
not_null_tables_cache(item->not_null_tables_cache),
90
const_item_cache(item->const_item_cache)
98
if (!(args=(Item**) thd->alloc(sizeof(Item*)*arg_count)))
101
memcpy((char*) args, (char*) item->args, sizeof(Item*)*arg_count);
107
Resolve references to table column for a function and its argument
112
ref Pointer to where this object is used. This reference
113
is used if we want to replace this object with another
114
one (for example in the summary functions).
117
Call fix_fields() for all arguments to the function. The main intention
118
is to allow all Item_field() objects to setup pointers to the table fields.
120
Sets as a side effect the following class variables:
121
maybe_null Set if any argument may return NULL
122
with_sum_func Set if any of the arguments contains a sum function
123
used_tables_cache Set to union of the tables used by arguments
125
str_value.charset If this is a string function, set this to the
126
character set for the first argument.
127
If any argument is binary, this is set to binary
129
If for any item any of the defaults are wrong, then this can
130
be fixed in the fix_length_and_dec() function that is called
131
after this one or by writing a specialized fix_fields() for the
136
true Got error. Stored with my_error().
140
Item_func::fix_fields(THD *thd, Item **ref __attribute__((__unused__)))
143
Item **arg,**arg_end;
144
void *save_thd_marker= thd->thd_marker;
145
uchar buff[STACK_BUFF_ALLOC]; // Max argument in function
147
used_tables_cache= not_null_tables_cache= 0;
150
if (check_stack_overrun(thd, STACK_MIN_SIZE, buff))
151
return true; // Fatal error if flag is set!
153
{ // Print purify happy
154
for (arg=args, arg_end=args+arg_count; arg != arg_end ; arg++)
158
We can't yet set item to *arg as fix_fields may change *arg
159
We shouldn't call fix_fields() twice, so check 'fixed' field first
161
if ((!(*arg)->fixed && (*arg)->fix_fields(thd, arg)))
162
return true; /* purecov: inspected */
165
if (allowed_arg_cols)
167
if (item->check_cols(allowed_arg_cols))
172
/* we have to fetch allowed_arg_cols from first argument */
173
assert(arg == args); // it is first argument
174
allowed_arg_cols= item->cols();
175
assert(allowed_arg_cols); // Can't be 0 any more
178
if (item->maybe_null)
181
with_sum_func= with_sum_func || item->with_sum_func;
182
used_tables_cache|= item->used_tables();
183
not_null_tables_cache|= item->not_null_tables();
184
const_item_cache&= item->const_item();
185
with_subselect|= item->with_subselect;
188
fix_length_and_dec();
189
if (thd->is_error()) // An error inside fix_length_and_dec occured
192
thd->thd_marker= save_thd_marker;
197
void Item_func::fix_after_pullout(st_select_lex *new_parent,
198
Item **ref __attribute__((__unused__)))
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_int64_t(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)
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= UINT32_MAX;
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()
627
return unsigned_flag ? (double) ((uint64_t) val_int()) : (double) val_int();
631
String *Item_int_func::val_str(String *str)
634
int64_t 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
assert(arg_count == 2);
667
Item_result r0= args[0]->result_type();
668
Item_result r1= args[1]->result_type();
670
if (r0 == REAL_RESULT || r1 == REAL_RESULT ||
671
r0 == STRING_RESULT || r1 ==STRING_RESULT)
674
max_length= float_length(decimals);
675
hybrid_type= REAL_RESULT;
677
else if (r0 == DECIMAL_RESULT || r1 == DECIMAL_RESULT)
679
hybrid_type= DECIMAL_RESULT;
684
assert(r0 == INT_RESULT && r1 == INT_RESULT);
686
hybrid_type=INT_RESULT;
694
Set result type for a numeric function of one argument
695
(can be also used by a numeric function of many arguments, if the result
696
type depends only on the first argument)
699
void Item_func_num1::find_num_type()
701
switch (hybrid_type= args[0]->result_type()) {
703
unsigned_flag= args[0]->unsigned_flag;
707
hybrid_type= REAL_RESULT;
708
max_length= float_length(decimals);
719
void Item_func_num1::fix_num_length_and_dec()
721
decimals= args[0]->decimals;
722
max_length= args[0]->max_length;
726
void Item_func_numhybrid::fix_length_and_dec()
728
fix_num_length_and_dec();
733
String *Item_func_numhybrid::val_str(String *str)
736
switch (hybrid_type) {
739
my_decimal decimal_value, *val;
740
if (!(val= decimal_op(&decimal_value)))
741
return 0; // null is set
742
my_decimal_round(E_DEC_FATAL_ERROR, val, decimals, false, val);
743
my_decimal2string(E_DEC_FATAL_ERROR, val, 0, 0, 0, str);
748
int64_t nr= int_op();
750
return 0; /* purecov: inspected */
751
str->set_int(nr, unsigned_flag, &my_charset_bin);
756
double nr= real_op();
758
return 0; /* purecov: inspected */
759
str->set_real(nr,decimals,&my_charset_bin);
763
return str_op(&str_value);
771
double Item_func_numhybrid::val_real()
774
switch (hybrid_type) {
777
my_decimal decimal_value, *val;
779
if (!(val= decimal_op(&decimal_value)))
780
return 0.0; // null is set
781
my_decimal2double(E_DEC_FATAL_ERROR, val, &result);
786
int64_t result= int_op();
787
return unsigned_flag ? (double) ((uint64_t) result) : (double) result;
795
String *res= str_op(&str_value);
796
return (res ? my_strntod(res->charset(), (char*) res->ptr(), res->length(),
797
&end_not_used, &err_not_used) : 0.0);
806
int64_t Item_func_numhybrid::val_int()
809
switch (hybrid_type) {
812
my_decimal decimal_value, *val;
813
if (!(val= decimal_op(&decimal_value)))
814
return 0; // null is set
816
my_decimal2int(E_DEC_FATAL_ERROR, val, unsigned_flag, &result);
822
return (int64_t) rint(real_op());
827
if (!(res= str_op(&str_value)))
830
char *end= (char*) res->ptr() + res->length();
831
CHARSET_INFO *cs= str_value.charset();
832
return (*(cs->cset->strtoll10))(cs, res->ptr(), &end, &err_not_used);
841
my_decimal *Item_func_numhybrid::val_decimal(my_decimal *decimal_value)
843
my_decimal *val= decimal_value;
845
switch (hybrid_type) {
847
val= decimal_op(decimal_value);
851
int64_t result= int_op();
852
int2my_decimal(E_DEC_FATAL_ERROR, result, unsigned_flag, decimal_value);
857
double result= (double)real_op();
858
double2my_decimal(E_DEC_FATAL_ERROR, result, decimal_value);
864
if (!(res= str_op(&str_value)))
867
str2my_decimal(E_DEC_FATAL_ERROR, (char*) res->ptr(),
868
res->length(), res->charset(), decimal_value);
879
void Item_func_signed::print(String *str, enum_query_type query_type)
881
str->append(STRING_WITH_LEN("cast("));
882
args[0]->print(str, query_type);
883
str->append(STRING_WITH_LEN(" as signed)"));
888
int64_t Item_func_signed::val_int_from_str(int *error)
890
char buff[MAX_FIELD_WIDTH], *end, *start;
892
String tmp(buff,sizeof(buff), &my_charset_bin), *res;
896
For a string result, we must first get the string and then convert it
900
if (!(res= args[0]->val_str(&tmp)))
907
start= (char *)res->ptr();
908
length= res->length();
911
value= my_strtoll10(start, &end, error);
912
if (*error > 0 || end != start+ length)
915
String err_tmp(err_buff,(uint32) sizeof(err_buff), system_charset_info);
916
err_tmp.copy(start, length, system_charset_info);
917
push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
918
ER_TRUNCATED_WRONG_VALUE,
919
ER(ER_TRUNCATED_WRONG_VALUE), "INTEGER",
926
int64_t Item_func_signed::val_int()
931
if (args[0]->cast_to_int_type() != STRING_RESULT ||
932
args[0]->result_as_int64_t())
934
value= args[0]->val_int();
935
null_value= args[0]->null_value;
939
value= val_int_from_str(&error);
940
if (value < 0 && error == 0)
942
push_warning(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN, ER_UNKNOWN_ERROR,
943
"Cast to signed converted positive out-of-range integer to "
944
"it's negative complement");
950
void Item_func_unsigned::print(String *str, enum_query_type query_type)
952
str->append(STRING_WITH_LEN("cast("));
953
args[0]->print(str, query_type);
954
str->append(STRING_WITH_LEN(" as unsigned)"));
959
int64_t Item_func_unsigned::val_int()
964
if (args[0]->cast_to_int_type() == DECIMAL_RESULT)
966
my_decimal tmp, *dec= args[0]->val_decimal(&tmp);
967
if (!(null_value= args[0]->null_value))
968
my_decimal2int(E_DEC_FATAL_ERROR, dec, 1, &value);
973
else if (args[0]->cast_to_int_type() != STRING_RESULT ||
974
args[0]->result_as_int64_t())
976
value= args[0]->val_int();
977
null_value= args[0]->null_value;
981
value= val_int_from_str(&error);
983
push_warning(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN, ER_UNKNOWN_ERROR,
984
"Cast to unsigned converted negative integer to it's "
985
"positive complement");
990
String *Item_decimal_typecast::val_str(String *str)
992
my_decimal tmp_buf, *tmp= val_decimal(&tmp_buf);
995
my_decimal2string(E_DEC_FATAL_ERROR, tmp, 0, 0, 0, str);
1000
double Item_decimal_typecast::val_real()
1002
my_decimal tmp_buf, *tmp= val_decimal(&tmp_buf);
1006
my_decimal2double(E_DEC_FATAL_ERROR, tmp, &res);
1011
int64_t Item_decimal_typecast::val_int()
1013
my_decimal tmp_buf, *tmp= val_decimal(&tmp_buf);
1017
my_decimal2int(E_DEC_FATAL_ERROR, tmp, unsigned_flag, &res);
1022
my_decimal *Item_decimal_typecast::val_decimal(my_decimal *dec)
1024
my_decimal tmp_buf, *tmp= args[0]->val_decimal(&tmp_buf);
1028
if ((null_value= args[0]->null_value))
1030
my_decimal_round(E_DEC_FATAL_ERROR, tmp, decimals, false, dec);
1036
my_decimal_set_zero(dec);
1040
precision= my_decimal_length_to_precision(max_length,
1041
decimals, unsigned_flag);
1042
if (precision - decimals < (uint) my_decimal_intg(dec))
1044
max_my_decimal(dec, precision, decimals);
1051
push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
1052
ER_WARN_DATA_OUT_OF_RANGE,
1053
ER(ER_WARN_DATA_OUT_OF_RANGE),
1059
void Item_decimal_typecast::print(String *str, enum_query_type query_type)
1061
char len_buf[20*3 + 1];
1064
uint precision= my_decimal_length_to_precision(max_length, decimals,
1066
str->append(STRING_WITH_LEN("cast("));
1067
args[0]->print(str, query_type);
1068
str->append(STRING_WITH_LEN(" as decimal("));
1070
end=int10_to_str(precision, len_buf,10);
1071
str->append(len_buf, (uint32) (end - len_buf));
1075
end=int10_to_str(decimals, len_buf,10);
1076
str->append(len_buf, (uint32) (end - len_buf));
1083
double Item_func_plus::real_op()
1085
double value= args[0]->val_real() + args[1]->val_real();
1086
if ((null_value=args[0]->null_value || args[1]->null_value))
1088
return fix_result(value);
1092
int64_t Item_func_plus::int_op()
1094
int64_t value=args[0]->val_int()+args[1]->val_int();
1095
if ((null_value=args[0]->null_value || args[1]->null_value))
1102
Calculate plus of two decimals.
1104
@param decimal_value Buffer that can be used to store result
1107
0 Value was NULL; In this case null_value is set
1109
\# Value of operation as a decimal
1112
my_decimal *Item_func_plus::decimal_op(my_decimal *decimal_value)
1114
my_decimal value1, *val1;
1115
my_decimal value2, *val2;
1116
val1= args[0]->val_decimal(&value1);
1117
if ((null_value= args[0]->null_value))
1119
val2= args[1]->val_decimal(&value2);
1120
if (!(null_value= (args[1]->null_value ||
1121
(my_decimal_add(E_DEC_FATAL_ERROR, decimal_value, val1,
1123
return decimal_value;
1128
Set precision of results for additive operations (+ and -)
1130
void Item_func_additive_op::result_precision()
1132
decimals= max(args[0]->decimals, args[1]->decimals);
1133
int max_int_part= max(args[0]->decimal_precision() - args[0]->decimals,
1134
args[1]->decimal_precision() - args[1]->decimals);
1135
int precision= min(max_int_part + 1 + decimals, DECIMAL_MAX_PRECISION);
1137
/* Integer operations keep unsigned_flag if one of arguments is unsigned */
1138
if (result_type() == INT_RESULT)
1139
unsigned_flag= args[0]->unsigned_flag | args[1]->unsigned_flag;
1141
unsigned_flag= args[0]->unsigned_flag & args[1]->unsigned_flag;
1142
max_length= my_decimal_precision_to_length(precision, decimals,
1148
The following function is here to allow the user to force
1149
subtraction of UNSIGNED BIGINT to return negative values.
1152
void Item_func_minus::fix_length_and_dec()
1154
Item_num_op::fix_length_and_dec();
1155
if (unsigned_flag &&
1156
(current_thd->variables.sql_mode & MODE_NO_UNSIGNED_SUBTRACTION))
1161
double Item_func_minus::real_op()
1163
double value= args[0]->val_real() - args[1]->val_real();
1164
if ((null_value=args[0]->null_value || args[1]->null_value))
1166
return fix_result(value);
1170
int64_t Item_func_minus::int_op()
1172
int64_t value=args[0]->val_int() - args[1]->val_int();
1173
if ((null_value=args[0]->null_value || args[1]->null_value))
1180
See Item_func_plus::decimal_op for comments.
1183
my_decimal *Item_func_minus::decimal_op(my_decimal *decimal_value)
1185
my_decimal value1, *val1;
1186
my_decimal value2, *val2=
1188
val1= args[0]->val_decimal(&value1);
1189
if ((null_value= args[0]->null_value))
1191
val2= args[1]->val_decimal(&value2);
1192
if (!(null_value= (args[1]->null_value ||
1193
(my_decimal_sub(E_DEC_FATAL_ERROR, decimal_value, val1,
1195
return decimal_value;
1200
double Item_func_mul::real_op()
1203
double value= args[0]->val_real() * args[1]->val_real();
1204
if ((null_value=args[0]->null_value || args[1]->null_value))
1206
return fix_result(value);
1210
int64_t Item_func_mul::int_op()
1213
int64_t value=args[0]->val_int()*args[1]->val_int();
1214
if ((null_value=args[0]->null_value || args[1]->null_value))
1220
/** See Item_func_plus::decimal_op for comments. */
1222
my_decimal *Item_func_mul::decimal_op(my_decimal *decimal_value)
1224
my_decimal value1, *val1;
1225
my_decimal value2, *val2;
1226
val1= args[0]->val_decimal(&value1);
1227
if ((null_value= args[0]->null_value))
1229
val2= args[1]->val_decimal(&value2);
1230
if (!(null_value= (args[1]->null_value ||
1231
(my_decimal_mul(E_DEC_FATAL_ERROR, decimal_value, val1,
1233
return decimal_value;
1238
void Item_func_mul::result_precision()
1240
/* Integer operations keep unsigned_flag if one of arguments is unsigned */
1241
if (result_type() == INT_RESULT)
1242
unsigned_flag= args[0]->unsigned_flag | args[1]->unsigned_flag;
1244
unsigned_flag= args[0]->unsigned_flag & args[1]->unsigned_flag;
1245
decimals= min(args[0]->decimals + args[1]->decimals, DECIMAL_MAX_SCALE);
1246
int precision= min(args[0]->decimal_precision() + args[1]->decimal_precision(),
1247
DECIMAL_MAX_PRECISION);
1248
max_length= my_decimal_precision_to_length(precision, decimals,unsigned_flag);
1252
double Item_func_div::real_op()
1255
double value= args[0]->val_real();
1256
double val2= args[1]->val_real();
1257
if ((null_value= args[0]->null_value || args[1]->null_value))
1261
signal_divide_by_null();
1264
return fix_result(value/val2);
1268
my_decimal *Item_func_div::decimal_op(my_decimal *decimal_value)
1270
my_decimal value1, *val1;
1271
my_decimal value2, *val2;
1274
val1= args[0]->val_decimal(&value1);
1275
if ((null_value= args[0]->null_value))
1277
val2= args[1]->val_decimal(&value2);
1278
if ((null_value= args[1]->null_value))
1280
if ((err= my_decimal_div(E_DEC_FATAL_ERROR & ~E_DEC_DIV_ZERO, decimal_value,
1281
val1, val2, prec_increment)) > 3)
1283
if (err == E_DEC_DIV_ZERO)
1284
signal_divide_by_null();
1288
return decimal_value;
1292
void Item_func_div::result_precision()
1294
uint precision=min(args[0]->decimal_precision() + prec_increment,
1295
DECIMAL_MAX_PRECISION);
1296
/* Integer operations keep unsigned_flag if one of arguments is unsigned */
1297
if (result_type() == INT_RESULT)
1298
unsigned_flag= args[0]->unsigned_flag | args[1]->unsigned_flag;
1300
unsigned_flag= args[0]->unsigned_flag & args[1]->unsigned_flag;
1301
decimals= min(args[0]->decimals + prec_increment, DECIMAL_MAX_SCALE);
1302
max_length= my_decimal_precision_to_length(precision, decimals,
1307
void Item_func_div::fix_length_and_dec()
1309
prec_increment= current_thd->variables.div_precincrement;
1310
Item_num_op::fix_length_and_dec();
1311
switch(hybrid_type) {
1314
decimals=max(args[0]->decimals,args[1]->decimals)+prec_increment;
1315
set_if_smaller(decimals, NOT_FIXED_DEC);
1316
max_length=args[0]->max_length - args[0]->decimals + decimals;
1317
uint tmp=float_length(decimals);
1318
set_if_smaller(max_length,tmp);
1322
hybrid_type= DECIMAL_RESULT;
1325
case DECIMAL_RESULT:
1331
maybe_null= 1; // devision by zero
1336
/* Integer division */
1337
int64_t Item_func_int_div::val_int()
1340
int64_t value=args[0]->val_int();
1341
int64_t val2=args[1]->val_int();
1342
if ((null_value= (args[0]->null_value || args[1]->null_value)))
1346
signal_divide_by_null();
1349
return (unsigned_flag ?
1350
(uint64_t) value / (uint64_t) val2 :
1355
void Item_func_int_div::fix_length_and_dec()
1357
Item_result argtype= args[0]->result_type();
1358
/* use precision ony for the data type it is applicable for and valid */
1359
max_length=args[0]->max_length -
1360
(argtype == DECIMAL_RESULT || argtype == INT_RESULT ?
1361
args[0]->decimals : 0);
1363
unsigned_flag=args[0]->unsigned_flag | args[1]->unsigned_flag;
1367
int64_t Item_func_mod::int_op()
1370
int64_t value= args[0]->val_int();
1371
int64_t val2= args[1]->val_int();
1374
if ((null_value= args[0]->null_value || args[1]->null_value))
1375
return 0; /* purecov: inspected */
1378
signal_divide_by_null();
1382
if (args[0]->unsigned_flag)
1383
result= args[1]->unsigned_flag ?
1384
((uint64_t) value) % ((uint64_t) val2) : ((uint64_t) value) % val2;
1386
result= args[1]->unsigned_flag ?
1387
value % ((uint64_t) val2) : value % val2;
1392
double Item_func_mod::real_op()
1395
double value= args[0]->val_real();
1396
double val2= args[1]->val_real();
1397
if ((null_value= args[0]->null_value || args[1]->null_value))
1398
return 0.0; /* purecov: inspected */
1401
signal_divide_by_null();
1404
return fmod(value,val2);
1408
my_decimal *Item_func_mod::decimal_op(my_decimal *decimal_value)
1410
my_decimal value1, *val1;
1411
my_decimal value2, *val2;
1413
val1= args[0]->val_decimal(&value1);
1414
if ((null_value= args[0]->null_value))
1416
val2= args[1]->val_decimal(&value2);
1417
if ((null_value= args[1]->null_value))
1419
switch (my_decimal_mod(E_DEC_FATAL_ERROR & ~E_DEC_DIV_ZERO, decimal_value,
1421
case E_DEC_TRUNCATED:
1423
return decimal_value;
1424
case E_DEC_DIV_ZERO:
1425
signal_divide_by_null();
1433
void Item_func_mod::result_precision()
1435
decimals= max(args[0]->decimals, args[1]->decimals);
1436
max_length= max(args[0]->max_length, args[1]->max_length);
1440
void Item_func_mod::fix_length_and_dec()
1442
Item_num_op::fix_length_and_dec();
1444
unsigned_flag= args[0]->unsigned_flag;
1448
double Item_func_neg::real_op()
1450
double value= args[0]->val_real();
1451
null_value= args[0]->null_value;
1456
int64_t Item_func_neg::int_op()
1458
int64_t value= args[0]->val_int();
1459
null_value= args[0]->null_value;
1464
my_decimal *Item_func_neg::decimal_op(my_decimal *decimal_value)
1466
my_decimal val, *value= args[0]->val_decimal(&val);
1467
if (!(null_value= args[0]->null_value))
1469
my_decimal2decimal(value, decimal_value);
1470
my_decimal_neg(decimal_value);
1471
return decimal_value;
1477
void Item_func_neg::fix_num_length_and_dec()
1479
decimals= args[0]->decimals;
1480
/* 1 add because sign can appear */
1481
max_length= args[0]->max_length + 1;
1485
void Item_func_neg::fix_length_and_dec()
1487
Item_func_num1::fix_length_and_dec();
1490
If this is in integer context keep the context as integer if possible
1491
(This is how multiplication and other integer functions works)
1492
Use val() to get value as arg_type doesn't mean that item is
1493
Item_int or Item_real due to existence of Item_param.
1495
if (hybrid_type == INT_RESULT && args[0]->const_item())
1497
int64_t val= args[0]->val_int();
1498
if ((uint64_t) val >= (uint64_t) INT64_MIN &&
1499
((uint64_t) val != (uint64_t) INT64_MIN ||
1500
args[0]->type() != INT_ITEM))
1503
Ensure that result is converted to DECIMAL, as int64_t can't hold
1506
hybrid_type= DECIMAL_RESULT;
1514
double Item_func_abs::real_op()
1516
double value= args[0]->val_real();
1517
null_value= args[0]->null_value;
1522
int64_t Item_func_abs::int_op()
1524
int64_t value= args[0]->val_int();
1525
if ((null_value= args[0]->null_value))
1527
return (value >= 0) || unsigned_flag ? value : -value;
1531
my_decimal *Item_func_abs::decimal_op(my_decimal *decimal_value)
1533
my_decimal val, *value= args[0]->val_decimal(&val);
1534
if (!(null_value= args[0]->null_value))
1536
my_decimal2decimal(value, decimal_value);
1537
if (decimal_value->sign())
1538
my_decimal_neg(decimal_value);
1539
return decimal_value;
1545
void Item_func_abs::fix_length_and_dec()
1547
Item_func_num1::fix_length_and_dec();
1548
unsigned_flag= args[0]->unsigned_flag;
1552
/** Gateway to natural LOG function. */
1553
double Item_func_ln::val_real()
1556
double value= args[0]->val_real();
1557
if ((null_value= args[0]->null_value))
1561
signal_divide_by_null();
1568
Extended but so slower LOG function.
1570
We have to check if all values are > zero and first one is not one
1571
as these are the cases then result is not a number.
1573
double Item_func_log::val_real()
1576
double value= args[0]->val_real();
1577
if ((null_value= args[0]->null_value))
1581
signal_divide_by_null();
1586
double value2= args[1]->val_real();
1587
if ((null_value= args[1]->null_value))
1589
if (value2 <= 0.0 || value == 1.0)
1591
signal_divide_by_null();
1594
return log(value2) / log(value);
1599
double Item_func_log2::val_real()
1602
double value= args[0]->val_real();
1604
if ((null_value=args[0]->null_value))
1608
signal_divide_by_null();
1611
return log(value) / M_LN2;
1614
double Item_func_log10::val_real()
1617
double value= args[0]->val_real();
1618
if ((null_value= args[0]->null_value))
1622
signal_divide_by_null();
1625
return log10(value);
1628
double Item_func_exp::val_real()
1631
double value= args[0]->val_real();
1632
if ((null_value=args[0]->null_value))
1633
return 0.0; /* purecov: inspected */
1634
return fix_result(exp(value));
1637
double Item_func_sqrt::val_real()
1640
double value= args[0]->val_real();
1641
if ((null_value=(args[0]->null_value || value < 0)))
1642
return 0.0; /* purecov: inspected */
1646
double Item_func_pow::val_real()
1649
double value= args[0]->val_real();
1650
double val2= args[1]->val_real();
1651
if ((null_value=(args[0]->null_value || args[1]->null_value)))
1652
return 0.0; /* purecov: inspected */
1653
return fix_result(pow(value,val2));
1656
// Trigonometric functions
1658
double Item_func_acos::val_real()
1661
// the volatile's for BUG #2338 to calm optimizer down (because of gcc's bug)
1662
volatile double value= args[0]->val_real();
1663
if ((null_value=(args[0]->null_value || (value < -1.0 || value > 1.0))))
1668
double Item_func_asin::val_real()
1671
// the volatile's for BUG #2338 to calm optimizer down (because of gcc's bug)
1672
volatile double value= args[0]->val_real();
1673
if ((null_value=(args[0]->null_value || (value < -1.0 || value > 1.0))))
1678
double Item_func_atan::val_real()
1681
double value= args[0]->val_real();
1682
if ((null_value=args[0]->null_value))
1686
double val2= args[1]->val_real();
1687
if ((null_value=args[1]->null_value))
1689
return fix_result(atan2(value,val2));
1694
double Item_func_cos::val_real()
1697
double value= args[0]->val_real();
1698
if ((null_value=args[0]->null_value))
1703
double Item_func_sin::val_real()
1706
double value= args[0]->val_real();
1707
if ((null_value=args[0]->null_value))
1712
double Item_func_tan::val_real()
1715
double value= args[0]->val_real();
1716
if ((null_value=args[0]->null_value))
1718
return fix_result(tan(value));
1722
// Shift-functions, same as << and >> in C/C++
1725
int64_t Item_func_shift_left::val_int()
1729
uint64_t res= ((uint64_t) args[0]->val_int() <<
1730
(shift=(uint) args[1]->val_int()));
1731
if (args[0]->null_value || args[1]->null_value)
1737
return (shift < sizeof(int64_t)*8 ? (int64_t) res : 0LL);
1740
int64_t Item_func_shift_right::val_int()
1744
uint64_t res= (uint64_t) args[0]->val_int() >>
1745
(shift=(uint) args[1]->val_int());
1746
if (args[0]->null_value || args[1]->null_value)
1752
return (shift < sizeof(int64_t)*8 ? (int64_t) res : 0LL);
1756
int64_t Item_func_bit_neg::val_int()
1759
uint64_t res= (uint64_t) args[0]->val_int();
1760
if ((null_value=args[0]->null_value))
1766
// Conversion functions
1768
void Item_func_integer::fix_length_and_dec()
1770
max_length=args[0]->max_length - args[0]->decimals+1;
1771
uint tmp=float_length(decimals);
1772
set_if_smaller(max_length,tmp);
1776
void Item_func_int_val::fix_num_length_and_dec()
1778
max_length= args[0]->max_length - (args[0]->decimals ?
1779
args[0]->decimals + 1 :
1781
uint tmp= float_length(decimals);
1782
set_if_smaller(max_length,tmp);
1787
void Item_func_int_val::find_num_type()
1789
switch(hybrid_type= args[0]->result_type())
1793
hybrid_type= REAL_RESULT;
1794
max_length= float_length(decimals);
1797
case DECIMAL_RESULT:
1799
-2 because in most high position can't be used any digit for int64_t
1800
and one position for increasing value during operation
1802
if ((args[0]->max_length - args[0]->decimals) >=
1803
(DECIMAL_LONGLONG_DIGITS - 2))
1805
hybrid_type= DECIMAL_RESULT;
1809
unsigned_flag= args[0]->unsigned_flag;
1810
hybrid_type= INT_RESULT;
1820
int64_t Item_func_ceiling::int_op()
1823
switch (args[0]->result_type()) {
1825
result= args[0]->val_int();
1826
null_value= args[0]->null_value;
1828
case DECIMAL_RESULT:
1830
my_decimal dec_buf, *dec;
1831
if ((dec= Item_func_ceiling::decimal_op(&dec_buf)))
1832
my_decimal2int(E_DEC_FATAL_ERROR, dec, unsigned_flag, &result);
1838
result= (int64_t)Item_func_ceiling::real_op();
1844
double Item_func_ceiling::real_op()
1847
the volatile's for BUG #3051 to calm optimizer down (because of gcc's
1850
volatile double value= args[0]->val_real();
1851
null_value= args[0]->null_value;
1856
my_decimal *Item_func_ceiling::decimal_op(my_decimal *decimal_value)
1858
my_decimal val, *value= args[0]->val_decimal(&val);
1859
if (!(null_value= (args[0]->null_value ||
1860
my_decimal_ceiling(E_DEC_FATAL_ERROR, value,
1861
decimal_value) > 1)))
1862
return decimal_value;
1867
int64_t Item_func_floor::int_op()
1870
switch (args[0]->result_type()) {
1872
result= args[0]->val_int();
1873
null_value= args[0]->null_value;
1875
case DECIMAL_RESULT:
1877
my_decimal dec_buf, *dec;
1878
if ((dec= Item_func_floor::decimal_op(&dec_buf)))
1879
my_decimal2int(E_DEC_FATAL_ERROR, dec, unsigned_flag, &result);
1885
result= (int64_t)Item_func_floor::real_op();
1891
double Item_func_floor::real_op()
1894
the volatile's for BUG #3051 to calm optimizer down (because of gcc's
1897
volatile double value= args[0]->val_real();
1898
null_value= args[0]->null_value;
1899
return floor(value);
1903
my_decimal *Item_func_floor::decimal_op(my_decimal *decimal_value)
1905
my_decimal val, *value= args[0]->val_decimal(&val);
1906
if (!(null_value= (args[0]->null_value ||
1907
my_decimal_floor(E_DEC_FATAL_ERROR, value,
1908
decimal_value) > 1)))
1909
return decimal_value;
1914
void Item_func_round::fix_length_and_dec()
1916
int decimals_to_set;
1920
unsigned_flag= args[0]->unsigned_flag;
1921
if (!args[1]->const_item())
1923
max_length= args[0]->max_length;
1924
decimals= args[0]->decimals;
1925
if (args[0]->result_type() == DECIMAL_RESULT)
1928
hybrid_type= DECIMAL_RESULT;
1931
hybrid_type= REAL_RESULT;
1935
val1= args[1]->val_int();
1936
val1_unsigned= args[1]->unsigned_flag;
1938
decimals_to_set= val1_unsigned ? INT_MAX : 0;
1940
decimals_to_set= (val1 > INT_MAX) ? INT_MAX : (int) val1;
1942
if (args[0]->decimals == NOT_FIXED_DEC)
1944
max_length= args[0]->max_length;
1945
decimals= min(decimals_to_set, NOT_FIXED_DEC);
1946
hybrid_type= REAL_RESULT;
1950
switch (args[0]->result_type()) {
1953
hybrid_type= REAL_RESULT;
1954
decimals= min(decimals_to_set, NOT_FIXED_DEC);
1955
max_length= float_length(decimals);
1958
if ((!decimals_to_set && truncate) || (args[0]->decimal_precision() < DECIMAL_LONGLONG_DIGITS))
1960
int length_can_increase= test(!truncate && (val1 < 0) && !val1_unsigned);
1961
max_length= args[0]->max_length + length_can_increase;
1962
/* Here we can keep INT_RESULT */
1963
hybrid_type= INT_RESULT;
1968
case DECIMAL_RESULT:
1970
hybrid_type= DECIMAL_RESULT;
1971
decimals_to_set= min(DECIMAL_MAX_SCALE, decimals_to_set);
1972
int decimals_delta= args[0]->decimals - decimals_to_set;
1973
int precision= args[0]->decimal_precision();
1974
int length_increase= ((decimals_delta <= 0) || truncate) ? 0:1;
1976
precision-= decimals_delta - length_increase;
1977
decimals= min(decimals_to_set, DECIMAL_MAX_SCALE);
1978
max_length= my_decimal_precision_to_length(precision, decimals,
1983
assert(0); /* This result type isn't handled */
1987
double my_double_round(double value, int64_t dec, bool dec_unsigned,
1991
bool dec_negative= (dec < 0) && !dec_unsigned;
1992
uint64_t abs_dec= dec_negative ? -dec : dec;
1994
tmp2 is here to avoid return the value with 80 bit precision
1995
This will fix that the test round(0.1,1) = round(0.1,1) is true
1997
volatile double tmp2;
1999
tmp=(abs_dec < array_elements(log_10) ?
2000
log_10[abs_dec] : pow(10.0,(double) abs_dec));
2002
if (dec_negative && my_isinf(tmp))
2004
else if (!dec_negative && my_isinf(value * tmp))
2009
tmp2= dec < 0 ? floor(value/tmp)*tmp : floor(value*tmp)/tmp;
2011
tmp2= dec < 0 ? ceil(value/tmp)*tmp : ceil(value*tmp)/tmp;
2014
tmp2=dec < 0 ? rint(value/tmp)*tmp : rint(value*tmp)/tmp;
2019
double Item_func_round::real_op()
2021
double value= args[0]->val_real();
2023
if (!(null_value= args[0]->null_value || args[1]->null_value))
2024
return my_double_round(value, args[1]->val_int(), args[1]->unsigned_flag,
2031
Rounds a given value to a power of 10 specified as the 'to' argument,
2032
avoiding overflows when the value is close to the uint64_t range boundary.
2035
static inline uint64_t my_unsigned_round(uint64_t value, uint64_t to)
2037
uint64_t tmp= value / to * to;
2038
return (value - tmp < (to >> 1)) ? tmp : tmp + to;
2042
int64_t Item_func_round::int_op()
2044
int64_t value= args[0]->val_int();
2045
int64_t dec= args[1]->val_int();
2048
if ((null_value= args[0]->null_value || args[1]->null_value))
2050
if ((dec >= 0) || args[1]->unsigned_flag)
2051
return value; // integer have not digits after point
2056
if(abs_dec >= array_elements(log_10_int))
2059
tmp= log_10_int[abs_dec];
2062
value= (unsigned_flag) ?
2063
((uint64_t) value / tmp) * tmp : (value / tmp) * tmp;
2065
value= (unsigned_flag || value >= 0) ?
2066
my_unsigned_round((uint64_t) value, tmp) :
2067
-(int64_t) my_unsigned_round((uint64_t) -value, tmp);
2072
my_decimal *Item_func_round::decimal_op(my_decimal *decimal_value)
2074
my_decimal val, *value= args[0]->val_decimal(&val);
2075
int64_t dec= args[1]->val_int();
2076
if (dec >= 0 || args[1]->unsigned_flag)
2077
dec= min((uint64_t) dec, decimals);
2078
else if (dec < INT_MIN)
2081
if (!(null_value= (args[0]->null_value || args[1]->null_value ||
2082
my_decimal_round(E_DEC_FATAL_ERROR, value, (int) dec,
2083
truncate, decimal_value) > 1)))
2085
decimal_value->frac= decimals;
2086
return decimal_value;
2092
void Item_func_rand::seed_random(Item *arg)
2095
TODO: do not do reinit 'rand' for every execute of PS/SP if
2096
args[0] is a constant.
2098
uint32 tmp= (uint32) arg->val_int();
2099
randominit(rand, (uint32) (tmp*0x10001L+55555555L),
2100
(uint32) (tmp*0x10000001L));
2104
bool Item_func_rand::fix_fields(THD *thd,Item **ref)
2106
if (Item_real_func::fix_fields(thd, ref))
2108
used_tables_cache|= RAND_TABLE_BIT;
2110
{ // Only use argument once in query
2112
Allocate rand structure once: we must use thd->stmt_arena
2113
to create rand in proper mem_root if it's a prepared statement or
2116
No need to send a Rand log event if seed was given eg: RAND(seed),
2117
as it will be replicated in the query as such.
2119
if (!rand && !(rand= (struct rand_struct*)
2120
thd->stmt_arena->alloc(sizeof(*rand))))
2123
if (args[0]->const_item())
2124
seed_random (args[0]);
2129
Save the seed only the first time RAND() is used in the query
2130
Once events are forwarded rather than recreated,
2131
the following can be skipped if inside the slave thread
2133
if (!thd->rand_used)
2136
thd->rand_saved_seed1= thd->rand.seed1;
2137
thd->rand_saved_seed2= thd->rand.seed2;
2144
void Item_func_rand::update_used_tables()
2146
Item_real_func::update_used_tables();
2147
used_tables_cache|= RAND_TABLE_BIT;
2151
double Item_func_rand::val_real()
2154
if (arg_count && !args[0]->const_item())
2155
seed_random (args[0]);
2156
return my_rnd(rand);
2159
int64_t Item_func_sign::val_int()
2162
double value= args[0]->val_real();
2163
null_value=args[0]->null_value;
2164
return value < 0.0 ? -1 : (value > 0 ? 1 : 0);
2168
double Item_func_units::val_real()
2171
double value= args[0]->val_real();
2172
if ((null_value=args[0]->null_value))
2174
return value*mul+add;
2178
void Item_func_min_max::fix_length_and_dec()
2181
bool datetime_found= false;
2185
cmp_type=args[0]->result_type();
2187
for (uint i=0 ; i < arg_count ; i++)
2189
set_if_bigger(max_length, args[i]->max_length);
2190
set_if_bigger(decimals, args[i]->decimals);
2191
set_if_bigger(max_int_part, args[i]->decimal_int_part());
2192
if (args[i]->maybe_null)
2194
cmp_type=item_cmp_type(cmp_type,args[i]->result_type());
2195
if (args[i]->result_type() != ROW_RESULT && args[i]->is_datetime())
2197
datetime_found= true;
2198
if (!datetime_item || args[i]->field_type() == MYSQL_TYPE_DATETIME)
2199
datetime_item= args[i];
2202
if (cmp_type == STRING_RESULT)
2204
agg_arg_charsets(collation, args, arg_count, MY_COLL_CMP_CONV, 1);
2208
compare_as_dates= true;
2211
else if ((cmp_type == DECIMAL_RESULT) || (cmp_type == INT_RESULT))
2212
max_length= my_decimal_precision_to_length(max_int_part+decimals, decimals,
2214
cached_field_type= agg_field_type(args, arg_count);
2219
Compare item arguments in the DATETIME context.
2223
value [out] found least/greatest DATE/DATETIME value
2226
Compare item arguments as DATETIME values and return the index of the
2227
least/greatest argument in the arguments array.
2228
The correct integer DATE/DATETIME value of the found argument is
2229
stored to the value pointer, if latter is provided.
2232
0 If one of arguments is NULL
2233
# index of the least/greatest argument
2236
uint Item_func_min_max::cmp_datetimes(uint64_t *value)
2238
uint64_t min_max= 0;
2239
uint min_max_idx= 0;
2241
for (uint i=0; i < arg_count ; i++)
2243
Item **arg= args + i;
2245
uint64_t res= get_datetime_value(thd, &arg, 0, datetime_item, &is_null);
2246
if ((null_value= args[i]->null_value))
2248
if (i == 0 || (res < min_max ? cmp_sign : -cmp_sign) > 0)
2257
if (datetime_item->field_type() == MYSQL_TYPE_NEWDATE)
2264
String *Item_func_min_max::val_str(String *str)
2267
if (compare_as_dates)
2270
uint min_max_idx= cmp_datetimes(NULL);
2273
str_res= args[min_max_idx]->val_str(str);
2274
str_res->set_charset(collation.collation);
2280
int64_t nr=val_int();
2283
str->set_int(nr, unsigned_flag, &my_charset_bin);
2286
case DECIMAL_RESULT:
2288
my_decimal dec_buf, *dec_val= val_decimal(&dec_buf);
2291
my_decimal2string(E_DEC_FATAL_ERROR, dec_val, 0, 0, 0, str);
2296
double nr= val_real();
2298
return 0; /* purecov: inspected */
2299
str->set_real(nr,decimals,&my_charset_bin);
2306
for (uint i=0; i < arg_count ; i++)
2309
res=args[i]->val_str(str);
2313
res2= args[i]->val_str(res == str ? &tmp_value : str);
2316
int cmp= sortcmp(res,res2,collation.collation);
2317
if ((cmp_sign < 0 ? cmp : -cmp) < 0)
2321
if ((null_value= args[i]->null_value))
2324
res->set_charset(collation.collation);
2329
// This case should never be chosen
2333
return 0; // Keep compiler happy
2337
double Item_func_min_max::val_real()
2341
if (compare_as_dates)
2344
(void)cmp_datetimes(&result);
2345
return (double)result;
2347
for (uint i=0; i < arg_count ; i++)
2350
value= args[i]->val_real();
2353
double tmp= args[i]->val_real();
2354
if (!args[i]->null_value && (tmp < value ? cmp_sign : -cmp_sign) > 0)
2357
if ((null_value= args[i]->null_value))
2364
int64_t Item_func_min_max::val_int()
2368
if (compare_as_dates)
2371
(void)cmp_datetimes(&result);
2372
return (int64_t)result;
2374
for (uint i=0; i < arg_count ; i++)
2377
value=args[i]->val_int();
2380
int64_t tmp=args[i]->val_int();
2381
if (!args[i]->null_value && (tmp < value ? cmp_sign : -cmp_sign) > 0)
2384
if ((null_value= args[i]->null_value))
2391
my_decimal *Item_func_min_max::val_decimal(my_decimal *dec)
2394
my_decimal tmp_buf, *tmp, *res= NULL;
2396
if (compare_as_dates)
2399
(void)cmp_datetimes(&value);
2400
uint64_t2decimal(value, dec);
2403
for (uint i=0; i < arg_count ; i++)
2406
res= args[i]->val_decimal(dec);
2409
tmp= args[i]->val_decimal(&tmp_buf); // Zero if NULL
2410
if (tmp && (my_decimal_cmp(tmp, res) * cmp_sign) < 0)
2412
if (tmp == &tmp_buf)
2414
/* Move value out of tmp_buf as this will be reused on next loop */
2415
my_decimal2decimal(tmp, dec);
2422
if ((null_value= args[i]->null_value))
2432
int64_t Item_func_length::val_int()
2435
String *res=args[0]->val_str(&value);
2439
return 0; /* purecov: inspected */
2442
return (int64_t) res->length();
2446
int64_t Item_func_char_length::val_int()
2449
String *res=args[0]->val_str(&value);
2453
return 0; /* purecov: inspected */
2456
return (int64_t) res->numchars();
2460
int64_t Item_func_coercibility::val_int()
2464
return (int64_t) args[0]->collation.derivation;
2468
void Item_func_locate::fix_length_and_dec()
2470
max_length= MY_INT32_NUM_DECIMAL_DIGITS;
2471
agg_arg_charsets(cmp_collation, args, 2, MY_COLL_CMP_CONV, 1);
2475
int64_t Item_func_locate::val_int()
2478
String *a=args[0]->val_str(&value1);
2479
String *b=args[1]->val_str(&value2);
2483
return 0; /* purecov: inspected */
2486
/* must be int64_t to avoid truncation */
2493
start0= start= args[2]->val_int() - 1;
2495
if ((start < 0) || (start > a->length()))
2498
/* start is now sufficiently valid to pass to charpos function */
2499
start= a->charpos((int) start);
2501
if (start + b->length() > a->length())
2505
if (!b->length()) // Found empty string at start
2508
if (!cmp_collation.collation->coll->instr(cmp_collation.collation,
2510
(uint) (a->length()-start),
2511
b->ptr(), b->length(),
2514
return (int64_t) match.mb_len + start0 + 1;
2518
void Item_func_locate::print(String *str, enum_query_type query_type)
2520
str->append(STRING_WITH_LEN("locate("));
2521
args[1]->print(str, query_type);
2523
args[0]->print(str, query_type);
2527
args[2]->print(str, query_type);
2533
int64_t Item_func_field::val_int()
2537
if (cmp_type == STRING_RESULT)
2540
if (!(field= args[0]->val_str(&value)))
2542
for (uint i=1 ; i < arg_count ; i++)
2544
String *tmp_value=args[i]->val_str(&tmp);
2545
if (tmp_value && !sortcmp(field,tmp_value,cmp_collation.collation))
2546
return (int64_t) (i);
2549
else if (cmp_type == INT_RESULT)
2551
int64_t val= args[0]->val_int();
2552
if (args[0]->null_value)
2554
for (uint i=1; i < arg_count ; i++)
2556
if (val == args[i]->val_int() && !args[i]->null_value)
2557
return (int64_t) (i);
2560
else if (cmp_type == DECIMAL_RESULT)
2562
my_decimal dec_arg_buf, *dec_arg,
2563
dec_buf, *dec= args[0]->val_decimal(&dec_buf);
2564
if (args[0]->null_value)
2566
for (uint i=1; i < arg_count; i++)
2568
dec_arg= args[i]->val_decimal(&dec_arg_buf);
2569
if (!args[i]->null_value && !my_decimal_cmp(dec_arg, dec))
2570
return (int64_t) (i);
2575
double val= args[0]->val_real();
2576
if (args[0]->null_value)
2578
for (uint i=1; i < arg_count ; i++)
2580
if (val == args[i]->val_real() && !args[i]->null_value)
2581
return (int64_t) (i);
2588
void Item_func_field::fix_length_and_dec()
2590
maybe_null=0; max_length=3;
2591
cmp_type= args[0]->result_type();
2592
for (uint i=1; i < arg_count ; i++)
2593
cmp_type= item_cmp_type(cmp_type, args[i]->result_type());
2594
if (cmp_type == STRING_RESULT)
2595
agg_arg_charsets(cmp_collation, args, arg_count, MY_COLL_CMP_CONV, 1);
2599
int64_t Item_func_ascii::val_int()
2602
String *res=args[0]->val_str(&value);
2609
return (int64_t) (res->length() ? (uchar) (*res)[0] : (uchar) 0);
2612
int64_t Item_func_ord::val_int()
2615
String *res=args[0]->val_str(&value);
2622
if (!res->length()) return 0;
2624
if (use_mb(res->charset()))
2626
register const char *str=res->ptr();
2627
register uint32 n=0, l=my_ismbchar(res->charset(),str,str+res->length());
2629
return (int64_t)((uchar) *str);
2631
n=(n<<8)|(uint32)((uchar) *str++);
2635
return (int64_t) ((uchar) (*res)[0]);
2638
/* Search after a string in a string of strings separated by ',' */
2639
/* Returns number of found type >= 1 or 0 if not found */
2640
/* This optimizes searching in enums to bit testing! */
2642
void Item_func_find_in_set::fix_length_and_dec()
2645
max_length=3; // 1-999
2646
if (args[0]->const_item() && args[1]->type() == FIELD_ITEM)
2648
Field *field= ((Item_field*) args[1])->field;
2649
if (field->real_type() == MYSQL_TYPE_SET)
2651
String *find=args[0]->val_str(&value);
2654
enum_value= find_type(((Field_enum*) field)->typelib,find->ptr(),
2658
enum_bit=1LL << (enum_value-1);
2662
agg_arg_charsets(cmp_collation, args, 2, MY_COLL_CMP_CONV, 1);
2665
static const char separator=',';
2667
int64_t Item_func_find_in_set::val_int()
2672
uint64_t tmp=(uint64_t) args[1]->val_int();
2673
if (!(null_value=args[1]->null_value || args[0]->null_value))
2681
String *find=args[0]->val_str(&value);
2682
String *buffer=args[1]->val_str(&value2);
2683
if (!find || !buffer)
2686
return 0; /* purecov: inspected */
2691
if ((diff=buffer->length() - find->length()) >= 0)
2694
CHARSET_INFO *cs= cmp_collation.collation;
2695
const char *str_begin= buffer->ptr();
2696
const char *str_end= buffer->ptr();
2697
const char *real_end= str_end+buffer->length();
2698
const uchar *find_str= (const uchar *) find->ptr();
2699
uint find_str_len= find->length();
2704
if ((symbol_len= cs->cset->mb_wc(cs, &wc, (uchar*) str_end,
2705
(uchar*) real_end)) > 0)
2707
const char *substr_end= str_end + symbol_len;
2708
bool is_last_item= (substr_end == real_end);
2709
bool is_separator= (wc == (my_wc_t) separator);
2710
if (is_separator || is_last_item)
2713
if (is_last_item && !is_separator)
2714
str_end= substr_end;
2715
if (!my_strnncoll(cs, (const uchar *) str_begin,
2716
str_end - str_begin,
2717
find_str, find_str_len))
2718
return (int64_t) position;
2720
str_begin= substr_end;
2722
str_end= substr_end;
2724
else if (str_end - str_begin == 0 &&
2725
find_str_len == 0 &&
2726
wc == (my_wc_t) separator)
2727
return (int64_t) ++position;
2735
int64_t Item_func_bit_count::val_int()
2738
uint64_t value= (uint64_t) args[0]->val_int();
2739
if ((null_value= args[0]->null_value))
2740
return 0; /* purecov: inspected */
2741
return (int64_t) my_count_bits(value);
2745
/****************************************************************************
2746
** Functions to handle dynamic loadable functions
2747
** Original source by: Alexis Mikhailov <root@medinf.chuvashia.su>
2748
** Rewritten by monty.
2749
****************************************************************************/
2751
void udf_handler::cleanup()
2757
if (u_d->func_deinit != NULL)
2759
Udf_func_deinit deinit= u_d->func_deinit;
2765
if (buffers) // Because of bug in ecc
2773
udf_handler::fix_fields(THD *thd, Item_result_field *func,
2774
uint arg_count, Item **arguments)
2776
uchar buff[STACK_BUFF_ALLOC]; // Max argument in function
2778
if (check_stack_overrun(thd, STACK_MIN_SIZE, buff))
2779
return(true); // Fatal error flag is set!
2781
udf_func *tmp_udf=find_udf(u_d->name.str,(uint) u_d->name.length);
2785
my_error(ER_CANT_FIND_UDF, MYF(0), u_d->name.str, errno);
2791
/* Fix all arguments */
2793
used_tables_cache=0;
2796
if ((f_args.arg_count=arg_count))
2798
if (!(f_args.arg_type= (Item_result*)
2799
sql_alloc(f_args.arg_count*sizeof(Item_result))))
2805
Item **arg,**arg_end;
2806
for (i=0, arg=arguments, arg_end=arguments+arg_count;
2810
if (!(*arg)->fixed &&
2811
(*arg)->fix_fields(thd, arg))
2813
// we can't assign 'item' before, because fix_fields() can change arg
2815
if (item->check_cols(1))
2818
TODO: We should think about this. It is not always
2819
right way just to set an UDF result to return my_charset_bin
2820
if one argument has binary sorting order.
2821
The result collation should be calculated according to arguments
2822
derivations in some cases and should not in other cases.
2823
Moreover, some arguments can represent a numeric input
2824
which doesn't effect the result character set and collation.
2825
There is no a general rule for UDF. Everything depends on
2826
the particular user defined function.
2828
if (item->collation.collation->state & MY_CS_BINSORT)
2829
func->collation.set(&my_charset_bin);
2830
if (item->maybe_null)
2832
func->with_sum_func= func->with_sum_func || item->with_sum_func;
2833
used_tables_cache|=item->used_tables();
2834
const_item_cache&=item->const_item();
2835
f_args.arg_type[i]=item->result_type();
2837
//TODO: why all following memory is not allocated with 1 call of sql_alloc?
2838
if (!(buffers=new String[arg_count]) ||
2839
!(f_args.args= (char**) sql_alloc(arg_count * sizeof(char *))) ||
2840
!(f_args.lengths= (ulong*) sql_alloc(arg_count * sizeof(long))) ||
2841
!(f_args.maybe_null= (char*) sql_alloc(arg_count * sizeof(char))) ||
2842
!(num_buffer= (char*) sql_alloc(arg_count *
2843
ALIGN_SIZE(sizeof(double)))) ||
2844
!(f_args.attributes= (char**) sql_alloc(arg_count * sizeof(char *))) ||
2845
!(f_args.attribute_lengths= (ulong*) sql_alloc(arg_count *
2851
func->fix_length_and_dec();
2852
initid.max_length=func->max_length;
2853
initid.maybe_null=func->maybe_null;
2854
initid.const_item=const_item_cache;
2855
initid.decimals=func->decimals;
2860
char init_msg_buff[MYSQL_ERRMSG_SIZE];
2861
char *to=num_buffer;
2862
for (uint i=0; i < arg_count; i++)
2865
For a constant argument i, args->args[i] points to the argument value.
2866
For non-constant, args->args[i] is NULL.
2868
f_args.args[i]= NULL; /* Non-const unless updated below. */
2870
f_args.lengths[i]= arguments[i]->max_length;
2871
f_args.maybe_null[i]= (char) arguments[i]->maybe_null;
2872
f_args.attributes[i]= arguments[i]->name;
2873
f_args.attribute_lengths[i]= arguments[i]->name_length;
2875
if (arguments[i]->const_item())
2877
switch (arguments[i]->result_type())
2880
case DECIMAL_RESULT:
2882
String *res= arguments[i]->val_str(&buffers[i]);
2883
if (arguments[i]->null_value)
2885
f_args.args[i]= (char*) res->c_ptr();
2886
f_args.lengths[i]= res->length();
2890
*((int64_t*) to)= arguments[i]->val_int();
2891
if (arguments[i]->null_value)
2894
to+= ALIGN_SIZE(sizeof(int64_t));
2897
*((double*) to)= arguments[i]->val_real();
2898
if (arguments[i]->null_value)
2901
to+= ALIGN_SIZE(sizeof(double));
2905
// This case should never be chosen
2911
Udf_func_init init= u_d->func_init;
2912
if ((error=(uchar) init(&initid, &f_args, init_msg_buff)))
2914
my_error(ER_CANT_INITIALIZE_UDF, MYF(0),
2915
u_d->name.str, init_msg_buff);
2918
func->max_length=min(initid.max_length,MAX_BLOB_WIDTH);
2919
func->maybe_null=initid.maybe_null;
2920
const_item_cache=initid.const_item;
2922
Keep used_tables_cache in sync with const_item_cache.
2923
See the comment in Item_udf_func::update_used tables.
2925
if (!const_item_cache && !used_tables_cache)
2926
used_tables_cache= RAND_TABLE_BIT;
2927
func->decimals=min(initid.decimals,NOT_FIXED_DEC);
2932
my_error(ER_CANT_INITIALIZE_UDF, MYF(0),
2933
u_d->name.str, ER(ER_UNKNOWN_ERROR));
2940
bool udf_handler::get_arguments()
2943
return 1; // Got an error earlier
2944
char *to= num_buffer;
2946
for (uint i=0; i < f_args.arg_count; i++)
2949
switch (f_args.arg_type[i]) {
2951
case DECIMAL_RESULT:
2953
String *res=args[i]->val_str(&buffers[str_count++]);
2954
if (!(args[i]->null_value))
2956
f_args.args[i]= (char*) res->ptr();
2957
f_args.lengths[i]= res->length();
2962
*((int64_t*) to) = args[i]->val_int();
2963
if (!args[i]->null_value)
2966
to+= ALIGN_SIZE(sizeof(int64_t));
2970
*((double*) to)= args[i]->val_real();
2971
if (!args[i]->null_value)
2974
to+= ALIGN_SIZE(sizeof(double));
2979
// This case should never be chosen
2989
(String*)NULL in case of NULL values
2991
String *udf_handler::val_str(String *str,String *save_str)
2993
uchar is_null_tmp=0;
2996
if (get_arguments())
2998
char * (*func)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *)=
2999
(char* (*)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *))
3002
if ((res_length=str->alloced_length()) < MAX_FIELD_WIDTH)
3003
{ // This happens VERY seldom
3004
if (str->alloc(MAX_FIELD_WIDTH))
3010
char *res=func(&initid, &f_args, (char*) str->ptr(), &res_length,
3011
&is_null_tmp, &error);
3012
if (is_null_tmp || !res || error) // The !res is for safety
3016
if (res == str->ptr())
3018
str->length(res_length);
3021
save_str->set(res, res_length, str->charset());
3027
For the moment, UDF functions are returning DECIMAL values as strings
3030
my_decimal *udf_handler::val_decimal(my_bool *null_value, my_decimal *dec_buf)
3032
char buf[DECIMAL_MAX_STR_LENGTH+1], *end;
3033
ulong res_length= DECIMAL_MAX_STR_LENGTH;
3035
if (get_arguments())
3040
char *(*func)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *)=
3041
(char* (*)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *))
3044
char *res= func(&initid, &f_args, buf, &res_length, &is_null, &error);
3045
if (is_null || error)
3050
end= res+ res_length;
3051
str2my_decimal(E_DEC_FATAL_ERROR, res, dec_buf, &end);
3056
void Item_udf_func::cleanup()
3059
Item_func::cleanup();
3063
void Item_udf_func::print(String *str, enum_query_type query_type)
3065
str->append(func_name());
3067
for (uint i=0 ; i < arg_count ; i++)
3071
args[i]->print_item_w_name(str, query_type);
3077
double Item_func_udf_float::val_real()
3080
return(udf.val(&null_value));
3084
String *Item_func_udf_float::val_str(String *str)
3087
double nr= val_real();
3089
return 0; /* purecov: inspected */
3090
str->set_real(nr,decimals,&my_charset_bin);
3095
int64_t Item_func_udf_int::val_int()
3098
return(udf.val_int(&null_value));
3102
String *Item_func_udf_int::val_str(String *str)
3105
int64_t nr=val_int();
3108
str->set_int(nr, unsigned_flag, &my_charset_bin);
3113
int64_t Item_func_udf_decimal::val_int()
3115
my_decimal dec_buf, *dec= udf.val_decimal(&null_value, &dec_buf);
3119
my_decimal2int(E_DEC_FATAL_ERROR, dec, unsigned_flag, &result);
3124
double Item_func_udf_decimal::val_real()
3126
my_decimal dec_buf, *dec= udf.val_decimal(&null_value, &dec_buf);
3130
my_decimal2double(E_DEC_FATAL_ERROR, dec, &result);
3135
my_decimal *Item_func_udf_decimal::val_decimal(my_decimal *dec_buf)
3138
return(udf.val_decimal(&null_value, dec_buf));
3142
String *Item_func_udf_decimal::val_str(String *str)
3144
my_decimal dec_buf, *dec= udf.val_decimal(&null_value, &dec_buf);
3147
if (str->length() < DECIMAL_MAX_STR_LENGTH)
3148
str->length(DECIMAL_MAX_STR_LENGTH);
3149
my_decimal_round(E_DEC_FATAL_ERROR, dec, decimals, false, &dec_buf);
3150
my_decimal2string(E_DEC_FATAL_ERROR, &dec_buf, 0, 0, '0', str);
3155
void Item_func_udf_decimal::fix_length_and_dec()
3157
fix_num_length_and_dec();
3161
/* Default max_length is max argument length */
3163
void Item_func_udf_str::fix_length_and_dec()
3166
for (uint i = 0; i < arg_count; i++)
3167
set_if_bigger(max_length,args[i]->max_length);
3171
String *Item_func_udf_str::val_str(String *str)
3174
String *res=udf.val_str(str,&str_value);
3182
This has to come last in the udf_handler methods, or C for AIX
3183
version 6.0.0.0 fails to compile with debugging enabled. (Yes, really.)
3186
udf_handler::~udf_handler()
3188
/* Everything should be properly cleaned up by this moment. */
3189
assert(not_original || !(initialized || buffers));
3196
pthread_mutex_t LOCK_user_locks;
3197
static HASH hash_user_locks;
3199
class User_level_lock
3207
pthread_cond_t cond;
3208
my_thread_id thread_id;
3209
void set_thread(THD *thd) { thread_id= thd->thread_id; }
3211
User_level_lock(const uchar *key_arg,uint length, ulong id)
3212
:key_length(length),count(1),locked(1), thread_id(id)
3214
key= (uchar*) my_memdup(key_arg,length,MYF(0));
3215
pthread_cond_init(&cond,NULL);
3218
if (my_hash_insert(&hash_user_locks,(uchar*) this))
3220
my_free(key,MYF(0));
3229
hash_delete(&hash_user_locks,(uchar*) this);
3230
my_free(key, MYF(0));
3232
pthread_cond_destroy(&cond);
3234
inline bool initialized() { return key != 0; }
3235
friend void item_user_lock_release(User_level_lock *ull);
3236
friend uchar *ull_get_key(const User_level_lock *ull, size_t *length,
3240
uchar *ull_get_key(const User_level_lock *ull, size_t *length,
3241
my_bool not_used __attribute__((unused)))
3243
*length= ull->key_length;
3248
static bool item_user_lock_inited= 0;
3250
void item_user_lock_init(void)
3252
pthread_mutex_init(&LOCK_user_locks,MY_MUTEX_INIT_SLOW);
3253
hash_init(&hash_user_locks,system_charset_info,
3254
16,0,0,(hash_get_key) ull_get_key,NULL,0);
3255
item_user_lock_inited= 1;
3258
void item_user_lock_free(void)
3260
if (item_user_lock_inited)
3262
item_user_lock_inited= 0;
3263
hash_free(&hash_user_locks);
3264
pthread_mutex_destroy(&LOCK_user_locks);
3268
void item_user_lock_release(User_level_lock *ull)
3273
pthread_cond_signal(&ull->cond);
3279
Wait until we are at or past the given position in the master binlog
3283
int64_t Item_master_pos_wait::val_int()
3286
THD* thd = current_thd;
3287
String *log_name = args[0]->val_str(&value);
3291
if (thd->slave_thread || !log_name || !log_name->length())
3296
#ifdef HAVE_REPLICATION
3297
int64_t pos = (ulong)args[1]->val_int();
3298
int64_t timeout = (arg_count==3) ? args[2]->val_int() : 0 ;
3299
if ((event_count = active_mi->rli.wait_for_pos(thd, log_name, pos, timeout)) == -2)
3309
void debug_sync_point(const char* lock_name, uint lock_timeout)
3316
int64_t Item_func_last_insert_id::val_int()
3318
THD *thd= current_thd;
3322
int64_t value= args[0]->val_int();
3323
null_value= args[0]->null_value;
3325
LAST_INSERT_ID(X) must affect the client's mysql_insert_id() as
3326
documented in the manual. We don't want to touch
3327
first_successful_insert_id_in_cur_stmt because it would make
3328
LAST_INSERT_ID(X) take precedence over an generated auto_increment
3331
thd->arg_of_last_insert_id_function= true;
3332
thd->first_successful_insert_id_in_prev_stmt= value;
3335
return thd->read_first_successful_insert_id_in_prev_stmt();
3339
bool Item_func_last_insert_id::fix_fields(THD *thd, Item **ref)
3341
return Item_int_func::fix_fields(thd, ref);
3345
/* This function is just used to test speed of different functions */
3347
int64_t Item_func_benchmark::val_int()
3350
char buff[MAX_FIELD_WIDTH];
3351
String tmp(buff,sizeof(buff), &my_charset_bin);
3352
my_decimal tmp_decimal;
3353
THD *thd=current_thd;
3354
uint64_t loop_count;
3356
loop_count= (uint64_t) args[0]->val_int();
3358
if (args[0]->null_value ||
3359
(!args[0]->unsigned_flag && (((int64_t) loop_count) < 0)))
3361
if (!args[0]->null_value)
3364
llstr(((int64_t) loop_count), buff);
3365
push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
3366
ER_WRONG_VALUE_FOR_TYPE, ER(ER_WRONG_VALUE_FOR_TYPE),
3367
"count", buff, "benchmark");
3375
for (uint64_t loop=0 ; loop < loop_count && !thd->killed; loop++)
3377
switch (args[1]->result_type()) {
3379
(void) args[1]->val_real();
3382
(void) args[1]->val_int();
3385
(void) args[1]->val_str(&tmp);
3387
case DECIMAL_RESULT:
3388
(void) args[1]->val_decimal(&tmp_decimal);
3392
// This case should never be chosen
3401
void Item_func_benchmark::print(String *str, enum_query_type query_type)
3403
str->append(STRING_WITH_LEN("benchmark("));
3404
args[0]->print(str, query_type);
3406
args[1]->print(str, query_type);
3410
#define extra_size sizeof(double)
3412
static user_var_entry *get_variable(HASH *hash, LEX_STRING &name,
3413
bool create_if_not_exists)
3415
user_var_entry *entry;
3417
if (!(entry = (user_var_entry*) hash_search(hash, (uchar*) name.str,
3419
create_if_not_exists)
3421
uint size=ALIGN_SIZE(sizeof(user_var_entry))+name.length+1+extra_size;
3422
if (!hash_inited(hash))
3424
if (!(entry = (user_var_entry*) my_malloc(size,MYF(MY_WME | ME_FATALERROR))))
3426
entry->name.str=(char*) entry+ ALIGN_SIZE(sizeof(user_var_entry))+
3428
entry->name.length=name.length;
3431
entry->update_query_id=0;
3432
entry->collation.set(NULL, DERIVATION_IMPLICIT, 0);
3433
entry->unsigned_flag= 0;
3435
If we are here, we were called from a SET or a query which sets a
3436
variable. Imagine it is this:
3437
INSERT INTO t SELECT @a:=10, @a:=@a+1.
3438
Then when we have a Item_func_get_user_var (because of the @a+1) so we
3439
think we have to write the value of @a to the binlog. But before that,
3440
we have a Item_func_set_user_var to create @a (@a:=10), in this we mark
3441
the variable as "already logged" (line below) so that it won't be logged
3442
by Item_func_get_user_var (because that's not necessary).
3444
entry->used_query_id=current_thd->query_id;
3445
entry->type=STRING_RESULT;
3446
memcpy(entry->name.str, name.str, name.length+1);
3447
if (my_hash_insert(hash,(uchar*) entry))
3449
my_free((char*) entry,MYF(0));
3457
When a user variable is updated (in a SET command or a query like
3461
bool Item_func_set_user_var::fix_fields(THD *thd, Item **ref)
3464
/* fix_fields will call Item_func_set_user_var::fix_length_and_dec */
3465
if (Item_func::fix_fields(thd, ref) ||
3466
!(entry= get_variable(&thd->user_vars, name, 1)))
3469
Remember the last query which updated it, this way a query can later know
3470
if this variable is a constant item in the query (it is if update_query_id
3471
is different from query_id).
3473
entry->update_query_id= thd->query_id;
3475
As it is wrong and confusing to associate any
3476
character set with NULL, @a should be latin2
3477
after this query sequence:
3479
SET @a=_latin2'string';
3482
I.e. the second query should not change the charset
3483
to the current default value, but should keep the
3484
original value assigned during the first query.
3485
In order to do it, we don't copy charset
3486
from the argument if the argument is NULL
3487
and the variable has previously been initialized.
3489
null_item= (args[0]->type() == NULL_ITEM);
3490
if (!entry->collation.collation || !null_item)
3491
entry->collation.set(args[0]->collation.collation, DERIVATION_IMPLICIT);
3492
collation.set(entry->collation.collation, DERIVATION_IMPLICIT);
3493
cached_result_type= args[0]->result_type();
3499
Item_func_set_user_var::fix_length_and_dec()
3501
maybe_null=args[0]->maybe_null;
3502
max_length=args[0]->max_length;
3503
decimals=args[0]->decimals;
3504
collation.set(args[0]->collation.collation, DERIVATION_IMPLICIT);
3509
Mark field in read_map
3512
This is used by filesort to register used fields in a a temporary
3513
column read set or to register used fields in a view
3516
bool Item_func_set_user_var::register_field_in_read_map(uchar *arg)
3520
TABLE *table= (TABLE *) arg;
3521
if (result_field->table == table || !table)
3522
bitmap_set_bit(result_field->table->read_set, result_field->field_index);
3529
Set value to user variable.
3531
@param entry pointer to structure representing variable
3532
@param set_null should we set NULL value ?
3533
@param ptr pointer to buffer with new value
3534
@param length length of new value
3535
@param type type of new value
3536
@param cs charset info for new value
3537
@param dv derivation for new value
3538
@param unsigned_arg indiates if a value of type INT_RESULT is unsigned
3540
@note Sets error and fatal error if allocation fails.
3549
update_hash(user_var_entry *entry, bool set_null, void *ptr, uint length,
3550
Item_result type, CHARSET_INFO *cs, Derivation dv,
3555
char *pos= (char*) entry+ ALIGN_SIZE(sizeof(user_var_entry));
3556
if (entry->value && entry->value != pos)
3557
my_free(entry->value,MYF(0));
3563
if (type == STRING_RESULT)
3564
length++; // Store strings with end \0
3565
if (length <= extra_size)
3567
/* Save value in value struct */
3568
char *pos= (char*) entry+ ALIGN_SIZE(sizeof(user_var_entry));
3569
if (entry->value != pos)
3572
my_free(entry->value,MYF(0));
3578
/* Allocate variable */
3579
if (entry->length != length)
3581
char *pos= (char*) entry+ ALIGN_SIZE(sizeof(user_var_entry));
3582
if (entry->value == pos)
3584
entry->value= (char*) my_realloc(entry->value, length,
3585
MYF(MY_ALLOW_ZERO_PTR | MY_WME |
3591
if (type == STRING_RESULT)
3593
length--; // Fix length change above
3594
entry->value[length]= 0; // Store end \0
3596
memcpy(entry->value,ptr,length);
3597
if (type == DECIMAL_RESULT)
3598
((my_decimal*)entry->value)->fix_buffer_pointer();
3599
entry->length= length;
3600
entry->collation.set(cs, dv);
3601
entry->unsigned_flag= unsigned_arg;
3609
Item_func_set_user_var::update_hash(void *ptr, uint length,
3610
Item_result res_type,
3611
CHARSET_INFO *cs, Derivation dv,
3615
If we set a variable explicitely to NULL then keep the old
3616
result type of the variable
3618
if ((null_value= args[0]->null_value) && null_item)
3619
res_type= entry->type; // Don't change type of item
3620
if (::update_hash(entry, (null_value= args[0]->null_value),
3621
ptr, length, res_type, cs, dv, unsigned_arg))
3630
/** Get the value of a variable as a double. */
3632
double user_var_entry::val_real(my_bool *null_value)
3634
if ((*null_value= (value == 0)))
3639
return *(double*) value;
3641
return (double) *(int64_t*) value;
3642
case DECIMAL_RESULT:
3645
my_decimal2double(E_DEC_FATAL_ERROR, (my_decimal *)value, &result);
3649
return my_atof(value); // This is null terminated
3651
assert(1); // Impossible
3654
return 0.0; // Impossible
3658
/** Get the value of a variable as an integer. */
3660
int64_t user_var_entry::val_int(my_bool *null_value) const
3662
if ((*null_value= (value == 0)))
3667
return (int64_t) *(double*) value;
3669
return *(int64_t*) value;
3670
case DECIMAL_RESULT:
3673
my_decimal2int(E_DEC_FATAL_ERROR, (my_decimal *)value, 0, &result);
3679
return my_strtoll10(value, (char**) 0, &error);// String is null terminated
3682
assert(1); // Impossible
3685
return 0LL; // Impossible
3689
/** Get the value of a variable as a string. */
3691
String *user_var_entry::val_str(my_bool *null_value, String *str,
3694
if ((*null_value= (value == 0)))
3699
str->set_real(*(double*) value, decimals, &my_charset_bin);
3703
str->set(*(int64_t*) value, &my_charset_bin);
3705
str->set(*(uint64_t*) value, &my_charset_bin);
3707
case DECIMAL_RESULT:
3708
my_decimal2string(E_DEC_FATAL_ERROR, (my_decimal *)value, 0, 0, 0, str);
3711
if (str->copy(value, length, collation.collation))
3712
str= 0; // EOM error
3714
assert(1); // Impossible
3720
/** Get the value of a variable as a decimal. */
3722
my_decimal *user_var_entry::val_decimal(my_bool *null_value, my_decimal *val)
3724
if ((*null_value= (value == 0)))
3729
double2my_decimal(E_DEC_FATAL_ERROR, *(double*) value, val);
3732
int2my_decimal(E_DEC_FATAL_ERROR, *(int64_t*) value, 0, val);
3734
case DECIMAL_RESULT:
3735
val= (my_decimal *)value;
3738
str2my_decimal(E_DEC_FATAL_ERROR, value, length, collation.collation, val);
3741
assert(1); // Impossible
3748
This functions is invoked on SET \@variable or
3749
\@variable:= expression.
3751
Evaluate (and check expression), store results.
3754
For now it always return OK. All problem with value evaluating
3755
will be caught by thd->is_error() check in sql_set_variables().
3762
Item_func_set_user_var::check(bool use_result_field)
3764
if (use_result_field && !result_field)
3765
use_result_field= false;
3767
switch (cached_result_type) {
3770
save_result.vreal= use_result_field ? result_field->val_real() :
3771
args[0]->val_real();
3776
save_result.vint= use_result_field ? result_field->val_int() :
3778
unsigned_flag= use_result_field ? ((Field_num*)result_field)->unsigned_flag:
3779
args[0]->unsigned_flag;
3784
save_result.vstr= use_result_field ? result_field->val_str(&value) :
3785
args[0]->val_str(&value);
3788
case DECIMAL_RESULT:
3790
save_result.vdec= use_result_field ?
3791
result_field->val_decimal(&decimal_buff) :
3792
args[0]->val_decimal(&decimal_buff);
3797
// This case should never be chosen
3806
This functions is invoked on
3807
SET \@variable or \@variable:= expression.
3810
We have to store the expression as such in the variable, independent of
3811
the value method used by the user
3821
Item_func_set_user_var::update()
3825
switch (cached_result_type) {
3828
res= update_hash((void*) &save_result.vreal,sizeof(save_result.vreal),
3829
REAL_RESULT, &my_charset_bin, DERIVATION_IMPLICIT, 0);
3834
res= update_hash((void*) &save_result.vint, sizeof(save_result.vint),
3835
INT_RESULT, &my_charset_bin, DERIVATION_IMPLICIT,
3841
if (!save_result.vstr) // Null value
3842
res= update_hash((void*) 0, 0, STRING_RESULT, &my_charset_bin,
3843
DERIVATION_IMPLICIT, 0);
3845
res= update_hash((void*) save_result.vstr->ptr(),
3846
save_result.vstr->length(), STRING_RESULT,
3847
save_result.vstr->charset(),
3848
DERIVATION_IMPLICIT, 0);
3851
case DECIMAL_RESULT:
3853
if (!save_result.vdec) // Null value
3854
res= update_hash((void*) 0, 0, DECIMAL_RESULT, &my_charset_bin,
3855
DERIVATION_IMPLICIT, 0);
3857
res= update_hash((void*) save_result.vdec,
3858
sizeof(my_decimal), DECIMAL_RESULT,
3859
&my_charset_bin, DERIVATION_IMPLICIT, 0);
3864
// This case should never be chosen
3872
double Item_func_set_user_var::val_real()
3876
update(); // Store expression
3877
return entry->val_real(&null_value);
3880
int64_t Item_func_set_user_var::val_int()
3884
update(); // Store expression
3885
return entry->val_int(&null_value);
3888
String *Item_func_set_user_var::val_str(String *str)
3892
update(); // Store expression
3893
return entry->val_str(&null_value, str, decimals);
3897
my_decimal *Item_func_set_user_var::val_decimal(my_decimal *val)
3901
update(); // Store expression
3902
return entry->val_decimal(&null_value, val);
3906
double Item_func_set_user_var::val_result()
3910
update(); // Store expression
3911
return entry->val_real(&null_value);
3914
int64_t Item_func_set_user_var::val_int_result()
3918
update(); // Store expression
3919
return entry->val_int(&null_value);
3922
String *Item_func_set_user_var::str_result(String *str)
3926
update(); // Store expression
3927
return entry->val_str(&null_value, str, decimals);
3931
my_decimal *Item_func_set_user_var::val_decimal_result(my_decimal *val)
3935
update(); // Store expression
3936
return entry->val_decimal(&null_value, val);
3940
void Item_func_set_user_var::print(String *str, enum_query_type query_type)
3942
str->append(STRING_WITH_LEN("(@"));
3943
str->append(name.str, name.length);
3944
str->append(STRING_WITH_LEN(":="));
3945
args[0]->print(str, query_type);
3950
void Item_func_set_user_var::print_as_stmt(String *str,
3951
enum_query_type query_type)
3953
str->append(STRING_WITH_LEN("set @"));
3954
str->append(name.str, name.length);
3955
str->append(STRING_WITH_LEN(":="));
3956
args[0]->print(str, query_type);
3960
bool Item_func_set_user_var::send(Protocol *protocol, String *str_arg)
3966
return protocol->store(result_field);
3968
return Item::send(protocol, str_arg);
3971
void Item_func_set_user_var::make_field(Send_field *tmp_field)
3975
result_field->make_field(tmp_field);
3976
assert(tmp_field->table_name != 0);
3978
tmp_field->col_name=Item::name; // Use user supplied name
3981
Item::make_field(tmp_field);
3986
Save the value of a user variable into a field
3990
field target field to save the value to
3991
no_conversion flag indicating whether conversions are allowed
3994
Save the function value into a field and update the user variable
3995
accordingly. If a result field is defined and the target field doesn't
3996
coincide with it then the value from the result field will be used as
3997
the new value of the user variable.
3999
The reason to have this method rather than simply using the result
4000
field in the val_xxx() methods is that the value from the result field
4001
not always can be used when the result field is defined.
4002
Let's consider the following cases:
4003
1) when filling a tmp table the result field is defined but the value of it
4004
is undefined because it has to be produced yet. Thus we can't use it.
4005
2) on execution of an INSERT ... SELECT statement the save_in_field()
4006
function will be called to fill the data in the new record. If the SELECT
4007
part uses a tmp table then the result field is defined and should be
4008
used in order to get the correct result.
4010
The difference between the SET_USER_VAR function and regular functions
4011
like CONCAT is that the Item_func objects for the regular functions are
4012
replaced by Item_field objects after the values of these functions have
4013
been stored in a tmp table. Yet an object of the Item_field class cannot
4014
be used to update a user variable.
4015
Due to this we have to handle the result field in a special way here and
4016
in the Item_func_set_user_var::send() function.
4023
int Item_func_set_user_var::save_in_field(Field *field, bool no_conversions,
4024
bool can_use_result_field)
4026
bool use_result_field= (!can_use_result_field ? 0 :
4027
(result_field && result_field != field));
4030
/* Update the value of the user variable */
4031
check(use_result_field);
4034
if (result_type() == STRING_RESULT ||
4035
(result_type() == REAL_RESULT && field->result_type() == STRING_RESULT))
4038
CHARSET_INFO *cs= collation.collation;
4039
char buff[MAX_FIELD_WIDTH]; // Alloc buffer for small columns
4040
str_value.set_quick(buff, sizeof(buff), cs);
4041
result= entry->val_str(&null_value, &str_value, decimals);
4045
str_value.set_quick(0, 0, cs);
4046
return set_field_to_null_with_conversions(field, no_conversions);
4049
/* NOTE: If null_value == false, "result" must be not NULL. */
4051
field->set_notnull();
4052
error=field->store(result->ptr(),result->length(),cs);
4053
str_value.set_quick(0, 0, cs);
4055
else if (result_type() == REAL_RESULT)
4057
double nr= entry->val_real(&null_value);
4059
return set_field_to_null(field);
4060
field->set_notnull();
4061
error=field->store(nr);
4063
else if (result_type() == DECIMAL_RESULT)
4065
my_decimal decimal_value;
4066
my_decimal *val= entry->val_decimal(&null_value, &decimal_value);
4068
return set_field_to_null(field);
4069
field->set_notnull();
4070
error=field->store_decimal(val);
4074
int64_t nr= entry->val_int(&null_value);
4076
return set_field_to_null_with_conversions(field, no_conversions);
4077
field->set_notnull();
4078
error=field->store(nr, unsigned_flag);
4085
Item_func_get_user_var::val_str(String *str)
4089
return((String*) 0); // No such variable
4090
return(var_entry->val_str(&null_value, str, decimals));
4094
double Item_func_get_user_var::val_real()
4098
return 0.0; // No such variable
4099
return (var_entry->val_real(&null_value));
4103
my_decimal *Item_func_get_user_var::val_decimal(my_decimal *dec)
4108
return var_entry->val_decimal(&null_value, dec);
4112
int64_t Item_func_get_user_var::val_int()
4116
return 0LL; // No such variable
4117
return (var_entry->val_int(&null_value));
4122
Get variable by name and, if necessary, put the record of variable
4123
use into the binary log.
4125
When a user variable is invoked from an update query (INSERT, UPDATE etc),
4126
stores this variable and its value in thd->user_var_events, so that it can be
4127
written to the binlog (will be written just before the query is written, see
4130
@param thd Current thread
4131
@param name Variable name
4132
@param[out] out_entry variable structure or NULL. The pointer is set
4133
regardless of whether function succeeded or not.
4138
1 Failed to put appropriate record into binary log
4142
int get_var_with_binlog(THD *thd, enum_sql_command sql_command,
4143
LEX_STRING &name, user_var_entry **out_entry)
4145
BINLOG_USER_VAR_EVENT *user_var_event;
4146
user_var_entry *var_entry;
4147
var_entry= get_variable(&thd->user_vars, name, 0);
4150
Any reference to user-defined variable which is done from stored
4151
function or trigger affects their execution and the execution of the
4152
calling statement. We must log all such variables even if they are
4153
not involved in table-updating statements.
4155
if (!(opt_bin_log &&
4156
(is_update_query(sql_command) || thd->in_sub_stmt)))
4158
*out_entry= var_entry;
4165
If the variable does not exist, it's NULL, but we want to create it so
4166
that it gets into the binlog (if it didn't, the slave could be
4167
influenced by a variable of the same name previously set by another
4169
We create it like if it had been explicitly set with SET before.
4170
The 'new' mimics what sql_yacc.yy does when 'SET @a=10;'.
4171
sql_set_variables() is what is called from 'case SQLCOM_SET_OPTION'
4172
in dispatch_command()). Instead of building a one-element list to pass to
4173
sql_set_variables(), we could instead manually call check() and update();
4174
this would save memory and time; but calling sql_set_variables() makes
4175
one unique place to maintain (sql_set_variables()).
4177
Manipulation with lex is necessary since free_underlaid_joins
4178
is going to release memory belonging to the main query.
4181
List<set_var_base> tmp_var_list;
4182
LEX *sav_lex= thd->lex, lex_tmp;
4185
tmp_var_list.push_back(new set_var_user(new Item_func_set_user_var(name,
4187
/* Create the variable */
4188
if (sql_set_variables(thd, &tmp_var_list))
4194
if (!(var_entry= get_variable(&thd->user_vars, name, 0)))
4197
else if (var_entry->used_query_id == thd->query_id ||
4198
mysql_bin_log.is_query_in_union(thd, var_entry->used_query_id))
4201
If this variable was already stored in user_var_events by this query
4202
(because it's used in more than one place in the query), don't store
4205
*out_entry= var_entry;
4211
First we need to store value of var_entry, when the next situation
4214
> insert into t1 values (@a), (@a:=@a+1), (@a:=@a+1);
4215
We have to write to binlog value @a= 1.
4217
We allocate the user_var_event on user_var_events_alloc pool, not on
4218
the this-statement-execution pool because in SPs user_var_event objects
4219
may need to be valid after current [SP] statement execution pool is
4222
size= ALIGN_SIZE(sizeof(BINLOG_USER_VAR_EVENT)) + var_entry->length;
4223
if (!(user_var_event= (BINLOG_USER_VAR_EVENT *)
4224
alloc_root(thd->user_var_events_alloc, size)))
4227
user_var_event->value= (char*) user_var_event +
4228
ALIGN_SIZE(sizeof(BINLOG_USER_VAR_EVENT));
4229
user_var_event->user_var_event= var_entry;
4230
user_var_event->type= var_entry->type;
4231
user_var_event->charset_number= var_entry->collation.collation->number;
4232
if (!var_entry->value)
4235
user_var_event->length= 0;
4236
user_var_event->value= 0;
4240
user_var_event->length= var_entry->length;
4241
memcpy(user_var_event->value, var_entry->value,
4244
/* Mark that this variable has been used by this query */
4245
var_entry->used_query_id= thd->query_id;
4246
if (insert_dynamic(&thd->user_var_events, (uchar*) &user_var_event))
4249
*out_entry= var_entry;
4253
*out_entry= var_entry;
4257
void Item_func_get_user_var::fix_length_and_dec()
4259
THD *thd=current_thd;
4262
decimals=NOT_FIXED_DEC;
4263
max_length=MAX_BLOB_WIDTH;
4265
error= get_var_with_binlog(thd, thd->lex->sql_command, name, &var_entry);
4268
If the variable didn't exist it has been created as a STRING-type.
4269
'var_entry' is NULL only if there occured an error during the call to
4270
get_var_with_binlog.
4274
m_cached_result_type= var_entry->type;
4275
unsigned_flag= var_entry->unsigned_flag;
4276
max_length= var_entry->length;
4278
collation.set(var_entry->collation);
4279
switch(m_cached_result_type) {
4281
max_length= DBL_DIG + 8;
4284
max_length= MAX_BIGINT_WIDTH;
4288
max_length= MAX_BLOB_WIDTH;
4290
case DECIMAL_RESULT:
4291
max_length= DECIMAL_MAX_STR_LENGTH;
4292
decimals= DECIMAL_MAX_SCALE;
4294
case ROW_RESULT: // Keep compiler happy
4302
collation.set(&my_charset_bin, DERIVATION_IMPLICIT);
4304
m_cached_result_type= STRING_RESULT;
4305
max_length= MAX_BLOB_WIDTH;
4310
bool Item_func_get_user_var::const_item() const
4312
return (!var_entry || current_thd->query_id != var_entry->update_query_id);
4316
enum Item_result Item_func_get_user_var::result_type() const
4318
return m_cached_result_type;
4322
void Item_func_get_user_var::print(String *str,
4323
enum_query_type query_type __attribute__((__unused__)))
4325
str->append(STRING_WITH_LEN("(@"));
4326
str->append(name.str,name.length);
4331
bool Item_func_get_user_var::eq(const Item *item,
4332
bool binary_cmp __attribute__((__unused__))) const
4334
/* Assume we don't have rtti */
4336
return 1; // Same item is same.
4337
/* Check if other type is also a get_user_var() object */
4338
if (item->type() != FUNC_ITEM ||
4339
((Item_func*) item)->functype() != functype())
4341
Item_func_get_user_var *other=(Item_func_get_user_var*) item;
4342
return (name.length == other->name.length &&
4343
!memcmp(name.str, other->name.str, name.length));
4347
bool Item_user_var_as_out_param::fix_fields(THD *thd, Item **ref)
4350
if (Item::fix_fields(thd, ref) ||
4351
!(entry= get_variable(&thd->user_vars, name, 1)))
4353
entry->type= STRING_RESULT;
4355
Let us set the same collation which is used for loading
4356
of fields in LOAD DATA INFILE.
4357
(Since Item_user_var_as_out_param is used only there).
4359
entry->collation.set(thd->variables.collation_database);
4360
entry->update_query_id= thd->query_id;
4365
void Item_user_var_as_out_param::set_null_value(CHARSET_INFO* cs)
4367
::update_hash(entry, true, 0, 0, STRING_RESULT, cs,
4368
DERIVATION_IMPLICIT, 0 /* unsigned_arg */);
4372
void Item_user_var_as_out_param::set_value(const char *str, uint length,
4375
::update_hash(entry, false, (void*)str, length, STRING_RESULT, cs,
4376
DERIVATION_IMPLICIT, 0 /* unsigned_arg */);
4380
double Item_user_var_as_out_param::val_real()
4387
int64_t Item_user_var_as_out_param::val_int()
4394
String* Item_user_var_as_out_param::val_str(String *str __attribute__((__unused__)))
4401
my_decimal* Item_user_var_as_out_param::val_decimal(my_decimal *decimal_buffer __attribute__((__unused__)))
4408
void Item_user_var_as_out_param::print(String *str,
4409
enum_query_type query_type __attribute__((__unused__)))
4412
str->append(name.str,name.length);
4416
Item_func_get_system_var::
4417
Item_func_get_system_var(sys_var *var_arg, enum_var_type var_type_arg,
4418
LEX_STRING *component_arg, const char *name_arg,
4419
size_t name_len_arg)
4420
:var(var_arg), var_type(var_type_arg), component(*component_arg)
4422
/* set_name() will allocate the name */
4423
set_name(name_arg, name_len_arg, system_charset_info);
4428
Item_func_get_system_var::fix_fields(THD *thd, Item **ref)
4433
Evaluate the system variable and substitute the result (a basic constant)
4434
instead of this item. If the variable can not be evaluated,
4435
the error is reported in sys_var::item().
4437
if (!(item= var->item(thd, var_type, &component)))
4438
return(1); // Impossible
4439
item->set_name(name, 0, system_charset_info); // don't allocate a new name
4440
thd->change_item_tree(ref, item);
4446
bool Item_func_get_system_var::is_written_to_binlog()
4448
return var->is_written_to_binlog(var_type);
4451
int64_t Item_func_bit_xor::val_int()
4454
uint64_t arg1= (uint64_t) args[0]->val_int();
4455
uint64_t arg2= (uint64_t) args[1]->val_int();
4456
if ((null_value= (args[0]->null_value || args[1]->null_value)))
4458
return (int64_t) (arg1 ^ arg2);
4462
/***************************************************************************
4464
****************************************************************************/
4467
Return value of an system variable base[.name] as a constant item.
4469
@param thd Thread handler
4470
@param var_type global / session
4471
@param name Name of base or system variable
4472
@param component Component.
4475
If component.str = 0 then the variable name is in 'name'
4483
Item *get_system_var(THD *thd, enum_var_type var_type, LEX_STRING name,
4484
LEX_STRING component)
4487
LEX_STRING *base_name, *component_name;
4491
base_name= &component;
4492
component_name= &name;
4497
component_name= &component; // Empty string
4500
if (!(var= find_sys_var(thd, base_name->str, base_name->length)))
4504
if (!var->is_struct())
4506
my_error(ER_VARIABLE_IS_NOT_STRUCT, MYF(0), base_name->str);
4511
set_if_smaller(component_name->length, MAX_SYS_VAR_LENGTH);
4513
return new Item_func_get_system_var(var, var_type, component_name,
4519
Check a user level lock.
4521
Sets null_value=true on error.
4526
0 Already taken, or error
4529
int64_t Item_func_is_free_lock::val_int()
4532
String *res=args[0]->val_str(&value);
4533
User_level_lock *ull;
4536
if (!res || !res->length())
4542
pthread_mutex_lock(&LOCK_user_locks);
4543
ull= (User_level_lock *) hash_search(&hash_user_locks, (uchar*) res->ptr(),
4544
(size_t) res->length());
4545
pthread_mutex_unlock(&LOCK_user_locks);
4546
if (!ull || !ull->locked)
4551
int64_t Item_func_is_used_lock::val_int()
4554
String *res=args[0]->val_str(&value);
4555
User_level_lock *ull;
4558
if (!res || !res->length())
4561
pthread_mutex_lock(&LOCK_user_locks);
4562
ull= (User_level_lock *) hash_search(&hash_user_locks, (uchar*) res->ptr(),
4563
(size_t) res->length());
4564
pthread_mutex_unlock(&LOCK_user_locks);
4565
if (!ull || !ull->locked)
4569
return ull->thread_id;
4573
int64_t Item_func_row_count::val_int()
4576
THD *thd= current_thd;
4578
return thd->row_count_func;
4581
int64_t Item_func_found_rows::val_int()
4584
THD *thd= current_thd;
4586
return thd->found_rows();